]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7986 Validation should not fail on hidden definitions 1223/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 8 Sep 2016 09:20:59 +0000 (11:20 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 8 Sep 2016 09:54:30 +0000 (11:54 +0200)
server/sonar-server/src/main/java/org/sonar/server/setting/ws/SettingValidator.java
server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java

index e0930cb112c1224e2b241990868c77349b75dcfa..45ab4668434153ca7329b121d3bc6503d0da9bae 100644 (file)
@@ -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);
index f0b670b33c25e5ce4efce0c527294ae359d1f089..c97ae05cfe934710b8b2d9790d75da7515d518b6 100644 (file)
@@ -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");