3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.db.migration.version.v108;
22 import java.nio.charset.StandardCharsets;
23 import java.sql.SQLException;
24 import java.util.HashMap;
25 import java.util.List;
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;
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;
39 class MigrateBranchesLiveMeasuresToMeasuresIT {
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'";
46 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(MigrateBranchesLiveMeasuresToMeasures.class);
48 private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory();
49 private final System2 system2 = mock();
50 private final DataChange underTest = new MigrateBranchesLiveMeasuresToMeasures(db.database(), system2);
53 void shall_complete_when_tables_are_empty() throws SQLException {
56 assertThat(db.countRowsOfTable("measures")).isZero();
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)));
70 insertMigratedBranch("branch_2");
71 insertMeasure("branch_2", nclocMetricUuid, Map.of("value", 14220));
75 assertThat(db.countRowsOfTable("measures")).isZero();
79 void should_flag_branch_with_no_measures() throws SQLException {
80 String branch = "branch_3";
81 insertNotMigratedBranch(branch);
85 assertBranchMigrated(branch);
86 assertThat(db.countRowsOfTable("measures")).isZero();
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");
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)));
103 String branch2 = "branch_5";
104 insertNotMigratedBranch(branch2);
105 insertMeasure(branch2, nclocMetricUuid, Map.of("value", 64));
107 String migratedBranch = "branch_6";
108 insertMigratedBranch(migratedBranch);
109 insertMeasure(migratedBranch, nclocMetricUuid, Map.of("value", 3684));
113 assertBranchMigrated(branch1);
114 assertBranchMigrated(branch2);
115 assertThat(db.countRowsOfTable("measures")).isEqualTo(3);
117 assertThat(db.select(format(SELECT_MEASURE, component1)))
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));
122 assertThat(db.select(format(SELECT_MEASURE, component2)))
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));
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));
132 .extracting(t -> t.get("MIGRATED"))
136 private String insertMetric(String metricName, String valueType) {
137 String metricUuid = uuidFactory.create();
138 db.executeInsert("metrics",
141 "val_type", valueType);
145 private void insertMeasure(String branchUuid, String metricUuid, Map<String, Object> data) {
146 insertMeasure(branchUuid, uuidFactory.create(), metricUuid, data);
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);
158 db.executeInsert("live_measures", dataMap);
161 private void insertNotMigratedBranch(String branchUuid) {
162 insertBranch(branchUuid, false);
165 private void insertMigratedBranch(String branchUuid) {
166 insertBranch(branchUuid, true);
169 private void insertBranch(String branchUuid, boolean migrated) {
170 db.executeInsert("project_branches",
173 "branch_type", "LONG",
174 "project_uuid", uuidFactory.create(),
175 MEASURES_MIGRATED_COLUMN, migrated,
176 "need_issue_sync", false,