diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-23 11:15:33 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-24 10:15:22 +0200 |
commit | 890c19cffe1fd59f9f2f716ce8b8333454a31737 (patch) | |
tree | ff64a4a799416854ba2f5f9eaf614960a93c1b30 /sonar-db/src | |
parent | 3d5c0f21706bd44755de23ead87e833f2a12db6f (diff) | |
download | sonarqube-890c19cffe1fd59f9f2f716ce8b8333454a31737.tar.gz sonarqube-890c19cffe1fd59f9f2f716ce8b8333454a31737.zip |
Add IssueDao#selectByOrderedKeys(...)
Diffstat (limited to 'sonar-db/src')
4 files changed, 91 insertions, 14 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 cecd0fe0f08..fb65086103a 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 @@ -20,9 +20,17 @@ package org.sonar.db.issue; +import com.google.common.base.Function; +import com.google.common.base.Predicates; +import com.google.common.collect.FluentIterable; +import java.util.Collection; +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; import org.apache.ibatis.session.SqlSession; import org.sonar.db.Dao; @@ -64,10 +72,43 @@ public class IssueDao implements Dao { return mapper(session).selectByActionPlan(actionPlan); } + /** + * Gets a list issues by their keys. The result does NOT contain {@code null} values for issues not found, so + * the size of result may be less than the number of keys. A single issue is returned + * if input keys contain multiple occurrences of a key. + * <p>Results may be in a different order as input keys (see {@link #selectByOrderedKeys(DbSession, List)}).</p> + */ public List<IssueDto> selectByKeys(DbSession session, List<String> keys) { return mapper(session).selectByKeys(keys); } + /** + * Gets a list issues by their keys. The result does NOT contain {@code null} values for issues not found, so + * the size of result may be less than the number of keys. A single issue is returned + * if input keys contain multiple occurrences of a key. + * <p>Contrary to {@link #selectByKeys(DbSession, List)}, results are in the same order as input keys.</p> + */ + public Iterable<IssueDto> selectByOrderedKeys(DbSession session, List<String> keys) { + List<IssueDto> unordered = selectByKeys(session, keys); + return FluentIterable.from(keys).transform(new KeyToIssue(unordered)).filter(Predicates.notNull()); + } + + private static class KeyToIssue implements Function<String, IssueDto> { + private final Map<String, IssueDto> map = new HashMap<>(); + + private KeyToIssue(Collection<IssueDto> unordered) { + for (IssueDto dto : unordered) { + map.put(dto.getKey(), dto); + } + } + + @Nullable + @Override + public IssueDto apply(@Nonnull String issueKey) { + return map.get(issueKey); + } + } + public Set<String> selectComponentUuidsOfOpenIssuesForProjectUuid(DbSession session, String projectUuid) { return mapper(session).selectComponentUuidsOfOpenIssuesForProjectUuid(projectUuid); } diff --git a/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java b/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java index f5eff6e3095..ede94b1f5e2 100644 --- a/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java +++ b/sonar-db/src/main/java/org/sonar/db/issue/IssueDto.java @@ -44,9 +44,6 @@ import org.sonar.db.rule.RuleDto; import static org.sonar.api.utils.DateUtils.dateToLong; import static org.sonar.api.utils.DateUtils.longToDate; -/** - * @since 3.6 - */ public final class IssueDto implements Serializable { private static final char TAGS_SEPARATOR = ','; @@ -131,7 +128,7 @@ public final class IssueDto implements Serializable { .setIssueUpdateDate(issue.updateDate()) .setSelectedAt(issue.selectedAt()) - // technical dates + // technical dates .setCreatedAt(now) .setUpdatedAt(now); } @@ -176,7 +173,7 @@ public final class IssueDto implements Serializable { .setIssueUpdateDate(issue.updateDate()) .setSelectedAt(issue.selectedAt()) - // technical date + // technical date .setUpdatedAt(now); } 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 fd5f5040009..2e11ebc9b7d 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 @@ -21,6 +21,7 @@ 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; @@ -33,6 +34,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.rule.RuleTesting; import org.sonar.test.DbTests; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @Category(DbTests.class) @@ -90,12 +92,12 @@ public class IssueDaoTest { } @Test - public void get_by_key() { + public void selectByKey() { dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml"); - IssueDto issue = dao.selectByKey(dbTester.getSession(), "ABCDE"); - assertThat(issue.getKee()).isEqualTo("ABCDE"); - assertThat(issue.getId()).isEqualTo(100L); + IssueDto issue = dao.selectByKey(dbTester.getSession(), "I1"); + assertThat(issue.getKee()).isEqualTo("I1"); + assertThat(issue.getId()).isEqualTo(1L); assertThat(issue.getComponentUuid()).isEqualTo("CDEF"); assertThat(issue.getProjectUuid()).isEqualTo("ABCD"); assertThat(issue.getRuleId()).isEqualTo(500); @@ -124,11 +126,23 @@ public class IssueDaoTest { } @Test - public void get_by_keys() { + public void selectByKeys() { dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml"); - List<IssueDto> issues = dao.selectByKeys(dbTester.getSession(), Arrays.asList("ABCDE")); - assertThat(issues).hasSize(1); + List<IssueDto> issues = dao.selectByKeys(dbTester.getSession(), asList("I1", "I2", "I3")); + // results are not ordered, so do not use "containsExactly" + assertThat(issues).extracting("key").containsOnly("I1", "I2"); + } + + @Test + public void selectByOrderedKeys() { + dbTester.prepareDbUnit(getClass(), "shared.xml", "get_by_key.xml"); + + Iterable<IssueDto> issues = dao.selectByOrderedKeys(dbTester.getSession(), asList("I1", "I2", "I3")); + assertThat(issues).extracting("key").containsExactly("I1", "I2"); + + issues = dao.selectByOrderedKeys(dbTester.getSession(), asList("I2", "I3", "I1")); + assertThat(issues).extracting("key").containsExactly("I2", "I1"); } @Test diff --git a/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml b/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml index 154a7a67883..3d5a5c70e2e 100644 --- a/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml +++ b/sonar-db/src/test/resources/org/sonar/db/issue/IssueDaoTest/get_by_key.xml @@ -1,8 +1,33 @@ <dataset> <issues - id="100" - kee="ABCDE" + id="1" + kee="I1" + component_uuid="CDEF" + project_uuid="ABCD" + rule_id="500" + severity="BLOCKER" + manual_severity="[false]" + message="[null]" + line="200" + effort_to_fix="4.2" + status="OPEN" + resolution="FIXED" + checksum="XXX" + reporter="arthur" + assignee="perceval" + author_login="karadoc" + issue_attributes="JIRA=FOO-1234" + issue_creation_date="1366063200000" + issue_update_date="1366063200000" + issue_close_date="1366063200000" + created_at="1400000000000" + updated_at="1450000000000" + /> + + <issues + id="2" + kee="I2" component_uuid="CDEF" project_uuid="ABCD" rule_id="500" |