]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5356 Now fail when updating a custom rule with an empty name or description
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 Jun 2014 15:49:50 +0000 (17:49 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 Jun 2014 15:49:50 +0000 (17:49 +0200)
sonar-server/src/main/java/org/sonar/server/rule/ws/UpdateAction.java
sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/ws/UpdateActionMediumTest.java

index 77808f4d614942e40813a72a3b1b4c920a926431..cca752f610b3095f6f48eda13cd0371b5bbd3d45 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.rule.ws;
 
 import com.google.common.base.Splitter;
-import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.rule.RuleKey;
@@ -139,23 +138,23 @@ public class UpdateAction implements RequestHandler {
     readDebt(request, update);
 
     String name = request.param(PARAM_NAME);
-    if (!Strings.isNullOrEmpty(name)) {
+    if (name != null) {
       update.setName(name);
     }
     String description = request.param(PARAM_DESCRIPTION);
-    if (!Strings.isNullOrEmpty(description)) {
+    if (description != null) {
       update.setHtmlDescription(description);
     }
     String severity = request.param(PARAM_SEVERITY);
-    if (!Strings.isNullOrEmpty(severity)) {
+    if (severity != null) {
       update.setSeverity(severity);
     }
     String status = request.param(PARAM_STATUS);
-    if (!Strings.isNullOrEmpty(status)) {
+    if (status != null) {
       update.setStatus(RuleStatus.valueOf(status));
     }
     String params = request.param(PARAMS);
-    if (!Strings.isNullOrEmpty(params)) {
+    if (params != null) {
       update.setParameters(KeyValueFormat.parse(params));
     }
     return update;
index 82124c640c2b861aae0d43dfd28573c4291b6bd3..abeabff3753f2b96a95052739e07f5ab23bb0ae2 100644 (file)
@@ -425,6 +425,54 @@ public class RuleUpdaterMediumTest {
     assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER);
   }
 
+  @Test
+  public void fail_to_update_custom_rule_when_empty_name() throws Exception {
+    // Create template rule
+    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
+    ruleDao.insert(dbSession, templateRule);
+
+    // Create custom rule
+    RuleDto customRule = RuleTesting.newCustomRule(templateRule);
+    ruleDao.insert(dbSession, customRule);
+
+    dbSession.commit();
+
+    // Update custom rule
+    RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
+      .setName("")
+      .setHtmlDescription("New desc");
+    try {
+      updater.update(update, UserSession.get());
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The name is missing");
+    }
+  }
+
+  @Test
+  public void fail_to_update_custom_rule_when_empty_description() throws Exception {
+    // Create template rule
+    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
+    ruleDao.insert(dbSession, templateRule);
+
+    // Create custom rule
+    RuleDto customRule = RuleTesting.newCustomRule(templateRule);
+    ruleDao.insert(dbSession, customRule);
+
+    dbSession.commit();
+
+    // Update custom rule
+    RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
+      .setName("New name")
+      .setHtmlDescription("");
+    try {
+      updater.update(update, UserSession.get());
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The description is missing");
+    }
+  }
+
   @Test
   public void update_manual_rule() throws Exception {
     // Create manual rule
index 8d241c2b315e7cd770e1f7946869463d4f103c80..dfc5483a7f2c047315256c4cf8afec2050cf0fca 100644 (file)
@@ -43,6 +43,9 @@ import org.sonar.server.tester.ServerTester;
 import org.sonar.server.user.MockUserSession;
 import org.sonar.server.ws.WsTester;
 
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
 @RunWith(MockitoJUnitRunner.class)
 public class UpdateActionMediumTest {
 
@@ -101,4 +104,32 @@ public class UpdateActionMediumTest {
     request.execute().assertJson(getClass(), "update_custom_rule.json", false);
   }
 
+  @Test
+  public void fail_to_update_custom_when_description_is_empty() throws Exception {
+    MockUserSession.set()
+      .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN)
+      .setLogin("me");
+
+    // Template rule
+    RuleDto templateRule = ruleDao.insert(session, RuleTesting.newTemplateRule(RuleKey.of("java", "S001")));
+
+    // Custom rule
+    RuleDto customRule = RuleTesting.newCustomRule(templateRule);
+    ruleDao.insert(session, customRule);
+    session.commit();
+    session.clearCache();
+
+    WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "update")
+      .setParam("key", customRule.getKey().toString())
+      .setParam("name", "My custom rule")
+      .setParam("html_description", "");
+
+    try {
+      request.execute();
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The description is missing");
+    }
+  }
+
 }