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;
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;
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;
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);
.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(),
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);
.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) {
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;
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);
}
}
}
- 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);
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;
assertThat(rule.getNoteUpdatedAt()).isNull();
}
- @Ignore
@Test
public void set_tags() {
// insert db
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);
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
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