--- /dev/null
+/*
+ * 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.core.util.Uuids;
+import org.sonar.server.computation.CeQueue;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.user.UserSession;
+
+public class CeCancelWsAction 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;
+
+ public CeCancelWsAction(UserSession userSession, CeQueue queue) {
+ this.userSession = userSession;
+ this.queue = queue;
+ }
+
+ @Override
+ public void define(WebService.NewController controller) {
+ WebService.NewAction action = controller.createAction("cancel")
+ .setDescription("Cancels a pending task. Requires system administration permission.")
+ .setInternal(true)
+ .setPost(true)
+ .setHandler(this);
+
+ action
+ .createParam(PARAM_TASK_ID)
+ .setDescription("Optional 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 {
+ 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");
+ }
+ wsResponse.noContent();
+ }
+}
public class CeTaskWsAction implements CeWsAction {
public static final String ACTION = "task";
-
public static final String PARAM_TASK_ID = "id";
private final DbClient dbClient;
import org.sonar.server.computation.monitoring.CEQueueStatusImpl;
import org.sonar.server.computation.monitoring.ComputeEngineQueueMonitor;
import org.sonar.server.computation.ws.CeActivityWsAction;
+import org.sonar.server.computation.ws.CeCancelWsAction;
import org.sonar.server.computation.ws.CeQueueWsAction;
import org.sonar.server.computation.ws.CeSubmitWsAction;
import org.sonar.server.computation.ws.CeTaskWsAction;
CeTaskWsAction.class,
CeSubmitWsAction.class,
CeActivityWsAction.class,
+ CeCancelWsAction.class,
CeQueueWsAction.class,
IsQueueEmptyWs.class,
DefaultPeriodCleaner.class,
--- /dev/null
+/*
+ * 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.BadRequestException;
+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 CeCancelWsActionTest {
+
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+
+ @Rule
+ public DbTester dbTester = DbTester.create(System2.INSTANCE);
+
+ CeQueue queue = mock(CeQueue.class);
+ CeCancelWsAction underTest = new CeCancelWsAction(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);
+
+ tester.newRequest()
+ .setParam("id", "T1")
+ .execute();
+
+ verify(queue).cancel("T1");
+ }
+
+ @Test(expected = BadRequestException.class)
+ public void missing_parameters() {
+ userSession.setGlobalPermissions(UserRole.ADMIN);
+
+ tester.newRequest().execute();
+
+ verifyZeroInteractions(queue);
+ }
+
+ @Test(expected = ForbiddenException.class)
+ public void not_authorized() {
+ tester.newRequest()
+ .setParam("id", "T1")
+ .execute();
+
+ verifyZeroInteractions(queue);
+ }
+}