From 2447af803bf2923a45b47c42524427ecf594a1e9 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 30 Jun 2014 11:13:13 +0200 Subject: [PATCH] Add ConvertProfileMeasuresMigrationTest --- .../db/migrations/DatabaseMigrations.java | 4 +- ...a => ConvertProfileMeasuresMigration.java} | 4 +- .../v44/FeedQProfileDatesMigration.java | 7 +- .../v44/IssueActionPlanKeyMigration.java | 7 +- .../migrate/550_convert_profile_measures.rb | 2 +- .../ConvertProfileMeasuresMigrationTest.java | 67 +++++++++++ .../before.xml | 56 +++++++++ .../schema.sql | 108 ++++++++++++++++++ 8 files changed, 238 insertions(+), 17 deletions(-) rename sonar-server/src/main/java/org/sonar/server/db/migrations/v44/{ConvertProfileMeasures.java => ConvertProfileMeasuresMigration.java} (95%) create mode 100644 sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/before.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/schema.sql diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java index bc1e543c2d1..fc1413fd316 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/DatabaseMigrations.java @@ -30,7 +30,7 @@ import org.sonar.server.db.migrations.v43.NotResolvedIssuesOnRemovedComponentsMi import org.sonar.server.db.migrations.v43.RequirementMeasuresMigration; import org.sonar.server.db.migrations.v43.TechnicalDebtMeasuresMigration; import org.sonar.server.db.migrations.v44.ChangeLogMigration; -import org.sonar.server.db.migrations.v44.ConvertProfileMeasures; +import org.sonar.server.db.migrations.v44.ConvertProfileMeasuresMigration; import org.sonar.server.db.migrations.v44.FeedQProfileDatesMigration; import org.sonar.server.db.migrations.v44.FeedQProfileKeysMigration; import org.sonar.server.db.migrations.v44.IssueActionPlanKeyMigration; @@ -61,7 +61,7 @@ public interface DatabaseMigrations { FeedQProfileKeysMigration.class, FeedQProfileDatesMigration.class, ChangeLogMigration.class, - ConvertProfileMeasures.class + ConvertProfileMeasuresMigration.class ); } diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasures.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigration.java similarity index 95% rename from sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasures.java rename to sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigration.java index 8b2f6c36a75..79d821297c5 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasures.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigration.java @@ -38,11 +38,11 @@ import java.util.Date; * * @since 4.4 */ -public class ConvertProfileMeasures implements DatabaseMigration { +public class ConvertProfileMeasuresMigration implements DatabaseMigration { private final DbClient db; - public ConvertProfileMeasures(DbClient db) { + public ConvertProfileMeasuresMigration(DbClient db) { this.db = db; } diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/FeedQProfileDatesMigration.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/FeedQProfileDatesMigration.java index 640fbbb7ca9..8194061fee4 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/FeedQProfileDatesMigration.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/FeedQProfileDatesMigration.java @@ -43,16 +43,11 @@ public class FeedQProfileDatesMigration implements DatabaseMigration { private final DbClient db; private final System2 system; - @VisibleForTesting - FeedQProfileDatesMigration(DbClient db, System2 system) { + public FeedQProfileDatesMigration(DbClient db, System2 system) { this.db = db; this.system = system; } - public FeedQProfileDatesMigration(DbClient db) { - this(db, System2.INSTANCE); - } - @Override public void execute() { DbSession session = db.openSession(false); diff --git a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java index b085c9ee119..fa015613f03 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java +++ b/sonar-server/src/main/java/org/sonar/server/db/migrations/v44/IssueActionPlanKeyMigration.java @@ -44,12 +44,7 @@ public class IssueActionPlanKeyMigration implements DatabaseMigration { private final System2 system2; private final Database db; - public IssueActionPlanKeyMigration(Database database) { - this(database, System2.INSTANCE); - } - - @VisibleForTesting - IssueActionPlanKeyMigration(Database database, System2 system2) { + public IssueActionPlanKeyMigration(Database database, System2 system2) { this.db = database; this.system2 = system2; } diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/550_convert_profile_measures.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/550_convert_profile_measures.rb index 427a7703a4a..a6f357c405b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/550_convert_profile_measures.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/550_convert_profile_measures.rb @@ -28,7 +28,7 @@ class ConvertProfileMeasures < ActiveRecord::Migration end def self.up - execute_java_migration('org.sonar.server.db.migrations.v44.ConvertProfileMeasures') + execute_java_migration('org.sonar.server.db.migrations.v44.ConvertProfileMeasuresMigration') Metric.reset_column_information metric = Metric.find_by_name('profile') diff --git a/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java b/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java new file mode 100644 index 00000000000..0d783725707 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest.java @@ -0,0 +1,67 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.db.migrations.v44; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.sonar.core.persistence.TestDatabase; +import org.sonar.server.db.DbClient; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +import static org.fest.assertions.Assertions.assertThat; + +public class ConvertProfileMeasuresMigrationTest { + + @ClassRule + public static TestDatabase db = new TestDatabase().schema(ConvertProfileMeasuresMigrationTest.class, "schema.sql"); + + ConvertProfileMeasuresMigration migration; + + @Before + public void setUp() throws Exception { + DbClient dbClient = new DbClient(db.database(), db.myBatis()); + migration = new ConvertProfileMeasuresMigration(dbClient); + } + + @Test + public void generate_profiles_measure_as_json() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + + migration.execute(); + + Connection connection = db.openConnection(); + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("select text_value from project_measures where id=2"); + try { + rs.next(); + // pb of comparison of timezones..., so using startsWith instead of equals + assertThat(rs.getString(1)).startsWith("[{\"key\":\"java-sonar-way\",\"language\":\"java\",\"name\":\"Sonar way\",\"rulesUpdatedAt\":\"2014-01-04T"); + } finally { + rs.close(); + stmt.close(); + connection.close(); + } + } +} diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/before.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/before.xml new file mode 100644 index 00000000000..92cf512ca3c --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/before.xml @@ -0,0 +1,56 @@ + + diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/schema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/schema.sql new file mode 100644 index 00000000000..4736d0cdb2c --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/db/migrations/v44/ConvertProfileMeasuresMigrationTest/schema.sql @@ -0,0 +1,108 @@ +CREATE TABLE "RULES_PROFILES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(100) NOT NULL, + "LANGUAGE" VARCHAR(20), + "KEE" VARCHAR(1000) NOT NULL, + "PARENT_KEE" VARCHAR(1000), + "RULES_UPDATED_AT" VARCHAR(100), + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP +); + +CREATE TABLE "ACTIVE_RULE_CHANGES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "USERNAME" VARCHAR(255), + "PROFILE_ID" INTEGER NOT NULL, + "PROFILE_VERSION" INTEGER NOT NULL, + "RULE_ID" INTEGER NOT NULL, + "CHANGE_DATE" TIMESTAMP NOT NULL, + "ENABLED" BOOLEAN, + "OLD_SEVERITY" INTEGER, + "NEW_SEVERITY" INTEGER +); + +CREATE TABLE "ACTIVE_RULE_PARAM_CHANGES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "ACTIVE_RULE_CHANGE_ID" INTEGER NOT NULL, + "RULES_PARAMETER_ID" INTEGER NOT NULL, + "OLD_VALUE" VARCHAR(4000), + "NEW_VALUE" VARCHAR(4000) + ); + +CREATE TABLE "PROJECT_MEASURES" ( + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "VALUE" DOUBLE, + "METRIC_ID" INTEGER NOT NULL, + "SNAPSHOT_ID" INTEGER, + "RULE_ID" INTEGER, + "RULES_CATEGORY_ID" INTEGER, + "TEXT_VALUE" VARCHAR(4000), + "TENDENCY" INTEGER, + "MEASURE_DATE" TIMESTAMP, + "PROJECT_ID" INTEGER, + "ALERT_STATUS" VARCHAR(5), + "ALERT_TEXT" VARCHAR(4000), + "URL" VARCHAR(2000), + "DESCRIPTION" VARCHAR(4000), + "RULE_PRIORITY" INTEGER, + "CHARACTERISTIC_ID" INTEGER, + "PERSON_ID" INTEGER, + "VARIATION_VALUE_1" DOUBLE, + "VARIATION_VALUE_2" DOUBLE, + "VARIATION_VALUE_3" DOUBLE, + "VARIATION_VALUE_4" DOUBLE, + "VARIATION_VALUE_5" DOUBLE, + "MEASURE_DATA" BINARY(167772150) +); + +CREATE TABLE "METRICS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(64) NOT NULL, + "DESCRIPTION" VARCHAR(255), + "DIRECTION" INTEGER NOT NULL DEFAULT 0, + "DOMAIN" VARCHAR(64), + "SHORT_NAME" VARCHAR(64), + "QUALITATIVE" BOOLEAN NOT NULL DEFAULT FALSE, + "VAL_TYPE" VARCHAR(8), + "USER_MANAGED" BOOLEAN DEFAULT FALSE, + "ENABLED" BOOLEAN DEFAULT TRUE, + "ORIGIN" VARCHAR(3), + "WORST_VALUE" DOUBLE, + "BEST_VALUE" DOUBLE, + "OPTIMIZED_BEST_VALUE" BOOLEAN, + "HIDDEN" BOOLEAN, + "DELETE_HISTORICAL_DATA" BOOLEAN +); + +CREATE TABLE "SNAPSHOTS" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "CREATED_AT" TIMESTAMP, + "BUILD_DATE" TIMESTAMP, + "PROJECT_ID" INTEGER NOT NULL, + "PARENT_SNAPSHOT_ID" INTEGER, + "STATUS" VARCHAR(4) NOT NULL DEFAULT 'U', + "PURGE_STATUS" INTEGER, + "ISLAST" BOOLEAN NOT NULL DEFAULT FALSE, + "SCOPE" VARCHAR(3), + "QUALIFIER" VARCHAR(10), + "ROOT_SNAPSHOT_ID" INTEGER, + "VERSION" VARCHAR(500), + "PATH" VARCHAR(500), + "DEPTH" INTEGER, + "ROOT_PROJECT_ID" INTEGER, + "PERIOD1_MODE" VARCHAR(100), + "PERIOD1_PARAM" VARCHAR(100), + "PERIOD1_DATE" TIMESTAMP, + "PERIOD2_MODE" VARCHAR(100), + "PERIOD2_PARAM" VARCHAR(100), + "PERIOD2_DATE" TIMESTAMP, + "PERIOD3_MODE" VARCHAR(100), + "PERIOD3_PARAM" VARCHAR(100), + "PERIOD3_DATE" TIMESTAMP, + "PERIOD4_MODE" VARCHAR(100), + "PERIOD4_PARAM" VARCHAR(100), + "PERIOD4_DATE" TIMESTAMP, + "PERIOD5_MODE" VARCHAR(100), + "PERIOD5_PARAM" VARCHAR(100), + "PERIOD5_DATE" TIMESTAMP +); -- 2.39.5