From f708dfbad7bacac33b18bb5c7c1221565b803fa5 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 14 Dec 2016 18:08:17 +0100 Subject: [PATCH] SONAR-7292 IssueChangeDao#selectCommentByKey now returns a dto --- .../server/issue/IssueCommentService.java | 4 +-- .../server/issue/IssueCommentServiceTest.java | 12 ++++---- .../org/sonar/db/issue/IssueChangeDao.java | 7 ++++- .../sonar/db/issue/IssueChangeDaoTest.java | 28 ++++++++++++++---- .../org/sonar/db/issue/IssueDbTester.java | 29 ++++++++++++++++++- 5 files changed, 65 insertions(+), 15 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java index 1cb5dbefd60..2107d3d2015 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java @@ -45,7 +45,7 @@ public class IssueCommentService { } public IssueComment deleteComment(String commentKey) { - DefaultIssueComment comment = dbClient.issueChangeDao().selectCommentByKey(commentKey); + DefaultIssueComment comment = dbClient.issueChangeDao().selectDefaultCommentByKey(commentKey); if (comment == null) { throw new NotFoundException("Comment not found: " + commentKey); } @@ -61,7 +61,7 @@ public class IssueCommentService { } public IssueComment editComment(String commentKey, String text) { - DefaultIssueComment comment = dbClient.issueChangeDao().selectCommentByKey(commentKey); + DefaultIssueComment comment = dbClient.issueChangeDao().selectDefaultCommentByKey(commentKey); if (StringUtils.isBlank(text)) { throw new BadRequestException("Cannot add empty comments to an issue"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceTest.java index fd6ccb153af..0bb45cc13b3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceTest.java @@ -76,7 +76,7 @@ public class IssueCommentServiceTest { @Test public void should_delete_comment() { - when(changeDao.selectCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setUserLogin("admin").setIssueKey("EFGH")); + when(changeDao.selectDefaultCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setUserLogin("admin").setIssueKey("EFGH")); issueCommentService.deleteComment("ABCD"); @@ -88,7 +88,7 @@ public class IssueCommentServiceTest { public void should_not_delete_not_found_comment() { throwable.expect(NotFoundException.class); - when(changeDao.selectCommentByKey("ABCD")).thenReturn(null); + when(changeDao.selectDefaultCommentByKey("ABCD")).thenReturn(null); issueCommentService.deleteComment("ABCD"); @@ -99,7 +99,7 @@ public class IssueCommentServiceTest { public void should_prevent_delete_others_comment() { throwable.expect(ForbiddenException.class); - when(changeDao.selectCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setUserLogin("julien")); + when(changeDao.selectDefaultCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setUserLogin("julien")); issueCommentService.deleteComment("ABCD"); @@ -108,7 +108,7 @@ public class IssueCommentServiceTest { @Test public void should_update_comment() { - when(changeDao.selectCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setIssueKey("EFGH").setUserLogin("admin")); + when(changeDao.selectDefaultCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setIssueKey("EFGH").setUserLogin("admin")); issueCommentService.editComment("ABCD", "updated comment"); @@ -120,7 +120,7 @@ public class IssueCommentServiceTest { public void should_not_update_not_found_comment() { throwable.expect(NotFoundException.class); - when(changeDao.selectCommentByKey("ABCD")).thenReturn(null); + when(changeDao.selectDefaultCommentByKey("ABCD")).thenReturn(null); issueCommentService.editComment("ABCD", "updated comment"); @@ -149,7 +149,7 @@ public class IssueCommentServiceTest { public void should_prevent_updating_others_comment() { throwable.expect(ForbiddenException.class); - when(changeDao.selectCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setUserLogin("julien")); + when(changeDao.selectDefaultCommentByKey("ABCD")).thenReturn(new DefaultIssueComment().setUserLogin("julien")); issueCommentService.editComment("ABCD", "updated comment"); diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java index d0c32c1fa47..95e33f0c7bd 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java @@ -22,6 +22,7 @@ package org.sonar.db.issue; import com.google.common.collect.Lists; import java.util.Collection; import java.util.List; +import java.util.Optional; import javax.annotation.CheckForNull; import org.sonar.core.issue.DefaultIssueComment; import org.sonar.core.issue.FieldDiffs; @@ -68,7 +69,7 @@ public class IssueChangeDao implements Dao { } @CheckForNull - public DefaultIssueComment selectCommentByKey(String commentKey) { + public DefaultIssueComment selectDefaultCommentByKey(String commentKey) { DbSession session = mybatis.openSession(false); try { IssueChangeMapper mapper = mapper(session); @@ -84,6 +85,10 @@ public class IssueChangeDao implements Dao { return executeLargeInputs(issueKeys, issueKeys1 -> mapper(session).selectByIssuesAndType(issueKeys1, changeType)); } + public Optional selectCommentByKey(DbSession session, String commentKey) { + return Optional.ofNullable(mapper(session).selectByKeyAndType(commentKey, IssueChangeDto.TYPE_COMMENT)); + } + public void insert(DbSession session, IssueChangeDto change) { mapper(session).insert(change); } diff --git a/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeDaoTest.java b/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeDaoTest.java index 09544234f7e..a1c2397efc6 100644 --- a/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/issue/IssueChangeDaoTest.java @@ -22,6 +22,7 @@ package org.sonar.db.issue; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.DateUtils; @@ -35,7 +36,6 @@ import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; - public class IssueChangeDaoTest { @Rule @@ -75,16 +75,16 @@ public class IssueChangeDaoTest { } @Test - public void select_comment_by_key() { + public void select_default_comment_by_key() { db.prepareDbUnit(getClass(), "shared.xml"); - DefaultIssueComment comment = underTest.selectCommentByKey("FGHIJ"); + DefaultIssueComment comment = underTest.selectDefaultCommentByKey("FGHIJ"); assertThat(comment).isNotNull(); assertThat(comment.key()).isEqualTo("FGHIJ"); assertThat(comment.key()).isEqualTo("FGHIJ"); assertThat(comment.userLogin()).isEqualTo("arthur"); - assertThat(underTest.selectCommentByKey("UNKNOWN")).isNull(); + assertThat(underTest.selectDefaultCommentByKey("UNKNOWN")).isNull(); } @Test @@ -116,6 +116,23 @@ public class IssueChangeDaoTest { assertThat(comments).isEmpty(); } + @Test + public void select_comment_by_key() { + IssueDto issueDto = db.issues().insertIssue(); + IssueChangeDto comment = db.issues().insertComment(issueDto, "john", "some comment"); + + Optional issueChangeDto = underTest.selectCommentByKey(db.getSession(), comment.getKey()); + + assertThat(issueChangeDto).isPresent(); + assertThat(issueChangeDto.get().getKey()).isEqualTo(comment.getKey()); + assertThat(issueChangeDto.get().getChangeType()).isEqualTo(IssueChangeDto.TYPE_COMMENT); + assertThat(issueChangeDto.get().getUserLogin()).isEqualTo("john"); + assertThat(issueChangeDto.get().getChangeData()).isEqualTo("some comment"); + assertThat(issueChangeDto.get().getIssueChangeCreationDate()).isNotNull(); + assertThat(issueChangeDto.get().getCreatedAt()).isNotNull(); + assertThat(issueChangeDto.get().getUpdatedAt()).isNotNull(); + } + @Test public void delete() { db.prepareDbUnit(getClass(), "delete.xml"); @@ -147,7 +164,7 @@ public class IssueChangeDaoTest { underTest.insert(db.getSession(), changeDto); db.getSession().commit(); - db.assertDbUnit(getClass(), "insert-result.xml", new String[]{"id"}, "issue_changes"); + db.assertDbUnit(getClass(), "insert-result.xml", new String[] {"id"}, "issue_changes"); } @Test @@ -179,4 +196,5 @@ public class IssueChangeDaoTest { assertThat(underTest.update(change)).isFalse(); } + } diff --git a/sonar-db/src/test/java/org/sonar/db/issue/IssueDbTester.java b/sonar-db/src/test/java/org/sonar/db/issue/IssueDbTester.java index 62d034a013f..e9ef4e5e6e3 100644 --- a/sonar-db/src/test/java/org/sonar/db/issue/IssueDbTester.java +++ b/sonar-db/src/test/java/org/sonar/db/issue/IssueDbTester.java @@ -21,8 +21,16 @@ package org.sonar.db.issue; import java.util.Arrays; +import javax.annotation.Nullable; +import org.sonar.core.issue.DefaultIssueComment; import org.sonar.core.issue.FieldDiffs; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.rule.RuleDto; + +import static org.sonar.db.component.ComponentTesting.newFileDto; +import static org.sonar.db.issue.IssueTesting.newDto; +import static org.sonar.db.rule.RuleTesting.newRuleDto; public class IssueDbTester { @@ -38,8 +46,27 @@ public class IssueDbTester { return issueDto; } - public void insertIssueChanges(IssueDto issueDto, FieldDiffs... diffs) { + public IssueDto insertIssue() { + RuleDto rule = db.rules().insertRule(newRuleDto()); + ComponentDto project = db.components().insertProject(); + ComponentDto file = db.components().insertComponent(newFileDto(project)); + return insertIssue(newDto(rule, file, project)); + } + + public IssueChangeDto insertChange(IssueChangeDto issueChangeDto) { + db.getDbClient().issueChangeDao().insert(db.getSession(), issueChangeDto); + db.commit(); + return issueChangeDto; + } + + public IssueChangeDto insertComment(IssueDto issueDto, @Nullable String login, String text) { + IssueChangeDto issueChangeDto = IssueChangeDto.of(DefaultIssueComment.create(issueDto.getKey(), login, text)); + return insertChange(issueChangeDto); + } + + public void insertFieldDiffs(IssueDto issueDto, FieldDiffs... diffs) { Arrays.stream(diffs).forEach(diff -> db.getDbClient().issueChangeDao().insert(db.getSession(), IssueChangeDto.of(issueDto.getKey(), diff))); db.commit(); } + } -- 2.39.5