3 * Copyright (C) 2009-2021 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.v82;
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;
33 import static org.apache.commons.lang.math.RandomUtils.nextInt;
34 import static org.assertj.core.api.Assertions.assertThat;
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";
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";
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;
50 public CoreDbTester db = CoreDbTester.createForSchema(DeleteSecurityReviewRatingMeasuresTest.class, "schema.sql");
52 private final DataChange underTest = new DeleteSecurityReviewRatingMeasures(db.database());
55 public void before() {
56 insertMetric(OTHER_METRIC_ID_1, "another metric#1");
57 insertMetric(OTHER_METRIC_ID_2, "another metric#2");
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);
68 assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isEqualTo(1);
69 assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isEqualTo(1);
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);
76 String applicationUuid = insertComponent("PRJ", "TRK");
78 insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid);
79 insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid);
81 generateOtherMetricMeasures(2, applicationUuid);
82 generateOtherMetricsLiveMeasures(applicationUuid);
86 assertSecurityReviewRatingMeasuresDeleted();
87 assertSecurityReviewRatingLiveMeasuresDeleted();
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);
92 // should not fail if called twice
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);
101 String applicationUuid = insertComponent("PRJ", "TRK");
103 insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid);
104 insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid);
106 generateOtherMetricMeasures(2, applicationUuid);
107 generateOtherMetricsLiveMeasures(applicationUuid);
111 assertSecurityReviewRatingMeasuresDeleted();
112 assertSecurityReviewRatingLiveMeasuresDeleted();
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);
117 // should not fail if called twice
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);
126 String portfolioUuid = insertComponent("PRJ", "VW");
127 String subPortfolioUuid = insertComponent("PRJ", "SVW");
129 insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid);
130 insertMeasure(2, SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid);
132 insertMeasure(3, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid);
133 insertMeasure(4, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid);
135 insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, portfolioUuid, portfolioUuid);
136 insertLiveMeasure("uuid-2", SECURITY_REVIEW_RATING_METRIC_ID, subPortfolioUuid, subPortfolioUuid);
138 insertLiveMeasure("uuid-3", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid, portfolioUuid);
139 insertLiveMeasure("uuid-4", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid, subPortfolioUuid);
142 generateOtherMetricMeasures(5, portfolioUuid);
143 generateOtherMetricsLiveMeasures(portfolioUuid);
147 assertSecurityReviewRatingMeasuresDeleted();
148 assertSecurityReviewRatingLiveMeasuresDeleted();
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);
153 // should not fail if called twice
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);
162 String applicationUuid = insertComponent("PRJ", "APP");
164 insertMeasure(1, SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid);
165 insertLiveMeasure("uuid-1", SECURITY_REVIEW_RATING_METRIC_ID, applicationUuid, applicationUuid);
167 generateOtherMetricMeasures(2, applicationUuid);
168 generateOtherMetricsLiveMeasures(applicationUuid);
172 assertSecurityReviewRatingMeasuresDeleted();
173 assertSecurityReviewRatingLiveMeasuresDeleted();
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);
178 // should not fail if called twice
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);
187 String portfolioUuid = insertComponent("PRJ", "VW");
188 String subPortfolioUuid = insertComponent("PRJ", "SVW");
189 String applicationUuid = insertComponent("PRJ", "APP");
190 String projectUuid = insertComponent("PRJ", "TRK");
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);
197 insertMeasure(5, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid);
198 insertMeasure(6, SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid);
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());
207 insertLiveMeasure("uuid-7", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, portfolioUuid, portfolioUuid);
208 insertLiveMeasure("uuid-8", SECURITY_REVIEW_RATING_EFFORT_METRIC_ID, subPortfolioUuid, subPortfolioUuid);
210 generateOtherMetricMeasures(7, projectUuid);
211 generateOtherMetricsLiveMeasures(projectUuid);
215 assertSecurityReviewRatingMeasuresDeleted();
216 assertSecurityReviewRatingLiveMeasuresDeleted();
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);
221 // should not fail if called twice
226 public void not_fail_if_empty_tables() throws SQLException {
227 insertMetric(SECURITY_REVIEW_RATING_METRIC_ID, SECURITY_REVIEW_RATING_METRIC_KEY);
231 assertThat(db.countRowsOfTable(PROJECT_MEASURES_TABLE_NAME)).isZero();
232 assertThat(db.countRowsOfTable(LIVE_MEASURES_TABLE_NAME)).isZero();
235 private void generateOtherMetricsLiveMeasures(String componentUuid) {
236 IntStream.range(0, OTHER_METRIC_MEASURES_COUNT)
237 .peek(i -> insertLiveMeasure("uuid-other-" + i, i, componentUuid, getRandomUuid()))
239 .collect(Collectors.toList());
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))
246 .collect(Collectors.toList());
249 private void assertSecurityReviewRatingLiveMeasuresDeleted() {
250 assertThat(db.countSql("select count(uuid) from LIVE_MEASURES where metric_id = " + SECURITY_REVIEW_RATING_METRIC_ID))
254 private void assertSecurityReviewRatingMeasuresDeleted() {
255 assertThat(db.countSql("select count(id) from project_measures where metric_id = " + SECURITY_REVIEW_RATING_METRIC_ID))
259 private void insertMeasure(int id, int metricId, String componentUuid) {
260 db.executeInsert("PROJECT_MEASURES",
262 "METRIC_ID", metricId,
263 "ANALYSIS_UUID", getRandomUuid(),
264 "COMPONENT_UUID", componentUuid);
267 private String getRandomUuid() {
268 return UUID.randomUUID().toString();
271 private void insertLiveMeasure(String uuid, int metricId, String projectUuid, String componentUuid) {
272 db.executeInsert("LIVE_MEASURES",
274 "PROJECT_UUID", projectUuid,
275 "COMPONENT_UUID", componentUuid,
276 "METRIC_ID", metricId,
277 "CREATED_AT", System.currentTimeMillis(),
278 "UPDATED_AT", System.currentTimeMillis());
281 private void insertMetric(int id, String name) {
282 db.executeInsert("METRICS",
286 "QUALITATIVE", true);
289 private String insertComponent(String scope, String qualifier) {
291 String uuid = getRandomUuid();
292 db.executeInsert("COMPONENTS",
295 "ORGANIZATION_UUID", "default",
296 "PROJECT_UUID", uuid,
299 "PRIVATE", Boolean.toString(false),
301 "QUALIFIER", qualifier);