LegacyDetectorRepositoryImpl implements LegacyDetectorRepository { @Override public void trustDetector(String uuid, Boolean trusted) { MDC.put("DetectorUuid", uuid); val updateRequest = new UpdateRequest(DETECTOR_INDEX, DETECTOR_DOC_TYPE, uuid); Date nowDate = DateUtil.now(); String nowValue = DateUtil.toDateString(nowDate.toInstant()); Map<String, Object> jsonMap = new HashMap<>(); Map<String, Object> metaMap = new HashMap<>(); metaMap.put("dateUpdated", nowDate); jsonMap.put("lastUpdateTimestamp", nowValue); jsonMap.put("trusted", trusted); jsonMap.put("meta", metaMap); updateRequest.doc(jsonMap); try { val updateResponse = legacyElasticSearchClient.update(updateRequest, RequestOptions.DEFAULT); if (elasticsearchUtil.checkNullResponse(updateResponse.getResult())) { throw new RecordNotFoundException("Invalid request: " + uuid); } } catch (IOException e) { log.error("Updating elastic search failed", e); throw new RuntimeException(e); } finally { MDC.remove("DetectorUuid"); } }  @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 testTrustDetector() { LegacyDetectorRepository legacyDetectorRepository = mock(LegacyDetectorRepository.class); doNothing().when(legacyDetectorRepository).trustDetector(anyString(), anyBoolean()); legacyDetectorRepository.trustDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", true); verify(legacyDetectorRepository, times(1)).trustDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", true); }
@Test(expected = RuntimeException.class) public void trustDetectorFail() throws IOException { Mockito.when(legacyElasticSearchClient.update(any(UpdateRequest.class), any(RequestOptions.class))).thenThrow(new IOException()); repoUnderTest.trustDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", true); }
@Test(expected = RecordNotFoundException.class) public void trustDetector_illegal_args() { val updateResponse = mock(UpdateResponse.class); val result = updateResponse.getResult(); Mockito.when(result).thenReturn(DocWriteResponse.Result.NOT_FOUND); Mockito.when(elasticsearchUtil.checkNullResponse(result)).thenReturn(true); repoUnderTest.trustDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", true); }