From a2cf8a110df9213200ab495f75003ce7833a0033 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 26 Jan 2016 18:39:28 +0100 Subject: SONAR-7070 Enforced leak period in quality gates - migration script --- .../sonar/db/version/MigrationStepModuleTest.java | 2 +- .../v54/MigrateQualityGatesConditionsTest.java | 80 ++++++++++++++++++++++ .../migrate-result.xml | 11 +++ .../MigrateQualityGatesConditionsTest/migrate.xml | 11 +++ .../MigrateQualityGatesConditionsTest/schema.sql | 11 +++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql (limited to 'sonar-db/src/test') diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index 4fbf2496389..a374e923cf7 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -29,6 +29,6 @@ public class MigrationStepModuleTest { public void verify_count_of_added_MigrationStep_types() { ComponentContainer container = new ComponentContainer(); new MigrationStepModule().configure(container); - assertThat(container.size()).isEqualTo(47); + assertThat(container.size()).isEqualTo(48); } } diff --git a/sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java new file mode 100644 index 00000000000..a0b2e231152 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest.java @@ -0,0 +1,80 @@ +/* + * 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.version.v54; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.db.DbTester; +import org.sonar.db.version.MigrationStep; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class MigrateQualityGatesConditionsTest { + static final String NOW = "1919-12-24"; + private static final String MSG_WARNING_QG_CONDITIONS_UPDATED = "The following Quality Gates have been updated to compare with the leak period: qg-1, qg-2."; + final System2 system2 = mock(System2.class); + + @Rule + public DbTester db = DbTester.createForSchema(System2.INSTANCE, MigrateQualityGatesConditionsTest.class, "schema.sql"); + @Rule + public LogTester log = new LogTester(); + + MigrationStep migration; + + @Before + public void setUp() { + db.truncateTables(); + when(system2.now()).thenReturn(DateUtils.parseDate(NOW).getTime()); + migration = new MigrateQualityGatesConditions(db.database(), system2); + } + + @Test + public void migrate_empty_db() throws Exception { + migration.execute(); + } + + @Test + public void migrate() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "migrate-result.xml", "quality_gates", "quality_gate_conditions"); + assertThat(log.logs(LoggerLevel.WARN)).contains(MSG_WARNING_QG_CONDITIONS_UPDATED); + } + + @Test + public void nothing_to_do_on_already_migrated_data() throws Exception { + db.prepareDbUnit(this.getClass(), "migrate-result.xml"); + + migration.execute(); + + db.assertDbUnit(getClass(), "migrate-result.xml", "quality_gates", "quality_gate_conditions"); + assertThat(log.logs(LoggerLevel.WARN)).doesNotContain(MSG_WARNING_QG_CONDITIONS_UPDATED); + } +} diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml new file mode 100644 index 00000000000..0792ddd3e20 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate-result.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml new file mode 100644 index 00000000000..f2397ae9f7e --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/migrate.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql new file mode 100644 index 00000000000..4aac9f1fe01 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateQualityGatesConditionsTest/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE "QUALITY_GATES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(100) NOT NULL +); + +CREATE TABLE "QUALITY_GATE_CONDITIONS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "QGATE_ID" INTEGER, + "PERIOD" INTEGER, + "UPDATED_AT" TIMESTAMP +); -- cgit v1.2.3