]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6162 Return template rule key in /batch/projects WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 9 Feb 2015 10:41:55 +0000 (11:41 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 9 Feb 2015 10:56:57 +0000 (11:56 +0100)
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectRepositoryLoader.java
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectRepositoryLoaderMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/RuleTesting.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectRepositoriesTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/ReportsMediumTest.java

index a224c4894afa4009fc2d9c346893013d41ad1b66..83eb56ee2cc54cc9bd6b13a8892142e9760873b5 100644 (file)
@@ -53,11 +53,7 @@ import org.sonar.server.user.UserSession;
 
 import javax.annotation.Nullable;
 
-import java.util.Collections;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Maps.newHashMap;
@@ -230,9 +226,11 @@ public class ProjectRepositoryLoader implements ServerComponent {
     for (org.sonar.batch.protocol.input.QProfile qProfile : ref.qProfiles()) {
       for (ActiveRule activeRule : qProfileLoader.findActiveRulesByProfile(qProfile.key())) {
         Rule rule = ruleService.getNonNullByKey(activeRule.key().ruleKey());
+        RuleKey templateKey = rule.templateKey();
         org.sonar.batch.protocol.input.ActiveRule inputActiveRule = new org.sonar.batch.protocol.input.ActiveRule(
           activeRule.key().ruleKey().repository(),
           activeRule.key().ruleKey().rule(),
+          templateKey != null ? templateKey.rule() : null,
           rule.name(),
           activeRule.severity(),
           rule.internalKey(),
@@ -254,7 +252,7 @@ public class ProjectRepositoryLoader implements ServerComponent {
       ref.addActiveRule(new org.sonar.batch.protocol.input.ActiveRule(
         RuleKey.MANUAL_REPOSITORY_KEY,
         rule.key().rule(),
-        rule.name(),
+        null, rule.name(),
         null, null, null));
     }
   }
index 17fa759880e263e61db4df1d73966ad78a9f8df7..3ab97eb09b61b3f4d17167b7f7909f077f80dd7b 100644 (file)
@@ -613,6 +613,39 @@ public class ProjectRepositoryLoaderMediumTest {
     assertThat(activeRules.get(0).params()).isEqualTo(ImmutableMap.of("max", "2"));
   }
 
+  @Test
+  public void return_custom_rule() throws Exception {
+    Date ruleUpdatedAt = DateUtils.parseDateTime("2014-01-14T13:00:00+0100");
+
+    ComponentDto project = ComponentTesting.newProjectDto();
+    MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION).addComponentUuidPermission(UserRole.USER, project.uuid(), project.uuid());
+    tester.get(DbClient.class).componentDao().insert(dbSession, project);
+
+    QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+      DateUtils.formatDateTime(ruleUpdatedAt));
+    tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
+    tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way"), dbSession);
+
+    RuleKey ruleKey = RuleKey.of("squid", "ArchitecturalConstraint");
+    RuleDto templateRule = RuleTesting.newTemplateRule(ruleKey).setName("Architectural Constraint").setLanguage(ServerTester.Xoo.KEY);
+    tester.get(DbClient.class).ruleDao().insert(dbSession, templateRule);
+
+    RuleDto customRule = RuleTesting.newCustomRule(templateRule);
+    tester.get(DbClient.class).ruleDao().insert(dbSession, customRule);
+    tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(customRule.getKey()).setSeverity(Severity.MINOR), profileDto.getKey());
+
+    dbSession.commit();
+
+    ProjectRepositories ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
+    List<ActiveRule> activeRules = newArrayList(ref.activeRules());
+    assertThat(activeRules).hasSize(1);
+    assertThat(activeRules.get(0).repositoryKey()).isEqualTo("squid");
+    assertThat(activeRules.get(0).ruleKey()).startsWith("ArchitecturalConstraint_");
+    assertThat(activeRules.get(0).templateRuleKey()).isEqualTo("ArchitecturalConstraint");
+    assertThat(activeRules.get(0).language()).isEqualTo("xoo");
+    assertThat(activeRules.get(0).severity()).isEqualTo("MINOR");
+  }
+
   @Test
   public void return_manual_rules() throws Exception {
     ComponentDto project = ComponentTesting.newProjectDto();
index c25b9b8dc98fae06430c55dc2951ea1da9e86218..274aec5de5bbd50b003106c1cecb630fafc0d5b4 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.rule;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableSet;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
@@ -89,7 +90,9 @@ public class RuleTesting {
   }
 
   public static RuleDto newCustomRule(RuleDto templateRule){
+    Preconditions.checkNotNull(templateRule.getId(), "The template rule need to be persisted before creating this custom rule.");
     return newDto(RuleKey.of(templateRule.getRepositoryKey(), templateRule.getRuleKey() + "_" + System.currentTimeMillis()))
+      .setLanguage(templateRule.getLanguage())
       .setTemplateId(templateRule.getId());
   }
 
index d9bf2e3bea712f061f686f10ea98189f774a422c..ab6e6c64192090d153e3b5d5cc0da39f784dbe5f 100644 (file)
@@ -26,13 +26,15 @@ import java.util.HashMap;
 import java.util.Map;
 
 public class ActiveRule {
-  private final String repositoryKey, ruleKey;
+  private final String repositoryKey, ruleKey, templateRuleKey;
   private final String name, severity, internalKey, language;
   private final Map<String, String> params = new HashMap<String, String>();
 
-  public ActiveRule(String repositoryKey, String ruleKey, String name, @Nullable String severity, @Nullable String internalKey, @Nullable String language) {
+  public ActiveRule(String repositoryKey, String ruleKey, @Nullable String templateRuleKey, String name, @Nullable String severity,
+    @Nullable String internalKey, @Nullable String language) {
     this.repositoryKey = repositoryKey;
     this.ruleKey = ruleKey;
+    this.templateRuleKey = templateRuleKey;
     this.name = name;
     this.severity = severity;
     this.internalKey = internalKey;
@@ -47,6 +49,11 @@ public class ActiveRule {
     return ruleKey;
   }
 
+  @CheckForNull
+  public String templateRuleKey() {
+    return templateRuleKey;
+  }
+
   public String name() {
     return name;
   }
index e4eb540d2918ce2a94a54fa94882391f2a4d8efb..cfb584f97e7f1b0f4024e13a4dcf0e4fe8a5fb70 100644 (file)
@@ -44,7 +44,7 @@ public class ProjectRepositoriesTest {
     settings.put("prop2", "value2");
     ref.addSettings("foo", settings);
     ref.settings("foo").put("prop", "value");
-    ActiveRule activeRule = new ActiveRule("repo", "rule", "Rule", "MAJOR", "rule", "java");
+    ActiveRule activeRule = new ActiveRule("repo", "rule", "templateRule", "Rule", "MAJOR", "rule", "java");
     activeRule.addParam("param1", "value1");
     ref.addActiveRule(activeRule);
     ref.setLastAnalysisDate(new SimpleDateFormat("dd/MM/yyyy").parse("31/10/2014"));
@@ -56,7 +56,7 @@ public class ProjectRepositoriesTest {
       .assertEquals(
         "{timestamp:10,"
           + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"1984-03-14T00:00:00+0100\"}},"
-          + "activeRules:[{repositoryKey:repo,ruleKey:rule,name:Rule,severity:MAJOR,internalKey:rule,language:java,params:{param1:value1}}],"
+          + "activeRules:[{repositoryKey:repo,ruleKey:rule,templateRuleKey:templateRule,name:Rule,severity:MAJOR,internalKey:rule,language:java,params:{param1:value1}}],"
           + "settingsByModule:{foo:{prop1:value1,prop2:value2,prop:value}},"
           + "fileDataByModuleAndPath:{foo:{\"src/main/java/Foo.java\":{hash:xyz,needBlame:true,scmLastCommitDatetimesByLine:\"1\u003d12345,2\u003d3456\",scmRevisionsByLine:\"1\u003d345,2\u003d345\",scmAuthorsByLine:\"1\u003dhenryju,2\u003dgaudin\"},"
           + "\"src/main/java/Foo2.java\":{hash:xyz,needBlame:false,scmLastCommitDatetimesByLine:\"1\u003d12345,2\u003d3456\",scmRevisionsByLine:\"1\u003d345,2\u003d345\",scmAuthorsByLine:\"1\u003dhenryju,2\u003dgaudin\"}}},"
@@ -69,7 +69,7 @@ public class ProjectRepositoriesTest {
     ProjectRepositories ref = ProjectRepositories
       .fromJson("{timestamp:1,"
         + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"1984-03-14T00:00:00+0100\"}},"
-        + "activeRules:[{repositoryKey:repo,ruleKey:rule,name:Rule,severity:MAJOR,internalKey:rule1,language:java,params:{param1:value1}}],"
+        + "activeRules:[{repositoryKey:repo,ruleKey:rule,templateRuleKey:templateRule,name:Rule,severity:MAJOR,internalKey:rule1,language:java,params:{param1:value1}}],"
         + "settingsByModule:{foo:{prop:value}},"
         + "fileDataByModuleAndPath:{foo:{\"src/main/java/Foo.java\":{hash:xyz,needBlame:true,scmLastCommitDatetimesByLine:\"1\u003d12345,2\u003d3456\",scmRevisionsByLine:\"1\u003d345,2\u003d345\",scmAuthorsByLine:\"1\u003dhenryju,2\u003dgaudin\"}}},"
         + "lastAnalysisDate:\"2014-10-31T00:00:00+0100\"}");
@@ -79,6 +79,7 @@ public class ProjectRepositoriesTest {
     ActiveRule activeRule = ref.activeRules().iterator().next();
     assertThat(activeRule.ruleKey()).isEqualTo("rule");
     assertThat(activeRule.repositoryKey()).isEqualTo("repo");
+    assertThat(activeRule.templateRuleKey()).isEqualTo("templateRule");
     assertThat(activeRule.name()).isEqualTo("Rule");
     assertThat(activeRule.severity()).isEqualTo("MAJOR");
     assertThat(activeRule.internalKey()).isEqualTo("rule1");
index 994fdaa676c821febb52616b6e12b03068e32ca1..22f8a339f1393083163f874dc4a6033720035e38 100644 (file)
@@ -44,7 +44,7 @@ public class IssuesMediumTest {
   public BatchMediumTester tester = BatchMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"))
     .build();
 
   @Before
index c6156e4cecc0f855cc4c9be19d5ae40bdaa6fc7b..10aef4cb60d4c260b171dcab5187c86eab14804c 100644 (file)
@@ -43,7 +43,7 @@ public class IssuesOnDirMediumTest {
   public BatchMediumTester tester = BatchMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(new ActiveRule("xoo", "OneIssueOnDirPerFile", "One issue per line", "MINOR", "xoo", "xoo"))
+    .activateRule(new ActiveRule("xoo", "OneIssueOnDirPerFile", null, "One issue per line", "MINOR", "xoo", "xoo"))
     .build();
 
   @Before
index 16d4f7350fb79130f2ba4b726f7e8c9ab79aac03..d3500431180b872fabd8e18f82816ad3efeffafa 100644 (file)
@@ -56,7 +56,7 @@ public class ReportsMediumTest {
     .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW))
     .registerPlugin("xoo", new XooPlugin())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"))
     // Existing issue
     .addPreviousIssue(new PreviousIssue().setKey("xyz")
       .setComponentKey("sample:xources/hello/HelloJava.xoo")