Search in sources :

Example 11 with FieldTransformer

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

the class StellarTransformationTest method testStellar.

/**
 * Test the happy path.  This ensures that a simple transformation, converting a timestamp in a yyyy-MM-dd HH:mm:ss
 * format can be converted to the expected UTC MS since Epoch.
 */
@Test
public void testStellar() throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(stellarConfig));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    JSONObject input = new JSONObject(new HashMap<String, Object>() {

        {
            put("timestamp", "2016-01-05 17:02:30");
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    long expected = 1452013350000L;
    assertEquals(expected, input.get("utc_timestamp"));
    assertTrue(input.containsKey("timestamp"));
}
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 12 with FieldTransformer

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

the class FieldTransformationTest method testSimpleMapping.

@Test
public void testSimpleMapping() throws IOException {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(config));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    assertNotNull(handler);
    assertEquals(ImmutableMap.of("protocol", "TCP"), handler.transform(new JSONObject(ImmutableMap.of("protocol", 6)), Context.EMPTY_CONTEXT(), c.getParserConfig()));
}
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 13 with FieldTransformer

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

the class RenameTransformationTest method renameMissingField.

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

        {
            for (int i = 2; i <= 10; ++i) {
                put("old_field" + i, "f" + i);
            }
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertFalse(input.containsKey("new_field1"));
    for (int i = 2; i <= 10; ++i) {
        assertEquals("f" + i, input.get("old_field" + i));
    }
    assertEquals(9, 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 14 with FieldTransformer

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

the class RenameTransformationTest method smokeTest.

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

        {
            for (int i = 1; i <= 10; ++i) {
                put("old_field" + i, "f" + i);
            }
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertEquals("f1", input.get("new_field1"));
    assertEquals("f2", input.get("new_field2"));
    for (int i = 3; i <= 10; ++i) {
        assertEquals("f" + i, input.get("old_field" + i));
    }
    assertFalse(input.containsKey("old_field1"));
    assertFalse(input.containsKey("old_field2"));
    assertEquals(10, 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 15 with FieldTransformer

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

the class StellarTransformationTest method testStellarNumericDomain.

@Test
public void testStellarNumericDomain() throws Exception {
    /*
    Despite the domain being weird, URL_TO_HOST should allow it to pass through.
    However, because it does NOT form a proper domain (no TLD), DOMAIN_REMOVE_SUBDOMAINS returns
    null indicating that the input is semantically incorrect.
     */
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(configNumericDomain));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    JSONObject input = new JSONObject();
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertTrue(input.containsKey("full_hostname"));
    assertEquals("1234567890123456789012345678901234567890123456789012345678901234567890", input.get("full_hostname"));
    assertFalse(input.containsKey("domain_without_subdomains"));
}
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)

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