]> source.dussan.org Git - sonarqube.git/blob
a6188242b4cc600ff8d813e62b48bb493b079eba
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.db.migration.version.v70;
21
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;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32
33 public class DeletePersonAndFileMeasuresTest {
34   private static final AtomicInteger GENERATOR = new AtomicInteger();
35   @Rule
36   public CoreDbTester db = CoreDbTester.createForSchema(DeletePersonAndFileMeasuresTest.class, "initial.sql");
37
38   @Test
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);
46     // past measures
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");
53     // last measures
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");
60
61     run(false);
62
63     assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
64     assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
65     assertThatMeasuresAreExactly(m1, m2, m7, m8);
66
67     // migration is re-entrant
68     run(false);
69     assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
70     assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
71     assertThatMeasuresAreExactly(m1, m2, m7, m8);
72   }
73
74   @Test
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");
80
81     run(true);
82
83     assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(1);
84     assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(1);
85     assertThat(db.countRowsOfTable("PROJECT_MEASURES")).isEqualTo(2);
86   }
87
88   private void run(boolean sonarCloud) throws SQLException {
89     MapSettings settings = new MapSettings();
90     if (sonarCloud) {
91       settings.setProperty("sonar.sonarcloud.enabled", true);
92     }
93     DataChange underTest = new DeletePersonAndFileMeasures(db.database(), settings.asConfig());
94     underTest.execute();
95   }
96
97   private void assertThatMeasuresAreExactly(long... expectedMeasureIds) {
98     long[] ids = db.select("select id as \"id\" from project_measures")
99       .stream()
100       .mapToLong(m -> (Long) m.get("id"))
101       .toArray();
102     assertThat(ids).containsOnly(expectedMeasureIds);
103   }
104
105   private void insertComponent(String uuid, String scope, String qualifier) {
106     db.executeInsert("PROJECTS",
107       "ORGANIZATION_UUID", "O1",
108       "KEE", "" + GENERATOR.incrementAndGet(),
109       "UUID", uuid,
110       "PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
111       "MAIN_BRANCH_PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
112       "UUID_PATH", ".",
113       "ROOT_UUID", "" + GENERATOR.incrementAndGet(),
114       "PRIVATE", "true",
115       "QUALIFIER", qualifier,
116       "SCOPE", scope);
117   }
118
119   private void insertSnapshot(String uuid, String projectUuid, boolean last) {
120     db.executeInsert("SNAPSHOTS",
121       "UUID", uuid,
122       "COMPONENT_UUID", projectUuid,
123       "STATUS", "P",
124       "ISLAST", last);
125   }
126
127   private long insertMeasure(String componentUuid, String analysisUuid) {
128     long id = GENERATOR.incrementAndGet();
129     db.executeInsert("PROJECT_MEASURES",
130       "ID", id,
131       "METRIC_ID", "42",
132       "COMPONENT_UUID", componentUuid,
133       "ANALYSIS_UUID", analysisUuid);
134     return id;
135   }
136
137   private long insertPersonMeasure(String componentUuid, String analysisUuid) {
138     long id = GENERATOR.incrementAndGet();
139     db.executeInsert("PROJECT_MEASURES",
140       "ID", id,
141       "METRIC_ID", "42",
142       "COMPONENT_UUID", componentUuid,
143       "ANALYSIS_UUID", analysisUuid,
144       "PERSON_ID", RandomUtils.nextInt(100));
145     return id;
146   }
147 }