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 {
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);
}
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);
}
private Long componentId;
+ private String componentUuid;
private Long createdAfter;
private Long createdBefore;
private String status;
return this;
}
+ @CheckForNull
+ public String getComponentUuid() {
+ return componentUuid;
+ }
+
+ public SnapshotQuery setComponentUuid(@Nullable String componentUuid) {
+ this.componentUuid = componentUuid;
+ return this;
+ }
+
@CheckForNull
public String getStatus() {
return status;
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}
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;
@Category(DbTests.class)
public class SnapshotDaoTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
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
public void test_setters_and_getters() throws Exception {
SnapshotQuery query = new SnapshotQuery()
.setComponentId(1L)
+ .setComponentUuid("abcd")
.setIsLast(true)
.setStatus("P")
.setVersion("1.0")
.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");
<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"
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"
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"
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>