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;
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;
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();
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()))));
}
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;
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;
@RunWith(DataProviderRunner.class)
public class IssueChangeWSSupportTest {
+ private static final UuidFactoryFast UUID_FACTORY = UuidFactoryFast.getInstance();
private static final Random RANDOM = new Random();
@Rule
@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() {
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();