]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7970 Create client for set WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 23 Aug 2016 16:00:18 +0000 (18:00 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 25 Aug 2016 09:29:41 +0000 (11:29 +0200)
server/sonar-server/src/main/java/org/sonar/server/settings/ws/SetAction.java
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SetRequestTest.java
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java

index 1e1b7ed9828ea1414fabd095b61ce8ae5fc765e3..a5b7e060a470e2587dce4aec5aca05ef5743c11b 100644 (file)
@@ -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.<br>" +
         "Either '%s' or '%s' can be provided, not both.<br> " +
         "Requires one of the following permissions: " +
         "<ul>" +
         "<li>'Administer System'</li>" +
         "<li>'Administer' rights on the specified component</li>" +
-        "</ul>", "componentId", "componentKey")
+        "</ul>", 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();
   }
 
index 1b9f14007ca8a0fea249bb2d8658649524fa8bfb..c30b418495e2639ce51644a520b56cd9100f4c83 100644 (file)
@@ -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()));
+  }
+
 }
index 9c21076f684dcad2abe0367eb2e52285458d2927..04f8fe24a65727b2dc4ce5a94bb28a30618e161c 100644 (file)
@@ -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
index 472044e874eeba2c2dd36aee4c2e8f202610fc60..b1e48020a301c14114eb8af2faedc0e3435efff5 100644 (file)
@@ -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();
   }
+
 }
index 57607b16704c204e35af13b6c3800e86981cba99..1c34102cf5a056a3d2b0a241bbcebccafc036f69 100644 (file)
@@ -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();
+  }
+
 }