@Override @SuppressWarnings("unchecked") public Document addCommentToAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to add comment. Document with guid %s cannot be found.", request.getGuid())); } List<Map<String, Object>> commentsField = (List<Map<String, Object>>) latest.getDocument() .getOrDefault(COMMENTS_FIELD, new ArrayList<>()); List<Map<String, Object>> originalComments = new ArrayList<>(commentsField); originalComments.add( new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp()) .asMap()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, originalComments); return update(newVersion, Optional.empty()); }
@Override public Document addCommentToAlert(CommentAddRemoveRequest request) throws IOException { Document latest = retrieveLatestDao.getLatest(request.getGuid(), request.getSensorType()); return addCommentToAlert(request, latest); }
private CommentAddRemoveRequest buildAlertRequest(String guid, String comment, String username, long timestamp) { CommentAddRemoveRequest request = new CommentAddRemoveRequest(); request.setGuid(guid); request.setComment(comment); request.setUsername(username); request.setTimestamp(timestamp); request.setSensorType(SENSOR_NAME); return request; }
@Test public void addCommentShouldAddCommentToAlert() throws Exception { Document latest = mock(Document.class); Document document1 = new Document(new HashMap<>(), "guid", "bro", 1L); Document document2 = new Document(new HashMap<>(), "guid", "bro", 2L); CommentAddRemoveRequest request = new CommentAddRemoveRequest(); request.setGuid("guid"); when(dao1.addCommentToAlert(request, latest)).thenReturn(document1); when(dao2.addCommentToAlert(request, latest)).thenReturn(document2); Document expected = new Document(new HashMap<>(), "guid", "bro", 2L); Assert.assertEquals(expected, multiIndexDao.addCommentToAlert(request, latest)); }
@Test public void getLatestShouldReturnLatestAlert() throws Exception { Document document1 = new Document(new HashMap<>(), "guid", "bro", 1L); Document document2 = new Document(new HashMap<>(), "guid", "bro", 2L); CommentAddRemoveRequest request = new CommentAddRemoveRequest(); request.setGuid("guid"); when(dao1.getLatest("guid", "bro")).thenReturn(document1); when(dao2.getLatest("guid", "bro")).thenReturn(document2); Document expected = new Document(new HashMap<>(), "guid", "bro", 2L); Assert.assertEquals(expected, multiIndexDao.getLatest("guid", "bro")); }
@Override public Document removeCommentFromAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to remove comment. Document with guid %s cannot be found.", request.getGuid())); } @SuppressWarnings("unchecked") List<Map<String, Object>> commentMap = (List<Map<String, Object>>) latest.getDocument() .get(COMMENTS_FIELD); // Can't remove anything if there's nothing there if (commentMap == null) { throw new IOException(String.format("Unable to remove comment. Document with guid %s has no comments.", request.getGuid())); } List<Map<String, Object>> originalComments = new ArrayList<>(commentMap); List<AlertComment> comments = new ArrayList<>(); for (Map<String, Object> commentStr : originalComments) { comments.add(new AlertComment(commentStr)); } comments.remove( new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp())); List<String> commentsAsJson = comments.stream().map(AlertComment::asJson) .collect(Collectors.toList()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, commentsAsJson); return update(newVersion, Optional.empty()); }
@Override @SuppressWarnings("unchecked") public Document addCommentToAlert(CommentAddRemoveRequest request) throws IOException { Document latest = getLatest(request.getGuid(), request.getSensorType()); return addCommentToAlert(request, latest); }
@Test public void removeCommentShouldRemoveCommentFromAlert() throws Exception { Document latest = mock(Document.class); Document document1 = new Document(new HashMap<>(), "guid", "bro", 1L); Document document2 = new Document(new HashMap<>(), "guid", "bro", 2L); CommentAddRemoveRequest request = new CommentAddRemoveRequest(); request.setGuid("guid"); when(dao1.removeCommentFromAlert(request, latest)).thenReturn(document1); when(dao2.removeCommentFromAlert(request, latest)).thenReturn(document2); Document expected = new Document(new HashMap<>(), "guid", "bro", 2L); Assert.assertEquals(expected, multiIndexDao.removeCommentFromAlert(request, latest)); } }
@Override @SuppressWarnings("unchecked") public Document addCommentToAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to add comment. Document with guid %s cannot be found.", request.getGuid())); } List<Map<String, Object>> comments = (List<Map<String, Object>>) latest.getDocument() .getOrDefault(COMMENTS_FIELD, new ArrayList<>()); List<Map<String, Object>> originalComments = new ArrayList<>(comments); // Convert all comments back to raw JSON before updating. List<Map<String, Object>> commentsMap = new ArrayList<>(); for (Map<String, Object> comment : originalComments) { commentsMap.add(new AlertComment(comment).asMap()); } commentsMap.add(new AlertComment( request.getComment(), request.getUsername(), request.getTimestamp()) .asMap()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, commentsMap); return update(newVersion, Optional.empty()); }
@Override @SuppressWarnings("unchecked") public Document removeCommentFromAlert(CommentAddRemoveRequest request) throws IOException { Document latest = retrieveLatestDao.getLatest(request.getGuid(), request.getSensorType()); return removeCommentFromAlert(request, latest); }
@Test public void removeCommentShouldThrowExceptionOnEmptyComments() throws Exception { exception.expect(IOException.class); exception.expectMessage("Unable to remove comment. Document with guid guid has no comments."); CommentAddRemoveRequest request = new CommentAddRemoveRequest(); request.setGuid("guid"); Document latest = new Document(new HashMap<>(), "guid", "bro", System.currentTimeMillis()); getUpdateDao().removeCommentFromAlert(request, latest); }
if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to remove comment. Document with guid %s cannot be found.", request.getGuid())); request.getGuid())); comments.remove(new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp())); Document newVersion = new Document(latest); if (comments.size() > 0) {
@Override public Document addCommentToAlert(CommentAddRemoveRequest request) throws IOException { Document latest = getLatest(request.getGuid(), request.getSensorType()); return addCommentToAlert(request, latest); }
@Test public void addCommentShouldThrowExceptionOnMissingAlert() throws Exception { exception.expect(IOException.class); exception.expectMessage("Unable to add comment. Document with guid guid cannot be found."); CommentAddRemoveRequest request = new CommentAddRemoveRequest(); request.setGuid("guid"); getUpdateDao().addCommentToAlert(request, null); }
if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to remove comment. Document with guid %s cannot be found.", request.getGuid())); request.getGuid())); new AlertComment(request.getComment(), request.getUsername(), request.getTimestamp())); List<Map<String, Object>> commentsFinal = alertComments.stream().map(AlertComment::asMap) .collect(Collectors.toList());
@Override public Document removeCommentFromAlert(CommentAddRemoveRequest request) throws IOException { Document latest = retrieveLatestDao.getLatest(request.getGuid(), request.getSensorType()); return removeCommentFromAlert(request, latest); }
@Test public void removeCommentShouldThrowExceptionOnMissingAlert() throws Exception { exception.expect(IOException.class); exception.expectMessage("Unable to remove comment. Document with guid guid cannot be found."); CommentAddRemoveRequest request = new CommentAddRemoveRequest(); request.setGuid("guid"); getUpdateDao().removeCommentFromAlert(request, null); }
@Override public Document addCommentToAlert(CommentAddRemoveRequest request, Document latest) throws IOException { if (latest == null || latest.getDocument() == null) { throw new IOException(String.format("Unable to add comment. Document with guid %s cannot be found.", request.getGuid())); } @SuppressWarnings("unchecked") List<Map<String, Object>> comments = (List<Map<String, Object>>) latest.getDocument() .getOrDefault(COMMENTS_FIELD, new ArrayList<>()); List<Map<String, Object>> originalComments = new ArrayList<>(comments); // Convert all comments back to raw JSON before updating. List<String> commentStrs = new ArrayList<>(); for (Map<String, Object> comment : originalComments) { commentStrs.add(new AlertComment(comment).asJson()); } commentStrs.add(new AlertComment( request.getComment(), request.getUsername(), request.getTimestamp() ).asJson()); Document newVersion = new Document(latest); newVersion.getDocument().put(COMMENTS_FIELD, commentStrs); return update(newVersion, Optional.empty()); }
@Override public Document removeCommentFromAlert(CommentAddRemoveRequest request) throws IOException { Document latest = getLatest(request.getGuid(), request.getSensorType()); return removeCommentFromAlert(request, latest); }
@Override @SuppressWarnings("unchecked") public Document removeCommentFromAlert(CommentAddRemoveRequest request) throws IOException { Document latest = getLatest(request.getGuid(), request.getSensorType()); return removeCommentFromAlert(request, latest); }