LegacyDetectorRepositoryImpl implements LegacyDetectorRepository { @Override public DetectorDocument findByUuid(String uuid) { MDC.put("DetectorUuid", uuid); val queryBuilder = QueryBuilders.termQuery("uuid", uuid); val searchSourceBuilder = elasticsearchUtil.getSourceBuilder(queryBuilder).size(DEFAULT_ES_RESULTS_SIZE); val searchRequest = elasticsearchUtil.getSearchRequest(searchSourceBuilder, DETECTOR_INDEX, DETECTOR_DOC_TYPE); val detectors = getDetectorsFromElasticSearch(searchRequest); MDC.remove("DetectorUuid"); return detectors.isEmpty() ? null : detectors.get(0); }  @Override UUID createDetector(DetectorDocument document); @Override void deleteDetector(String uuid); @Override void updateDetector(String uuid, DetectorDocument document); @Override DetectorDocument findByUuid(String uuid); @Override List<DetectorDocument> findByCreatedBy(String user); @Override void toggleDetector(String uuid, Boolean enabled); @Override void trustDetector(String uuid, Boolean trusted); @Override List<DetectorDocument> getLastUpdatedDetectors(long interval); @Override List<DetectorDocument> getLastUpdatedDetectors(String fromDate, String toDate);  }
@Test public void testFindByUuid() { DetectorDocument actualDetector = repoUnderTest.findByUuid("uuid"); assertNotNull(actualDetector); Assert.assertEquals(UUID.fromString("aeb4d849-847a-45c0-8312-dc0fcf22b639"), actualDetector.getUuid()); Assert.assertEquals("test-user", actualDetector.getCreatedBy()); Assert.assertEquals(true, actualDetector.isEnabled()); Assert.assertEquals(true, actualDetector.isTrusted()); }
@Test public void testFindByUuid_emptyResult() throws IOException { SearchHits hits = new SearchHits(new SearchHit[]{}, 0, 0); SearchResponseSections searchResponseSections = new SearchResponseSections(hits, null, null, false, null, null, 5); SearchResponse emptySearchResponse = new SearchResponse(searchResponseSections, "", 0, 0, 0, 0, null, null); Mockito.when(legacyElasticSearchClient.search(any(SearchRequest.class), any(RequestOptions.class))).thenReturn(emptySearchResponse); DetectorDocument actualDetector = repoUnderTest.findByUuid("missing-uuid"); assertNull(actualDetector); }
@Test(expected = RuntimeException.class) public void searchDetectorFail() throws IOException { Mockito.when(legacyElasticSearchClient.search(any(SearchRequest.class), any(RequestOptions.class))).thenThrow(new IOException()); repoUnderTest.findByUuid("aeb4d849-847a-45c0-8312-dc0fcf22b639"); }