]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9056 WS api/components/suggestion filter allowed qualifiers
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 11 May 2017 16:14:19 +0000 (18:14 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 12 May 2017 10:37:19 +0000 (12:37 +0200)
server/sonar-db-dao/src/test/java/org/sonar/db/component/ResourceTypesRule.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java

index 48799b3b9495f0cd2eb085bfead0148356c7770b..7ffb37640c91a39a820572c21e6ebd551eb36c3d 100644 (file)
@@ -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
index 4fc37137a3fb0d8dd14aa1b93d0f78288f5854d9..ced1b25dda16bf4a05dfe6e3a7cbe44549769e72 100644 (file)
@@ -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<String> getQualifiers(@Nullable String more) {
+  private List<String> getQualifiers(@Nullable String more) {
+    Set<String> 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<String> recentlyBrowsedKeys, Set<String> favoriteUuids, ComponentIndexResults componentsPerQualifiers,
index 9c5e0c35a04f63b665ecfa73b8d1c4bff1a48aa1..f31a4a0342ad6ea7a1f38427c67b7ee6de6e27bd 100644 (file)
@@ -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";
index 1d67e4624eec51d2ec1f3eee46fd4bd33b988352..10c15a143465b35fd22afa57e587401ed65ac722 100644 (file)
@@ -47,12 +47,7 @@ import org.sonar.api.server.ServerSide;
 @ComputeEngineSide
 public class ResourceTypes {
 
-  public static final Predicate<ResourceType> AVAILABLE_FOR_FILTERS = new Predicate<ResourceType>() {
-    @Override
-    public boolean apply(@Nullable ResourceType input) {
-      return input != null && input.getBooleanProperty("supportsMeasureFilters");
-    }
-  };
+  public static final Predicate<ResourceType> AVAILABLE_FOR_FILTERS = input -> input != null && input.getBooleanProperty("supportsMeasureFilters");
 
   private final Map<String, ResourceTypeTree> treeByQualifier;
   private final Map<String, ResourceType> typeByQualifier;