]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8651 factor organization related in projects actions
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 31 Jan 2017 12:48:15 +0000 (13:48 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 2 Feb 2017 15:12:13 +0000 (16:12 +0100)
server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/project/ws/ProvisionedAction.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/GhostsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java

index 8a06f08e34a499662b664b4dc608517b38a705b8..fb52681288af8553e3634f3a6893dddf5c69a6a6 100644 (file)
@@ -25,6 +25,7 @@ public class ProjectsWsModule extends Module {
   @Override
   protected void configureModule() {
     add(
+      ProjectsWsSupport.class,
       ProjectsWs.class,
       CreateAction.class,
       IndexAction.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java
new file mode 100644 (file)
index 0000000..e587cbf
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.project.ws;
+
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
+
+import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+
+public class ProjectsWsSupport {
+  static final String PARAM_ORGANIZATION = "organization";
+
+  private final DbClient dbClient;
+
+  public ProjectsWsSupport(DbClient dbClient) {
+    this.dbClient = dbClient;
+  }
+
+  void addOrganizationParam(WebService.NewAction action) {
+    action.createParam(PARAM_ORGANIZATION)
+      .setDescription("The key of the organization")
+      .setRequired(false)
+      .setInternal(true)
+      .setSince("6.3");
+  }
+
+  OrganizationDto getOrganization(DbSession dbSession, String organizationKey) {
+    return checkFoundWithOptional(
+        dbClient.organizationDao().selectByKey(dbSession, organizationKey),
+        "No organization for key '%s'", organizationKey);
+  }
+}
index f7101ec670fb9da8bfc7f97af8dd4754817a9c6a..6bf6ee984acdc821416304c7d1ec80a2fa7e4502 100644 (file)
@@ -41,19 +41,20 @@ import org.sonar.server.user.UserSession;
 
 import static com.google.common.collect.Sets.newHashSet;
 import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
-import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+import static org.sonar.server.project.ws.ProjectsWsSupport.PARAM_ORGANIZATION;
 
 public class ProvisionedAction implements ProjectsWsAction {
 
-  private static final String PARAM_ORGANIZATION = "organization";
   private static final Set<String> QUALIFIERS_FILTER = newHashSet(Qualifiers.PROJECT);
   private static final Set<String> POSSIBLE_FIELDS = newHashSet("uuid", "key", "name", "creationDate");
 
+  private final ProjectsWsSupport support;
   private final DbClient dbClient;
   private final UserSession userSession;
   private final DefaultOrganizationProvider defaultOrganizationProvider;
 
-  public ProvisionedAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) {
+  public ProvisionedAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) {
+    this.support = support;
     this.dbClient = dbClient;
     this.userSession = userSession;
     this.defaultOrganizationProvider = defaultOrganizationProvider;
@@ -73,11 +74,7 @@ public class ProvisionedAction implements ProjectsWsAction {
       .addSearchQuery("sonar", "names", "keys")
       .addFieldsParam(POSSIBLE_FIELDS);
 
-    action.createParam(PARAM_ORGANIZATION)
-      .setDescription("The key of the organization")
-      .setRequired(false)
-      .setInternal(true)
-      .setSince("6.3");
+    support.addOrganizationParam(action);
   }
 
   @Override
@@ -91,7 +88,8 @@ public class ProvisionedAction implements ProjectsWsAction {
     String query = request.param(Param.TEXT_QUERY);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      OrganizationDto organization = getOrganization(dbSession, request);
+      OrganizationDto organization = support.getOrganization(dbSession,
+        request.getParam(PARAM_ORGANIZATION).or(defaultOrganizationProvider.get()::getKey));
       userSession.checkOrganizationPermission(organization.getUuid(), GlobalPermissions.PROVISIONING);
 
       RowBounds rowBounds = new RowBounds(options.getOffset(), options.getLimit());
@@ -104,14 +102,6 @@ public class ProvisionedAction implements ProjectsWsAction {
     }
   }
 
-  private OrganizationDto getOrganization(DbSession dbSession, Request request) {
-    String organizationKey = request.getParam(PARAM_ORGANIZATION)
-      .or(defaultOrganizationProvider.get()::getKey);
-    return checkFoundWithOptional(
-      dbClient.organizationDao().selectByKey(dbSession, organizationKey),
-      "No organization for key '%s'", organizationKey);
-  }
-
   private static void writeProjects(List<ComponentDto> projects, JsonWriter json, Set<String> desiredFields) {
     json.name("projects");
     json.beginArray();
index 43840703549cb37fce26f9df871ad2c56cdd31a2..7b81da97a0d22c0b3b392448c40dd971b32ff4b2 100644 (file)
@@ -34,7 +34,6 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
-import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.component.SnapshotTesting;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.exceptions.ForbiddenException;
@@ -254,14 +253,4 @@ public class GhostsActionTest {
     return project;
   }
 
-  private void insertNewActiveProject(String id) {
-    ComponentDto project = ComponentTesting
-      .newProjectDto(db.organizations().insert(), "analyzed-uuid-" + id)
-      .setName("analyzed-name-" + id)
-      .setKey("analyzed-key-" + id);
-    dbClient.componentDao().insert(db.getSession(), project);
-    SnapshotDto snapshot = SnapshotTesting.newAnalysis(project);
-    dbClient.snapshotDao().insert(db.getSession(), snapshot);
-    db.getSession().commit();
-  }
 }
index 87215ece5d202d6ddb6a9ff723f64aae6de4de77..e3029b650e62199412f09d43efeef7c6ad9f7c70 100644 (file)
@@ -23,12 +23,13 @@ import org.junit.Test;
 import org.sonar.core.platform.ComponentContainer;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER;
 
 public class ProjectsWsModuleTest {
   @Test
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new ProjectsWsModule().configure(container);
-    assertThat(container.size()).isEqualTo(2 + 9);
+    assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 10);
   }
 }
index ffff04a81e616eca120e7aef6147852e192302ba..fd6a5925c6239ab80386cb1f96aa6734752ad981 100644 (file)
@@ -59,7 +59,7 @@ public class ProvisionedActionTest {
 
   private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
   private DbClient dbClient = db.getDbClient();
-  private WsActionTester underTest = new WsActionTester(new ProvisionedAction(dbClient, userSessionRule, defaultOrganizationProvider));
+  private WsActionTester underTest = new WsActionTester(new ProvisionedAction(new ProjectsWsSupport(dbClient), dbClient, userSessionRule, defaultOrganizationProvider));
 
   @Test
   public void verify_definition() {