diff options
author | Jacek <jacek.poreda@sonarsource.com> | 2021-02-19 16:20:34 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-02-26 20:07:39 +0000 |
commit | 7657870eed718a74c6b4e8dbd18ab6f8d5d5bc78 (patch) | |
tree | ed8eecb242a735063065f8533d589dc4e39166f6 /server | |
parent | 9cb17b6dbce261af578b7c5fe430fa340d4ff1ad (diff) | |
download | sonarqube-7657870eed718a74c6b4e8dbd18ab6f8d5d5bc78.tar.gz sonarqube-7657870eed718a74c6b4e8dbd18ab6f8d5d5bc78.zip |
SONAR-14498 Support JSON property type
Diffstat (limited to 'server')
3 files changed, 73 insertions, 0 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java index 774a7e3ffcf..bf2ce289c4c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java @@ -20,8 +20,12 @@ package org.sonar.server.setting.ws; import com.google.common.collect.ImmutableSet; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import java.io.IOException; import java.util.List; import java.util.Locale; +import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -121,6 +125,8 @@ public class SettingValidations { validateMetric(data); } else if (definition.type() == PropertyType.USER_LOGIN) { validateLogin(data); + } else if (definition.type() == PropertyType.JSON) { + validateJson(data); } else { validateOtherTypes(data, definition); } @@ -152,5 +158,16 @@ public class SettingValidations { data.key, data.values.stream().collect(Collectors.joining(", "))); } } + + private void validateJson(SettingData data) { + Optional<String> jsonContent = data.values.stream().findFirst(); + if (jsonContent.isPresent()) { + try { + new Gson().getAdapter(JsonElement.class).fromJson(jsonContent.get()); + } catch (IOException e) { + throw new IllegalArgumentException("Provided JSON is invalid"); + } + } + } } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ListDefinitionsActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ListDefinitionsActionTest.java index c5c287b4079..b1009264049 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ListDefinitionsActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/ListDefinitionsActionTest.java @@ -192,6 +192,21 @@ public class ListDefinitionsActionTest { } @Test + public void return_JSON_property() { + logIn(); + propertyDefinitions.addComponent(PropertyDefinition + .builder("foo") + .type(PropertyType.JSON) + .build()); + + ListDefinitionsWsResponse result = executeRequest(); + + assertThat(result.getDefinitionsList()).hasSize(1); + Definition definition = result.getDefinitions(0); + assertThat(definition.getType()).isEqualTo(Settings.Type.JSON); + } + + @Test public void return_property_set() { logIn(); propertyDefinitions.addComponent(PropertyDefinition diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SetActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SetActionTest.java index f2004c3bfbc..7f9e3adbb14 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SetActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/setting/ws/SetActionTest.java @@ -64,6 +64,7 @@ import static com.google.common.collect.Lists.newArrayList; import static java.lang.String.format; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.groups.Tuple.tuple; import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.db.metric.MetricTesting.newMetricDto; @@ -389,6 +390,46 @@ public class SetActionTest { } @Test + public void persist_JSON_property() { + definitions.addComponent(PropertyDefinition + .builder("my.key") + .name("foo") + .description("desc") + .category("cat") + .subCategory("subCat") + .type(PropertyType.JSON) + .build()); + + callForGlobalSetting("my.key", "{\"test\":\"value\"}"); + + assertGlobalSetting("my.key", "{\"test\":\"value\"}"); + } + + @Test + public void fail_if_JSON_invalid_for_JSON_property() { + definitions.addComponent(PropertyDefinition + .builder("my.key") + .name("foo") + .description("desc") + .category("cat") + .subCategory("subCat") + .type(PropertyType.JSON) + .build()); + + assertThatThrownBy(() -> callForGlobalSetting("my.key", "{\"test\":\"value\"")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Provided JSON is invalid"); + + assertThatThrownBy(() -> callForGlobalSetting("my.key", "{\"test\":\"value\",}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Provided JSON is invalid"); + + assertThatThrownBy(() -> callForGlobalSetting("my.key", "{\"test\":[\"value\",]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Provided JSON is invalid"); + } + + @Test public void persist_global_setting_with_non_ascii_characters() { callForGlobalSetting("my.key", "fi±∞…"); |