]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9928 ComponentIndexQuery renamed to SuggestionQuery
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 11 Oct 2017 12:46:26 +0000 (14:46 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Thu, 12 Oct 2017 12:50:59 +0000 (14:50 +0200)
13 files changed:
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexQuery.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/component/index/SuggestionQuery.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexQueryTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java
server/sonar-server/src/test/java/org/sonar/server/component/index/SuggestionQueryTest.java [new file with mode: 0644]

index 41b07f569e6b710062472db0cedfb68223b0d490..0946a08abd7780b86ef437eef90dd561bc44be7f 100644 (file)
@@ -68,25 +68,12 @@ public class ComponentIndex {
     this.authorizationTypeSupport = authorizationTypeSupport;
   }
 
-  private static HighlightBuilder.Field createHighlighterField() {
-    HighlightBuilder.Field field = new HighlightBuilder.Field(FIELD_NAME);
-    field.highlighterType("fvh");
-    field.matchedFields(
-      Stream.concat(
-        Stream.of(FIELD_NAME),
-        Arrays
-          .stream(NAME_ANALYZERS)
-          .map(a -> a.subField(FIELD_NAME)))
-        .toArray(String[]::new));
-    return field;
-  }
-
-  public ComponentIndexResults search(ComponentIndexQuery query) {
-    return search(query, ComponentTextSearchFeatureRepertoire.values());
+  public ComponentIndexResults searchSuggestions(SuggestionQuery query) {
+    return searchSuggestions(query, ComponentTextSearchFeatureRepertoire.values());
   }
 
   @VisibleForTesting
-  ComponentIndexResults search(ComponentIndexQuery query, ComponentTextSearchFeature... features) {
+  ComponentIndexResults searchSuggestions(SuggestionQuery query, ComponentTextSearchFeature... features) {
     Collection<String> qualifiers = query.getQualifiers();
     if (qualifiers.isEmpty()) {
       return ComponentIndexResults.newBuilder().build();
@@ -105,14 +92,27 @@ public class ComponentIndex {
     return aggregationsToQualifiers(response);
   }
 
-  private static FiltersAggregationBuilder createAggregation(ComponentIndexQuery query) {
+  private static HighlightBuilder.Field createHighlighterField() {
+    HighlightBuilder.Field field = new HighlightBuilder.Field(FIELD_NAME);
+    field.highlighterType("fvh");
+    field.matchedFields(
+      Stream.concat(
+        Stream.of(FIELD_NAME),
+        Arrays
+          .stream(NAME_ANALYZERS)
+          .map(a -> a.subField(FIELD_NAME)))
+        .toArray(String[]::new));
+    return field;
+  }
+
+  private static FiltersAggregationBuilder createAggregation(SuggestionQuery query) {
     return AggregationBuilders.filters(
       FILTERS_AGGREGATION_NAME,
       query.getQualifiers().stream().map(q -> new KeyedFilter(q, termQuery(FIELD_QUALIFIER, q))).toArray(KeyedFilter[]::new))
       .subAggregation(createSubAggregation(query));
   }
 
-  private static TopHitsAggregationBuilder createSubAggregation(ComponentIndexQuery query) {
+  private static TopHitsAggregationBuilder createSubAggregation(SuggestionQuery query) {
     return AggregationBuilders.topHits(DOCS_AGGREGATION_NAME)
       .highlighter(new HighlightBuilder()
         .encoder("html")
@@ -127,7 +127,7 @@ public class ComponentIndex {
       .fetchSource(false);
   }
 
-  private QueryBuilder createQuery(ComponentIndexQuery query, ComponentTextSearchFeature... features) {
+  private QueryBuilder createQuery(SuggestionQuery query, ComponentTextSearchFeature... features) {
     BoolQueryBuilder esQuery = boolQuery();
     esQuery.filter(authorizationTypeSupport.createQueryFilter());
     ComponentTextSearchQuery componentTextSearchQuery = ComponentTextSearchQuery.builder()
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexQuery.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexQuery.java
deleted file mode 100644 (file)
index e314afc..0000000
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.component.index;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Set;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Objects.requireNonNull;
-
-public class ComponentIndexQuery {
-
-  public static final int DEFAULT_LIMIT = 6;
-
-  private final String query;
-  private final Collection<String> qualifiers;
-  private final Set<String> recentlyBrowsedKeys;
-  private final Set<String> favoriteKeys;
-  private final int skip;
-  private final int limit;
-
-  private ComponentIndexQuery(Builder builder) {
-    this.query = requireNonNull(builder.query);
-    this.qualifiers = requireNonNull(builder.qualifiers);
-    this.recentlyBrowsedKeys = requireNonNull(builder.recentlyBrowsedKeys);
-    this.favoriteKeys = requireNonNull(builder.favoriteKeys);
-    this.skip = builder.skip;
-    this.limit = builder.limit;
-  }
-
-  public Collection<String> getQualifiers() {
-    return qualifiers;
-  }
-
-  public String getQuery() {
-    return query;
-  }
-
-  public Set<String> getRecentlyBrowsedKeys() {
-    return recentlyBrowsedKeys;
-  }
-
-  public int getSkip() {
-    return skip;
-  }
-
-  public int getLimit() {
-    return limit;
-  }
-
-  public static Builder builder() {
-    return new Builder();
-  }
-
-  public Set<String> getFavoriteKeys() {
-    return favoriteKeys;
-  }
-
-  public static class Builder {
-    private String query;
-    private Collection<String> qualifiers = Collections.emptyList();
-    private Set<String> recentlyBrowsedKeys = Collections.emptySet();
-    private Set<String> favoriteKeys = Collections.emptySet();
-    private int skip = 0;
-    private int limit = DEFAULT_LIMIT;
-
-    private Builder() {
-    }
-
-    public Builder setQuery(String query) {
-      checkArgument(query.length() >= 2, "Query must be at least two characters long: %s", query);
-      this.query = query;
-      return this;
-    }
-
-    public Builder setQualifiers(Collection<String> qualifiers) {
-      this.qualifiers = Collections.unmodifiableCollection(qualifiers);
-      return this;
-    }
-
-    public Builder setRecentlyBrowsedKeys(Set<String> recentlyBrowsedKeys) {
-      this.recentlyBrowsedKeys = Collections.unmodifiableSet(recentlyBrowsedKeys);
-      return this;
-    }
-
-    public Builder setFavoriteKeys(Set<String> favoriteKeys) {
-      this.favoriteKeys = Collections.unmodifiableSet(favoriteKeys);
-      return this;
-    }
-
-    public Builder setSkip(int skip) {
-      checkArgument(limit > 0, "Skip has to be strictly positive: %s", limit);
-      this.skip = skip;
-      return this;
-    }
-
-    public Builder setLimit(int limit) {
-      checkArgument(limit > 0, "Limit has to be strictly positive: %s", limit);
-      this.limit = limit;
-      return this;
-    }
-
-    public ComponentIndexQuery build() {
-      return new ComponentIndexQuery(this);
-    }
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/SuggestionQuery.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/SuggestionQuery.java
new file mode 100644 (file)
index 0000000..8e15fcf
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.component.index;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+public class SuggestionQuery {
+
+  public static final int DEFAULT_LIMIT = 6;
+
+  private final String query;
+  private final Collection<String> qualifiers;
+  private final Set<String> recentlyBrowsedKeys;
+  private final Set<String> favoriteKeys;
+  private final int skip;
+  private final int limit;
+
+  private SuggestionQuery(Builder builder) {
+    this.query = requireNonNull(builder.query);
+    this.qualifiers = requireNonNull(builder.qualifiers);
+    this.recentlyBrowsedKeys = requireNonNull(builder.recentlyBrowsedKeys);
+    this.favoriteKeys = requireNonNull(builder.favoriteKeys);
+    this.skip = builder.skip;
+    this.limit = builder.limit;
+  }
+
+  public Collection<String> getQualifiers() {
+    return qualifiers;
+  }
+
+  public String getQuery() {
+    return query;
+  }
+
+  public Set<String> getRecentlyBrowsedKeys() {
+    return recentlyBrowsedKeys;
+  }
+
+  public int getSkip() {
+    return skip;
+  }
+
+  public int getLimit() {
+    return limit;
+  }
+
+  public static Builder builder() {
+    return new Builder();
+  }
+
+  public Set<String> getFavoriteKeys() {
+    return favoriteKeys;
+  }
+
+  public static class Builder {
+    private String query;
+    private Collection<String> qualifiers = Collections.emptyList();
+    private Set<String> recentlyBrowsedKeys = Collections.emptySet();
+    private Set<String> favoriteKeys = Collections.emptySet();
+    private int skip = 0;
+    private int limit = DEFAULT_LIMIT;
+
+    private Builder() {
+    }
+
+    public Builder setQuery(String query) {
+      checkArgument(query.length() >= 2, "Query must be at least two characters long: %s", query);
+      this.query = query;
+      return this;
+    }
+
+    public Builder setQualifiers(Collection<String> qualifiers) {
+      this.qualifiers = Collections.unmodifiableCollection(qualifiers);
+      return this;
+    }
+
+    public Builder setRecentlyBrowsedKeys(Set<String> recentlyBrowsedKeys) {
+      this.recentlyBrowsedKeys = Collections.unmodifiableSet(recentlyBrowsedKeys);
+      return this;
+    }
+
+    public Builder setFavoriteKeys(Set<String> favoriteKeys) {
+      this.favoriteKeys = Collections.unmodifiableSet(favoriteKeys);
+      return this;
+    }
+
+    public Builder setSkip(int skip) {
+      checkArgument(limit > 0, "Skip has to be strictly positive: %s", limit);
+      this.skip = skip;
+      return this;
+    }
+
+    public Builder setLimit(int limit) {
+      checkArgument(limit > 0, "Limit has to be strictly positive: %s", limit);
+      this.limit = limit;
+      return this;
+    }
+
+    public SuggestionQuery build() {
+      return new SuggestionQuery(this);
+    }
+  }
+}
index dbc6953d94dfb9aec7de41994cb3efc8bf68de54..5d728d6ab7d9b532b849dc5031e41478ebdba87d 100644 (file)
@@ -51,7 +51,7 @@ import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.component.index.ComponentHit;
 import org.sonar.server.component.index.ComponentHitsPerQualifier;
 import org.sonar.server.component.index.ComponentIndex;
-import org.sonar.server.component.index.ComponentIndexQuery;
+import org.sonar.server.component.index.SuggestionQuery;
 import org.sonar.server.component.index.ComponentIndexResults;
 import org.sonar.server.es.DefaultIndexSettings;
 import org.sonar.server.favorite.FavoriteFinder;
@@ -69,7 +69,7 @@ import static java.util.Collections.singletonList;
 import static org.sonar.api.web.UserRole.USER;
 import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.core.util.stream.MoreCollectors.toSet;
-import static org.sonar.server.component.index.ComponentIndexQuery.DEFAULT_LIMIT;
+import static org.sonar.server.component.index.SuggestionQuery.DEFAULT_LIMIT;
 import static org.sonar.server.es.DefaultIndexSettings.MINIMUM_NGRAM_LENGTH;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 import static org.sonarqube.ws.WsComponents.SuggestionsWsResponse.Organization;
@@ -218,7 +218,7 @@ public class SuggestionsAction implements ComponentsWsAction {
 
     List<ComponentDto> favorites = favoriteFinder.list();
     Set<String> favoriteKeys = favorites.stream().map(ComponentDto::getDbKey).collect(MoreCollectors.toSet(favorites.size()));
-    ComponentIndexQuery.Builder queryBuilder = ComponentIndexQuery.builder()
+    SuggestionQuery.Builder queryBuilder = SuggestionQuery.builder()
       .setQuery(query)
       .setRecentlyBrowsedKeys(recentlyBrowsedKeys)
       .setFavoriteKeys(favoriteKeys)
@@ -296,8 +296,8 @@ public class SuggestionsAction implements ComponentsWsAction {
       .collect(MoreCollectors.uniqueIndex(OrganizationDto::getUuid));
   }
 
-  private ComponentIndexResults searchInIndex(ComponentIndexQuery componentIndexQuery) {
-    return index.search(componentIndexQuery);
+  private ComponentIndexResults searchInIndex(SuggestionQuery suggestionQuery) {
+    return index.searchSuggestions(suggestionQuery);
   }
 
   private static SuggestionsWsResponse.Builder toResponse(ComponentIndexResults componentsPerQualifiers, Set<String> recentlyBrowsedKeys, Set<String> favoriteUuids,
index 56b82d6730d01349edff712af151bc06f2b843bf..86f93f1065b81fbafc5c1c2f5f95a86961d49da1 100644 (file)
@@ -33,7 +33,7 @@ public class ComponentIndexCombinationTest extends ComponentIndexTest {
   public void return_empty_list_if_no_fields_match_query() {
     indexProject("struts", "Apache Struts");
 
-    assertThat(index.search(ComponentIndexQuery.builder().setQuery("missing").build()).isEmpty()).isTrue();
+    assertThat(index.searchSuggestions(SuggestionQuery.builder().setQuery("missing").build()).isEmpty()).isTrue();
   }
 
   @Test
@@ -48,14 +48,14 @@ public class ComponentIndexCombinationTest extends ComponentIndexTest {
     ComponentDto project = indexProject("struts", "Apache Struts");
     indexFile(project, "src/main/java/StrutsManager.java", "StrutsManager.java");
 
-    assertSearchResults(ComponentIndexQuery.builder().setQuery("struts").setQualifiers(singletonList(Qualifiers.PROJECT)).build(), project);
+    assertSearchResults(SuggestionQuery.builder().setQuery("struts").setQualifiers(singletonList(Qualifiers.PROJECT)).build(), project);
   }
 
   @Test
   public void should_limit_the_number_of_results() {
     IntStream.rangeClosed(0, 10).forEach(i -> indexProject("sonarqube" + i, "SonarQube" + i));
 
-    assertSearch(ComponentIndexQuery.builder().setQuery("sonarqube").setLimit(5).setQualifiers(singletonList(Qualifiers.PROJECT)).build()).hasSize(5);
+    assertSearch(SuggestionQuery.builder().setQuery("sonarqube").setLimit(5).setQualifiers(singletonList(Qualifiers.PROJECT)).build()).hasSize(5);
   }
 
   @Test
index 458fe4f5f4c49717d952f67ea6b89f29e151bc38..146ff2acbcf62d806203ab97e173521d7b1085e4 100644 (file)
@@ -41,7 +41,7 @@ public class ComponentIndexFeatureExactTest extends ComponentIndexTest {
     ComponentDto project1 = indexProject("project1", "LongNameLongNameLongNameLongNameSonarQube");
     ComponentDto project2 = indexProject("project2", "LongNameLongNameLongNameLongNameSonarQubeX");
 
-    ComponentIndexQuery query1 = ComponentIndexQuery.builder()
+    SuggestionQuery query1 = SuggestionQuery.builder()
       .setQuery("LongNameLongNameLongNameLongNameSonarQube")
       .setQualifiers(Collections.singletonList(PROJECT))
       .build();
index e37db705eeb1586583002020172ce51f582f8b4c..c06db8cceae59654bffb6c82f87f7a3fc32f376a 100644 (file)
@@ -44,14 +44,14 @@ public class ComponentIndexFeatureFavoriteTest extends ComponentIndexTest {
     ComponentDto project1 = indexProject("sonarqube", "SonarQube");
     ComponentDto project2 = indexProject("recent", "SonarQube Recently");
 
-    ComponentIndexQuery query1 = ComponentIndexQuery.builder()
+    SuggestionQuery query1 = SuggestionQuery.builder()
       .setQuery("SonarQube")
       .setQualifiers(singletonList(PROJECT))
       .setFavoriteKeys(of(project1.getDbKey()))
       .build();
     assertSearch(query1).containsExactly(uuids(project1, project2));
 
-    ComponentIndexQuery query2 = ComponentIndexQuery.builder()
+    SuggestionQuery query2 = SuggestionQuery.builder()
       .setQuery("SonarQube")
       .setQualifiers(singletonList(PROJECT))
       .setFavoriteKeys(of(project2.getDbKey()))
@@ -64,7 +64,7 @@ public class ComponentIndexFeatureFavoriteTest extends ComponentIndexTest {
     features.set(q -> termQuery(FIELD_KEY, "non-existing-value"), ComponentTextSearchFeatureRepertoire.FAVORITE);
     ComponentDto project1 = indexProject("foo", "foo");
 
-    ComponentIndexQuery query1 = ComponentIndexQuery.builder()
+    SuggestionQuery query1 = SuggestionQuery.builder()
       .setQuery("bar")
       .setQualifiers(singletonList(PROJECT))
       .setFavoriteKeys(of(project1.getDbKey()))
index 24158ff3296ad220abc7e1cc820f6671d9adf7b1..4d2c15340cfd4fc1e900e60a33d51865f433903a 100644 (file)
@@ -42,14 +42,14 @@ public class ComponentIndexFeatureRecentlyBrowsedTest extends ComponentIndexTest
     ComponentDto project1 = indexProject("sonarqube", "SonarQube");
     ComponentDto project2 = indexProject("recent", "SonarQube Recently");
 
-    ComponentIndexQuery query1 = ComponentIndexQuery.builder()
+    SuggestionQuery query1 = SuggestionQuery.builder()
       .setQuery("SonarQube")
       .setQualifiers(Collections.singletonList(PROJECT))
       .setRecentlyBrowsedKeys(of(project1.getDbKey()))
       .build();
     assertSearch(query1).containsExactly(uuids(project1, project2));
 
-    ComponentIndexQuery query2 = ComponentIndexQuery.builder()
+    SuggestionQuery query2 = SuggestionQuery.builder()
       .setQuery("SonarQube")
       .setQualifiers(Collections.singletonList(PROJECT))
       .setRecentlyBrowsedKeys(of(project2.getDbKey()))
index 97f81c171ebf0e55db8bf0521d6ee90af05af17b..e41d82ff6d2f7c46afd5b298225273f804d9c6c1 100644 (file)
@@ -62,11 +62,11 @@ public class ComponentIndexHighlightTest extends ComponentIndexTest {
   private void assertHighlighting(String fileName, String search, String expectedHighlighting) {
     indexFile(fileName);
 
-    ComponentIndexQuery query = ComponentIndexQuery.builder()
+    SuggestionQuery query = SuggestionQuery.builder()
       .setQuery(search)
       .setQualifiers(Collections.singletonList(Qualifiers.FILE))
       .build();
-    Stream<ComponentHitsPerQualifier> results = index.search(query, features.get()).getQualifiers();
+    Stream<ComponentHitsPerQualifier> results = index.searchSuggestions(query, features.get()).getQualifiers();
 
     assertThat(results).flatExtracting(ComponentHitsPerQualifier::getHits)
       .extracting(ComponentHit::getHighlightedText)
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexQueryTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexQueryTest.java
deleted file mode 100644 (file)
index 7192e92..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.component.index;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ComponentIndexQueryTest {
-
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
-
-  @Test
-  public void should_fail_with_IAE_if_query_is_empty() {
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Query must be at least two characters long");
-
-    ComponentIndexQuery.builder().setQuery("");
-  }
-
-  @Test
-  public void should_fail_with_IAE_if_query_is_one_character_long() {
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Query must be at least two characters long");
-
-    ComponentIndexQuery.builder().setQuery("a");
-  }
-
-  @Test
-  public void should_support_query_with_two_characters_long() {
-    ComponentIndexQuery query = ComponentIndexQuery.builder().setQuery("ab").build();
-
-    assertThat(query.getQuery()).isEqualTo("ab");
-  }
-
-  @Test
-  public void should_fail_with_IAE_if_limit_is_negative() {
-    ComponentIndexQuery.Builder query = ComponentIndexQuery.builder().setQuery("ab");
-
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Limit has to be strictly positive");
-
-    query.setLimit(-1);
-  }
-
-  @Test
-  public void should_fail_with_IAE_if_limit_is_zero() {
-    ComponentIndexQuery.Builder query = ComponentIndexQuery.builder().setQuery("ab");
-
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Limit has to be strictly positive");
-
-    query.setLimit(0);
-  }
-
-  @Test
-  public void should_support_positive_limit() {
-    ComponentIndexQuery query = ComponentIndexQuery.builder().setQuery("ab")
-      .setLimit(1).build();
-
-    assertThat(query.getLimit()).isEqualTo(1);
-  }
-}
index 205917259cff7495f175e1adfb85a5554c5e6285..42dd0859ac99305cfaed6e3818a7d0546a4bfaf6 100644 (file)
@@ -118,14 +118,14 @@ public class ComponentIndexScoreTest extends ComponentIndexTest {
     ComponentDto file2 = db.components().insertPrivateProject(c -> c.setName("File2"));
     index(file2);
 
-    assertSearch(ComponentIndexQuery.builder()
+    assertSearch(SuggestionQuery.builder()
       .setQuery("File")
       .setQualifiers(asList(PROJECT, MODULE, FILE))
       .setRecentlyBrowsedKeys(ImmutableSet.of(file1.getDbKey()))
       .setFavoriteKeys(ImmutableSet.of(file2.getDbKey()))
       .build()).containsExactly(uuids(file2, file1));
 
-    assertSearch(ComponentIndexQuery.builder()
+    assertSearch(SuggestionQuery.builder()
       .setQuery("File")
       .setQualifiers(asList(PROJECT, MODULE, FILE))
       .setRecentlyBrowsedKeys(ImmutableSet.of(file2.getDbKey()))
index cabec1a8c2fa921f0bcbf1f5d574165e09f35be0..bbbb51a2d4367d69e7dabc52e3b7fffc9f165dbf 100644 (file)
@@ -98,20 +98,20 @@ public abstract class ComponentIndexTest {
   }
 
   protected AbstractListAssert<?, ? extends List<? extends String>, String> assertSearch(String query) {
-    return assertSearch(ComponentIndexQuery.builder().setQuery(query).setQualifiers(asList(PROJECT, MODULE, FILE)).build());
+    return assertSearch(SuggestionQuery.builder().setQuery(query).setQualifiers(asList(PROJECT, MODULE, FILE)).build());
   }
 
-  protected AbstractListAssert<?, ? extends List<? extends String>, String> assertSearch(ComponentIndexQuery query) {
-    return assertThat(index.search(query, features.get()).getQualifiers())
+  protected AbstractListAssert<?, ? extends List<? extends String>, String> assertSearch(SuggestionQuery query) {
+    return assertThat(index.searchSuggestions(query, features.get()).getQualifiers())
       .flatExtracting(ComponentHitsPerQualifier::getHits)
       .extracting(ComponentHit::getUuid);
   }
 
   protected void assertSearchResults(String query, ComponentDto... expectedComponents) {
-    assertSearchResults(ComponentIndexQuery.builder().setQuery(query).setQualifiers(asList(PROJECT, MODULE, FILE)).build(), expectedComponents);
+    assertSearchResults(SuggestionQuery.builder().setQuery(query).setQualifiers(asList(PROJECT, MODULE, FILE)).build(), expectedComponents);
   }
 
-  protected void assertSearchResults(ComponentIndexQuery query, ComponentDto... expectedComponents) {
+  protected void assertSearchResults(SuggestionQuery query, ComponentDto... expectedComponents) {
     assertSearch(query).containsOnly(uuids(expectedComponents));
   }
 
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/SuggestionQueryTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/SuggestionQueryTest.java
new file mode 100644 (file)
index 0000000..b41105b
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.component.index;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SuggestionQueryTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void should_fail_with_IAE_if_query_is_empty() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Query must be at least two characters long");
+
+    SuggestionQuery.builder().setQuery("");
+  }
+
+  @Test
+  public void should_fail_with_IAE_if_query_is_one_character_long() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Query must be at least two characters long");
+
+    SuggestionQuery.builder().setQuery("a");
+  }
+
+  @Test
+  public void should_support_query_with_two_characters_long() {
+    SuggestionQuery query = SuggestionQuery.builder().setQuery("ab").build();
+
+    assertThat(query.getQuery()).isEqualTo("ab");
+  }
+
+  @Test
+  public void should_fail_with_IAE_if_limit_is_negative() {
+    SuggestionQuery.Builder query = SuggestionQuery.builder().setQuery("ab");
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Limit has to be strictly positive");
+
+    query.setLimit(-1);
+  }
+
+  @Test
+  public void should_fail_with_IAE_if_limit_is_zero() {
+    SuggestionQuery.Builder query = SuggestionQuery.builder().setQuery("ab");
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Limit has to be strictly positive");
+
+    query.setLimit(0);
+  }
+
+  @Test
+  public void should_support_positive_limit() {
+    SuggestionQuery query = SuggestionQuery.builder().setQuery("ab")
+      .setLimit(1).build();
+
+    assertThat(query.getLimit()).isEqualTo(1);
+  }
+}