Skip to content

Commit

Permalink
SONAR-9073 rename attributes of api/components/suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schwarz authored and bartfastiel committed Apr 20, 2017
1 parent c48062f commit 9f10a00
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 40 deletions.
@@ -0,0 +1,55 @@
/*
* 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.ws;

import org.sonar.api.resources.Qualifiers;

import static java.util.Arrays.stream;

public enum SuggestionCategory {
VIEW("views", Qualifiers.VIEW),
SUBVIEW("sub-views", Qualifiers.SUBVIEW),
PROJECT("projects", Qualifiers.PROJECT),
MODULE("modules", Qualifiers.MODULE),
FILE("files", Qualifiers.FILE),
UNIT_TEST_FILE("unit-test-files", Qualifiers.UNIT_TEST_FILE),;

private final String name;
private final String qualifier;

SuggestionCategory(String name, String qualifier) {
this.name = name;
this.qualifier = qualifier;
}

public String getName() {
return name;
}

public String getQualifier() {
return qualifier;
}

public static SuggestionCategory getByName(String name) {
return stream(values()).filter(c -> c.getName().equals(name)).findAny()
.orElseThrow(() -> new IllegalStateException(String.format("Cannot find category for name '%s'.", name)));
}
}
Expand Up @@ -26,7 +26,6 @@
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService;
Expand All @@ -42,10 +41,11 @@
import org.sonar.server.es.textsearch.ComponentTextSearchFeature; import org.sonar.server.es.textsearch.ComponentTextSearchFeature;
import org.sonarqube.ws.WsComponents.Component; import org.sonarqube.ws.WsComponents.Component;
import org.sonarqube.ws.WsComponents.SuggestionsWsResponse; import org.sonarqube.ws.WsComponents.SuggestionsWsResponse;
import org.sonarqube.ws.WsComponents.SuggestionsWsResponse.Qualifier; import org.sonarqube.ws.WsComponents.SuggestionsWsResponse.Category;


import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static java.util.Arrays.asList; import static java.util.Arrays.stream;
import static java.util.Collections.singletonList;
import static java.util.Optional.ofNullable; import static java.util.Optional.ofNullable;
import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.server.es.DefaultIndexSettings.MINIMUM_NGRAM_LENGTH; import static org.sonar.server.es.DefaultIndexSettings.MINIMUM_NGRAM_LENGTH;
Expand All @@ -58,15 +58,6 @@ public class SuggestionsAction implements ComponentsWsAction {
static final String PARAM_MORE = "more"; static final String PARAM_MORE = "more";
static final String SHORT_INPUT_WARNING = "short_input"; static final String SHORT_INPUT_WARNING = "short_input";


private static final String[] QUALIFIERS = {
Qualifiers.VIEW,
Qualifiers.SUBVIEW,
Qualifiers.PROJECT,
Qualifiers.MODULE,
Qualifiers.FILE,
Qualifiers.UNIT_TEST_FILE
};

static final int DEFAULT_LIMIT = 6; static final int DEFAULT_LIMIT = 6;
static final int EXTENDED_LIMIT = 20; static final int EXTENDED_LIMIT = 20;


Expand Down Expand Up @@ -95,8 +86,8 @@ public void define(WebService.NewController context) {
.setExampleValue("sonar"); .setExampleValue("sonar");


action.createParam(PARAM_MORE) action.createParam(PARAM_MORE)
.setDescription("Qualifier, for which to display " + EXTENDED_LIMIT + " instead of " + DEFAULT_LIMIT + " results") .setDescription("Category, for which to display " + EXTENDED_LIMIT + " instead of " + DEFAULT_LIMIT + " results")
.setPossibleValues(QUALIFIERS) .setPossibleValues(stream(SuggestionCategory.values()).map(SuggestionCategory::getName).toArray(String[]::new))
.setSince("6.4"); .setSince("6.4");
} }


Expand Down Expand Up @@ -125,10 +116,10 @@ private static String getWarning(String query) {
private List<ComponentsPerQualifier> getComponentsPerQualifiers(String more, ComponentIndexQuery.Builder queryBuilder) { private List<ComponentsPerQualifier> getComponentsPerQualifiers(String more, ComponentIndexQuery.Builder queryBuilder) {
List<ComponentsPerQualifier> componentsPerQualifiers; List<ComponentsPerQualifier> componentsPerQualifiers;
if (more == null) { if (more == null) {
queryBuilder.setQualifiers(asList(QUALIFIERS)) queryBuilder.setQualifiers(stream(SuggestionCategory.values()).map(SuggestionCategory::getQualifier).collect(Collectors.toList()))
.setLimit(DEFAULT_LIMIT); .setLimit(DEFAULT_LIMIT);
} else { } else {
queryBuilder.setQualifiers(Collections.singletonList(more)) queryBuilder.setQualifiers(singletonList(SuggestionCategory.getByName(more).getQualifier()))
.setLimit(EXTENDED_LIMIT); .setLimit(EXTENDED_LIMIT);
} }
componentsPerQualifiers = searchInIndex(queryBuilder.build()); componentsPerQualifiers = searchInIndex(queryBuilder.build());
Expand All @@ -141,12 +132,12 @@ private List<ComponentsPerQualifier> searchInIndex(ComponentIndexQuery component


private SuggestionsWsResponse toResponse(List<ComponentsPerQualifier> componentsPerQualifiers, @Nullable String warning) { private SuggestionsWsResponse toResponse(List<ComponentsPerQualifier> componentsPerQualifiers, @Nullable String warning) {
SuggestionsWsResponse.Builder builder = SuggestionsWsResponse.newBuilder() SuggestionsWsResponse.Builder builder = SuggestionsWsResponse.newBuilder()
.addAllResults(getResultsOfAllQualifiers(componentsPerQualifiers)); .addAllSuggestions(getResultsOfAllQualifiers(componentsPerQualifiers));
ofNullable(warning).ifPresent(builder::setWarning); ofNullable(warning).ifPresent(builder::setWarning);
return builder.build(); return builder.build();
} }


private List<Qualifier> getResultsOfAllQualifiers(List<ComponentsPerQualifier> componentsPerQualifiers) { private List<Category> getResultsOfAllQualifiers(List<ComponentsPerQualifier> componentsPerQualifiers) {
if (componentsPerQualifiers.isEmpty()) { if (componentsPerQualifiers.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
Expand All @@ -167,8 +158,8 @@ private List<Qualifier> getResultsOfAllQualifiers(List<ComponentsPerQualifier> c
.map(dto -> dtoToComponent(dto, organizationKeyByUuids)) .map(dto -> dtoToComponent(dto, organizationKeyByUuids))
.collect(toList()); .collect(toList());


return Qualifier.newBuilder() return Category.newBuilder()
.setQ(qualifier.getQualifier()) .setCategory(qualifier.getQualifier())
.setMore(qualifier.getNumberOfFurtherResults()) .setMore(qualifier.getNumberOfFurtherResults())
.addAllItems(results) .addAllItems(results)
.build(); .build();
Expand Down
@@ -1,7 +1,7 @@
{ {
"results": [ "suggestions": [
{ {
"q": "TRK", "category": "projects",
"items": [ "items": [
{ {
"organization": "default-organization", "organization": "default-organization",
Expand All @@ -17,7 +17,7 @@
"more": 74 "more": 74
}, },
{ {
"q": "FIL", "category": "files",
"items": [ "items": [
] ]
} }
Expand Down
Expand Up @@ -102,14 +102,14 @@ public void exact_match_in_one_qualifier() throws Exception {
.executeProtobuf(SuggestionsWsResponse.class); .executeProtobuf(SuggestionsWsResponse.class);


// assert match in qualifier "TRK" // assert match in qualifier "TRK"
assertThat(response.getResultsList()) assertThat(response.getSuggestionsList())
.filteredOn(q -> q.getItemsCount() > 0) .filteredOn(q -> q.getItemsCount() > 0)
.extracting(SuggestionsWsResponse.Qualifier::getQ) .extracting(SuggestionsWsResponse.Category::getCategory)
.containsExactly(Qualifiers.PROJECT); .containsExactly(Qualifiers.PROJECT);


// assert correct id to be found // assert correct id to be found
assertThat(response.getResultsList()) assertThat(response.getSuggestionsList())
.flatExtracting(SuggestionsWsResponse.Qualifier::getItemsList) .flatExtracting(SuggestionsWsResponse.Category::getItemsList)
.extracting(WsComponents.Component::getKey, WsComponents.Component::getOrganization) .extracting(WsComponents.Component::getKey, WsComponents.Component::getOrganization)
.containsExactly(tuple(project.getKey(), organization.getKey())); .containsExactly(tuple(project.getKey(), organization.getKey()));
} }
Expand All @@ -126,7 +126,7 @@ public void must_not_search_if_no_valid_tokens_are_provided() throws Exception {
.setParam(PARAM_QUERY, "S o") .setParam(PARAM_QUERY, "S o")
.executeProtobuf(SuggestionsWsResponse.class); .executeProtobuf(SuggestionsWsResponse.class);


assertThat(response.getResultsList()).filteredOn(q -> q.getItemsCount() > 0).isEmpty(); assertThat(response.getSuggestionsList()).filteredOn(q -> q.getItemsCount() > 0).isEmpty();
assertThat(response.getWarning()).contains(SHORT_INPUT_WARNING); assertThat(response.getWarning()).contains(SHORT_INPUT_WARNING);
} }


Expand Down Expand Up @@ -157,10 +157,10 @@ public void should_not_propose_to_show_more_results_if_5_projects_are_found() th


@Test @Test
public void show_show_more_results_if_requested() throws Exception { public void show_show_more_results_if_requested() throws Exception {
check_proposal_to_show_more_results(21, EXTENDED_LIMIT, 1L, Qualifiers.PROJECT); check_proposal_to_show_more_results(21, EXTENDED_LIMIT, 1L, SuggestionCategory.PROJECT);
} }


private void check_proposal_to_show_more_results(int numberOfProjects, int results, long numberOfMoreResults, @Nullable String moreQualifier) throws Exception { private void check_proposal_to_show_more_results(int numberOfProjects, int results, long numberOfMoreResults, @Nullable SuggestionCategory more) throws Exception {
String namePrefix = "MyProject"; String namePrefix = "MyProject";


List<ComponentDto> projects = range(0, numberOfProjects) List<ComponentDto> projects = range(0, numberOfProjects)
Expand All @@ -173,25 +173,25 @@ private void check_proposal_to_show_more_results(int numberOfProjects, int resul
TestRequest request = actionTester.newRequest() TestRequest request = actionTester.newRequest()
.setMethod("POST") .setMethod("POST")
.setParam(PARAM_QUERY, namePrefix); .setParam(PARAM_QUERY, namePrefix);
ofNullable(moreQualifier).ifPresent(q -> request.setParam(PARAM_MORE, q)); ofNullable(more).ifPresent(c -> request.setParam(PARAM_MORE, c.getName()));
SuggestionsWsResponse response = request SuggestionsWsResponse response = request
.executeProtobuf(SuggestionsWsResponse.class); .executeProtobuf(SuggestionsWsResponse.class);


// assert match in qualifier "TRK" // assert match in qualifier "TRK"
assertThat(response.getResultsList()) assertThat(response.getSuggestionsList())
.filteredOn(q -> q.getItemsCount() > 0) .filteredOn(q -> q.getItemsCount() > 0)
.extracting(SuggestionsWsResponse.Qualifier::getQ) .extracting(SuggestionsWsResponse.Category::getCategory)
.containsExactly(Qualifiers.PROJECT); .containsExactly(Qualifiers.PROJECT);


// include limited number of results in the response // include limited number of results in the response
assertThat(response.getResultsList()) assertThat(response.getSuggestionsList())
.flatExtracting(SuggestionsWsResponse.Qualifier::getItemsList) .flatExtracting(SuggestionsWsResponse.Category::getItemsList)
.hasSize(Math.min(results, numberOfProjects)); .hasSize(Math.min(results, numberOfProjects));


// indicate, that there are more results // indicate, that there are more results
assertThat(response.getResultsList()) assertThat(response.getSuggestionsList())
.filteredOn(q -> q.getItemsCount() > 0) .filteredOn(q -> q.getItemsCount() > 0)
.extracting(SuggestionsWsResponse.Qualifier::getMore) .extracting(SuggestionsWsResponse.Category::getMore)
.containsExactly(numberOfMoreResults); .containsExactly(numberOfMoreResults);
} }
} }
6 changes: 3 additions & 3 deletions sonar-ws/src/main/protobuf/ws-components.proto
Expand Up @@ -48,11 +48,11 @@ message ShowWsResponse {


// WS api/components/suggestions // WS api/components/suggestions
message SuggestionsWsResponse { message SuggestionsWsResponse {
repeated Qualifier results = 1; repeated Category suggestions = 1;
optional string warning = 2; optional string warning = 2;


message Qualifier { message Category {
optional string q = 1; optional string category = 1;
repeated Component items = 2; repeated Component items = 2;
optional int64 more = 3; optional int64 more = 3;
} }
Expand Down

0 comments on commit 9f10a00

Please sign in to comment.