Skip to content

Commit

Permalink
SONAR-9993 Functional creation date of comment is no more nullable
Browse files Browse the repository at this point in the history
 Old comments do not have functional creation date as this column has been added later
  • Loading branch information
julienlancelot committed Nov 1, 2017
1 parent 7dd92e6 commit 7848297
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 253 deletions.
Expand Up @@ -30,6 +30,7 @@
import org.sonar.core.issue.FieldDiffs;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* @since 3.6
Expand All @@ -51,6 +52,7 @@ public final class IssueChangeDto implements Serializable {
private Long updatedAt;

// functional date
@Nullable
private Long issueChangeCreationDate;

public static IssueChangeDto of(DefaultIssueComment comment) {
Expand All @@ -59,7 +61,8 @@ public static IssueChangeDto of(DefaultIssueComment comment) {
dto.setChangeType(IssueChangeDto.TYPE_COMMENT);
dto.setChangeData(comment.markdownText());
dto.setUserLogin(comment.userLogin());
dto.setIssueChangeCreationDate(comment.createdAt() == null ? null : comment.createdAt().getTime());
Date createdAt = requireNonNull(comment.createdAt(), "Comment created at must not be null");
dto.setIssueChangeCreationDate(createdAt.getTime());
return dto;
}

Expand All @@ -68,7 +71,8 @@ public static IssueChangeDto of(String issueKey, FieldDiffs diffs) {
dto.setChangeType(IssueChangeDto.TYPE_FIELD_CHANGE);
dto.setChangeData(diffs.toString());
dto.setUserLogin(diffs.userLogin());
dto.setIssueChangeCreationDate(diffs.creationDate() == null ? null : diffs.creationDate().getTime());
Date createdAt = requireNonNull(diffs.creationDate(), "Diffs created at must not be null");
dto.setIssueChangeCreationDate(createdAt.getTime());
return dto;
}

Expand Down Expand Up @@ -157,10 +161,11 @@ public IssueChangeDto setUpdatedAt(@Nullable Long updatedAt) {
}

public Long getIssueChangeCreationDate() {
return issueChangeCreationDate;
// Old comments do not have functional creation date as this column has been added later
return issueChangeCreationDate == null ? createdAt : issueChangeCreationDate;
}

public IssueChangeDto setIssueChangeCreationDate(@Nullable Long issueChangeCreationDate) {
public IssueChangeDto setIssueChangeCreationDate(long issueChangeCreationDate) {
this.issueChangeCreationDate = issueChangeCreationDate;
return this;
}
Expand All @@ -174,7 +179,7 @@ public DefaultIssueComment toComment() {
return new DefaultIssueComment()
.setMarkdownText(changeData)
.setKey(kee)
.setCreatedAt(new Date(createdAt))
.setCreatedAt(new Date(getIssueChangeCreationDate()))
.setUpdatedAt(updatedAt == null ? null : new Date(updatedAt))
.setUserLogin(userLogin)
.setIssueKey(issueKey)
Expand All @@ -184,8 +189,7 @@ public DefaultIssueComment toComment() {
public FieldDiffs toFieldDiffs() {
return FieldDiffs.parse(changeData)
.setUserLogin(userLogin)
// issueChangeCreationDate can be null as it has been introduced after createdAt
.setCreationDate(issueChangeCreationDate != null ? new Date(issueChangeCreationDate) : new Date(createdAt))
.setCreationDate(new Date(getIssueChangeCreationDate()))
.setIssueKey(issueKey);
}
}
Expand Up @@ -59,16 +59,17 @@ public void create_from_diff() {
FieldDiffs diffs = new FieldDiffs();
diffs.setDiff("severity", "INFO", "BLOCKER");
diffs.setUserLogin("emmerik");
diffs.setCreationDate(parseDate("2015-01-13"));

IssueChangeDto dto = IssueChangeDto.of("ABCDE", diffs);

assertThat(dto.getChangeData()).isEqualTo("severity=INFO|BLOCKER");
assertThat(dto.getChangeType()).isEqualTo("diff");
assertThat(dto.getCreatedAt()).isNotNull();
assertThat(dto.getUpdatedAt()).isNotNull();
assertThat(dto.getIssueChangeCreationDate()).isNull();
assertThat(dto.getIssueKey()).isEqualTo("ABCDE");
assertThat(dto.getUserLogin()).isEqualTo("emmerik");
assertThat(dto.getIssueChangeCreationDate()).isEqualTo(parseDate("2015-01-13").getTime());
}

@Test
Expand Down Expand Up @@ -132,6 +133,19 @@ public void to_field_diffs_with_create_at() {
assertThat(diffs.creationDate()).isNotNull();
}

@Test
public void getIssueChangeCreationDate_fallback_to_createAt_when_null() {
IssueChangeDto changeDto = new IssueChangeDto()
.setKey("EFGH")
.setUserLogin("emmerik")
.setChangeData("Some text")
.setIssueKey("ABCDE")
.setCreatedAt(10_000_000L)
.setUpdatedAt(20_000_000L);

assertThat(changeDto.getIssueChangeCreationDate()).isEqualTo(10_000_000L);
}

@Test
public void to_string() {
DefaultIssueComment comment = DefaultIssueComment.create("ABCDE", "emmerik", "the comment");
Expand Down
Expand Up @@ -57,6 +57,7 @@ public void insert_comment() {
dto.setChangeData("the comment");
dto.setCreatedAt(1_500_000_000_000L);
dto.setUpdatedAt(1_500_000_000_000L);
dto.setIssueChangeCreationDate(1_500_000_000_000L);
dbTester.getSession().getMapper(IssueChangeMapper.class).insert(dto);
dbTester.getSession().commit();

Expand Down
Expand Up @@ -8,6 +8,6 @@
change_data="the comment"
created_at="1500000000000"
updated_at="1500000000000"
issue_change_creation_date="[null]"
issue_change_creation_date="1500000000000"
/>
</dataset>
Expand Up @@ -193,11 +193,11 @@ public void lazy_load_changes() {
IssueDto issue1 = db.issues()
.insertIssue(IssueTesting.newIssue(rule, branch1Dto, fileOnBranch1Dto).setKee("issue1").setStatus(Issue.STATUS_REOPENED).setLine(1).setChecksum("checksum"));
db.issues().insertComment(issue1, "user1", "A comment 1");
db.issues().insertFieldDiffs(issue1, FieldDiffs.parse("severity=BLOCKER|INFO,assignee=toto|titi"));
db.issues().insertFieldDiffs(issue1, FieldDiffs.parse("severity=BLOCKER|INFO,assignee=toto|titi").setCreationDate(new Date()));
IssueDto issue2 = db.issues()
.insertIssue(IssueTesting.newIssue(rule, branch2Dto, fileOnBranch2Dto).setKee("issue2").setStatus(Issue.STATUS_CONFIRMED).setLine(1).setChecksum("checksum"));
db.issues().insertComment(issue2, "user2", "A comment 2");
db.issues().insertFieldDiffs(issue2, FieldDiffs.parse("severity=BLOCKER|MINOR,assignee=foo|bar"));
db.issues().insertFieldDiffs(issue2, FieldDiffs.parse("severity=BLOCKER|MINOR,assignee=foo|bar").setCreationDate(new Date()));
DefaultIssue newIssue = createIssue("newIssue", rule.getKey(), Issue.STATUS_OPEN, null, new Date());

copier.tryMerge(FILE_1, Collections.singleton(newIssue));
Expand Down

0 comments on commit 7848297

Please sign in to comment.