Search in sources :

Example 6 with FieldTransformer

use of org.apache.metron.common.configuration.FieldTransformer in project metron by apache.

the class StellarTransformationTest method testConfigAll.

@Test
public void testConfigAll() throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(configAll));
    JSONObject input = new JSONObject();
    input.put("source.type", "test");
    for (FieldTransformer handler : c.getFieldTransformations()) {
        handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    }
    assertEquals(2, input.size());
    assertTrue(input.containsKey("new_field"));
    assertEquals("test", input.get("new_field"));
}
Also used : JSONObject(org.json.simple.JSONObject) FieldTransformer(org.apache.metron.common.configuration.FieldTransformer) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 7 with FieldTransformer

use of org.apache.metron.common.configuration.FieldTransformer in project metron by apache.

the class RemoveTransformationTest method testConditionalRemove.

@Test
public void testConditionalRemove() throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(removeConditionalConfig));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    {
        JSONObject input = new JSONObject(new HashMap<String, Object>() {

            {
                put("field1", "foo");
            }
        });
        handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
        // no removal happened because field2 does not exist
        assertTrue(input.containsKey("field1"));
        assertFalse(input.containsKey("field2"));
    }
    {
        JSONObject input = new JSONObject(new HashMap<String, Object>() {

            {
                put("field1", "foo");
                put("field2", "bar");
            }
        });
        handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
        // no removal happened because field2 != bar
        assertTrue(input.containsKey("field1"));
        assertTrue(input.containsKey("field2"));
    }
    {
        JSONObject input = new JSONObject(new HashMap<String, Object>() {

            {
                put("field1", "bar");
                put("field2", "foo");
            }
        });
        // removal of field1 happens because field2 exists and is 'bar'
        handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
        assertFalse(input.containsKey("field1"));
        assertTrue(input.containsKey("field2"));
    }
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) FieldTransformer(org.apache.metron.common.configuration.FieldTransformer) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 8 with FieldTransformer

use of org.apache.metron.common.configuration.FieldTransformer in project metron by apache.

the class RemoveTransformationTest method testUnconditionalRemove.

@Test
public void testUnconditionalRemove() throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(removeUnconditionalConfig));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    JSONObject input = new JSONObject(new HashMap<String, Object>() {

        {
            put("field1", "foo");
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertFalse(input.containsKey("field1"));
}
Also used : JSONObject(org.json.simple.JSONObject) FieldTransformer(org.apache.metron.common.configuration.FieldTransformer) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) JSONObject(org.json.simple.JSONObject) Test(org.junit.jupiter.api.Test)

Example 9 with FieldTransformer

use of org.apache.metron.common.configuration.FieldTransformer in project metron by apache.

the class SelectTransformationTest method testPreserveSystemFields.

@Test
public void testPreserveSystemFields() throws Exception {
    SensorParserConfig sensorConfig = SensorParserConfig.fromBytes(Bytes.toBytes(selectSingleFieldConfig));
    FieldTransformer handler = Iterables.getFirst(sensorConfig.getFieldTransformations(), null);
    JSONObject input = new JSONObject(new HashMap<String, Object>() {

        {
            put("timestamp", 12345);
            put("original_string", "foo,bar");
            put("source.type", "test");
            put("field1", "foo");
            put("field2", "bar");
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertTrue(input.containsKey("timestamp"));
    assertTrue(input.containsKey("original_string"));
    assertTrue(input.containsKey("source.type"));
    assertTrue(input.containsKey("field1"));
    assertFalse(input.containsKey("field2"));
    assertEquals(4, input.size());
}
Also used : JSONObject(org.json.simple.JSONObject) FieldTransformer(org.apache.metron.common.configuration.FieldTransformer) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) JSONObject(org.json.simple.JSONObject) Test(org.junit.jupiter.api.Test)

Example 10 with FieldTransformer

use of org.apache.metron.common.configuration.FieldTransformer in project metron by apache.

the class RegexSelectTransformationTest method transform.

private String transform(String in, String config) throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(config));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    JSONObject input = new JSONObject(new HashMap<String, Object>() {

        {
            put("in_field", in);
            // this is added to ensure that it looks like something approaching a real message
            put("dummy_field", "dummy");
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    return (String) input.get("out_field");
}
Also used : JSONObject(org.json.simple.JSONObject) FieldTransformer(org.apache.metron.common.configuration.FieldTransformer) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) JSONObject(org.json.simple.JSONObject)

Aggregations

FieldTransformer (org.apache.metron.common.configuration.FieldTransformer)22 SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)20 JSONObject (org.json.simple.JSONObject)19 Test (org.junit.jupiter.api.Test)18 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 SensorParserContext (org.apache.metron.rest.model.SensorParserContext)1