]> source.dussan.org Git - sonarqube.git/blob
16421382ed32674a45f6b59b7fb2d28781fc536e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.v108;
21
22 import java.sql.SQLException;
23 import java.util.Map;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.RegisterExtension;
26 import org.sonar.core.util.UuidFactoryImpl;
27 import org.sonar.db.MigrationDbTester;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.tuple;
31
32 class CreateNewSoftwareQualityMetricsIT {
33   @RegisterExtension
34   public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(CreateNewSoftwareQualityMetrics.class);
35   private final CreateNewSoftwareQualityMetrics underTest = new CreateNewSoftwareQualityMetrics(db.database(), UuidFactoryImpl.INSTANCE);
36
37   @Test
38   void execute_shouldCreateMetrics() throws SQLException {
39     assertThat(db.select("select name,direction,qualitative,enabled,best_value,optimized_best_value,delete_historical_data  from metrics"))
40       .isEmpty();
41     underTest.execute();
42     assertThat(db.select("select name,direction,qualitative,enabled,best_value,optimized_best_value,delete_historical_data  from metrics"))
43       .hasSize(6)
44       .extracting(s -> s.get("name"), s -> s.get("direction"), s -> s.get("qualitative"), s -> s.get("enabled"), s -> s.get("best_value"), s -> s.get("optimized_best_value"),
45         s -> s.get("delete_historical_data"))
46       .containsExactlyInAnyOrder(
47         tuple("software_quality_reliability_issues", -1L, false, true, 0.0, true, false),
48         tuple("software_quality_security_issues", -1L, false, true, 0.0, true, false),
49         tuple("new_software_quality_reliability_issues", -1L, true, true, 0.0, true, true),
50         tuple("new_software_quality_security_issues", -1L, true, true, 0.0, true, true),
51         tuple("new_software_quality_maintainability_issues", -1L, true, true, 0.0, true, true),
52         tuple("software_quality_maintainability_issues", -1L, false, true, 0.0, true, false));
53   }
54
55   @Test
56   void execute_shouldBeReentrant() throws SQLException {
57     underTest.execute();
58     underTest.execute();
59     assertThat(db.select("select name,direction,qualitative,enabled,best_value,optimized_best_value,delete_historical_data  from metrics"))
60       .hasSize(6);
61   }
62
63   @Test
64   void execute_whenOnlyOneMetricExists_shouldCreateOtherOnes() throws SQLException {
65     String existingMetricUuid = insertMetric("software_quality_security_issues");
66     underTest.execute();
67
68     assertThat(db.select("select uuid  from metrics"))
69       .hasSize(6)
70       .extracting(e -> e.get("uuid"))
71       .contains(existingMetricUuid);
72   }
73
74   private String insertMetric(String key) {
75     String uuid = UuidFactoryImpl.INSTANCE.create();
76     Map<String, Object> map = Map.ofEntries(
77       Map.entry("UUID", uuid),
78       Map.entry("NAME", key));
79     db.executeInsert("metrics", map);
80     return uuid;
81   }
82
83 }