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.v70;
22 import java.sql.SQLException;
23 import java.util.concurrent.atomic.AtomicInteger;
24 import org.apache.commons.lang.math.RandomUtils;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.config.internal.MapSettings;
28 import org.sonar.db.CoreDbTester;
29 import org.sonar.server.platform.db.migration.step.DataChange;
31 import static org.assertj.core.api.Assertions.assertThat;
33 public class DeletePersonAndFileMeasuresTest {
34 private static final AtomicInteger GENERATOR = new AtomicInteger();
36 public CoreDbTester db = CoreDbTester.createForSchema(DeletePersonAndFileMeasuresTest.class, "initial.sql");
39 public void delete_file_and_person_measures() throws SQLException {
40 insertComponent("P1", "PRJ", "TRK");
41 insertComponent("D1", "DIR", "DIR");
42 insertComponent("F1", "FIL", "FIL");
43 insertComponent("F2", "FIL", "UTS");
44 insertSnapshot("S1", "P1", false);
45 insertSnapshot("S2", "P1", true);
47 long m1 = insertMeasure("P1", "S1");
48 long m2 = insertMeasure("D1", "S1");
49 long m3 = insertMeasure("F1", "S1");
50 long m4 = insertMeasure("F2", "S1");
51 long m5 = insertPersonMeasure("P1", "S1");
52 long m6 = insertPersonMeasure("F1", "S1");
54 long m7 = insertMeasure("P1", "S2");
55 long m8 = insertMeasure("D1", "S2");
56 long m9 = insertMeasure("F1", "S2");
57 long m10 = insertMeasure("F2", "S2");
58 long m11 = insertPersonMeasure("P1", "S2");
59 long m12 = insertPersonMeasure("F1", "S2");
63 assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
64 assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
65 assertThatMeasuresAreExactly(m1, m2, m7, m8);
67 // migration is re-entrant
69 assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
70 assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
71 assertThatMeasuresAreExactly(m1, m2, m7, m8);
75 public void migration_is_disabled_on_sonarcloud() throws SQLException {
76 insertComponent("F1", "FIL", "FIL");
77 insertSnapshot("S1", "P1", false);
78 insertMeasure("F1", "S1");
79 insertPersonMeasure("F1", "S1");
83 assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(1);
84 assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(1);
85 assertThat(db.countRowsOfTable("PROJECT_MEASURES")).isEqualTo(2);
88 private void run(boolean sonarCloud) throws SQLException {
89 MapSettings settings = new MapSettings();
91 settings.setProperty("sonar.sonarcloud.enabled", true);
93 DataChange underTest = new DeletePersonAndFileMeasures(db.database(), settings.asConfig());
97 private void assertThatMeasuresAreExactly(long... expectedMeasureIds) {
98 long[] ids = db.select("select id as \"id\" from project_measures")
100 .mapToLong(m -> (Long) m.get("id"))
102 assertThat(ids).containsOnly(expectedMeasureIds);
105 private void insertComponent(String uuid, String scope, String qualifier) {
106 db.executeInsert("PROJECTS",
107 "ORGANIZATION_UUID", "O1",
108 "KEE", "" + GENERATOR.incrementAndGet(),
110 "PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
111 "MAIN_BRANCH_PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
113 "ROOT_UUID", "" + GENERATOR.incrementAndGet(),
115 "QUALIFIER", qualifier,
119 private void insertSnapshot(String uuid, String projectUuid, boolean last) {
120 db.executeInsert("SNAPSHOTS",
122 "COMPONENT_UUID", projectUuid,
127 private long insertMeasure(String componentUuid, String analysisUuid) {
128 long id = GENERATOR.incrementAndGet();
129 db.executeInsert("PROJECT_MEASURES",
132 "COMPONENT_UUID", componentUuid,
133 "ANALYSIS_UUID", analysisUuid);
137 private long insertPersonMeasure(String componentUuid, String analysisUuid) {
138 long id = GENERATOR.incrementAndGet();
139 db.executeInsert("PROJECT_MEASURES",
142 "COMPONENT_UUID", componentUuid,
143 "ANALYSIS_UUID", analysisUuid,
144 "PERSON_ID", RandomUtils.nextInt(100));