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;
validateMetric(data);
} else if (definition.type() == PropertyType.USER_LOGIN) {
validateLogin(data);
+ } else if (definition.type() == PropertyType.JSON) {
+ validateJson(data);
} else {
validateOtherTypes(data, definition);
}
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");
+ }
+ }
+ }
}
}
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;
assertGlobalSetting("my.key", "My Value");
}
+ @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±∞…");
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.Properties;
import org.sonar.api.Property;
import org.sonar.api.PropertyField;
import org.sonar.api.PropertyType;
+import org.sonar.api.config.PropertyDefinition.Builder;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.AnnotationUtils;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
public class PropertyDefinitionTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
@Test
public void should_override_toString() {
assertThat(def.type()).isEqualTo(PropertyType.LICENSE);
}
+ @Test
+ public void should_create_json_property_type() {
+ Builder builder = PropertyDefinition.builder("json-prop").type(PropertyType.JSON).multiValues(false);
+ assertThatCode(builder::build)
+ .doesNotThrowAnyException();
+ }
+
@Test
public void should_not_authorise_empty_key() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Key must be set");
+ Builder builder = PropertyDefinition.builder(null);
+ assertThatThrownBy(builder::build)
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Key must be set");
+ }
- PropertyDefinition.builder(null).build();
+ @Test
+ public void should_not_create_json_multivalue() {
+ Builder builder = PropertyDefinition.builder("json-prop").type(PropertyType.JSON).multiValues(true);
+ assertThatThrownBy(builder::build)
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Multivalues are not allowed to be defined for JSON-type property.");
}
@Test
public void should_not_authorize_defining_on_qualifiers_and_hidden() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Cannot be hidden and defining qualifiers on which to display");
-
- PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.PROJECT).hidden().build();
+ Builder builder = PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.PROJECT).hidden();
+ assertThatThrownBy(builder::build)
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Cannot be hidden and defining qualifiers on which to display");
}
@Test
public void should_not_authorize_defining_ony_on_qualifiers_and_hidden() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Cannot be hidden and defining qualifiers on which to display");
-
- PropertyDefinition.builder("foo").name("foo").onlyOnQualifiers(Qualifiers.PROJECT).hidden().build();
+ Builder builder = PropertyDefinition.builder("foo").name("foo").onlyOnQualifiers(Qualifiers.PROJECT).hidden();
+ assertThatThrownBy(builder::build)
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Cannot be hidden and defining qualifiers on which to display");
}
@Test
public void should_not_authorize_defining_on_qualifiers_and_only_on_qualifiers() {
- thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("Cannot define both onQualifiers and onlyOnQualifiers");
-
- PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.MODULE).onlyOnQualifiers(Qualifiers.PROJECT).build();
+ Builder builder = PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.MODULE)
+ .onlyOnQualifiers(Qualifiers.PROJECT);
+ assertThatThrownBy(builder::build)
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Cannot define both onQualifiers and onlyOnQualifiers");
}
private static final Set<String> ALLOWED_QUALIFIERS = ImmutableSet.of("TRK", "VW", "BRC", "SVW");