]> source.dussan.org Git - sonarqube.git/blob
2019003f8e0682fc3d1511f7789e545f94f08507
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.db.CoreDbTester;
28 import org.sonar.server.platform.db.migration.step.DataChange;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31
32 public class DeletePersonAndFileMeasuresTest {
33   private static final AtomicInteger GENERATOR = new AtomicInteger();
34   @Rule
35   public CoreDbTester db = CoreDbTester.createForSchema(DeletePersonAndFileMeasuresTest.class, "initial.sql");
36
37   private DataChange underTest = new DeletePersonAndFileMeasures(db.database());
38
39   @Test
40   public void delete_file_and_person_measures() throws SQLException {
41     insertComponent("P1", "PRJ", "TRK");
42     insertComponent("D1", "DIR", "DIR");
43     insertComponent("F1", "FIL", "FIL");
44     insertComponent("F2", "FIL", "UTS");
45     insertSnapshot("S1", "P1", false);
46     insertSnapshot("S2", "P1", true);
47     // past measures
48     long m1 = insertMeasure("P1", "S1");
49     long m2 = insertMeasure("D1", "S1");
50     long m3 = insertMeasure("F1", "S1");
51     long m4 = insertMeasure("F2", "S1");
52     long m5 = insertPersonMeasure("P1", "S1");
53     long m6 = insertPersonMeasure("F1", "S1");
54     // last measures
55     long m7 = insertMeasure("P1", "S2");
56     long m8 = insertMeasure("D1", "S2");
57     long m9 = insertMeasure("F1", "S2");
58     long m10 = insertMeasure("F2", "S2");
59     long m11 = insertPersonMeasure("P1", "S2");
60     long m12 = insertPersonMeasure("F1", "S2");
61
62     underTest.execute();
63
64     assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
65     assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
66     assertThatMeasuresAreExactly(m1, m2, m7, m8);
67
68     // migration is re-entrant
69     underTest.execute();
70     assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
71     assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
72     assertThatMeasuresAreExactly(m1, m2, m7, m8);
73   }
74
75   private void assertThatMeasuresAreExactly(long... expectedMeasureIds) {
76     long[] ids = db.select("select id as \"id\" from project_measures")
77       .stream()
78       .mapToLong(m -> (Long) m.get("id"))
79       .toArray();
80     assertThat(ids).containsOnly(expectedMeasureIds);
81   }
82
83   private void insertComponent(String uuid, String scope, String qualifier) {
84     db.executeInsert("PROJECTS",
85       "ORGANIZATION_UUID", "O1",
86       "KEE", "" + GENERATOR.incrementAndGet(),
87       "UUID", uuid,
88       "PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
89       "MAIN_BRANCH_PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
90       "UUID_PATH", ".",
91       "ROOT_UUID", "" + GENERATOR.incrementAndGet(),
92       "PRIVATE", "true",
93       "QUALIFIER", qualifier,
94       "SCOPE", scope);
95   }
96
97   private void insertSnapshot(String uuid, String projectUuid, boolean last) {
98     db.executeInsert("SNAPSHOTS",
99       "UUID", uuid,
100       "COMPONENT_UUID", projectUuid,
101       "STATUS", "P",
102       "ISLAST", last);
103   }
104
105   private long insertMeasure(String componentUuid, String analysisUuid) {
106     long id = GENERATOR.incrementAndGet();
107     db.executeInsert("PROJECT_MEASURES",
108       "ID", id,
109       "METRIC_ID", "42",
110       "COMPONENT_UUID", componentUuid,
111       "ANALYSIS_UUID", analysisUuid);
112     return id;
113   }
114
115   private long insertPersonMeasure(String componentUuid, String analysisUuid) {
116     long id = GENERATOR.incrementAndGet();
117     db.executeInsert("PROJECT_MEASURES",
118       "ID", id,
119       "METRIC_ID", "42",
120       "COMPONENT_UUID", componentUuid,
121       "ANALYSIS_UUID", analysisUuid,
122       "PERSON_ID", RandomUtils.nextInt(100));
123     return id;
124   }
125 }