]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9181 ProjectWsSupport has dependency on DefaultOrganizationProvider
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 5 Sep 2017 16:17:37 +0000 (18:17 +0200)
committerStas Vilchik <stas.vilchik@sonarsource.com>
Mon, 11 Sep 2017 09:28:29 +0000 (11:28 +0200)
server/sonar-server/src/main/java/org/sonar/server/project/ws/CreateAction.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/ProvisionedAction.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchAction.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/BulkDeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java

index 76e31ce5077d574eda29af1d2e37a57f0d8b5caf..90f4ffd2e8c954bae57f3028e0f7b9e720be5f85 100644 (file)
@@ -28,13 +28,11 @@ import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.component.ComponentUpdater;
-import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.project.Visibility;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.WsProjects.CreateWsResponse;
 import org.sonarqube.ws.client.project.CreateRequest;
 
-import static java.util.Optional.ofNullable;
 import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS;
 import static org.sonar.server.component.NewComponent.newComponentBuilder;
@@ -55,15 +53,12 @@ public class CreateAction implements ProjectsWsAction {
   private final DbClient dbClient;
   private final UserSession userSession;
   private final ComponentUpdater componentUpdater;
-  private final DefaultOrganizationProvider defaultOrganizationProvider;
 
-  public CreateAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater,
-    DefaultOrganizationProvider defaultOrganizationProvider) {
+  public CreateAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater) {
     this.support = support;
     this.dbClient = dbClient;
     this.userSession = userSession;
     this.componentUpdater = componentUpdater;
-    this.defaultOrganizationProvider = defaultOrganizationProvider;
   }
 
   @Override
@@ -114,8 +109,7 @@ public class CreateAction implements ProjectsWsAction {
 
   private CreateWsResponse doHandle(CreateRequest request) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      OrganizationDto organization = support.getOrganization(dbSession, ofNullable(request.getOrganization())
-        .orElseGet(defaultOrganizationProvider.get()::getKey));
+      OrganizationDto organization = support.getOrganization(dbSession, request.getOrganization());
       userSession.checkPermission(PROVISION_PROJECTS, organization);
       String visibility = request.getVisibility();
       Boolean changeToPrivate = visibility == null ? dbClient.organizationDao().getNewProjectPrivate(dbSession, organization) : "private".equals(visibility);
index 3a044b3d43edf0798eea5adb8bf5dac99fe9391c..5a3debb07b6b69ff265e48d30630a04ae6aca8c8 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.project.ws;
 
+import javax.annotation.Nullable;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
@@ -26,6 +27,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.organization.BillingValidations;
 import org.sonar.server.organization.BillingValidationsProxy;
+import org.sonar.server.organization.DefaultOrganizationProvider;
 
 import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
 
@@ -34,11 +36,12 @@ public class ProjectsWsSupport {
   public static final String PARAM_ORGANIZATION = "organization";
 
   private final DbClient dbClient;
-
+  private final DefaultOrganizationProvider organizationProvider;
   private final BillingValidationsProxy billingValidations;
 
-  public ProjectsWsSupport(DbClient dbClient, BillingValidationsProxy billingValidations) {
+  public ProjectsWsSupport(DbClient dbClient, DefaultOrganizationProvider organizationProvider, BillingValidationsProxy billingValidations) {
     this.dbClient = dbClient;
+    this.organizationProvider = organizationProvider;
     this.billingValidations = billingValidations;
   }
 
@@ -50,10 +53,11 @@ public class ProjectsWsSupport {
       .setSince("6.3");
   }
 
-  OrganizationDto getOrganization(DbSession dbSession, String organizationKey) {
+  OrganizationDto getOrganization(DbSession dbSession, @Nullable String organizationKeyParam) {
+    String organizationKey = organizationKeyParam == null ? organizationProvider.get().getKey() : organizationKeyParam;
     return checkFoundWithOptional(
-        dbClient.organizationDao().selectByKey(dbSession, organizationKey),
-        "No organization for key '%s'", organizationKey);
+      dbClient.organizationDao().selectByKey(dbSession, organizationKey),
+      "No organization for key '%s'", organizationKey);
   }
 
   void checkCanUpdateProjectsVisibility(OrganizationDto organization, boolean newProjectsPrivate) {
index f4fafb04bc1b9a5c323c9803e62c31439a76e727..a19e83f044ffb43850f3c537bd80d1d3b09be55d 100644 (file)
@@ -37,7 +37,6 @@ import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentQuery;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.Common.Paging;
 import org.sonarqube.ws.WsComponents.ProvisionedWsResponse;
@@ -57,19 +56,16 @@ import static org.sonar.server.ws.WsUtils.writeProtobuf;
 
 public class ProvisionedAction implements ProjectsWsAction {
 
-  private static final Set<String> QUALIFIERS_FILTER = newHashSet(Qualifiers.PROJECT);
   private static final Set<String> POSSIBLE_FIELDS = newHashSet("uuid", "key", "name", "creationDate", "visibility");
 
   private final ProjectsWsSupport support;
   private final DbClient dbClient;
   private final UserSession userSession;
-  private final DefaultOrganizationProvider defaultOrganizationProvider;
 
-  public ProvisionedAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) {
+  public ProvisionedAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession) {
     this.support = support;
     this.dbClient = dbClient;
     this.userSession = userSession;
-    this.defaultOrganizationProvider = defaultOrganizationProvider;
   }
 
   @Override
@@ -105,8 +101,7 @@ public class ProvisionedAction implements ProjectsWsAction {
     String query = request.param(Param.TEXT_QUERY);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      OrganizationDto organization = support.getOrganization(dbSession,
-        request.getParam(PARAM_ORGANIZATION).or(defaultOrganizationProvider.get()::getKey));
+      OrganizationDto organization = support.getOrganization(dbSession, request.param(PARAM_ORGANIZATION));
       userSession.checkPermission(PROVISION_PROJECTS, organization);
 
       ComponentQuery dbQuery = buildDbQuery(query);
index ecbf987db9e6602bde8105a6f269a274dda68e8a..7cfd3cb87eeb7e59f6fec8498009466f0df451f2 100644 (file)
@@ -37,14 +37,12 @@ import org.sonar.db.component.ComponentQuery;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.OrganizationPermission;
-import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.project.Visibility;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.WsProjects.SearchWsResponse;
 import org.sonarqube.ws.client.project.SearchWsRequest;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Optional.ofNullable;
 import static org.sonar.api.resources.Qualifiers.APP;
 import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.api.resources.Qualifiers.VIEW;
@@ -74,13 +72,11 @@ public class SearchAction implements ProjectsWsAction {
 
   private final DbClient dbClient;
   private final UserSession userSession;
-  private final DefaultOrganizationProvider defaultOrganizationProvider;
   private final ProjectsWsSupport support;
 
-  public SearchAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider, ProjectsWsSupport support) {
+  public SearchAction(DbClient dbClient, UserSession userSession, ProjectsWsSupport support) {
     this.dbClient = dbClient;
     this.userSession = userSession;
-    this.defaultOrganizationProvider = defaultOrganizationProvider;
     this.support = support;
   }
 
@@ -167,7 +163,7 @@ public class SearchAction implements ProjectsWsAction {
 
   private SearchWsResponse doHandle(SearchWsRequest request) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      OrganizationDto organization = support.getOrganization(dbSession, ofNullable(request.getOrganization()).orElseGet(defaultOrganizationProvider.get()::getKey));
+      OrganizationDto organization = support.getOrganization(dbSession, request.getOrganization());
       userSession.checkPermission(OrganizationPermission.ADMINISTER, organization);
 
       ComponentQuery query = buildDbQuery(request);
index 0ca7c7f2886d533a65601530c2697aaaadf73adf..5982f45f5b0560ed1034c000be6cd8b277cd53a5 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.server.component.ComponentCleanerService;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.organization.BillingValidationsProxy;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
 
@@ -61,7 +62,8 @@ public class BulkDeleteActionTest {
   private ComponentCleanerService componentCleanerService = mock(ComponentCleanerService.class);
   private WsTester ws;
   private DbClient dbClient = db.getDbClient();
-  private BulkDeleteAction underTest = new BulkDeleteAction(componentCleanerService, dbClient, userSession, new ProjectsWsSupport(dbClient, mock(BillingValidationsProxy.class)));
+  private ProjectsWsSupport support = new ProjectsWsSupport(dbClient, TestDefaultOrganizationProvider.from(db), mock(BillingValidationsProxy.class));
+  private BulkDeleteAction underTest = new BulkDeleteAction(componentCleanerService, dbClient, userSession, support);
   private OrganizationDto org1;
   private OrganizationDto org2;
 
index c9d7e3013e4609b1edd40dc0bb0914ab9c957ff9..d8e17c542f8719d58eb30a7a3990a2fde16c81d4 100644 (file)
@@ -85,11 +85,11 @@ public class CreateActionTest {
   private TestProjectIndexers projectIndexers = new TestProjectIndexers();
   private WsActionTester ws = new WsActionTester(
     new CreateAction(
-      new ProjectsWsSupport(db.getDbClient(), billingValidations),
+      new ProjectsWsSupport(db.getDbClient(), defaultOrganizationProvider, billingValidations),
       db.getDbClient(), userSession,
       new ComponentUpdater(db.getDbClient(), i18n, system2, mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()),
-        projectIndexers),
-      defaultOrganizationProvider));
+        projectIndexers)
+    ));
 
   @Test
   public void create_project() throws Exception {
index 218e0912cd8fda236528b4f1c9f4259ac8a7d1c1..269a85ba0687f9dcce09f7016196879b16ac117e 100644 (file)
@@ -66,7 +66,7 @@ public class ProvisionedActionTest {
   private DbClient dbClient = db.getDbClient();
 
   private WsActionTester ws = new WsActionTester(
-    new ProvisionedAction(new ProjectsWsSupport(dbClient, mock(BillingValidationsProxy.class)), dbClient, userSessionRule, defaultOrganizationProvider));
+    new ProvisionedAction(new ProjectsWsSupport(dbClient, defaultOrganizationProvider, mock(BillingValidationsProxy.class)), dbClient, userSessionRule));
 
   @Test
   public void definition() {
index 40cf88ca7c1d88f4a52b1dc86c34a2cba07fa61f..799bd1ef68afdf088e28dc2150467da5d9856eb8 100644 (file)
@@ -94,7 +94,7 @@ public class SearchActionTest {
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
 
   private WsActionTester ws = new WsActionTester(
-    new SearchAction(db.getDbClient(), userSession, defaultOrganizationProvider, new ProjectsWsSupport(db.getDbClient(), mock(BillingValidationsProxy.class))));
+    new SearchAction(db.getDbClient(), userSession, new ProjectsWsSupport(db.getDbClient(), defaultOrganizationProvider, mock(BillingValidationsProxy.class))));
 
   @Test
   public void search_by_key_query_with_partial_match_case_insensitive() throws IOException {
index 3621957098bce9c850835c79e4bf204ef3cdfbfa..da79a2b05e1e3d89fd5f1fa8c72f4fa5d4ac679e 100644 (file)
@@ -53,6 +53,7 @@ import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.organization.BillingValidations;
 import org.sonar.server.organization.BillingValidationsProxy;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.permission.index.FooIndexDefinition;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
@@ -94,7 +95,7 @@ public class UpdateVisibilityActionTest {
   private TestProjectIndexers projectIndexers = new TestProjectIndexers();
   private BillingValidationsProxy billingValidations = mock(BillingValidationsProxy.class);
 
-  private ProjectsWsSupport wsSupport = new ProjectsWsSupport(dbClient, billingValidations);
+  private ProjectsWsSupport wsSupport = new ProjectsWsSupport(dbClient, TestDefaultOrganizationProvider.from(dbTester), billingValidations);
   private UpdateVisibilityAction underTest = new UpdateVisibilityAction(dbClient, TestComponentFinder.from(dbTester), userSessionRule, projectIndexers, wsSupport);
   private WsActionTester actionTester = new WsActionTester(underTest);