LegacyDetectorRepositoryImpl implements LegacyDetectorRepository { @Override public void updateDetector(String uuid, DetectorDocument document) { notNull(document, "document can't be null"); MDC.put("DetectorUuid", uuid); RequestValidator.validateDetectorDocument(document); val updateRequest = new UpdateRequest(DETECTOR_INDEX, DETECTOR_DOC_TYPE, uuid); Map<String, Object> jsonMap = new HashMap<>(); for (Field field : document.getClass().getDeclaredFields()) { ReflectionUtils.makeAccessible(field); String name = field.getName(); if (!name.isEmpty()) { Object value; Date nowDate = DateUtil.now(); try { value = field.get(document); } catch (IllegalAccessException e) { log.error("Updating elastic search failed", e); throw new RuntimeException(e); } if ("lastUpdateTimestamp".equals(name)) { value = DateUtil.toDateString(nowDate.toInstant()); } if ("meta".equals(name)) { if (document.getMeta() == null) { DetectorDocument.Meta metaBlock = new DetectorDocument.Meta(); metaBlock.setDateUpdated(nowDate); document.setMeta(metaBlock); } else { DetectorDocument.Meta metaBlock = document.getMeta(); metaBlock.setDateUpdated(nowDate); metaBlock.setUpdatedBy(document.getMeta().getUpdatedBy()); } } if (field.isAnnotationPresent(JsonProperty.class)) { name = field.getAnnotation(JsonProperty.class).value(); } if (value != null) { jsonMap.put(name, value); } } } val json = objectMapperUtil.convertToString(jsonMap); updateRequest.doc(json, XContentType.JSON); 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 testUpdateDetector() { LegacyDetectorRepository legacyDetectorRepository = mock(LegacyDetectorRepository.class); doNothing().when(legacyDetectorRepository).updateDetector(anyString(), any(DetectorDocument.class)); legacyDetectorRepository.updateDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", new DetectorDocument()); verify(legacyDetectorRepository, times(1)).updateDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", new DetectorDocument()); }
@Test(expected = DetectorException.class) public void testUpdateDetector_nullValues() { val document = new DetectorDocument(); document.setCreatedBy("user"); repoUnderTest.updateDetector("", document); }
@Test(expected = IllegalArgumentException.class) public void testUpdateDetector_illegalThresholds() { repoUnderTest.updateDetector("", illegalParamsDetector); }
@Test public void testUpdateDetector_emptyMeta() { val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); document.setMeta(null); UUID someUuid = UUID.randomUUID(); document.setUuid(someUuid); repoUnderTest.updateDetector(someUuid.toString(), document); }
@Test public void testUpdateDetector_populatedMeta() { val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); DetectorDocument.Meta metaBlock = new DetectorDocument.Meta(); metaBlock.setCreatedBy("user"); document.setMeta(metaBlock); UUID someUuid = UUID.randomUUID(); document.setUuid(someUuid); repoUnderTest.updateDetector(someUuid.toString(), document); }
@Test public void testUpdateDetector_populatedMeta_noUser() { val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); DetectorDocument.Meta metaBlock = new DetectorDocument.Meta(); metaBlock.setCreatedBy(null); document.setMeta(metaBlock); UUID someUuid = UUID.randomUUID(); document.setUuid(someUuid); repoUnderTest.updateDetector(someUuid.toString(), document); }
@Test(expected = RecordNotFoundException.class) public void updateDetector_illegal_args() { val updateResponse = mock(UpdateResponse.class); val result = updateResponse.getResult(); val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); document.setUuid(UUID.randomUUID()); Mockito.when(result).thenReturn(DocWriteResponse.Result.NOT_FOUND); Mockito.when(elasticsearchUtil.checkNullResponse(result)).thenReturn(true); repoUnderTest.updateDetector("aeb4d849-847a-45c0-8312-dc0fcf22b639", document); }