]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8761 do not fail if org feature is already enabled
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 10 Feb 2017 14:23:49 +0000 (15:23 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 10 Feb 2017 22:07:22 +0000 (23:07 +0100)
server/sonar-server/src/main/java/org/sonar/server/organization/ws/EnableSupportAction.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/EnableSupportActionTest.java

index dc7f198e01db7650fe666bafc135cd120e6268c0..60427f8cb9373e9a51fd81c0e101097c4399e884 100644 (file)
@@ -24,7 +24,6 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 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.organization.OrganizationFlags;
 import org.sonar.server.user.UserSession;
@@ -64,10 +63,11 @@ public class EnableSupportAction implements OrganizationsAction {
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
       verifySystemAdministrator();
-      verifyFeatureIsDisabled(dbSession);
-      flagCurrentUserAsRoot(dbSession);
-      enableFeature(dbSession);
-      dbSession.commit();
+      if (isSupportDisabled(dbSession)) {
+        flagCurrentUserAsRoot(dbSession);
+        enableFeature(dbSession);
+        dbSession.commit();
+      }
     }
     response.noContent();
   }
@@ -76,10 +76,8 @@ public class EnableSupportAction implements OrganizationsAction {
     userSession.checkLoggedIn().checkOrganizationPermission(defaultOrganizationProvider.get().getUuid(), SYSTEM_ADMIN);
   }
 
-  private void verifyFeatureIsDisabled(DbSession dbSession) {
-    if (organizationFlags.isEnabled(dbSession)) {
-      throw new BadRequestException("Organizations are already enabled");
-    }
+  private boolean isSupportDisabled(DbSession dbSession) {
+    return !organizationFlags.isEnabled(dbSession);
   }
 
   private void flagCurrentUserAsRoot(DbSession dbSession) {
index 0f55aeef8bddfe1f1f2d7e027af1801be4b47ea3..6f03cc442e1bf2765a1388afba0e24b31de447cb 100644 (file)
@@ -26,7 +26,6 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbTester;
 import org.sonar.db.user.UserDto;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.organization.DefaultOrganizationProvider;
@@ -92,15 +91,17 @@ public class EnableSupportActionTest {
   }
 
   @Test
-  public void throw_BadRequestException_if_support_is_already_enabled() {
+  public void do_nothing_if_support_is_already_enabled() {
     logInAsSystemAdministrator("foo");
 
     call();
+    verifyFeatureEnabled(true);
 
-    expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("Organizations are already enabled");
-
+    // the test could be improved to verify that
+    // the caller user is not flagged as root
+    // if he was not already root
     call();
+    verifyFeatureEnabled(true);
   }
 
   @Test