From 6984a2e2db012d49ebbf7992b6a371f78fd9a3c7 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 15 Nov 2021 14:12:45 -0600 Subject: [PATCH] SONAR-15406 Issue change log is not sort by createdDate --- .../server/issue/IssueChangeWSSupport.java | 14 ++++---- .../issue/IssueChangeWSSupportTest.java | 36 ++++++++++++++++--- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/IssueChangeWSSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/IssueChangeWSSupport.java index c2a6b868d37..d67cb1ec2cc 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/IssueChangeWSSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/IssueChangeWSSupport.java @@ -22,10 +22,10 @@ package org.sonar.server.issue; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; -import com.google.common.collect.Ordering; import com.google.common.collect.Sets; import java.io.Serializable; import java.util.Collection; +import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Map; @@ -65,7 +65,6 @@ import static org.sonar.server.issue.IssueFieldsSetter.TECHNICAL_DEBT; public class IssueChangeWSSupport { private static final String EFFORT_CHANGELOG_KEY = "effort"; - private static final Ordering ISSUE_CHANGE_CREATED_AT_COMPARATOR = Ordering.natural().onResultOf(IssueChangeDto::getCreatedAt); private final DbClient dbClient; private final AvatarResolver avatarFactory; @@ -100,8 +99,7 @@ public class IssueChangeWSSupport { return newFormattingContext(dbSession, dtos, load, ImmutableSet.of(), ImmutableSet.of()); } - public FormattingContext newFormattingContext(DbSession dbSession, Set dtos, Load load, - Set preloadedUsers, Set preloadedComponents) { + public FormattingContext newFormattingContext(DbSession dbSession, Set dtos, Load load, Set preloadedUsers, Set preloadedComponents) { Set issueKeys = dtos.stream().map(IssueDto::getKey).collect(toSet()); List changes = List.of(); @@ -126,23 +124,23 @@ public class IssueChangeWSSupport { throw new IllegalStateException("Unsupported Load value:" + load); } - Map> changesByRuleKey = indexAndSort(changes, IssueChangeDto::toFieldDiffs); - Map> commentsByIssueKey = indexAndSort(comments, t -> t); + Map> changesByRuleKey = indexAndSort(changes, IssueChangeDto::toFieldDiffs, Comparator.comparing(FieldDiffs::creationDate)); + Map> commentsByIssueKey = indexAndSort(comments, t -> t, Comparator.comparing(IssueChangeDto::getIssueChangeCreationDate)); Map usersByUuid = loadUsers(dbSession, changesByRuleKey, commentsByIssueKey, preloadedUsers); Map filesByUuid = loadFiles(dbSession, changesByRuleKey, preloadedComponents); Map updatableCommentByKey = loadUpdatableFlag(commentsByIssueKey); return new FormattingContextImpl(changesByRuleKey, commentsByIssueKey, usersByUuid, filesByUuid, updatableCommentByKey); } - private static Map> indexAndSort(List changes, Function transform) { + private static Map> indexAndSort(List changes, Function transform, Comparator sortingComparator) { Multimap unordered = changes.stream() .collect(MoreCollectors.index(IssueChangeDto::getIssueKey, t -> t)); return unordered.asMap().entrySet().stream() .collect(uniqueIndex( Map.Entry::getKey, t -> t.getValue().stream() - .sorted(ISSUE_CHANGE_CREATED_AT_COMPARATOR) .map(transform) + .sorted(sortingComparator) .collect(toList(t.getValue().size())))); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueChangeWSSupportTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueChangeWSSupportTest.java index 8a0de9727df..5abca19d1d2 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueChangeWSSupportTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueChangeWSSupportTest.java @@ -27,8 +27,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Random; +import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.IntStream; import org.junit.Rule; @@ -36,6 +38,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.sonar.api.utils.System2; import org.sonar.core.issue.FieldDiffs; +import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; @@ -62,6 +65,7 @@ import static org.sonar.db.issue.IssueChangeDto.TYPE_FIELD_CHANGE; @RunWith(DataProviderRunner.class) public class IssueChangeWSSupportTest { + private static final UuidFactoryFast UUID_FACTORY = UuidFactoryFast.getInstance(); private static final Random RANDOM = new Random(); @Rule @@ -69,10 +73,9 @@ public class IssueChangeWSSupportTest { @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); - private DbClient dbClient = dbTester.getDbClient(); - private AvatarResolverImpl avatarResolver = new AvatarResolverImpl(); - - private IssueChangeWSSupport underTest = new IssueChangeWSSupport(dbClient, avatarResolver, userSessionRule); + private final DbClient dbClient = dbTester.getDbClient(); + private final AvatarResolverImpl avatarResolver = new AvatarResolverImpl(); + private final IssueChangeWSSupport underTest = new IssueChangeWSSupport(dbClient, avatarResolver, userSessionRule); @Test public void newFormattingContext_with_Load_CHANGE_LOG_loads_only_changelog() { @@ -96,6 +99,31 @@ public class IssueChangeWSSupportTest { assertThat(formattingContext.getComments(issue)).isEmpty(); } + @Test + public void newFormattingContext_sorts_changes() { + IssueDto issue = dbTester.issues().insertIssue(); + insertFieldChange(issue, c -> c.setCreatedAt(3L)); + insertFieldChange(issue, c -> c.setIssueChangeCreationDate(1L)); + insertFieldChange(issue, c -> c.setCreatedAt(2L)); + + FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), Load.CHANGE_LOG); + + assertThat(formattingContext.getChanges(issue)) + .extracting(FieldDiffs::creationDate) + .containsExactly(new Date(1L), new Date(2L), new Date(3L)); + } + + private void insertFieldChange(IssueDto issue, Consumer consumer ) { + IssueChangeDto change = new IssueChangeDto() + .setUuid(UUID_FACTORY.create()) + .setProjectUuid(UUID_FACTORY.create()) + .setChangeType(TYPE_FIELD_CHANGE) + .setIssueKey(issue.getKey()); + consumer.accept(change); + dbTester.issues().insertChange(change); + dbTester.getSession().commit(); + } + @Test public void newFormattingContext_with_Load_COMMENTS_loads_only_comments() { IssueDto issue = dbTester.issues().insertIssue(); -- 2.39.5