From feda9c29f81ed7a1628afb9fbb5e47824f2f0c35 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 10 Nov 2016 12:02:26 +0100 Subject: [PATCH] SONAR-8353 purge webhook deliveries when deleting project --- .../webhook/WebhookDeliveryStorageTest.java | 15 ++--- .../org/sonar/db/purge/PurgeCommands.java | 7 +++ .../java/org/sonar/db/purge/PurgeDao.java | 1 + .../java/org/sonar/db/purge/PurgeMapper.java | 2 + .../org/sonar/db/purge/PurgeMapper.xml | 4 ++ .../java/org/sonar/db/purge/PurgeDaoTest.java | 12 ++++ .../sonar/db/webhook/WebhookDbTesting.java | 58 +++++++++++++++++++ .../db/webhook/WebhookDeliveryDaoTest.java | 21 +++---- 8 files changed, 96 insertions(+), 24 deletions(-) create mode 100644 sonar-db/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookDeliveryStorageTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookDeliveryStorageTest.java index 5b5fe6e999e..145a411f5bc 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookDeliveryStorageTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookDeliveryStorageTest.java @@ -20,8 +20,6 @@ package org.sonar.server.computation.task.projectanalysis.webhook; import java.io.IOException; -import java.util.List; -import java.util.Map; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; @@ -31,10 +29,11 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.webhook.WebhookDeliveryDto; -import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto; +import static org.sonar.db.webhook.WebhookDbTesting.selectAllDeliveryUuids; public class WebhookDeliveryStorageTest { @@ -96,9 +95,8 @@ public class WebhookDeliveryStorageTest { underTest.purge("PROJECT_1"); - List> uuids = dbTester.select(dbSession, "select uuid as \"uuid\" from webhook_deliveries"); // do not purge another project PROJECT_2 - assertThat(uuids).extracting(column -> column.get("uuid")).containsOnly("D2", "D3"); + assertThat(selectAllDeliveryUuids(dbTester, dbSession)).containsOnly("D2", "D3"); } private static WebhookDelivery.Builder newBuilderTemplate() { @@ -111,14 +109,9 @@ public class WebhookDeliveryStorageTest { } private static WebhookDeliveryDto newDto(String uuid, String componentUuid, long at) { - return new WebhookDeliveryDto() + return newWebhookDeliveryDto() .setUuid(uuid) .setComponentUuid(componentUuid) - .setCeTaskUuid(randomAlphanumeric(40)) - .setName("Jenkins") - .setUrl("http://jenkins") - .setSuccess(true) - .setPayload("{json}") .setCreatedAt(at); } } diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java index f77dfc2cdc8..4ce1529558e 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java @@ -210,4 +210,11 @@ class PurgeCommands { session.commit(); profiler.stop(); } + + public void deleteWebhookDeliveries(String rootUuid) { + profiler.start("deleteWebhookDeliveries (webhook_deliveries)"); + purgeMapper.deleteWebhookDeliveriesByProjectUuid(rootUuid); + session.commit(); + profiler.stop(); + } } diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java index aba3ff7e704..9621fdbf1f7 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -162,6 +162,7 @@ public class PurgeDao implements Dao { commands.deleteComponents(childrenIds); commands.deleteFileSources(rootUuid); commands.deleteCeActivity(rootUuid); + commands.deleteWebhookDeliveries(rootUuid); } public void deleteAnalyses(DbSession session, PurgeProfiler profiler, List analysisIdUuids) { diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java index 91cb547e29f..c5662ec929d 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java @@ -89,4 +89,6 @@ public interface PurgeMapper { void deleteFileSourcesByUuid(@Param("fileUuids") List fileUuids); void deleteCeActivityByProjectUuid(String projectUuid); + + void deleteWebhookDeliveriesByProjectUuid(@Param("projectUuid") String projectUuid); } diff --git a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index fa9fd4b78c5..8e8424fc56a 100644 --- a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -319,5 +319,9 @@ delete from ce_activity where component_uuid=#{rootProjectUuid} + + delete from webhook_deliveries where component_uuid=#{projectUuid,jdbcType=VARCHAR} + + diff --git a/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java index e2869998727..b73b4e52c20 100644 --- a/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/purge/PurgeDaoTest.java @@ -43,6 +43,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.db.ce.CeTaskTypes.REPORT; +import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto; +import static org.sonar.db.webhook.WebhookDbTesting.selectAllDeliveryUuids; public class PurgeDaoTest { @@ -210,6 +212,16 @@ public class PurgeDaoTest { dbTester.assertDbUnit(getClass(), "should_delete_all_closed_issues-result.xml", "issues", "issue_changes"); } + @Test + public void deleteProject_deletes_webhook_deliveries() { + dbClient.webhookDeliveryDao().insert(dbSession, newWebhookDeliveryDto().setComponentUuid("P1").setUuid("D1")); + dbClient.webhookDeliveryDao().insert(dbSession, newWebhookDeliveryDto().setComponentUuid("P2").setUuid("D2")); + + underTest.deleteProject(dbSession, "P1"); + + assertThat(selectAllDeliveryUuids(dbTester, dbSession)).containsOnly("D2"); + } + private CeActivityDto insertCeActivity(String componentUuid) { CeQueueDto queueDto = new CeQueueDto(); queueDto.setUuid(Uuids.create()); diff --git a/sonar-db/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java b/sonar-db/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java new file mode 100644 index 00000000000..2ba3de86b1a --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.webhook; + +import java.util.List; +import java.util.stream.Collectors; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang.math.RandomUtils.nextBoolean; +import static org.apache.commons.lang.math.RandomUtils.nextInt; +import static org.apache.commons.lang.math.RandomUtils.nextLong; + +public class WebhookDbTesting { + + private WebhookDbTesting() { + // only statics + } + + public static WebhookDeliveryDto newWebhookDeliveryDto() { + return new WebhookDeliveryDto() + .setUuid(randomAlphanumeric(40)) + .setComponentUuid(randomAlphanumeric(40)) + .setCeTaskUuid(randomAlphanumeric(40)) + .setName(randomAlphanumeric(10)) + .setUrl(randomAlphanumeric(10)) + .setDurationMs(nextInt()) + .setHttpStatus(nextInt()) + .setSuccess(nextBoolean()) + .setPayload(randomAlphanumeric(10)) + .setCreatedAt(nextLong()); + } + + public static List selectAllDeliveryUuids(DbTester dbTester, DbSession dbSession) { + return dbTester.select(dbSession, "select uuid as \"uuid\" from webhook_deliveries") + .stream() + .map(columns -> (String)columns.get("uuid")) + .collect(Collectors.toList()); + } +} 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 8b3734c8de2..8445f7f69b6 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 @@ -31,6 +31,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.webhook.WebhookDbTesting.newWebhookDeliveryDto; public class WebhookDeliveryDaoTest { @@ -53,7 +54,10 @@ public class WebhookDeliveryDaoTest { @Test public void insert_row_with_only_mandatory_columns() { - WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1"); + WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1") + .setHttpStatus(null) + .setDurationMs(null) + .setErrorStacktrace(null); underTest.insert(dbSession, dto); @@ -68,11 +72,7 @@ public class WebhookDeliveryDaoTest { @Test public void insert_row_with_all_columns() { - WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1") - .setDurationMs(10000) - .setHttpStatus(200) - .setErrorStacktrace("timeout") - .setPayload("{json}"); + WebhookDeliveryDto dto = newDto("DELIVERY_1", "COMPONENT_1", "TASK_1"); underTest.insert(dbSession, dto); @@ -128,15 +128,10 @@ public class WebhookDeliveryDaoTest { * Optional fields are kept null. */ private static WebhookDeliveryDto newDto(String uuid, String componentUuid, String ceTaskUuid) { - return new WebhookDeliveryDto() + return newWebhookDeliveryDto() .setUuid(uuid) .setComponentUuid(componentUuid) - .setCeTaskUuid(ceTaskUuid) - .setName("Jenkins") - .setUrl("http://jenkins") - .setSuccess(true) - .setPayload("{json}") - .setCreatedAt(DATE_1); + .setCeTaskUuid(ceTaskUuid); } private WebhookDeliveryDto selectByUuid(String uuid) { -- 2.39.5