public void transformAndUpdate(JSONObject message, Context context, Map<String, Object>... sensorConfig) { Map<String, Object> currentValue = transform(message, context, sensorConfig); if(currentValue != null) { for(Map.Entry<String, Object> kv : currentValue.entrySet()) { if(kv.getValue() == null) { message.remove(kv.getKey()); } else { message.put(kv.getKey(), kv.getValue()); } } } }
@Test public void testSimpleMapping() throws IOException { SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(config)); FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null); Assert.assertNotNull(handler); Assert.assertEquals(ImmutableMap.of("protocol", "TCP") ,handler.transform(new JSONObject(ImmutableMap.of("protocol", 6)) , Context.EMPTY_CONTEXT() , c.getParserConfig() ) ); } }
@Test public void testComplexMapping() throws IOException { SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(complexConfig)); FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null); Assert.assertNotNull(handler); Assert.assertEquals(ImmutableMap.of("output", "field1=value1,field2=value2") ,handler.transform(new JSONObject(ImmutableMap.of("field1", "value1" ,"field2", "value2" ) ) , Context.EMPTY_CONTEXT() , c.getParserConfig() ) ); } @Test