]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7300 Replace key parameter by id
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 24 Jan 2017 11:05:44 +0000 (12:05 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 24 Jan 2017 15:22:34 +0000 (16:22 +0100)
Key parameter was badly used instead of id when rewritting api/properties to Java

it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java
server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java
server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java
server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java
server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java

index cff8e72f41cf90ad40d1b24f4f475225303a80f7..c0f431a7e45663b6f9667f1e5ac8b3f70ac1f599 100644 (file)
@@ -102,7 +102,8 @@ public class DeprecatedPropertiesWsTest {
   }
 
   private static void doResetSettings() {
-    resetSettings(orchestrator, null, "some-property", "custom-property", "int", "multi", "boolean", "hidden", "not_defined", "setting.secured", "setting.license.secured", "list", "undefined");
+    resetSettings(orchestrator, null, "some-property", "custom-property", "int", "multi", "boolean", "hidden", "not_defined", "setting.secured", "setting.license.secured", "list",
+      "undefined");
     resetSettings(orchestrator, PROJECT_KEY, PROJECT_SETTING_KEY, "sonar.coverage.exclusions", "project.setting");
   }
 
@@ -228,30 +229,44 @@ public class DeprecatedPropertiesWsTest {
       .doesNotContain("hidden");
   }
 
+  @Test
+  public void get_global_value_using_id_parameter() throws Exception {
+    setProperty("some-property", "value", null);
+
+    assertThat(getProperty(adminWsClient, "some-property", null, true).getValue()).isEqualTo("value");
+  }
+
   @Test
   public void put_property() throws Exception {
-    putProperty("some-property", "some-value", null);
+    putProperty("some-property", "some-value", null, false);
+
+    assertThat(getProperty("some-property", null).getValue()).isEqualTo("some-value");
+  }
+
+  @Test
+  public void put_property_using_id_parameter() throws Exception {
+    putProperty("some-property", "some-value", null, true);
 
     assertThat(getProperty("some-property", null).getValue()).isEqualTo("some-value");
   }
 
   @Test
   public void put_property_on_project() throws Exception {
-    putProperty("project.setting", "some-value", PROJECT_KEY);
+    putProperty("project.setting", "some-value", PROJECT_KEY, false);
 
     assertThat(getProperty("project.setting", PROJECT_KEY).getValue()).isEqualTo("some-value");
   }
 
   @Test
   public void put_property_for_undefined_setting() throws Exception {
-    putProperty("undefined", "some-value", null);
+    putProperty("undefined", "some-value", null, false);
 
     assertThat(getProperty("undefined", null).getValue()).isEqualTo("some-value");
   }
 
   @Test
   public void put_property_multi_values() throws Exception {
-    putProperty("multi", "value1,value2,value3", null);
+    putProperty("multi", "value1,value2,value3", null, false);
 
     Properties.Property setting = getProperty("multi", null);
     assertThat(setting.getValue()).isEqualTo("value1,value2,value3");
@@ -262,7 +277,16 @@ public class DeprecatedPropertiesWsTest {
   public void delete_property() throws Exception {
     setProperty("custom-property", "value", null);
 
-    deleteProperty("custom-property", null);
+    deleteProperty("custom-property", null, false);
+
+    assertThat(getProperty("custom-property", null)).isNull();
+  }
+
+  @Test
+  public void delete_property_using_id_parameter() throws Exception {
+    setProperty("custom-property", "value", null);
+
+    deleteProperty("custom-property", null, true);
 
     assertThat(getProperty("custom-property", null)).isNull();
   }
@@ -271,7 +295,7 @@ public class DeprecatedPropertiesWsTest {
   public void delete_property_on_project() throws Exception {
     setProperty("project.setting", "value", PROJECT_KEY);
 
-    deleteProperty("project.setting", PROJECT_KEY);
+    deleteProperty("project.setting", PROJECT_KEY, false);
 
     assertThat(getProperty("project.setting", PROJECT_KEY)).isNull();
   }
@@ -297,29 +321,32 @@ public class DeprecatedPropertiesWsTest {
   }
 
   private static Properties.Property getProperty(String key, @Nullable String componentKey) throws UnsupportedEncodingException {
-    return getProperty(adminWsClient, key, componentKey);
+    return getProperty(adminWsClient, key, componentKey, false);
   }
 
   @CheckForNull
-  private static Properties.Property getProperty(WsClient wsClient, String key, @Nullable String componentKey) throws UnsupportedEncodingException {
+  private static Properties.Property getProperty(WsClient wsClient, String key, @Nullable String componentKey, boolean useIdParameter) throws UnsupportedEncodingException {
+    GetRequest getRequest = useIdParameter ? new GetRequest("api/properties").setParam("id", encode(key, "UTF-8")).setParam("resource", componentKey)
+      : new GetRequest("api/properties/" + encode(key, "UTF-8")).setParam("resource", componentKey);
     WsResponse response = wsClient.wsConnector()
-      .call(new GetRequest("api/properties/" + encode(key, "UTF-8"))
-        .setParam("resource", componentKey))
+      .call(getRequest)
       .failIfNotSuccessful();
     Properties.Property[] properties = Properties.parse(response.content());
     return Arrays.stream(properties).findFirst().orElseGet(() -> null);
   }
 
-  private static void putProperty(String key, String value, @Nullable String componentKey) throws UnsupportedEncodingException {
-    String url = orchestrator.getServer().getUrl() + "/api/properties/" + encode(key, "UTF-8") + "?value=" + value;
+  private static void putProperty(String key, String value, @Nullable String componentKey, boolean useIdParameter) throws UnsupportedEncodingException {
+    String url = useIdParameter ? orchestrator.getServer().getUrl() + "/api/properties?id=" + encode(key, "UTF-8") + "&value=" + value
+      : orchestrator.getServer().getUrl() + "/api/properties/" + encode(key, "UTF-8") + "?value=" + value;
     url += componentKey != null ? "&resource=" + componentKey : "";
     call(new Request.Builder()
       .put(new FormBody.Builder().build())
       .url(url));
   }
 
-  private static void deleteProperty(String key, @Nullable String componentKey) throws UnsupportedEncodingException {
-    String url = orchestrator.getServer().getUrl() + "/api/properties/" + encode(key, "UTF-8");
+  private static void deleteProperty(String key, @Nullable String componentKey, boolean useIdParameter) throws UnsupportedEncodingException {
+    String url = useIdParameter ? orchestrator.getServer().getUrl() + "/api/properties?id=" + encode(key, "UTF-8")
+      : orchestrator.getServer().getUrl() + "/api/properties/" + encode(key, "UTF-8");
     url += componentKey != null ? "?resource=" + componentKey : "";
     call(new Request.Builder()
       .delete(new FormBody.Builder().build())
index d8db9d0fb602cac207759d77f3e62b0a1ee688ba..c4205ff3f54252667e25ab09b65084c21f8b5e3c 100644 (file)
@@ -62,7 +62,7 @@ public class IndexAction implements WsAction {
   private static final Splitter COMMA_SPLITTER = Splitter.on(",");
   private static final String COMMA_ENCODED_VALUE = "%2C";
 
-  public static final String PARAM_KEY = "key";
+  public static final String PARAM_ID = "id";
   public static final String PARAM_COMPONENT = "resource";
 
   private final DbClient dbClient;
@@ -83,7 +83,7 @@ public class IndexAction implements WsAction {
       .setResponseExample(getClass().getResource("index-example.json"))
       .setSince("2.6")
       .setHandler(this);
-    action.createParam(PARAM_KEY)
+    action.createParam(PARAM_ID)
       .setDescription("Setting key")
       .setExampleValue("sonar.technicalDebt.hoursInDay");
     action.createParam(PARAM_COMPONENT)
@@ -106,7 +106,7 @@ public class IndexAction implements WsAction {
     DbSession dbSession = dbClient.openSession(true);
     try {
       Optional<ComponentDto> component = loadComponent(dbSession, request);
-      String key = request.param(PARAM_KEY);
+      String key = request.param(PARAM_ID);
       List<PropertyDto> propertyDtos = loadProperties(dbSession, component, Optional.ofNullable(key));
       new ResponseBuilder(propertyDtos).build(json);
     } finally {
index d4e106f1f9708aa27d804478eeab7f573420459f..c3749bd9dc703372cfc8d74bb7fba7342ac6696d 100644 (file)
@@ -170,7 +170,7 @@ public class DeprecatedRestWebServiceFilter extends ServletFilter {
     }
 
     private void handleGet(Optional<String> key, Optional<String> component) {
-      addParameterIfPresent(PARAM_KEY, key);
+      addParameterIfPresent(IndexAction.PARAM_ID, key);
       addParameterIfPresent(IndexAction.PARAM_COMPONENT, component);
       addParameterIfPresent(PARAM_FORMAT, readParam(PARAM_FORMAT));
       redirectedPath = CONTROLLER_PROPERTIES + "/index";
index 119202861b88d9c0cabcc386a581557aef8d2241..d650f922628fdd7f7f8cd63ad26b1616b4773ffa 100644 (file)
@@ -456,7 +456,7 @@ public class IndexActionTest {
   private void executeAndVerify(@Nullable String componentKey, @Nullable String key, String expectedFile) {
     TestRequest request = ws.newRequest().setMediaType(MediaTypes.JSON);
     if (key != null) {
-      request.setParam("key", key);
+      request.setParam("id", key);
     }
     if (componentKey != null) {
       request.setParam("resource", componentKey);
index 65cbf7f8c780154bbefa2621235688cca5498da7..2d88112cb84637e46f1520c8ce60d3fea0e66892 100644 (file)
@@ -102,7 +102,7 @@ public class DeprecatedRestWebServiceFilterTest {
     underTest.doFilter(request, response, chain);
 
     assertRedirection("api/properties/index", "GET");
-    assertParam("key", "my.property");
+    assertParam("id", "my.property");
     assertNoParam("component", "value", "values");
   }
 
@@ -115,7 +115,7 @@ public class DeprecatedRestWebServiceFilterTest {
     underTest.doFilter(request, response, chain);
 
     assertRedirection("api/properties/index", "GET");
-    assertParam("key", "my.property");
+    assertParam("id", "my.property");
     assertNoParam("component", "value", "values");
   }
 
@@ -128,7 +128,7 @@ public class DeprecatedRestWebServiceFilterTest {
     underTest.doFilter(request, response, chain);
 
     assertRedirection("api/properties/index", "GET");
-    assertParam("key", "my.property");
+    assertParam("id", "my.property");
     assertNoParam("component", "value", "values");
   }
 
@@ -141,7 +141,7 @@ public class DeprecatedRestWebServiceFilterTest {
     underTest.doFilter(request, response, chain);
 
     assertRedirection("api/properties/index", "GET");
-    assertParam("key", "my.property");
+    assertParam("id", "my.property");
     assertParam("resource", "my_project");
     assertNoParam("component", "value", "values");
   }
@@ -155,7 +155,7 @@ public class DeprecatedRestWebServiceFilterTest {
     underTest.doFilter(request, response, chain);
 
     assertRedirection("api/properties/index", "GET");
-    assertParam("key", "my.property");
+    assertParam("id", "my.property");
     assertParam("format", "json");
     assertNoParam("component", "value", "values");
   }