3 * Copyright (C) 2009-2017 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.db.CoreDbTester;
28 import org.sonar.server.platform.db.migration.step.DataChange;
30 import static org.assertj.core.api.Assertions.assertThat;
32 public class DeletePersonAndFileMeasuresTest {
33 private static final AtomicInteger GENERATOR = new AtomicInteger();
35 public CoreDbTester db = CoreDbTester.createForSchema(DeletePersonAndFileMeasuresTest.class, "initial.sql");
37 private DataChange underTest = new DeletePersonAndFileMeasures(db.database());
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);
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");
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");
64 assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
65 assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
66 assertThatMeasuresAreExactly(m1, m2, m7, m8);
68 // migration is re-entrant
70 assertThat(db.countRowsOfTable("PROJECTS")).isEqualTo(4);
71 assertThat(db.countRowsOfTable("SNAPSHOTS")).isEqualTo(2);
72 assertThatMeasuresAreExactly(m1, m2, m7, m8);
75 private void assertThatMeasuresAreExactly(long... expectedMeasureIds) {
76 long[] ids = db.select("select id as \"id\" from project_measures")
78 .mapToLong(m -> (Long) m.get("id"))
80 assertThat(ids).containsOnly(expectedMeasureIds);
83 private void insertComponent(String uuid, String scope, String qualifier) {
84 db.executeInsert("PROJECTS",
85 "ORGANIZATION_UUID", "O1",
86 "KEE", "" + GENERATOR.incrementAndGet(),
88 "PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
89 "MAIN_BRANCH_PROJECT_UUID", "" + GENERATOR.incrementAndGet(),
91 "ROOT_UUID", "" + GENERATOR.incrementAndGet(),
93 "QUALIFIER", qualifier,
97 private void insertSnapshot(String uuid, String projectUuid, boolean last) {
98 db.executeInsert("SNAPSHOTS",
100 "COMPONENT_UUID", projectUuid,
105 private long insertMeasure(String componentUuid, String analysisUuid) {
106 long id = GENERATOR.incrementAndGet();
107 db.executeInsert("PROJECT_MEASURES",
110 "COMPONENT_UUID", componentUuid,
111 "ANALYSIS_UUID", analysisUuid);
115 private long insertPersonMeasure(String componentUuid, String analysisUuid) {
116 long id = GENERATOR.incrementAndGet();
117 db.executeInsert("PROJECT_MEASURES",
120 "COMPONENT_UUID", componentUuid,
121 "ANALYSIS_UUID", analysisUuid,
122 "PERSON_ID", RandomUtils.nextInt(100));