]> source.dussan.org Git - sonarqube.git/blob
9ec43b5e6dae41bd3df390adacabf1ac280ddd33
[sonarqube.git] /
1 package org.sonar.server.platform.db.migration.version.v108;
2
3 import java.sql.SQLException;
4 import java.util.Map;
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;
9
10 import static org.assertj.core.api.Assertions.assertThat;
11
12 class DeleteSoftwareQualityRatingFromProjectMeasuresIT {
13
14   @RegisterExtension
15   public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(DeleteSoftwareQualityRatingFromProjectMeasures.class);
16
17   private final DeleteSoftwareQualityRatingFromProjectMeasures underTest = new DeleteSoftwareQualityRatingFromProjectMeasures(db.database());
18
19   @Test
20   void execute_whenMeasuresExists_shouldDeleteMeasures() throws SQLException {
21     DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.forEach(key -> {
22       String metricUUid = insertMetric(key);
23       insertProjectMeasure(metricUUid);
24     });
25
26     assertThat(db.countSql("select count(1) from project_measures;"))
27       .isEqualTo(DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.size());
28
29     underTest.execute();
30
31     assertThat(db.countSql("select count(1) from project_measures;")).isZero();
32   }
33
34   @Test
35   void execute_whenOtherMeasuresExists_shouldNotDeleteMeasures() throws SQLException {
36     String metricUUid = insertMetric("other_metric");
37     insertProjectMeasure(metricUUid);
38
39     assertThat(db.countSql("select count(1) from project_measures;")).isEqualTo(1);
40
41     underTest.execute();
42
43     assertThat(db.countSql("select count(1) from project_measures;")).isEqualTo(1);
44   }
45
46   @Test
47   void execute_shouldBeReentrant() throws SQLException {
48     DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.forEach(key -> {
49       String metricUUid = insertMetric(key);
50       insertProjectMeasure(metricUUid);
51     });
52
53     String metricUUid = insertMetric("other_metric");
54     insertProjectMeasure(metricUUid);
55
56     assertThat(db.countSql("select count(1) from project_measures;"))
57       .isEqualTo(DeleteSoftwareQualityRatingFromProjectMeasures.SOFTWARE_QUALITY_METRICS_TO_DELETE.size() + 1);
58
59     underTest.execute();
60     underTest.execute();
61
62     assertThat(db.countSql("select count(1) from project_measures;")).isOne();
63   }
64
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);
71     return uuid;
72   }
73
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);
81   }
82 }