From 976aa63a8ee9bf1df31eb23d5085a67074f0d5e7 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 10 Nov 2016 18:28:57 +0100 Subject: SONAR-8353 add WS api/webhooks/deliveries --- .../org/sonar/db/webhook/WebhookDeliveryDao.java | 16 +++++++ .../sonar/db/webhook/WebhookDeliveryMapper.java | 5 +++ .../org/sonar/db/webhook/WebhookDeliveryMapper.xml | 26 ++++++++++-- .../sonar/db/webhook/WebhookDeliveryDaoTest.java | 49 +++++++++++++++++++++- 4 files changed, 91 insertions(+), 5 deletions(-) (limited to 'sonar-db') 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 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 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 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 selectOrderedByComponentUuid(@Param("componentUuid") String componentUuid); + + List 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 @@ - + select + , + payload, + error_stacktrace as errorStacktrace from webhook_deliveries where uuid = #{uuid,jdbcType=VARCHAR} + + + + 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); @@ -52,6 +53,52 @@ public class WebhookDeliveryDaoTest { assertThat(underTest.selectByUuid(dbSession, "missing")).isEmpty(); } + @Test + public void selectOrderedByComponentUuid_returns_empty_if_no_records() { + underTest.insert(dbSession, newDto("D1", "COMPONENT_1", "TASK_1")); + + List 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 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 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 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") -- cgit v1.2.3