aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/test
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-12-02 18:12:56 +0100
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-12-05 16:52:54 +0100
commit37494f41d3d07042fedb9bab93fb44587866db05 (patch)
tree88a381ec0813aa9fd8343fc4bc21b7d20d11aeb2 /sonar-db/src/test
parent19882feb2c947ed14cc90190a000d6fa0d3b39d0 (diff)
downloadsonarqube-37494f41d3d07042fedb9bab93fb44587866db05.tar.gz
sonarqube-37494f41d3d07042fedb9bab93fb44587866db05.zip
SONAR-8464 Populate UUID column of table EVENTS
Diffstat (limited to 'sonar-db/src/test')
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java2
-rw-r--r--sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java95
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql12
3 files changed, 108 insertions, 1 deletions
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 b292574a3d2..2812e2550f7 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(165);
+ assertThat(container.size()).isEqualTo(166);
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java
new file mode 100644
index 00000000000..1c67cc9fd20
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.v63;
+
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang.StringUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactoryImpl;
+import org.sonar.db.DbTester;
+
+import static java.lang.String.valueOf;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PopulateUuidColumnOfEventsTest {
+
+ private static final String TABLE_EVENTS = "events";
+
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, PopulateUuidColumnOfEventsTest.class, "in_progress_events.sql");
+
+ private PopulateUuidColumnOfEvents underTest = new PopulateUuidColumnOfEvents(db.database(), UuidFactoryImpl.INSTANCE);
+
+ @Test
+ public void migration_has_no_effect_on_empty_tables() throws SQLException {
+ underTest.execute();
+
+ assertThat(db.countRowsOfTable(TABLE_EVENTS)).isEqualTo(0);
+ }
+
+ @Test
+ public void migration_generates_uuids() throws SQLException {
+ insertEvents(1);
+ insertEvents(2);
+ insertEvents(3);
+ db.commit();
+
+ underTest.execute();
+
+ verifyUuids(3);
+ }
+
+ @Test
+ public void migration_is_reentrant() throws SQLException {
+ insertEvents(1);
+
+ underTest.execute();
+ verifyUuids(1);
+
+ underTest.execute();
+ verifyUuids(1);
+ }
+
+ private void verifyUuids(int expectedCount) {
+ List<Map<String, Object>> rows = db.select("select uuid from events where uuid is not null");
+ Set<Object> uuids = rows.stream().map(cols -> cols.get("UUID")).filter(uuid -> StringUtils.isNotBlank((String) uuid)).collect(Collectors.toSet());
+ assertThat(uuids).hasSize(expectedCount);
+ }
+
+ private String insertEvents(long id) {
+ String uuid = "uuid_" + id;
+ db.executeInsert(
+ TABLE_EVENTS,
+ "ID", valueOf(id),
+ "COMPONENT_UUID", valueOf(id + 10),
+ "ANALYSIS_UUID", valueOf(id + 100),
+ "EVENT_DATE", 123456,
+ "CREATED_AT", 456789);
+ return uuid;
+ }
+
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql b/sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql
new file mode 100644
index 00000000000..d83b16dd817
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v63/PopulateUuidColumnOfEventsTest/in_progress_events.sql
@@ -0,0 +1,12 @@
+CREATE TABLE "EVENTS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "UUID" VARCHAR(40),
+ "NAME" VARCHAR(400),
+ "ANALYSIS_UUID" VARCHAR(50) NOT NULL,
+ "COMPONENT_UUID" VARCHAR(50),
+ "CATEGORY" VARCHAR(50),
+ "EVENT_DATE" BIGINT NOT NULL,
+ "CREATED_AT" BIGINT NOT NULL,
+ "DESCRIPTION" VARCHAR(4000),
+ "EVENT_DATA" VARCHAR(4000)
+);