You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebhookDeliveryDaoTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.webhook;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.DbTester;
  31. import static com.google.common.collect.ImmutableList.of;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.sonar.db.webhook.WebhookTesting.newProjectWebhook;
  34. public class WebhookDeliveryDaoTest {
  35. private static final long NOW = 1_500_000_000L;
  36. private static final long BEFORE = NOW - 1_000L;
  37. @Rule
  38. public final DbTester dbTester = DbTester.create(System2.INSTANCE);
  39. @Rule
  40. public ExpectedException expectedException = ExpectedException.none();
  41. private final DbClient dbClient = dbTester.getDbClient();
  42. private final DbSession dbSession = dbTester.getSession();
  43. private final WebhookDbTester dbWebhooks = dbTester.webhooks();
  44. private final WebhookDeliveryDao underTest = dbClient.webhookDeliveryDao();
  45. @Test
  46. public void selectByUuid_returns_empty_if_uuid_does_not_exist() {
  47. assertThat(underTest.selectByUuid(dbSession, "missing")).isEmpty();
  48. }
  49. @Test
  50. public void selectOrderedByComponentUuid_returns_empty_if_no_records() {
  51. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1"));
  52. List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "ANOTHER_COMPONENT", 0, 10);
  53. assertThat(deliveries).isEmpty();
  54. }
  55. @Test
  56. public void selectOrderedByComponentUuid_returns_records_ordered_by_date() {
  57. WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE);
  58. WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(NOW);
  59. WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_1").setCreatedAt(NOW);
  60. underTest.insert(dbSession, dto3);
  61. underTest.insert(dbSession, dto2);
  62. underTest.insert(dbSession, dto1);
  63. List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByComponentUuid(dbSession, "COMPONENT_1", 0, 10);
  64. assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
  65. }
  66. @Test
  67. public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() {
  68. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1"));
  69. List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "ANOTHER_TASK", 0, 10);
  70. assertThat(deliveries).isEmpty();
  71. }
  72. @Test
  73. public void selectOrderedByCeTaskUuid_returns_records_ordered_by_date() {
  74. WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE);
  75. WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(NOW);
  76. WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_2").setCreatedAt(NOW);
  77. underTest.insert(dbSession, dto3);
  78. underTest.insert(dbSession, dto2);
  79. underTest.insert(dbSession, dto1);
  80. List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "TASK_1", 0, 10);
  81. assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
  82. }
  83. @Test
  84. public void selectByWebhookUuid_returns_empty_if_no_records() {
  85. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1"));
  86. List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, "a-webhook-uuid", 0, 10);
  87. assertThat(deliveries).isEmpty();
  88. }
  89. @Test
  90. public void selectByWebhookUuid_returns_records_ordered_by_date() {
  91. WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"));
  92. WebhookDeliveryDto dto1 = WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE);
  93. WebhookDeliveryDto dto2 = WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW);
  94. WebhookDeliveryDto dto3 = WebhookDeliveryTesting.newDto("D3", "fake-webhook-uuid", "COMPONENT_2", "TASK_1").setCreatedAt(NOW);
  95. underTest.insert(dbSession, dto3);
  96. underTest.insert(dbSession, dto2);
  97. underTest.insert(dbSession, dto1);
  98. List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), 0, 10);
  99. assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
  100. }
  101. @Test
  102. public void selectByWebhookUuid_returns_records_according_to_pagination() {
  103. WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"));
  104. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 5_000L));
  105. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 4_000L));
  106. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D3", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 3_000L));
  107. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D4", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 2_000L));
  108. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D5", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW - 1_000L));
  109. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D6", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
  110. List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), 2, 2);
  111. assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D4", "D3");
  112. }
  113. @Test
  114. public void selectLatestDelivery_of_a_webhook() {
  115. WebhookDto webhook1 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1"));
  116. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-1-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE));
  117. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH1-DELIVERY-2-UUID", webhook1.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
  118. WebhookDto webhook2 = dbWebhooks.insert(newProjectWebhook("COMPONENT_1"));
  119. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-1-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(BEFORE));
  120. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("WH2-DELIVERY-2-UUID", webhook2.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(NOW));
  121. Map<String, WebhookDeliveryLiteDto> map = underTest.selectLatestDeliveries(dbSession, of(webhook1, webhook2));
  122. assertThat(map).containsKeys(webhook1.getUuid());
  123. assertThat(map.get(webhook1.getUuid())).extracting(WebhookDeliveryLiteDto::getUuid).isEqualTo("WH1-DELIVERY-2-UUID");
  124. assertThat(map).containsKeys(webhook2.getUuid());
  125. assertThat(map.get(webhook2.getUuid())).extracting(WebhookDeliveryLiteDto::getUuid).isEqualTo("WH2-DELIVERY-2-UUID");
  126. }
  127. @Test
  128. public void insert_row_with_only_mandatory_columns() {
  129. WebhookDeliveryDto dto = WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1")
  130. .setDurationMs(1000)
  131. .setHttpStatus(null)
  132. .setErrorStacktrace(null);
  133. underTest.insert(dbSession, dto);
  134. WebhookDeliveryDto stored = selectByUuid(dto.getUuid());
  135. verifyMandatoryFields(dto, stored);
  136. assertThat(stored.getDurationMs()).isEqualTo(1000);
  137. // optional fields are null
  138. assertThat(stored.getHttpStatus()).isNull();
  139. assertThat(stored.getErrorStacktrace()).isNull();
  140. }
  141. @Test
  142. public void insert_row_with_all_columns() {
  143. WebhookDeliveryDto dto = WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1");
  144. underTest.insert(dbSession, dto);
  145. WebhookDeliveryDto stored = selectByUuid(dto.getUuid());
  146. verifyMandatoryFields(dto, stored);
  147. assertThat(stored.getWebhookUuid()).isEqualTo(dto.getWebhookUuid());
  148. assertThat(stored.getHttpStatus()).isEqualTo(dto.getHttpStatus());
  149. assertThat(stored.getDurationMs()).isEqualTo(dto.getDurationMs());
  150. assertThat(stored.getErrorStacktrace()).isEqualTo(dto.getErrorStacktrace());
  151. }
  152. @Test
  153. public void deleteByWebhook() {
  154. WebhookDto webhookDto = dbWebhooks.insert(WebhookTesting.newProjectWebhook("COMPONENT_1"));
  155. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", webhookDto.getUuid(), "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L));
  156. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_2", webhookDto.getUuid(), "COMPONENT_1", "TASK_2").setCreatedAt(2_000_000L));
  157. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_3", "WONT BE DELETED WEBHOOK_UUID_2", "COMPONENT_2", "TASK_3").setCreatedAt(1_000_000L));
  158. underTest.deleteByWebhook(dbSession, webhookDto);
  159. assertThat(dbTester.countRowsOfTable(dbSession, "webhook_deliveries")).isEqualTo(1);
  160. }
  161. @Test
  162. public void deleteComponentBeforeDate_deletes_rows_before_date() {
  163. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L));
  164. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_2", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_2").setCreatedAt(2_000_000L));
  165. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_3", "WEBHOOK_UUID_1", "COMPONENT_2", "TASK_3").setCreatedAt(1_000_000L));
  166. // should delete the old delivery on COMPONENT_1 and keep the one of COMPONENT_2
  167. underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_1", 1_500_000L);
  168. List<Map<String, Object>> uuids = dbTester.select(dbSession, "select uuid as \"uuid\" from webhook_deliveries");
  169. assertThat(uuids).extracting(column -> column.get("uuid")).containsOnly("DELIVERY_2", "DELIVERY_3");
  170. }
  171. @Test
  172. public void deleteComponentBeforeDate_does_nothing_on_empty_table() {
  173. underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_1", 1_500_000L);
  174. assertThat(dbTester.countRowsOfTable(dbSession, "webhook_deliveries")).isZero();
  175. }
  176. @Test
  177. public void deleteComponentBeforeDate_does_nothing_on_invalid_uuid() {
  178. underTest.insert(dbSession, WebhookDeliveryTesting.newDto("DELIVERY_1", "WEBHOOK_UUID_1", "COMPONENT_1", "TASK_1").setCreatedAt(1_000_000L));
  179. underTest.deleteComponentBeforeDate(dbSession, "COMPONENT_2", 1_500_000L);
  180. assertThat(dbTester.countRowsOfTable(dbSession, "webhook_deliveries")).isEqualTo(1);
  181. }
  182. private void verifyMandatoryFields(WebhookDeliveryDto expected, WebhookDeliveryDto actual) {
  183. assertThat(actual.getUuid()).isEqualTo(expected.getUuid());
  184. assertThat(actual.getComponentUuid()).isEqualTo(expected.getComponentUuid());
  185. assertThat(actual.getCeTaskUuid()).isEqualTo(expected.getCeTaskUuid());
  186. assertThat(actual.getName()).isEqualTo(expected.getName());
  187. assertThat(actual.getUrl()).isEqualTo(expected.getUrl());
  188. assertThat(actual.isSuccess()).isEqualTo(expected.isSuccess());
  189. assertThat(actual.getPayload()).isEqualTo(expected.getPayload());
  190. assertThat(actual.getCreatedAt()).isEqualTo(expected.getCreatedAt());
  191. }
  192. private WebhookDeliveryDto selectByUuid(String uuid) {
  193. Optional<WebhookDeliveryDto> dto = underTest.selectByUuid(dbSession, uuid);
  194. assertThat(dto).isPresent();
  195. return dto.get();
  196. }
  197. }