3 * Copyright (C) 2009-2022 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.ce.task.projectanalysis.component;
22 import java.util.Collections;
23 import java.util.HashMap;
25 import java.util.function.Function;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.utils.System2;
29 import org.sonar.ce.task.projectanalysis.analysis.Branch;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ComponentTesting;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME;
37 public class ComponentUuidFactoryWithMigrationTest {
38 private final Branch mainBranch = new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
40 public DbTester db = DbTester.create(System2.INSTANCE);
41 private Function<String, String> pathToKey = path -> path != null ? "project:" + path : "project";
44 public void load_uuids_from_existing_components_in_db() {
45 ComponentDto project = db.components().insertPrivateProject();
46 ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project));
47 Map<String, String> reportModulesPath = Collections.singletonMap(module.getKey(), "module1_path");
48 pathToKey = path -> path != null ? project.getKey() + ":" + path : project.getKey();
50 ComponentUuidFactoryWithMigration underTest =
51 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, reportModulesPath);
53 assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
54 assertThat(underTest.getOrCreateForKey(module.getKey())).isEqualTo(module.uuid());
58 public void migrate_project_with_modules() {
59 ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
60 ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
61 .setKey("project:module1"));
62 ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(module1)
63 .setKey("project:module1:module2"));
64 ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
65 .setKey("project:file1")
66 .setPath("file1_path"));
67 ComponentDto file2 = db.components().insertComponent(ComponentTesting.newFileDto(module2)
68 .setKey("project:module1:module2:file2")
69 .setPath("file2_path"));
71 assertThat(file2.moduleUuidPath()).isEqualTo("." + project.uuid() + "." + module1.uuid() + "." + module2.uuid() + ".");
72 Map<String, String> modulesRelativePaths = new HashMap<>();
73 modulesRelativePaths.put("project:module1", "module1_path");
74 modulesRelativePaths.put("project:module1:module2", "module1_path/module2_path");
75 ComponentUuidFactoryWithMigration underTest =
76 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
79 assertThat(underTest.getOrCreateForKey("project:file1_path")).isEqualTo(file1.uuid());
80 assertThat(underTest.getOrCreateForKey("project:module1_path/module2_path/file2_path")).isEqualTo(file2.uuid());
82 // project remains the same
83 assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
87 public void migrate_project_with_disabled_components_no_path() {
88 ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
89 ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
90 .setKey("project:module1"));
91 ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
92 .setKey("project:file1")
94 ComponentDto disabledFileNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project)
95 .setKey("project:file2")
99 Map<String, String> modulesRelativePaths = new HashMap<>();
100 modulesRelativePaths.put("project:module1", "module1_path");
101 ComponentUuidFactoryWithMigration underTest =
102 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
105 assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
107 // project remains the same
108 assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
112 public void migrate_project_with_disabled_components_same_path() {
113 ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
114 ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
115 .setKey("project:module1"));
116 ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
117 .setKey("project:file1")
119 ComponentDto disabledFileSamePath = db.components().insertComponent(ComponentTesting.newFileDto(project)
120 .setKey("project:file2")
124 Map<String, String> modulesRelativePaths = new HashMap<>();
125 modulesRelativePaths.put("project:module1", "module1_path");
126 ComponentUuidFactoryWithMigration underTest =
127 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
130 assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
132 // project remains the same
133 assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
137 public void prefers_component_having_same_key() {
138 ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
139 ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
140 .setKey("project:module1"));
141 ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(module1)
142 .setKey("project:module1:file1")
144 ComponentDto disabledFileSameKey = db.components().insertComponent(ComponentTesting.newFileDto(project)
145 .setKey("project:module1/file1")
146 .setPath("module1_path/file1")
149 Map<String, String> modulesRelativePaths = new HashMap<>();
150 modulesRelativePaths.put("project:module1", "module1_path");
151 ComponentUuidFactoryWithMigration underTest =
152 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
154 // in theory we should migrate file1. But since disabledFileSameKey already have the expected migrated key, let's reuse it.
155 assertThat(underTest.getOrCreateForKey("project:module1/file1")).isEqualTo(disabledFileSameKey.uuid());
157 // project remains the same
158 assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
162 public void migrate_branch_with_modules() {
163 pathToKey = path -> path != null ? "project:" + path : "project";
164 ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
165 ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
166 .setKey("project:module1"));
167 ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(module1)
168 .setKey("project:module1:module2"));
169 ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
170 .setKey("project:file1")
171 .setPath("file1_path"));
172 ComponentDto file2 = db.components().insertComponent(ComponentTesting.newFileDto(module2)
173 .setKey("project:module1:module2:file2")
174 .setPath("file2_path"));
176 assertThat(file2.moduleUuidPath()).isEqualTo("." + project.uuid() + "." + module1.uuid() + "." + module2.uuid() + ".");
177 Map<String, String> modulesRelativePaths = new HashMap<>();
178 modulesRelativePaths.put("project:module1", "module1_path");
179 modulesRelativePaths.put("project:module1:module2", "module1_path/module2_path");
180 ComponentUuidFactoryWithMigration underTest =
181 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
184 assertThat(underTest.getOrCreateForKey("project:file1_path")).isEqualTo(file1.uuid());
185 assertThat(underTest.getOrCreateForKey("project:module1_path/module2_path/file2_path")).isEqualTo(file2.uuid());
187 // project remains the same
188 assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
192 public void migrate_project_with_root_folders() {
193 ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
194 ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
195 .setKey("project:module1"));
196 ComponentDto dir1 = db.components().insertComponent(ComponentTesting.newDirectory(module1, "/")
197 .setKey("project:module1:/"));
199 Map<String, String> modulesRelativePaths = Collections.singletonMap("project:module1", "module1_path");
200 ComponentUuidFactoryWithMigration underTest =
201 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
203 // project remains the same
204 assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
206 // module migrated to folder
207 assertThat(underTest.getOrCreateForKey("project:module1_path")).isEqualTo(module1.uuid());
211 public void dont_override_root_uuid_if_module_path_is_not_sent() {
212 ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
213 ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
214 .setKey("project:module1")
216 ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
217 .setKey("project:module2")
219 Map<String, String> modulesRelativePaths = new HashMap<>();
220 modulesRelativePaths.put("project", "");
221 modulesRelativePaths.put("project:module2", "module2");
222 ComponentUuidFactoryWithMigration underTest =
223 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
225 // check root project.
226 assertThat(underTest.getOrCreateForKey("project")).isEqualTo(project.uuid());
230 public void generate_uuid_if_it_does_not_exist_in_db() {
231 ComponentUuidFactoryWithMigration underTest =
232 new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), "theProjectKey", mainBranch, pathToKey, Collections.emptyMap());
234 String generatedKey = underTest.getOrCreateForKey("foo");
235 assertThat(generatedKey).isNotEmpty();
237 // uuid is kept in memory for further calls with same key
238 assertThat(underTest.getOrCreateForKey("foo")).isEqualTo(generatedKey);