From 66def29047707ed251bdb36423296302ea7885ba Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 6 Feb 2014 13:37:29 +0100 Subject: [PATCH] When creating a manual issue, if the message is empty, use the rule name instead --- .../org/sonar/server/issue/IssueService.java | 11 ++++---- .../sonar/server/issue/IssueServiceTest.java | 28 ++++++++++++++----- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java b/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java index fdf674289f3..cca54668f0b 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java @@ -47,11 +47,7 @@ import org.sonar.server.user.UserSession; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.List; +import java.util.*; /** * @since 3.6 @@ -218,7 +214,7 @@ public class IssueService implements ServerComponent { // TODO throw unauthorized throw new IllegalStateException("User does not have the required role"); } - if (!"manual".equals(issue.ruleKey().repository())) { + if (!org.sonar.server.rule.Rule.MANUAL_REPOSITORY_KEY.equals(issue.ruleKey().repository())) { throw new IllegalArgumentException("Issues can be created only on rules marked as 'manual': " + issue.ruleKey()); } @@ -226,6 +222,9 @@ public class IssueService implements ServerComponent { if (rule == null) { throw new IllegalArgumentException("Unknown rule: " + issue.ruleKey()); } + if (Strings.isNullOrEmpty(issue.message())) { + issue.setMessage(rule.getName()); + } Date now = new Date(); issue.setCreationDate(now); diff --git a/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java b/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java index 68091cdea87..cb3cc7b5223 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java @@ -59,11 +59,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class IssueServiceTest { @@ -365,14 +361,32 @@ public class IssueServiceTest { } @Test - public void should_create_manual_issue() { + public void create_manual_issue() { RuleKey ruleKey = RuleKey.of("manual", "manualRuleKey"); - DefaultIssue manualIssue = new DefaultIssue().setKey("GHIJ").setRuleKey(RuleKey.of("manual", "manualRuleKey")).setComponentKey("org.sonar.Sample"); + DefaultIssue manualIssue = new DefaultIssue().setKey("GHIJ").setRuleKey(RuleKey.of("manual", "manualRuleKey")).setComponentKey("org.sonar.Sample").setMessage("Fix it"); when(ruleFinder.findByKey(ruleKey)).thenReturn(Rule.create("manual", "manualRuleKey")); when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(mock(ResourceDto.class)); Issue result = issueService.createManualIssue(manualIssue, userSession); assertThat(result).isNotNull(); + assertThat(result.message()).isEqualTo("Fix it"); + assertThat(result.creationDate()).isNotNull(); + assertThat(result.updateDate()).isNotNull(); + + verify(issueStorage).save(manualIssue); + verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.USER)); + } + + @Test + public void create_manual_issue_use_rule_name_if_no_message() { + RuleKey ruleKey = RuleKey.of("manual", "manualRuleKey"); + DefaultIssue manualIssue = new DefaultIssue().setKey("GHIJ").setRuleKey(RuleKey.of("manual", "manualRuleKey")).setComponentKey("org.sonar.Sample").setMessage(""); + when(ruleFinder.findByKey(ruleKey)).thenReturn(Rule.create("manual", "manualRuleKey").setName("Manual Rule")); + when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(mock(ResourceDto.class)); + + Issue result = issueService.createManualIssue(manualIssue, userSession); + assertThat(result).isNotNull(); + assertThat(result.message()).isEqualTo("Manual Rule"); assertThat(result.creationDate()).isNotNull(); assertThat(result.updateDate()).isNotNull(); -- 2.39.5