@Test public void return_settings_definitions() { logIn(); propertyDefinitions.addComponent(PropertyDefinition .builder("foo") .name("Foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.TEXT) .defaultValue("default") .multiValues(true) .build()); ListDefinitionsWsResponse result = executeRequest(); assertThat(result.getDefinitionsList()).hasSize(1); Definition definition = result.getDefinitions(0); assertThat(definition.getKey()).isEqualTo("foo"); assertThat(definition.getName()).isEqualTo("Foo"); assertThat(definition.getDescription()).isEqualTo("desc"); assertThat(definition.getCategory()).isEqualTo("cat"); assertThat(definition.getSubCategory()).isEqualTo("subCat"); assertThat(definition.getType()).isEqualTo(TEXT); assertThat(definition.getDefaultValue()).isEqualTo("default"); assertThat(definition.getMultiValues()).isTrue(); }
@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_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 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_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_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_data_and_metric_type_with_invalid_key() { definitions.addComponent(PropertyDefinition .builder("my_key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.METRIC) .defaultValue("default") .multiValues(true) .build()); dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric_key")); dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric_disabled_key").setEnabled(false)); dbSession.commit(); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Error when validating metric setting with key 'my_key' and values [metric_key, metric_disabled_key]. A value is not a valid metric key."); callForMultiValueGlobalSetting("my_key", newArrayList("metric_key", "metric_disabled_key")); }
@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 fail_when_property_with_definition_when_component_qualifier_does_not_match() { ComponentDto project = randomPublicOrPrivateProject(); ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project)); definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.STRING) .defaultValue("default") .onQualifiers(Qualifiers.PROJECT) .build()); i18n.put("qualifier." + file.qualifier(), "CptLabel"); logInAsProjectAdministrator(project); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Setting 'my.key' cannot be set on a CptLabel"); callForProjectSettingByKey("my.key", "My Value", file.getDbKey()); }
@Test public void persist_multi_value_with_type_metric() { definitions.addComponent(PropertyDefinition .builder("my_key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.METRIC) .defaultValue("default") .multiValues(true) .build()); dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric_key_1")); dbClient.metricDao().insert(dbSession, newMetricDto().setKey("metric_key_2")); dbSession.commit(); callForMultiValueGlobalSetting("my_key", newArrayList("metric_key_1", "metric_key_2")); assertGlobalSetting("my_key", "metric_key_1,metric_key_2"); }
@Test public void fail_when_view_property_when_on_projects_only() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.STRING) .defaultValue("default") .onQualifiers(Qualifiers.PROJECT) .build()); ComponentDto view = db.components().insertComponent(newView(db.getDefaultOrganization(), "view-uuid")); i18n.put("qualifier." + Qualifiers.VIEW, "View"); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Setting 'my.key' cannot be set on a View"); logInAsProjectAdministrator(view); callForProjectSettingByKey("my.key", "My Value", view.getDbKey()); }
@Test public void fail_when_data_and_login_type_with_invalid_logIn() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.USER_LOGIN) .defaultValue("default") .multiValues(true) .build()); db.users().insertUser(newUserDto().setLogin("login.1")); db.users().insertUser(newUserDto().setLogin("login.2").setActive(false)); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Error when validating login setting with key 'my.key' and values [login.1, login.2]. A value is not a valid login."); callForMultiValueGlobalSetting("my.key", newArrayList("login.1", "login.2")); }
@Test public void persist_multi_value_with_type_logIn() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.USER_LOGIN) .defaultValue("default") .multiValues(true) .build()); db.users().insertUser(newUserDto().setLogin("login.1")); db.users().insertUser(newUserDto().setLogin("login.2")); callForMultiValueGlobalSetting("my.key", newArrayList("login.1", "login.2")); assertGlobalSetting("my.key", "login.1,login.2"); }
@Test public void fail_when_global_with_property_only_on_projects() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.INTEGER) .defaultValue("default") .onlyOnQualifiers(Qualifiers.PROJECT) .build()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Setting 'my.key' cannot be global"); callForGlobalSetting("my.key", "42"); }
@Test public void fail_when_data_and_type_do_not_match() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.INTEGER) .defaultValue("default") .build()); i18n.put("property.error.notInteger", "Not an integer error message"); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Not an integer error message"); callForGlobalSetting("my.key", "My Value"); }
@Test public void persist_global_property_with_deprecated_key() { definitions.addComponent(PropertyDefinition .builder("my.key") .deprecatedKey("my.old.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.STRING) .defaultValue("default") .build()); callForGlobalSetting("my.old.key", "My Value"); assertGlobalSetting("my.key", "My Value"); }
@Test public void fail_when_data_and_type_do_not_match_with_unknown_error_key() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .subCategory("subCat") .type(PropertyType.INTEGER) .defaultValue("default") .build()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Error when validating setting with key 'my.key' and value [My Value, My Other Value]"); callForMultiValueGlobalSetting("my.key", newArrayList("My Value", "My Other Value")); }
@Test public void should_group_by_subcategory() { PropertyDefinitions def = new PropertyDefinitions( PropertyDefinition.builder("global1").name("Global1").category("catGlobal1").subCategory("sub1").build(), PropertyDefinition.builder("global2").name("Global2").category("catGlobal1").subCategory("sub2").build(), PropertyDefinition.builder("global3").name("Global3").category("catGlobal1").build(), PropertyDefinition.builder("global4").name("Global4").category("catGlobal2").build()); assertThat(def.propertiesByCategory(null).get(new Category("catGlobal1")).keySet()).containsOnly(new SubCategory("catGlobal1"), new SubCategory("sub1"), new SubCategory("sub2")); assertThat(def.propertiesByCategory(null).get(new Category("catGlobal2")).keySet()).containsOnly(new SubCategory("catGlobal2")); }