From 72f64c95fdef18ec950d341c1a9093f30087e1c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 7 Feb 2018 11:43:26 +0100 Subject: [PATCH] SONAR-10410 Rule#getSystemTags is populated with definition tags --- .../java/org/sonar/db/rule/RuleDbTester.java | 6 ++++ .../sonar/server/rule/CachingRuleFinder.java | 5 ++- .../sonar/server/rule/DefaultRuleFinder.java | 1 + .../server/rule/CachingRuleFinderTest.java | 4 ++- .../server/rule/DefaultRuleFinderTest.java | 34 +++++++++++++++++++ .../main/java/org/sonar/api/rules/Rule.java | 5 +++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDbTester.java index 7f222a3e0ae..8571c26311a 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDbTester.java @@ -146,4 +146,10 @@ public class RuleDbTester { db.commit(); return param; } + + public RuleDto insertRule(RuleDefinitionDto ruleDefinition, RuleMetadataDto ruleMetadata) { + db.getDbClient().ruleDao().insertOrUpdate(db.getSession(), ruleMetadata.setRuleId(ruleDefinition.getId())); + db.commit(); + return new RuleDto(ruleDefinition, ruleMetadata); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/CachingRuleFinder.java b/server/sonar-server/src/main/java/org/sonar/server/rule/CachingRuleFinder.java index f4a5555f8ff..36a25efd1ca 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/CachingRuleFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/CachingRuleFinder.java @@ -163,6 +163,7 @@ public class CachingRuleFinder implements RuleFinder { RuleDto.Format descriptionFormat = ruleDefinition.getDescriptionFormat(); Rule apiRule = new Rule(); + String[] systemTags = ruleDefinition.getSystemTags().toArray(new String[ruleDefinition.getSystemTags().size()]); apiRule .setName(ruleDefinition.getName()) .setLanguage(ruleDefinition.getLanguage()) @@ -174,7 +175,9 @@ public class CachingRuleFinder implements RuleFinder { .setRepositoryKey(ruleDefinition.getRepositoryKey()) .setSeverity(severity != null ? RulePriority.valueOf(severity) : null) .setStatus(ruleDefinition.getStatus().name()) - .setTags(ruleDefinition.getSystemTags().toArray(new String[ruleDefinition.getSystemTags().size()])) + .setSystemTags(systemTags) + // FIXME tags should contain user defined tags and we can't provide them without organization => tags should return [] + .setTags(systemTags) .setId(ruleDefinition.getId()); if (description != null && descriptionFormat != null) { if (RuleDto.Format.HTML.equals(descriptionFormat)) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java b/server/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java index e96599002e2..900078fda57 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java @@ -145,6 +145,7 @@ public class DefaultRuleFinder implements RuleFinder { .setRepositoryKey(rule.getRepositoryKey()) .setSeverity(severity != null ? RulePriority.valueOf(severity) : null) .setStatus(rule.getStatus().name()) + .setSystemTags(rule.getSystemTags().toArray(new String[rule.getSystemTags().size()])) .setTags(rule.getTags().toArray(new String[rule.getTags().size()])) .setId(rule.getId()); if (description != null && descriptionFormat != null) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java index 73992868bd4..b4e7bf3a9bc 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java @@ -408,7 +408,9 @@ public class CachingRuleFinderTest { assertThat(rule.getUpdatedAt().getTime()).isEqualTo(ruleDefinition.getUpdatedAt()); assertThat(rule.getRepositoryKey()).isEqualTo(ruleDefinition.getRepositoryKey()); assertThat(rule.getSeverity().name()).isEqualTo(ruleDefinition.getSeverityString()); - assertThat(rule.getTags()).isEqualTo(ruleDefinition.getSystemTags().stream().toArray(String[]::new)); + String[] systemTags = ruleDefinition.getSystemTags().stream().toArray(String[]::new); + assertThat(rule.getSystemTags()).isEqualTo(systemTags); + assertThat(rule.getTags()).isEqualTo(systemTags); assertThat(rule.getId()).isEqualTo(ruleDefinition.getId()); assertThat(rule.getDescription()).isEqualTo(ruleDefinition.getDescription()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java index 7e3b86e489c..138d6535861 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.rule; +import com.google.common.collect.ImmutableSet; import org.junit.Before; import org.junit.Test; import org.sonar.api.rule.RuleStatus; @@ -28,10 +29,13 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.db.rule.RuleDto; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; public class DefaultRuleFinderTest { @@ -133,4 +137,34 @@ public class DefaultRuleFinderTest { assertThat(underTest.findAll(RuleQuery.create())).extracting("id").containsOnly(rule1.getId(), rule3.getId(), rule4.getId()); } + @Test + public void findById_populates_system_tags_but_not_tags() { + RuleDefinitionDto ruleDefinition = dbTester.rules() + .insert(t -> t.setSystemTags(ImmutableSet.of(randomAlphanumeric(5), randomAlphanumeric(6)))); + OrganizationDto organization = dbTester.organizations().insert(); + dbTester.rules().insertRule(organization); + + Rule rule = underTest.findById(ruleDefinition.getId()); + assertThat(rule.getSystemTags()) + .containsOnlyElementsOf(ruleDefinition.getSystemTags()); + assertThat(rule.getTags()).isEmpty(); + } + + @Test + public void findByKey_populates_system_tags_but_not_tags() { + RuleDefinitionDto ruleDefinition = dbTester.rules() + .insert(t -> t.setSystemTags(ImmutableSet.of(randomAlphanumeric(5), randomAlphanumeric(6)))); + OrganizationDto organization = dbTester.organizations().insert(); + dbTester.rules().insertRule(organization); + + Rule rule = underTest.findByKey(ruleDefinition.getKey()); + assertThat(rule.getSystemTags()) + .containsOnlyElementsOf(ruleDefinition.getSystemTags()); + assertThat(rule.getTags()).isEmpty(); + + rule = underTest.findByKey(ruleDefinition.getRepositoryKey(), ruleDefinition.getRuleKey()); + assertThat(rule.getSystemTags()) + .containsOnlyElementsOf(ruleDefinition.getSystemTags()); + assertThat(rule.getTags()).isEmpty(); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java index ea1949354e2..e5deb9fab7d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java @@ -400,6 +400,11 @@ public class Rule { return systemTags == null ? new String[0] : StringUtils.split(systemTags, ','); } + public Rule setSystemTags(String[] tags) { + this.systemTags = tags == null ? null : StringUtils.join(tags, ','); + return this; + } + /** * For internal use only. * -- 2.39.5