}
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);
}
}
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");
}
@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");
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");
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");
@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");
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");
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");
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;
}
@CheckForNull
- public DefaultIssueComment selectCommentByKey(String commentKey) {
+ public DefaultIssueComment selectDefaultCommentByKey(String commentKey) {
DbSession session = mybatis.openSession(false);
try {
IssueChangeMapper mapper = mapper(session);
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);
}
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;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
-
public class IssueChangeDaoTest {
@Rule
}
@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
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");
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
assertThat(underTest.update(change)).isFalse();
}
+
}
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 {
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();
}
+
}