From 981f0bf9bddf9ebcd0c875746cc575b08d9ea814 Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Tue, 11 Apr 2017 15:59:50 +0200 Subject: [PATCH] SONAR-9038 sort results for api/rules/tags and api/issues/tags --- .../sonar/server/rule/index/RuleIndex.java | 31 ++++++------------- .../org/sonar/server/rule/ws/TagsAction.java | 6 ++-- .../server/rule/RuleUpdaterMediumTest.java | 15 +++++---- .../server/rule/index/RuleIndexTest.java | 2 +- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java index d9b2fa048b2..381a6af12c4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -26,12 +26,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Optional; -import java.util.Set; import java.util.function.Function; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; @@ -67,6 +64,7 @@ import org.sonar.server.es.SearchOptions; import org.sonar.server.es.StickyFacetBuilder; import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Optional.ofNullable; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; @@ -120,6 +118,7 @@ public class RuleIndex { Collections2.filter( Collections2.transform(Arrays.asList(RuleStatus.values()), RuleStatus::toString), input -> !RuleStatus.REMOVED.toString().equals(input))); + private static final String AGGREGATION_NAME = "_ref"; private static final String AGGREGATION_NAME_FOR_TAGS = "tagsAggregation"; private final EsClient client; @@ -491,14 +490,12 @@ public class RuleIndex { esSearch.setSize(options.getLimit()); } - public Set terms(String fields) { + public List terms(String fields) { return terms(fields, null, Integer.MAX_VALUE); } - public Set terms(String fields, @Nullable String query, int size) { - String aggregationKey = "_ref"; - - TermsBuilder termsAggregation = AggregationBuilders.terms(aggregationKey) + public List terms(String fields, @Nullable String query, int size) { + TermsBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME) .field(fields) .size(size) .minDocCount(1); @@ -512,19 +509,10 @@ public class RuleIndex { .addAggregation(termsAggregation); SearchResponse esResponse = request.get(); - return extractAggregationTerms(aggregationKey, esResponse); - } - - private static Set extractAggregationTerms(String aggregationKey, SearchResponse esResponse) { - Set terms = new HashSet<>(); - Terms aggregation = esResponse.getAggregations().get(aggregationKey); - if (aggregation != null) { - aggregation.getBuckets().forEach(value -> terms.add(value.getKeyAsString())); - } - return terms; + return EsUtils.termsKeys(esResponse.getAggregations().get(AGGREGATION_NAME)); } - public Set listTags(OrganizationDto organization, @Nullable String query, int size) { + public List listTags(OrganizationDto organization, @Nullable String query, int size) { TermsQueryBuilder scopeFilter = QueryBuilders.termsQuery( FIELD_RULE_EXTENSION_SCOPE, RuleExtensionScope.system().getScope(), @@ -533,8 +521,9 @@ public class RuleIndex { TermsBuilder termsAggregation = AggregationBuilders.terms(AGGREGATION_NAME_FOR_TAGS) .field(FIELD_RULE_EXTENSION_TAGS) .size(size) + .order(Terms.Order.term(true)) .minDocCount(1); - Optional.ofNullable(query) + ofNullable(query) .map(EsUtils::escapeSpecialRegexChars) .map(queryString -> ".*" + queryString + ".*") .ifPresent(termsAggregation::include); @@ -546,7 +535,7 @@ public class RuleIndex { .addAggregation(termsAggregation); SearchResponse esResponse = request.get(); - return extractAggregationTerms(AGGREGATION_NAME_FOR_TAGS, esResponse); + return EsUtils.termsKeys(esResponse.getAggregations().get(AGGREGATION_NAME_FOR_TAGS)); } private static boolean isNotEmpty(@Nullable Collection list) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/TagsAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/TagsAction.java index 9ec9b53e878..77315ccaba4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/TagsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/TagsAction.java @@ -20,8 +20,8 @@ package org.sonar.server.rule.ws; import com.google.common.io.Resources; +import java.util.List; import java.util.Optional; -import java.util.Set; import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -80,7 +80,7 @@ public class TagsAction implements RulesWsAction { String query = request.param(Param.TEXT_QUERY); int pageSize = request.mandatoryParamAsInt("ps"); - Set tags = ruleIndex.listTags(organization, query, pageSize == 0 ? Integer.MAX_VALUE : pageSize); + List tags = ruleIndex.listTags(organization, query, pageSize == 0 ? Integer.MAX_VALUE : pageSize); writeResponse(response, tags); } @@ -95,7 +95,7 @@ public class TagsAction implements RulesWsAction { } } - private static void writeResponse(Response response, Set tags) { + private static void writeResponse(Response response, List tags) { JsonWriter json = response.newJsonWriter().beginObject(); json.name("tags").beginArray(); tags.forEach(json::value); diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java index 5a8f4ce1f0b..6db295282ef 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java @@ -26,12 +26,10 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import java.util.List; import java.util.Map; -import java.util.Set; import javax.annotation.Nonnull; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Test; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; @@ -195,7 +193,6 @@ public class RuleUpdaterMediumTest { assertThat(rule.getNoteUpdatedAt()).isNull(); } - @Ignore @Test public void set_tags() { // insert db @@ -205,7 +202,9 @@ public class RuleUpdaterMediumTest { dbSession.commit(); // java8 is a system tag -> ignore - RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(Sets.newHashSet("bug", "java8")); + RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY) + .setTags(Sets.newHashSet("bug", "java8")) + .setOrganization(defaultOrganization); underTest.update(dbSession, update, defaultOrganization, userSessionRule); RuleDto rule = ruleDao.selectOrFailByKey(dbSession, defaultOrganization, RULE_KEY); @@ -213,8 +212,8 @@ public class RuleUpdaterMediumTest { assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc"); // verify that tags are indexed in index - Set tags = ruleIndex.listTags(defaultOrganization, null, 10); - assertThat(tags).containsOnly("bug", "java8", "javadoc"); + List tags = ruleIndex.listTags(defaultOrganization, null, 10); + assertThat(tags).containsExactly("bug", "java8", "javadoc"); } @Test @@ -237,8 +236,8 @@ public class RuleUpdaterMediumTest { assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc"); // verify that tags are indexed in index - Set tags = ruleIndex.listTags(defaultOrganization, null, 10); - assertThat(tags).containsOnly("java8", "javadoc"); + List tags = ruleIndex.listTags(defaultOrganization, null, 10); + assertThat(tags).containsExactly("java8", "javadoc"); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java index 891c4f2cd11..6713881844a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java @@ -793,7 +793,7 @@ public class RuleIndexTest { } @Test - public void tags_facet_should_be_available_if_organization_is_speficied() { + public void tags_facet_should_be_available_if_organization_is_specified() { RuleQuery query = new RuleQuery() .setOrganizationUuid("some_org_id"); SearchOptions options = new SearchOptions().addFacets(singletonList(FACET_TAGS)); -- 2.39.5