aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/test
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-09-09 11:16:37 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-09-12 14:11:58 +0200
commitb5851d0ff8318549c2356b20df36401b879fc5a2 (patch)
tree49c1f9a615715f10d9d3852a7ad02d9ead2f178a /sonar-db/src/test
parent0fc1b325a335c9ecad906908d2c8d87c12f0f976 (diff)
downloadsonarqube-b5851d0ff8318549c2356b20df36401b879fc5a2.tar.gz
sonarqube-b5851d0ff8318549c2356b20df36401b879fc5a2.zip
SONAR-7851 copy activities to qprofile_changes
Diffstat (limited to 'sonar-db/src/test')
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java133
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql22
2 files changed, 155 insertions, 0 deletions
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java
new file mode 100644
index 00000000000..d4eacf9aacb
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.v61;
+
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class CopyActivitiesToQprofileChangesTest {
+
+ private static final long A_DATE = 1_500_000_000_000L;
+ private static final String TABLE_ACTIVITIES = "activities";
+ private static final String TABLE_QPROFILE_CHANGES = "qprofile_changes";
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, CopyActivitiesToQprofileChangesTest.class, "schema.sql");
+
+ private CopyActivitiesToQprofileChanges underTest = new CopyActivitiesToQprofileChanges(db.database());
+
+ @Test
+ public void migration_has_no_effect_on_empty_table() throws SQLException {
+ underTest.execute();
+
+ assertThat(db.countRowsOfTable(TABLE_QPROFILE_CHANGES)).isEqualTo(0);
+ }
+
+ @Test
+ public void copy_qprofile_changes() throws SQLException {
+ String key = "U1";
+ String profileKey = "P1";
+ String login = "marcel";
+ String type = "ACTIVATED";
+ String data = "D1";
+ insertActivity(key, profileKey, login, "QPROFILE", type, data, A_DATE);
+
+ underTest.execute();
+
+ assertThat(db.countRowsOfTable(TABLE_QPROFILE_CHANGES)).isEqualTo(1);
+ Map<String, Object> change = selectChangeByKey(key);
+ assertThat(change.get("qprofileKey")).isEqualTo(profileKey);
+ assertThat(change.get("createdAt")).isEqualTo(A_DATE);
+ assertThat(change.get("login")).isEqualTo(login);
+ assertThat(change.get("changeType")).isEqualTo(type);
+ assertThat(change.get("data")).isEqualTo(data);
+ }
+
+ /**
+ * Do not copy twice the same row
+ */
+ @Test
+ public void copy_is_reentrant() throws SQLException {
+ insertActivity("U1", "P1", "marcel", "QPROFILE", "ACTIVATED", "D1", A_DATE);
+
+ // first run
+ underTest.execute();
+ assertThat(db.countRowsOfTable(TABLE_QPROFILE_CHANGES)).isEqualTo(1);
+
+ // second run
+ underTest.execute();
+ assertThat(db.countRowsOfTable(TABLE_QPROFILE_CHANGES)).isEqualTo(1);
+ }
+
+ @Test
+ public void copy_nullable_fields() throws SQLException {
+ String key = "U1";
+ String type = "ACTIVATED";
+ // no login nor data
+ insertActivity(key, "P1", null, "QPROFILE", type, null, A_DATE);
+
+ underTest.execute();
+
+ Map<String, Object> change = selectChangeByKey(key);
+ assertThat(change.get("qprofileKey")).isEqualTo("P1");
+ assertThat(change.get("createdAt")).isEqualTo(A_DATE);
+ assertThat(change.get("changeType")).isEqualTo(type);
+ assertThat(change.get("login")).isNull();
+ assertThat(change.get("data")).isNull();
+ }
+
+ @Test
+ public void ignore_activities_that_do_not_relate_to_qprofiles() throws SQLException {
+ insertActivity("U1", "P1", "marcel", "OTHER_ACTIVITY_TYPE", "T1", "D1", A_DATE);
+
+ underTest.execute();
+
+ assertThat(db.countRowsOfTable(TABLE_QPROFILE_CHANGES)).isEqualTo(0);
+ }
+
+ @Test
+ public void ignore_invalid_activities() throws SQLException {
+ // no change type
+ insertActivity("U1", "P1", "marcel", "QPROFILE", null, "D1", A_DATE);
+ // no date
+ insertActivity("U2", "P1", "marcel", "QPROFILE", "ACTIVATED", "D1", null);
+
+ underTest.execute();
+
+ assertThat(db.countRowsOfTable(TABLE_QPROFILE_CHANGES)).isEqualTo(0);
+ }
+
+ private void insertActivity(String key, @Nullable String profileKey, @Nullable String login, @Nullable String activityType, @Nullable String type, @Nullable String data, @Nullable Long createdAt) {
+ db.executeInsert(TABLE_ACTIVITIES, "log_key", key, "profile_key", profileKey, "user_login", login, "log_type", activityType, "log_action", type, "data_field", data, "created_at", createdAt != null ? new Date(createdAt) : null);
+ }
+
+ private Map<String, Object> selectChangeByKey(String key) {
+ return db.selectFirst("select qprofile_key as \"qprofileKey\", created_at as \"createdAt\", user_login as \"login\", change_type as \"changeType\", data as \"data\" from qprofile_changes where kee='" + key + "'");
+ }
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql
new file mode 100644
index 00000000000..1ad0dd1d259
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v61/CopyActivitiesToQprofileChangesTest/schema.sql
@@ -0,0 +1,22 @@
+CREATE TABLE "ACTIVITIES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "LOG_KEY" VARCHAR(250),
+ "PROFILE_KEY" VARCHAR(255) NOT NULL,
+ "CREATED_AT" TIMESTAMP,
+ "USER_LOGIN" VARCHAR(255),
+ "LOG_TYPE" VARCHAR(250),
+ "LOG_ACTION" VARCHAR(250),
+ "LOG_MESSAGE" VARCHAR(250),
+ "DATA_FIELD" CLOB(2147483647)
+);
+CREATE UNIQUE INDEX "ACTIVITIES_LOG_KEY" ON "ACTIVITIES" ("LOG_KEY");
+
+CREATE TABLE "QPROFILE_CHANGES" (
+ "KEE" VARCHAR(40) NOT NULL PRIMARY KEY,
+ "QPROFILE_KEY" VARCHAR(255) NOT NULL,
+ "CHANGE_TYPE" VARCHAR(20) NOT NULL,
+ "CREATED_AT" BIGINT NOT NULL,
+ "USER_LOGIN" VARCHAR(255),
+ "DATA" CLOB
+);
+CREATE INDEX "QPROFILE_CHANGES_QPROFILE_KEY" ON "QPROFILE_CHANGES" ("QPROFILE_KEY");