]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7106 WS api/permissions/search_project_permissions rely on resource_index to...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 17 Mar 2016 18:00:53 +0000 (19:00 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 18 Mar 2016 08:03:48 +0000 (09:03 +0100)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java
sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java

index 96d92b973788b1649d3b9053fc7e36bc5f5257bc..83ef79bbcac96002648b16bea727073004fa26d8 100644 (file)
@@ -25,7 +25,6 @@ import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Table;
 import com.google.common.collect.TreeBasedTable;
-import java.util.Collection;
 import java.util.List;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -36,10 +35,10 @@ import org.sonar.api.utils.Paging;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentQuery;
 import org.sonar.db.permission.CountByProjectAndPermissionDto;
 import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
 
-import static java.util.Collections.singleton;
 import static java.util.Collections.singletonList;
 import static org.sonar.api.utils.Paging.forPageIndex;
 import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
@@ -49,19 +48,19 @@ import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRe
 public class SearchProjectPermissionsDataLoader {
   private final DbClient dbClient;
   private final PermissionDependenciesFinder finder;
-  private final Collection<String> rootQualifiers;
+  private final String[] rootQualifiers;
 
   public SearchProjectPermissionsDataLoader(DbClient dbClient, PermissionDependenciesFinder finder, ResourceTypes resourceTypes) {
     this.dbClient = dbClient;
     this.finder = finder;
-    this.rootQualifiers = Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER);
+    this.rootQualifiers = Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER).toArray(new String[resourceTypes.getRoots().size()]);
   }
 
   SearchProjectPermissionsData load(SearchProjectPermissionsWsRequest request) {
     DbSession dbSession = dbClient.openSession(false);
     try {
       SearchProjectPermissionsData.Builder data = newBuilder();
-      int countRootComponents = countRootComponents(dbSession, qualifiers(request.getQualifier()), request);
+      int countRootComponents = countRootComponents(dbSession, request);
       List<ComponentDto> rootComponents = searchRootComponents(dbSession, request, paging(request, countRootComponents));
       List<Long> rootComponentIds = Lists.transform(rootComponents, ComponentToIdFunction.INSTANCE);
 
@@ -82,25 +81,31 @@ public class SearchProjectPermissionsDataLoader {
       .andTotal(total);
   }
 
-  private int countRootComponents(DbSession dbSession, Collection<String> qualifiers, SearchProjectPermissionsWsRequest request) {
-    return dbClient.componentDao().countRootComponents(dbSession, qualifiers, request.getQuery());
+  private int countRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
+    return dbClient.componentDao().countByQuery(dbSession, toDbQuery(request));
   }
 
   private List<ComponentDto> searchRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request, Paging paging) {
-    String query = request.getQuery();
     Optional<WsProjectRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
 
     if (project.isPresent()) {
       return singletonList(finder.getRootComponentOrModule(dbSession, project.get()));
     }
 
-    return dbClient.componentDao().selectComponents(dbSession, qualifiers(request.getQualifier()), paging.offset(), paging.pageSize(), query);
+    return dbClient.componentDao().selectByQuery(dbSession, toDbQuery(request), paging.offset(), paging.pageSize());
   }
 
-  private Collection<String> qualifiers(@Nullable String requestQualifier) {
+  private ComponentQuery toDbQuery(SearchProjectPermissionsWsRequest wsRequest) {
+    return ComponentQuery.builder()
+      .setQualifiers(qualifiers(wsRequest.getQualifier()))
+      .setNameOrKeyQuery(wsRequest.getQuery())
+      .build();
+  }
+
+  private String[] qualifiers(@Nullable String requestQualifier) {
     return requestQualifier == null
       ? rootQualifiers
-      : singleton(requestQualifier);
+      : new String[] {requestQualifier};
   }
 
   private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
index 6118c8b0c709b3ba4f720214094e0aa3f6a941b9..fa54083e525734bde532f67d7bf131b60ef5c619 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.user.GroupDto;
@@ -71,6 +72,7 @@ public class SearchProjectPermissionsActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  ComponentDbTester componentDb = new ComponentDbTester(db);
 
   WsActionTester ws;
   I18nRule i18n = new I18nRule();
@@ -180,9 +182,9 @@ public class SearchProjectPermissionsActionTest {
 
   @Test
   public void search_by_query_on_name() {
-    insertComponent(newProjectDto().setName("project-name"));
-    insertComponent(newProjectDto().setName("another-name"));
-    commit();
+    componentDb.insertProjectAndSnapshot(newProjectDto().setName("project-name"));
+    componentDb.insertProjectAndSnapshot(newProjectDto().setName("another-name"));
+    componentDb.indexProjects();
 
     String result = ws.newRequest()
       .setParam(TEXT_QUERY, "project")
@@ -193,13 +195,13 @@ public class SearchProjectPermissionsActionTest {
   }
 
   @Test
-  public void search_by_query_on_key() {
-    insertComponent(newProjectDto().setKey("project-key"));
-    insertComponent(newProjectDto().setKey("another-key"));
-    commit();
+  public void search_by_query_on_key_must_match_exactly() {
+    componentDb.insertProjectAndSnapshot(newProjectDto().setKey("project-key"));
+    componentDb.insertProjectAndSnapshot(newProjectDto().setKey("another-key"));
+    componentDb.indexProjects();
 
     String result = ws.newRequest()
-      .setParam(TEXT_QUERY, "project")
+      .setParam(TEXT_QUERY, "project-key")
       .execute().getInput();
 
     assertThat(result).contains("project-key")
@@ -209,9 +211,9 @@ public class SearchProjectPermissionsActionTest {
   @Test
   public void handle_more_than_1000_projects() {
     for (int i = 1; i <= 1001; i++) {
-      insertComponent(newProjectDto("project-uuid-" + i));
+      componentDb.insertProjectAndSnapshot(newProjectDto("project-uuid-" + i));
     }
-    commit();
+    componentDb.indexProjects();
 
     String result = ws.newRequest()
       .setParam(TEXT_QUERY, "project")
index 77345bdfafadb6214635e361b90420c7491d467a..f1628ddedeab8af508a6324de1f12bbef6494b39 100644 (file)
@@ -211,26 +211,6 @@ public class ComponentDao implements Dao {
     return mapper(session).selectProjects();
   }
 
-  /**
-   * Does not return component copies
-   */
-  public List<ComponentDto> selectComponents(DbSession session, Collection<String> qualifiers, int offset, int limit, @Nullable String query) {
-    Map<String, Object> parameters = newHashMapWithExpectedSize(3);
-    addProjectQualifier(parameters);
-    addPartialQueryParameterIfNotNull(parameters, query);
-    addQualifiers(parameters, qualifiers);
-
-    return mapper(session).selectComponents(parameters, new RowBounds(offset, limit));
-  }
-
-  public int countRootComponents(DbSession session, Collection<String> qualifiers, @Nullable String query) {
-    Map<String, Object> parameters = newHashMapWithExpectedSize(2);
-    addPartialQueryParameterIfNotNull(parameters, query);
-    addQualifiers(parameters, qualifiers);
-
-    return mapper(session).countRootComponents(parameters);
-  }
-
   public List<ComponentDto> selectProvisionedProjects(DbSession session, int offset, int limit, @Nullable String query) {
     Map<String, Object> parameters = newHashMapWithExpectedSize(2);
     addProjectQualifier(parameters);
@@ -292,10 +272,6 @@ public class ComponentDao implements Dao {
     parameters.put("qualifier", Qualifiers.PROJECT);
   }
 
-  private static void addQualifiers(Map<String, Object> parameters, Collection<String> qualifiers) {
-    parameters.put("qualifiers", qualifiers);
-  }
-
   public void insert(DbSession session, ComponentDto item) {
     mapper(session).insert(item);
   }
index 1026c04ab0453542af2cde84e08ea6cd77956fd6..8a4de8f8cdeb5f360e9da642696bd7accdaf8363 100644 (file)
@@ -655,21 +655,6 @@ public class ComponentDaoTest {
     assertThat(underTest.selectByKey(dbSession, "PROJECT_2")).isPresent();
   }
 
-  @Test
-  public void select_components_with_paging_query_and_qualifiers() {
-    underTest.insert(dbSession, newProjectDto().setName("aaaa-name"));
-    underTest.insert(dbSession, newView());
-    underTest.insert(dbSession, newDeveloper("project-name"));
-    for (int i = 9; i >= 1; i--) {
-      underTest.insert(dbSession, newProjectDto().setName("project-" + i));
-    }
-
-    List<ComponentDto> result = underTest.selectComponents(dbSession, singleton(Qualifiers.PROJECT), 1, 3, "project");
-
-    assertThat(result).hasSize(3);
-    assertThat(result).extracting("name").containsExactly("project-2", "project-3", "project-4");
-  }
-
   @Test
   public void select_by_query_with_paging_query_and_qualifiers() {
     componentDb.insertProjectAndSnapshot(newProjectDto().setName("aaaa-name"));