]> source.dussan.org Git - sonarqube.git/commitdiff
When creating a manual issue, if the message is empty, use the rule name instead
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 6 Feb 2014 12:37:29 +0000 (13:37 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 6 Feb 2014 12:37:29 +0000 (13:37 +0100)
sonar-server/src/main/java/org/sonar/server/issue/IssueService.java
sonar-server/src/test/java/org/sonar/server/issue/IssueServiceTest.java

index fdf674289f3bdadd80c6084901f25b88a73993b8..cca54668f0bb2a2d64da30724859aea5f730e952 100644 (file)
@@ -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);
index 68091cdea8748f94313740e64b4c19cf05c847ef..cb3cc7b5223a13670d02c138086bd966893bc547 100644 (file)
@@ -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();