]> source.dussan.org Git - sonarqube.git/blob
10bd9221d05a6eabe8665b0557f1129f009dadaa
[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.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.function.Function;
27 import org.assertj.core.groups.Tuple;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.internal.TestSystem2;
32 import org.sonar.db.CoreDbTester;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
36
37 public class PopulateLiveMeasuresTest {
38
39   private System2 system2 = new TestSystem2().setNow(1_500_000_000_000L);
40
41   @Rule
42   public CoreDbTester db = CoreDbTester.createForSchema(PopulateLiveMeasuresTest.class, "initial.sql");
43
44   private PopulateLiveMeasures underTest = new PopulateLiveMeasures(db.database(), system2);
45
46   @Test
47   public void do_nothing_when_no_data() throws SQLException {
48     assertThat(db.countRowsOfTable("PROJECT_MEASURES")).isEqualTo(0);
49     underTest.execute();
50     assertThat(db.countRowsOfTable("LIVE_MEASURES")).isEqualTo(0);
51   }
52
53   @Test
54   public void execute_must_update_database() throws SQLException {
55     generateProjectMeasures();
56
57     underTest.execute();
58
59     assertThat(getLiveMeasures()).extracting(
60       field("COMPONENT_UUID"),
61       field("PROJECT_UUID"),
62       field("METRIC_ID"),
63       field("VALUE"),
64       field("TEXT_VALUE"),
65       field("VARIATION"),
66       field("MEASURE_DATA")
67     ).containsExactlyInAnyOrder(generateLiveMeasures());
68   }
69
70   private Function<Map<String, Object>, Object> field(String name) {
71     return m -> m.get(name);
72   }
73
74   @Test
75   public void migration_is_reentrant() throws SQLException {
76     generateProjectMeasures();
77
78     underTest.execute();
79     underTest.execute();
80
81     assertThat(getLiveMeasures()).extracting(
82       field("COMPONENT_UUID"),
83       field("PROJECT_UUID"),
84       field("METRIC_ID"),
85       field("VALUE"),
86       field("TEXT_VALUE"),
87       field("VARIATION"),
88       field("MEASURE_DATA")
89     ).containsExactlyInAnyOrder(generateLiveMeasures());
90   }
91
92   private void generateProjectMeasures() {
93     Map<String, Object> project = new HashMap<>();
94     project.put("UUID", "PRJ1");
95     project.put("ORGANIZATION_UUID", "ORG1");
96     project.put("UUID_PATH", "X");
97     project.put("ROOT_UUID", "X");
98     project.put("PROJECT_UUID", "PRJ1");
99     project.put("PRIVATE", "FALSE");
100     db.executeInsert("PROJECTS", project);
101
102     Map<String, Object> analysis1 = new HashMap<>();
103     analysis1.put("UUID", "A1");
104     analysis1.put("ISLAST", "FALSE");
105     analysis1.put("COMPONENT_UUID", "PRJ1");
106     db.executeInsert("SNAPSHOTS", analysis1);
107
108     Map<String, Object> analysis2 = new HashMap<>();
109     analysis2.put("UUID", "A2");
110     analysis2.put("ISLAST", "TRUE");
111     analysis2.put("COMPONENT_UUID", "PRJ1");
112     db.executeInsert("SNAPSHOTS", analysis2);
113
114     Map<String, Object> measure1 = new HashMap<>();
115     measure1.put("COMPONENT_UUID", "PRJ1");
116     measure1.put("ANALYSIS_UUID", "A1");
117     measure1.put("METRIC_ID", "123");
118     db.executeInsert("PROJECT_MEASURES", measure1);
119
120     Map<String, Object> measure2 = new HashMap<>();
121     measure2.put("COMPONENT_UUID", "PRJ1");
122     measure2.put("ANALYSIS_UUID", "A2");
123     measure2.put("METRIC_ID", "123");
124     measure2.put("VALUE", "234");
125     measure2.put("TEXT_VALUE", "TEXT_VALUEx");
126     measure2.put("VARIATION_VALUE_1", "345");
127     measure2.put("MEASURE_DATA", "FFFF");
128     db.executeInsert("PROJECT_MEASURES", measure2);
129
130     // measures with person_id not null are purged later
131     // by another migration
132     Map<String, Object> personMeasure = new HashMap<>();
133     personMeasure.put("COMPONENT_UUID", "PRJ1");
134     personMeasure.put("ANALYSIS_UUID", "A2");
135     personMeasure.put("METRIC_ID", "200");
136     personMeasure.put("VALUE", "234");
137     personMeasure.put("TEXT_VALUE", "TEXT_VALUEx");
138     personMeasure.put("VARIATION_VALUE_1", "345");
139     personMeasure.put("MEASURE_DATA", "FFFF");
140     personMeasure.put("PERSON_ID", "99");
141     db.executeInsert("PROJECT_MEASURES", personMeasure);
142   }
143
144   private List<Map<String, Object>> getLiveMeasures() {
145     return db.select("SELECT * FROM LIVE_MEASURES");
146   }
147
148   private Tuple[] generateLiveMeasures() {
149     return new Tuple[] {
150       tuple("PRJ1", "PRJ1", 123L, 234.0, "TEXT_VALUEx", 345.0, new byte[] {-1, -1})
151     };
152   }
153 }