private Consumer<CommentData> deleteComment(DbSession dbSession) { return commentData -> { dbClient.issueChangeDao().delete(dbSession, commentData.getIssueChangeDto().getKey()); dbSession.commit(); }; }
private void loadComments(Collector collector, DbSession dbSession, Set<SearchAdditionalField> fields, SearchResponseData result) { if (fields.contains(COMMENTS)) { List<IssueChangeDto> comments = dbClient.issueChangeDao().selectByTypeAndIssueKeys(dbSession, collector.getIssueKeys(), IssueChangeDto.TYPE_COMMENT); result.setComments(comments); for (IssueChangeDto comment : comments) { collector.addUserUuids(singletonList(comment.getUserUuid())); if (canEditOrDelete(comment)) { result.addUpdatableComment(comment.getKey()); } } } }
@Test public void edit_comment() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); loginWithBrowsePermission(user, USER, issueDto); call(commentDto.getKey(), "please have a look"); verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); verifyContentOfPreloadedSearchResponseData(issueDto); IssueChangeDto issueComment = dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey()).get(); assertThat(issueComment.getChangeData()).isEqualTo("please have a look"); assertThat(issueComment.getUpdatedAt()).isEqualTo(NOW); }
@Test public void select_comment_by_key() { IssueDto issue = db.issues().insertIssue(); IssueChangeDto issueChange = db.issues().insertChange(issue, c -> c.setChangeType(TYPE_COMMENT)); db.issues().insertChange(issue, c -> c.setChangeType(TYPE_COMMENT)); Optional<IssueChangeDto> issueChangeDto = underTest.selectCommentByKey(db.getSession(), issueChange.getKey()); assertThat(issueChangeDto).isPresent(); assertThat(issueChangeDto.get().getKey()).isEqualTo(issueChange.getKey()); assertThat(issueChangeDto.get().getChangeType()).isEqualTo(TYPE_COMMENT); assertThat(issueChangeDto.get().getUserUuid()).isEqualTo(issueChange.getUserUuid()); assertThat(issueChangeDto.get().getChangeData()).isEqualTo(issueChange.getChangeData()); assertThat(issueChangeDto.get().getIssueChangeCreationDate()).isNotNull(); assertThat(issueChangeDto.get().getCreatedAt()).isNotNull(); assertThat(issueChangeDto.get().getUpdatedAt()).isNotNull(); }
@Test public void delete_comment() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); loginAndAddProjectPermission(user, issueDto, USER); call(commentDto.getKey()); verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); assertThat(dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey())).isNotPresent(); verifyContentOfPreloadedSearchResponseData(issueDto); }
@Test public void edit_comment_using_deprecated_key_parameter() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); loginWithBrowsePermission(user, USER, issueDto); tester.newRequest().setParam("key", commentDto.getKey()).setParam("text", "please have a look").execute(); verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); verifyContentOfPreloadedSearchResponseData(issueDto); IssueChangeDto issueComment = dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey()).get(); assertThat(issueComment.getChangeData()).isEqualTo("please have a look"); assertThat(issueComment.getUpdatedAt()).isEqualTo(NOW); }
@Test public void delete_comment_using_deprecated_key_parameter() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); loginAndAddProjectPermission(user, issueDto, USER); tester.newRequest().setParam("key", commentDto.getKey()).setParam("text", "please have a look").execute(); verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); assertThat(dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey())).isNotPresent(); verifyContentOfPreloadedSearchResponseData(issueDto); }
private static void formatIssueComments(SearchResponseData data, Issue.Builder wsIssue, IssueDto dto) { Comments.Builder wsComments = Comments.newBuilder(); List<IssueChangeDto> comments = data.getCommentsForIssueKey(dto.getKey()); if (comments != null) { Comment.Builder wsComment = Comment.newBuilder(); for (IssueChangeDto comment : comments) { String markdown = comment.getChangeData(); wsComment .clear() .setKey(comment.getKey()) .setUpdatable(data.isUpdatableComment(comment.getKey())) .setCreatedAt(DateUtils.formatDateTime(new Date(comment.getIssueChangeCreationDate()))); ofNullable(data.getUserByUuid(comment.getUserUuid())).ifPresent(user -> wsComment.setLogin(user.getLogin())); if (markdown != null) { wsComment .setHtmlText(Markdown.convertToHtml(markdown)) .setMarkdown(markdown); } wsComments.addComments(wsComment); } } wsIssue.setComments(wsComments); }
@Test public void add_comment() { IssueDto issueDto = issueDbTester.insertIssue(); loginWithBrowsePermission(issueDto, USER); call(issueDto.getKey(), "please fix it"); verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class)); verifyContentOfPreloadedSearchResponseData(issueDto); IssueChangeDto issueComment = dbClient.issueChangeDao().selectByTypeAndIssueKeys(dbTester.getSession(), singletonList(issueDto.getKey()), TYPE_COMMENT).get(0); assertThat(issueComment.getKey()).isNotNull(); assertThat(issueComment.getUserUuid()).isEqualTo(userSession.getUuid()); assertThat(issueComment.getChangeType()).isEqualTo(TYPE_COMMENT); assertThat(issueComment.getChangeData()).isEqualTo("please fix it"); assertThat(issueComment.getCreatedAt()).isNotNull(); assertThat(issueComment.getUpdatedAt()).isNotNull(); assertThat(issueComment.getIssueKey()).isEqualTo(issueDto.getKey()); assertThat(issueComment.getIssueChangeCreationDate()).isNotNull(); IssueDto issueReloaded = dbClient.issueDao().selectByKey(dbTester.getSession(), issueDto.getKey()).get(); assertThat(issueReloaded.getIssueUpdateTime()).isEqualTo(NOW); assertThat(issueChangePostProcessor.wasCalled()).isFalse(); }
@Test public void delete() { IssueDto issue = db.issues().insertIssue(); IssueChangeDto issueChange1 = db.issues().insertChange(issue); IssueChangeDto issueChange2 = db.issues().insertChange(issue); assertThat(underTest.delete(db.getSession(), issueChange1.getKey())).isTrue(); assertThat(db.countRowsOfTable(db.getSession(), "issue_changes")).isEqualTo(1); }
@Test public void scrollDiffChangesOfIssues_orders_changes_by_issue_and_then_creationDate() { IssueDto issue1 = db.issues().insertIssue(); IssueChangeDto[] diffChanges = { db.issues().insertChange(issue1, t -> t.setChangeType(TYPE_FIELD_CHANGE).setCreatedAt(1L).setIssueChangeCreationDate(50L)), db.issues().insertChange(issue1, t -> t.setChangeType(TYPE_FIELD_CHANGE).setCreatedAt(2L).setIssueChangeCreationDate(20L)), db.issues().insertChange(issue1, t -> t.setChangeType(TYPE_FIELD_CHANGE).setCreatedAt(3L).setIssueChangeCreationDate(30L)), db.issues().insertChange(issue1, t -> t.setChangeType(TYPE_FIELD_CHANGE).setCreatedAt(4L).setIssueChangeCreationDate(80L)), db.issues().insertChange(issue1, t -> t.setChangeType(TYPE_FIELD_CHANGE).setCreatedAt(5L).setIssueChangeCreationDate(10L)), }; RecordingIssueChangeDtoResultHandler recordingHandler = new RecordingIssueChangeDtoResultHandler(); underTest.scrollDiffChangesOfIssues(db.getSession(), of(issue1.getKee()), recordingHandler.clear()); assertThat(recordingHandler.getDtoKeys()).containsExactly( diffChanges[3].getKey(), diffChanges[0].getKey(), diffChanges[2].getKey(), diffChanges[1].getKey(), diffChanges[4].getKey()); }
@Test public void fail_when_empty_comment_text() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); loginWithBrowsePermission(user, USER, issueDto); expectedException.expect(IllegalArgumentException.class); call(commentDto.getKey(), ""); }
@Test public void scrollDiffChangesOfIssues_scrolls_only_diff_changes_of_selected_issues() { IssueDto issue1 = db.issues().insertIssue(); IssueChangeDto diffChange1 = db.issues().insertChange(issue1, t -> t.setChangeType(TYPE_FIELD_CHANGE)); db.issues().insertChange(issue1, t -> t.setChangeType(TYPE_COMMENT)); IssueDto issue2 = db.issues().insertIssue(); IssueChangeDto diffChange2 = db.issues().insertChange(issue2, t -> t.setChangeType(TYPE_FIELD_CHANGE)); db.issues().insertChange(issue2, t -> t.setChangeType(TYPE_COMMENT)); IssueDto issue3 = db.issues().insertIssue(); IssueChangeDto diffChange31 = db.issues().insertChange(issue3, t -> t.setChangeType(TYPE_FIELD_CHANGE)); IssueChangeDto diffChange32 = db.issues().insertChange(issue3, t -> t.setChangeType(TYPE_FIELD_CHANGE)); db.issues().insertChange(issue3, t -> t.setChangeType(TYPE_COMMENT)); RecordingIssueChangeDtoResultHandler recordingHandler = new RecordingIssueChangeDtoResultHandler(); underTest.scrollDiffChangesOfIssues(db.getSession(), of(), recordingHandler.clear()); assertThat(recordingHandler.getDtoKeys()).isEmpty(); underTest.scrollDiffChangesOfIssues(db.getSession(), of("fooBarCacahuete"), recordingHandler.clear()); assertThat(recordingHandler.getDtoKeys()).isEmpty(); underTest.scrollDiffChangesOfIssues(db.getSession(), of(issue1.getKee()), recordingHandler.clear()); assertThat(recordingHandler.getDtoKeys()).containsOnly(diffChange1.getKey()); underTest.scrollDiffChangesOfIssues(db.getSession(), of(issue2.getKee()), recordingHandler.clear()); assertThat(recordingHandler.getDtoKeys()).containsOnly(diffChange2.getKey()); underTest.scrollDiffChangesOfIssues(db.getSession(), of(issue1.getKee(), issue3.getKee()), recordingHandler.clear()); assertThat(recordingHandler.getDtoKeys()).containsOnly(diffChange1.getKey(), diffChange31.getKey(), diffChange32.getKey()); }
@Test public void fail_when_not_enough_permission() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); loginAndAddProjectPermission(user, issueDto, CODEVIEWER); expectedException.expect(ForbiddenException.class); call(commentDto.getKey()); }
@Test public void fail_when_not_enough_permission() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); loginWithBrowsePermission(user, CODEVIEWER, issueDto); expectedException.expect(ForbiddenException.class); call(commentDto.getKey(), "please have a look"); }
@Test public void fail_when_comment_has_not_user() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, null, "please fix it"); loginAndAddProjectPermission(user, issueDto, USER); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("You can only delete your own comments"); call(commentDto.getKey()); }
@Test public void fail_when_comment_has_not_user() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, null, "please fix it"); loginWithBrowsePermission(user, USER, issueDto); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("You can only edit your own comments"); call(commentDto.getKey(), "please have a look"); }
@Test public void update_unknown_key() { IssueDto issue = db.issues().insertIssue(); IssueChangeDto issueChange = db.issues().insertChange(issue); assertThat(underTest.update(db.getSession(), new IssueChangeDto() .setKey("UNKNOWN") .setIssueKey("other_issue_uuid") .setChangeData("new comment") .setUpdatedAt(DateUtils.parseDate("2013-06-30").getTime()))) .isFalse(); assertThat(underTest.selectByIssueKeys(db.getSession(), singletonList(issue.getKey()))) .extracting(IssueChangeDto::getKey, IssueChangeDto::getIssueKey, IssueChangeDto::getChangeData, IssueChangeDto::getChangeType, IssueChangeDto::getIssueChangeCreationDate, IssueChangeDto::getCreatedAt, IssueChangeDto::getUpdatedAt) .containsExactlyInAnyOrder( tuple(issueChange.getKey(), issue.getKey(), issueChange.getChangeData(), issueChange.getChangeType(), issueChange.getIssueChangeCreationDate(), issueChange.getCreatedAt(), issueChange.getUpdatedAt())); }
@Test public void fail_when_comment_does_not_belong_to_current_user() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); UserDto another = dbTester.users().insertUser(); loginWithBrowsePermission(another, USER, issueDto); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("You can only edit your own comments"); call(commentDto.getKey(), "please have a look"); }
@Test public void fail_when_comment_does_not_belong_to_current_user() { IssueDto issueDto = issueDbTester.insertIssue(); UserDto user = dbTester.users().insertUser(); IssueChangeDto commentDto = issueDbTester.insertComment(issueDto, user, "please fix it"); UserDto another = dbTester.users().insertUser(); loginAndAddProjectPermission(another, issueDto, USER); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("You can only delete your own comments"); call(commentDto.getKey()); }