aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/test
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-07-27 11:05:57 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-07-28 09:14:54 +0200
commit9e90ec77057147e8ee53837b6b282a66795cce79 (patch)
tree08f82b41225b8741366488798b531c2bf2e8eab7 /sonar-db/src/test
parentf4a5427ca6a024184e7336a45d4806525aafe687 (diff)
downloadsonarqube-9e90ec77057147e8ee53837b6b282a66795cce79.tar.gz
sonarqube-9e90ec77057147e8ee53837b6b282a66795cce79.zip
SONAR-7917 Delete project dashboard data in DB
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/v61/DeleteProjectDashboardsTest.java141
-rw-r--r--sonar-db/src/test/resources/org/sonar/db/version/v61/DeleteProjectDashboardsTest/schema.sql39
3 files changed, 181 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 579506d38b1..c5fbc32539b 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(126);
+ assertThat(container.size()).isEqualTo(127);
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/version/v61/DeleteProjectDashboardsTest.java b/sonar-db/src/test/java/org/sonar/db/version/v61/DeleteProjectDashboardsTest.java
new file mode 100644
index 00000000000..705fd7f2774
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/version/v61/DeleteProjectDashboardsTest.java
@@ -0,0 +1,141 @@
+/*
+ * 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.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static java.lang.String.valueOf;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DeleteProjectDashboardsTest {
+ @Rule
+ public DbTester db = DbTester.createForSchema(System2.INSTANCE, DeleteProjectDashboardsTest.class, "schema.sql");
+
+ private DeleteProjectDashboards underTest = new DeleteProjectDashboards(db.database());
+
+ @Test
+ public void no_effect_on_empty_tables() throws SQLException {
+ underTest.execute();
+
+ assertThat(db.countRowsOfTable("dashboards")).isEqualTo(0);
+ }
+
+ @Test
+ public void delete_project_dashboard_data() throws SQLException {
+ insertGlobalDashboards(1L, 10L, 11L);
+ insertProjectDashboards(2L, 20L, 21L);
+ insertActiveDashboards(1L, 10L, 11L, 12L);
+ insertActiveDashboards(2L, 20L, 21L, 22L);
+ insertWidgets(1L, 100L, 101L, 102L);
+ insertWidgets(2L, 200L, 201L, 202L);
+ insertWidgetProperties(100L, 1001L, 1002L, 1003L);
+ insertWidgetProperties(202L, 2021L, 2022L, 2023L);
+
+ underTest.execute();
+
+ assertIdsOfDashboardsAre(1L, 10L, 11L);
+ assertIdsOfActiveDashboardsAre(10L, 11L, 12L);
+ assertIdsOfWidgetsAre(100L, 101L, 102L);
+ assertIdsOfWidgetPropertiesAre(1001L, 1002L, 1003L);
+ }
+
+ @Test
+ public void is_reentrant() throws SQLException {
+ insertGlobalDashboards(10L, 11L, 12L);
+ insertProjectDashboards(20L, 21L, 22L);
+ underTest.execute();
+ assertIdsOfDashboardsAre(10L, 11L, 12L);
+
+ underTest.execute();
+ assertIdsOfDashboardsAre(10L, 11L, 12L);
+ }
+
+ private void insertProjectDashboards(long... ids) {
+ Arrays.stream(ids).forEach(id -> insertDashboard(id, false));
+ }
+
+ private void insertGlobalDashboards(long... ids) {
+ Arrays.stream(ids).forEach(id -> insertDashboard(id, true));
+ }
+
+ private void insertDashboard(long id, boolean isGlobal) {
+ db.executeInsert(
+ "dashboards",
+ "ID", valueOf(id),
+ "IS_GLOBAL", valueOf(isGlobal));
+ }
+
+ private void insertActiveDashboards(long dashboardId, long... ids) {
+ Arrays.stream(ids).forEach(
+ id -> db.executeInsert(
+ "active_dashboards",
+ "ID", valueOf(id),
+ "DASHBOARD_ID", valueOf(dashboardId)));
+ }
+
+ private void insertWidgets(long dashboardId, long... ids) {
+ Arrays.stream(ids).forEach(
+ id -> db.executeInsert(
+ "widgets",
+ "ID", valueOf(id),
+ "WIDGET_KEY", valueOf(id),
+ "DASHBOARD_ID", valueOf(dashboardId)));
+ }
+
+ private void insertWidgetProperties(long widgetId, long... ids) {
+ Arrays.stream(ids).forEach(
+ id -> db.executeInsert(
+ "widget_properties",
+ "ID", valueOf(id),
+ "WIDGET_ID", valueOf(widgetId)));
+ }
+
+ private void assertIdsOfDashboardsAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from dashboards").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+
+ private void assertIdsOfActiveDashboardsAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from active_dashboards").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+
+ private void assertIdsOfWidgetsAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from widgets").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+
+ private void assertIdsOfWidgetPropertiesAre(Long... ids) {
+ List<Long> idsInDb = db.select("select ID from widget_properties").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList());
+
+ assertThat(idsInDb).containsOnly(ids);
+ }
+}
diff --git a/sonar-db/src/test/resources/org/sonar/db/version/v61/DeleteProjectDashboardsTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/version/v61/DeleteProjectDashboardsTest/schema.sql
new file mode 100644
index 00000000000..1030e93bff0
--- /dev/null
+++ b/sonar-db/src/test/resources/org/sonar/db/version/v61/DeleteProjectDashboardsTest/schema.sql
@@ -0,0 +1,39 @@
+CREATE TABLE "DASHBOARDS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "USER_ID" INTEGER,
+ "NAME" VARCHAR(256),
+ "DESCRIPTION" VARCHAR(1000),
+ "COLUMN_LAYOUT" VARCHAR(20),
+ "SHARED" BOOLEAN,
+ "IS_GLOBAL" BOOLEAN,
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP
+);
+
+CREATE TABLE "ACTIVE_DASHBOARDS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "DASHBOARD_ID" INTEGER NOT NULL,
+ "USER_ID" INTEGER,
+ "ORDER_INDEX" INTEGER
+);
+
+CREATE TABLE "WIDGETS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "DASHBOARD_ID" INTEGER NOT NULL,
+ "WIDGET_KEY" VARCHAR(256) NOT NULL,
+ "NAME" VARCHAR(256),
+ "DESCRIPTION" VARCHAR(1000),
+ "COLUMN_INDEX" INTEGER,
+ "ROW_INDEX" INTEGER,
+ "CONFIGURED" BOOLEAN,
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP,
+ "RESOURCE_ID" INTEGER
+);
+
+CREATE TABLE "WIDGET_PROPERTIES" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "WIDGET_ID" INTEGER NOT NULL,
+ "KEE" VARCHAR(100),
+ "TEXT_VALUE" VARCHAR(4000)
+);