]> source.dussan.org Git - sonarqube.git/blob
c2ff3c08ab73090e2a48f747ab56117d32ff2f3b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.ce.task.projectanalysis.component;
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
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;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME;
36
37 public class ComponentUuidFactoryWithMigrationTest {
38   private final Branch mainBranch = new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
39   @Rule
40   public DbTester db = DbTester.create(System2.INSTANCE);
41   private Function<String, String> pathToKey = path -> path != null ? "project:" + path : "project";
42
43   @Test
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();
49
50     ComponentUuidFactoryWithMigration underTest =
51       new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, reportModulesPath);
52
53     assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
54     assertThat(underTest.getOrCreateForKey(module.getKey())).isEqualTo(module.uuid());
55   }
56
57   @Test
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"));
70
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);
77
78     // migrated files
79     assertThat(underTest.getOrCreateForKey("project:file1_path")).isEqualTo(file1.uuid());
80     assertThat(underTest.getOrCreateForKey("project:module1_path/module2_path/file2_path")).isEqualTo(file2.uuid());
81
82     // project remains the same
83     assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
84   }
85
86   @Test
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")
93       .setPath("file1"));
94     ComponentDto disabledFileNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project)
95       .setKey("project:file2")
96       .setPath(null)
97       .setEnabled(false));
98
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);
103
104     // migrated files
105     assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
106
107     // project remains the same
108     assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
109   }
110
111   @Test
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")
118       .setPath("file1"));
119     ComponentDto disabledFileSamePath = db.components().insertComponent(ComponentTesting.newFileDto(project)
120       .setKey("project:file2")
121       .setPath("file1")
122       .setEnabled(false));
123
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);
128
129     // migrated files
130     assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
131
132     // project remains the same
133     assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
134   }
135
136   @Test
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")
143       .setPath("file1"));
144     ComponentDto disabledFileSameKey = db.components().insertComponent(ComponentTesting.newFileDto(project)
145       .setKey("project:module1/file1")
146       .setPath("module1_path/file1")
147       .setEnabled(false));
148
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);
153
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());
156
157     // project remains the same
158     assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
159   }
160
161   @Test
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"));
175
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);
182
183     // migrated files
184     assertThat(underTest.getOrCreateForKey("project:file1_path")).isEqualTo(file1.uuid());
185     assertThat(underTest.getOrCreateForKey("project:module1_path/module2_path/file2_path")).isEqualTo(file2.uuid());
186
187     // project remains the same
188     assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
189   }
190
191   @Test
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:/"));
198
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);
202
203     // project remains the same
204     assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
205
206     // module migrated to folder
207     assertThat(underTest.getOrCreateForKey("project:module1_path")).isEqualTo(module1.uuid());
208   }
209
210   @Test
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")
215       .setEnabled(false));
216     ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
217       .setKey("project:module2")
218       .setEnabled(false));
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);
224
225     // check root project.
226     assertThat(underTest.getOrCreateForKey("project")).isEqualTo(project.uuid());
227   }
228
229   @Test
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());
233
234     String generatedKey = underTest.getOrCreateForKey("foo");
235     assertThat(generatedKey).isNotEmpty();
236
237     // uuid is kept in memory for further calls with same key
238     assertThat(underTest.getOrCreateForKey("foo")).isEqualTo(generatedKey);
239   }
240
241 }