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"));
}
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);
}
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());
}
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());
}
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();
}
Aggregations