diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-01-10 15:09:43 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-01-11 10:35:47 +0100 |
commit | f3020783d4aecd24160171f13c3cfa93bb46fe93 (patch) | |
tree | 3cfd9bbe01b14115f26d5ab5c765faffb713f03a /it | |
parent | 6a8201750d74dda13710b72364f2cb864d2d2eb9 (diff) | |
download | sonarqube-f3020783d4aecd24160171f13c3cfa93bb46fe93.tar.gz sonarqube-f3020783d4aecd24160171f13c3cfa93bb46fe93.zip |
SONAR-7300 Redirect PUT|POST|DELETE api/properties to api/settings
Diffstat (limited to 'it')
-rw-r--r-- | it/it-plugins/server-plugin/src/main/java/ServerPlugin.java | 1 | ||||
-rw-r--r-- | it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java | 82 |
2 files changed, 79 insertions, 4 deletions
diff --git a/it/it-plugins/server-plugin/src/main/java/ServerPlugin.java b/it/it-plugins/server-plugin/src/main/java/ServerPlugin.java index 9b7b820dbaa..9996c90804e 100644 --- a/it/it-plugins/server-plugin/src/main/java/ServerPlugin.java +++ b/it/it-plugins/server-plugin/src/main/java/ServerPlugin.java @@ -56,6 +56,7 @@ import static org.sonar.api.PropertyType.USER_LOGIN; @Property(key = "text", name = "Text", type = TEXT, global = true, project = false), @Property(key = "multi", name = "Multi", type = STRING, multiValues = true, global = true, project = false), @Property(key = "hidden", name = "Hidden", type = STRING, global = false, project = false), + @Property(key = "project.setting", name = "Project setting", type = STRING, global = false, project = true), @Property(key = "setting.secured", name = "Secured", type = STRING, global = true, project = false), @Property(key = "sonar.jira", name = "Jira Server", type = PROPERTY_SET, propertySetKey = "jira", fields = { @PropertyField(key = "key", name = "Key", description = "Server key"), 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 efe590f63a9..80deea8e407 100644 --- a/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java +++ b/it/it-tests/src/test/java/it/settings/DeprecatedPropertiesWsTest.java @@ -20,13 +20,22 @@ package it.settings; +import com.google.common.base.Throwables; import com.google.gson.Gson; import com.sonar.orchestrator.Orchestrator; import it.Category1Suite; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import okhttp3.Credentials; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -93,8 +102,8 @@ public class DeprecatedPropertiesWsTest { } private static void doResetSettings() { - resetSettings(orchestrator, null, "some-property", "int", "multi", "boolean", "hidden", "not_defined", "setting.secured", "setting.license.secured", "list"); - resetSettings(orchestrator, PROJECT_KEY, PROJECT_SETTING_KEY, "sonar.coverage.exclusions"); + resetSettings(orchestrator, null, "some-property", "custom-property", "int", "multi", "boolean", "hidden", "not_defined", "setting.secured", "setting.license.secured", "list"); + resetSettings(orchestrator, PROJECT_KEY, PROJECT_SETTING_KEY, "sonar.coverage.exclusions", "project.setting"); } @Test @@ -167,7 +176,7 @@ public class DeprecatedPropertiesWsTest { assertThat(getProperties(userWsClient, null)).extracting(Properties.Property::getKey).contains("setting.license.secured"); // Anonymous cannot see the license setting - assertThat(getProperties(anonymousWsClient, null)).extracting(Properties.Property::getKey).doesNotContain("setting.license.secured"); + assertThat(getProperties(anonymousWsClient, null)).extracting(Properties.Property::getKey).doesNotContain("setting.license.secured"); } @Test @@ -212,6 +221,38 @@ public class DeprecatedPropertiesWsTest { .doesNotContain("hidden"); } + @Test + public void put_property() throws Exception { + putProperty("some-property", "some-value", null); + + 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); + + assertThat(getProperty("project.setting", PROJECT_KEY).getValue()).isEqualTo("some-value"); + } + + @Test + public void delete_property() throws Exception { + setProperty("custom-property", "value", null); + + deleteProperty("custom-property", null); + + assertThat(getProperty("custom-property", null)).isNull(); + } + + @Test + public void delete_property_on_project() throws Exception { + setProperty("project.setting", "value", PROJECT_KEY); + + deleteProperty("project.setting", PROJECT_KEY); + + assertThat(getProperty("project.setting", PROJECT_KEY)).isNull(); + } + private static void setProperty(String key, String value, @Nullable String componentKey) { adminSettingsService.set(SetRequest.builder().setKey(key).setValue(value).setComponent(componentKey).build()); } @@ -236,13 +277,46 @@ public class DeprecatedPropertiesWsTest { return getProperty(adminWsClient, key, componentKey); } + @CheckForNull private static Properties.Property getProperty(WsClient wsClient, String key, @Nullable String componentKey) throws UnsupportedEncodingException { WsResponse response = wsClient.wsConnector() .call(new GetRequest("api/properties/" + encode(key, "UTF-8")) .setParam("resource", componentKey)) .failIfNotSuccessful(); Properties.Property[] properties = Properties.parse(response.content()); - return Arrays.stream(properties).findFirst().orElseThrow(() -> new IllegalArgumentException("Property does not exist : " + key)); + 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; + 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"); + url += componentKey != null ? "?resource=" + componentKey : ""; + call(new Request.Builder() + .delete(new FormBody.Builder().build()) + .url(url)); + } + + public static Response call(Request.Builder requestBuilder) { + try { + requestBuilder.header("Authorization", Credentials.basic("admin", "admin")); + Response response = new OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .build() + .newCall(requestBuilder.build()) + .execute(); + assertThat(response.isSuccessful()).as("Error code is '%s', error message is '%s'", Integer.toString(response.code()), response.body().string()).isTrue(); + return response; + } catch (IOException e) { + throw Throwables.propagate(e); + } } public static class Properties { |