@Test public void should_support_property_sets() { PropertyDefinition def = PropertyDefinition.builder("hello") .name("Hello") .fields( PropertyFieldDefinition.build("first").name("First").description("Description").options("A", "B").build(), PropertyFieldDefinition.build("second").name("Second").type(PropertyType.INTEGER).indicativeSize(5).build()) .build(); assertThat(def.type()).isEqualTo(PropertyType.PROPERTY_SET); assertThat(def.fields()).hasSize(2); assertThat(def.fields().get(0).key()).isEqualTo("first"); assertThat(def.fields().get(0).name()).isEqualTo("First"); assertThat(def.fields().get(0).description()).isEqualTo("Description"); assertThat(def.fields().get(0).type()).isEqualTo(PropertyType.STRING); assertThat(def.fields().get(0).options()).containsOnly("A", "B"); assertThat(def.fields().get(0).indicativeSize()).isEqualTo(20); assertThat(def.fields().get(1).key()).isEqualTo("second"); assertThat(def.fields().get(1).name()).isEqualTo("Second"); assertThat(def.fields().get(1).type()).isEqualTo(PropertyType.INTEGER); assertThat(def.fields().get(1).options()).isEmpty(); assertThat(def.fields().get(1).indicativeSize()).isEqualTo(5); }
@Test public void return_property_set() { logIn(); propertyDefinitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields( PropertyFieldDefinition.build("boolean").name("Boolean").description("boolean desc").type(PropertyType.BOOLEAN).build(), PropertyFieldDefinition.build("list").name("List").description("list desc").type(PropertyType.SINGLE_SELECT_LIST).options("one", "two").build()) .build()); ListDefinitionsWsResponse result = executeRequest(); assertThat(result.getDefinitionsList()).hasSize(1); Definition definition = result.getDefinitions(0); assertThat(definition.getType()).isEqualTo(PROPERTY_SET); assertThat(definition.getFieldsList()).hasSize(2); assertThat(definition.getFields(0).getKey()).isEqualTo("boolean"); assertThat(definition.getFields(0).getName()).isEqualTo("Boolean"); assertThat(definition.getFields(0).getDescription()).isEqualTo("boolean desc"); assertThat(definition.getFields(0).getType()).isEqualTo(BOOLEAN); assertThat(definition.getFields(0).getOptionsCount()).isZero(); assertThat(definition.getFields(1).getKey()).isEqualTo("list"); assertThat(definition.getFields(1).getName()).isEqualTo("List"); assertThat(definition.getFields(1).getDescription()).isEqualTo("list desc"); assertThat(definition.getFields(1).getType()).isEqualTo(SINGLE_SELECT_LIST); assertThat(definition.getFields(1).getOptionsList()).containsExactly("one", "two"); }
static List<PropertyFieldDefinition> create(PropertyField[] fields) { List<PropertyFieldDefinition> definitions = new ArrayList<>(); for (PropertyField field : fields) { definitions.add(PropertyFieldDefinition.build(field.key()) .name(field.name()) .description(field.description()) .indicativeSize(field.indicativeSize()) .type(field.type()) .options(field.options()) .build()); } return definitions; }
.name("Language Key") .description("Ex: java, cs, cpp...") .type(PropertyType.STRING) .build(), PropertyFieldDefinition.build(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY) .name("Development cost") .description("If left blank, the generic value defined in this section will be used.") .type(PropertyType.FLOAT) .build()) .build());
@Test public void fail_when_empty_values_on_one_property_set() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.PROPERTY_SET) .defaultValue("default") .fields(newArrayList( PropertyFieldDefinition.build("firstField") .name("First Field") .type(PropertyType.STRING) .build(), PropertyFieldDefinition.build("secondField") .name("Second Field") .type(PropertyType.STRING) .build())) .build()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("A non empty value must be provided"); callForGlobalPropertySet("my.key", newArrayList( GSON.toJson(ImmutableMap.of("firstField", "firstValue", "secondField", "secondValue")), GSON.toJson(ImmutableMap.of("firstField", "", "secondField", "")), GSON.toJson(ImmutableMap.of("firstField", "yetFirstValue", "secondField", "yetSecondValue")))); }
@Test public void do_not_fail_when_only_one_empty_value_on_one_property_set() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.PROPERTY_SET) .defaultValue("default") .fields(newArrayList( PropertyFieldDefinition.build("firstField") .name("First Field") .type(PropertyType.STRING) .build(), PropertyFieldDefinition.build("secondField") .name("Second Field") .type(PropertyType.STRING) .build())) .build()); callForGlobalPropertySet("my.key", newArrayList( GSON.toJson(ImmutableMap.of("firstField", "firstValue", "secondField", "secondValue")), GSON.toJson(ImmutableMap.of("firstField", "anotherFirstValue", "secondField", "")), GSON.toJson(ImmutableMap.of("firstField", "yetFirstValue", "secondField", "yetSecondValue")))); assertGlobalSetting("my.key", "1,2,3"); }
@Test public void fail_when_property_set_with_invalid_json() { definitions.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 '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_on_component_of_global_setting() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.PROPERTY_SET) .defaultValue("default") .fields(newArrayList(PropertyFieldDefinition.build("firstField").name("First Field").type(PropertyType.STRING).build())) .build()); i18n.put("qualifier." + Qualifiers.PROJECT, "Project"); ComponentDto project = db.components().insertPrivateProject(); logInAsProjectAdministrator(project); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Setting 'my.key' cannot be set on a Project"); callForComponentPropertySet("my.key", newArrayList( GSON.toJson(ImmutableMap.of("firstField", "firstValue"))), project.getDbKey()); }
@Test public void fail_when_property_set_has_a_null_field_value() { definitions.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("A non empty value must be provided"); callForGlobalPropertySet("my.key", newArrayList("{\"field\": null}")); }
@Test public void fail_when_property_set_with_unknown_field() { definitions.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("Unknown field key 'unknownField' for setting 'my.key'"); callForGlobalPropertySet("my.key", newArrayList(GSON.toJson(ImmutableMap.of("field", "value", "unknownField", "anotherValue")))); }
@Test public void fail_when_property_set_with_json_of_the_wrong_format() { definitions.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_has_field_with_incorrect_type() { definitions.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.INTEGER) .build())) .build()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Error when validating setting with key 'my.key'. Field 'field' has incorrect value 'notAnInt'."); callForGlobalPropertySet("my.key", newArrayList(GSON.toJson(ImmutableMap.of("field", "notAnInt")))); }
@Test public void return_secured_and_license_settings_in_property_set_when_system_admin() { logInAsAdmin(); definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("plugin.license.secured").name("License").type(LICENSE).build(), PropertyFieldDefinition.build("secret.secured").name("Secured").build())) .build()); propertyDb.insertPropertySet("foo", null, ImmutableMap.of("key", "key1", "plugin.license.secured", "ABCD", "secret.secured", "123456")); ValuesWsResponse result = executeRequestForGlobalProperties(); assertFieldValues(result.getSettings(0), ImmutableMap.of("key", "key1", "plugin.license.secured", "ABCD", "secret.secured", "123456")); }
@Test public void do_not_return_secured_and_license_settings_in_property_set_when_not_authenticated() { definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("plugin.license.secured").name("License").type(LICENSE).build(), PropertyFieldDefinition.build("secret.secured").name("Secured").build())) .build()); propertyDb.insertPropertySet("foo", null, ImmutableMap.of("key", "key1", "plugin.license.secured", "ABCD", "secret.secured", "123456")); ValuesWsResponse result = executeRequestForGlobalProperties(); assertFieldValues(result.getSettings(0), ImmutableMap.of("key", "key1")); }
@Test public void does_not_returned_secured_and_license_settings_in_property_set_when_not_authenticated() { definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("plugin.license.secured").name("License").type(LICENSE).build(), PropertyFieldDefinition.build("secret.secured").name("Secured").build())) .build()); propertyDb.insertPropertySet("foo", null, ImmutableMap.of("key", "key1", "plugin.license.secured", "ABCD", "secret.secured", "123456")); executeAndVerify(null, null, "does_not_returned_secured_and_license_settings_in_property_set_when_not_authenticated.json"); }
@Test public void return_secured_and_license_settings_in_property_set_when_system_admin() { logInAsSystemAdministrator(); definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("plugin.license.secured").name("License").type(LICENSE).build(), PropertyFieldDefinition.build("secret.secured").name("Secured").build())) .build()); propertyDb.insertPropertySet("foo", null, ImmutableMap.of("key", "key1", "plugin.license.secured", "ABCD", "secret.secured", "123456")); executeAndVerify(null, null, "return_secured_and_license_settings_in_property_set_when_system_admin.json"); }
@Test public void return_license_type_in_property_set() { logIn(); propertyDefinitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(PropertyFieldDefinition.build("license").name("License").type(PropertyType.LICENSE).build()) .build()); ListDefinitionsWsResponse result = executeRequest(); assertThat(result.getDefinitionsList()).hasSize(1); assertThat(result.getDefinitions(0).getFieldsList()).extracting(Settings.Field::getKey, Settings.Field::getType).containsOnly(tuple("license", LICENSE)); }