aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-11-10 15:54:44 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-11-12 11:01:28 +0100
commite0fbfda29745e0fb1512dc8eaaeb352626e681df (patch)
tree048867b44ad22ae17ba29c1f743966172ee52b00 /sonar-db
parent337341591d1506af4a17ea6fbbb9109c98631cb1 (diff)
downloadsonarqube-e0fbfda29745e0fb1512dc8eaaeb352626e681df.tar.gz
sonarqube-e0fbfda29745e0fb1512dc8eaaeb352626e681df.zip
SONAR-6993 Add component uuid in SnapshotQuery
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java11
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java3
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/SnapshotQuery.java11
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml3
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java27
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/SnapshotQueryTest.java2
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_snapshots_by_query.xml26
7 files changed, 83 insertions, 0 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
index 575d5ac26a0..4ef0cea7fa4 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
@@ -32,6 +32,7 @@ import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
+import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.FluentIterable.from;
public class SnapshotDao implements Dao {
@@ -70,6 +71,16 @@ public class SnapshotDao implements Dao {
return mapper(session).selectSnapshotsByQuery(query);
}
+ @CheckForNull
+ public SnapshotDto selectSnapshotByQuery(DbSession session, SnapshotQuery query) {
+ List<SnapshotDto> snapshotDtos = mapper(session).selectSnapshotsByQuery(query);
+ if (snapshotDtos.isEmpty()) {
+ return null;
+ }
+ checkState(snapshotDtos.size() == 1, "Expected one snapshot to be returned, got %s", snapshotDtos.size());
+ return snapshotDtos.get(0);
+ }
+
public List<SnapshotDto> selectPreviousVersionSnapshots(DbSession session, long componentId, String lastVersion) {
return mapper(session).selectPreviousVersionSnapshots(componentId, lastVersion);
}
diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java
index 5cc7e177d5b..043390bdf13 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java
@@ -37,6 +37,9 @@ public interface SnapshotMapper {
int countLastSnapshotByComponentUuid(String componentUuid);
+ @CheckForNull
+ SnapshotDto selectLastSnapshotByComponentUuid(String componentUuid);
+
List<SnapshotDto> selectSnapshotsByQuery(@Param("query") SnapshotQuery query);
List<SnapshotDto> selectPreviousVersionSnapshots(@Param(value = "componentId") Long componentId, @Param(value = "lastVersion") String lastVersion);
diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotQuery.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotQuery.java
index 13b2b553b62..e7f0d7f37bb 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotQuery.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotQuery.java
@@ -44,6 +44,7 @@ public final class SnapshotQuery {
}
private Long componentId;
+ private String componentUuid;
private Long createdAfter;
private Long createdBefore;
private String status;
@@ -101,6 +102,16 @@ public final class SnapshotQuery {
}
@CheckForNull
+ public String getComponentUuid() {
+ return componentUuid;
+ }
+
+ public SnapshotQuery setComponentUuid(@Nullable String componentUuid) {
+ this.componentUuid = componentUuid;
+ return this;
+ }
+
+ @CheckForNull
public String getStatus() {
return status;
}
diff --git a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
index 597f033cc50..a8531b0a407 100644
--- a/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
+++ b/sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
@@ -69,6 +69,9 @@
SELECT
<include refid="snapshotColumns"/>
FROM snapshots s
+ <if test="query.componentUuid != null">
+ INNER JOIN projects p ON p.id=s.project_id AND p.uuid=#{query.componentUuid} AND p.enabled=${_true}
+ </if>
<where>
<if test="query.componentId != null">
AND s.project_id=#{query.componentId}
diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java
index 37675670851..2f8639db024 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java
@@ -25,6 +25,7 @@ import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
+import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.DateUtils;
@@ -43,6 +44,9 @@ import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
public class SnapshotDaoTest {
@Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbSession dbSession = db.getSession();
@@ -160,6 +164,29 @@ public class SnapshotDaoTest {
assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setScope(Scopes.PROJECT).setQualifier(Qualifiers.PACKAGE))).extracting("id").containsOnly(1L);
assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setScope(Scopes.DIRECTORY).setQualifier(Qualifiers.PACKAGE))).extracting("id").containsOnly(2L, 3L, 4L, 5L, 6L);
+
+ assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD"))).hasSize(3);
+ assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isEmpty();
+ assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("GHIJ"))).isEmpty();
+ }
+
+ @Test
+ public void select_snapshot_by_query() {
+ db.prepareDbUnit(getClass(), "select_snapshots_by_query.xml");
+
+ assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD").setIsLast(true))).isNotNull();
+ assertThat(underTest.selectSnapshotByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isNull();
+ assertThat(underTest.selectSnapshotByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("GHIJ"))).isNull();
+ }
+
+ @Test
+ public void fail_with_ISE_to_select_snapshot_by_query_when_more_than_one_result() {
+ db.prepareDbUnit(getClass(), "select_snapshots_by_query.xml");
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Expected one snapshot to be returned, got 6");
+
+ underTest.selectSnapshotByQuery(db.getSession(), new SnapshotQuery());
}
@Test
diff --git a/sonar-db/src/test/java/org/sonar/db/component/SnapshotQueryTest.java b/sonar-db/src/test/java/org/sonar/db/component/SnapshotQueryTest.java
index 613686ec55b..e7fb2686724 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/SnapshotQueryTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/SnapshotQueryTest.java
@@ -32,6 +32,7 @@ public class SnapshotQueryTest {
public void test_setters_and_getters() throws Exception {
SnapshotQuery query = new SnapshotQuery()
.setComponentId(1L)
+ .setComponentUuid("abcd")
.setIsLast(true)
.setStatus("P")
.setVersion("1.0")
@@ -40,6 +41,7 @@ public class SnapshotQueryTest {
.setSort(BY_DATE, ASC);
assertThat(query.getComponentId()).isEqualTo(1L);
+ assertThat(query.getComponentUuid()).isEqualTo("abcd");
assertThat(query.getIsLast()).isTrue();
assertThat(query.getStatus()).isEqualTo("P");
assertThat(query.getVersion()).isEqualTo("1.0");
diff --git a/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_snapshots_by_query.xml b/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_snapshots_by_query.xml
index 1f32010f385..36cb562b62f 100644
--- a/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_snapshots_by_query.xml
+++ b/sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_snapshots_by_query.xml
@@ -1,6 +1,12 @@
<dataset>
<!-- PROJECT_ID = 1 -->
+ <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="."
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]"
+ authorization_updated_at="[null]"/>
+
<snapshots id="1" project_id="1" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="1"
status="P" islast="[true]" purge_status="1"
period1_mode="days1" period1_param="30" period1_date="1316815200000"
@@ -30,6 +36,12 @@
version="2.2-SNAPSHOT" path="1.2."/>
<!-- PROJECT_ID = 2 -->
+ <projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core"
+ uuid="EFGH" project_uuid="ABCD" module_uuid="[null]" module_uuid_path=".ABCD."
+ scope="PRJ" qualifier="BRC" long_name="Struts Core"
+ description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]"
+ authorization_updated_at="[null]"/>
+
<snapshots id="4" project_id="2" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="3"
status="P" islast="[true]" purge_status="1"
period1_mode="days1" period1_param="30" period1_date="1316815200000"
@@ -51,6 +63,12 @@
version="2.1-SNAPSHOT" path="1.2."/>
<!-- PROJECT_ID = 3 - no last snapshot -->
+ <projects id="3" root_id="1" kee="org.struts:struts-data" name="Struts Data"
+ uuid="FGHI" project_uuid="ABCD" module_uuid="EFGH" module_uuid_path=".ABCD.EFGH."
+ scope="PRJ" qualifier="BRC" long_name="Struts Data"
+ description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]"
+ authorization_updated_at="[null]"/>
+
<snapshots id="6" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="3"
status="P" islast="[false]" purge_status="1"
period1_mode="days1" period1_param="30" period1_date="1316815200000"
@@ -61,4 +79,12 @@
depth="1" scope="DIR" qualifier="PAC" created_at="1228172400000" build_date="1317247200000"
version="2.1-SNAPSHOT" path="1.2."/>
+ <!-- PROJECT_ID = 4 - no snapshot -->
+ <projects id="4" root_id="1" kee="org.struts:struts-deprecated" name="Struts Deprecated"
+ uuid="GHIJ" project_uuid="ABCD" module_uuid="EFGH" module_uuid_path=".ABCD.EFGH."
+ scope="PRJ" qualifier="BRC" long_name="Struts Deprecated"
+ description="[null]" enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]"
+ authorization_updated_at="[null]"/>
+
+
</dataset>