SearchServiceImpl implements SearchService { @Override public SearchResponse search(SearchRequest searchRequest) throws RestException { try { if (searchRequest.getIndices() == null || searchRequest.getIndices().isEmpty()) { List<String> indices = getDefaultIndices(); indices.add(METAALERT_TYPE); searchRequest.setIndices(indices); } if (searchRequest.getFacetFields() != null && searchRequest.getFacetFields().isEmpty()) { searchRequest.setFacetFields(getDefaultFacetFields()); } return dao.search(searchRequest); } catch(InvalidSearchException ise) { throw new RestException(ise.getMessage(), ise); } } @Autowired  SearchServiceImpl(IndexDao dao,
      Environment environment,
      SensorIndexingConfigService sensorIndexingConfigService,
      GlobalConfigService globalConfigService,
      AlertsUIService alertsUIService); @Override SearchResponse search(SearchRequest searchRequest); @Override GroupResponse group(GroupRequest groupRequest); @Override Optional<Map<String, Object>> getLatest(GetRequest request); @Override Map<String, FieldType> getColumnMetadata(List<String> indices); @SuppressWarnings("unchecked") List<String> getDefaultFacetFields();  }
@Test public void searchShouldProperlySearchDefaultIndices() throws Exception { when(environment.getProperty(INDEX_WRITER_NAME)).thenReturn("elasticsearch"); when(sensorIndexingConfigService.getAllIndices("elasticsearch")).thenReturn(Arrays.asList("bro", "snort", "error")); SearchRequest searchRequest = new SearchRequest(); searchService.search(searchRequest); SearchRequest expectedSearchRequest = new SearchRequest(); expectedSearchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert")); verify(dao).search(eq(expectedSearchRequest)); verifyNoMoreInteractions(dao); }
@Test public void searchShouldProperlySearchWithEmptyDefaultFacetFields() throws Exception { when(environment.getProperty(SEARCH_FACET_FIELDS_SPRING_PROPERTY, String.class, "")) .thenReturn(""); SearchRequest searchRequest = new SearchRequest(); searchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert")); searchService.search(searchRequest); SearchRequest expectedSearchRequest = new SearchRequest(); expectedSearchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert")); verify(dao).search(eq(expectedSearchRequest)); }
@Test public void searchShouldProperlySearchDefaultFacetFields() throws Exception { when(environment.getProperty(SEARCH_FACET_FIELDS_SPRING_PROPERTY, String.class, "")) .thenReturn("ip_src_addr,ip_dst_addr"); when(alertsUIService.getAlertsUIUserSettings()).thenReturn(Optional.empty()); SearchRequest searchRequest = new SearchRequest(); searchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert")); searchRequest.setFacetFields(new ArrayList<>()); searchService.search(searchRequest); SearchRequest expectedSearchRequest = new SearchRequest(); expectedSearchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert")); expectedSearchRequest.setFacetFields(Arrays.asList("source:type", "ip_src_addr", "ip_dst_addr")); verify(dao).search(eq(expectedSearchRequest)); }
@Test public void searchShouldProperlySearchWithUserSettingsFacetFields() throws Exception { AlertsUIUserSettings alertsUIUserSettings = new AlertsUIUserSettings(); alertsUIUserSettings.setFacetFields(Arrays.asList("ip_src_addr", "ip_dst_addr")); when(alertsUIService.getAlertsUIUserSettings()).thenReturn(Optional.of(alertsUIUserSettings)); SearchRequest searchRequest = new SearchRequest(); searchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert")); searchRequest.setFacetFields(new ArrayList<>()); searchService.search(searchRequest); SearchRequest expectedSearchRequest = new SearchRequest(); expectedSearchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert")); expectedSearchRequest.setFacetFields(Arrays.asList("ip_src_addr", "ip_dst_addr")); verify(dao).search(eq(expectedSearchRequest)); }
@Test public void searchShouldProperlySearch() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.setIndices(Arrays.asList("bro")); searchRequest.setFacetFields(Arrays.asList("ip_src_addr")); searchService.search(searchRequest); SearchRequest expectedSearchRequest = new SearchRequest(); expectedSearchRequest.setIndices(Arrays.asList("bro")); expectedSearchRequest.setFacetFields(Arrays.asList("ip_src_addr")); verify(dao).search(eq(expectedSearchRequest)); verifyNoMoreInteractions(dao); }
@Test public void saveShouldWrapExceptionInRestException() throws Exception { when(dao.search(any(SearchRequest.class))).thenThrow(InvalidSearchException.class); SearchRequest searchRequest = new SearchRequest(); searchRequest.setIndices(Arrays.asList("bro")); searchRequest.setFacetFields(Arrays.asList("ip_src_addr")); assertThrows(RestException.class, () -> searchService.search(searchRequest)); }