LegacyDetectorRepositoryImpl implements LegacyDetectorRepository { @Override public UUID createDetector(DetectorDocument document) { notNull(document, "document can't be null"); isNull(document.getUuid(), "Required: document.uuid == null"); val uuid = UUID.randomUUID(); document.setUuid(uuid); Date nowDate = DateUtil.now(); document.setLastUpdateTimestamp(nowDate); if (document.getMeta() == null) { DetectorDocument.Meta metaBlock = new DetectorDocument.Meta(); metaBlock.setDateCreated(nowDate); metaBlock.setCreatedBy(document.getCreatedBy()); document.setMeta(metaBlock); } else { DetectorDocument.Meta metaBlock = document.getMeta(); metaBlock.setDateCreated(nowDate); if (metaBlock.getCreatedBy() == null) { metaBlock.setCreatedBy(document.getCreatedBy()); } } RequestValidator.validateDetectorDocument(document); val indexRequest = new IndexRequest(DETECTOR_INDEX, DETECTOR_DOC_TYPE, uuid.toString()); val json = objectMapperUtil.convertToString(getElasticSearchDetector(document)); elasticsearchUtil.index(indexRequest, json); return uuid; }  @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 testCreateDetector() { val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); repoUnderTest.createDetector(document); }
@Test(expected = DetectorException.class) public void testCreateDetectorNullValues() { val detector1 = new DetectorDocument(); detector1.setCreatedBy("user"); repoUnderTest.createDetector(detector1); }
@Test(expected = IllegalArgumentException.class) public void testCreateDetectorIllegalThresholds() { repoUnderTest.createDetector(illegalParamsDetector); }
@Test(expected = IllegalArgumentException.class) public void testCreateDetector_uuidAlreadySet() { val document = new DetectorDocument(); document.setUuid(UUID.randomUUID()); repoUnderTest.createDetector(document); }
@Test public void testCreateDetector_emptyMeta() { val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); document.setMeta(null); repoUnderTest.createDetector(document); }
@Test public void testCreateDetector_populatedMeta() { val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); DetectorDocument.Meta metaBlock = new DetectorDocument.Meta(); metaBlock.setCreatedBy("user"); document.setMeta(metaBlock); repoUnderTest.createDetector(document); }
@Test public void testCreateDetector_populatedMeta_noUser() { val mom = ObjectMother.instance(); val document = mom.buildDetectorDocument(); DetectorDocument.Meta metaBlock = new DetectorDocument.Meta(); metaBlock.setCreatedBy(null); document.setMeta(metaBlock); repoUnderTest.createDetector(document); }