diff options
author | Eric Giffon <eric.giffon@sonarsource.com> | 2024-03-11 10:10:15 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-03-11 20:02:51 +0000 |
commit | 1b90ac2d10894f67227892a3642acbf70d9ba096 (patch) | |
tree | b1a69d41bcdff8f36895a5c0c8ce89261d009a68 | |
parent | d1b63f42044302bfe3b9a9a4a72fdef16243bfb1 (diff) | |
download | sonarqube-1b90ac2d10894f67227892a3642acbf70d9ba096.tar.gz sonarqube-1b90ac2d10894f67227892a3642acbf70d9ba096.zip |
SONAR-21776 Fix api/rules/list sorting
-rw-r--r-- | server/sonar-webserver-webapi/src/it/java/org/sonar/server/rule/ws/ListActionIT.java | 25 | ||||
-rw-r--r-- | server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ListAction.java | 9 |
2 files changed, 28 insertions, 6 deletions
diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/rule/ws/ListActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/rule/ws/ListActionIT.java index d636510aded..9d4ca401190 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/rule/ws/ListActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/rule/ws/ListActionIT.java @@ -21,6 +21,8 @@ package org.sonar.server.rule.ws; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Comparator; +import java.util.List; import java.util.Map; import java.util.Optional; import org.junit.Rule; @@ -73,8 +75,9 @@ public class ListActionIT { @Test public void execute_shouldReturnRules() { - db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_1)).setConfigKey(null).setName(null)); - db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_2)).setConfigKey("I002").setName("Rule Two")); + List<RuleDto> rules = List.of( + db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_1)).setConfigKey(null).setName(null)), + db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_2)).setConfigKey("I002").setName("Rule Two"))); db.getSession().commit(); Rules.ListResponse listResponse = ws.newRequest() @@ -91,6 +94,24 @@ public class ListActionIT { assertThat(ruleS002.getKey()).isEqualTo(RULE_KEY_2); assertThat(ruleS002.getInternalKey()).isEqualTo("I002"); assertThat(ruleS002.getName()).isEqualTo("Rule Two"); + + assertThat(listResponse.getRulesList()).extracting(Rules.Rule::getKey).containsExactly( + rules.stream().sorted(Comparator.comparing(RuleDto::getUuid)).map(rule -> rule.getKey().toString()).toArray(String[]::new)); + } + + @Test + public void execute_whenSortingDefined_shouldReturnSortedRules() { + db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_1)).setCreatedAt(2_000_000L)); + db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_2)).setCreatedAt(1_000_000L)); + db.getSession().commit(); + + Rules.ListResponse listResponse = ws.newRequest() + .setParam(WebService.Param.SORT, "createdAt") + .setParam(WebService.Param.ASCENDING, "true") + .executeProtobuf(Rules.ListResponse.class); + + assertThat(listResponse.getRulesCount()).isEqualTo(2); + assertThat(listResponse.getRulesList()).extracting(Rules.Rule::getKey).containsExactly(RULE_KEY_2, RULE_KEY_1); } @Test diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ListAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ListAction.java index 74328f9f81f..fd0ee759500 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ListAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ListAction.java @@ -21,7 +21,6 @@ package org.sonar.server.rule.ws; import com.google.common.collect.Maps; import java.util.Date; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -81,7 +80,9 @@ public class ListAction implements RulesWsAction { new Change("10.4", "Add pagination"), new Change("10.4", String.format("Add the '%s' parameter", PARAM_AVAILABLE_SINCE)), new Change("10.4", String.format("Add the '%s' parameter", PARAM_QPROFILE)), - new Change("10.4", "Add the 'createdAt' sorting parameter")); + new Change("10.4", "Add the 'createdAt' sorting field"), + new Change("10.5", String.format("The sorting parameter '%s' no longer has a default value (was 'createdAt')", + WebService.Param.SORT))); action.createParam(PARAM_AVAILABLE_SINCE) .setDescription("Filter rules available since the given date. If no value is provided, all rules are returned. Format is yyyy-MM-dd.") @@ -90,7 +91,7 @@ public class ListAction implements RulesWsAction { action.createParam(PARAM_QPROFILE) .setDescription("Filter rules that are activated in the given quality profile.") .setSince("10.4"); - action.createSortParams(Set.of("createdAt"), "createdAt", false) + action.createSortParams(Set.of("createdAt"), null, false) .setSince("10.4"); action.addPagingParamsSince(100, 500, "10.4"); } @@ -139,7 +140,7 @@ public class ListAction implements RulesWsAction { Pagination.forPage(wsRequest.page).andSize(wsRequest.pageSize)); Map<String, RuleDto> rulesByUuid = Maps.uniqueIndex(dbClient.ruleDao().selectByUuids(dbSession, ruleListResult.getUuids()), RuleDto::getUuid); Set<String> ruleUuids = rulesByUuid.keySet(); - List<RuleDto> rules = new LinkedList<>(rulesByUuid.values()); + List<RuleDto> rules = ruleListResult.getUuids().stream().map(rulesByUuid::get).toList(); List<String> templateRuleUuids = rules.stream() .map(RuleDto::getTemplateUuid) |