]> source.dussan.org Git - sonarqube.git/blob
358a6ace30c47af9889c9795ccbea3d3d2c143ca
[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 MigrateBranchesLiveMeasuresToMeasuresIT {
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(MigrateBranchesLiveMeasuresToMeasures.class);
47
48   private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory();
49   private final System2 system2 = mock();
50   private final DataChange underTest = new MigrateBranchesLiveMeasuresToMeasures(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_branch_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 branch1 = "branch_1";
65     insertMigratedBranch(branch1);
66     insertMeasure(branch1, nclocMetricUuid, Map.of("value", 120));
67     insertMeasure(branch1, qgStatusMetricUuid, Map.of("text_value", "ok"));
68     insertMeasure(branch1, metricWithDataUuid, Map.of("measure_data", "some data".getBytes(StandardCharsets.UTF_8)));
69
70     insertMigratedBranch("branch_2");
71     insertMeasure("branch_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_branch_with_no_measures() throws SQLException {
80     String branch = "branch_3";
81     insertNotMigratedBranch(branch);
82
83     underTest.execute();
84
85     assertBranchMigrated(branch);
86     assertThat(db.countRowsOfTable("measures")).isZero();
87   }
88
89   @Test
90   void should_migrate_branch_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 branch1 = "branch_4";
96     insertNotMigratedBranch(branch1);
97     String component1 = uuidFactory.create();
98     String component2 = uuidFactory.create();
99     insertMeasure(branch1, component1, nclocMetricUuid, Map.of("value", 120));
100     insertMeasure(branch1, component1, qgStatusMetricUuid, Map.of("text_value", "ok"));
101     insertMeasure(branch1, component2, metricWithDataUuid, Map.of("measure_data", "some data".getBytes(StandardCharsets.UTF_8)));
102
103     String branch2 = "branch_5";
104     insertNotMigratedBranch(branch2);
105     insertMeasure(branch2, nclocMetricUuid, Map.of("value", 64));
106
107     String migratedBranch = "branch_6";
108     insertMigratedBranch(migratedBranch);
109     insertMeasure(migratedBranch, nclocMetricUuid, Map.of("value", 3684));
110
111     underTest.execute();
112
113     assertBranchMigrated(branch1);
114     assertBranchMigrated(branch2);
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, branch1, "{\"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, branch1, "{\"metric_with_data\":\"some data\"}", -4524184678167636687L));
126   }
127
128   private void assertBranchMigrated(String branch) {
129     List<Map<String, Object>> result = db.select(format("select %s as \"MIGRATED\" from project_branches where uuid = '%s'", MEASURES_MIGRATED_COLUMN, branch));
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 branchUuid, String metricUuid, Map<String, Object> data) {
146     insertMeasure(branchUuid, uuidFactory.create(), metricUuid, data);
147   }
148
149   private void insertMeasure(String branchUuid, 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", branchUuid);
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 insertNotMigratedBranch(String branchUuid) {
162     insertBranch(branchUuid, false);
163   }
164
165   private void insertMigratedBranch(String branchUuid) {
166     insertBranch(branchUuid, true);
167   }
168
169   private void insertBranch(String branchUuid, boolean migrated) {
170     db.executeInsert("project_branches",
171       "uuid", branchUuid,
172       "kee", branchUuid,
173       "branch_type", "LONG",
174       "project_uuid", uuidFactory.create(),
175       MEASURES_MIGRATED_COLUMN, migrated,
176       "need_issue_sync", false,
177       "is_main", true,
178       "created_at", 12L,
179       "updated_at", 12L
180     );
181   }
182
183
184 }