From: Teryk Bellahsene Date: Thu, 11 May 2017 16:14:19 +0000 (+0200) Subject: SONAR-9056 WS api/components/suggestion filter allowed qualifiers X-Git-Tag: 6.4-RC1~25 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c01e10ac1000017f3930193d42222e257155fc42;p=sonarqube.git SONAR-9056 WS api/components/suggestion filter allowed qualifiers --- diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ResourceTypesRule.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ResourceTypesRule.java index 48799b3b949..7ffb37640c9 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ResourceTypesRule.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ResourceTypesRule.java @@ -86,7 +86,9 @@ public class ResourceTypesRule extends ResourceTypes { @Override public ResourceType get(String qualifier) { - throw new UnsupportedOperationException(); + return allResourceTypes.stream() + .filter(resourceType -> qualifier.equals(resourceType.getQualifier())) + .findAny().orElse(null); } @Override 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 4fc37137a3f..ced1b25dda1 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 @@ -33,6 +33,8 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nullable; +import org.sonar.api.resources.ResourceType; +import org.sonar.api.resources.ResourceTypes; import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; @@ -59,6 +61,7 @@ import org.sonarqube.ws.WsComponents.SuggestionsWsResponse.Suggestion; import static com.google.common.base.Preconditions.checkState; import static java.util.Arrays.stream; +import static java.util.Collections.emptyList; import static java.util.Collections.emptySet; import static java.util.Collections.singletonList; import static org.sonar.api.web.UserRole.USER; @@ -85,14 +88,16 @@ public class SuggestionsAction implements ComponentsWsAction { private final ComponentIndex index; private final FavoriteFinder favoriteFinder; private final UserSession userSession; + private final ResourceTypes resourceTypes; private DbClient dbClient; - public SuggestionsAction(DbClient dbClient, ComponentIndex index, FavoriteFinder favoriteFinder, UserSession userSession) { + public SuggestionsAction(DbClient dbClient, ComponentIndex index, FavoriteFinder favoriteFinder, UserSession userSession, ResourceTypes resourceTypes) { this.dbClient = dbClient; this.index = index; this.favoriteFinder = favoriteFinder; this.userSession = userSession; + this.resourceTypes = resourceTypes; } @Override @@ -247,11 +252,19 @@ public class SuggestionsAction implements ComponentsWsAction { return Arrays.stream(query.split(DefaultIndexSettings.SEARCH_TERM_TOKENIZER_PATTERN)); } - private static List getQualifiers(@Nullable String more) { + private List getQualifiers(@Nullable String more) { + Set availableQualifiers = resourceTypes.getAll().stream().map(ResourceType::getQualifier).collect(MoreCollectors.toSet()); if (more == null) { - return stream(SuggestionCategory.values()).map(SuggestionCategory::getQualifier).collect(Collectors.toList()); + return stream(SuggestionCategory.values()) + .map(SuggestionCategory::getQualifier) + .filter(availableQualifiers::contains) + .collect(Collectors.toList()); } - return singletonList(SuggestionCategory.getByName(more).getQualifier()); + + String qualifier = SuggestionCategory.getByName(more).getQualifier(); + return availableQualifiers.contains(qualifier) ? + singletonList(qualifier) + : emptyList(); } private SuggestionsWsResponse.Builder buildResponse(Set recentlyBrowsedKeys, Set favoriteUuids, ComponentIndexResults componentsPerQualifiers, diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java index 9c5e0c35a04..f31a4a0342a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java @@ -32,9 +32,11 @@ import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; +import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; +import org.sonar.db.component.ResourceTypesRule; import org.sonar.db.organization.OrganizationDto; import org.sonar.server.component.index.ComponentIndex; import org.sonar.server.component.index.ComponentIndexDefinition; @@ -65,6 +67,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.groups.Tuple.tuple; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.sonar.api.resources.Qualifiers.FILE; +import static org.sonar.api.resources.Qualifiers.MODULE; +import static org.sonar.api.resources.Qualifiers.PROJECT; +import static org.sonar.api.resources.Qualifiers.SUBVIEW; +import static org.sonar.api.resources.Qualifiers.VIEW; import static org.sonar.api.web.UserRole.USER; import static org.sonar.db.component.ComponentTesting.newModuleDto; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; @@ -77,6 +84,9 @@ import static org.sonarqube.ws.WsComponents.SuggestionsWsResponse.Category; import static org.sonarqube.ws.WsComponents.SuggestionsWsResponse.Organization; public class SuggestionsActionTest { + private static final String[] SUGGESTION_QUALIFIERS = Stream.of(SuggestionCategory.values()) + .map(SuggestionCategory::getQualifier) + .collect(MoreCollectors.toList()).toArray(new String[0]); @Rule public DbTester db = DbTester.create(System2.INSTANCE); @@ -84,11 +94,12 @@ public class SuggestionsActionTest { public EsTester es = new EsTester(new ComponentIndexDefinition(new MapSettings())); @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); + public ResourceTypesRule resourceTypes = new ResourceTypesRule(); private ComponentIndexer componentIndexer = new ComponentIndexer(db.getDbClient(), es.client()); private FavoriteFinder favoriteFinder = mock(FavoriteFinder.class); private ComponentIndex index = new ComponentIndex(es.client(), new AuthorizationTypeSupport(userSessionRule)); - private SuggestionsAction underTest = new SuggestionsAction(db.getDbClient(), index, favoriteFinder, userSessionRule); + private SuggestionsAction underTest = new SuggestionsAction(db.getDbClient(), index, favoriteFinder, userSessionRule, resourceTypes); private OrganizationDto organization; private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(es, componentIndexer); private WsActionTester ws = new WsActionTester(underTest); @@ -96,6 +107,7 @@ public class SuggestionsActionTest { @Before public void setUp() { organization = db.organizations().insert(); + resourceTypes.setAllQualifiers(SUGGESTION_QUALIFIERS); } @Test @@ -160,7 +172,7 @@ public class SuggestionsActionTest { assertThat(response.getResultsList()) .filteredOn(q -> q.getItemsCount() > 0) .extracting(Category::getQ) - .containsExactly(Qualifiers.PROJECT); + .containsExactly(PROJECT); // assert correct id to be found assertThat(response.getResultsList()) @@ -184,7 +196,7 @@ public class SuggestionsActionTest { assertThat(response.getResultsList()) .filteredOn(q -> q.getItemsCount() > 0) .extracting(Category::getQ) - .containsExactly(Qualifiers.PROJECT); + .containsExactly(PROJECT); // assert correct id to be found assertThat(response.getResultsList()) @@ -225,7 +237,7 @@ public class SuggestionsActionTest { assertThat(response.getResultsList()) .filteredOn(q -> q.getItemsCount() > 0) .extracting(Category::getQ) - .containsExactly(Qualifiers.PROJECT); + .containsExactly(PROJECT); // assert correct id to be found assertThat(response.getResultsList()) @@ -340,6 +352,23 @@ public class SuggestionsActionTest { .containsExactlyInAnyOrder(tuple("VW", 0), tuple("SVW", 0), tuple("TRK", 1), tuple("BRC", 0), tuple("FIL", 0), tuple("UTS", 0)); } + @Test + public void suggestions_should_filter_allowed_qualifiers() { + resourceTypes.setAllQualifiers(PROJECT, MODULE, FILE); + ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organization)); + componentIndexer.indexProject(project.projectUuid(), ProjectIndexer.Cause.PROJECT_CREATION); + userSessionRule.addProjectPermission(USER, project); + + SuggestionsWsResponse response = ws.newRequest() + .setMethod("POST") + .setParam(PARAM_RECENTLY_BROWSED, project.key()) + .executeProtobuf(SuggestionsWsResponse.class); + + assertThat(response.getResultsList()) + .extracting(Category::getQ) + .containsExactlyInAnyOrder(PROJECT, MODULE, FILE); + } + @Test public void exact_match_in_one_qualifier() throws Exception { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organization)); @@ -356,7 +385,7 @@ public class SuggestionsActionTest { assertThat(response.getResultsList()) .filteredOn(q -> q.getItemsCount() > 0) .extracting(Category::getQ) - .containsExactly(Qualifiers.PROJECT); + .containsExactly(PROJECT); // assert correct id to be found assertThat(response.getResultsList()) @@ -659,6 +688,13 @@ public class SuggestionsActionTest { check_proposal_to_show_more_results(27, 20, 1L, SuggestionCategory.PROJECT, false); } + @Test + public void show_more_results_filter_out_if_non_allowed_qualifiers() { + resourceTypes.setAllQualifiers(VIEW, SUBVIEW); + + check_proposal_to_show_more_results(10, 0, 0L, SuggestionCategory.PROJECT, true); + } + private void check_proposal_to_show_more_results(int numberOfProjects, int expectedNumberOfResults, long expectedNumberOfMoreResults, @Nullable SuggestionCategory more, boolean useQuery) { String namePrefix = "MyProject"; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java index 1d67e4624ee..10c15a14346 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java @@ -47,12 +47,7 @@ import org.sonar.api.server.ServerSide; @ComputeEngineSide public class ResourceTypes { - public static final Predicate AVAILABLE_FOR_FILTERS = new Predicate() { - @Override - public boolean apply(@Nullable ResourceType input) { - return input != null && input.getBooleanProperty("supportsMeasureFilters"); - } - }; + public static final Predicate AVAILABLE_FOR_FILTERS = input -> input != null && input.getBooleanProperty("supportsMeasureFilters"); private final Map treeByQualifier; private final Map typeByQualifier;