]> 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 11:06:35 +0000 (12:06 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 9 Feb 2015 11:06:35 +0000 (12:06 +0100)
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectReferentialsLoader.java
server/sonar-server/src/test/java/org/sonar/server/batch/ProjectReferentialsLoaderMediumTest.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/ProjectReferentialsTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.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

index 39ab3e198bd9d1f139055403cd9d55d8f73a04d3..4d6ad3502316c9b1d0c25d438f233375db23dd61 100644 (file)
@@ -26,6 +26,7 @@ import com.google.common.collect.Multimap;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
+import org.sonar.api.rule.RuleKey;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 import org.sonar.core.UtcDateUtils;
 import org.sonar.core.component.AuthorizedComponentDto;
@@ -205,9 +206,11 @@ public class ProjectReferentialsLoader 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 templateRuleKey = rule.templateKey();
         org.sonar.batch.protocol.input.ActiveRule inputActiveRule = new org.sonar.batch.protocol.input.ActiveRule(
           activeRule.key().ruleKey().repository(),
           activeRule.key().ruleKey().rule(),
+          templateRuleKey != null ? templateRuleKey.rule() : null,
           rule.name(),
           activeRule.severity(),
           rule.internalKey(),
index 3eae77a6be78c1c297d3cf36e82b4f210c0c529c..7a4a9ef57f19bc706322a2f781faa7126e7fb4fe 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.api.utils.DateUtils;
+import org.sonar.api.web.UserRole;
 import org.sonar.batch.protocol.input.ActiveRule;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 import org.sonar.batch.protocol.input.QProfile;
@@ -658,6 +659,39 @@ public class ProjectReferentialsLoaderMediumTest {
     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).addComponentPermission(UserRole.USER, project.key(), project.key());
+    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();
+
+    ProjectReferentials ref = loader.load(ProjectReferentialsQuery.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 fail_if_no_permission() throws Exception {
     MockUserSession.set().setLogin("john").setGlobalPermissions();
index d86c17e1ff80b772b959e0573c7ac2d6332e96d2..375ccb4da782d2337fa7698c19319238fd89386b 100644 (file)
@@ -92,6 +92,7 @@ public class RuleTesting {
 
   public static RuleDto newCustomRule(RuleDto templateRule){
     return newDto(RuleKey.of(templateRule.getRepositoryKey(), templateRule.getRuleKey() + "_" + new Date().getTime()))
+      .setLanguage(templateRule.getLanguage())
       .setTemplateId(templateRule.getId());
   }
 
index 4fda09f8910363614212062c324c85c6dcd76f4e..157253fa05688948b6ab918f2e6b54f641656eab 100644 (file)
@@ -26,13 +26,14 @@ 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, String severity, @Nullable String internalKey, String language) {
+  public ActiveRule(String repositoryKey, String ruleKey, @Nullable String templateRuleKey, String name, String severity, @Nullable String internalKey, String language) {
     this.repositoryKey = repositoryKey;
     this.ruleKey = ruleKey;
+    this.templateRuleKey = templateRuleKey;
     this.name = name;
     this.severity = severity;
     this.internalKey = internalKey;
@@ -47,6 +48,11 @@ public class ActiveRule {
     return ruleKey;
   }
 
+  @CheckForNull
+  public String templateRuleKey() {
+    return templateRuleKey;
+  }
+
   public String name() {
     return name;
   }
index 9196a831ec97289ddf360dd8c63908aa0c419bdd..1bca74f2efc9493971b4de6ae6241cea759c1679 100644 (file)
@@ -45,7 +45,7 @@ public class ProjectReferentialsTest {
     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.setTimestamp(10);
@@ -55,7 +55,7 @@ public class ProjectReferentialsTest {
       .assertEquals(
         "{timestamp:10,"
           + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}},"
-          + "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}}}",
         ref.toJson(), true);
   }
@@ -64,7 +64,7 @@ public class ProjectReferentialsTest {
   public void testFromJson() throws JSONException, ParseException {
     ProjectReferentials ref = ProjectReferentials.fromJson("{timestamp:1,"
       + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}},"
-      + "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}}}");
 
     assertThat(ref.timestamp()).isEqualTo(1);
@@ -72,6 +72,7 @@ public class ProjectReferentialsTest {
     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 0d171bcfc098f96ceaf5bd3a684831de5d67555a..129c5ef620cfb1354ed63214bdc6dd354baae89c 100644 (file)
@@ -52,7 +52,7 @@ public class FileSystemMediumTest {
   public BatchMediumTester tester = BatchMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "One issue per line", "MAJOR", "xoo", "xoo"))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "xoo", "xoo"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
 
index f3eebf971e3e5a4767e92572029fe856d937e0b1..9add0221f209bdfd4b26f270100f591bff53c9b6 100644 (file)
@@ -45,7 +45,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"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
 
index fb3e8d16edd3b63e73a05577c3cbb940ec31de51..cb203d2234d5a114d6e3d862e5dcef62ce595457 100644 (file)
@@ -44,7 +44,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"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();