diff options
author | Antoine Vinot <antoine.vinot@sonarsource.com> | 2022-09-13 10:27:42 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-09-19 20:03:08 +0000 |
commit | 9d1fe387d11c2f27c8463b7bd05497ee1418c31d (patch) | |
tree | a43a39503f9516a4cdee550084f684b983dadc3a /sonar-core | |
parent | fa0c45f87a15dfca332ff46ab5b3061b5c4c3efa (diff) | |
download | sonarqube-9d1fe387d11c2f27c8463b7bd05497ee1418c31d.tar.gz sonarqube-9d1fe387d11c2f27c8463b7bd05497ee1418c31d.zip |
SONAR-17271 - Add origin on issue update from ALM
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/issue/IssueChangeContext.java | 77 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/issue/IssueChangeContextTest.java | 79 |
2 files changed, 136 insertions, 20 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/issue/IssueChangeContext.java b/sonar-core/src/main/java/org/sonar/core/issue/IssueChangeContext.java index b6fae7d532f..a81a0ac2224 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/IssueChangeContext.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/IssueChangeContext.java @@ -25,16 +25,22 @@ import java.util.Objects; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import static java.util.Objects.requireNonNull; + public class IssueChangeContext implements Serializable { private final String userUuid; private final Date date; private final boolean scan; + private final boolean refreshMeasures; + private final boolean fromAlm; - private IssueChangeContext(@Nullable String userUuid, Date date, boolean scan) { + private IssueChangeContext(@Nullable String userUuid, Date date, boolean scan, boolean refreshMeasures, boolean fromAlm) { this.userUuid = userUuid; - this.date = date; + this.date = requireNonNull(date); this.scan = scan; + this.refreshMeasures = refreshMeasures; + this.fromAlm = fromAlm; } @CheckForNull @@ -50,12 +56,12 @@ public class IssueChangeContext implements Serializable { return scan; } - public static IssueChangeContext createScan(Date date) { - return new IssueChangeContext(null, date, true); + public boolean refreshMeasures() { + return refreshMeasures; } - public static IssueChangeContext createUser(Date date, @Nullable String userUuid) { - return new IssueChangeContext(userUuid, date, false); + public boolean fromAlm() { + return fromAlm; } @Override @@ -69,12 +75,13 @@ public class IssueChangeContext implements Serializable { IssueChangeContext that = (IssueChangeContext) o; return scan == that.scan && Objects.equals(userUuid, that.userUuid) && - Objects.equals(date, that.date); + Objects.equals(date, that.date) && + refreshMeasures == that.refreshMeasures; } @Override public int hashCode() { - return Objects.hash(userUuid, date, scan); + return Objects.hash(userUuid, date, scan, refreshMeasures, fromAlm); } @Override @@ -83,6 +90,60 @@ public class IssueChangeContext implements Serializable { "userUuid='" + userUuid + '\'' + ", date=" + date + ", scan=" + scan + + ", refreshMeasures=" + refreshMeasures + + ", fromAlm=" + fromAlm + '}'; } + + public static IssueChangeContextBuilder newBuilder() { + return new IssueChangeContextBuilder(); + } + + public static IssueChangeContextBuilder issueChangeContextByScanBuilder(Date date) { + return newBuilder().withScan().setUserUuid(null).setDate(date); + } + + public static IssueChangeContextBuilder issueChangeContextByUserBuilder(Date date, @Nullable String userUuid) { + return newBuilder().setUserUuid(userUuid).setDate(date); + } + + public static final class IssueChangeContextBuilder { + private String userUuid; + private Date date; + private boolean scan = false; + private boolean refreshMeasures = false; + private boolean fromAlm = false; + + private IssueChangeContextBuilder() { + } + + public IssueChangeContextBuilder setUserUuid(@Nullable String userUuid) { + this.userUuid = userUuid; + return this; + } + + public IssueChangeContextBuilder setDate(Date date) { + this.date = date; + return this; + } + + public IssueChangeContextBuilder withScan() { + this.scan = true; + return this; + } + + public IssueChangeContextBuilder withRefreshMeasures() { + this.refreshMeasures = true; + return this; + } + + public IssueChangeContextBuilder withFromAlm() { + this.fromAlm = true; + return this; + } + + public IssueChangeContext build() { + return new IssueChangeContext(userUuid, date, scan, refreshMeasures, fromAlm); + } + } } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/IssueChangeContextTest.java b/sonar-core/src/test/java/org/sonar/core/issue/IssueChangeContextTest.java index ab3c40118b7..d5467d90002 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/IssueChangeContextTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/IssueChangeContextTest.java @@ -20,28 +20,83 @@ package org.sonar.core.issue; import java.util.Date; +import java.util.Objects; +import javax.annotation.Nullable; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByScanBuilder; +import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder; public class IssueChangeContextTest { + + private static final Date NOW = new Date(); + private static final String USER_UUID = "user_uuid"; + + private IssueChangeContext context; + + @Test + public void test_issueChangeContextByScanBuilder() { + context = issueChangeContextByScanBuilder(NOW).build(); + + verifyContext(null, true, false, false); + } + + @Test + public void test_issueChangeContextByUserBuilder() { + context = issueChangeContextByUserBuilder(NOW, USER_UUID).build(); + + verifyContext(USER_UUID, false, false, false); + } + @Test - public void test_scan_context() { - Date now = new Date(); - IssueChangeContext context = IssueChangeContext.createScan(now); + public void test_newBuilder() { + context = IssueChangeContext.newBuilder() + .withScan() + .withRefreshMeasures() + .setUserUuid(USER_UUID) + .setDate(NOW) + .withFromAlm() + .build(); - assertThat(context.scan()).isTrue(); - assertThat(context.userUuid()).isNull(); - assertThat(context.date()).isEqualTo(now); + verifyContext(USER_UUID, true, true, true); } @Test - public void test_end_user_context() { - Date now = new Date(); - IssueChangeContext context = IssueChangeContext.createUser(now, "user_uuid"); + public void test_equal() { + context = IssueChangeContext.newBuilder().setUserUuid(USER_UUID).setDate(NOW).build(); + IssueChangeContext equalContext = IssueChangeContext.newBuilder().setUserUuid(USER_UUID).setDate(NOW).build(); + IssueChangeContext notEqualContext = IssueChangeContext.newBuilder().setUserUuid("other_user_uuid").setDate(NOW).build(); - assertThat(context.scan()).isFalse(); - assertThat(context.userUuid()).isEqualTo("user_uuid"); - assertThat(context.date()).isEqualTo(now); + assertThat(context).isEqualTo(context) + .isEqualTo(equalContext) + .isNotEqualTo(notEqualContext) + .isNotEqualTo(null) + .isNotEqualTo(new Object()); } + + @Test + public void test_hashCode() { + context = IssueChangeContext.newBuilder().setUserUuid(USER_UUID).setDate(NOW).build(); + + assertThat(context.hashCode()).isEqualTo(Objects.hash(USER_UUID, NOW, false, false, false)); + } + + @Test + public void test_toString() { + context = IssueChangeContext.newBuilder().setUserUuid(USER_UUID).setDate(NOW).build(); + String expected = "IssueChangeContext{userUuid='user_uuid', date=" + NOW + ", scan=false, refreshMeasures=false, fromAlm=false}"; + + assertThat(context).hasToString(expected); + } + + private void verifyContext(@Nullable String userUuid, boolean scan, boolean refreshMeasures, boolean fromAlm) { + assertThat(context.userUuid()).isEqualTo(userUuid); + assertThat(context.date()).isEqualTo(NOW); + assertThat(context.scan()).isEqualTo(scan); + assertThat(context.refreshMeasures()).isEqualTo(refreshMeasures); + assertThat(context.fromAlm()).isEqualTo(fromAlm); + } + + } |