DetectorMappingRepositoryImpl implements DetectorMappingRepository { @Override public void deleteDetectorMapping(String id) { val deleteRequest = new DeleteRequest(elasticSearchProperties.getIndexName(), elasticSearchProperties.getDocType(), id); try { val deleteResponse = legacyElasticSearchClient.delete(deleteRequest, RequestOptions.DEFAULT); if (elasticsearchUtil.checkNullResponse(deleteResponse.getResult())) { throw new RecordNotFoundException("Invalid request: " + id); } } catch (IOException e) { log.error(String.format("Deleting mapping %s failed", id), e); throw new RuntimeException(e); } } @Autowired  DetectorMappingRepositoryImpl(MetricRegistry metricRegistry); @Override String createDetectorMapping(CreateDetectorMappingRequest request); @Override MatchingDetectorsResponse findMatchingDetectorMappings(List<Map<String, String>> tagsList); @Override DetectorMapping findDetectorMapping(String id); @Override List<DetectorMapping> findLastUpdated(int timeInSeconds); @Override List<DetectorMapping> search(SearchMappingsRequest request); @Override void disableDetectorMapping(String id); @Override void deleteDetectorMapping(String id); @Override void deleteMappingsByDetectorUUID(UUID uuid);  }
@Test public void deleteDetectorMapping_successful() throws Exception { val id = "adsvade8^szx"; DeleteResponse deleteResponse = mockDeleteResponse(id); when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(new DeleteResponse()); repoUnderTest.deleteDetectorMapping(id); verify(legacyElasticSearchClient, atLeastOnce()).delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT)); assertEquals(id, deleteResponse.getId()); assertEquals(elasticSearchProperties.getIndexName(), deleteResponse.getIndex()); assertEquals("DELETED", deleteResponse.getResult().toString()); }
@Test(expected = RuntimeException.class) public void deleteDetectorMapping_fail() throws IOException { val id = "adsvade8^szx"; DeleteRequest deleteRequest = new DeleteRequest(elasticSearchProperties.getIndexName(), elasticSearchProperties.getDocType(), id); when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenThrow(new IOException()); repoUnderTest.deleteDetectorMapping(id); }
@Test(expected = RecordNotFoundException.class) public void deleteDetectorMapping_illegal_args() throws Exception { val deleteResponse = mockDeleteResponse("1"); Mockito.when(legacyElasticSearchClient.delete(any(DeleteRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(deleteResponse); Mockito.when(deleteResponse.getResult()).thenReturn(DocWriteResponse.Result.NOT_FOUND); Mockito.when(elasticsearchUtil.checkNullResponse(deleteResponse.getResult())).thenReturn(true); repoUnderTest.deleteDetectorMapping("1"); }