--- /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.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();
+ }
+}
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;
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();
}
}
add(
ActivityWsAction.class,
CancelWsAction.class,
+ CancelAllWsAction.class,
CeQueueWsAction.class,
CeWs.class,
IsQueueEmptyWs.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.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);
+ }
+}
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;
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);
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();
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 */);
}
}