@Override protected String getTupleValue(Tuple t, int idx) { return new String(t.getBinary(idx)); } }
@Override public byte[] get(Tuple tuple) { return tuple.getBinary(position); } }
@Override public Map<String, byte[]> getValues(Tuple tuple, JSONObject message) { Map<String, byte[]> values = new HashMap<>(); values.put(column, tuple.getBinary(0)); return values; }
@Override public JSONObject get(Tuple tuple) { String s = null; try { s = new String(tuple.getBinary(position), Charsets.UTF_8); return (JSONObject) parser.get().parse(s); } catch (Exception e) { throw new IllegalStateException("Unable to parse " + s + " due to " + e.getMessage(), e); } } }
@Override public JSONObject generateMessage(Tuple tuple) { JSONObject message = null; if (messageFieldName == null) { byte[] data = tuple.getBinary(0); try { message = (JSONObject) parser.parse(new String(data, "UTF8")); message.put(getClass().getSimpleName().toLowerCase() + ".splitter.begin.ts", "" + System.currentTimeMillis()); } catch (ParseException | UnsupportedEncodingException e) { e.printStackTrace(); } } else { message = (JSONObject) tuple.getValueByField(messageFieldName); message.put(getClass().getSimpleName().toLowerCase() + ".splitter.begin.ts", "" + System.currentTimeMillis()); } return message; }
public Tuple toTuple(byte[] record) { Tuple ret = mock(Tuple.class); when(ret.getStringByField("topic")).thenReturn(sensorType); when(ret.getBinary(eq(0))).thenReturn(record); return ret; } }
byte[] keyObj = t.getBinary(KEY_INDEX); String keyStr = null; try {
@Test public void jsonFromPositionShouldReturnJSON() { Tuple tuple = mock(Tuple.class); when(tuple.getBinary(1)).thenReturn("{\"field\":\"value\"}".getBytes(UTF_8)); JSONObject expected = new JSONObject(); expected.put("field", "value"); MessageGetStrategy messageGetStrategy = MessageGetters.JSON_FROM_POSITION.get("1"); assertEquals(expected, messageGetStrategy.get(tuple)); }
@Test public void defaultJSONFromPositionShouldReturnJSON() { Tuple tuple = mock(Tuple.class); when(tuple.getBinary(0)).thenReturn("{\"field\":\"value\"}".getBytes(UTF_8)); JSONObject expected = new JSONObject(); expected.put("field", "value"); MessageGetStrategy messageGetStrategy = MessageGetters.DEFAULT_JSON_FROM_POSITION.get(); assertEquals(expected, messageGetStrategy.get(tuple)); }
@Test public void defaultBytesFromPositionShouldReturnBytes() { Tuple tuple = mock(Tuple.class); when(tuple.getBinary(0)).thenReturn("bytes".getBytes(UTF_8)); MessageGetStrategy messageGetStrategy = MessageGetters.DEFAULT_BYTES_FROM_POSITION.get(); assertEquals("bytes", new String((byte[]) messageGetStrategy.get(tuple), UTF_8)); }
@Test public void bytesFromPositionShouldReturnBytes() { Tuple tuple = mock(Tuple.class); when(tuple.getBinary(1)).thenReturn("bytes".getBytes(UTF_8)); MessageGetStrategy messageGetStrategy = MessageGetters.BYTES_FROM_POSITION.get("1"); assertEquals("bytes", new String((byte[]) messageGetStrategy.get(tuple), UTF_8)); }
@Test public void jsonFromPositionShouldThrowException() { exception.expect(IllegalStateException.class); Tuple tuple = mock(Tuple.class); when(tuple.getBinary(1)).thenReturn("{\"field\":".getBytes(UTF_8)); MessageGetStrategy messageGetStrategy = MessageGetters.JSON_FROM_POSITION.get("1"); messageGetStrategy.get(tuple); }
private static Tuple createTuple(Map<String, Object> kafkaFields, String metadata) throws Exception { List<Map.Entry<String, Object>> fields = new ArrayList<>(); for(Map.Entry<String, Object> kv : kafkaFields.entrySet()) { fields.add(kv); } Tuple t = mock(Tuple.class); Fields f = mock(Fields.class); when(f.size()).thenReturn(fields.size()+2); for(int i = 0;i < fields.size();++i) { when(f.get(eq(i + 2))).thenReturn(fields.get(i).getKey()); when(t.getValue(eq(i + 2))).thenReturn(fields.get(i).getValue()); } when(t.getFields()).thenReturn(f); when(t.getBinary(eq(MetadataUtil.KEY_INDEX))).thenReturn(metadata.getBytes()); return t; }
/** * If an invalid message is sent to indexing, the message should be handled as an error * and the topology should continue processing. */ @Test public void testMessageInvalid() throws Exception { FakeClock clock = new FakeClock(); // setup the bolt BulkMessageWriterBolt<IndexingConfigurations> bolt = new BulkMessageWriterBolt<IndexingConfigurations>( "zookeeperUrl", "INDEXING") .withBulkMessageWriter(bulkMessageWriter) .withMessageGetter(MessageGetters.JSON_FROM_POSITION.name()) .withMessageGetterField("message"); bolt.setCuratorFramework(client); bolt.setZKCache(cache); bolt.getConfigurations().updateSensorIndexingConfig(sensorType, new FileInputStream(sampleSensorIndexingConfigPath)); // initialize the bolt bolt.declareOutputFields(declarer); Map stormConf = new HashMap(); bolt.prepare(stormConf, topologyContext, outputCollector, clock); // execute a tuple that contains an invalid message byte[] invalidJSON = "this is not valid JSON".getBytes(); when(tuple.getBinary(0)).thenReturn(invalidJSON); bolt.execute(tuple); // the tuple should be handled as an error and ack'd verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), any()); verify(outputCollector, times(1)).ack(tuple); }
@Override protected String getTupleValue(Tuple t, int idx) { return new String(t.getBinary(idx)); } }
key = enrichmentSplitterBolt.getKey(tuple, sampleMessage); Assert.assertEquals(guid, key); when(tuple.getBinary(0)).thenReturn(sampleMessageString.getBytes()); JSONObject generatedMessage = enrichmentSplitterBolt.generateMessage(tuple); removeTimingFields(generatedMessage);