@Test public void set_property_string_array_trims_key() { String key = randomAlphanumeric(3); Settings underTest = new MapSettings(new PropertyDefinitions(singletonList(PropertyDefinition.builder(key).multiValues(true).build()))); Random random = new Random(); String blankBefore = blank(random); String blankAfter = blank(random); underTest.setProperty(blankBefore + key + blankAfter, new String[] {"1", "2"}); assertThat(underTest.hasKey(key)).isTrue(); }
@Test public void return_multi_value_with_coma() { logIn(); definitions.addComponent(PropertyDefinition.builder("global").multiValues(true).build()); propertyDb.insertProperties(newGlobalPropertyDto().setKey("global").setValue("three,four%2Cfive")); ValuesWsResponse result = executeRequestForGlobalProperties("global"); assertThat(result.getSettingsList()).hasSize(1); Settings.Setting setting = result.getSettings(0); assertThat(setting.getKey()).isEqualTo("global"); assertThat(setting.getValues().getValuesList()).containsOnly("three", "four,five"); }
@Test public void childSettings_should_retrieve_parent_settings() { String multipleValuesKey = randomAlphanumeric(19); PropertyDefinition multipleValues = PropertyDefinition.builder(multipleValuesKey).multiValues(true).build(); MapSettings parent = new MapSettings(new PropertyDefinitions(Collections.singletonList(multipleValues))); ChildSettings underTest = new ChildSettings(parent); parent.setProperty(randomAlphanumeric(10), randomAlphanumeric(20)); parent.setProperty(randomAlphanumeric(11), RANDOM.nextLong()); parent.setProperty(randomAlphanumeric(12), RANDOM.nextDouble()); parent.setProperty(randomAlphanumeric(13), RANDOM.nextFloat()); parent.setProperty(randomAlphanumeric(14), RANDOM.nextBoolean()); parent.setProperty(randomAlphanumeric(15), RANDOM.nextInt()); parent.setProperty(randomAlphanumeric(16), new Date(RANDOM.nextInt())); parent.setProperty(randomAlphanumeric(17), new Date(RANDOM.nextInt()), true); parent.setProperty(randomAlphanumeric(18), new Date(RANDOM.nextInt()), false); parent.setProperty(multipleValuesKey, new String[] { randomAlphanumeric(10), randomAlphanumeric(20) }); assertThat(underTest.getProperties()).isEqualTo(parent.getProperties()); }
@Test public void return_multi_values() { logIn(); // Property never defined, default value is returned definitions.addComponent(PropertyDefinition.builder("default") .multiValues(true) .defaultValue("one,two") .build()); // Property defined at global level definitions.addComponent(PropertyDefinition.builder("global") .multiValues(true) .build()); propertyDb.insertProperties(newGlobalPropertyDto().setKey("global").setValue("three,four")); ValuesWsResponse result = executeRequestForGlobalProperties("default", "global"); assertThat(result.getSettingsList()).hasSize(2); Settings.Setting foo = result.getSettings(0); assertThat(foo.getKey()).isEqualTo("default"); assertThat(foo.getValues().getValuesList()).containsOnly("one", "two"); Settings.Setting bar = result.getSettings(1); assertThat(bar.getKey()).isEqualTo("global"); assertThat(bar.getValues().getValuesList()).containsOnly("three", "four"); }
@Test public void accessingMultiValuedPropertiesShouldBeConsistentWithDeclaration() { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("single").multiValues(false).build(), PropertyDefinition.builder("multiA").multiValues(true).build())), new Encryption(null), ImmutableMap.of("single", "foo", "multiA", "a,b", "notDeclared", "c,d")) { }; assertThat(config.get("multiA")).hasValue("a,b"); assertThat(logTester.logs(LoggerLevel.WARN)) .contains( "Access to the multi-values/property set property 'multiA' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated."); logTester.clear(); assertThat(config.getStringArray("single")).containsExactly("foo"); assertThat(logTester.logs(LoggerLevel.WARN)) .contains( "Property 'single' is not declared as multi-values/property set but was read using 'getStringArray' method. The SonarQube plugin declaring this property should be updated."); logTester.clear(); assertThat(config.get("notDeclared")).hasValue("c,d"); assertThat(config.getStringArray("notDeclared")).containsExactly("c", "d"); assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty(); }
@Test public void test_example_json_response() { logIn(); definitions.addComponent(PropertyDefinition .builder("sonar.test.jira") .defaultValue("abc") .build()); definitions.addComponent(PropertyDefinition .builder("sonar.autogenerated") .multiValues(true) .build()); propertyDb.insertProperties(newGlobalPropertyDto().setKey("sonar.autogenerated").setValue("val1,val2,val3")); definitions.addComponent(PropertyDefinition .builder("sonar.demo") .type(PropertyType.PROPERTY_SET) .fields(PropertyFieldDefinition.build("text").name("Text").build(), PropertyFieldDefinition.build("boolean").name("Boolean").build()) .build()); propertyDb.insertPropertySet("sonar.demo", null, ImmutableMap.of("text", "foo", "boolean", "true"), ImmutableMap.of("text", "bar", "boolean", "false")); String result = ws.newRequest().setMediaType(JSON).execute().getInput(); JsonAssert.assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result); }
@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 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_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 return_parent_values() { logInAsProjectUser(); ComponentDto module = componentDb.insertComponent(newModuleDto(project)); ComponentDto subModule = componentDb.insertComponent(newModuleDto(module)); definitions.addComponents(asList( PropertyDefinition.builder("foo").defaultValue("default1,default2").multiValues(true).onQualifiers(PROJECT, MODULE).build())); propertyDb.insertProperties( newGlobalPropertyDto().setKey("foo").setValue("global1,global2"), newComponentPropertyDto(project).setKey("foo").setValue("project1,project2"), newComponentPropertyDto(module).setKey("foo").setValue("module1,module2")); assertParentValues(executeRequestForComponentProperties(subModule, "foo").getSettings(0), "module1", "module2"); assertParentValues(executeRequestForComponentProperties(module, "foo").getSettings(0), "project1", "project2"); assertParentValues(executeRequestForComponentProperties(project, "foo").getSettings(0), "global1", "global2"); assertParentValues(executeRequestForGlobalProperties("foo").getSettings(0), "default1", "default2"); }
@Test public void return_multi_values() { logIn(); // Property never defined, default value is returned definitions.addComponent(PropertyDefinition.builder("default") .multiValues(true) .defaultValue("one,two") .build()); // Property defined at global level definitions.addComponent(PropertyDefinition.builder("global") .multiValues(true) .build()); propertyDb.insertProperties(newGlobalPropertyDto().setKey("global").setValue("three,four")); executeAndVerify(null, null, "return_multi_values.json"); }
@Test public void fail_when_multi_definition_and_single_value_provided() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .type(PropertyType.STRING) .multiValues(true) .build()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Parameter 'value' must be used for single value setting. Parameter 'values' must be used for multi value setting."); callForGlobalSetting("my.key", "My Value"); }
@Test public void fail_when_single_definition_and_multi_value_provided() { definitions.addComponent(PropertyDefinition .builder("my.key") .name("foo") .description("desc") .category("cat") .type(PropertyType.STRING) .multiValues(false) .build()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Parameter 'value' must be used for single value setting. Parameter 'values' must be used for multi value setting."); callForMultiValueGlobalSetting("my.key", newArrayList("My Value")); }
public static ImmutableList<PropertyDefinition> properties() { return ImmutableList.of( PropertyDefinition.builder(REPORT_PATHS_PROPERTY_KEY) .name("Coverage report paths") .description("List of comma-separated paths (absolute or relative) containing coverage report.") .category(CATEGORY_CODE_COVERAGE) .onQualifiers(Qualifiers.PROJECT) .multiValues(true) .deprecatedKey(OLD_COVERAGE_REPORT_PATHS_PROPERTY_KEY) .build()); }
@Test public void set_property_string_throws_NPE_if_key_is_null() { String key = randomAlphanumeric(3); Settings underTest = new MapSettings(new PropertyDefinitions(singletonList(PropertyDefinition.builder(key).multiValues(true).build()))); expectKeyNullNPE(); underTest.setProperty(null, new String[] {"1", "2"}); }
public static ImmutableList<PropertyDefinition> properties() { return ImmutableList.of( PropertyDefinition.builder(REPORT_PATHS_PROPERTY_KEY) .name("Unit tests results report paths") .description("List of comma-separated paths (absolute or relative) containing unit tests results report.") .category(CATEGORY_CODE_COVERAGE) .onQualifiers(Qualifiers.PROJECT) .multiValues(true) .deprecatedKey(OLD_UNIT_TEST_REPORT_PATHS_PROPERTY_KEY) .build()); }
@Test public void return_multi_value_with_coma() { logIn(); definitions.addComponent(PropertyDefinition.builder("global").multiValues(true).build()); propertyDb.insertProperties(newGlobalPropertyDto().setKey("global").setValue("three,four%2Cfive")); executeAndVerify(null, null, "return_multi_value_with_coma.json"); }
@Test public void getDefaultValues() { Configuration config = new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("single").multiValues(false).defaultValue("default").build(), PropertyDefinition.builder("multiA").multiValues(true).defaultValue("foo,bar").build())), new Encryption(null), ImmutableMap.of()) { }; assertThat(config.get("multiA")).hasValue("foo,bar"); assertThat(config.getStringArray("multiA")).containsExactly("foo", "bar"); assertThat(config.get("single")).hasValue("default"); assertThat(config.getStringArray("single")).containsExactly("default"); }
private String[] getStringArray(String value) { return new DefaultConfiguration(new PropertyDefinitions(Arrays.asList( PropertyDefinition.builder("multi").multiValues(true).build())), new Encryption(null), ImmutableMap.of("multi", value)) { }.getStringArray("multi"); } }