public Map<String, Object> getSensorIndexingConfig(String sensorType) { return getSensorIndexingConfig(sensorType, true); }
@Override public Map<String, Object> findOne(String name) throws RestException { IndexingConfigurations configs = cache.get( IndexingConfigurations.class); return configs.getSensorIndexingConfig(name, false); }
@Override public Map<String, Object> getSensorConfig(String sensorName) { return config.orElse(new IndexingConfigurations()).getSensorIndexingConfig(sensorName, writerName); }
public String getOutputPathFunction(String sensorName, String writerName) { return getOutputPathFunction(getSensorIndexingConfig(sensorName, writerName), sensorName); }
public int getBatchSize(String sensorName, String writerName ) { return getBatchSize(getSensorIndexingConfig(sensorName, writerName)); }
public int getBatchTimeout(String sensorName, String writerName ) { return getBatchTimeout(getSensorIndexingConfig(sensorName, writerName)); }
public String getFieldNameConverter(String sensorName, String writerName) { return getFieldNameConverter(getSensorIndexingConfig(sensorName, writerName), sensorName); }
public String getIndex(String sensorName, String writerName) { return getIndex(getSensorIndexingConfig(sensorName, writerName), sensorName); }
public boolean isEnabled(String sensorName, String writerName) { return isEnabled(getSensorIndexingConfig(sensorName, writerName)); }
@SuppressWarnings("unchecked") public static Function<String, String> getIndexLookupFunction(ConfigurationsCache cache, String writerName) { return sensorType -> { String indexingTopic = sensorType; IndexingConfigurations indexingConfigs = cache.get( IndexingConfigurations.class); Map<String, Object> indexingSensorConfigs = indexingConfigs.getSensorIndexingConfig(sensorType); if (indexingSensorConfigs != null) { Map<String, Object> writerConfigs = (Map<String, Object>) indexingSensorConfigs.get(writerName); if (writerConfigs != null) { indexingTopic = (String) writerConfigs.getOrDefault(IndexingConfigurations.INDEX_CONF, indexingTopic); } } return indexingTopic; }; } }
@Test public void getIndexLookupFunctionShouldReturnConfiguredIndex() { IndexingConfigurations indexingConfigs = mock(IndexingConfigurations.class); ConfigurationsCache cache = mock(ConfigurationsCache.class); Map<String, Object> broIndexingConfig = new HashMap<String, Object>() {{ put("writer", new HashMap<String, Object>() {{ put("index", "bro_index"); }}); }}; when(indexingConfigs.getSensorIndexingConfig("bro")).thenReturn(broIndexingConfig); when(cache.get(IndexingConfigurations.class)).thenReturn(indexingConfigs); assertEquals("bro_index", IndexingCacheUtil.getIndexLookupFunction(cache, "writer").apply("bro")); }
@Test public void getIndexLookupFunctionShouldDefaultToSensorType() { IndexingConfigurations indexingConfigs = mock(IndexingConfigurations.class); ConfigurationsCache cache = mock(ConfigurationsCache.class); Map<String, Object> broIndexingConfig = new HashMap<String, Object>() {{ put("writer", new HashMap<String, Object>() {{ put("index", "bro_index"); }}); }}; when(indexingConfigs.getSensorIndexingConfig("bro")).thenReturn(broIndexingConfig); when(cache.get(IndexingConfigurations.class)).thenReturn(indexingConfigs); assertEquals("Should default to sensor type on missing sensor config", "snort", IndexingCacheUtil.getIndexLookupFunction(cache, "writer").apply("snort")); assertEquals("Should default to sensor type on missing writer config", "bro", IndexingCacheUtil.getIndexLookupFunction(cache, "someWriter").apply("bro")); } }
assertEventually(() -> Assert.assertEquals(expectedConfig, config.getSensorIndexingConfig("test")));
Map<String, Object> expectedConfig = JSONUtils.INSTANCE.load(inFile, JSONUtils.MAP_SUPPLIER); IndexingConfigurations config = cache.get( IndexingConfigurations.class); assertEventually(() -> Assert.assertEquals(expectedConfig, config.getSensorIndexingConfig("test"))); assertEventually(() -> Assert.assertEquals(expectedGlobalConfig, config.getGlobalConfig())); assertEventually(() -> Assert.assertNull(config.getSensorIndexingConfig("notthere", false)));
assertEventually(() -> Assert.assertNull(config.getSensorIndexingConfig("test", false))); assertEventually(() -> Assert.assertNull(config.getGlobalConfig(false)));