From: Julien Lancelot Date: Thu, 8 Sep 2016 09:20:59 +0000 (+0200) Subject: SONAR-7986 Validation should not fail on hidden definitions X-Git-Tag: 6.1-RC1~181 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bf9c3dfba11f91d17ebe9934549dfadbcab66846;p=sonarqube.git SONAR-7986 Validation should not fail on hidden definitions --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidator.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidator.java index e0930cb112c..45ab4668434 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidator.java @@ -37,7 +37,8 @@ public class SettingValidator { } public static SettingValidation validateScope() { - return data -> checkRequest(data.component != null || data.definition == null || data.definition.global(), "Setting '%s' cannot be global", data.key); + return data -> checkRequest(data.component != null || data.definition == null || data.definition.global() || isGlobal(data.definition), + "Setting '%s' cannot be global", data.key); } public SettingValidation validateQualifier() { @@ -48,6 +49,10 @@ public class SettingValidator { }; } + private static boolean isGlobal(PropertyDefinition definition) { + return !definition.global() && definition.qualifiers().isEmpty(); + } + @FunctionalInterface public interface SettingValidation { void validate(SettingData data); diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java index f0b670b33c2..c97ae05cfe9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java @@ -27,7 +27,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.config.PropertyDefinitions; -import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; @@ -51,6 +50,8 @@ import org.sonarqube.ws.MediaTypes; import static java.net.HttpURLConnection.HTTP_NO_CONTENT; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.resources.Qualifiers.PROJECT; +import static org.sonar.api.resources.Qualifiers.VIEW; import static org.sonar.api.web.UserRole.ADMIN; import static org.sonar.api.web.UserRole.USER; import static org.sonar.core.permission.GlobalPermissions.DASHBOARD_SHARING; @@ -94,6 +95,16 @@ public class ResetActionTest { @Test public void remove_global_setting() throws Exception { + setUserAsSystemAdmin(); + definitions.addComponent(PropertyDefinition.builder("foo").build()); + propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one")); + + executeRequestOnGlobalSetting("foo"); + assertGlobalPropertyDoesNotExist("foo"); + } + + @Test + public void remove_global_setting_even_if_not_defined() throws Exception { setUserAsSystemAdmin(); propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one")); @@ -104,12 +115,32 @@ public class ResetActionTest { @Test public void remove_component_setting() throws Exception { setUserAsProjectAdmin(); + definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build()); propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value")); executeRequestOnProjectSetting("foo"); assertProjectPropertyDoesNotExist("foo"); } + @Test + public void remove_component_setting_even_if_not_defined() throws Exception { + setUserAsProjectAdmin(); + propertyDb.insertProperties(newComponentPropertyDto(project).setKey("foo").setValue("value")); + + executeRequestOnProjectSetting("foo"); + assertProjectPropertyDoesNotExist("foo"); + } + + @Test + public void remove_hidden_setting() throws Exception { + setUserAsSystemAdmin(); + definitions.addComponent(PropertyDefinition.builder("foo").hidden().build()); + propertyDb.insertProperties(newGlobalPropertyDto().setKey("foo").setValue("one")); + + executeRequestOnGlobalSetting("foo"); + assertGlobalPropertyDoesNotExist("foo"); + } + @Test public void ignore_project_setting_when_removing_global_setting() throws Exception { setUserAsSystemAdmin(); @@ -216,7 +247,7 @@ public class ResetActionTest { public void fail_when_not_global_and_no_component() { setUserAsSystemAdmin(); definitions.addComponent(PropertyDefinition.builder("foo") - .onlyOnQualifiers(Qualifiers.VIEW) + .onlyOnQualifiers(VIEW) .build()); expectedException.expect(BadRequestException.class); @@ -229,9 +260,21 @@ public class ResetActionTest { public void fail_when_qualifier_not_included() { setUserAsSystemAdmin(); definitions.addComponent(PropertyDefinition.builder("foo") - .onQualifiers(Qualifiers.VIEW) + .onQualifiers(VIEW) .build()); - i18n.put("qualifier." + Qualifiers.PROJECT, "project"); + i18n.put("qualifier." + PROJECT, "project"); + + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("Setting 'foo' cannot be set on a project"); + + executeRequestOnComponentSetting("foo", project); + } + + @Test + public void fail_to_reset_setting_component_when_setting_is_global() { + setUserAsSystemAdmin(); + definitions.addComponent(PropertyDefinition.builder("foo").build()); + i18n.put("qualifier." + PROJECT, "project"); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Setting 'foo' cannot be set on a project");