From: Julien Lancelot Date: Tue, 24 Jan 2017 11:05:44 +0000 (+0100) Subject: SONAR-7300 Replace key parameter by id X-Git-Tag: 6.3-RC1~441 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d652052d5e959577b590b91acf5a9f2e2ca5a6d8;p=sonarqube.git SONAR-7300 Replace key parameter by id Key parameter was badly used instead of id when rewritting api/properties to Java --- diff --git a/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java b/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java index cff8e72f41c..c0f431a7e45 100644 --- a/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java +++ b/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java @@ -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()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java index d8db9d0fb60..c4205ff3f54 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/property/ws/IndexAction.java @@ -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 component = loadComponent(dbSession, request); - String key = request.param(PARAM_KEY); + String key = request.param(PARAM_ID); List propertyDtos = loadProperties(dbSession, component, Optional.ofNullable(key)); new ResponseBuilder(propertyDtos).build(json); } finally { diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java b/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java index d4e106f1f97..c3749bd9dc7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java @@ -170,7 +170,7 @@ public class DeprecatedRestWebServiceFilter extends ServletFilter { } private void handleGet(Optional key, Optional 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"; diff --git a/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java index 119202861b8..d650f922628 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/property/ws/IndexActionTest.java @@ -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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java index 65cbf7f8c78..2d88112cb84 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java @@ -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"); }