]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9038 sort results for api/rules/tags and api/issues/tags
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Tue, 11 Apr 2017 13:59:50 +0000 (15:59 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Fri, 14 Apr 2017 06:57:18 +0000 (08:57 +0200)
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/TagsAction.java
server/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java

index d9b2fa048b252724fbc45b898e24bdf645c45e55..381a6af12c4fb3e82cdb6e7325ba744fd6220d5a 100644 (file)
@@ -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<String> terms(String fields) {
+  public List<String> terms(String fields) {
     return terms(fields, null, Integer.MAX_VALUE);
   }
 
-  public Set<String> terms(String fields, @Nullable String query, int size) {
-    String aggregationKey = "_ref";
-
-    TermsBuilder termsAggregation = AggregationBuilders.terms(aggregationKey)
+  public List<String> 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<String> extractAggregationTerms(String aggregationKey, SearchResponse esResponse) {
-    Set<String> 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<String> listTags(OrganizationDto organization, @Nullable String query, int size) {
+  public List<String> 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) {
index 9ec9b53e8788ef56105b4160b8f479bec1a3c908..77315ccaba4b0733770f4b52198739b1a68df8d5 100644 (file)
@@ -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<String> tags = ruleIndex.listTags(organization, query, pageSize == 0 ? Integer.MAX_VALUE : pageSize);
+    List<String> 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<String> tags) {
+  private static void writeResponse(Response response, List<String> tags) {
     JsonWriter json = response.newJsonWriter().beginObject();
     json.name("tags").beginArray();
     tags.forEach(json::value);
index 5a8f4ce1f0bc9220239462f7a10642400d507c13..6db295282ef2125edb296fc6a3c52b39612c1bef 100644 (file)
@@ -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<String> tags = ruleIndex.listTags(defaultOrganization, null, 10);
-    assertThat(tags).containsOnly("bug", "java8", "javadoc");
+    List<String> 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<String> tags = ruleIndex.listTags(defaultOrganization, null, 10);
-    assertThat(tags).containsOnly("java8", "javadoc");
+    List<String> tags = ruleIndex.listTags(defaultOrganization, null, 10);
+    assertThat(tags).containsExactly("java8", "javadoc");
   }
 
   @Test
index 891c4f2cd11937087750e1c7b459859efe8b7144..6713881844ae3e0d97c96f1688589709bfa350ae 100644 (file)
@@ -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));