aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-23 11:59:11 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-07-24 10:20:56 +0200
commit9cf194d84485f74efd43355bf85e0adbfe538f6c (patch)
treee82dd085daf944eae625aad163e05565357d08e8 /sonar-db/src
parent70b5cfedb06242304b4fe16d4531130998322672 (diff)
downloadsonarqube-9cf194d84485f74efd43355bf85e0adbfe538f6c.tar.gz
sonarqube-9cf194d84485f74efd43355bf85e0adbfe538f6c.zip
Apply conventions to IssueDao
Diffstat (limited to 'sonar-db/src')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java20
-rw-r--r--sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java25
2 files changed, 29 insertions, 16 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java
index fb65086103a..505c9f540f5 100644
--- a/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueDao.java
@@ -21,6 +21,7 @@
package org.sonar.db.issue;
import com.google.common.base.Function;
+import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import java.util.Collection;
@@ -28,7 +29,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.ibatis.session.ResultHandler;
@@ -36,6 +36,7 @@ import org.apache.ibatis.session.SqlSession;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
+import org.sonar.db.RowNotFoundException;
public class IssueDao implements Dao {
@@ -55,20 +56,19 @@ public class IssueDao implements Dao {
}
}
- @CheckForNull
- public IssueDto selectNullableByKey(DbSession session, String key) {
- return mapper(session).selectByKey(key);
+ public Optional<IssueDto> selectByKey(DbSession session, String key) {
+ return Optional.fromNullable(mapper(session).selectByKey(key));
}
- public IssueDto selectByKey(DbSession session, String key) {
- IssueDto issue = selectNullableByKey(session, key);
- if (issue == null) {
- throw new IllegalArgumentException(String.format("Issue key '%s' does not exist", key));
+ public IssueDto selectByKeyOrFail(DbSession session, String key) {
+ Optional<IssueDto> issue = selectByKey(session, key);
+ if (!issue.isPresent()) {
+ throw new RowNotFoundException(String.format("Issue with key '%s' does not exist", key));
}
- return issue;
+ return issue.get();
}
- public List<IssueDto> findByActionPlan(DbSession session, String actionPlan) {
+ public List<IssueDto> selectByActionPlan(DbSession session, String actionPlan) {
return mapper(session).selectByActionPlan(actionPlan);
}
diff --git a/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java b/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java
index 2e11ebc9b7d..c13d873d510 100644
--- a/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/issue/IssueDaoTest.java
@@ -20,18 +20,18 @@
package org.sonar.db.issue;
-import java.util.Arrays;
-import java.util.Collection;
import java.util.List;
import org.apache.ibatis.executor.result.DefaultResultHandler;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
+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.component.ComponentDto;
import org.sonar.db.rule.RuleTesting;
+import org.sonar.db.RowNotFoundException;
import org.sonar.test.DbTests;
import static java.util.Arrays.asList;
@@ -41,6 +41,9 @@ import static org.assertj.core.api.Assertions.assertThat;
public class IssueDaoTest {
@Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
IssueDao dao = dbTester.getDbClient().issueDao();
@@ -92,10 +95,10 @@ public class IssueDaoTest {
}
@Test
- public void selectByKey() {
+ public void selectByKeyOrFail() {
dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml");
- IssueDto issue = dao.selectByKey(dbTester.getSession(), "I1");
+ IssueDto issue = dao.selectByKeyOrFail(dbTester.getSession(), "I1");
assertThat(issue.getKee()).isEqualTo("I1");
assertThat(issue.getId()).isEqualTo(1L);
assertThat(issue.getComponentUuid()).isEqualTo("CDEF");
@@ -126,6 +129,16 @@ public class IssueDaoTest {
}
@Test
+ public void selectByKeyOrFail_fails_if_key_not_found() {
+ thrown.expect(RowNotFoundException.class);
+ thrown.expectMessage("Issue with key 'DOES_NOT_EXIST' does not exist");
+
+ dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml");
+
+ dao.selectByKeyOrFail(dbTester.getSession(), "DOES_NOT_EXIST");
+ }
+
+ @Test
public void selectByKeys() {
dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml");
@@ -146,10 +159,10 @@ public class IssueDaoTest {
}
@Test
- public void find_by_action_plan() {
+ public void selectByActionPlan() {
dbTester.prepareDbUnit(getClass(), "shared.xml", "find_by_action_plan.xml");
- List<IssueDto> issues = dao.findByActionPlan(dbTester.getSession(), "AP-1");
+ List<IssueDto> issues = dao.selectByActionPlan(dbTester.getSession(), "AP-1");
assertThat(issues).hasSize(1);
IssueDto issue = issues.get(0);