aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-02-06 10:27:33 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-02-08 13:41:00 +0100
commit044c7ad3a24f240e2fda5f2ff586ecc5c4c8b457 (patch)
treeb4e7a869abb3315afbb29abd6e5822bf961ecafe /server
parent9f942542372beaf442a4e2665912599c03a4b930 (diff)
downloadsonarqube-044c7ad3a24f240e2fda5f2ff586ecc5c4c8b457.tar.gz
sonarqube-044c7ad3a24f240e2fda5f2ff586ecc5c4c8b457.zip
SONAR-10308 support report referencing deprecated rule keys
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImpl.java6
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java65
2 files changed, 65 insertions, 6 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImpl.java
index fdc0fb9db67..a7698d48162 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImpl.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImpl.java
@@ -21,11 +21,14 @@ package org.sonar.server.computation.task.projectanalysis.issue;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Multimap;
import java.util.Map;
import javax.annotation.CheckForNull;
import org.sonar.api.rule.RuleKey;
+import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.rule.DeprecatedRuleKeyDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
@@ -99,10 +102,13 @@ public class RuleRepositoryImpl implements RuleRepository {
ImmutableMap.Builder<RuleKey, Rule> rulesByKeyBuilder = ImmutableMap.builder();
ImmutableMap.Builder<Integer, Rule> rulesByIdBuilder = ImmutableMap.builder();
String organizationUuid = analysisMetadataHolder.getOrganization().getUuid();
+ Multimap<Integer, DeprecatedRuleKeyDto> deprecatedRuleKeysByRuleId = dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream()
+ .collect(MoreCollectors.index(DeprecatedRuleKeyDto::getRuleId));
for (RuleDto ruleDto : dbClient.ruleDao().selectAll(dbSession, organizationUuid)) {
Rule rule = new RuleImpl(ruleDto);
rulesByKeyBuilder.put(ruleDto.getKey(), rule);
rulesByIdBuilder.put(ruleDto.getId(), rule);
+ deprecatedRuleKeysByRuleId.get(ruleDto.getId()).forEach(t -> rulesByKeyBuilder.put(RuleKey.of(t.getOldRepositoryKey(), t.getOldRuleKey()), rule));
}
this.rulesByKey = rulesByKeyBuilder.build();
this.rulesById = rulesByIdBuilder.build();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java
index 2f1873dd904..d673c0e7560 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java
@@ -21,6 +21,7 @@ package org.sonar.server.computation.task.projectanalysis.issue;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -29,6 +30,7 @@ import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rules.RuleType;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.rule.DeprecatedRuleKeyDto;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleDto;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
@@ -47,29 +49,48 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
public class RuleRepositoryImplTest {
- private static final RuleDto AB_RULE = createABRuleDto();
+ private static final RuleDto AB_RULE = createABRuleDto()
+ .setId(9688);
+ private static final RuleKey AB_RULE_DEPRECATED_KEY_1 = RuleKey.of("old_a", "old_b");
+ private static final RuleKey AB_RULE_DEPRECATED_KEY_2 = RuleKey.of(AB_RULE.getRepositoryKey(), "old_b");
+ private static final RuleKey DEPRECATED_KEY_OF_NON_EXITING_RULE = RuleKey.of("some_rep", "some_key");
private static final RuleKey AC_RULE_KEY = RuleKey.of("a", "c");
private static final int AC_RULE_ID = 684;
private static final String ORGANIZATION_UUID = "org-1";
- public static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID";
+ private static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID";
@org.junit.Rule
public ExpectedException expectedException = ExpectedException.none();
@org.junit.Rule
public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
- .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID);
+ .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID);
private DbClient dbClient = mock(DbClient.class);
private DbSession dbSession = mock(DbSession.class);
private RuleDao ruleDao = mock(RuleDao.class);
- RuleRepositoryImpl underTest = new RuleRepositoryImpl(dbClient, analysisMetadataHolder);
+ private RuleRepositoryImpl underTest = new RuleRepositoryImpl(dbClient, analysisMetadataHolder);
@Before
public void setUp() throws Exception {
when(dbClient.openSession(anyBoolean())).thenReturn(dbSession);
when(dbClient.ruleDao()).thenReturn(ruleDao);
when(ruleDao.selectAll(any(DbSession.class), eq(ORGANIZATION_UUID))).thenReturn(ImmutableList.of(AB_RULE));
+ DeprecatedRuleKeyDto abDeprecatedRuleKey1 = deprecatedRuleKeyOf(AB_RULE, AB_RULE_DEPRECATED_KEY_1);
+ DeprecatedRuleKeyDto abDeprecatedRuleKey2 = deprecatedRuleKeyOf(AB_RULE, AB_RULE_DEPRECATED_KEY_2);
+ DeprecatedRuleKeyDto deprecatedRuleOfNonExistingRule = deprecatedRuleKeyOf(77777, DEPRECATED_KEY_OF_NON_EXITING_RULE);
+ when(ruleDao.selectAllDeprecatedRuleKeys(any(DbSession.class))).thenReturn(ImmutableSet.of(
+ abDeprecatedRuleKey1, abDeprecatedRuleKey2, deprecatedRuleOfNonExistingRule));
+ }
+
+ private static DeprecatedRuleKeyDto deprecatedRuleKeyOf(RuleDto ruleDto, RuleKey deprecatedRuleKey) {
+ return deprecatedRuleKeyOf(ruleDto.getId(), deprecatedRuleKey);
+ }
+
+ private static DeprecatedRuleKeyDto deprecatedRuleKeyOf(int ruleId, RuleKey deprecatedRuleKey) {
+ return new DeprecatedRuleKeyDto().setRuleId(ruleId)
+ .setOldRepositoryKey(deprecatedRuleKey.repository())
+ .setOldRuleKey(deprecatedRuleKey.rule());
}
@Test
@@ -137,14 +158,32 @@ public class RuleRepositoryImplTest {
}
@Test
+ public void getByKey_returns_Rule_if_argument_is_deprecated_key_in_DB_of_rule_in_DB() {
+ Rule rule = underTest.getByKey(AB_RULE_DEPRECATED_KEY_1);
+
+ assertIsABRule(rule);
+ }
+
+ @Test
public void getByKey_throws_IAE_if_rules_does_not_exist_in_DB() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Can not find rule for key a:c. This rule does not exist in DB");
+ expectIAERuleNotFound(AC_RULE_KEY);
underTest.getByKey(AC_RULE_KEY);
}
@Test
+ public void getByKey_throws_IAE_if_argument_is_deprecated_key_in_DB_of_non_existing_rule() {
+ expectIAERuleNotFound(DEPRECATED_KEY_OF_NON_EXITING_RULE);
+
+ underTest.getByKey(DEPRECATED_KEY_OF_NON_EXITING_RULE);
+ }
+
+ private void expectIAERuleNotFound(RuleKey ruleKey) {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Can not find rule for key " + ruleKey.toString() + ". This rule does not exist in DB");
+ }
+
+ @Test
public void findByKey_throws_NPE_if_key_argument_is_null() {
expectNullRuleKeyNPE();
@@ -175,6 +214,20 @@ public class RuleRepositoryImplTest {
}
@Test
+ public void findByKey_returns_Rule_if_argument_is_deprecated_key_in_DB_of_rule_in_DB() {
+ Optional<Rule> rule = underTest.findByKey(AB_RULE_DEPRECATED_KEY_1);
+
+ assertIsABRule(rule.get());
+ }
+
+ @Test
+ public void findByKey_returns_empty_if_argument_is_deprecated_key_in_DB_of_rule_in_DB() {
+ Optional<Rule> rule = underTest.findByKey(DEPRECATED_KEY_OF_NON_EXITING_RULE);
+
+ assertThat(rule).isAbsent();
+ }
+
+ @Test
public void getById_returns_Rule_if_it_exists_in_DB() {
Rule rule = underTest.getById(AB_RULE.getId());