From: Julien Lancelot Date: Tue, 23 Aug 2016 16:00:18 +0000 (+0200) Subject: SONAR-7970 Create client for set WS X-Git-Tag: 6.1-RC1~308 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6fe143ec0c68c77d557dc48d0ba010568f63d4fd;p=sonarqube.git SONAR-7970 Create client for set WS --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SetAction.java b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SetAction.java index 1e1b7ed9828..a5b7e060a47 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SetAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SetAction.java @@ -36,6 +36,12 @@ import org.sonar.server.user.UserSession; import org.sonar.server.ws.KeyExamples; import org.sonarqube.ws.client.setting.SetRequest; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_SET; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUE; + public class SetAction implements SettingsWsAction { private final DbClient dbClient; private final ComponentFinder componentFinder; @@ -49,33 +55,33 @@ public class SetAction implements SettingsWsAction { @Override public void define(WebService.NewController context) { - WebService.NewAction action = context.createAction("set") + WebService.NewAction action = context.createAction(ACTION_SET) .setDescription("Update a setting value.
" + "Either '%s' or '%s' can be provided, not both.
" + "Requires one of the following permissions: " + "", "componentId", "componentKey") + "", PARAM_COMPONENT_ID, PARAM_COMPONENT_KEY) .setSince("6.1") .setPost(true) .setHandler(this); - action.createParam("key") + action.createParam(PARAM_KEY) .setDescription("Setting key") .setExampleValue("sonar.links.scm") .setRequired(true); - action.createParam("value") + action.createParam(PARAM_VALUE) .setDescription("Setting value. To reset a value, please use the reset web service.") .setExampleValue("git@github.com:SonarSource/sonarqube.git") .setRequired(true); - action.createParam("componentId") + action.createParam(PARAM_COMPONENT_ID) .setDescription("Component id") .setExampleValue(Uuids.UUID_EXAMPLE_01); - action.createParam("componentKey") + action.createParam(PARAM_COMPONENT_KEY) .setDescription("Component key") .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001); } @@ -107,10 +113,10 @@ public class SetAction implements SettingsWsAction { private static SetRequest toWsRequest(Request request) { return SetRequest.builder() - .setKey(request.mandatoryParam("key")) - .setValue(request.mandatoryParam("value")) - .setComponentId(request.param("componentId")) - .setComponentKey(request.param("componentKey")) + .setKey(request.mandatoryParam(PARAM_KEY)) + .setValue(request.mandatoryParam(PARAM_VALUE)) + .setComponentId(request.param(PARAM_COMPONENT_ID)) + .setComponentKey(request.param(PARAM_COMPONENT_KEY)) .build(); } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java index 1b9f14007ca..c30b418495e 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java @@ -23,14 +23,18 @@ import org.sonarqube.ws.Settings.ListDefinitionsWsResponse; import org.sonarqube.ws.Settings.ValuesWsResponse; import org.sonarqube.ws.client.BaseService; import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsConnector; import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_ID; import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_KEY; import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_SET; import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_VALUES; import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY; import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEYS; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUE; public class SettingsService extends BaseService { public SettingsService(WsConnector wsConnector) { @@ -52,4 +56,12 @@ public class SettingsService extends BaseService { return call(getRequest, ValuesWsResponse.parser()); } + public void set(SetRequest request) { + call(new PostRequest(path(ACTION_SET)) + .setParam(PARAM_KEY, request.getKey()) + .setParam(PARAM_VALUE, request.getValue()) + .setParam(PARAM_COMPONENT_ID, request.getComponentId()) + .setParam(PARAM_COMPONENT_KEY, request.getComponentKey())); + } + } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java index 9c21076f684..04f8fe24a65 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java @@ -25,10 +25,13 @@ public class SettingsWsParameters { public static final String ACTION_LIST_DEFINITIONS = "list_definitions"; public static final String ACTION_VALUES = "values"; + public static final String ACTION_SET = "set"; public static final String PARAM_COMPONENT_ID = "componentId"; public static final String PARAM_COMPONENT_KEY = "componentKey"; public static final String PARAM_KEYS = "keys"; + public static final String PARAM_KEY = "key"; + public static final String PARAM_VALUE = "value"; private SettingsWsParameters() { // Only static stuff diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SetRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SetRequestTest.java index 472044e874e..b1e48020a30 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SetRequestTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SetRequestTest.java @@ -39,6 +39,28 @@ public class SetRequestTest { assertThat(result.getKey()).isEqualTo("my.key"); assertThat(result.getValue()).isEqualTo("my value"); + assertThat(result.getComponentKey()).isNull(); + assertThat(result.getComponentId()).isNull(); + } + + @Test + public void create_request_with_component_id() { + SetRequest result = underTest.setKey("my.key").setValue("my value").setComponentId("projectId").build(); + + assertThat(result.getKey()).isEqualTo("my.key"); + assertThat(result.getValue()).isEqualTo("my value"); + assertThat(result.getComponentId()).isEqualTo("projectId"); + assertThat(result.getComponentKey()).isNull(); + } + + @Test + public void create_request_with_component_key() { + SetRequest result = underTest.setKey("my.key").setValue("my value").setComponentKey("projectKey").build(); + + assertThat(result.getKey()).isEqualTo("my.key"); + assertThat(result.getValue()).isEqualTo("my value"); + assertThat(result.getComponentId()).isNull(); + assertThat(result.getComponentKey()).isEqualTo("projectKey"); } @Test @@ -60,4 +82,5 @@ public class SetRequestTest { .setValue(null) .build(); } + } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java index 57607b16704..1c34102cf5a 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java @@ -25,13 +25,16 @@ import org.junit.Test; import org.sonarqube.ws.Settings.ListDefinitionsWsResponse; import org.sonarqube.ws.Settings.ValuesWsResponse; import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.ServiceTester; import org.sonarqube.ws.client.WsConnector; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY; import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEYS; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUE; public class SettingsServiceTest { @@ -68,4 +71,20 @@ public class SettingsServiceTest { .andNoOtherParam(); } + @Test + public void set() { + underTest.set(SetRequest.builder() + .setKey("sonar.debt") + .setValue("8h") + .setComponentKey("KEY") + .build()); + PostRequest request = serviceTester.getPostRequest(); + + serviceTester.assertThat(request) + .hasParam(PARAM_KEY, "sonar.debt") + .hasParam(PARAM_VALUE, "8h") + .hasParam(PARAM_COMPONENT_KEY, "KEY") + .andNoOtherParam(); + } + }