]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8798 fix unit tests
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Wed, 19 Jul 2017 13:44:57 +0000 (15:44 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Wed, 9 Aug 2017 13:09:54 +0000 (15:09 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java
server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java
server/sonar-server/src/test/java/org/sonar/server/es/NewIndexTest.java
server/sonar-server/src/test/java/org/sonar/server/es/request/ProxySearchRequestBuilderTest.java
server/sonar-server/src/test/java/org/sonar/server/es/textsearch/ComponentTextSearchQueryFactoryTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/index/AuthorizationTypeSupportTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java

index 263fd4782448810bc87bbb0bbb1ff2d0907c125e..9078c0b2d96f7634038529e2403be75a36bbae8f 100644 (file)
@@ -32,6 +32,8 @@ import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.SearchHits;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
 import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator;
+import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter;
 import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters;
 import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters.InternalBucket;
 import org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits;
@@ -107,7 +109,7 @@ public class ComponentIndex {
   private static FiltersAggregationBuilder createAggregation(ComponentIndexQuery query) {
     return AggregationBuilders.filters(
       FILTERS_AGGREGATION_NAME,
-      query.getQualifiers().stream().map(q -> termQuery(FIELD_QUALIFIER, q)).toArray(QueryBuilder[]::new))
+      query.getQualifiers().stream().map(q -> new KeyedFilter(q, termQuery(FIELD_QUALIFIER, q))).toArray(KeyedFilter[]::new))
       .subAggregation(createSubAggregation(query));
   }
 
index 047c8daea5572b9175beae370d3433da956220c3..960ab8b3014a835c4554bb05c7508512d5e62ddc 100644 (file)
@@ -37,6 +37,7 @@ import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
 import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
+import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator;
 import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
@@ -261,13 +262,13 @@ public class ProjectMeasuresIndex {
   private static AbstractAggregationBuilder createRatingFacet(String metricKey) {
     return AggregationBuilders.nested("nested_" + metricKey, FIELD_MEASURES)
       .subAggregation(
-        AggregationBuilders.filter("filter_" + metricKey, termsQuery(FIELD_MEASURES_KEY, metricKey))
-          .subAggregation(filters(metricKey,
-            termQuery(FIELD_MEASURES_VALUE, 1d),
-            termQuery(FIELD_MEASURES_VALUE, 2d),
-            termQuery(FIELD_MEASURES_VALUE, 3d),
-            termQuery(FIELD_MEASURES_VALUE, 4d),
-            termQuery(FIELD_MEASURES_VALUE, 5d))));
+              AggregationBuilders.filter("filter_" + metricKey, termsQuery(FIELD_MEASURES_KEY, metricKey))
+                      .subAggregation(filters(metricKey,
+                              new FiltersAggregator.KeyedFilter("1", termQuery(FIELD_MEASURES_VALUE, 1d)),
+                              new FiltersAggregator.KeyedFilter("2", termQuery(FIELD_MEASURES_VALUE, 2d)),
+                              new FiltersAggregator.KeyedFilter("3", termQuery(FIELD_MEASURES_VALUE, 3d)),
+                              new FiltersAggregator.KeyedFilter("4", termQuery(FIELD_MEASURES_VALUE, 4d)),
+                              new FiltersAggregator.KeyedFilter("5", termQuery(FIELD_MEASURES_VALUE, 5d)))));
   }
 
   private static AbstractAggregationBuilder createQualityGateFacet() {
index 3842a5b62750d0af935103296a9d712fd43de33c..e1669812974795da0ac60543af45324f195abc0e 100644 (file)
@@ -461,7 +461,7 @@ public class RuleIndex {
   }
 
   private static StickyFacetBuilder stickyFacetBuilder(QueryBuilder query, Map<String, QueryBuilder> filters) {
-    return new StickyFacetBuilder(query, filters);
+    return new StickyFacetBuilder(query, filters, null, Terms.Order.compound(Terms.Order.count(false), Terms.Order.term(true)));
   }
 
   private static void setSorting(RuleQuery query, SearchRequestBuilder esSearch) {
index 86874bb8fcc5f29bb0dc62dc442baee8df0960a7..0af3a20efc86f95e20e012ea441798cef9a834b6 100644 (file)
@@ -39,10 +39,12 @@ import org.sonar.server.es.ProjectIndexer;
 import static java.util.Collections.emptySet;
 import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
 import static org.elasticsearch.index.query.QueryBuilders.termQuery;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME;
 import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_TYPE_COMPONENT;
+import static org.sonar.server.es.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
 import static org.sonar.server.es.ProjectIndexer.Cause.PROJECT_CREATION;
 import static org.sonar.server.es.ProjectIndexer.Cause.PROJECT_DELETION;
 
@@ -250,7 +252,7 @@ public class ComponentIndexerTest {
   private void assertThatComponentHasName(ComponentDto component, String expectedName) {
     SearchHit[] hits = es.client()
       .prepareSearch(INDEX_TYPE_COMPONENT)
-      .setQuery(termQuery(FIELD_NAME, expectedName))
+      .setQuery(matchQuery(SORTABLE_ANALYZER.subField(FIELD_NAME), expectedName))
       .get()
       .getHits()
       .getHits();
index 68a8950f7efc95c7a8b02ceec405d83daacbc4d0..16dba3566b97a0367944e0d362105beaad9aff62 100644 (file)
@@ -112,7 +112,7 @@ public class NewIndexTest {
     assertThat(props.get("fields")).isNull();
 
     props = (Map) mapping.getProperty("all_capabilities_field");
-    assertThat(props.get("type")).isEqualTo("multi_field");
+    assertThat(props.get("type")).isEqualTo("keyword");
     // no need to test values, it's not the scope of this test
     assertThat((Map) props.get("fields")).isNotEmpty();
   }
index 95cf67681763309019c14b06c0bc9aadcbbea55a..b44d5df59aea1d2bf17a8e2773199f8687ab0851 100644 (file)
@@ -67,7 +67,7 @@ public class ProxySearchRequestBuilderTest {
       fail();
     } catch (Exception e) {
       assertThat(e).isInstanceOf(IllegalStateException.class);
-      assertThat(e.getMessage()).contains("Fail to execute ES search request '{").contains("}' on indices '[fakes]'");
+      assertThat(e.getMessage()).contains("Fail to execute ES search request 'SearchRequest{").contains("}' on indices '[non-existing-index]'");
     }
   }
 
index 546d0060a06d4a30a2628b8d46331edc33ac464d..e0cbc109fcfa1f5263db9f750768e47fe9497988 100644 (file)
@@ -41,19 +41,18 @@ public class ComponentTextSearchQueryFactoryTest {
 
     assertJson(result.toString()).isSimilarTo("{" +
       "  \"bool\" : {" +
-      "    \"must\" : {" +
+      "    \"must\" : [{" +
       "      \"bool\" : {" +
-      "        \"should\" : {" +
+      "        \"should\" : [{" +
       "          \"match\" : {" +
       "            \"key.sortable_analyzer\" : {" +
       "              \"query\" : \"SonarQube\"," +
-      "              \"type\" : \"boolean\"," +
       "              \"boost\" : 50.0\n" +
       "            }" +
       "          }" +
-      "        }" +
+      "        }]" +
       "      }" +
-      "    }" +
+      "    }]" +
       "  }" +
       "}");
   }
index 6b1423222c2103c247742eaba006733e8b19214a..f0efd0ff5b8cf92693e2f897b639be44d9f2a108 100644 (file)
@@ -57,15 +57,15 @@ public class AuthorizationTypeSupportTest {
       "  \"has_parent\" : {" +
       "    \"query\" : {" +
       "      \"bool\" : {" +
-      "        \"filter\" : {" +
+      "        \"filter\" : [{" +
       "          \"bool\" : {" +
-      "            \"should\" : {" +
+      "            \"should\" : [{" +
       "              \"term\" : {" +
-      "                \"allowAnyone\" : true" +
+      "                \"allowAnyone\" : {\"value\": true}" +
       "              }" +
-      "            }" +
+      "            }]" +
       "          }" +
-      "        }" +
+      "        }]" +
       "      }" +
       "    }," +
       "    \"parent_type\" : \"authorization\"" +
@@ -83,22 +83,22 @@ public class AuthorizationTypeSupportTest {
       "  \"has_parent\": {" +
       "    \"query\": {" +
       "      \"bool\": {" +
-      "        \"filter\": {" +
+      "        \"filter\": [{" +
       "          \"bool\": {" +
       "            \"should\": [" +
       "              {" +
       "                \"term\": {" +
-      "                  \"allowAnyone\": true" +
+      "                  \"allowAnyone\": {\"value\": true}" +
       "                }" +
       "              }," +
       "              {" +
       "                \"term\": {" +
-      "                  \"userIds\": 1234" +
+      "                  \"userIds\": {\"value\": 1234}" +
       "                }" +
       "              }" +
       "            ]" +
       "          }" +
-      "        }" +
+      "        }]" +
       "      }" +
       "    }," +
       "    \"parent_type\": \"authorization\"" +
@@ -118,32 +118,32 @@ public class AuthorizationTypeSupportTest {
       "  \"has_parent\": {" +
       "    \"query\": {" +
       "      \"bool\": {" +
-      "        \"filter\": {" +
+      "        \"filter\": [{" +
       "          \"bool\": {" +
       "            \"should\": [" +
       "              {" +
       "                \"term\": {" +
-      "                  \"allowAnyone\": true" +
+      "                  \"allowAnyone\": {\"value\": true}" +
       "                }" +
       "              }," +
       "              {" +
       "                \"term\": {" +
-      "                  \"userIds\": 1234" +
+      "                  \"userIds\": {\"value\": 1234}" +
       "                }" +
       "              }," +
       "              {" +
       "                \"term\": {" +
-      "                  \"groupIds\": 10" +
+      "                  \"groupIds\": {\"value\": 10}" +
       "                }" +
       "              }," +
       "              {" +
       "                \"term\": {" +
-      "                  \"groupIds\": 11" +
+      "                  \"groupIds\": {\"value\": 11}" +
       "                }" +
       "              }" +
       "            ]" +
       "          }" +
-      "        }" +
+      "        }]" +
       "      }" +
       "    }," +
       "    \"parent_type\": \"authorization\"" +
index ba57d07a22808042fb0d1c260ed54d8bbcbc2011..2fe7748be0bfd5bfec806e709705703ed0431e86 100644 (file)
@@ -267,9 +267,26 @@ public class SearchActionTest {
       .setParam("facets", "tags")
       .setParam("organization", organization.getKey())
       .executeProtobuf(SearchResponse.class);
-    assertThat(result.getFacets().getFacets(0).getValuesList()).extracting(v -> entry(v.getVal(), v.getCount()))
-      .containsExactly(entry("tag1", 1L), entry("tag2", 1L), entry("tag3", 1L), entry("tag4", 1L), entry("tag5", 1L), entry("tag6", 1L), entry("tag7", 1L), entry("tag8", 1L),
-        entry("tag9", 1L), entry("tagA", 1L));
+    assertThat(result.getFacets().getFacets(0).getValuesList()).extracting(v -> v.getVal(), v -> v.getCount())
+      .containsExactly(tuple("tag1", 1L), tuple("tag2", 1L), tuple("tag3", 1L), tuple("tag4", 1L), tuple("tag5", 1L), tuple("tag6", 1L), tuple("tag7", 1L), tuple("tag8", 1L),
+              tuple("tag9", 1L), tuple("tagA", 1L));
+  }
+
+  @Test
+  public void should_list_tags_ordered_by_count_then_by_name_in_tags_facet() throws IOException {
+    OrganizationDto organization = db.organizations().insert();
+    RuleDefinitionDto rule = db.rules().insert(setSystemTags("tag7", "tag5", "tag3", "tag1", "tag9", "x"));
+    RuleMetadataDto metadata = insertMetadata(organization, rule, setTags("tag2", "tag4", "tag6", "tag8", "tagA"));
+    RuleDefinitionDto rule2 = db.rules().insert(setSystemTags("tag2"));
+    indexRules();
+
+    SearchResponse result = ws.newRequest()
+      .setParam("facets", "tags")
+      .setParam("organization", organization.getKey())
+      .executeProtobuf(SearchResponse.class);
+    assertThat(result.getFacets().getFacets(0).getValuesList()).extracting(v -> v.getVal(), v -> v.getCount())
+      .containsExactly(tuple("tag2", 2L), tuple("tag1", 1L), tuple("tag3", 1L), tuple("tag4", 1L), tuple("tag5", 1L), tuple("tag6", 1L), tuple("tag7", 1L), tuple("tag8", 1L),
+              tuple("tag9", 1L), tuple("tagA", 1L));
   }
 
   @Test