]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15406 Issue change log is not sort by createdDate
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 15 Nov 2021 20:12:45 +0000 (14:12 -0600)
committersonartech <sonartech@sonarsource.com>
Tue, 16 Nov 2021 20:03:55 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/IssueChangeWSSupport.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueChangeWSSupportTest.java

index c2a6b868d374b0a8072f1a4082839fe6be71c1a4..d67cb1ec2cc7a03787c648d8635dc5938ae11054 100644 (file)
@@ -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<IssueChangeDto> 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<IssueDto> dtos, Load load,
-    Set<UserDto> preloadedUsers, Set<ComponentDto> preloadedComponents) {
+  public FormattingContext newFormattingContext(DbSession dbSession, Set<IssueDto> dtos, Load load, Set<UserDto> preloadedUsers, Set<ComponentDto> preloadedComponents) {
     Set<String> issueKeys = dtos.stream().map(IssueDto::getKey).collect(toSet());
 
     List<IssueChangeDto> changes = List.of();
@@ -126,23 +124,23 @@ public class IssueChangeWSSupport {
         throw new IllegalStateException("Unsupported Load value:" + load);
     }
 
-    Map<String, List<FieldDiffs>> changesByRuleKey = indexAndSort(changes, IssueChangeDto::toFieldDiffs);
-    Map<String, List<IssueChangeDto>> commentsByIssueKey = indexAndSort(comments, t -> t);
+    Map<String, List<FieldDiffs>> changesByRuleKey = indexAndSort(changes, IssueChangeDto::toFieldDiffs, Comparator.comparing(FieldDiffs::creationDate));
+    Map<String, List<IssueChangeDto>> commentsByIssueKey = indexAndSort(comments, t -> t, Comparator.comparing(IssueChangeDto::getIssueChangeCreationDate));
     Map<String, UserDto> usersByUuid = loadUsers(dbSession, changesByRuleKey, commentsByIssueKey, preloadedUsers);
     Map<String, ComponentDto> filesByUuid = loadFiles(dbSession, changesByRuleKey, preloadedComponents);
     Map<String, Boolean> updatableCommentByKey = loadUpdatableFlag(commentsByIssueKey);
     return new FormattingContextImpl(changesByRuleKey, commentsByIssueKey, usersByUuid, filesByUuid, updatableCommentByKey);
   }
 
-  private static <T> Map<String, List<T>> indexAndSort(List<IssueChangeDto> changes, Function<IssueChangeDto, T> transform) {
+  private static <T> Map<String, List<T>> indexAndSort(List<IssueChangeDto> changes, Function<IssueChangeDto, T> transform, Comparator<T> sortingComparator) {
     Multimap<String, IssueChangeDto> 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()))));
   }
 
index 8a0de9727df1aaf73e7c1fc53edbac9e9c385863..5abca19d1d2ca18360e35d2c2a7c6ee37015f184 100644 (file)
@@ -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<IssueChangeDto> 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();