From 38ce80934961773a9a35ec0401994b3d726597dc Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 17 Mar 2016 19:00:53 +0100 Subject: [PATCH] SONAR-7106 WS api/permissions/search_project_permissions rely on resource_index to search for component name --- .../SearchProjectPermissionsDataLoader.java | 27 +++++++++++-------- .../SearchProjectPermissionsActionTest.java | 22 ++++++++------- .../org/sonar/db/component/ComponentDao.java | 24 ----------------- .../sonar/db/component/ComponentDaoTest.java | 15 ----------- 4 files changed, 28 insertions(+), 60 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java index 96d92b97378..83ef79bbcac 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java @@ -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 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 rootComponents = searchRootComponents(dbSession, request, paging(request, countRootComponents)); List rootComponentIds = Lists.transform(rootComponents, ComponentToIdFunction.INSTANCE); @@ -82,25 +81,31 @@ public class SearchProjectPermissionsDataLoader { .andTotal(total); } - private int countRootComponents(DbSession dbSession, Collection 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 searchRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request, Paging paging) { - String query = request.getQuery(); Optional 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 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 userCountByRootComponentIdAndPermission(DbSession dbSession, List rootComponentIds) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java index 6118c8b0c70..fa54083e525 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java @@ -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") diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java index 77345bdfafa..f1628ddedea 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java @@ -211,26 +211,6 @@ public class ComponentDao implements Dao { return mapper(session).selectProjects(); } - /** - * Does not return component copies - */ - public List selectComponents(DbSession session, Collection qualifiers, int offset, int limit, @Nullable String query) { - Map 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 qualifiers, @Nullable String query) { - Map parameters = newHashMapWithExpectedSize(2); - addPartialQueryParameterIfNotNull(parameters, query); - addQualifiers(parameters, qualifiers); - - return mapper(session).countRootComponents(parameters); - } - public List selectProvisionedProjects(DbSession session, int offset, int limit, @Nullable String query) { Map parameters = newHashMapWithExpectedSize(2); addProjectQualifier(parameters); @@ -292,10 +272,6 @@ public class ComponentDao implements Dao { parameters.put("qualifier", Qualifiers.PROJECT); } - private static void addQualifiers(Map parameters, Collection qualifiers) { - parameters.put("qualifiers", qualifiers); - } - public void insert(DbSession session, ComponentDto item) { mapper(session).insert(item); } diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java index 1026c04ab04..8a4de8f8cde 100644 --- a/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java @@ -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 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")); -- 2.39.5