@@ -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); | |||
} |
@@ -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); | |||
} | |||
@@ -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 |
@@ -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" |