diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-24 14:44:52 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-28 08:02:49 +0200 |
commit | c24960fb4118de3791637c054b0a661772e7af16 (patch) | |
tree | 2ef25410789d380151a41749513f3290187e19eb /sonar-db/src | |
parent | b9fbae13635f53c5a6cc8bb50130a3a3bf42ee84 (diff) | |
download | sonarqube-c24960fb4118de3791637c054b0a661772e7af16.tar.gz sonarqube-c24960fb4118de3791637c054b0a661772e7af16.zip |
Remove call to deprecated RuleDao from CE RuleCacheLoader
Diffstat (limited to 'sonar-db/src')
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java | 16 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java | 31 |
2 files changed, 46 insertions, 1 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java b/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java index b20552a3b62..8be99ab5832 100644 --- a/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java +++ b/sonar-db/src/main/java/org/sonar/db/rule/RuleDao.java @@ -20,21 +20,35 @@ package org.sonar.db.rule; import com.google.common.base.Function; +import com.google.common.base.Optional; import java.util.List; import javax.annotation.Nonnull; import org.sonar.api.rule.RuleKey; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import org.sonar.db.RowNotFoundException; import static org.sonar.db.DatabaseUtils.executeLargeInputs; public class RuleDao implements Dao { + public Optional<RuleDto> selectByKey(DbSession session, RuleKey key) { + return Optional.fromNullable(mapper(session).selectByKey(key)); + } + + public RuleDto selectOrFailByKey(DbSession session, RuleKey key) { + RuleDto rule = mapper(session).selectByKey(key); + if (rule == null) { + throw new RowNotFoundException(String.format("Rule with key '%s' does not exist", key)); + } + return rule; + } + /** * Select rules by keys, whatever their status. Returns an empty list * if the list of {@code keys} is empty, without any db round trip. */ - public List<RuleDto> selectByKeys(final DbSession session, List<RuleKey> keys) { + public List<RuleDto> selectByKeys(DbSession session, List<RuleKey> keys) { return executeLargeInputs(keys, new KeyToDto(mapper(session))); } diff --git a/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java index 80de551b89f..499e80f34b8 100644 --- a/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/rule/RuleDaoTest.java @@ -19,6 +19,7 @@ */ package org.sonar.db.rule; +import com.google.common.base.Optional; import java.util.Collections; import java.util.List; import org.junit.Rule; @@ -28,6 +29,7 @@ import org.junit.rules.ExpectedException; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.db.RowNotFoundException; import org.sonar.test.DbTests; import static java.util.Arrays.asList; @@ -45,6 +47,35 @@ public class RuleDaoTest { RuleDao underTest = dbTester.getDbClient().ruleDao(); @Test + public void selectByKey() { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + assertThat(underTest.selectByKey(dbTester.getSession(), RuleKey.of("NOT", "FOUND")).isPresent()).isFalse(); + + Optional<RuleDto> rule = underTest.selectByKey(dbTester.getSession(), RuleKey.of("java", "S001")); + assertThat(rule.isPresent()).isTrue(); + assertThat(rule.get().getId()).isEqualTo(1); + } + + @Test + public void selectOrFailByKey() { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + RuleDto rule = underTest.selectOrFailByKey(dbTester.getSession(), RuleKey.of("java", "S001")); + assertThat(rule.getId()).isEqualTo(1); + } + + @Test + public void selectOrFailByKey_fails_if_rule_not_found() { + thrown.expect(RowNotFoundException.class); + thrown.expectMessage("Rule with key 'NOT:FOUND' does not exist"); + + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + underTest.selectOrFailByKey(dbTester.getSession(), RuleKey.of("NOT", "FOUND")); + } + + @Test public void selectByKeys() { dbTester.prepareDbUnit(getClass(), "shared.xml"); |