Search in sources :

Example 76 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig 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 77 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig 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 78 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig 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)

Example 79 with SensorParserConfig

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

the class ParserFunctionsTest method testInitFromMap.

@Test
public void testInitFromMap() throws Exception {
    Map<String, Object> configAsMap = (JSONObject) new JSONParser().parse(broParserConfig);
    set("configAsMap", configAsMap);
    StellarParserRunner runner = execute("PARSER_INIT('bro', configAsMap)", StellarParserRunner.class);
    assertNotNull(runner);
    SensorParserConfig actual = runner.getParserConfigurations().getSensorParserConfig("bro");
    SensorParserConfig expected = SensorParserConfig.fromBytes(broParserConfig.getBytes(StandardCharsets.UTF_8));
    assertEquals(expected, actual);
}
Also used : JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 80 with SensorParserConfig

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

the class SensorParserGroupServiceImplTest method shouldSaveExistingGroup.

@Test
public void shouldSaveExistingGroup() throws Exception {
    SensorParserGroup oldGroup = new SensorParserGroup();
    oldGroup.setName("oldGroup");
    oldGroup.setDescription("old description");
    oldGroup.setSensors(Collections.singleton("oldSensor"));
    ParserConfigurations parserConfigurations = mock(ParserConfigurations.class);
    when(cache.get(ParserConfigurations.class)).thenReturn(new ParserConfigurations());
    when(parserConfigurations.getSensorParserGroups()).thenReturn(new HashMap<String, SensorParserGroup>() {

        {
            put("newSensor", oldGroup);
        }
    });
    when(sensorParserConfigService.findOne("newSensor")).thenReturn(new SensorParserConfig());
    SensorParserGroup newGroup = new SensorParserGroup();
    newGroup.setName("newGroup");
    newGroup.setDescription("new description");
    newGroup.setSensors(Collections.singleton("newSensor"));
    Map<String, Object> expectedGlobalConfig = new HashMap<>();
    Collection<SensorParserGroup> expectedGroup = Collections.singleton(newGroup);
    expectedGlobalConfig.put(PARSER_GROUPS_CONF, expectedGroup);
    assertEquals(newGroup, sensorParserGroupService.save(newGroup));
    verify(globalConfigService, times(1)).save(expectedGlobalConfig);
    verifyNoMoreInteractions(globalConfigService);
}
Also used : ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Aggregations

SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)97 Test (org.junit.jupiter.api.Test)74 JSONObject (org.json.simple.JSONObject)40 FieldTransformer (org.apache.metron.common.configuration.FieldTransformer)20 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)19 HashMap (java.util.HashMap)15 Config (org.apache.storm.Config)7 BaseBoltTest (org.apache.metron.test.bolt.BaseBoltTest)6 File (java.io.File)5 Map (java.util.Map)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 WriterHandler (org.apache.metron.parsers.bolt.WriterHandler)5 IOException (java.io.IOException)4 SensorParserGroup (org.apache.metron.common.configuration.SensorParserGroup)4 MetronError (org.apache.metron.common.error.MetronError)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 CommandLine (org.apache.commons.cli.CommandLine)3 ParseException (org.apache.commons.cli.ParseException)3 PosixParser (org.apache.commons.cli.PosixParser)3