]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7292 IssueChangeDao#selectCommentByKey now returns a dto
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 14 Dec 2016 17:08:17 +0000 (18:08 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 15 Dec 2016 13:45:04 +0000 (14:45 +0100)
server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java
server/sonar-server/src/test/java/org/sonar/server/issue/IssueCommentServiceTest.java
sonar-db/src/main/java/org/sonar/db/issue/IssueChangeDao.java
sonar-db/src/test/java/org/sonar/db/issue/IssueChangeDaoTest.java
sonar-db/src/test/java/org/sonar/db/issue/IssueDbTester.java

index 1cb5dbefd60f04dda2a5d53737c6bb75acc6a321..2107d3d201583427704c85e1b19e51248acbf843 100644 (file)
@@ -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");
     }
index fd6ccb153afc6122efe54eedbdff0cf92c595506..0bb45cc13b38c67d83b02930163d965c358bc032 100644 (file)
@@ -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");
 
index d0c32c1fa47b2bb652b0f06c54b1650b5f39f62c..95e33f0c7bd54d6b3fafc5dad67ac184f8501964 100644 (file)
@@ -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<IssueChangeDto> 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);
   }
index 09544234f7e8671573541823d2e36d24ed85a2c6..a1c2397efc62cb6b61f4060312c9086231d3be45 100644 (file)
@@ -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> 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();
   }
+
 }
index 62d034a013fa71273b07659aef63e17ebd7b5a61..e9ef4e5e6e3b8f95e30b54ce1651aad744cf36e0 100644 (file)
 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();
   }
+
 }