IndexingCacheUtil { @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; }; }  @SuppressWarnings("unchecked") static Function<String, String> getIndexLookupFunction(ConfigurationsCache cache, String writerName);  }
@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("snort", IndexingCacheUtil.getIndexLookupFunction(cache, "writer").apply("snort"), "Should default to sensor type on missing sensor config"); assertEquals("bro", IndexingCacheUtil.getIndexLookupFunction(cache, "someWriter").apply("bro"), "Should default to sensor type on missing writer config"); }