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;
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;
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
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 {
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");
+ }
+ }
+
}