]> source.dussan.org Git - sonarqube.git/blob
f0b87843f20a481d19164da39392d050ed674d4c
[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.nio.charset.StandardCharsets;
23 import java.sql.SQLException;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.RegisterExtension;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.util.SequenceUuidFactory;
31 import org.sonar.db.MigrationDbTester;
32 import org.sonar.server.platform.db.migration.step.DataChange;
33
34 import static java.lang.String.format;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.tuple;
37 import static org.mockito.Mockito.mock;
38
39 class MigratePortfoliosLiveMeasuresToMeasuresIT {
40
41   private static final String MEASURES_MIGRATED_COLUMN = "measures_migrated";
42   public static final String SELECT_MEASURE = "select component_uuid, branch_uuid, json_value, json_value_hash, created_at, updated_at " +
43     "from measures where component_uuid = '%s'";
44
45   @RegisterExtension
46   public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(MigratePortfoliosLiveMeasuresToMeasures.class);
47
48   private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory();
49   private final System2 system2 = mock();
50   private final DataChange underTest = new MigratePortfoliosLiveMeasuresToMeasures(db.database(), system2);
51
52   @Test
53   void shall_complete_when_tables_are_empty() throws SQLException {
54     underTest.execute();
55
56     assertThat(db.countRowsOfTable("measures")).isZero();
57   }
58
59   @Test
60   void shall_not_migrate_when_portfolio_is_already_flagged() throws SQLException {
61     String nclocMetricUuid = insertMetric("ncloc", "INT");
62     String qgStatusMetricUuid = insertMetric("quality_gate_status", "STRING");
63     String metricWithDataUuid = insertMetric("metric_with_data", "DATA");
64     String portfolio1 = "portfolio_1";
65     insertMigratedPortfolio(portfolio1);
66     insertMeasure(portfolio1, nclocMetricUuid, Map.of("value", 120));
67     insertMeasure(portfolio1, qgStatusMetricUuid, Map.of("text_value", "ok"));
68     insertMeasure(portfolio1, metricWithDataUuid, Map.of("measure_data", "some data".getBytes(StandardCharsets.UTF_8)));
69
70     insertMigratedPortfolio("portfolio_2");
71     insertMeasure("portfolio_2", nclocMetricUuid, Map.of("value", 14220));
72
73     underTest.execute();
74
75     assertThat(db.countRowsOfTable("measures")).isZero();
76   }
77
78   @Test
79   void should_flag_portfolio_with_no_measures() throws SQLException {
80     String portfolio = "portfolio_3";
81     insertNotMigratedPortfolio(portfolio);
82
83     underTest.execute();
84
85     assertPortfolioMigrated(portfolio);
86     assertThat(db.countRowsOfTable("measures")).isZero();
87   }
88
89   @Test
90   void should_migrate_portfolio_with_measures() throws SQLException {
91     String nclocMetricUuid = insertMetric("ncloc", "INT");
92     String qgStatusMetricUuid = insertMetric("quality_gate_status", "STRING");
93     String metricWithDataUuid = insertMetric("metric_with_data", "DATA");
94
95     String portfolio1 = "portfolio_4";
96     insertNotMigratedPortfolio(portfolio1);
97     String component1 = uuidFactory.create();
98     String component2 = uuidFactory.create();
99     insertMeasure(portfolio1, component1, nclocMetricUuid, Map.of("value", 120));
100     insertMeasure(portfolio1, component1, qgStatusMetricUuid, Map.of("text_value", "ok"));
101     insertMeasure(portfolio1, component2, metricWithDataUuid, Map.of("measure_data", "some data".getBytes(StandardCharsets.UTF_8)));
102
103     String portfolio2 = "portfolio_5";
104     insertNotMigratedPortfolio(portfolio2);
105     insertMeasure(portfolio2, nclocMetricUuid, Map.of("value", 64));
106
107     String migratedPortfolio = "portfolio_6";
108     insertMigratedPortfolio(migratedPortfolio);
109     insertMeasure(migratedPortfolio, nclocMetricUuid, Map.of("value", 3684));
110
111     underTest.execute();
112
113     assertPortfolioMigrated(portfolio1);
114     assertPortfolioMigrated(portfolio2);
115     assertThat(db.countRowsOfTable("measures")).isEqualTo(3);
116
117     assertThat(db.select(format(SELECT_MEASURE, component1)))
118       .hasSize(1)
119       .extracting(t -> t.get("component_uuid"), t -> t.get("branch_uuid"), t -> t.get("json_value"), t -> t.get("json_value_hash"))
120       .containsOnly(tuple(component1, portfolio1, "{\"ncloc\":120.0,\"quality_gate_status\":\"ok\"}", 6033012287291512746L));
121
122     assertThat(db.select(format(SELECT_MEASURE, component2)))
123       .hasSize(1)
124       .extracting(t -> t.get("component_uuid"), t -> t.get("branch_uuid"), t -> t.get("json_value"), t -> t.get("json_value_hash"))
125       .containsOnly(tuple(component2, portfolio1, "{\"metric_with_data\":\"some data\"}", -4524184678167636687L));
126   }
127
128   private void assertPortfolioMigrated(String portfolio) {
129     List<Map<String, Object>> result = db.select(format("select %s as \"MIGRATED\" from portfolios where uuid = '%s'", MEASURES_MIGRATED_COLUMN, portfolio));
130     assertThat(result)
131       .hasSize(1)
132       .extracting(t -> t.get("MIGRATED"))
133       .containsOnly(true);
134   }
135
136   private String insertMetric(String metricName, String valueType) {
137     String metricUuid = uuidFactory.create();
138     db.executeInsert("metrics",
139       "uuid", metricUuid,
140       "name", metricName,
141       "val_type", valueType);
142     return metricUuid;
143   }
144
145   private void insertMeasure(String portfolioUuid, String metricUuid, Map<String, Object> data) {
146     insertMeasure(portfolioUuid, uuidFactory.create(), metricUuid, data);
147   }
148
149   private void insertMeasure(String portfolioUuid, String componentUuid, String metricUuid, Map<String, Object> data) {
150     Map<String, Object> dataMap = new HashMap<>(data);
151     dataMap.put("uuid", uuidFactory.create());
152     dataMap.put("component_uuid", componentUuid);
153     dataMap.put("project_uuid", portfolioUuid);
154     dataMap.put("metric_uuid", metricUuid);
155     dataMap.put("created_at", 12L);
156     dataMap.put("updated_at", 12L);
157
158     db.executeInsert("live_measures", dataMap);
159   }
160
161   private void insertNotMigratedPortfolio(String portfolioUuid) {
162     insertPortfolio(portfolioUuid, false);
163   }
164
165   private void insertMigratedPortfolio(String portfolioUuid) {
166     insertPortfolio(portfolioUuid, true);
167   }
168
169   private void insertPortfolio(String portfolioUuid, boolean migrated) {
170     db.executeInsert("portfolios",
171       "uuid", portfolioUuid,
172       "kee", portfolioUuid,
173       "name", portfolioUuid,
174       "private", true,
175       "root_uuid", portfolioUuid,
176       "selection_mode", "MANUAL",
177       MEASURES_MIGRATED_COLUMN, migrated,
178       "created_at", 12L,
179       "updated_at", 12L
180     );
181   }
182 }