SensorIndexingConfigServiceImpl implements SensorIndexingConfigService { @Override public Iterable<String> getAllIndices(String writerName) throws RestException { if(StringUtils.isEmpty(writerName)) { return Collections.emptyList(); } IndexingConfigurations indexingConfigs = cache.get( IndexingConfigurations.class); ParserConfigurations parserConfigs = cache.get( ParserConfigurations.class); Set<String> ret = new HashSet<>(); for(String sensorName : Iterables.concat(parserConfigs.getTypes(), indexingConfigs.getTypes())) { if(indexingConfigs.isEnabled(sensorName, writerName)) { String indexName = indexingConfigs.getIndex(sensorName, writerName); ret.add(indexName == null ? sensorName : indexName); } } return ret; } @Autowired  SensorIndexingConfigServiceImpl(ObjectMapper objectMapper, CuratorFramework client, ConfigurationsCache cache); @Override Map<String, Object> save(String name, Map<String, Object> sensorIndexingConfig); @Override Map<String, Object> findOne(String name); @Override Map<String, Map<String, Object>> getAll(); @Override List<String> getAllTypes(); @Override Iterable<String> getAllIndices(String writerName); @Override boolean delete(String name);  }
@Test public void getAllIndicesWithOnlyParsers() throws RestException { ParserConfigurations parserConfiguration = mock(ParserConfigurations.class); when(parserConfiguration.getTypes()).thenReturn(ImmutableList.of("bro", "snort")); IndexingConfigurations indexingConfiguration = mock(IndexingConfigurations.class); when(indexingConfiguration.getTypes()).thenReturn(Collections.emptyList()); when(indexingConfiguration.getIndex(eq("bro"), eq("elasticsearch"))).thenReturn(null); 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(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("bro")); assertTrue(indices.contains("snort")); }
@Test public void getAllIndicesWithOnlyIndexing() throws RestException { ParserConfigurations parserConfiguration = mock(ParserConfigurations.class); when(parserConfiguration.getTypes()).thenReturn(Collections.emptyList()); IndexingConfigurations indexingConfiguration = mock(IndexingConfigurations.class); 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")); }
@Test public void getAllIndicesWithParsersAndIndexConfigs() throws RestException { ParserConfigurations parserConfiguration = mock(ParserConfigurations.class); when(parserConfiguration.getTypes()).thenReturn(ImmutableList.of("bro", "yaf")); IndexingConfigurations indexingConfiguration = mock(IndexingConfigurations.class); when(indexingConfiguration.getTypes()).thenReturn(ImmutableList.of("bro", "snort", "squid")); when(indexingConfiguration.getIndex(eq("bro"), eq("elasticsearch"))).thenReturn("renamed_bro"); when(indexingConfiguration.getIndex(eq("snort"), eq("elasticsearch"))).thenReturn("snort"); when(indexingConfiguration.getIndex(eq("yaf"), 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(true); when(indexingConfiguration.isEnabled(eq("squid"), 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(3, indices.size()); assertTrue(indices.contains("renamed_bro")); assertTrue(indices.contains("snort")); assertTrue(indices.contains("yaf")); }
@Test public void getAllIndicesWithNoConfigs() throws RestException { ParserConfigurations parserConfiguration = mock(ParserConfigurations.class); when(parserConfiguration.getTypes()).thenReturn(Collections.emptyList()); IndexingConfigurations indexingConfiguration = mock(IndexingConfigurations.class); when(indexingConfiguration.getTypes()).thenReturn(Collections.emptyList()); 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(0, indices.size()); }