From: Teryk Bellahsene Date: Wed, 11 Oct 2017 12:46:26 +0000 (+0200) Subject: SONAR-9928 ComponentIndexQuery renamed to SuggestionQuery X-Git-Tag: 6.7-RC1~286 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d0234973112df69164cd8435a74e714fa97625cd;p=sonarqube.git SONAR-9928 ComponentIndexQuery renamed to SuggestionQuery --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java index 41b07f569e6..0946a08abd7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndex.java @@ -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 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 index e314afce4db..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexQuery.java +++ /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 qualifiers; - private final Set recentlyBrowsedKeys; - private final Set 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 getQualifiers() { - return qualifiers; - } - - public String getQuery() { - return query; - } - - public Set getRecentlyBrowsedKeys() { - return recentlyBrowsedKeys; - } - - public int getSkip() { - return skip; - } - - public int getLimit() { - return limit; - } - - public static Builder builder() { - return new Builder(); - } - - public Set getFavoriteKeys() { - return favoriteKeys; - } - - public static class Builder { - private String query; - private Collection qualifiers = Collections.emptyList(); - private Set recentlyBrowsedKeys = Collections.emptySet(); - private Set 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 qualifiers) { - this.qualifiers = Collections.unmodifiableCollection(qualifiers); - return this; - } - - public Builder setRecentlyBrowsedKeys(Set recentlyBrowsedKeys) { - this.recentlyBrowsedKeys = Collections.unmodifiableSet(recentlyBrowsedKeys); - return this; - } - - public Builder setFavoriteKeys(Set 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 index 00000000000..8e15fcfee4c --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/SuggestionQuery.java @@ -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 qualifiers; + private final Set recentlyBrowsedKeys; + private final Set 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 getQualifiers() { + return qualifiers; + } + + public String getQuery() { + return query; + } + + public Set getRecentlyBrowsedKeys() { + return recentlyBrowsedKeys; + } + + public int getSkip() { + return skip; + } + + public int getLimit() { + return limit; + } + + public static Builder builder() { + return new Builder(); + } + + public Set getFavoriteKeys() { + return favoriteKeys; + } + + public static class Builder { + private String query; + private Collection qualifiers = Collections.emptyList(); + private Set recentlyBrowsedKeys = Collections.emptySet(); + private Set 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 qualifiers) { + this.qualifiers = Collections.unmodifiableCollection(qualifiers); + return this; + } + + public Builder setRecentlyBrowsedKeys(Set recentlyBrowsedKeys) { + this.recentlyBrowsedKeys = Collections.unmodifiableSet(recentlyBrowsedKeys); + return this; + } + + public Builder setFavoriteKeys(Set 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); + } + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java index dbc6953d94d..5d728d6ab7d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java @@ -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 favorites = favoriteFinder.list(); Set 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 recentlyBrowsedKeys, Set favoriteUuids, diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java index 56b82d6730d..86f93f1065b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexCombinationTest.java @@ -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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java index 458fe4f5f4c..146ff2acbcf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureExactTest.java @@ -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(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java index e37db705eeb..c06db8cceae 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureFavoriteTest.java @@ -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())) diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java index 24158ff3296..4d2c15340cf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexFeatureRecentlyBrowsedTest.java @@ -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())) diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java index 97f81c171eb..e41d82ff6d2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java @@ -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 results = index.search(query, features.get()).getQualifiers(); + Stream 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 index 7192e929fc8..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexQueryTest.java +++ /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); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java index 205917259cf..42dd0859ac9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java @@ -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())) diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java index cabec1a8c2f..bbbb51a2d43 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java @@ -98,20 +98,20 @@ public abstract class ComponentIndexTest { } protected AbstractListAssert, 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, String> assertSearch(ComponentIndexQuery query) { - return assertThat(index.search(query, features.get()).getQualifiers()) + protected AbstractListAssert, 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 index 00000000000..b41105b4d60 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/SuggestionQueryTest.java @@ -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); + } +}