1 package org.sonar.server.platform.db.migration.version.v108;
3 import java.sql.SQLException;
5 import org.junit.jupiter.api.Test;
6 import org.junit.jupiter.api.extension.RegisterExtension;
7 import org.sonar.core.util.UuidFactoryImpl;
8 import org.sonar.db.MigrationDbTester;
10 import static org.assertj.core.api.Assertions.assertThat;
12 class DeleteSoftwareQualityRatingFromProjectMeasuresIT {
15 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(DeleteSoftwareQualityRatingFromProjectMeasures.class);
17 private final DeleteSoftwareQualityRatingFromProjectMeasures underTest = new DeleteSoftwareQualityRatingFromProjectMeasures(db.database());
20 void execute_whenMeasuresExists_shouldDeleteMeasures() throws SQLException {
21 DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.forEach(key -> {
22 String metricUUid = insertMetric(key);
23 insertProjectMeasure(metricUUid);
26 assertThat(db.countSql("select count(1) from project_measures;"))
27 .isEqualTo(DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.size());
31 assertThat(db.countSql("select count(1) from project_measures;")).isZero();
35 void execute_whenOtherMeasuresExists_shouldNotDeleteMeasures() throws SQLException {
36 String metricUUid = insertMetric("other_metric");
37 insertProjectMeasure(metricUUid);
39 assertThat(db.countSql("select count(1) from project_measures;")).isEqualTo(1);
43 assertThat(db.countSql("select count(1) from project_measures;")).isEqualTo(1);
47 void execute_shouldBeReentrant() throws SQLException {
48 DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.forEach(key -> {
49 String metricUUid = insertMetric(key);
50 insertProjectMeasure(metricUUid);
53 String metricUUid = insertMetric("other_metric");
54 insertProjectMeasure(metricUUid);
56 assertThat(db.countSql("select count(1) from project_measures;"))
57 .isEqualTo(DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.size() + 1);
62 assertThat(db.countSql("select count(1) from project_measures;")).isOne();
65 private String insertMetric(String key) {
66 String uuid = UuidFactoryImpl.INSTANCE.create();
67 Map<String, Object> map = Map.ofEntries(
68 Map.entry("UUID", uuid),
69 Map.entry("NAME", key));
70 db.executeInsert("metrics", map);
74 private void insertProjectMeasure(String metricUuid) {
75 Map<String, Object> map = Map.ofEntries(
76 Map.entry("UUID", UuidFactoryImpl.INSTANCE.create()),
77 Map.entry("METRIC_UUID", metricUuid),
78 Map.entry("COMPONENT_UUID", UuidFactoryImpl.INSTANCE.create()),
79 Map.entry("ANALYSIS_UUID", UuidFactoryImpl.INSTANCE.create()));
80 db.executeInsert("project_measures", map);