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.v100;
22 import java.sql.SQLException;
23 import java.util.HashMap;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.RegisterExtension;
27 import org.sonar.core.util.UuidFactory;
28 import org.sonar.core.util.UuidFactoryFast;
29 import org.sonar.db.MigrationDbTester;
30 import org.sonar.server.platform.db.migration.step.DataChange;
32 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
33 import static org.assertj.core.api.Assertions.assertThat;
35 class PopulateNclocForForProjectsIT {
37 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
40 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(PopulateNclocForForProjects.class);
42 private final DataChange underTest = new PopulateNclocForForProjects(db.database());
45 void migration_populates_ncloc_for_projects() throws SQLException {
46 Map<String, Long> expectedNclocByProjectUuid = populateData();
48 verifyNclocCorrectlyPopulatedForProjects(expectedNclocByProjectUuid);
52 void migration_should_be_reentrant() throws SQLException {
53 Map<String, Long> expectedNclocByProjectUuid = populateData();
57 verifyNclocCorrectlyPopulatedForProjects(expectedNclocByProjectUuid);
60 private Map<String, Long> populateData() {
61 String nclocMetricUuid = insertMetric("ncloc");
63 String projectUuid1 = insertProject();
64 String project1Branch1 = insertProjectBranch(projectUuid1);
65 String project1Branch2 = insertProjectBranch(projectUuid1);
67 long project1maxNcloc = 100;
68 insertLiveMeasure(nclocMetricUuid, projectUuid1, project1Branch1, 80L);
69 insertLiveMeasure(nclocMetricUuid, projectUuid1, project1Branch2, project1maxNcloc);
71 String otherMetricUuid = insertMetric("other");
72 insertLiveMeasure(otherMetricUuid, projectUuid1, project1Branch1, 5000L);
73 insertLiveMeasure(otherMetricUuid, projectUuid1, project1Branch2, 6000L);
75 String projectUuid2 = insertProject();
76 String project2Branch1 = insertProjectBranch(projectUuid2);
77 String project2Branch2 = insertProjectBranch(projectUuid2);
78 String project2Branch3 = insertProjectBranch(projectUuid2);
80 long project2maxNcloc = 60;
81 insertLiveMeasure(nclocMetricUuid, projectUuid2, project2Branch1, 20L);
82 insertLiveMeasure(nclocMetricUuid, projectUuid2, project2Branch2, 50L);
83 insertLiveMeasure(nclocMetricUuid, projectUuid2, project2Branch3, project2maxNcloc);
85 return Map.of(projectUuid1, project1maxNcloc, projectUuid2, project2maxNcloc);
88 private void verifyNclocCorrectlyPopulatedForProjects(Map<String, Long> expectedNclocByProjectUuid) {
89 for (Map.Entry<String, Long> entry : expectedNclocByProjectUuid.entrySet()) {
90 String query = String.format("select ncloc from projects where uuid='%s'", entry.getKey());
91 Long nclocFromProject = (Long) db.selectFirst(query).get("NCLOC");
92 assertThat(nclocFromProject).isEqualTo(entry.getValue());
96 private String insertMetric(String name) {
97 Map<String, Object> map = new HashMap<>();
98 String uuid = uuidFactory.create();
99 map.put("UUID", uuid);
100 map.put("NAME", name);
101 db.executeInsert("metrics", map);
105 private String insertProject() {
106 Map<String, Object> map = new HashMap<>();
107 String uuid = uuidFactory.create();
108 map.put("UUID", uuid);
109 map.put("KEE", randomAlphabetic(20));
110 map.put("QUALIFIER", "TRK");
111 map.put("PRIVATE", true);
112 map.put("UPDATED_AT", System.currentTimeMillis());
113 db.executeInsert("projects", map);
117 private String insertProjectBranch(String projectUuid) {
118 Map<String, Object> map = new HashMap<>();
119 String uuid = uuidFactory.create();
120 map.put("UUID", uuid);
121 map.put("PROJECT_UUID", projectUuid);
122 map.put("KEE", randomAlphabetic(20));
123 map.put("BRANCH_TYPE", "PULL_REQUEST");
124 map.put("UPDATED_AT", System.currentTimeMillis());
125 map.put("CREATED_AT", System.currentTimeMillis());
126 map.put("NEED_ISSUE_SYNC", false);
127 db.executeInsert("project_branches", map);
131 private void insertLiveMeasure(String metricUuid, String projectUuid, String componentUuid, Long value) {
132 Map<String, Object> map = new HashMap<>();
133 String uuid = uuidFactory.create();
134 map.put("UUID", uuid);
135 map.put("PROJECT_UUID", projectUuid);
136 map.put("COMPONENT_UUID", componentUuid);
137 map.put("METRIC_UUID", metricUuid);
138 map.put("VALUE", value);
139 map.put("UPDATED_AT", System.currentTimeMillis());
140 map.put("CREATED_AT", System.currentTimeMillis());
141 db.executeInsert("live_measures", map);