From b175d0ec0966792cdeb401196305daa995b22370 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 30 Jan 2017 16:07:56 +0100 Subject: [PATCH] SONAR-7298 Correctly return modules when using 'search' param --- .../sonar/server/project/ws/IndexAction.java | 22 +++++++++---------- .../server/project/ws/IndexActionTest.java | 19 ++++++++++++++-- ...name.json => search_projects_by_name.json} | 0 .../search_projects_with_modules_by_name.json | 16 ++++++++++++++ 4 files changed, 44 insertions(+), 13 deletions(-) rename server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/{search_project_by_name.json => search_projects_by_name.json} (100%) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_with_modules_by_name.json diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java index fb9ea3c316e..1661ccb99cb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java @@ -22,6 +22,7 @@ package org.sonar.server.project.ws; import com.google.common.io.Resources; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; @@ -39,7 +40,6 @@ import org.sonar.server.user.UserSession; import static java.util.Optional.ofNullable; import static org.sonar.api.web.UserRole.USER; -import static org.sonar.core.util.stream.Collectors.toList; import static org.sonar.core.util.stream.Collectors.uniqueIndex; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_INDEX; @@ -92,7 +92,7 @@ public class IndexAction implements ProjectsWsAction { @Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { - List projects = getAuthorizedProjects(dbSession, searchProjects(dbSession, request)); + List projects = getAuthorizedComponents(dbSession, searchComponents(dbSession, request)); JsonWriter json = response.newJsonWriter(); json.beginArray(); for (ComponentDto project : projects) { @@ -112,7 +112,7 @@ public class IndexAction implements ProjectsWsAction { } } - private List searchProjects(DbSession dbSession, Request request) { + private List searchComponents(DbSession dbSession, Request request) { String projectKey = request.param(PARAM_KEY); List projects = new ArrayList<>(); if (projectKey != null) { @@ -125,16 +125,16 @@ public class IndexAction implements ProjectsWsAction { return projects; } - private List getAuthorizedProjects(DbSession dbSession, List projectDtos) { - if (projectDtos.isEmpty()) { + private List getAuthorizedComponents(DbSession dbSession, List components) { + if (components.isEmpty()) { return Collections.emptyList(); } - Map projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId)); - Set authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, - projectDtos.stream().map(ComponentDto::getId).collect(toList()), - userSession.getUserId(), USER); - return projectDtos.stream() - .filter(c -> authorizedProjectIds.contains(projectIdsByUuids.get(c.projectUuid()))) + Set projectUuids = components.stream().map(ComponentDto::projectUuid).collect(Collectors.toSet()); + List projects = dbClient.componentDao().selectByUuids(dbSession, projectUuids); + Map projectIdsByUuids = projects.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId)); + Collection authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIdsByUuids.values(), userSession.getUserId(), USER); + return components.stream() + .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid()))) .collect(Collectors.toList()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java index a3be3e099bd..f918edcebae 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java @@ -118,7 +118,7 @@ public class IndexActionTest { } @Test - public void search_project_by_name() throws Exception { + public void search_projects_by_name() throws Exception { insertProjectsAuthorizedForUser( newProjectDto(db.getDefaultOrganization()).setKey("org.jenkins-ci.plugins:sonar").setName("Jenkins Sonar Plugin"), newProjectDto(db.getDefaultOrganization()).setKey("org.codehaus.sonar-plugins:sonar-ant-task").setName("Sonar Ant Task"), @@ -126,7 +126,22 @@ public class IndexActionTest { String result = call(null, "Plu", null); - verifyResult(result, "search_project_by_name.json"); + verifyResult(result, "search_projects_by_name.json"); + } + + @Test + public void search_projects_with_modules_by_name() throws Exception { + ComponentDto project1 = newProjectDto(db.getDefaultOrganization()).setKey("org.jenkins-ci.plugins:sonar").setName("Jenkins Sonar Plugin"); + ComponentDto project2 = newProjectDto(db.getDefaultOrganization()).setKey("org.codehaus.sonar-plugins:sonar-ant-task").setName("Sonar Ant Task"); + insertProjectsAuthorizedForUser(project1, project2); + db.components().insertComponents( + newModuleDto(project1).setKey("org.jenkins-ci.plugins:sonar-common-db").setName("Jenkins Common DB"), + newModuleDto(project1).setKey("org.jenkins-ci.plugins:sonar-common-server").setName("Jenkins Common Server"), + newModuleDto(project2).setKey("org.codehaus.sonar-plugins:sonar-ant-db").setName("Ant DB")); + + String result = call(null, "Com", true); + + verifyResult(result, "search_projects_with_modules_by_name.json"); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_project_by_name.json b/server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_by_name.json similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_project_by_name.json rename to server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_by_name.json diff --git a/server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_with_modules_by_name.json b/server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_with_modules_by_name.json new file mode 100644 index 00000000000..a82ce85f671 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_with_modules_by_name.json @@ -0,0 +1,16 @@ +[ + { + "id": 15, + "k": "org.jenkins-ci.plugins:sonar-common-db", + "nm": "Jenkins Common DB", + "sc": "PRJ", + "qu": "BRC" + }, + { + "id": 15, + "k": "org.jenkins-ci.plugins:sonar-common-server", + "nm": "Jenkins Common Server", + "sc": "PRJ", + "qu": "BRC" + } +] -- 2.39.5