import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
-import com.google.gson.JsonParseException;
+import com.google.gson.Gson;
+import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Collections;
private static Map<String, String> readOneFieldValues(String json, String key) {
Type type = new TypeToken<Map<String, String>>() {
}.getType();
+ Gson gson = GsonHelper.create();
try {
- return (Map<String, String>) GsonHelper.create().fromJson(json, type);
- } catch (JsonParseException e) {
- throw new BadRequestException(String.format("Invalid JSON '%s' for setting '%s'", json, key));
+ return (Map<String, String>) gson.fromJson(json, type);
+ } catch (JsonSyntaxException e) {
+ throw new BadRequestException(String.format("JSON '%s' does not respect expected format for setting '%s'. Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}", json, key));
}
}
.build());
expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("Invalid JSON 'incorrectJson:incorrectJson' for setting 'my.key'");
+ expectedException.expectMessage("JSON 'incorrectJson:incorrectJson' does not respect expected format for setting 'my.key'. " +
+ "Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}");
callForGlobalPropertySet("my.key", newArrayList("incorrectJson:incorrectJson"));
}
+ @Test
+ public void fail_when_property_set_with_json_of_the_wrong_format() {
+ propertyDefinitions.addComponent(PropertyDefinition
+ .builder("my.key")
+ .name("foo")
+ .description("desc")
+ .category("cat")
+ .subCategory("subCat")
+ .type(PropertyType.PROPERTY_SET)
+ .defaultValue("default")
+ .fields(newArrayList(
+ PropertyFieldDefinition.build("field")
+ .name("Field")
+ .type(PropertyType.STRING)
+ .build()))
+ .build());
+
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage("JSON '[{\"field\":\"v1\"}, {\"field\":\"v2\"}]' does not respect expected format for setting 'my.key'. " +
+ "Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}");
+
+ callForGlobalPropertySet("my.key", newArrayList("[{\"field\":\"v1\"}, {\"field\":\"v2\"}]"));
+ }
+
@Test
public void fail_when_property_set_on_component_of_global_setting() {
propertyDefinitions.addComponent(PropertyDefinition