]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6993 Add component uuid in SnapshotQuery
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 10 Nov 2015 14:54:44 +0000 (15:54 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 Nov 2015 10:01:28 +0000 (11:01 +0100)
sonar-db/src/main/java/org/sonar/db/component/SnapshotDao.java
sonar-db/src/main/java/org/sonar/db/component/SnapshotMapper.java
sonar-db/src/main/java/org/sonar/db/component/SnapshotQuery.java
sonar-db/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
sonar-db/src/test/java/org/sonar/db/component/SnapshotDaoTest.java
sonar-db/src/test/java/org/sonar/db/component/SnapshotQueryTest.java
sonar-db/src/test/resources/org/sonar/db/component/SnapshotDaoTest/select_snapshots_by_query.xml

index 575d5ac26a0a825ee6b510651bb96ed229e85f49..4ef0cea7fa4b4ea7f8e4d8c1f81bf912441dcc8c 100644 (file)
@@ -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);
   }
index 5cc7e177d5b623f0a54e39b553b7ff3943fff9f5..043390bdf13a33be7c0972049a354cade0b3ff23 100644 (file)
@@ -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);
index 13b2b553b6270bd69317bba9942b7b9493caadee..e7f0d7f37bbc83582621d52ec73b7f246dbbdd67 100644 (file)
@@ -44,6 +44,7 @@ public final class SnapshotQuery {
   }
 
   private Long componentId;
+  private String componentUuid;
   private Long createdAfter;
   private Long createdBefore;
   private String status;
@@ -100,6 +101,16 @@ public final class SnapshotQuery {
     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;
index 597f033cc50c65d7907562132363aae69565bc6c..a8531b0a4074763d6d254e44a6d5165eabb760f3 100644 (file)
@@ -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}
index 37675670851dc2e437e39659a1735d5e12905c78..2f8639db024dcf1e6127daa28aafafa207ae68f6 100644 (file)
@@ -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;
@@ -42,6 +43,9 @@ import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
 @Category(DbTests.class)
 public class SnapshotDaoTest {
 
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
@@ -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
index 613686ec55b2ed05e95f4dc75dd704cccced3b45..e7fb2686724a8dcf8f0e736eff0d5ca8bfdf8da4 100644 (file)
@@ -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");
index 1f32010f385a57ce44290c22a7d1c6e397277d61..36cb562b62ff1dcec95c8ff60ef78251c54810ee 100644 (file)
@@ -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"
              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>