]> source.dussan.org Git - sonarqube.git/blob
5785339655ec0f2ffc39927918d0aa45d6199316
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.v82;
21
22 import java.sql.SQLException;
23 import java.util.Random;
24 import java.util.UUID;
25 import java.util.stream.Collectors;
26 import java.util.stream.IntStream;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.db.CoreDbTester;
31 import org.sonar.server.platform.db.migration.step.DataChange;
32
33 import static org.apache.commons.lang.math.RandomUtils.nextInt;
34 import static org.assertj.core.api.Assertions.assertThat;
35
36 public class DeleteSecurityReviewRatingMeasuresTest {
37   private static final String PROJECT_MEASURES_TABLE_NAME = "PROJECT_MEASURES";
38   private static final String LIVE_MEASURES_TABLE_NAME = "LIVE_MEASURES";
39
40   private static final int SECURITY_REVIEW_RATING_METRIC_ID = 200;
41   private static final String SECURITY_REVIEW_RATING_METRIC_KEY = "security_review_rating";
42   private static final int SECURITY_REVIEW_RATING_EFFORT_METRIC_ID = 201;
43   private static final String SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY = "security_review_rating_effort";
44
45   private static final int OTHER_METRIC_ID_1 = 1;
46   private static final int OTHER_METRIC_ID_2 = 2;
47   private static final int OTHER_METRIC_MEASURES_COUNT = 20;
48
49   @Rule
50   public CoreDbTester db = CoreDbTester.createForSchema(DeleteSecurityReviewRatingMeasuresTest.class, "schema.sql");
51
52   private final DataChange underTest = new DeleteSecurityReviewRatingMeasures(db.database());
53
54   @Before
55   public void before() {
56     insertMetric(OTHER_METRIC_ID_1, "another metric#1");
57     insertMetric(OTHER_METRIC_ID_2, "another metric#2");
58   }
59
60   @Test
61   public void not_fail_if_metrics_not_defined() throws SQLException {
62     String projectUuid = insertComponent("PRJ", "TRK");
63     insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, projectUuid);
64     insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, projectUuid);
65
66     underTest.execute();
67
68     assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(1);
69     assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(1);
70   }
71
72   @Test
73   public void not_fail_if_security_review_rating_effort_metric_not_found() throws SQLException {
74     insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY);
75
76     String applicationUuid = insertComponent("PRJ", "TRK");
77
78     insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid);
79     insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid);
80
81     generateOtherMetricMeasures(2, applicationUuid);
82     generateOtherMetricsLiveMeasures(applicationUuid);
83
84     underTest.execute();
85
86     assertSecurityReviewRatingMeasuresDeleted();
87     assertSecurityReviewRatingLiveMeasuresDeleted();
88
89     assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
90     assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
91
92     // should not fail if called twice
93     underTest.execute();
94   }
95
96   @Test
97   public void remove_security_rating_review_from_measures_and_live_measures_projects() throws SQLException {
98     insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY);
99     insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY);
100
101     String applicationUuid = insertComponent("PRJ", "TRK");
102
103     insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid);
104     insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid);
105
106     generateOtherMetricMeasures(2, applicationUuid);
107     generateOtherMetricsLiveMeasures(applicationUuid);
108
109     underTest.execute();
110
111     assertSecurityReviewRatingMeasuresDeleted();
112     assertSecurityReviewRatingLiveMeasuresDeleted();
113
114     assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
115     assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
116
117     // should not fail if called twice
118     underTest.execute();
119   }
120
121   @Test
122   public void remove_security_rating_review_from_measures_and_live_measures_for_portfolios() throws SQLException {
123     insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY);
124     insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY);
125
126     String portfolioUuid = insertComponent("PRJ", "VW");
127     String subPortfolioUuid = insertComponent("PRJ", "SVW");
128
129     insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid);
130     insertMeasure(2, SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid);
131
132     insertMeasure(3, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid);
133     insertMeasure(4, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid);
134
135     insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid, portfolioUuid);
136     insertLiveMeasure("uuid-2", SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid, subPortfolioUuid);
137
138     insertLiveMeasure("uuid-3", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid, portfolioUuid);
139     insertLiveMeasure("uuid-4", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid, subPortfolioUuid);
140
141
142     generateOtherMetricMeasures(5, portfolioUuid);
143     generateOtherMetricsLiveMeasures(portfolioUuid);
144
145     underTest.execute();
146
147     assertSecurityReviewRatingMeasuresDeleted();
148     assertSecurityReviewRatingLiveMeasuresDeleted();
149
150     assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
151     assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
152
153     // should not fail if called twice
154     underTest.execute();
155   }
156
157   @Test
158   public void remove_security_rating_review_from_measures_and_live_measures_applications() throws SQLException {
159     insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY);
160     insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY);
161
162     String applicationUuid = insertComponent("PRJ", "APP");
163
164     insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid);
165     insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid);
166
167     generateOtherMetricMeasures(2, applicationUuid);
168     generateOtherMetricsLiveMeasures(applicationUuid);
169
170     underTest.execute();
171
172     assertSecurityReviewRatingMeasuresDeleted();
173     assertSecurityReviewRatingLiveMeasuresDeleted();
174
175     assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
176     assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
177
178     // should not fail if called twice
179     underTest.execute();
180   }
181
182   @Test
183   public void remove_security_rating_review_from_measures_and_live_measures_mixed() throws SQLException {
184     insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY);
185     insertMetric(SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, SECURITY_REVIEW_RATING_EFFORT_METRIC_KEY);
186
187     String portfolioUuid = insertComponent("PRJ", "VW");
188     String subPortfolioUuid = insertComponent("PRJ", "SVW");
189     String applicationUuid = insertComponent("PRJ", "APP");
190     String projectUuid = insertComponent("PRJ", "TRK");
191
192     insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid);
193     insertMeasure(2, SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid);
194     insertMeasure(3, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid);
195     insertMeasure(4, SECURITY_REVIEW_RATING_METRIC_ID, projectUuid);
196
197     insertMeasure(5, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid);
198     insertMeasure(6, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid);
199
200     insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid, portfolioUuid);
201     insertLiveMeasure("uuid-2", SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid, subPortfolioUuid);
202     insertLiveMeasure("uuid-3", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid);
203     insertLiveMeasure("uuid-4", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, projectUuid);
204     insertLiveMeasure("uuid-5", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, getRandomUuid());
205     insertLiveMeasure("uuid-6", SECURITY_REVIEW_RATING_METRIC_ID, projectUuid, getRandomUuid());
206
207     insertLiveMeasure("uuid-7", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid, portfolioUuid);
208     insertLiveMeasure("uuid-8", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid, subPortfolioUuid);
209
210     generateOtherMetricMeasures(7, projectUuid);
211     generateOtherMetricsLiveMeasures(projectUuid);
212
213     underTest.execute();
214
215     assertSecurityReviewRatingMeasuresDeleted();
216     assertSecurityReviewRatingLiveMeasuresDeleted();
217
218     assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
219     assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(OTHER_METRIC_MEASURES_COUNT);
220
221     // should not fail if called twice
222     underTest.execute();
223   }
224
225   @Test
226   public void not_fail_if_empty_tables() throws SQLException {
227     insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY);
228
229     underTest.execute();
230
231     assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isZero();
232     assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isZero();
233   }
234
235   private void generateOtherMetricsLiveMeasures(String componentUuid) {
236     IntStream.range(0, OTHER_METRIC_MEASURES_COUNT)
237       .peek(i -> insertLiveMeasure("uuid-other-" + i, i, componentUuid, getRandomUuid()))
238       .boxed()
239       .collect(Collectors.toList());
240   }
241
242   private void generateOtherMetricMeasures(int startId, String componentUuid) {
243     IntStream.range(startId, startId + OTHER_METRIC_MEASURES_COUNT)
244       .peek(i -> insertMeasure(i, new Random().nextBoolean() ? OTHER_METRIC_ID_1 : OTHER_METRIC_ID_2, componentUuid))
245       .boxed()
246       .collect(Collectors.toList());
247   }
248
249   private void assertSecurityReviewRatingLiveMeasuresDeleted() {
250     assertThat(db.countSql("select count(uuid) from LIVE_MEASURES where metric_id = " + SECURITY_REVIEW_RATING_METRIC_ID))
251       .isZero();
252   }
253
254   private void assertSecurityReviewRatingMeasuresDeleted() {
255     assertThat(db.countSql("select count(id) from project_measures where metric_id = " + SECURITY_REVIEW_RATING_METRIC_ID))
256       .isZero();
257   }
258
259   private void insertMeasure(int id, int metricId, String componentUuid) {
260     db.executeInsert("PROJECT_MEASURES",
261       "ID", id,
262       "METRIC_ID", metricId,
263       "ANALYSIS_UUID", getRandomUuid(),
264       "COMPONENT_UUID", componentUuid);
265   }
266
267   private String getRandomUuid() {
268     return UUID.randomUUID().toString();
269   }
270
271   private void insertLiveMeasure(String uuid, int metricId, String projectUuid, String componentUuid) {
272     db.executeInsert("LIVE_MEASURES",
273       "UUID", uuid,
274       "PROJECT_UUID", projectUuid,
275       "COMPONENT_UUID", componentUuid,
276       "METRIC_ID", metricId,
277       "CREATED_AT", System.currentTimeMillis(),
278       "UPDATED_AT", System.currentTimeMillis());
279   }
280
281   private void insertMetric(int id, String name) {
282     db.executeInsert("METRICS",
283       "ID", id,
284       "NAME", name,
285       "DIRECTION", 0,
286       "QUALITATIVE", true);
287   }
288
289   private String insertComponent(String scope, String qualifier) {
290     int id = nextInt();
291     String uuid = getRandomUuid();
292     db.executeInsert("COMPONENTS",
293       "ID", id,
294       "UUID", uuid,
295       "ORGANIZATION_UUID", "default",
296       "PROJECT_UUID", uuid,
297       "UUID_PATH", ".",
298       "ROOT_UUID", uuid,
299       "PRIVATE", Boolean.toString(false),
300       "SCOPE", scope,
301       "QUALIFIER", qualifier);
302     return uuid;
303   }
304 }