Search in sources :

Example 51 with ParserConfigurations

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

the class SensorIndexingConfigServiceImplTest method getAllIndicesWithOnlyIndexing.

@Test
public void getAllIndicesWithOnlyIndexing() throws RestException {
    ParserConfigurations parserConfiguration = mock(ParserConfigurations.class);
    when(parserConfiguration.getTypes()).thenReturn(Collections.emptyList());
    IndexingConfigurations indexingConfiguration = mock(IndexingConfigurations.class);
    // rename bro, include snort by default configs, and disable yaf
    when(indexingConfiguration.getTypes()).thenReturn(ImmutableList.of("bro", "snort", "yaf"));
    when(indexingConfiguration.getIndex(eq("bro"), eq("elasticsearch"))).thenReturn("renamed_bro");
    when(indexingConfiguration.getIndex(eq("snort"), eq("elasticsearch"))).thenReturn(null);
    when(indexingConfiguration.isEnabled(eq("snort"), eq("elasticsearch"))).thenReturn(true);
    when(indexingConfiguration.isEnabled(eq("bro"), eq("elasticsearch"))).thenReturn(true);
    when(indexingConfiguration.isEnabled(eq("yaf"), eq("elasticsearch"))).thenReturn(false);
    when(cache.get(eq(ParserConfigurations.class))).thenReturn(parserConfiguration);
    when(cache.get(eq(IndexingConfigurations.class))).thenReturn(indexingConfiguration);
    List<String> indices = new ArrayList<String>();
    Iterables.addAll(indices, sensorIndexingConfigService.getAllIndices("elasticsearch"));
    assertEquals(2, indices.size());
    assertTrue(indices.contains("renamed_bro"));
    assertTrue(indices.contains("snort"));
}
Also used : ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) IndexingConfigurations(org.apache.metron.common.configuration.IndexingConfigurations) Test(org.junit.jupiter.api.Test)

Example 52 with ParserConfigurations

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

Example 53 with ParserConfigurations

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

the class SensorParserGroupServiceImplTest method saveShouldThrowExceptionOnSensorInAnotherGroup.

@Test
public void saveShouldThrowExceptionOnSensorInAnotherGroup() throws Exception {
    SensorParserGroup existingGroup = new SensorParserGroup();
    existingGroup.setName("existingGroup");
    existingGroup.setSensors(Collections.singleton("bro"));
    ParserConfigurations parserConfigurations = mock(ParserConfigurations.class);
    when(parserConfigurations.getSensorParserGroups()).thenReturn(new HashMap<String, SensorParserGroup>() {

        {
            put("existingGroup", existingGroup);
        }
    });
    when(cache.get(ParserConfigurations.class)).thenReturn(parserConfigurations);
    when(sensorParserConfigService.findOne("bro")).thenReturn(new SensorParserConfig());
    SensorParserGroup newGroup = new SensorParserGroup();
    newGroup.setName("newGroup");
    newGroup.setSensors(Collections.singleton("bro"));
    RestException e = assertThrows(RestException.class, () -> sensorParserGroupService.save(newGroup));
    assertEquals("Sensor bro is already in group existingGroup", e.getMessage());
}
Also used : ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) RestException(org.apache.metron.rest.RestException) SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 54 with ParserConfigurations

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

the class SensorParserGroupServiceImplTest method saveShouldThrowExceptionOnMissingConfig.

@Test
public void saveShouldThrowExceptionOnMissingConfig() {
    when(cache.get(ParserConfigurations.class)).thenReturn(new ParserConfigurations());
    SensorParserGroup sensorParserGroup = new SensorParserGroup();
    sensorParserGroup.setSensors(Collections.singleton("bro"));
    RestException e = assertThrows(RestException.class, () -> sensorParserGroupService.save(sensorParserGroup));
    assertEquals("Could not find config for sensor bro", e.getMessage());
}
Also used : ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) RestException(org.apache.metron.rest.RestException) SensorParserGroup(org.apache.metron.common.configuration.SensorParserGroup) Test(org.junit.jupiter.api.Test)

Example 55 with ParserConfigurations

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

the class ConfiguredParserBoltTest method test.

@Test
public void test() throws Exception {
    ParserConfigurations sampleConfigurations = new ParserConfigurations();
    UnitTestHelper.setLog4jLevel(ConfiguredBolt.class, Level.FATAL);
    StandAloneConfiguredParserBolt configuredBoltNullZk = new ConfiguredParserBoltTest.StandAloneConfiguredParserBolt(null);
    assertThrows(RuntimeException.class, () -> configuredBoltNullZk.prepare(new HashMap(), topologyContext, outputCollector), "A valid zookeeper url must be supplied");
    UnitTestHelper.setLog4jLevel(ConfiguredBolt.class, Level.ERROR);
    configsUpdated = new HashSet<>();
    sampleConfigurations.updateGlobalConfig(ConfigurationsUtils.readGlobalConfigFromFile("../" + TestConstants.SAMPLE_CONFIG_PATH));
    Map<String, byte[]> sensorParserConfigs = ConfigurationsUtils.readSensorParserConfigsFromFile("../" + TestConstants.PARSER_CONFIGS_PATH);
    for (String sensorType : sensorParserConfigs.keySet()) {
        sampleConfigurations.updateSensorParserConfig(sensorType, sensorParserConfigs.get(sensorType));
    }
    StandAloneConfiguredParserBolt configuredBolt = new StandAloneConfiguredParserBolt(zookeeperUrl);
    configuredBolt.prepare(new HashMap(), topologyContext, outputCollector);
    waitForConfigUpdate(parserConfigurationTypes);
    assertEquals(sampleConfigurations, configuredBolt.getConfigurations());
    configsUpdated = new HashSet<>();
    Map<String, Object> sampleGlobalConfig = sampleConfigurations.getGlobalConfig();
    sampleGlobalConfig.put("newGlobalField", "newGlobalValue");
    ConfigurationsUtils.writeGlobalConfigToZookeeper(sampleGlobalConfig, zookeeperUrl);
    waitForConfigUpdate(ConfigurationType.GLOBAL.getTypeName());
    assertEquals(sampleConfigurations.getGlobalConfig(), configuredBolt.getConfigurations().getGlobalConfig(), "Add global config field");
    configsUpdated = new HashSet<>();
    sampleGlobalConfig.remove("newGlobalField");
    ConfigurationsUtils.writeGlobalConfigToZookeeper(sampleGlobalConfig, zookeeperUrl);
    waitForConfigUpdate(ConfigurationType.GLOBAL.getTypeName());
    assertEquals(sampleConfigurations, configuredBolt.getConfigurations(), "Remove global config field");
    configsUpdated = new HashSet<>();
    String sensorType = "testSensorConfig";
    SensorParserConfig testSensorConfig = new SensorParserConfig();
    testSensorConfig.setParserClassName("className");
    testSensorConfig.setSensorTopic("sensorTopic");
    testSensorConfig.setParserConfig(new HashMap<String, Object>() {

        {
            put("configName", "configObject");
        }
    });
    sampleConfigurations.updateSensorParserConfig(sensorType, testSensorConfig);
    ConfigurationsUtils.writeSensorParserConfigToZookeeper(sensorType, testSensorConfig, zookeeperUrl);
    waitForConfigUpdate(sensorType);
    ParserConfigurations configuredBoltConfigs = configuredBolt.getConfigurations();
    if (!sampleConfigurations.equals(configuredBoltConfigs)) {
        // before we fail, let's try to dump out some info.
        if (sampleConfigurations.getFieldValidations().size() != configuredBoltConfigs.getFieldValidations().size()) {
            System.out.println("Field validations don't line up");
        }
        for (int i = 0; i < sampleConfigurations.getFieldValidations().size(); ++i) {
            FieldValidator l = sampleConfigurations.getFieldValidations().get(i);
            FieldValidator r = configuredBoltConfigs.getFieldValidations().get(i);
            if (!l.equals(r)) {
                System.out.println(l + " != " + r);
            }
        }
        if (sampleConfigurations.getConfigurations().size() != configuredBoltConfigs.getConfigurations().size()) {
            System.out.println("Configs don't line up");
        }
        for (Map.Entry<String, Object> kv : sampleConfigurations.getConfigurations().entrySet()) {
            Object l = kv.getValue();
            Object r = configuredBoltConfigs.getConfigurations().get(kv.getKey());
            if (!l.equals(r)) {
                System.out.println(kv.getKey() + " config does not line up: ");
                System.out.println(l);
                System.out.println(r);
            }
        }
        assertEquals(sampleConfigurations, configuredBoltConfigs, "Add new sensor config");
    }
    assertEquals(sampleConfigurations, configuredBoltConfigs, "Add new sensor config");
    configuredBolt.cleanup();
}
Also used : HashMap(java.util.HashMap) FieldValidator(org.apache.metron.common.configuration.FieldValidator) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)57 Test (org.junit.jupiter.api.Test)33 BaseBoltTest (org.apache.metron.test.bolt.BaseBoltTest)25 JSONObject (org.json.simple.JSONObject)24 SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)20 Test (org.junit.Test)13 SensorParserGroup (org.apache.metron.common.configuration.SensorParserGroup)9 ParserWriterConfiguration (org.apache.metron.common.configuration.writer.ParserWriterConfiguration)8 Tuple (org.apache.storm.tuple.Tuple)8 IOException (java.io.IOException)7 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)7 MetronError (org.apache.metron.common.error.MetronError)6 MetronErrorJSONMatcher (org.apache.metron.test.error.MetronErrorJSONMatcher)6 IndexingConfigurations (org.apache.metron.common.configuration.IndexingConfigurations)5 Context (org.apache.metron.stellar.dsl.Context)5 HashMap (java.util.HashMap)4 RawMessage (org.apache.metron.common.message.metadata.RawMessage)4 RestException (org.apache.metron.rest.RestException)4 TopologyContext (org.apache.storm.task.TopologyContext)4 Map (java.util.Map)3