]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8761 add class OrganizationFlags
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 10 Feb 2017 14:21:47 +0000 (15:21 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 10 Feb 2017 22:07:21 +0000 (23:07 +0100)
15 files changed:
server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationFlags.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationFlagsImpl.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/organization/ws/CreateAction.java
server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java
server/sonar-server/src/main/java/org/sonar/server/organization/ws/EnableSupportAction.java
server/sonar-server/src/main/java/org/sonar/server/organization/ws/OrganizationsWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/organization/ws/UpdateAction.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel3.java
server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationFlagsImplTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/organization/TestOrganizationFlags.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/EnableSupportActionTest.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/UpdateActionTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationFlags.java b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationFlags.java
new file mode 100644 (file)
index 0000000..087780b
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.organization;
+
+import org.sonar.db.DbSession;
+
+public interface OrganizationFlags {
+
+  boolean isEnabled(DbSession dbSession);
+
+  /**
+   * Ensures that {@link #isEnabled(DbSession)} is {@code true},
+   * otherwise throws {@link IllegalStateException}
+   */
+  void checkEnabled(DbSession dbSession);
+
+  void enable(DbSession dbSession);
+
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationFlagsImpl.java b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationFlagsImpl.java
new file mode 100644 (file)
index 0000000..d4f6540
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.organization;
+
+import java.util.Optional;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.server.property.InternalProperties;
+
+import static java.lang.String.valueOf;
+
+public class OrganizationFlagsImpl implements OrganizationFlags {
+
+  public static final String FAILURE_MESSAGE = "Organization support is disabled";
+
+  private final DbClient dbClient;
+
+  public OrganizationFlagsImpl(DbClient dbClient) {
+    this.dbClient = dbClient;
+  }
+
+  @Override
+  public boolean isEnabled(DbSession dbSession) {
+    Optional<String> value = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.ORGANIZATION_ENABLED);
+    return value.map(s -> "true".equals(s)).orElse(false);
+  }
+
+  @Override
+  public void checkEnabled(DbSession dbSession) {
+    if (!isEnabled(dbSession)) {
+      throw new IllegalStateException(FAILURE_MESSAGE);
+    }
+  }
+
+  @Override
+  public void enable(DbSession dbSession) {
+    dbClient.internalPropertiesDao().save(dbSession, InternalProperties.ORGANIZATION_ENABLED, valueOf(true));
+  }
+}
index 5fb1b26d71c33c3f3389dbfd1a0cbbda887de849..de22c988c72d486777ea73dbcef88cf911a898b0 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.organization.OrganizationCreation;
+import org.sonar.server.organization.OrganizationFlags;
 import org.sonar.server.organization.OrganizationValidation;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.Organizations.CreateWsResponse;
@@ -48,15 +49,17 @@ public class CreateAction implements OrganizationsAction {
   private final OrganizationsWsSupport wsSupport;
   private final OrganizationValidation organizationValidation;
   private final OrganizationCreation organizationCreation;
+  private final OrganizationFlags organizationFlags;
 
   public CreateAction(Settings settings, UserSession userSession, DbClient dbClient, OrganizationsWsSupport wsSupport,
-    OrganizationValidation organizationValidation, OrganizationCreation organizationCreation) {
+    OrganizationValidation organizationValidation, OrganizationCreation organizationCreation, OrganizationFlags organizationFlags) {
     this.settings = settings;
     this.userSession = userSession;
     this.dbClient = dbClient;
     this.wsSupport = wsSupport;
     this.organizationValidation = organizationValidation;
     this.organizationCreation = organizationCreation;
+    this.organizationFlags = organizationFlags;
   }
 
   @Override
@@ -64,7 +67,7 @@ public class CreateAction implements OrganizationsAction {
     WebService.NewAction action = context.createAction(ACTION)
       .setPost(true)
       .setDescription("Create an organization.<br />" +
-        "Requires 'Administer System' permission unless any logged in user is allowed to create an organization (see appropriate setting). Organization feature must be enabled.")
+        "Requires 'Administer System' permission unless any logged in user is allowed to create an organization (see appropriate setting). Organization support must be enabled.")
       .setResponseExample(getClass().getResource("example-create.json"))
       .setInternal(true)
       .setSince("6.2")
@@ -97,7 +100,7 @@ public class CreateAction implements OrganizationsAction {
     String avatar = wsSupport.getAndCheckAvatar(request);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      wsSupport.checkFeatureEnabled(dbSession);
+      organizationFlags.checkEnabled(dbSession);
       OrganizationDto organization = organizationCreation.create(
         dbSession,
         userSession.getUserId().longValue(),
index f6eb82c613f320456714daaf3aabf04d21c73081..81713602ffd3b9384803a694fe9b1393c44d74d7 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.component.ComponentCleanerService;
 import org.sonar.server.organization.DefaultOrganization;
 import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.OrganizationFlags;
 import org.sonar.server.user.UserSession;
 
 import static com.google.common.base.Preconditions.checkArgument;
@@ -44,15 +45,15 @@ public class DeleteAction implements OrganizationsAction {
   private final DbClient dbClient;
   private final DefaultOrganizationProvider defaultOrganizationProvider;
   private final ComponentCleanerService componentCleanerService;
-  private final OrganizationsWsSupport support;
+  private final OrganizationFlags organizationFlags;
 
   public DeleteAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider,
-    ComponentCleanerService componentCleanerService, OrganizationsWsSupport support) {
+    ComponentCleanerService componentCleanerService, OrganizationFlags organizationFlags) {
     this.userSession = userSession;
     this.dbClient = dbClient;
     this.defaultOrganizationProvider = defaultOrganizationProvider;
     this.componentCleanerService = componentCleanerService;
-    this.support = support;
+    this.organizationFlags = organizationFlags;
   }
 
   @Override
@@ -60,7 +61,7 @@ public class DeleteAction implements OrganizationsAction {
     WebService.NewAction action = context.createAction(ACTION)
       .setPost(true)
       .setDescription("Delete an organization.<br/>" +
-        "Require 'Administer System' permission on the specified organization. Organization feature must be enabled.")
+        "Require 'Administer System' permission on the specified organization. Organization support must be enabled.")
       .setInternal(true)
       .setSince("6.2")
       .setHandler(this);
@@ -76,7 +77,7 @@ public class DeleteAction implements OrganizationsAction {
     userSession.checkLoggedIn();
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      support.checkFeatureEnabled(dbSession);
+      organizationFlags.checkEnabled(dbSession);
 
       String key = request.mandatoryParam(PARAM_KEY);
       preventDeletionOfDefaultOrganization(key, defaultOrganizationProvider.get());
index 61759ee382fa01fd0d4495fcd60cd8f6ebea57cb..dc7f198e01db7650fe666bafc135cd120e6268c0 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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
@@ -26,10 +26,9 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.organization.DefaultOrganizationProvider;
-import org.sonar.server.property.InternalProperties;
+import org.sonar.server.organization.OrganizationFlags;
 import org.sonar.server.user.UserSession;
 
-import static java.lang.String.valueOf;
 import static java.util.Objects.requireNonNull;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 
@@ -39,13 +38,14 @@ public class EnableSupportAction implements OrganizationsAction {
   private final UserSession userSession;
   private final DbClient dbClient;
   private final DefaultOrganizationProvider defaultOrganizationProvider;
-  private final OrganizationsWsSupport support;
+  private final OrganizationFlags organizationFlags;
 
-  public EnableSupportAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider, OrganizationsWsSupport support) {
+  public EnableSupportAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider,
+    OrganizationFlags organizationFlags) {
     this.userSession = userSession;
     this.dbClient = dbClient;
     this.defaultOrganizationProvider = defaultOrganizationProvider;
-    this.support = support;
+    this.organizationFlags = organizationFlags;
   }
 
   @Override
@@ -62,9 +62,8 @@ public class EnableSupportAction implements OrganizationsAction {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    verifySystemAdministrator();
-
     try (DbSession dbSession = dbClient.openSession(false)) {
+      verifySystemAdministrator();
       verifyFeatureIsDisabled(dbSession);
       flagCurrentUserAsRoot(dbSession);
       enableFeature(dbSession);
@@ -78,7 +77,7 @@ public class EnableSupportAction implements OrganizationsAction {
   }
 
   private void verifyFeatureIsDisabled(DbSession dbSession) {
-    if (support.isFeatureEnabled(dbSession)) {
+    if (organizationFlags.isEnabled(dbSession)) {
       throw new BadRequestException("Organizations are already enabled");
     }
   }
@@ -88,7 +87,7 @@ public class EnableSupportAction implements OrganizationsAction {
   }
 
   private void enableFeature(DbSession dbSession) {
-    dbClient.internalPropertiesDao().save(dbSession, InternalProperties.ORGANIZATION_ENABLED, valueOf(true));
+    organizationFlags.enable(dbSession);
   }
 
 }
index 38a9fe68117a9f5fabc5301c3488994d85638416..42019093cd604a752ec677d10f1db62a67294c84 100644 (file)
  */
 package org.sonar.server.organization.ws;
 
-import java.util.Optional;
 import javax.annotation.CheckForNull;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.organization.OrganizationValidation;
-import org.sonar.server.property.InternalProperties;
 import org.sonarqube.ws.Organizations;
 
 import static org.sonar.core.util.Protobuf.setNullable;
@@ -44,11 +39,9 @@ public class OrganizationsWsSupport {
   static final String PARAM_AVATAR_URL = "avatar";
 
   private final OrganizationValidation organizationValidation;
-  private final DbClient dbClient;
 
-  public OrganizationsWsSupport(OrganizationValidation organizationValidation, DbClient dbClient) {
+  public OrganizationsWsSupport(OrganizationValidation organizationValidation) {
     this.organizationValidation = organizationValidation;
-    this.dbClient = dbClient;
   }
 
   String getAndCheckMandatoryName(Request request) {
@@ -119,18 +112,4 @@ public class OrganizationsWsSupport {
     setNullable(dto.getAvatarUrl(), builder::setAvatar);
     return builder.build();
   }
-
-  boolean isFeatureEnabled(DbSession dbSession) {
-    Optional<String> value = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.ORGANIZATION_ENABLED);
-    return value.isPresent() && Boolean.parseBoolean(value.get());
-  }
-
-  /**
-   * Ensures that the organization feature is enabled, otherwise throws {@link BadRequestException}
-   */
-  void checkFeatureEnabled(DbSession dbSession) {
-    if (!isFeatureEnabled(dbSession)) {
-      throw new BadRequestException("Organizations feature is not enabled");
-    }
-  }
 }
index 3fb4c4237a2213d35fce07b0dc9909092bddd3c0..ac51fe383fcb7b9600ca991860eae7a875b13bc3 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.organization.OrganizationFlags;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.Organizations;
 
@@ -47,11 +48,14 @@ public class UpdateAction implements OrganizationsAction {
   private final UserSession userSession;
   private final OrganizationsWsSupport wsSupport;
   private final DbClient dbClient;
+  private final OrganizationFlags organizationFlags;
 
-  public UpdateAction(UserSession userSession, OrganizationsWsSupport wsSupport, DbClient dbClient) {
+  public UpdateAction(UserSession userSession, OrganizationsWsSupport wsSupport, DbClient dbClient,
+    OrganizationFlags organizationFlags) {
     this.userSession = userSession;
     this.wsSupport = wsSupport;
     this.dbClient = dbClient;
+    this.organizationFlags = organizationFlags;
   }
 
   @Override
@@ -59,7 +63,7 @@ public class UpdateAction implements OrganizationsAction {
     WebService.NewAction action = context.createAction(ACTION)
       .setPost(true)
       .setDescription("Update an organization.<br/>" +
-        "Require 'Administer System' permission. Organization feature must be enabled.")
+        "Require 'Administer System' permission. Organization support must be enabled.")
       .setInternal(true)
       .setSince("6.2")
       .setHandler(this);
@@ -77,7 +81,7 @@ public class UpdateAction implements OrganizationsAction {
     userSession.checkLoggedIn();
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      wsSupport.checkFeatureEnabled(dbSession);
+      organizationFlags.checkEnabled(dbSession);
 
       String key = request.mandatoryParam(PARAM_KEY);
 
index e161fea3447d6919f3322654e219e29a1d2a09f3..a275fad23b5ed329ac8a5e954d4dde7b7bcf3531 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.platform.platformlevel;
 import org.sonar.api.utils.UriReader;
 import org.sonar.core.util.DefaultHttpDownloader;
 import org.sonar.server.organization.DefaultOrganizationProviderImpl;
+import org.sonar.server.organization.OrganizationFlagsImpl;
 import org.sonar.server.platform.ServerIdGenerator;
 import org.sonar.server.platform.ServerIdLoader;
 import org.sonar.server.platform.ServerIdManager;
@@ -51,6 +52,7 @@ public class PlatformLevel3 extends PlatformLevel {
       ServerIdGenerator.class,
       LogServerId.class,
       DefaultHttpDownloader.class,
-      DefaultOrganizationProviderImpl.class);
+      DefaultOrganizationProviderImpl.class,
+      OrganizationFlagsImpl.class);
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationFlagsImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/OrganizationFlagsImplTest.java
new file mode 100644 (file)
index 0000000..4ec4454
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.organization;
+
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.DbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class OrganizationFlagsImplTest {
+
+  @Rule
+  public DbTester db = DbTester.create();
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private OrganizationFlagsImpl underTest = new OrganizationFlagsImpl(db.getDbClient());
+
+  @Test
+  public void isEnabled_returns_false_by_default() {
+    assertThat(underTest.isEnabled(db.getSession())).isFalse();
+    verifyInternalProperty(null);
+  }
+
+  @Test
+  public void enable_does_enable_feature_by_inserting_internal_property() {
+    underTest.enable(db.getSession());
+
+    assertThat(underTest.isEnabled(db.getSession())).isTrue();
+    verifyInternalProperty("true");
+  }
+
+  @Test
+  public void checkEnabled_throws_IllegalStateException_if_feature_is_disabled() {
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Organization support is disabled");
+
+    underTest.checkEnabled(db.getSession());
+  }
+
+  @Test
+  public void checkEnabled_succeeds_if_feature_is_enabled() {
+    underTest.enable(db.getSession());
+
+    underTest.checkEnabled(db.getSession());
+  }
+
+  private void verifyInternalProperty(@Nullable String expectedValue) {
+    db.properties().verifyInternal("organization.enabled", expectedValue);
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/TestOrganizationFlags.java b/server/sonar-server/src/test/java/org/sonar/server/organization/TestOrganizationFlags.java
new file mode 100644 (file)
index 0000000..5ebadac
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.organization;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.sonar.db.DbSession;
+
+import static org.sonar.server.organization.OrganizationFlagsImpl.FAILURE_MESSAGE;
+
+public class TestOrganizationFlags implements OrganizationFlags {
+
+  private final AtomicBoolean enabled = new AtomicBoolean(false);
+
+  private TestOrganizationFlags() {
+  }
+
+  @Override
+  public boolean isEnabled(DbSession dbSession) {
+    return enabled.get();
+  }
+
+  public TestOrganizationFlags setEnabled(boolean b) {
+    this.enabled.set(b);
+    return this;
+  }
+
+  @Override
+  public void enable(DbSession dbSession) {
+    setEnabled(true);
+  }
+
+  @Override
+  public void checkEnabled(DbSession dbSession) {
+    if (!isEnabled(dbSession)) {
+      throw new IllegalStateException(FAILURE_MESSAGE);
+    }
+  }
+
+  /**
+   * By default Organization support is disabled
+   */
+  public static TestOrganizationFlags standalone() {
+    return new TestOrganizationFlags();
+  }
+}
index be2136eaf550cafd0dc99108f53896bc50e8a431..0c0f123183d2a6d2b0d121aba702d44fe2e3d6ff 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.config.MapSettings;
 import org.sonar.api.config.Settings;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
+import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.util.UuidFactory;
@@ -47,13 +48,13 @@ import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.db.user.UserMembershipDto;
 import org.sonar.db.user.UserMembershipQuery;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.organization.OrganizationCreation;
 import org.sonar.server.organization.OrganizationCreationImpl;
 import org.sonar.server.organization.OrganizationValidation;
 import org.sonar.server.organization.OrganizationValidationImpl;
+import org.sonar.server.organization.TestOrganizationFlags;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.WsActionTester;
@@ -90,7 +91,8 @@ public class CreateActionTest {
   private UuidFactory uuidFactory = mock(UuidFactory.class);
   private OrganizationValidation organizationValidation = new OrganizationValidationImpl();
   private OrganizationCreation organizationCreation = new OrganizationCreationImpl(dbClient, system2, uuidFactory, organizationValidation, settings);
-  private CreateAction underTest = new CreateAction(settings, userSession, dbClient, new OrganizationsWsSupport(organizationValidation, dbClient), organizationValidation, organizationCreation);
+  private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone().setEnabled(true);
+  private CreateAction underTest = new CreateAction(settings, userSession, dbClient, new OrganizationsWsSupport(organizationValidation), organizationValidation, organizationCreation, organizationFlags);
   private WsActionTester wsTester = new WsActionTester(underTest);
 
   @Test
@@ -99,7 +101,7 @@ public class CreateActionTest {
     assertThat(action.key()).isEqualTo("create");
     assertThat(action.isPost()).isTrue();
     assertThat(action.description()).isEqualTo("Create an organization.<br />" +
-      "Requires 'Administer System' permission unless any logged in user is allowed to create an organization (see appropriate setting). Organization feature must be enabled.");
+      "Requires 'Administer System' permission unless any logged in user is allowed to create an organization (see appropriate setting). Organization support must be enabled.");
     assertThat(action.isInternal()).isTrue();
     assertThat(action.since()).isEqualTo("6.2");
     assertThat(action.handler()).isEqualTo(underTest);
@@ -129,7 +131,7 @@ public class CreateActionTest {
 
   @Test
   public void verify_response_example() throws URISyntaxException, IOException {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(Uuids.UUID_EXAMPLE_01, SOME_DATE);
 
     String response = executeJsonRequest("Foo Company", "foo-company", "The Foo company produces quality software for Bar.", "https://www.foo.com", "https://www.foo.com/foo.png");
@@ -139,7 +141,6 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_user_is_not_logged_in_and_logged_in_users_cannot_create_organizations() {
-    enableOrganizations();
     userSession.anonymous();
 
     expectedException.expect(ForbiddenException.class);
@@ -150,7 +151,6 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_user_is_not_logged_in_and_logged_in_users_can_create_organizations() {
-    enableOrganizations();
     userSession.anonymous();
     settings.setProperty(ORGANIZATIONS_ANYONE_CAN_CREATE, true);
 
@@ -162,7 +162,6 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_user_is_not_root_and_logged_in_users_cannot_create_organizations() {
-    enableOrganizations();
     userSession.logIn();
 
     expectedException.expect(ForbiddenException.class);
@@ -173,7 +172,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_user_is_root_and_logged_in_users_cannot_create_organizations() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     verifyResponseAndDb(executeRequest("foo"), SOME_UUID, "foo", "foo", SOME_DATE);
@@ -181,7 +180,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_user_is_root_and_logged_in_users_can_create_organizations() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     settings.setProperty(ORGANIZATIONS_ANYONE_CAN_CREATE, true);
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
@@ -190,7 +189,6 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_user_is_not_root_and_logged_in_users_can_create_organizations() {
-    enableOrganizations();
     userSession.logIn();
     settings.setProperty(ORGANIZATIONS_ANYONE_CAN_CREATE, true);
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
@@ -200,7 +198,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_name_param_is_missing() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("The 'name' parameter is missing");
@@ -210,7 +208,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_name_is_one_char_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Name 'a' must be at least 2 chars long");
@@ -220,7 +218,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_name_is_two_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     verifyResponseAndDb(executeRequest("ab"), SOME_UUID, "ab", "ab", SOME_DATE);
@@ -228,7 +226,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_name_is_65_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Name '" + STRING_65_CHARS_LONG + "' must be at most 64 chars long");
@@ -238,7 +236,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_name_is_64_char_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     String name = STRING_65_CHARS_LONG.substring(0, 64);
@@ -248,7 +246,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_key_one_char_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Key 'a' must be at least 2 chars long");
@@ -258,7 +256,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_key_is_33_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     String key = STRING_65_CHARS_LONG.substring(0, 33);
 
@@ -270,7 +268,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_key_is_2_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     verifyResponseAndDb(executeRequest("foo", "ab"), SOME_UUID, "foo", "ab", SOME_DATE);
@@ -278,7 +276,7 @@ public class CreateActionTest {
 
   @Test
   public void requests_succeeds_if_key_is_32_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     String key = STRING_65_CHARS_LONG.substring(0, 32);
@@ -288,28 +286,28 @@ public class CreateActionTest {
 
   @Test
   public void requests_fails_if_key_contains_non_ascii_chars_but_dash() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     requestFailsWithInvalidCharInKey("ab@");
   }
 
   @Test
   public void request_fails_if_key_starts_with_a_dash() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     requestFailsWithInvalidCharInKey("-ab");
   }
 
   @Test
   public void request_fails_if_key_ends_with_a_dash() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     requestFailsWithInvalidCharInKey("ab-");
   }
 
   @Test
   public void request_fails_if_key_contains_space() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     requestFailsWithInvalidCharInKey("a b");
   }
@@ -323,7 +321,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_key_is_specified_and_already_exists_in_DB() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto org = insertOrganization("the-key");
 
     expectedException.expect(IllegalArgumentException.class);
@@ -334,7 +332,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_key_computed_from_name_already_exists_in_DB() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     String key = STRING_65_CHARS_LONG.substring(0, 32);
     insertOrganization(key);
 
@@ -348,7 +346,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_description_url_and_avatar_are_not_specified() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     CreateWsResponse response = executeRequest("foo", "bar", null, null, null);
@@ -357,7 +355,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_description_url_and_avatar_are_specified() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     CreateWsResponse response = executeRequest("foo", "bar", "moo", "doo", "boo");
@@ -366,7 +364,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_to_generate_key_from_name_more_then_32_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     String name = STRING_65_CHARS_LONG.substring(0, 33);
@@ -377,7 +375,7 @@ public class CreateActionTest {
 
   @Test
   public void request_generates_key_ignoring_multiple_following_spaces() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
 
     String name = "ab   cd";
@@ -388,7 +386,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_description_is_257_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Description '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long");
@@ -398,7 +396,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_description_is_256_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
     String description = STRING_257_CHARS_LONG.substring(0, 256);
 
@@ -408,7 +406,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_url_is_257_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Url '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long");
@@ -418,7 +416,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_url_is_256_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
     String url = STRING_257_CHARS_LONG.substring(0, 256);
 
@@ -428,7 +426,7 @@ public class CreateActionTest {
 
   @Test
   public void request_fails_if_avatar_is_257_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Avatar '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long");
@@ -438,7 +436,7 @@ public class CreateActionTest {
 
   @Test
   public void request_succeeds_if_avatar_is_256_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
     String avatar = STRING_257_CHARS_LONG.substring(0, 256);
 
@@ -448,7 +446,6 @@ public class CreateActionTest {
 
   @Test
   public void request_creates_owners_group_with_all_permissions_for_new_organization_and_add_current_user_to_it() {
-    enableOrganizations();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
     UserDto user = dbTester.users().makeRoot(dbTester.users().insertUser());
     userSession.logIn(user).setRoot();
@@ -473,7 +470,6 @@ public class CreateActionTest {
 
   @Test
   public void request_creates_default_template_for_owner_group_and_anyone() {
-    enableOrganizations();
     mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
     UserDto user = dbTester.users().makeRoot(dbTester.users().insertUser());
     userSession.logIn(user).setRoot();
@@ -496,11 +492,12 @@ public class CreateActionTest {
   }
 
   @Test
-  public void request_fails_with_BadRequestException_if_organization_feature_is_disabled() {
+  public void request_fails_with_IllegalStateException_if_organization_feature_is_disabled() {
+    organizationFlags.setEnabled(false);
     logInAsRoot();
 
-    expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("Organizations feature is not enabled");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Organization support is disabled");
 
     executeJsonRequest("Foo Company", "foo-company", "The Foo company produces quality software for Bar.", "https://www.foo.com", "https://www.foo.com/foo.png");
   }
@@ -597,16 +594,7 @@ public class CreateActionTest {
     return dto;
   }
 
-  private void enableOrganizations() {
-    dbTester.enableOrganizations();
-  }
-
   private void logInAsRoot() {
     userSession.logIn().setRoot();
   }
-
-  private void enableOrganizationsAndLogInAsRoot() {
-    enableOrganizations();
-    logInAsRoot();
-  }
 }
index 83260c6eb7010ccb2391239abfc175d7f8320276..8b103429116ef93bf1232a909167ac9b91bbc0f7 100644 (file)
@@ -36,12 +36,11 @@ import org.sonar.db.permission.template.PermissionTemplateDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.component.ComponentCleanerService;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.exceptions.UnauthorizedException;
-import org.sonar.server.organization.OrganizationValidationImpl;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.organization.TestOrganizationFlags;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
 
@@ -65,8 +64,9 @@ public class DeleteActionTest {
   private DbClient dbClient = dbTester.getDbClient();
   private DbSession session = dbTester.getSession();
   private ComponentCleanerService componentCleanerService = mock(ComponentCleanerService.class);
-  private OrganizationsWsSupport support = new OrganizationsWsSupport(new OrganizationValidationImpl(), dbClient);
-  private DeleteAction underTest = new DeleteAction(userSession, dbTester.getDbClient(), TestDefaultOrganizationProvider.from(dbTester), componentCleanerService, support);
+  private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone().setEnabled(true);
+  private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
+  private DeleteAction underTest = new DeleteAction(userSession, dbTester.getDbClient(), defaultOrganizationProvider, componentCleanerService, organizationFlags);
   private WsActionTester wsTester = new WsActionTester(underTest);
 
   @Test
@@ -75,7 +75,7 @@ public class DeleteActionTest {
     assertThat(action.key()).isEqualTo("delete");
     assertThat(action.isPost()).isTrue();
     assertThat(action.description()).isEqualTo("Delete an organization.<br/>" +
-      "Require 'Administer System' permission on the specified organization. Organization feature must be enabled.");
+      "Require 'Administer System' permission on the specified organization. Organization support must be enabled.");
     assertThat(action.isInternal()).isTrue();
     assertThat(action.since()).isEqualTo("6.2");
     assertThat(action.handler()).isEqualTo(underTest);
@@ -89,19 +89,18 @@ public class DeleteActionTest {
   }
 
   @Test
-  public void request_fails_with_organization_feature_is_disabled() {
+  public void request_fails_with_IllegalStateException_if_organization_feature_is_disabled() {
+    organizationFlags.setEnabled(false);
     userSession.logIn();
 
-    expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Organization support is disabled");
 
     wsTester.newRequest().execute();
   }
 
   @Test
   public void request_fails_with_UnauthorizedException_if_user_is_not_logged_in() {
-    enableOrganizations();
-
     expectedException.expect(UnauthorizedException.class);
     expectedException.expectMessage("Authentication is required");
 
@@ -111,7 +110,7 @@ public class DeleteActionTest {
 
   @Test
   public void request_fails_with_IAE_if_key_param_is_missing() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("The 'key' parameter is missing");
@@ -121,7 +120,7 @@ public class DeleteActionTest {
 
   @Test
   public void request_fails_with_IAE_if_key_is_the_one_of_default_organization() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Default Organization can't be deleted");
@@ -131,7 +130,7 @@ public class DeleteActionTest {
 
   @Test
   public void request_fails_with_NotFoundException_if_organization_with_specified_key_does_not_exist() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(NotFoundException.class);
     expectedException.expectMessage("Organization with key 'foo' not found");
@@ -141,7 +140,6 @@ public class DeleteActionTest {
 
   @Test
   public void request_fails_with_ForbiddenException_when_user_is_not_root_and_is_not_administrator_of_specified_organization() {
-    enableOrganizations();
     OrganizationDto organization = dbTester.organizations().insert();
     userSession.logIn();
 
@@ -153,7 +151,6 @@ public class DeleteActionTest {
 
   @Test
   public void request_fails_with_ForbiddenException_when_user_is_not_root_and_is_administrator_of_other_organization() {
-    enableOrganizations();
     OrganizationDto organization = dbTester.organizations().insert();
     logInAsAdministrator(dbTester.getDefaultOrganization());
 
@@ -165,7 +162,6 @@ public class DeleteActionTest {
 
   @Test
   public void request_deletes_specified_organization_if_exists_and_user_is_administrator_of_it() {
-    enableOrganizations();
     OrganizationDto organization = dbTester.organizations().insert();
     logInAsAdministrator(organization);
 
@@ -176,7 +172,6 @@ public class DeleteActionTest {
 
   @Test
   public void request_deletes_specified_organization_if_exists_and_user_is_root() {
-    enableOrganizations();
     OrganizationDto organization = dbTester.organizations().insert();
     userSession.logIn().setRoot();
 
@@ -197,7 +192,7 @@ public class DeleteActionTest {
 
   @Test
   public void request_also_deletes_components_of_specified_organization() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     OrganizationDto organization = dbTester.organizations().insert();
     ComponentDto project = dbTester.components().insertProject(organization);
@@ -220,7 +215,7 @@ public class DeleteActionTest {
 
   @Test
   public void request_also_deletes_permissions_templates_and_permissions_and_groups_of_specified_organization() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     OrganizationDto org = dbTester.organizations().insert();
     OrganizationDto otherOrg = dbTester.organizations().insert();
@@ -286,10 +281,6 @@ public class DeleteActionTest {
       .execute();
   }
 
-  private void enableOrganizations() {
-    dbTester.enableOrganizations();
-  }
-
   private void logInAsRoot() {
     userSession.logIn().setRoot();
   }
@@ -297,9 +288,4 @@ public class DeleteActionTest {
   private void logInAsAdministrator(OrganizationDto organization) {
     userSession.logIn().addOrganizationPermission(organization.getUuid(), SYSTEM_ADMIN);
   }
-
-  private void enableOrganizationsAndLogInAsRoot() {
-    enableOrganizations();
-    logInAsRoot();
-  }
 }
index ef1d7e13127e5724b48a3f0332de56e69290be64..0f55aeef8bddfe1f1f2d7e027af1801be4b47ea3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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
@@ -31,6 +31,8 @@ import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.organization.OrganizationValidationImpl;
+import org.sonar.server.organization.OrganizationFlags;
+import org.sonar.server.organization.OrganizationFlagsImpl;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestResponse;
@@ -49,24 +51,24 @@ public class EnableSupportActionTest {
   public DbTester db = DbTester.create();
 
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
-  private OrganizationsWsSupport support = new OrganizationsWsSupport(new OrganizationValidationImpl(), db.getDbClient());
-  private EnableSupportAction underTest = new EnableSupportAction(userSession, db.getDbClient(), defaultOrganizationProvider, support);
+  private OrganizationFlags organizationFlags = new OrganizationFlagsImpl(db.getDbClient());
+  private EnableSupportAction underTest = new EnableSupportAction(userSession, db.getDbClient(), defaultOrganizationProvider, organizationFlags);
   private WsActionTester tester = new WsActionTester(underTest);
 
   @Test
   public void enabling_support_saves_internal_property_and_flags_caller_as_root() {
     UserDto user = db.users().insertUser();
     UserDto otherUser = db.users().insertUser();
-    db.properties().verifyInternal("organization.enabled", null);
-    db.rootFlag().verify(user.getLogin(), false);
-    db.rootFlag().verify(otherUser.getLogin(), false);
+    verifyFeatureEnabled(false);
+    verifyRoot(user, false);
+    verifyRoot(otherUser, false);
     logInAsSystemAdministrator(user.getLogin());
 
     call();
 
-    db.properties().verifyInternal("organization.enabled", "true");
-    db.rootFlag().verify(user.getLogin(), true);
-    db.rootFlag().verify(otherUser.getLogin(), false);
+    verifyFeatureEnabled(true);
+    verifyRoot(user, true);
+    verifyRoot(otherUser, false);
   }
 
   @Test
@@ -118,4 +120,12 @@ public class EnableSupportActionTest {
     TestResponse response = tester.newRequest().setMethod("POST").execute();
     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
   }
+
+  private void verifyFeatureEnabled(boolean enabled) {
+    assertThat(organizationFlags.isEnabled(db.getSession())).isEqualTo(enabled);
+  }
+
+  private void verifyRoot(UserDto user, boolean root) {
+    db.rootFlag().verify(user.getLogin(), root);
+  }
 }
index 5e0588450376fdc57cc147cb6e7687cfee4d6d67..b2a6977848b947676ebc6385f58a1890bc75c893 100644 (file)
@@ -68,7 +68,7 @@ public class SearchActionTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  private SearchAction underTest = new SearchAction(dbTester.getDbClient(), new OrganizationsWsSupport(new OrganizationValidationImpl(), dbTester.getDbClient()));
+  private SearchAction underTest = new SearchAction(dbTester.getDbClient(), new OrganizationsWsSupport(new OrganizationValidationImpl()));
   private WsActionTester wsTester = new WsActionTester(underTest);
 
   @Test
index a06d13521915d63dd94530941552f158e9fbb7eb..46ee0eca1c64f2838d110627bca8a342f6f76f06 100644 (file)
@@ -28,10 +28,10 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.organization.OrganizationValidationImpl;
+import org.sonar.server.organization.TestOrganizationFlags;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.WsActionTester;
@@ -60,7 +60,8 @@ public class UpdateActionTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  private UpdateAction underTest = new UpdateAction(userSession, new OrganizationsWsSupport(new OrganizationValidationImpl(), dbTester.getDbClient()), dbTester.getDbClient());
+  private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone().setEnabled(true);
+  private UpdateAction underTest = new UpdateAction(userSession, new OrganizationsWsSupport(new OrganizationValidationImpl()), dbTester.getDbClient(), organizationFlags);
   private WsActionTester wsTester = new WsActionTester(underTest);
 
   @Test
@@ -69,7 +70,7 @@ public class UpdateActionTest {
     assertThat(action.key()).isEqualTo("update");
     assertThat(action.isPost()).isTrue();
     assertThat(action.description()).isEqualTo("Update an organization.<br/>" +
-      "Require 'Administer System' permission. Organization feature must be enabled.");
+      "Require 'Administer System' permission. Organization support must be enabled.");
     assertThat(action.isInternal()).isTrue();
     assertThat(action.since()).isEqualTo("6.2");
     assertThat(action.handler()).isEqualTo(underTest);
@@ -99,18 +100,19 @@ public class UpdateActionTest {
   }
 
   @Test
-  public void request_fails_with_organization_feature_is_disabled() {
+  public void request_fails_with_IllegalStateException_if_organization_feature_is_disabled() {
+    organizationFlags.setEnabled(false);
     userSession.logIn();
 
-    expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("");
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Organization support is disabled");
 
     wsTester.newRequest().execute();
   }
 
   @Test
   public void request_succeeds_if_user_is_root() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
 
     verifyResponseAndDb(executeKeyRequest(dto.getKey(), "ab"), dto, "ab", DATE_2);
@@ -118,7 +120,6 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_user_is_administrator_of_specified_organization() {
-    enableOrganizations();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
     logInAsAdministrator(dto);
 
@@ -127,8 +128,6 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_with_UnauthorizedException_when_user_is_not_logged_in() {
-    enableOrganizations();
-
     expectedException.expect(UnauthorizedException.class);
     expectedException.expectMessage("Authentication is required");
 
@@ -137,7 +136,6 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_user_is_not_root_and_is_not_organization_administrator() {
-    enableOrganizations();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
     userSession.logIn();
 
@@ -149,7 +147,6 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_user_is_administrator_of_another_organization() {
-    enableOrganizations();
     OrganizationDto org = dbTester.organizations().insert();
     logInAsAdministrator(dbTester.getDefaultOrganization());
 
@@ -161,7 +158,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_key_is_missing() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("The 'key' parameter is missing");
@@ -171,7 +168,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_with_only_key_param_succeeds_and_updates_only_updateAt_field() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
 
     verifyResponseAndDb(executeKeyRequest(dto.getKey(), null), dto, dto.getName(), DATE_2);
@@ -179,7 +176,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_name_is_one_char_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Name 'a' must be at least 2 chars long");
@@ -189,7 +186,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_name_is_two_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
 
     verifyResponseAndDb(executeKeyRequest(dto.getKey(), "ab"), dto, "ab", DATE_2);
@@ -197,7 +194,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_name_is_65_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Name '" + STRING_65_CHARS_LONG + "' must be at most 64 chars long");
@@ -207,7 +204,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_name_is_64_char_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
 
     String name = STRING_65_CHARS_LONG.substring(0, 64);
@@ -217,7 +214,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_description_url_and_avatar_are_not_specified() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
 
     Organizations.UpdateWsResponse response = executeKeyRequest(dto.getKey(), "bar", null, null, null);
@@ -226,7 +223,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_description_url_and_avatar_are_specified() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
 
     Organizations.UpdateWsResponse response = executeKeyRequest(dto.getKey(), "bar", "moo", "doo", "boo");
@@ -235,7 +232,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_description_is_257_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Description '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long");
@@ -245,7 +242,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_description_is_256_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
     String description = STRING_257_CHARS_LONG.substring(0, 256);
 
@@ -255,7 +252,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_url_is_257_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Url '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long");
@@ -265,7 +262,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_url_is_256_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
     String url = STRING_257_CHARS_LONG.substring(0, 256);
 
@@ -275,7 +272,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_fails_if_avatar_is_257_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Avatar '" + STRING_257_CHARS_LONG + "' must be at most 256 chars long");
@@ -285,7 +282,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_succeeds_if_avatar_is_256_chars_long() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
     String avatar = STRING_257_CHARS_LONG.substring(0, 256);
 
@@ -295,7 +292,7 @@ public class UpdateActionTest {
 
   @Test
   public void request_removes_optional_parameters_when_associated_parameter_are_empty() {
-    enableOrganizationsAndLogInAsRoot();
+    logInAsRoot();
     OrganizationDto dto = mockForSuccessfulUpdate(DATE_1, DATE_2);
 
     Organizations.UpdateWsResponse response = executeKeyRequest(dto.getKey(), "bla", "", "", "");
@@ -378,10 +375,6 @@ public class UpdateActionTest {
     assertThat(newDto.getUpdatedAt()).isEqualTo(updateAt);
   }
 
-  private void enableOrganizations() {
-    dbTester.enableOrganizations();
-  }
-
   private void logInAsRoot() {
     userSession.logIn().setRoot();
   }
@@ -389,9 +382,4 @@ public class UpdateActionTest {
   private void logInAsAdministrator(OrganizationDto organizationDto) {
     userSession.logIn().addOrganizationPermission(organizationDto.getUuid(), SYSTEM_ADMIN);
   }
-
-  private void enableOrganizationsAndLogInAsRoot() {
-    enableOrganizations();
-    logInAsRoot();
-  }
 }