Search in sources :

Example 16 with FieldTransformer

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

the class StellarTransformationTest method testStellarSpecialCharacters.

@Test
public void testStellarSpecialCharacters() throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(stellarConfigEspecial));
    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"));
    assertTrue(input.containsKey("newStellarField"));
}
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 17 with FieldTransformer

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

the class StellarTransformationTest method testStellarBadConfig.

@Test
public void testStellarBadConfig() throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(badConfig));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    JSONObject input = new JSONObject();
    IllegalStateException ex = assertThrows(IllegalStateException.class, () -> handler.transformAndUpdate(input, Context.EMPTY_CONTEXT()));
    assertTrue(ex.getMessage().contains("URL_TO_HOST"));
    assertTrue(ex.getMessage().contains("123"));
}
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 18 with FieldTransformer

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

the class StellarTransformationTest method testStellar_negative.

/**
 * Ensures that if we try to transform with a field which does not exist, it does not
 * 1. throw an exception
 * 2. do any transformation.
 */
@Test
public void testStellar_negative() throws Exception {
    SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(stellarConfig));
    FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
    // no input fields => no transformation
    JSONObject input = new JSONObject(new HashMap<String, Object>() {

        {
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertFalse(input.containsKey("utc_timestamp"));
    assertTrue(input.isEmpty());
}
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 19 with FieldTransformer

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

the class SelectTransformationTest method testMulitpleFieldReturned.

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

        {
            put("field1", "foo");
            put("field2", "bar");
            put("field3", "bar2");
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertTrue(input.containsKey("field1"));
    assertTrue(input.containsKey("field2"));
    assertFalse(input.containsKey("field3"));
    assertEquals(2, 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 20 with FieldTransformer

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

the class SelectTransformationTest method testSingleFieldReturned.

@Test
public void testSingleFieldReturned() 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("field1", "foo");
            put("field2", "bar");
        }
    });
    handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
    assertTrue(input.containsKey("field1"));
    assertFalse(input.containsKey("field2"));
    assertEquals(1, 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)

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