3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v76;
22 import java.sql.SQLException;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.impl.config.MapSettings;
26 import org.sonar.db.CoreDbTester;
28 import static java.lang.String.valueOf;
29 import static org.assertj.core.api.Assertions.assertThat;
31 public class DeleteModuleAndFolderMeasuresTest {
33 private static final String TABLE_MEASURES = "project_measures";
34 private static final int COMPONENT_ID_1 = 125;
35 private static final int COMPONENT_ID_2 = 604;
38 public CoreDbTester db = CoreDbTester.createForSchema(DeleteModuleAndFolderMeasuresTest.class, "project_measures.sql");
40 private MapSettings settings = new MapSettings();
41 private DeleteModuleAndFolderMeasures underTest = new DeleteModuleAndFolderMeasures(db.database(), settings.asConfig());
44 public void migration_has_no_effect_on_empty_tables() throws SQLException {
47 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isZero();
51 public void execute_has_no_effect_on_SonarCloud() throws SQLException {
52 String moduleUuid = insertComponent(1, "BRC");
53 insertMeasure(1, moduleUuid);
55 settings.setProperty("sonar.sonarcloud.enabled", true);
59 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(1);
63 public void migration_removes_module_level_measures() throws SQLException {
64 String moduleUuid = insertComponent(1, "BRC");
65 insertMeasure(1, moduleUuid);
69 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isZero();
73 public void migration_removes_folder_level_measures() throws SQLException {
74 String dirUuid = insertComponent(1, "DIR");
75 insertMeasure(1, dirUuid);
79 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isZero();
83 public void migration_ignores_not_relevant_measures() throws SQLException {
84 String projectUuid = insertComponent(1, "TRK");
85 insertMeasure(1, projectUuid);
86 String moduleUuid = insertComponent(2, "BRC");
87 insertMeasure(2, moduleUuid);
88 insertMeasure(3, moduleUuid);
89 String dirUuid = insertComponent(3, "DIR");
90 insertMeasure(4, dirUuid);
91 insertMeasure(5, dirUuid);
92 String fileUuid = insertComponent(4, "FIL");
93 insertMeasure(6, fileUuid);
97 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(2);
101 public void migration_is_reentrant() throws SQLException {
102 String dirUuid = insertComponent(3, "DIR");
103 insertMeasure(1, dirUuid);
104 String fileUuid = insertComponent(4, "FIL");
105 insertMeasure(2, fileUuid);
108 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(1);
111 assertThat(db.countRowsOfTable(TABLE_MEASURES)).isEqualTo(1);
114 private String insertComponent(long id, String qualifier) {
115 String uuid = "uuid_" + id;
119 "QUALIFIER", qualifier,
120 "ORGANIZATION_UUID", "org_" + id,
121 "UUID_PATH", "path_" + id,
122 "ROOT_UUID", "root_" + id,
123 "PROJECT_UUID", "project_" + id,
129 private void insertMeasure(long id, String componentUuid) {
133 "COMPONENT_UUID", componentUuid,
134 "METRIC_ID", valueOf(id + 10),
135 "ANALYSIS_UUID", valueOf(id + 100),
136 "VALUE", valueOf(id + 1000));