]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6798 split WS api/ce/cancel and api/ce/cancel_all
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 27 Sep 2015 10:48:56 +0000 (12:48 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 27 Sep 2015 10:48:56 +0000 (12:48 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CancelAllWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CancelWsAction.java
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeWsModule.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/CancelAllWsActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/ws/CancelWsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/CeWsModuleTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CancelAllWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CancelAllWsAction.java
new file mode 100644 (file)
index 0000000..eb2df43
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.computation.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.api.web.UserRole;
+import org.sonar.server.computation.CeQueue;
+import org.sonar.server.user.UserSession;
+
+public class CancelAllWsAction implements CeWsAction {
+
+  private final UserSession userSession;
+  private final CeQueue queue;
+
+  public CancelAllWsAction(UserSession userSession, CeQueue queue) {
+    this.userSession = userSession;
+    this.queue = queue;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    controller.createAction("cancel_all")
+      .setDescription("Cancels all pending task. Requires system administration permission.")
+      .setInternal(true)
+      .setPost(true)
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request wsRequest, Response wsResponse) {
+    userSession.checkGlobalPermission(UserRole.ADMIN);
+    queue.cancelAll();
+    wsResponse.noContent();
+  }
+}
index 7588bebbc4b3a9b314e83a6503e43fd2b9ce48d5..22f380069fa4ecf3b0b85ba6f418c1b45846fbd6 100644 (file)
@@ -25,13 +25,11 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.util.Uuids;
 import org.sonar.server.computation.CeQueue;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.user.UserSession;
 
 public class CancelWsAction implements CeWsAction {
 
   public static final String PARAM_TASK_ID = "id";
-  public static final String PARAM_ALL = "all";
 
   private final UserSession userSession;
   private final CeQueue queue;
@@ -51,27 +49,16 @@ public class CancelWsAction implements CeWsAction {
 
     action
       .createParam(PARAM_TASK_ID)
-      .setDescription("Optional id of the task to cancel.")
+      .setRequired(true)
+      .setDescription("Id of the task to cancel.")
       .setExampleValue(Uuids.UUID_EXAMPLE_01);
-
-    action
-      .createParam(PARAM_ALL)
-      .setDescription("Cancels all pending tasks if this parameter is set. Ignored if the parameter " + PARAM_TASK_ID + " is set.")
-      .setBooleanPossibleValues()
-      .setDefaultValue("false");
   }
 
   @Override
-  public void handle(Request wsRequest, Response wsResponse) throws Exception {
+  public void handle(Request wsRequest, Response wsResponse) {
     userSession.checkGlobalPermission(UserRole.ADMIN);
-    String taskId = wsRequest.param(PARAM_TASK_ID);
-    if (taskId != null) {
-      queue.cancel(taskId);
-    } else if (wsRequest.paramAsBoolean(PARAM_ALL)) {
-      queue.cancelAll();
-    } else {
-      throw new BadRequestException("Missing parameters");
-    }
+    String taskId = wsRequest.mandatoryParam(PARAM_TASK_ID);
+    queue.cancel(taskId);
     wsResponse.noContent();
   }
 }
index 79bd61ce1c3cdde1afabe9600e24384f1f057ad0..2092778ad2628ce79437acd7f1f5cbbaa78de54a 100644 (file)
@@ -27,6 +27,7 @@ public class CeWsModule extends Module {
     add(
       ActivityWsAction.class,
       CancelWsAction.class,
+      CancelAllWsAction.class,
       CeQueueWsAction.class,
       CeWs.class,
       IsQueueEmptyWs.class,
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/CancelAllWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/CancelAllWsActionTest.java
new file mode 100644 (file)
index 0000000..f86045b
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.computation.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbTester;
+import org.sonar.server.computation.CeQueue;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+public class CancelAllWsActionTest {
+
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+
+  @Rule
+  public DbTester dbTester = DbTester.create(System2.INSTANCE);
+
+  CeQueue queue = mock(CeQueue.class);
+  CancelAllWsAction underTest = new CancelAllWsAction(userSession, queue);
+  WsActionTester tester = new WsActionTester(underTest);
+
+  @Test
+  public void cancel_all_pending_tasks() {
+    userSession.setGlobalPermissions(UserRole.ADMIN);
+
+    tester.newRequest().execute();
+
+    verify(queue).cancelAll();
+  }
+
+  @Test(expected = ForbiddenException.class)
+  public void not_authorized() {
+    tester.newRequest().execute();
+
+    verifyZeroInteractions(queue);
+  }
+}
index 3374424484f69095e2835f1f04734eed8e0a278f..d0fdefe6f6dde5e3b1748e90fe236b4c413ddc07 100644 (file)
@@ -25,7 +25,6 @@ import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbTester;
 import org.sonar.server.computation.CeQueue;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
@@ -46,17 +45,6 @@ public class CancelWsActionTest {
   CancelWsAction underTest = new CancelWsAction(userSession, queue);
   WsActionTester tester = new WsActionTester(underTest);
 
-  @Test
-  public void cancel_all_pending_tasks() {
-    userSession.setGlobalPermissions(UserRole.ADMIN);
-
-    tester.newRequest()
-      .setParam("all", "true")
-      .execute();
-
-    verify(queue).cancelAll();
-  }
-
   @Test
   public void cancel_pending_task() {
     userSession.setGlobalPermissions(UserRole.ADMIN);
@@ -68,8 +56,8 @@ public class CancelWsActionTest {
     verify(queue).cancel("T1");
   }
 
-  @Test(expected = BadRequestException.class)
-  public void missing_parameters() {
+  @Test(expected = IllegalArgumentException.class)
+  public void missing_id() {
     userSession.setGlobalPermissions(UserRole.ADMIN);
 
     tester.newRequest().execute();
index 0c557159e263dd8a2af81670e95213152fc218c0..1711fa04ba0918e528b1862f33d5ed22a7fc252e 100644 (file)
@@ -30,6 +30,6 @@ public class CeWsModuleTest {
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new CeWsModule().configure(container);
-    assertThat(container.size()).isEqualTo(10 + 2 /* injected by ComponentContainer */);
+    assertThat(container.size()).isEqualTo(11 + 2 /* injected by ComponentContainer */);
   }
 }