]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7298 Correctly return modules when using 'search' param 1593/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 30 Jan 2017 15:07:56 +0000 (16:07 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 31 Jan 2017 07:31:36 +0000 (08:31 +0100)
server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_project_by_name.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_by_name.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_with_modules_by_name.json [new file with mode: 0644]

index fb9ea3c316e82e746f6bc7b30ae558f601e85745..1661ccb99cb361d3938a3eb9d26aed46c1b0d576 100644 (file)
@@ -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<ComponentDto> projects = getAuthorizedProjects(dbSession, searchProjects(dbSession, request));
+      List<ComponentDto> 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<ComponentDto> searchProjects(DbSession dbSession, Request request) {
+  private List<ComponentDto> searchComponents(DbSession dbSession, Request request) {
     String projectKey = request.param(PARAM_KEY);
     List<ComponentDto> projects = new ArrayList<>();
     if (projectKey != null) {
@@ -125,16 +125,16 @@ public class IndexAction implements ProjectsWsAction {
     return projects;
   }
 
-  private List<ComponentDto> getAuthorizedProjects(DbSession dbSession, List<ComponentDto> projectDtos) {
-    if (projectDtos.isEmpty()) {
+  private List<ComponentDto> getAuthorizedComponents(DbSession dbSession, List<ComponentDto> components) {
+    if (components.isEmpty()) {
       return Collections.emptyList();
     }
-    Map<String, Long> projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
-    Set<Long> 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<String> projectUuids = components.stream().map(ComponentDto::projectUuid).collect(Collectors.toSet());
+    List<ComponentDto> projects = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
+    Map<String, Long> projectIdsByUuids = projects.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
+    Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIdsByUuids.values(), userSession.getUserId(), USER);
+    return components.stream()
+      .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
       .collect(Collectors.toList());
   }
 
index a3be3e099bd46b7c1b80577bb2b2d2302812f657..f918edcebaed39234bc0a3c84f7587e3ca00ec38 100644 (file)
@@ -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_project_by_name.json
deleted file mode 100644 (file)
index bd36e68..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-[
-  {
-    "id": 7,
-    "k": "org.jenkins-ci.plugins:sonar",
-    "nm": "Jenkins Sonar Plugin",
-    "sc": "PRJ",
-    "qu": "TRK"
-  },
-  {
-    "id": 9,
-    "k": "org.codehaus.sonar-plugins:sonar-build-breaker-plugin",
-    "nm": "Sonar Build Breaker Plugin",
-    "sc": "PRJ",
-    "qu": "TRK"
-  }
-]
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_by_name.json b/server/sonar-server/src/test/resources/org/sonar/server/project/ws/IndexActionTest/search_projects_by_name.json
new file mode 100644 (file)
index 0000000..bd36e68
--- /dev/null
@@ -0,0 +1,16 @@
+[
+  {
+    "id": 7,
+    "k": "org.jenkins-ci.plugins:sonar",
+    "nm": "Jenkins Sonar Plugin",
+    "sc": "PRJ",
+    "qu": "TRK"
+  },
+  {
+    "id": 9,
+    "k": "org.codehaus.sonar-plugins:sonar-build-breaker-plugin",
+    "nm": "Sonar Build Breaker Plugin",
+    "sc": "PRJ",
+    "qu": "TRK"
+  }
+]
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 (file)
index 0000000..a82ce85
--- /dev/null
@@ -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"
+  }
+]