diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-07-05 17:42:43 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-07-17 10:52:47 +0200 |
commit | 0839c4ba9cf741764c885ce6ba544bc6b1a82410 (patch) | |
tree | 8457fc50f7b0dce1b1ac80389fd51b3700cb6739 | |
parent | 20873ba1d51089dce84bf6f3e115564adde4e721 (diff) | |
download | sonarqube-0839c4ba9cf741764c885ce6ba544bc6b1a82410.tar.gz sonarqube-0839c4ba9cf741764c885ce6ba544bc6b1a82410.zip |
SONAR-9508 Create api/ce/worker_count WS
10 files changed, 219 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()); + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java index bebc2f1f028..ad610ee4a04 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java @@ -22,10 +22,12 @@ package org.sonarqube.ws.client.ce; import org.sonarqube.ws.WsCe; import org.sonarqube.ws.WsCe.ActivityResponse; import org.sonarqube.ws.WsCe.TaskTypesWsResponse; +import org.sonarqube.ws.WsCe.WorkerCountResponse; import org.sonarqube.ws.client.BaseService; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.WsConnector; +import static org.sonarqube.ws.client.ce.CeWsParameters.ACTION_WORKER_COUNT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT; @@ -89,4 +91,8 @@ public class CeService extends BaseService { WsCe.ActivityStatusWsResponse.parser()); } + public WorkerCountResponse workerCount() { + return call(new GetRequest(path(ACTION_WORKER_COUNT)), WorkerCountResponse.parser()); + } + } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java index a56247b77bd..77cbbd673e0 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java @@ -20,6 +20,9 @@ package org.sonarqube.ws.client.ce; public class CeWsParameters { + + public static final String ACTION_WORKER_COUNT = "worker_count"; + public static final String PARAM_COMPONENT_ID = "componentId"; public static final String PARAM_COMPONENT_KEY = "componentKey"; public static final String PARAM_COMPONENT_QUERY = "componentQuery"; diff --git a/sonar-ws/src/main/protobuf/ws-ce.proto b/sonar-ws/src/main/protobuf/ws-ce.proto index 6c80616fd69..cc38df268c7 100644 --- a/sonar-ws/src/main/protobuf/ws-ce.proto +++ b/sonar-ws/src/main/protobuf/ws-ce.proto @@ -61,6 +61,12 @@ message TaskTypesWsResponse { repeated string taskTypes = 1; } +// GET api/ce/worker_count +message WorkerCountResponse { + optional int32 value = 1; + optional bool canSetWorkerCount = 2; +} + message Task { optional string organization = 20; optional string id = 1; diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java index e8e33e1eb90..16d443a7276 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java @@ -133,4 +133,12 @@ public class CeServiceTest { assertThat(serviceTester.getGetRequest().getPath()).isEqualTo("api/ce/task"); assertThat(serviceTester.getGetRequest().getParams()).containsOnly(entry("id", "task_id"), entry("additionalFields", "stacktrace")); } + + @Test + public void worker_count() { + underTest.workerCount(); + + assertThat(serviceTester.getGetRequest().getPath()).isEqualTo("api/ce/worker_count"); + assertThat(serviceTester.getGetParser()).isSameAs(WsCe.WorkerCountResponse.parser()); + } } diff --git a/tests/src/test/java/org/sonarqube/tests/ce/WorkerCountTest.java b/tests/src/test/java/org/sonarqube/tests/ce/WorkerCountTest.java index c5c63bde250..3a8e766953d 100644 --- a/tests/src/test/java/org/sonarqube/tests/ce/WorkerCountTest.java +++ b/tests/src/test/java/org/sonarqube/tests/ce/WorkerCountTest.java @@ -27,8 +27,11 @@ import java.util.Set; import java.util.stream.Collectors; import org.junit.After; import org.junit.Test; +import org.sonarqube.ws.WsCe.WorkerCountResponse; +import static java.lang.Integer.parseInt; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.newAdminWsClient; import static util.ItUtils.pluginArtifact; public class WorkerCountTest { @@ -58,5 +61,9 @@ public class WorkerCountTest { assertThat(line) .hasSize(1); assertThat(line.iterator().next()).contains(workerCount); + + assertThat(newAdminWsClient(orchestrator).ce().workerCount()) + .extracting(WorkerCountResponse::getValue, WorkerCountResponse::getCanSetWorkerCount) + .containsOnly(parseInt(workerCount), true); } } |