From 332cb13bb6a20434d358db772386a4e87200a9a4 Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Wed, 27 Sep 2017 16:44:33 +0200 Subject: [PATCH] SONAR-9880 Add ANALYSIS_UUID on WEBHOOK_DELIVERIES --- .../org/sonar/db/version/schema-h2.ddl | 4 +- .../db/webhook/WebhookDeliveryLiteDto.java | 16 ++- .../db/webhook/WebhookDeliveryMapper.xml | 2 + .../sonar/db/webhook/WebhookDbTesting.java | 1 + ...AnalysisUuidColumnOnWebhookDeliveries.java | 56 +++++++++ ...idColumnToNullableOnWebhookDeliveries.java | 47 ++++++++ .../AddAnalysisUuidToWebhookDeliveries.java | 49 ++++++++ .../db/migration/version/v67/DbVersion67.java | 2 +- ...ysisUuidColumnOnWebhookDeliveriesTest.java | 109 ++++++++++++++++++ ...lumnToNullableOnWebhookDeliveriesTest.java | 56 +++++++++ ...ddAnalysisUuidToWebhookDeliveriesTest.java | 55 +++++++++ .../version/v67/DbVersion67Test.java | 2 +- .../initial.sql | 17 +++ .../initial.sql | 46 ++++++++ .../initial.sql | 17 +++ .../initial.sql | 16 +++ .../webhook/WebhookPostTask.java | 2 +- .../org/sonar/server/webhook/WebHooks.java | 23 +++- .../sonar/server/webhook/WebHooksImpl.java | 2 +- .../org/sonar/server/webhook/Webhook.java | 12 +- .../webhook/WebhookDeliveryStorage.java | 1 + .../webhook/WebhookPostTaskTest.java | 8 +- .../server/webhook/WebHooksImplTest.java | 8 +- .../server/webhook/WebhookCallerImplTest.java | 17 +-- .../webhook/WebhookDeliveryStorageTest.java | 7 +- 25 files changed, 542 insertions(+), 33 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveries.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveries.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/MakeAnalysisUuidNotNullOnWebhookDeliveriesTest/initial.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest/initial.sql diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index 0aab8ac7160..6afd58a258b 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -678,7 +678,8 @@ CREATE UNIQUE INDEX "USER_TOKENS_LOGIN_NAME" ON "USER_TOKENS" ("LOGIN", "NAME"); CREATE TABLE "WEBHOOK_DELIVERIES" ( "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, "COMPONENT_UUID" VARCHAR(40) NOT NULL, - "CE_TASK_UUID" VARCHAR(40) NOT NULL, + "ANALYSIS_UUID" VARCHAR(40), + "CE_TASK_UUID" VARCHAR(40), "NAME" VARCHAR(100) NOT NULL, "URL" VARCHAR(2000) NOT NULL, "SUCCESS" BOOLEAN NOT NULL, @@ -691,6 +692,7 @@ CREATE TABLE "WEBHOOK_DELIVERIES" ( CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID"); CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID"); CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID"); +CREATE INDEX "ANALYSIS_UUID" ON "WEBHOOK_DELIVERIES" ("ANALYSIS_UUID"); CREATE TABLE "ES_QUEUE" ( diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryLiteDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryLiteDto.java index 22a5aa03cd5..804899768fd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryLiteDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryLiteDto.java @@ -28,8 +28,10 @@ public class WebhookDeliveryLiteDto { protected String uuid; /** Component UUID, can't be null */ protected String componentUuid; - /** Compute Engine task UUID, can't be null */ + /** Compute Engine task UUID, can be null */ protected String ceTaskUuid; + /** analysis UUID, can be null */ + protected String analysisUuid; /** Name, can't be null */ protected String name; protected boolean success; @@ -64,11 +66,21 @@ public class WebhookDeliveryLiteDto { return ceTaskUuid; } - public T setCeTaskUuid(String s) { + public T setCeTaskUuid(@Nullable String s) { this.ceTaskUuid = s; return (T)this; } + @CheckForNull + public String getAnalysisUuid() { + return analysisUuid; + } + + public T setAnalysisUuid(@Nullable String s) { + this.analysisUuid = s; + return (T)this; + } + public String getName() { return name; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml index 2f9c957753e..c13ead5a155 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml @@ -44,6 +44,7 @@ uuid, component_uuid, ce_task_uuid, + analysis_uuid, name, url, success, @@ -56,6 +57,7 @@ #{uuid,jdbcType=VARCHAR}, #{componentUuid,jdbcType=VARCHAR}, #{ceTaskUuid,jdbcType=VARCHAR}, + #{analysisUuid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, #{success,jdbcType=BOOLEAN}, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java b/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java index 4a698207ce3..a6349922bbe 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/webhook/WebhookDbTesting.java @@ -40,6 +40,7 @@ public class WebhookDbTesting { .setUuid(randomAlphanumeric(40)) .setComponentUuid(randomAlphanumeric(40)) .setCeTaskUuid(randomAlphanumeric(40)) + .setAnalysisUuid(randomAlphanumeric(40)) .setName(randomAlphanumeric(10)) .setUrl(randomAlphanumeric(10)) .setDurationMs(nextInt()) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveries.java new file mode 100644 index 00000000000..05b4bd7f1bf --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveries.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v66; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; +import org.sonar.server.platform.db.migration.step.Select; +import org.sonar.server.platform.db.migration.step.SqlStatement; + +public class PopulateAnalysisUuidColumnOnWebhookDeliveries extends DataChange { + + public PopulateAnalysisUuidColumnOnWebhookDeliveries(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + massUpdate.select("SELECT wd.uuid, ca.analysis_uuid " + + " FROM webhook_deliveries wd " + + " INNER JOIN ce_activity ca ON ca.uuid=wd.ce_task_uuid " + + " WHERE wd.analysis_uuid IS NULL AND ca.analysis_uuid IS NOT NULL"); + massUpdate.update("UPDATE webhook_deliveries SET analysis_uuid=? WHERE uuid=?"); + massUpdate.rowPluralName("webhook_deliveries"); + massUpdate.execute(PopulateAnalysisUuidColumnOnWebhookDeliveries::handle); + } + + private static boolean handle(Select.Row row, SqlStatement update) throws SQLException { + String uuid = row.getString(1); + String analysisUuid = row.getString(2); + + update.setString(1, analysisUuid); + update.setString(2, uuid); + + return true; + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java new file mode 100644 index 00000000000..236b3cb61be --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v66; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries extends DdlChange { + + public static final String TABLE_WEBHOOK_DELIVERIES = "webhook_deliveries"; + + public UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), TABLE_WEBHOOK_DELIVERIES) + .updateColumn(VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("ce_task_uuid") + .setLimit(40) + .setIsNullable(true) + .build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveries.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveries.java new file mode 100644 index 00000000000..058adf56345 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveries.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v67; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.def.VarcharColumnDef; +import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class AddAnalysisUuidToWebhookDeliveries extends DdlChange { + + public static final String TABLE = "webhook_deliveries"; + + public static final VarcharColumnDef ANALYSIS_UUID_COLUMN = VarcharColumnDef.newVarcharColumnDefBuilder() + .setColumnName("analysis_uuid") + .setLimit(40) + .setIsNullable(true) + .build(); + + public AddAnalysisUuidToWebhookDeliveries(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new AddColumnsBuilder(getDialect(), TABLE) + .addColumn(ANALYSIS_UUID_COLUMN) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java index b02a6065997..e5b479e4d1d 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67.java @@ -27,6 +27,6 @@ public class DbVersion67 implements DbVersion { public void addSteps(MigrationStepRegistry registry) { registry .add(1830, "Copy deprecated server ID", CopyDeprecatedServerId.class) - ; + .add(1831, "Add webhook_deliveries.analysis_uuid", AddAnalysisUuidToWebhookDeliveries.class); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java new file mode 100644 index 00000000000..a8d73b2ce8f --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.java @@ -0,0 +1,109 @@ +package org.sonar.server.platform.db.migration.version.v66;/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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. + */ + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import javax.annotation.Nullable; +import org.apache.commons.lang.math.RandomUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +public class PopulateAnalysisUuidColumnOnWebhookDeliveriesTest { + private static final String TABLE_WEBHOOK_DELIVERY = "webhook_deliveries"; + private static final String TABLE_CE_ACTIVITIY = "ce_activity"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.class, "initial.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private PopulateAnalysisUuidColumnOnWebhookDeliveries underTest = new PopulateAnalysisUuidColumnOnWebhookDeliveries(db.database()); + + @Test + public void migration_must_set_analysis_uuid() throws SQLException { + String ceTaskUuid = randomAlphanumeric(40); + String analysisUuid = randomAlphanumeric(40); + String webhookDeliveryUuid = randomAlphanumeric(40); + insertWebhookDelivery(webhookDeliveryUuid,null, ceTaskUuid); + insertCeActivity(ceTaskUuid, analysisUuid); + + assertThat(db.countRowsOfTable(TABLE_WEBHOOK_DELIVERY)).isEqualTo(1); + assertThat(db.countRowsOfTable(TABLE_CE_ACTIVITIY)).isEqualTo(1); + underTest.execute(); + + List> maps = selectAllWebhookDeliveries(); + assertThat(maps).hasSize(1); + assertThat(maps.get(0)).containsExactly( + entry("ANALYSIS_UUID", analysisUuid), entry("UUID", webhookDeliveryUuid), entry("CE_TASK_UUID", ceTaskUuid)); + } + + @Test + public void migration_should_be_reentrant() throws SQLException { + for (int i = 0; i < 10; i++) { + insertWebhookDelivery(randomAlphanumeric(40),null, randomAlphanumeric(40)); + insertCeActivity(randomAlphanumeric(40), randomAlphanumeric(40)); + } + + underTest.execute(); + List> firstExecutionResult = selectAllWebhookDeliveries(); + underTest.execute(); + assertThat(selectAllWebhookDeliveries()).isEqualTo(firstExecutionResult); + } + + private List> selectAllWebhookDeliveries() { + return db.select("select uuid, ce_task_uuid, analysis_uuid from webhook_deliveries"); + } + + private void insertCeActivity(String uuid, String analysisUuid) { + db.executeInsert(TABLE_CE_ACTIVITIY, + "UUID", uuid, + "TASK_TYPE", randomAlphanumeric(5), + "ANALYSIS_UUID", analysisUuid, + "STATUS", randomAlphanumeric(5), + "IS_LAST", RandomUtils.nextBoolean(), + "IS_LAST_KEY", randomAlphanumeric(50), + "EXECUTION_COUNT", RandomUtils.nextInt(10), + "SUBMITTED_AT", RandomUtils.nextInt(), + "CREATED_AT", RandomUtils.nextInt(), + "UPDATED_AT", RandomUtils.nextInt() + ); + } + + private void insertWebhookDelivery(String uuid, @Nullable String analysisUuid, String ceTaskUuid) { + db.executeInsert(TABLE_WEBHOOK_DELIVERY, + "UUID", uuid, + "COMPONENT_UUID", randomAlphanumeric(30), + "ANALYSIS_UUID", analysisUuid, + "CE_TASK_UUID", ceTaskUuid, + "NAME", randomAlphanumeric(15), + "URL", randomAlphanumeric(15), + "SUCCESS", RandomUtils.nextBoolean(), + "PAYLOAD", randomAlphanumeric(200), + "CREATED_AT", RandomUtils.nextInt() + ); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java new file mode 100644 index 00000000000..f2d234c44b9 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v66; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest { + private static final String TABLE = "webhook_deliveries"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest.class, "initial.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries underTest = new UpdateCeTaskUuidColumnToNullableOnWebhookDeliveries(db.database()); + + @Test + public void update_column() throws SQLException { + underTest.execute(); + + assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0); + db.assertColumnDefinition(TABLE, "ce_task_uuid", Types.VARCHAR, 40, true); + db.assertColumnDefinition(TABLE, "analysis_uuid", Types.VARCHAR, 40, true); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest.java new file mode 100644 index 00000000000..63a59e46b0d --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.server.platform.db.migration.version.v67; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +public class AddAnalysisUuidToWebhookDeliveriesTest { + + private static final String TABLE = "webhook_deliveries"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(AddAnalysisUuidToWebhookDeliveriesTest.class, "initial.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private AddAnalysisUuidToWebhookDeliveries underTest = new AddAnalysisUuidToWebhookDeliveries(db.database()); + + @Test + public void add_column() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition(TABLE, "analysis_uuid", Types.VARCHAR, 40, true); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java index 41ea47048be..ada1069cc8d 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v67/DbVersion67Test.java @@ -36,7 +36,7 @@ public class DbVersion67Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 1); + verifyMigrationCount(underTest, 2); } } diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/MakeAnalysisUuidNotNullOnWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/MakeAnalysisUuidNotNullOnWebhookDeliveriesTest/initial.sql new file mode 100644 index 00000000000..1d8b219839a --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/MakeAnalysisUuidNotNullOnWebhookDeliveriesTest/initial.sql @@ -0,0 +1,17 @@ +CREATE TABLE "WEBHOOK_DELIVERIES" ( + "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, + "COMPONENT_UUID" VARCHAR(40) NOT NULL, + "ANALYSIS_UUID" VARCHAR(40), + "CE_TASK_UUID" VARCHAR(40), + "NAME" VARCHAR(100) NOT NULL, + "URL" VARCHAR(2000) NOT NULL, + "SUCCESS" BOOLEAN NOT NULL, + "HTTP_STATUS" INT, + "DURATION_MS" INT, + "PAYLOAD" CLOB NOT NULL, + "ERROR_STACKTRACE" CLOB, + "CREATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID"); +CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID"); +CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql new file mode 100644 index 00000000000..de520902cff --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/PopulateAnalysisUuidColumnOnWebhookDeliveriesTest/initial.sql @@ -0,0 +1,46 @@ +CREATE TABLE "CE_ACTIVITY" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "UUID" VARCHAR(40) NOT NULL, + "TASK_TYPE" VARCHAR(15) NOT NULL, + "COMPONENT_UUID" VARCHAR(40) NULL, + "ANALYSIS_UUID" VARCHAR(50) NULL, + "STATUS" VARCHAR(15) NOT NULL, + "IS_LAST" BOOLEAN NOT NULL, + "IS_LAST_KEY" VARCHAR(55) NOT NULL, + "SUBMITTER_LOGIN" VARCHAR(255) NULL, + "WORKER_UUID" VARCHAR(40) NULL, + "EXECUTION_COUNT" INTEGER NOT NULL, + "SUBMITTED_AT" BIGINT NOT NULL, + "STARTED_AT" BIGINT NULL, + "EXECUTED_AT" BIGINT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL, + "EXECUTION_TIME_MS" BIGINT NULL, + "ERROR_MESSAGE" VARCHAR(1000), + "ERROR_STACKTRACE" CLOB(2147483647) +); + +CREATE UNIQUE INDEX "CE_ACTIVITY_UUID" ON "CE_ACTIVITY" ("UUID"); +CREATE INDEX "CE_ACTIVITY_COMPONENT_UUID" ON "CE_ACTIVITY" ("COMPONENT_UUID"); +CREATE INDEX "CE_ACTIVITY_ISLASTKEY" ON "CE_ACTIVITY" ("IS_LAST_KEY"); +CREATE INDEX "CE_ACTIVITY_ISLAST_STATUS" ON "CE_ACTIVITY" ("IS_LAST", "STATUS"); + + +CREATE TABLE "WEBHOOK_DELIVERIES" ( + "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, + "COMPONENT_UUID" VARCHAR(40) NOT NULL, + "ANALYSIS_UUID" VARCHAR(40), + "CE_TASK_UUID" VARCHAR(40), + "NAME" VARCHAR(100) NOT NULL, + "URL" VARCHAR(2000) NOT NULL, + "SUCCESS" BOOLEAN NOT NULL, + "HTTP_STATUS" INT, + "DURATION_MS" INT, + "PAYLOAD" CLOB NOT NULL, + "ERROR_STACKTRACE" CLOB, + "CREATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID"); +CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID"); +CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID"); +CREATE INDEX "ANALYSES_UUID" ON "WEBHOOK_DELIVERIES" ("ANALYSIS_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql new file mode 100644 index 00000000000..0e120b5f08a --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v66/UpdateCeTaskUuidColumnToNullableOnWebhookDeliveriesTest/initial.sql @@ -0,0 +1,17 @@ +CREATE TABLE "WEBHOOK_DELIVERIES" ( + "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, + "COMPONENT_UUID" VARCHAR(40) NOT NULL, + "ANALYSIS_UUID" VARCHAR(40) NULL, + "CE_TASK_UUID" VARCHAR(40) NOT NULL, + "NAME" VARCHAR(100) NOT NULL, + "URL" VARCHAR(2000) NOT NULL, + "SUCCESS" BOOLEAN NOT NULL, + "HTTP_STATUS" INT, + "DURATION_MS" INT, + "PAYLOAD" CLOB NOT NULL, + "ERROR_STACKTRACE" CLOB, + "CREATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID"); +CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID"); +CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest/initial.sql new file mode 100644 index 00000000000..e4509afd200 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v67/AddAnalysisUuidToWebhookDeliveriesTest/initial.sql @@ -0,0 +1,16 @@ +CREATE TABLE "WEBHOOK_DELIVERIES" ( + "UUID" VARCHAR(40) NOT NULL PRIMARY KEY, + "COMPONENT_UUID" VARCHAR(40) NOT NULL, + "CE_TASK_UUID" VARCHAR(40) NOT NULL, + "NAME" VARCHAR(100) NOT NULL, + "URL" VARCHAR(2000) NOT NULL, + "SUCCESS" BOOLEAN NOT NULL, + "HTTP_STATUS" INT, + "DURATION_MS" INT, + "PAYLOAD" CLOB NOT NULL, + "ERROR_STACKTRACE" CLOB, + "CREATED_AT" BIGINT NOT NULL +); +CREATE UNIQUE INDEX "PK_WEBHOOK_DELIVERIES" ON "WEBHOOK_DELIVERIES" ("UUID"); +CREATE INDEX "COMPONENT_UUID" ON "WEBHOOK_DELIVERIES" ("COMPONENT_UUID"); +CREATE INDEX "CE_TASK_UUID" ON "WEBHOOK_DELIVERIES" ("CE_TASK_UUID"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTask.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTask.java index f6112804a54..e489df52c14 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTask.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTask.java @@ -50,7 +50,7 @@ public class WebhookPostTask implements PostProjectAnalysisTask { webHooks.sendProjectAnalysisUpdate( config, - new WebHooks.Analysis(analysis.getProject().getUuid(), analysis.getCeTask().getId()), + new WebHooks.Analysis(analysis.getProject().getUuid(), "", analysis.getCeTask().getId()), // FIXME () -> payloadFactory.create(convert(analysis))); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooks.java b/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooks.java index 35768995ac9..a65a7526375 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooks.java +++ b/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooks.java @@ -19,8 +19,10 @@ */ package org.sonar.server.webhook; -import java.util.Objects; +import com.google.common.base.Objects; import java.util.function.Supplier; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.api.config.Configuration; import static java.util.Objects.requireNonNull; @@ -44,35 +46,44 @@ public interface WebHooks { final class Analysis { private final String projectUuid; private final String ceTaskUuid; + private final String analysisUuid; - public Analysis(String projectUuid, String ceTaskUuid) { + public Analysis(String projectUuid, String analysisUuid, @Nullable String ceTaskUuid) { this.projectUuid = requireNonNull(projectUuid, "projectUuid can't be null"); - this.ceTaskUuid = requireNonNull(ceTaskUuid, "ceTaskUuid can't be null"); + this.analysisUuid = requireNonNull(analysisUuid, "analysisUuid can't be null"); + this.ceTaskUuid = ceTaskUuid; } public String getProjectUuid() { return projectUuid; } + @CheckForNull public String getCeTaskUuid() { return ceTaskUuid; } + public String getAnalysisUuid() { + return analysisUuid; + } + @Override public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { + if (!(o instanceof Analysis)) { return false; } Analysis analysis = (Analysis) o; - return projectUuid.equals(analysis.projectUuid) && ceTaskUuid.equals(analysis.ceTaskUuid); + return Objects.equal(projectUuid, analysis.projectUuid) && + Objects.equal(ceTaskUuid, analysis.ceTaskUuid) && + Objects.equal(analysisUuid, analysis.analysisUuid); } @Override public int hashCode() { - return Objects.hash(projectUuid, ceTaskUuid); + return Objects.hashCode(projectUuid, ceTaskUuid, analysisUuid); } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooksImpl.java b/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooksImpl.java index c2ac1e2e3aa..b839d8c43fe 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooksImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/webhook/WebHooksImpl.java @@ -81,7 +81,7 @@ public class WebHooksImpl implements WebHooks { @Override public void sendProjectAnalysisUpdate(Configuration config, Analysis analysis, Supplier payloadSupplier) { List webhooks = readWebHooksFrom(config) - .map(nameUrl -> new Webhook(analysis.getProjectUuid(), analysis.getCeTaskUuid(), nameUrl.getName(), nameUrl.getUrl())) + .map(nameUrl -> new Webhook(analysis.getProjectUuid(), analysis.getCeTaskUuid(), analysis.getAnalysisUuid(), nameUrl.getName(), nameUrl.getUrl())) .collect(MoreCollectors.toList()); if (webhooks.isEmpty()) { return; diff --git a/server/sonar-server/src/main/java/org/sonar/server/webhook/Webhook.java b/server/sonar-server/src/main/java/org/sonar/server/webhook/Webhook.java index 3a9f1a3b253..1e43b9f1de1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/webhook/Webhook.java +++ b/server/sonar-server/src/main/java/org/sonar/server/webhook/Webhook.java @@ -19,6 +19,7 @@ */ package org.sonar.server.webhook; +import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; import static java.util.Objects.requireNonNull; @@ -28,12 +29,14 @@ public class Webhook { private final String componentUuid; private final String ceTaskUuid; + private final String analysisUuid; private final String name; private final String url; - public Webhook(String componentUuid, String ceTaskUuid, String name, String url) { + public Webhook(String componentUuid, String ceTaskUuid, String analysisUuid, String name, String url) { this.componentUuid = requireNonNull(componentUuid); - this.ceTaskUuid = requireNonNull(ceTaskUuid); + this.ceTaskUuid = ceTaskUuid; + this.analysisUuid = requireNonNull(analysisUuid); this.name = requireNonNull(name); this.url = requireNonNull(url); } @@ -42,6 +45,7 @@ public class Webhook { return componentUuid; } + @CheckForNull public String getCeTaskUuid() { return ceTaskUuid; } @@ -53,4 +57,8 @@ public class Webhook { public String getUrl() { return url; } + + public String getAnalysisUuid() { + return analysisUuid; + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java b/server/sonar-server/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java index cd2dbe6e315..5824dc823df 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java +++ b/server/sonar-server/src/main/java/org/sonar/server/webhook/WebhookDeliveryStorage.java @@ -67,6 +67,7 @@ public class WebhookDeliveryStorage { dto.setUuid(uuidFactory.create()); dto.setComponentUuid(delivery.getWebhook().getComponentUuid()); dto.setCeTaskUuid(delivery.getWebhook().getCeTaskUuid()); + dto.setAnalysisUuid(delivery.getWebhook().getAnalysisUuid()); dto.setName(delivery.getWebhook().getName()); dto.setUrl(delivery.getWebhook().getUrl()); dto.setSuccess(delivery.isSuccess()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTaskTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTaskTest.java index d398ae6f561..3169b72eb90 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTaskTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPostTaskTest.java @@ -84,7 +84,13 @@ public class WebhookPostTaskTest { .execute(); ArgumentCaptor supplierCaptor = ArgumentCaptor.forClass(Supplier.class); - verify(webHooks).sendProjectAnalysisUpdate(same(configuration), eq(new WebHooks.Analysis(project.getUuid(), ceTask.getId())), supplierCaptor.capture()); + verify(webHooks) + .sendProjectAnalysisUpdate( + same(configuration), + eq(new WebHooks.Analysis(project.getUuid(), + "", + ceTask.getId())), + supplierCaptor.capture()); assertThat(supplierCaptor.getValue().get()).isSameAs(webhookPayload); diff --git a/server/sonar-server/src/test/java/org/sonar/server/webhook/WebHooksImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/webhook/WebHooksImplTest.java index 49f0fe5a34e..abc72c3a316 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/webhook/WebHooksImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/webhook/WebHooksImplTest.java @@ -106,7 +106,7 @@ public class WebHooksImplTest { @Test public void do_nothing_if_no_webhooks() { - underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "#1"), () -> mock); + underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "1", "#1"), () -> mock); assertThat(caller.countSent()).isEqualTo(0); assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty(); @@ -123,7 +123,7 @@ public class WebHooksImplTest { caller.enqueueSuccess(NOW, 200, 1_234); caller.enqueueFailure(NOW, new IOException("Fail to connect")); - underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "#1"), () -> mock); + underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "1", "#1"), () -> mock); assertThat(caller.countSent()).isEqualTo(2); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Sent webhook 'First' | url=http://url1 | time=1234ms | status=200"); @@ -139,7 +139,7 @@ public class WebHooksImplTest { settings.setProperty("sonar.webhooks.project.1.url", "http://url1"); caller.enqueueSuccess(NOW, 200, 1_234); - underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "#1"), () -> mock); + underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "1", "#1"), () -> mock); assertThat(caller.countSent()).isEqualTo(1); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Sent webhook 'First' | url=http://url1 | time=1234ms | status=200"); @@ -166,7 +166,7 @@ public class WebHooksImplTest { }); settings.setProperty(property, IntStream.range(1, 15).mapToObj(String::valueOf).collect(Collectors.joining(","))); - underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "#1"), () -> mock); + underTest.sendProjectAnalysisUpdate(settings.asConfig(), new WebHooks.Analysis(PROJECT_UUID, "1", "#1"), () -> mock); assertThat(caller.countSent()).isEqualTo(10); assertThat(logTester.logs(LoggerLevel.DEBUG).stream().filter(log -> log.contains("Sent"))).hasSize(10); diff --git a/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookCallerImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookCallerImplTest.java index 40712c0f9fc..1cd20273c3f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookCallerImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookCallerImplTest.java @@ -24,6 +24,7 @@ import okhttp3.HttpUrl; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.DisableOnDebug; @@ -58,7 +59,7 @@ public class WebhookCallerImplTest { @Test public void send_posts_payload_to_http_server() throws Exception { - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", server.url("/ping").toString()); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", server.url("/ping").toString()); server.enqueue(new MockResponse().setBody("pong").setResponseCode(201)); WebhookDelivery delivery = newSender().call(webhook, PAYLOAD); @@ -81,7 +82,7 @@ public class WebhookCallerImplTest { @Test public void silently_catch_error_when_external_server_does_not_answer() throws Exception { - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", server.url("/ping").toString()); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", server.url("/ping").toString()); server.shutdown(); WebhookDelivery delivery = newSender().call(webhook, PAYLOAD); @@ -97,7 +98,7 @@ public class WebhookCallerImplTest { @Test public void silently_catch_error_when_url_is_incorrect() throws Exception { - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", "this_is_not_an_url"); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", "this_is_not_an_url"); WebhookDelivery delivery = newSender().call(webhook, PAYLOAD); @@ -115,7 +116,7 @@ public class WebhookCallerImplTest { */ @Test public void redirects_should_be_followed_with_POST_method() throws Exception { - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", server.url("/redirect").toString()); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", server.url("/redirect").toString()); // /redirect redirects to /target server.enqueue(new MockResponse().setResponseCode(307).setHeader("Location", server.url("target"))); @@ -137,7 +138,7 @@ public class WebhookCallerImplTest { @Test public void credentials_are_propagated_to_POST_redirects() throws Exception { HttpUrl url = server.url("/redirect").newBuilder().username("theLogin").password("thePassword").build(); - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", url.toString()); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString()); // /redirect redirects to /target server.enqueue(new MockResponse().setResponseCode(307).setHeader("Location", server.url("target"))); @@ -157,7 +158,7 @@ public class WebhookCallerImplTest { @Test public void redirects_throws_ISE_if_header_Location_is_missing() throws Exception { HttpUrl url = server.url("/redirect"); - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", url.toString()); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString()); server.enqueue(new MockResponse().setResponseCode(307)); @@ -172,7 +173,7 @@ public class WebhookCallerImplTest { @Test public void redirects_throws_ISE_if_header_Location_does_not_relate_to_a_supported_protocol() throws Exception { HttpUrl url = server.url("/redirect"); - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", url.toString()); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString()); server.enqueue(new MockResponse().setResponseCode(307).setHeader("Location", "ftp://foo")); @@ -187,7 +188,7 @@ public class WebhookCallerImplTest { @Test public void send_basic_authentication_header_if_url_contains_credentials() throws Exception { HttpUrl url = server.url("/ping").newBuilder().username("theLogin").password("thePassword").build(); - Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", url.toString()); + Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString()); server.enqueue(new MockResponse().setBody("pong")); WebhookDelivery delivery = newSender().call(webhook, PAYLOAD); diff --git a/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookDeliveryStorageTest.java b/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookDeliveryStorageTest.java index d64a8f333c7..4477054190e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookDeliveryStorageTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/webhook/WebhookDeliveryStorageTest.java @@ -20,6 +20,7 @@ package org.sonar.server.webhook; import java.io.IOException; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; @@ -28,10 +29,6 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.webhook.WebhookDeliveryDto; -import org.sonar.server.webhook.Webhook; -import org.sonar.server.webhook.WebhookDelivery; -import org.sonar.server.webhook.WebhookDeliveryStorage; -import org.sonar.server.webhook.WebhookPayload; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -105,7 +102,7 @@ public class WebhookDeliveryStorageTest { private static WebhookDelivery.Builder newBuilderTemplate() { return new WebhookDelivery.Builder() - .setWebhook(new Webhook("COMPONENT1", "TASK1", "Jenkins", "http://jenkins")) + .setWebhook(new Webhook("COMPONENT1", "TASK1", RandomStringUtils.randomAlphanumeric(40),"Jenkins", "http://jenkins")) .setPayload(new WebhookPayload("my-project", "{json}")) .setAt(1_000_000L) .setHttpStatus(200) -- 2.39.5