aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-07-05 17:42:43 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-07-17 10:52:47 +0200
commit0839c4ba9cf741764c885ce6ba544bc6b1a82410 (patch)
tree8457fc50f7b0dce1b1ac80389fd51b3700cb6739 /server
parent20873ba1d51089dce84bf6f3e115564adde4e721 (diff)
downloadsonarqube-0839c4ba9cf741764c885ce6ba544bc6b1a82410.tar.gz
sonarqube-0839c4ba9cf741764c885ce6ba544bc6b1a82410.zip
SONAR-9508 Create api/ce/worker_count WS
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ce/ws/CeWsModule.java3
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java80
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/ce/ws/worker_count-example.json4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/ce/ws/WorkerCountActionTest.java101
5 files changed, 189 insertions, 2 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/CeWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/CeWsModule.java
index 95421c9d444..d29cce937dc 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/CeWsModule.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/CeWsModule.java
@@ -35,6 +35,7 @@ public class CeWsModule extends Module {
SubmitAction.class,
TaskFormatter.class,
TaskAction.class,
- TaskTypesAction.class);
+ TaskTypesAction.class,
+ WorkerCountAction.class);
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java
new file mode 100644
index 00000000000..6a25ea910f1
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/WorkerCountAction.java
@@ -0,0 +1,80 @@
+/*
+ * 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.ce.ws;
+
+import javax.annotation.Nullable;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.ce.configuration.WorkerCountProvider;
+import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.WsCe.WorkerCountResponse;
+
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.ce.CeWsParameters.ACTION_WORKER_COUNT;
+
+public class WorkerCountAction implements CeWsAction {
+
+ private static final int DEFAULT_WORKER_COUNT = 1;
+
+ private final UserSession userSession;
+ private final WorkerCountProvider workerCountProvider;
+
+ public WorkerCountAction(UserSession userSession, @Nullable WorkerCountProvider workerCountProvider) {
+ this.userSession = userSession;
+ this.workerCountProvider = workerCountProvider;
+ }
+
+ public WorkerCountAction(UserSession userSession) {
+ this(userSession, null);
+ }
+
+ @Override
+ public void define(WebService.NewController controller) {
+ controller.createAction(ACTION_WORKER_COUNT)
+ .setDescription("Return number of Compute Engine workers.<br/>" +
+ "Requires the system administration permission")
+ .setResponseExample(getClass().getResource("worker_count-example.json"))
+ .setSince("6.5")
+ .setInternal(true)
+ .setHandler(this);
+ }
+
+ @Override
+ public void handle(Request wsRequest, Response wsResponse) throws Exception {
+ userSession.checkIsSystemAdministrator();
+ writeProtobuf(createResponse(), wsRequest, wsResponse);
+ }
+
+ private WorkerCountResponse createResponse(){
+ WorkerCountResponse.Builder builder = WorkerCountResponse.newBuilder();
+ if (workerCountProvider == null) {
+ return builder
+ .setValue(DEFAULT_WORKER_COUNT)
+ .setCanSetWorkerCount(false)
+ .build();
+ }
+ return builder
+ .setValue(workerCountProvider.get())
+ .setCanSetWorkerCount(true)
+ .build();
+ }
+
+}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/ce/ws/worker_count-example.json b/server/sonar-server/src/main/resources/org/sonar/server/ce/ws/worker_count-example.json
new file mode 100644
index 00000000000..0b54c6d1dc4
--- /dev/null
+++ b/server/sonar-server/src/main/resources/org/sonar/server/ce/ws/worker_count-example.json
@@ -0,0 +1,4 @@
+{
+ "value": 5,
+ "canSetWorkerCount": true
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java
index b966a06d464..be120e1e7d8 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/CeWsModuleTest.java
@@ -23,6 +23,7 @@ import org.junit.Test;
import org.sonar.core.platform.ComponentContainer;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER;
public class CeWsModuleTest {
@@ -30,6 +31,6 @@ public class CeWsModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new CeWsModule().configure(container);
- assertThat(container.size()).isEqualTo(11 + 2 /* injected by ComponentContainer */);
+ assertThat(container.size()).isEqualTo(12 + COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/WorkerCountActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/WorkerCountActionTest.java
new file mode 100644
index 00000000000..e28f0bfe534
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/WorkerCountActionTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.ce.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.ce.configuration.WorkerCountProvider;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.WsCe.WorkerCountResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class WorkerCountActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+
+ private WorkerCountProvider workerCountProvider = mock(WorkerCountProvider.class);
+
+ @Test
+ public void return_value_and_can_set_worker_count_to_true_when_provider_exists() {
+ userSession.logIn().setSystemAdministrator();
+ when(workerCountProvider.get()).thenReturn(5);
+ WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession, workerCountProvider));
+
+ WorkerCountResponse response = ws.newRequest().executeProtobuf(WorkerCountResponse.class);
+
+ assertThat(response.getValue()).isEqualTo(5);
+ assertThat(response.getCanSetWorkerCount()).isTrue();
+ }
+
+ @Test
+ public void return_1_and_can_set_worker_count_to_false_when_provider_does_not_exist() {
+ userSession.logIn().setSystemAdministrator();
+ WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession));
+
+ WorkerCountResponse response = ws.newRequest().executeProtobuf(WorkerCountResponse.class);
+
+ assertThat(response.getValue()).isEqualTo(1);
+ assertThat(response.getCanSetWorkerCount()).isFalse();
+ }
+
+ @Test
+ public void fail_when_not_system_administrator() {
+ userSession.logIn().setNonSystemAdministrator();
+ WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession));
+
+ expectedException.expect(ForbiddenException.class);
+
+ ws.newRequest().execute();
+ }
+
+ @Test
+ public void test_definition() {
+ WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession, workerCountProvider));
+
+ WebService.Action action = ws.getDef();
+ assertThat(action.key()).isEqualTo("worker_count");
+ assertThat(action.since()).isEqualTo("6.5");
+ assertThat(action.responseExampleAsString()).isNotEmpty();
+ assertThat(action.params()).isEmpty();
+ }
+
+ @Test
+ public void test_example() {
+ userSession.logIn().setSystemAdministrator();
+ when(workerCountProvider.get()).thenReturn(5);
+ WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession, workerCountProvider));
+
+ String response = ws.newRequest().execute().getInput();
+
+ assertJson(response).isSimilarTo(ws.getDef().responseExample());
+ }
+}