diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-11-10 18:28:57 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-11-14 12:18:51 +0100 |
commit | 976aa63a8ee9bf1df31eb23d5085a67074f0d5e7 (patch) | |
tree | c397cfcabeb74f878588577f88fd8a86ac1c6e02 /sonar-db | |
parent | fc8fa7830f47874a67f3a13b333d24a9ceafe856 (diff) | |
download | sonarqube-976aa63a8ee9bf1df31eb23d5085a67074f0d5e7.tar.gz sonarqube-976aa63a8ee9bf1df31eb23d5085a67074f0d5e7.zip |
SONAR-8353 add WS api/webhooks/deliveries
Diffstat (limited to 'sonar-db')
4 files changed, 91 insertions, 5 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java b/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java index 69af1576d22..ec444d3cb6f 100644 --- a/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java +++ b/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java @@ -19,6 +19,7 @@ */ package org.sonar.db.webhook; +import java.util.List; import java.util.Optional; import org.sonar.db.Dao; import org.sonar.db.DbSession; @@ -28,6 +29,21 @@ public class WebhookDeliveryDao implements Dao { public Optional<WebhookDeliveryDto> selectByUuid(DbSession dbSession, String uuid) { return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid)); } + + /** + * All the deliveries for the specified component. Results are ordered by descending date. + */ + public List<WebhookDeliveryLiteDto> selectOrderedByComponentUuid(DbSession dbSession, String componentUuid) { + return mapper(dbSession).selectOrderedByComponentUuid(componentUuid); + } + + /** + * All the deliveries for the specified CE task. Results are ordered by descending date. + */ + public List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(DbSession dbSession, String ceTaskUuid) { + return mapper(dbSession).selectOrderedByCeTaskUuid(ceTaskUuid); + } + public void insert(DbSession dbSession, WebhookDeliveryDto dto) { mapper(dbSession).insert(dto); } diff --git a/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java b/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java index 0d39cc3bfe0..c89dbc767d8 100644 --- a/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java @@ -19,6 +19,7 @@ */ package org.sonar.db.webhook; +import java.util.List; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; @@ -27,6 +28,10 @@ public interface WebhookDeliveryMapper { @CheckForNull WebhookDeliveryDto selectByUuid(@Param("uuid") String uuid); + List<WebhookDeliveryLiteDto> selectOrderedByComponentUuid(@Param("componentUuid") String componentUuid); + + List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(@Param("ceTaskUuid") String ceTaskUuid); + void insert(WebhookDeliveryDto dto); void deleteComponentBeforeDate(@Param("componentUuid") String componentUuid, @Param("beforeDate") long beforeDate); diff --git a/sonar-db/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml b/sonar-db/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml index ac90c321045..dc178b7d4d1 100644 --- a/sonar-db/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml @@ -4,8 +4,7 @@ <mapper namespace="org.sonar.db.webhook.WebhookDeliveryMapper"> - <select id="selectByUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryDto"> - select + <sql id="sqlLiteColumns"> uuid, component_uuid as componentUuid, ce_task_uuid as ceTaskUuid, @@ -14,13 +13,32 @@ success, http_status as httpStatus, duration_ms as durationMs, - payload, - error_stacktrace as errorStacktrace, created_at as createdAt + </sql> + + <select id="selectByUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryDto"> + select + <include refid="sqlLiteColumns" />, + payload, + error_stacktrace as errorStacktrace from webhook_deliveries where uuid = #{uuid,jdbcType=VARCHAR} </select> + <select id="selectOrderedByComponentUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto"> + select <include refid="sqlLiteColumns" /> + from webhook_deliveries + where component_uuid = #{componentUuid,jdbcType=VARCHAR} + order by created_at desc + </select> + + <select id="selectOrderedByCeTaskUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto"> + select <include refid="sqlLiteColumns" /> + from webhook_deliveries + where ce_task_uuid = #{ceTaskUuid,jdbcType=VARCHAR} + order by created_at desc + </select> + <insert id="insert" parameterType="org.sonar.db.webhook.WebhookDeliveryDto" useGeneratedKeys="false"> insert into webhook_deliveries ( uuid, diff --git a/sonar-db/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java b/sonar-db/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java index 8445f7f69b6..17cc45b046a 100644 --- a/sonar-db/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/webhook/WebhookDeliveryDaoTest.java @@ -35,7 +35,8 @@ import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto; public class WebhookDeliveryDaoTest { - private static final long DATE_1 = 1_999_000L; + private static final long NOW = 1_500_000_000L; + private static final long BEFORE = NOW - 1_000L; @Rule public final DbTester dbTester = DbTester.create(System2.INSTANCE).setDisableDefaultOrganization(true); @@ -53,6 +54,52 @@ public class WebhookDeliveryDaoTest { } @Test + public void selectOrderedByComponentUuid_returns_empty_if_no_records() { + underTest.insert(dbSession, newDto("D1", "COMPONENT_1", "TASK_1")); + + List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "ANOTHER_COMPONENT"); + + assertThat(deliveries).isEmpty(); + } + + @Test + public void selectOrderedByComponentUuid_returns_records_ordered_by_date() { + WebhookDeliveryDto dto1 = newDto("D1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); + WebhookDeliveryDto dto2 = newDto("D2", "COMPONENT_1", "TASK_1").setCreatedAt(NOW); + WebhookDeliveryDto dto3 = newDto("D3", "COMPONENT_2", "TASK_1").setCreatedAt(NOW); + underTest.insert(dbSession, dto3); + underTest.insert(dbSession, dto2); + underTest.insert(dbSession, dto1); + + List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "COMPONENT_1"); + + assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1"); + } + + @Test + public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() { + underTest.insert(dbSession, newDto("D1", "COMPONENT_1", "TASK_1")); + + List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "ANOTHER_TASK"); + + assertThat(deliveries).isEmpty(); + } + + @Test + public void selectOrderedByCeTaskUuid_returns_records_ordered_by_date() { + WebhookDeliveryDto dto1 = newDto("D1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); + WebhookDeliveryDto dto2 = newDto("D2", "COMPONENT_1", "TASK_1").setCreatedAt(NOW); + WebhookDeliveryDto dto3 = newDto("D3", "COMPONENT_2", "TASK_2").setCreatedAt(NOW); + underTest.insert(dbSession, dto3); + underTest.insert(dbSession, dto2); + underTest.insert(dbSession, dto1); + + List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "TASK_1"); + + assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1"); + } + + @Test public void insert_row_with_only_mandatory_columns() { WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1") .setHttpStatus(null) |