]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10002 add WS api/editions/clear_error_message
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 18 Oct 2017 12:31:20 +0000 (14:31 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Mon, 23 Oct 2017 15:01:13 +0000 (08:01 -0700)
server/sonar-server/src/main/java/org/sonar/server/edition/EditionsWsModule.java
server/sonar-server/src/main/java/org/sonar/server/edition/ws/ApplyLicenseAction.java
server/sonar-server/src/main/java/org/sonar/server/edition/ws/ClearErrorMessageAction.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/edition/EditionsWsModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/edition/ws/ClearErrorMessageActionTest.java [new file with mode: 0644]

index 7470ffc2a370014270e9891dee0ad5e6f1d93b49..37027d6c5f04f941b4eedc1dc88d249a31ae547d 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.edition;
 
 import org.sonar.core.platform.Module;
 import org.sonar.server.edition.ws.ApplyLicenseAction;
+import org.sonar.server.edition.ws.ClearErrorMessageAction;
 import org.sonar.server.edition.ws.EditionsWs;
 import org.sonar.server.edition.ws.FormDataAction;
 import org.sonar.server.edition.ws.PreviewAction;
@@ -35,6 +36,7 @@ public class EditionsWsModule extends Module {
       ApplyLicenseAction.class,
       PreviewAction.class,
       FormDataAction.class,
+      ClearErrorMessageAction.class,
       UninstallAction.class,
       EditionsWs.class);
   }
index 9f9dd7e87d4f8b8ef1b76f6eb559af231a23b8ff..8fc65d10ff95a5bc6e9ba6bb767b2790c77fd67e 100644 (file)
@@ -62,7 +62,9 @@ public class ApplyLicenseAction implements EditionsWsAction {
     WebService.NewAction action = controller.createAction("apply_license")
       .setSince("6.7")
       .setPost(true)
-      .setDescription("Apply changes to SonarQube to match the specified license. Require 'Administer System' permission.")
+      .setDescription("Apply changes to SonarQube to match the specified license." +
+        " Clear error message of previous automatic install of an edition, if there is any." +
+        " Require 'Administer System' permission.")
       .setResponseExample(getClass().getResource("example-edition-apply_license.json"))
       .setHandler(this);
 
diff --git a/server/sonar-server/src/main/java/org/sonar/server/edition/ws/ClearErrorMessageAction.java b/server/sonar-server/src/main/java/org/sonar/server/edition/ws/ClearErrorMessageAction.java
new file mode 100644 (file)
index 0000000..7c9ecb4
--- /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.edition.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.edition.MutableEditionManagementState;
+import org.sonar.server.user.UserSession;
+
+public class ClearErrorMessageAction implements EditionsWsAction {
+  private final UserSession userSession;
+  private final MutableEditionManagementState editionManagementState;
+
+  public ClearErrorMessageAction(UserSession userSession, MutableEditionManagementState editionManagementState) {
+    this.userSession = userSession;
+    this.editionManagementState = editionManagementState;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    WebService.NewAction action = controller.createAction("clear_error_message")
+      .setSince("6.7")
+      .setPost(true)
+      .setDescription("Clear error message of last install of an edition (if any). Require 'Administer System' permission.")
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    userSession
+      .checkLoggedIn()
+      .checkIsSystemAdministrator();
+
+    editionManagementState.clearInstallErrorMessage();
+
+    response.noContent();
+  }
+}
index 34ab5aa5fa3ab20680d263ac16811f2c2d7f0e06..0a801053276e2d15a919acb01d18f240d80a8219 100644 (file)
@@ -34,6 +34,6 @@ public class EditionsWsModuleTest {
     underTest.configure(container);
 
     assertThat(container.getPicoContainer().getComponentAdapters())
-      .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 6);
+      .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7);
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/edition/ws/ClearErrorMessageActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/edition/ws/ClearErrorMessageActionTest.java
new file mode 100644 (file)
index 0000000..b387b7d
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * 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.edition.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.edition.MutableEditionManagementState;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.verify;
+
+public class ClearErrorMessageActionTest {
+  @Rule
+  public UserSessionRule userSessionRule = UserSessionRule.standalone();
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private MutableEditionManagementState editionManagementState = Mockito.mock(MutableEditionManagementState.class);
+  private ClearErrorMessageAction underTest = new ClearErrorMessageAction(userSessionRule, editionManagementState);
+  private WsActionTester actionTester = new WsActionTester(underTest);
+
+  @Test
+  public void verify_definition() {
+    WebService.Action def = actionTester.getDef();
+
+    assertThat(def.key()).isEqualTo("clear_error_message");
+    assertThat(def.since()).isEqualTo("6.7");
+    assertThat(def.isPost()).isTrue();
+    assertThat(def.isInternal()).isFalse();
+    assertThat(def.description()).isNotEmpty();
+    assertThat(def.responseExample()).isNull();
+    assertThat(def.params()).isEmpty();
+  }
+
+  @Test
+  public void request_fails_if_user_not_logged_in() {
+    userSessionRule.anonymous();
+    TestRequest request = actionTester.newRequest();
+
+    expectedException.expect(UnauthorizedException.class);
+    expectedException.expectMessage("Authentication is required");
+
+    request.execute();
+  }
+
+  @Test
+  public void request_fails_if_user_is_not_system_administer() {
+    userSessionRule.logIn();
+    TestRequest request = actionTester.newRequest();
+
+    expectedException.expect(ForbiddenException.class);
+    expectedException.expectMessage("Insufficient privileges");
+
+    request.execute();
+  }
+
+  @Test
+  public void request_clears_errorMessage_from_editionManagement_state() {
+    userSessionRule.logIn().setSystemAdministrator();
+
+    actionTester.newRequest().execute();
+
+    verify(editionManagementState).clearInstallErrorMessage();
+  }
+}