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; }
@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(); definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("size").name("Size").build())) .build()); propertyDb.insertPropertySet("foo", null, ImmutableMap.of("key", "key1", "size", "size1"), ImmutableMap.of("key", "key2")); ValuesWsResponse result = executeRequestForGlobalProperties("foo"); assertThat(result.getSettingsList()).hasSize(1); Settings.Setting value = result.getSettings(0); assertThat(value.getKey()).isEqualTo("foo"); assertFieldValues(value, ImmutableMap.of("key", "key1", "size", "size1"), ImmutableMap.of("key", "key2")); }
@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_property_set() { logIn(); definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("size").name("Size").build())) .build()); propertyDb.insertPropertySet("foo", null, ImmutableMap.of("key", "key1", "size", "size1"), ImmutableMap.of("key", "key2")); executeAndVerify(null, null, "return_property_set.json"); }
@Test public void return_property_set_for_component() { logInAsProjectUser(); definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .onQualifiers(PROJECT) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("size").name("Size").build())) .build()); propertyDb.insertPropertySet("foo", project, ImmutableMap.of("key", "key1", "size", "size1"), ImmutableMap.of("key", "key2")); ValuesWsResponse result = executeRequestForProjectProperties("foo"); assertThat(result.getSettingsList()).hasSize(1); Settings.Setting value = result.getSettings(0); assertThat(value.getKey()).isEqualTo("foo"); assertFieldValues(value, ImmutableMap.of("key", "key1", "size", "size1"), ImmutableMap.of("key", "key2")); }
@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 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_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 accessingPropertySetPropertiesShouldBeConsistentWithDeclaration() { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("props").fields(PropertyFieldDefinition.build("foo1").name("Foo1").build(), PropertyFieldDefinition.build("foo2").name("Foo2").build()).build())), new Encryption(null), ImmutableMap.of("props", "1,2", "props.1.foo1", "a", "props.1.foo2", "b")) { }; assertThat(config.get("props")).hasValue("1,2"); assertThat(logTester.logs(LoggerLevel.WARN)) .contains( "Access to the multi-values/property set property 'props' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated."); logTester.clear(); assertThat(config.getStringArray("props")).containsExactly("1", "2"); assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty(); }
@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)); }
@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_parent_field_values() { logInAsProjectUser(); ComponentDto module = componentDb.insertComponent(newModuleDto(project)); ComponentDto subModule = componentDb.insertComponent(newModuleDto(module)); definitions.addComponent(PropertyDefinition .builder("foo") .onQualifiers(PROJECT, MODULE) .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("size").name("Size").build())) .build()); propertyDb.insertPropertySet("foo", null, ImmutableMap.of("key", "keyG1", "size", "sizeG1")); propertyDb.insertPropertySet("foo", project, ImmutableMap.of("key", "keyP1", "size", "sizeP1")); propertyDb.insertPropertySet("foo", module, ImmutableMap.of("key", "keyM1", "size", "sizeM1")); assertParentFieldValues(executeRequestForComponentProperties(subModule, "foo").getSettings(0), ImmutableMap.of("key", "keyM1", "size", "sizeM1")); assertParentFieldValues(executeRequestForComponentProperties(module, "foo").getSettings(0), ImmutableMap.of("key", "keyP1", "size", "sizeP1")); assertParentFieldValues(executeRequestForComponentProperties(project, "foo").getSettings(0), ImmutableMap.of("key", "keyG1", "size", "sizeG1")); assertParentFieldValues(executeRequestForGlobalProperties("foo").getSettings(0)); }
@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 does_not_fail_when_deleting_unknown_property_set() { definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("size").name("Size").build())) .build()); propertyDb.insertProperties( newComponentPropertyDto(project).setKey("other").setValue("1,2"), newComponentPropertyDto(project).setKey("other.1.key").setValue("key1")); underTest.deleteComponentSettings(dbSession, project, "foo"); assertProjectPropertyExists("other"); }
@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_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_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 delete_global_property_set() { definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("size").name("Size").build())) .build()); propertyDb.insertProperties( newGlobalPropertyDto().setKey("foo").setValue("1,2"), newGlobalPropertyDto().setKey("foo.1.key").setValue("key1"), newGlobalPropertyDto().setKey("foo.1.size").setValue("size1"), newGlobalPropertyDto().setKey("foo.2.key").setValue("key2")); underTest.deleteGlobalSettings(dbSession, "foo"); assertGlobalPropertyDoesNotExist("foo"); assertGlobalPropertyDoesNotExist("foo.1.key"); assertGlobalPropertyDoesNotExist("foo.1.size"); assertGlobalPropertyDoesNotExist("foo.2.key"); }
@Test public void delete_component_property_set() { definitions.addComponent(PropertyDefinition .builder("foo") .type(PropertyType.PROPERTY_SET) .fields(asList( PropertyFieldDefinition.build("key").name("Key").build(), PropertyFieldDefinition.build("size").name("Size").build())) .build()); propertyDb.insertProperties( newComponentPropertyDto(project).setKey("foo").setValue("1,2"), newComponentPropertyDto(project).setKey("foo.1.key").setValue("key1"), newComponentPropertyDto(project).setKey("foo.1.size").setValue("size1"), newComponentPropertyDto(project).setKey("foo.2.key").setValue("key2")); underTest.deleteComponentSettings(dbSession, project, "foo"); assertProjectPropertyDoesNotExist("foo"); assertProjectPropertyDoesNotExist("foo.1.key"); assertProjectPropertyDoesNotExist("foo.1.size"); assertProjectPropertyDoesNotExist("foo.2.key"); }