Skip to content

Commit

Permalink
SONAR-10347 Add pagination to webhook deliveries search ws.
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Jambet authored and gjambet committed Mar 1, 2018
1 parent 3ae1835 commit d535686
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 58 deletions.
Expand Up @@ -21,6 +21,7 @@


import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.apache.ibatis.session.RowBounds;
import org.sonar.db.Dao; import org.sonar.db.Dao;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;


Expand All @@ -30,25 +31,37 @@ public Optional<WebhookDeliveryDto> selectByUuid(DbSession dbSession, String uui
return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid)); return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid));
} }


public int countDeliveriesByWebhookUuid(DbSession dbSession, String webhookUuid) {
return mapper(dbSession).countByWebhookUuid(webhookUuid);
}

/** /**
* All the deliveries for the specified webhook. Results are ordered by descending date. * All the deliveries for the specified webhook. Results are ordered by descending date.
*/ */
public List<WebhookDeliveryLiteDto> selectByWebhookUuid(DbSession dbSession, String webhookUuid) { public List<WebhookDeliveryLiteDto> selectByWebhookUuid(DbSession dbSession, String webhookUuid, int offset, int limit) {
return mapper(dbSession).selectByWebhookUuid(webhookUuid); return mapper(dbSession).selectByWebhookUuid(webhookUuid, new RowBounds(offset, limit));
}

public int countDeliveriesByComponentUuid(DbSession dbSession, String componentUuid) {
return mapper(dbSession).countByComponentUuid(componentUuid);
} }


/** /**
* All the deliveries for the specified component. Results are ordered by descending date. * All the deliveries for the specified component. Results are ordered by descending date.
*/ */
public List<WebhookDeliveryLiteDto> selectOrderedByComponentUuid(DbSession dbSession, String componentUuid) { public List<WebhookDeliveryLiteDto> selectOrderedByComponentUuid(DbSession dbSession, String componentUuid, int offset, int limit) {
return mapper(dbSession).selectOrderedByComponentUuid(componentUuid); return mapper(dbSession).selectOrderedByComponentUuid(componentUuid, new RowBounds(offset, limit));
}

public int countDeliveriesByCeTaskUuid(DbSession dbSession, String ceTaskId) {
return mapper(dbSession).countByCeTaskUuid(ceTaskId);
} }


/** /**
* All the deliveries for the specified CE task. Results are ordered by descending date. * All the deliveries for the specified CE task. Results are ordered by descending date.
*/ */
public List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(DbSession dbSession, String ceTaskUuid) { public List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(DbSession dbSession, String ceTaskUuid, int offset, int limit) {
return mapper(dbSession).selectOrderedByCeTaskUuid(ceTaskUuid); return mapper(dbSession).selectOrderedByCeTaskUuid(ceTaskUuid, new RowBounds(offset, limit));
} }


public void insert(DbSession dbSession, WebhookDeliveryDto dto) { public void insert(DbSession dbSession, WebhookDeliveryDto dto) {
Expand Down
Expand Up @@ -22,17 +22,24 @@
import java.util.List; import java.util.List;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;


public interface WebhookDeliveryMapper { public interface WebhookDeliveryMapper {


@CheckForNull @CheckForNull
WebhookDeliveryDto selectByUuid(@Param("uuid") String uuid); WebhookDeliveryDto selectByUuid(@Param("uuid") String uuid);


List<WebhookDeliveryLiteDto> selectByWebhookUuid(@Param("webhookUuid") String webhookUuid); int countByWebhookUuid(@Param("webhookUuid") String webhookUuid);


List<WebhookDeliveryLiteDto> selectOrderedByComponentUuid(@Param("componentUuid") String componentUuid); List<WebhookDeliveryLiteDto> selectByWebhookUuid(@Param("webhookUuid") String webhookUuid, RowBounds rowBounds);


List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(@Param("ceTaskUuid") String ceTaskUuid); int countByComponentUuid(@Param("componentUuid") String componentUuid);

List<WebhookDeliveryLiteDto> selectOrderedByComponentUuid(@Param("componentUuid") String componentUuid, RowBounds rowBounds);

int countByCeTaskUuid(@Param("ceTaskUuid") String ceTaskId);

List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(@Param("ceTaskUuid") String ceTaskUuid, RowBounds rowBounds);


void insert(WebhookDeliveryDto dto); void insert(WebhookDeliveryDto dto);


Expand Down
Expand Up @@ -26,13 +26,27 @@
where uuid = #{uuid,jdbcType=VARCHAR} where uuid = #{uuid,jdbcType=VARCHAR}
</select> </select>


<select id="countByWebhookUuid" parameterType="String" resultType="int">
select
count(1)
from webhook_deliveries
where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR}
</select>

<select id="selectByWebhookUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto"> <select id="selectByWebhookUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
select <include refid="sqlLiteColumns" /> select <include refid="sqlLiteColumns" />
from webhook_deliveries from webhook_deliveries
where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR} where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR}
order by created_at desc order by created_at desc
</select> </select>


<select id="countByComponentUuid" parameterType="String" resultType="int">
select
count(1)
from webhook_deliveries
where component_uuid = #{componentUuid,jdbcType=VARCHAR}
</select>

<select id="selectOrderedByComponentUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto"> <select id="selectOrderedByComponentUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
select <include refid="sqlLiteColumns" /> select <include refid="sqlLiteColumns" />
from webhook_deliveries from webhook_deliveries
Expand All @@ -47,6 +61,13 @@
order by created_at desc order by created_at desc
</select> </select>


<select id="countByCeTaskUuid" parameterType="String" resultType="int">
select
count(1)
from webhook_deliveries
where ce_task_uuid = #{ceTaskUuid,jdbcType=VARCHAR}
</select>

<insert id="insert" parameterType="org.sonar.db.webhook.WebhookDeliveryDto" useGeneratedKeys="false"> <insert id="insert" parameterType="org.sonar.db.webhook.WebhookDeliveryDto" useGeneratedKeys="false">
insert into webhook_deliveries ( insert into webhook_deliveries (
uuid, uuid,
Expand Down
Expand Up @@ -46,6 +46,8 @@ public class WebhookDeliveryDaoTest {


private final DbClient dbClient = dbTester.getDbClient(); private final DbClient dbClient = dbTester.getDbClient();
private final DbSession dbSession = dbTester.getSession(); private final DbSession dbSession = dbTester.getSession();
private final WebhookDbTester dbWebhooks = dbTester.webhooks();

private final WebhookDeliveryDao underTest = dbClient.webhookDeliveryDao(); private final WebhookDeliveryDao underTest = dbClient.webhookDeliveryDao();


@Test @Test
Expand All @@ -55,53 +57,97 @@ public void selectByUuid_returns_empty_if_uuid_does_not_exist() {


@Test @Test
public void selectOrderedByComponentUuid_returns_empty_if_no_records() { public void selectOrderedByComponentUuid_returns_empty_if_no_records() {
underTest.insert(dbSession, newDto("D1", "COMPONENT_1", "TASK_1")); underTest.insert(dbSession, newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1"));


List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "ANOTHER_COMPONENT"); List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "ANOTHER_COMPONENT", 0, 10);


assertThat(deliveries).isEmpty(); assertThat(deliveries).isEmpty();
} }


@Test @Test
public void selectOrderedByComponentUuid_returns_records_ordered_by_date() { public void selectOrderedByComponentUuid_returns_records_ordered_by_date() {
WebhookDeliveryDto dto1 = newDto("D1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); WebhookDeliveryDto dto1 = newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE);
WebhookDeliveryDto dto2 = newDto("D2", "COMPONENT_1", "TASK_1").setCreatedAt(NOW); WebhookDeliveryDto dto2 = newDto("D2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(NOW);
WebhookDeliveryDto dto3 = newDto("D3", "COMPONENT_2", "TASK_1").setCreatedAt(NOW); WebhookDeliveryDto dto3 = newDto("D3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_1").setCreatedAt(NOW);
underTest.insert(dbSession, dto3); underTest.insert(dbSession, dto3);
underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto2);
underTest.insert(dbSession, dto1); underTest.insert(dbSession, dto1);


List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "COMPONENT_1"); List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "COMPONENT_1", 0, 10);


assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1"); assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
} }


@Test @Test
public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() { public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() {
underTest.insert(dbSession, newDto("D1", "COMPONENT_1", "TASK_1")); underTest.insert(dbSession, newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1"));


List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "ANOTHER_TASK"); List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "ANOTHER_TASK", 0, 10);


assertThat(deliveries).isEmpty(); assertThat(deliveries).isEmpty();
} }


@Test @Test
public void selectOrderedByCeTaskUuid_returns_records_ordered_by_date() { public void selectOrderedByCeTaskUuid_returns_records_ordered_by_date() {
WebhookDeliveryDto dto1 = newDto("D1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE); WebhookDeliveryDto dto1 = newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE);
WebhookDeliveryDto dto2 = newDto("D2", "COMPONENT_1", "TASK_1").setCreatedAt(NOW); WebhookDeliveryDto dto2 = newDto("D2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(NOW);
WebhookDeliveryDto dto3 = newDto("D3", "COMPONENT_2", "TASK_2").setCreatedAt(NOW); WebhookDeliveryDto dto3 = newDto("D3", "WEBHOOK_UUID_1", "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", 0, 10);

assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
}

@Test
public void selectByWebhookUuid_returns_empty_if_no_records() {

underTest.insert(dbSession, newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1"));

List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, "a-webhook-uuid", 0, 10);

assertThat(deliveries).isEmpty();
}

@Test
public void selectByWebhookUuid_returns_records_ordered_by_date() {
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"));
WebhookDeliveryDto dto1 = newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE);
WebhookDeliveryDto dto2 = newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW);
WebhookDeliveryDto dto3 = newDto("D3", "fake-webhook-uuid", "COMPONENT_2", "TASK_1").setCreatedAt(NOW);
underTest.insert(dbSession, dto3); underTest.insert(dbSession, dto3);
underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto2);
underTest.insert(dbSession, dto1); underTest.insert(dbSession, dto1);


List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "TASK_1"); List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), 0, 10);


assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1"); assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
} }


@Test
public void selectByWebhookUuid_returns_records_according_to_pagination() {
WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"));
WebhookDeliveryDto dto1 = newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE);
underTest.insert(dbSession, dto1);
WebhookDeliveryDto dto2 = newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW);
underTest.insert(dbSession, dto2);
underTest.insert(dbSession, newDto("D3", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
underTest.insert(dbSession, newDto("D4", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
underTest.insert(dbSession, newDto("D5", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
underTest.insert(dbSession, newDto("D6", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));

List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), 1, 3);

assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactlyInAnyOrder("D3", "D4", "D5");
}



@Test @Test
public void insert_row_with_only_mandatory_columns() { public void insert_row_with_only_mandatory_columns() {
WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1") WebhookDeliveryDto dto = newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1")
.setHttpStatus(null) .setHttpStatus(null)
.setDurationMs(null) .setDurationMs(null)
.setErrorStacktrace(null); .setErrorStacktrace(null);
Expand All @@ -119,22 +165,23 @@ public void insert_row_with_only_mandatory_columns() {


@Test @Test
public void insert_row_with_all_columns() { public void insert_row_with_all_columns() {
WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1"); WebhookDeliveryDto dto = newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1");


underTest.insert(dbSession, dto); underTest.insert(dbSession, dto);


WebhookDeliveryDto stored = selectByUuid(dto.getUuid()); WebhookDeliveryDto stored = selectByUuid(dto.getUuid());
verifyMandatoryFields(dto, stored); verifyMandatoryFields(dto, stored);
assertThat(stored.getWebhookUuid()).isEqualTo(dto.getWebhookUuid());
assertThat(stored.getHttpStatus()).isEqualTo(dto.getHttpStatus()); assertThat(stored.getHttpStatus()).isEqualTo(dto.getHttpStatus());
assertThat(stored.getDurationMs()).isEqualTo(dto.getDurationMs()); assertThat(stored.getDurationMs()).isEqualTo(dto.getDurationMs());
assertThat(stored.getErrorStacktrace()).isEqualTo(dto.getErrorStacktrace()); assertThat(stored.getErrorStacktrace()).isEqualTo(dto.getErrorStacktrace());
} }


@Test @Test
public void deleteComponentBeforeDate_deletes_rows_before_date() { public void deleteComponentBeforeDate_deletes_rows_before_date() {
underTest.insert(dbSession, newDto("DELIVERY_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L)); underTest.insert(dbSession, newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L));
underTest.insert(dbSession, newDto("DELIVERY_2", "COMPONENT_1", "TASK_2").setCreatedAt(2_000_000L)); underTest.insert(dbSession, newDto("DELIVERY_2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_2").setCreatedAt(2_000_000L));
underTest.insert(dbSession, newDto("DELIVERY_3", "COMPONENT_2", "TASK_3").setCreatedAt(1_000_000L)); underTest.insert(dbSession, newDto("DELIVERY_3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_3").setCreatedAt(1_000_000L));


// should delete the old delivery on COMPONENT_1 and keep the one of COMPONENT_2 // should delete the old delivery on COMPONENT_1 and keep the one of COMPONENT_2
underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_1", 1_500_000L); underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_1", 1_500_000L);
Expand All @@ -152,7 +199,7 @@ public void deleteComponentBeforeDate_does_nothing_on_empty_table() {


@Test @Test
public void deleteComponentBeforeDate_does_nothing_on_invalid_uuid() { public void deleteComponentBeforeDate_does_nothing_on_invalid_uuid() {
underTest.insert(dbSession, newDto("DELIVERY_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L)); underTest.insert(dbSession, newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L));


underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_2", 1_500_000L); underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_2", 1_500_000L);


Expand All @@ -174,9 +221,10 @@ private void verifyMandatoryFields(WebhookDeliveryDto expected, WebhookDeliveryD
* Build a {@link WebhookDeliveryDto} with all mandatory fields. * Build a {@link WebhookDeliveryDto} with all mandatory fields.
* Optional fields are kept null. * Optional fields are kept null.
*/ */
private static WebhookDeliveryDto newDto(String uuid, String componentUuid, String ceTaskUuid) { private static WebhookDeliveryDto newDto(String uuid, String webhookUuid, String componentUuid, String ceTaskUuid) {
return newWebhookDeliveryDto() return newWebhookDeliveryDto()
.setUuid(uuid) .setUuid(uuid)
.setWebhookUuid(webhookUuid)
.setComponentUuid(componentUuid) .setComponentUuid(componentUuid)
.setCeTaskUuid(ceTaskUuid); .setCeTaskUuid(ceTaskUuid);
} }
Expand Down
Expand Up @@ -36,6 +36,11 @@ public static WebhookDto newWebhook(ComponentDto project) {
.setProjectUuid(project.uuid()); .setProjectUuid(project.uuid());
} }


public static WebhookDto newProjectWebhook(String projectUuid) {
return getWebhookDto()
.setProjectUuid(projectUuid);
}

public static WebhookDto newWebhook(OrganizationDto organizationDto) { public static WebhookDto newWebhook(OrganizationDto organizationDto) {
return getWebhookDto() return getWebhookDto()
.setOrganizationUuid(organizationDto.getUuid()); .setOrganizationUuid(organizationDto.getUuid());
Expand Down

0 comments on commit d535686

Please sign in to comment.