3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.computation.step;
22 import com.google.common.base.Optional;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.utils.DateUtils;
29 import org.sonar.api.utils.System2;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.component.ComponentTesting;
34 import org.sonar.server.computation.batch.TreeRootHolderRule;
35 import org.sonar.server.computation.component.Component;
36 import org.sonar.server.computation.component.FileAttributes;
37 import org.sonar.server.computation.component.MutableDbIdsRepositoryRule;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.guava.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43 import static org.sonar.server.computation.component.Component.Type.DIRECTORY;
44 import static org.sonar.server.computation.component.Component.Type.FILE;
45 import static org.sonar.server.computation.component.Component.Type.PROJECT;
46 import static org.sonar.server.computation.component.ReportComponent.builder;
49 public class ReportPersistComponentsStepTest extends BaseStepTest {
51 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
53 private static final String PROJECT_KEY = "PROJECT_KEY";
56 public DbTester dbTester = DbTester.create(System2.INSTANCE);
58 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
60 public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
62 System2 system2 = mock(System2.class);
64 DbClient dbClient = dbTester.getDbClient();
68 PersistComponentsStep underTest;
71 public void setup() throws Exception {
72 now = DATE_FORMAT.parse("2015-06-02");
73 when(system2.now()).thenReturn(now.getTime());
75 underTest = new PersistComponentsStep(dbClient, treeRootHolder, dbIdsRepository, system2);
79 protected ComputationStep step() {
84 public void persist_components() {
85 Component file = builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
86 .setPath("src/main/java/dir/Foo.java")
87 .setFileAttributes(new FileAttributes(false, "java"))
89 Component directory = builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
90 .setPath("src/main/java/dir")
93 Component module = builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
96 .setDescription("Module description")
97 .addChildren(directory)
99 Component project = builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
101 .setDescription("Project description")
104 treeRootHolder.setRoot(project);
108 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
110 ComponentDto projectDto = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
111 assertThat(projectDto.name()).isEqualTo("Project");
112 assertThat(projectDto.description()).isEqualTo("Project description");
113 assertThat(projectDto.path()).isNull();
114 assertThat(projectDto.uuid()).isEqualTo("ABCD");
115 assertThat(projectDto.moduleUuid()).isNull();
116 assertThat(projectDto.moduleUuidPath()).isEqualTo("." + projectDto.uuid() + ".");
117 assertThat(projectDto.projectUuid()).isEqualTo(projectDto.uuid());
118 assertThat(projectDto.qualifier()).isEqualTo("TRK");
119 assertThat(projectDto.scope()).isEqualTo("PRJ");
120 assertThat(projectDto.parentProjectId()).isNull();
121 assertThat(projectDto.getCreatedAt()).isEqualTo(now);
123 ComponentDto moduleDto = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
124 assertThat(moduleDto.name()).isEqualTo("Module");
125 assertThat(moduleDto.description()).isEqualTo("Module description");
126 assertThat(moduleDto.path()).isEqualTo("module");
127 assertThat(moduleDto.uuid()).isEqualTo("BCDE");
128 assertThat(moduleDto.moduleUuid()).isEqualTo(projectDto.uuid());
129 assertThat(moduleDto.moduleUuidPath()).isEqualTo(projectDto.moduleUuidPath() + moduleDto.uuid() + ".");
130 assertThat(moduleDto.projectUuid()).isEqualTo(projectDto.uuid());
131 assertThat(moduleDto.qualifier()).isEqualTo("BRC");
132 assertThat(moduleDto.scope()).isEqualTo("PRJ");
133 assertThat(moduleDto.parentProjectId()).isEqualTo(projectDto.getId());
134 assertThat(moduleDto.getCreatedAt()).isEqualTo(now);
136 ComponentDto directoryDto = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir").get();
137 assertThat(directoryDto.name()).isEqualTo("src/main/java/dir");
138 assertThat(directoryDto.description()).isNull();
139 assertThat(directoryDto.path()).isEqualTo("src/main/java/dir");
140 assertThat(directoryDto.uuid()).isEqualTo("CDEF");
141 assertThat(directoryDto.moduleUuid()).isEqualTo(moduleDto.uuid());
142 assertThat(directoryDto.moduleUuidPath()).isEqualTo(moduleDto.moduleUuidPath());
143 assertThat(directoryDto.projectUuid()).isEqualTo(projectDto.uuid());
144 assertThat(directoryDto.qualifier()).isEqualTo("DIR");
145 assertThat(directoryDto.scope()).isEqualTo("DIR");
146 assertThat(directoryDto.parentProjectId()).isEqualTo(moduleDto.getId());
147 assertThat(directoryDto.getCreatedAt()).isEqualTo(now);
149 ComponentDto fileDto = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir/Foo.java").get();
150 assertThat(fileDto.name()).isEqualTo("Foo.java");
151 assertThat(fileDto.description()).isNull();
152 assertThat(fileDto.path()).isEqualTo("src/main/java/dir/Foo.java");
153 assertThat(fileDto.language()).isEqualTo("java");
154 assertThat(fileDto.uuid()).isEqualTo("DEFG");
155 assertThat(fileDto.moduleUuid()).isEqualTo(moduleDto.uuid());
156 assertThat(fileDto.moduleUuidPath()).isEqualTo(moduleDto.moduleUuidPath());
157 assertThat(fileDto.projectUuid()).isEqualTo(projectDto.uuid());
158 assertThat(fileDto.qualifier()).isEqualTo("FIL");
159 assertThat(fileDto.scope()).isEqualTo("FIL");
160 assertThat(fileDto.parentProjectId()).isEqualTo(moduleDto.getId());
161 assertThat(fileDto.getCreatedAt()).isEqualTo(now);
163 assertThat(dbIdsRepository.getComponentId(project)).isEqualTo(projectDto.getId());
164 assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
165 assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
166 assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
170 public void persist_file_directly_attached_on_root_directory() {
171 treeRootHolder.setRoot(
172 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setName("Project")
174 builder(DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":/").setPath("/")
176 builder(FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":pom.xml").setPath("pom.xml")
183 ComponentDto directory = dbClient.componentDao().selectByKey(dbTester.getSession(), "PROJECT_KEY:/").get();
184 assertThat(directory.name()).isEqualTo("/");
185 assertThat(directory.path()).isEqualTo("/");
187 ComponentDto file = dbClient.componentDao().selectByKey(dbTester.getSession(), "PROJECT_KEY:pom.xml").get();
188 assertThat(file.name()).isEqualTo("pom.xml");
189 assertThat(file.path()).isEqualTo("pom.xml");
193 public void persist_unit_test() {
194 treeRootHolder.setRoot(
195 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
198 builder(DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/test/java/dir")
199 .setPath("src/test/java/dir")
201 builder(FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/test/java/dir/FooTest.java")
202 .setPath("src/test/java/dir/FooTest.java")
203 .setFileAttributes(new FileAttributes(true, null))
210 ComponentDto file = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY + ":src/test/java/dir/FooTest.java").get();
211 assertThat(file.name()).isEqualTo("FooTest.java");
212 assertThat(file.path()).isEqualTo("src/test/java/dir/FooTest.java");
213 assertThat(file.qualifier()).isEqualTo("UTS");
214 assertThat(file.scope()).isEqualTo("FIL");
218 public void persist_only_new_components() {
219 // Project amd module already exists
220 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
221 dbClient.componentDao().insert(dbTester.getSession(), project);
222 ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module");
223 dbClient.componentDao().insert(dbTester.getSession(), module);
224 dbTester.getSession().commit();
226 treeRootHolder.setRoot(
227 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
230 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
233 builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
234 .setPath("src/main/java/dir")
236 builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
237 .setPath("src/main/java/dir/Foo.java")
245 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
247 ComponentDto projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
248 assertThat(projectReloaded.getId()).isEqualTo(project.getId());
249 assertThat(projectReloaded.uuid()).isEqualTo(project.uuid());
251 ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
252 assertThat(moduleReloaded.getId()).isEqualTo(module.getId());
253 assertThat(moduleReloaded.uuid()).isEqualTo(module.uuid());
254 assertThat(moduleReloaded.moduleUuid()).isEqualTo(module.moduleUuid());
255 assertThat(moduleReloaded.moduleUuidPath()).isEqualTo(module.moduleUuidPath());
256 assertThat(moduleReloaded.projectUuid()).isEqualTo(module.projectUuid());
257 assertThat(moduleReloaded.parentProjectId()).isEqualTo(module.parentProjectId());
259 ComponentDto directory = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir").get();
260 assertThat(directory.moduleUuid()).isEqualTo(module.uuid());
261 assertThat(directory.moduleUuidPath()).isEqualTo(module.moduleUuidPath());
262 assertThat(directory.projectUuid()).isEqualTo(project.uuid());
263 assertThat(directory.parentProjectId()).isEqualTo(module.getId());
265 ComponentDto file = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir/Foo.java").get();
266 assertThat(file.moduleUuid()).isEqualTo(module.uuid());
267 assertThat(file.moduleUuidPath()).isEqualTo(module.moduleUuidPath());
268 assertThat(file.projectUuid()).isEqualTo(project.uuid());
269 assertThat(file.parentProjectId()).isEqualTo(module.getId());
273 public void compute_parent_project_id() {
274 treeRootHolder.setRoot(
275 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
278 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
281 builder(Component.Type.MODULE, 3).setUuid("CDEF").setKey("SUB_MODULE_1_KEY")
282 .setName("Sub Module 1")
284 builder(Component.Type.MODULE, 4).setUuid("DEFG").setKey("SUB_MODULE_2_KEY")
285 .setName("Sub Module 2")
287 builder(DIRECTORY, 5).setUuid("EFGH").setKey("SUB_MODULE_2_KEY:src/main/java/dir")
288 .setPath("src/main/java/dir")
297 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(5);
299 Optional<ComponentDto> project = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY);
300 assertThat(project).isPresent();
301 assertThat(project.get().parentProjectId()).isNull();
303 Optional<ComponentDto> module = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY");
304 assertThat(module).isPresent();
305 assertThat(module.get().parentProjectId()).isEqualTo(project.get().getId());
307 Optional<ComponentDto> subModule1 = dbClient.componentDao().selectByKey(dbTester.getSession(), "SUB_MODULE_1_KEY");
308 assertThat(subModule1).isPresent();
309 assertThat(subModule1.get().parentProjectId()).isEqualTo(project.get().getId());
311 Optional<ComponentDto> subModule2 = dbClient.componentDao().selectByKey(dbTester.getSession(), "SUB_MODULE_2_KEY");
312 assertThat(subModule2).isPresent();
313 assertThat(subModule2.get().parentProjectId()).isEqualTo(project.get().getId());
315 Optional<ComponentDto> directory = dbClient.componentDao().selectByKey(dbTester.getSession(), "SUB_MODULE_2_KEY:src/main/java/dir");
316 assertThat(directory).isPresent();
317 assertThat(directory.get().parentProjectId()).isEqualTo(subModule2.get().getId());
321 public void persist_multi_modules() {
322 treeRootHolder.setRoot(
323 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
326 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_A")
329 builder(Component.Type.MODULE, 3).setUuid("DEFG").setKey("SUB_MODULE_A")
330 .setName("Sub Module A")
333 builder(Component.Type.MODULE, 4).setUuid("CDEF").setKey("MODULE_B")
340 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
342 ComponentDto project = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
343 assertThat(project.moduleUuid()).isNull();
344 assertThat(project.moduleUuidPath()).isEqualTo("." + project.uuid() + ".");
345 assertThat(project.parentProjectId()).isNull();
347 ComponentDto moduleA = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_A").get();
348 assertThat(moduleA.moduleUuid()).isEqualTo(project.uuid());
349 assertThat(moduleA.moduleUuidPath()).isEqualTo(project.moduleUuidPath() + moduleA.uuid() + ".");
350 assertThat(moduleA.parentProjectId()).isEqualTo(project.getId());
352 ComponentDto subModuleA = dbClient.componentDao().selectByKey(dbTester.getSession(), "SUB_MODULE_A").get();
353 assertThat(subModuleA.moduleUuid()).isEqualTo(moduleA.uuid());
354 assertThat(subModuleA.moduleUuidPath()).isEqualTo(moduleA.moduleUuidPath() + subModuleA.uuid() + ".");
355 assertThat(subModuleA.parentProjectId()).isEqualTo(project.getId());
357 ComponentDto moduleB = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_B").get();
358 assertThat(moduleB.moduleUuid()).isEqualTo(project.uuid());
359 assertThat(moduleB.moduleUuidPath()).isEqualTo(project.moduleUuidPath() + moduleB.uuid() + ".");
360 assertThat(moduleB.parentProjectId()).isEqualTo(project.getId());
364 public void nothing_to_persist() {
365 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
366 dbClient.componentDao().insert(dbTester.getSession(), project);
367 ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module");
368 dbClient.componentDao().insert(dbTester.getSession(), module);
369 ComponentDto directory = ComponentTesting.newDirectory(module, "src/main/java/dir").setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir");
370 ComponentDto file = ComponentTesting.newFileDto(module, "DEFG").setPath("src/main/java/dir/Foo.java").setName("Foo.java").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
371 dbClient.componentDao().insert(dbTester.getSession(), directory, file);
372 dbTester.getSession().commit();
374 treeRootHolder.setRoot(
375 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
378 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
381 builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
382 .setPath("src/main/java/dir")
384 builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
385 .setPath("src/main/java/dir/Foo.java")
393 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
394 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get().getId()).isEqualTo(project.getId());
395 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get().getId()).isEqualTo(module.getId());
396 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir").get().getId()).isEqualTo(directory.getId());
397 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir/Foo.java").get().getId()).isEqualTo(file.getId());
399 ComponentDto projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
400 assertThat(projectReloaded.getId()).isEqualTo(project.getId());
401 assertThat(projectReloaded.uuid()).isEqualTo(project.uuid());
402 assertThat(projectReloaded.moduleUuid()).isEqualTo(project.moduleUuid());
403 assertThat(projectReloaded.moduleUuidPath()).isEqualTo(project.moduleUuidPath());
404 assertThat(projectReloaded.projectUuid()).isEqualTo(project.projectUuid());
405 assertThat(projectReloaded.parentProjectId()).isEqualTo(project.parentProjectId());
407 ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
408 assertThat(moduleReloaded.getId()).isEqualTo(module.getId());
409 assertThat(moduleReloaded.uuid()).isEqualTo(module.uuid());
410 assertThat(moduleReloaded.moduleUuid()).isEqualTo(module.moduleUuid());
411 assertThat(moduleReloaded.moduleUuidPath()).isEqualTo(module.moduleUuidPath());
412 assertThat(moduleReloaded.projectUuid()).isEqualTo(module.projectUuid());
413 assertThat(moduleReloaded.parentProjectId()).isEqualTo(module.parentProjectId());
415 ComponentDto directoryReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir").get();
416 assertThat(directoryReloaded.uuid()).isEqualTo(directory.uuid());
417 assertThat(directoryReloaded.moduleUuid()).isEqualTo(directory.moduleUuid());
418 assertThat(directoryReloaded.moduleUuidPath()).isEqualTo(directory.moduleUuidPath());
419 assertThat(directoryReloaded.projectUuid()).isEqualTo(directory.projectUuid());
420 assertThat(directoryReloaded.parentProjectId()).isEqualTo(directory.parentProjectId());
421 assertThat(directoryReloaded.name()).isEqualTo(directory.name());
422 assertThat(directoryReloaded.path()).isEqualTo(directory.path());
424 ComponentDto fileReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir/Foo.java").get();
425 assertThat(fileReloaded.uuid()).isEqualTo(file.uuid());
426 assertThat(fileReloaded.moduleUuid()).isEqualTo(file.moduleUuid());
427 assertThat(fileReloaded.moduleUuidPath()).isEqualTo(file.moduleUuidPath());
428 assertThat(fileReloaded.projectUuid()).isEqualTo(file.projectUuid());
429 assertThat(fileReloaded.parentProjectId()).isEqualTo(file.parentProjectId());
430 assertThat(fileReloaded.name()).isEqualTo(file.name());
431 assertThat(fileReloaded.path()).isEqualTo(file.path());
435 public void update_module_name() {
436 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
437 dbClient.componentDao().insert(dbTester.getSession(), project);
438 ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module").setPath("path");
439 dbClient.componentDao().insert(dbTester.getSession(), module);
440 dbTester.getSession().commit();
442 treeRootHolder.setRoot(
443 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
444 .setName("New project name")
446 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
447 .setName("New module name")
454 ComponentDto projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
455 assertThat(projectReloaded.name()).isEqualTo("New project name");
457 ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
458 assertThat(moduleReloaded.name()).isEqualTo("New module name");
462 public void update_module_description() {
463 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project").setDescription("Project description");
464 dbClient.componentDao().insert(dbTester.getSession(), project);
465 ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module");
466 dbClient.componentDao().insert(dbTester.getSession(), module);
467 dbTester.getSession().commit();
469 treeRootHolder.setRoot(
470 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
472 .setDescription("New project description")
474 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
476 .setDescription("New module description")
482 ComponentDto projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
483 assertThat(projectReloaded.description()).isEqualTo("New project description");
485 ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
486 assertThat(moduleReloaded.description()).isEqualTo("New module description");
490 public void update_module_path() {
491 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
492 dbClient.componentDao().insert(dbTester.getSession(), project);
493 ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module").setPath("path");
494 dbClient.componentDao().insert(dbTester.getSession(), module);
495 dbTester.getSession().commit();
497 treeRootHolder.setRoot(
498 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
501 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
509 ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
510 assertThat(moduleReloaded.path()).isEqualTo("New path");
514 public void update_module_uuid_when_moving_a_module() {
515 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
516 dbClient.componentDao().insert(dbTester.getSession(), project);
517 ComponentDto moduleA = ComponentTesting.newModuleDto("EDCB", project).setKey("MODULE_A").setName("Module A");
518 ComponentDto moduleB = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_B").setName("Module B");
519 dbClient.componentDao().insert(dbTester.getSession(), moduleA, moduleB);
520 ComponentDto directory = ComponentTesting.newDirectory(moduleB, "src/main/java/dir").setUuid("CDEF").setKey("MODULE_B:src/main/java/dir");
521 ComponentDto file = ComponentTesting.newFileDto(moduleB, "DEFG").setPath("src/main/java/dir/Foo.java").setName("Foo.java").setKey("MODULE_B:src/main/java/dir/Foo.java");
522 dbClient.componentDao().insert(dbTester.getSession(), directory, file);
523 dbTester.getSession().commit();
525 treeRootHolder.setRoot(
526 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
529 builder(Component.Type.MODULE, 2).setUuid("EDCB").setKey("MODULE_A")
532 builder(Component.Type.MODULE, 3).setUuid("BCDE").setKey("MODULE_B")
535 builder(DIRECTORY, 4).setUuid("CDEF").setKey("MODULE_B:src/main/java/dir")
536 .setPath("src/main/java/dir")
538 builder(FILE, 5).setUuid("DEFG").setKey("MODULE_B:src/main/java/dir/Foo.java")
539 .setPath("src/main/java/dir/Foo.java")
548 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(5);
550 ComponentDto moduleAreloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_A").get();
552 ComponentDto moduleBReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_B").get();
553 assertThat(moduleBReloaded).isNotNull();
554 assertThat(moduleBReloaded.uuid()).isEqualTo(moduleB.uuid());
555 assertThat(moduleBReloaded.moduleUuid()).isEqualTo(moduleAreloaded.uuid());
556 assertThat(moduleBReloaded.moduleUuidPath()).isEqualTo(moduleAreloaded.moduleUuidPath() + moduleBReloaded.uuid() + ".");
557 assertThat(moduleBReloaded.projectUuid()).isEqualTo(project.uuid());
558 assertThat(moduleBReloaded.parentProjectId()).isEqualTo(project.getId());
560 ComponentDto directoryReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_B:src/main/java/dir").get();
561 assertThat(directoryReloaded).isNotNull();
562 assertThat(directoryReloaded.uuid()).isEqualTo(directory.uuid());
563 assertThat(directoryReloaded.moduleUuid()).isEqualTo(moduleBReloaded.uuid());
564 assertThat(directoryReloaded.moduleUuidPath()).isEqualTo(moduleBReloaded.moduleUuidPath());
565 assertThat(directoryReloaded.projectUuid()).isEqualTo(project.uuid());
566 assertThat(directoryReloaded.parentProjectId()).isEqualTo(moduleBReloaded.getId());
568 ComponentDto fileReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_B:src/main/java/dir/Foo.java").get();
569 assertThat(fileReloaded).isNotNull();
570 assertThat(fileReloaded.uuid()).isEqualTo(file.uuid());
571 assertThat(fileReloaded.moduleUuid()).isEqualTo(moduleBReloaded.uuid());
572 assertThat(fileReloaded.moduleUuidPath()).isEqualTo(moduleBReloaded.moduleUuidPath());
573 assertThat(fileReloaded.projectUuid()).isEqualTo(project.uuid());
574 assertThat(fileReloaded.parentProjectId()).isEqualTo(moduleBReloaded.getId());
578 public void do_not_update_created_at_on_existing_component() {
579 Date oldDate = DateUtils.parseDate("2015-01-01");
580 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project").setCreatedAt(oldDate);
581 dbClient.componentDao().insert(dbTester.getSession(), project);
582 ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module").setPath("path").setCreatedAt(oldDate);
583 dbClient.componentDao().insert(dbTester.getSession(), module);
584 dbTester.getSession().commit();
586 treeRootHolder.setRoot(
587 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
588 .setName("New project name")
593 Optional<ComponentDto> projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY);
594 assertThat(projectReloaded.get().name()).isEqualTo("New project name");
595 assertThat(projectReloaded.get().getCreatedAt()).isNotEqualTo(now);
599 public void persist_components_that_were_previously_removed() {
600 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
601 dbClient.componentDao().insert(dbTester.getSession(), project);
602 ComponentDto removedModule = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module").setEnabled(false);
603 dbClient.componentDao().insert(dbTester.getSession(), removedModule);
604 ComponentDto removedDirectory = ComponentTesting.newDirectory(removedModule, "src/main/java/dir").setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").setEnabled(false);
605 ComponentDto removedFile = ComponentTesting.newFileDto(removedModule, "DEFG").setPath("src/main/java/dir/Foo.java").setName("Foo.java")
606 .setKey("MODULE_KEY:src/main/java/dir/Foo.java").setEnabled(false);
607 dbClient.componentDao().insert(dbTester.getSession(), removedDirectory, removedFile);
608 dbTester.getSession().commit();
610 treeRootHolder.setRoot(
611 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
614 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
617 builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
618 .setPath("src/main/java/dir")
620 builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
621 .setPath("src/main/java/dir/Foo.java")
629 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
630 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get().getId()).isEqualTo(project.getId());
631 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get().getId()).isEqualTo(removedModule.getId());
632 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir").get().getId()).isEqualTo(removedDirectory.getId());
633 assertThat(dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir/Foo.java").get().getId()).isEqualTo(removedFile.getId());
635 ComponentDto projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
636 assertThat(projectReloaded.getId()).isEqualTo(project.getId());
637 assertThat(projectReloaded.uuid()).isEqualTo(project.uuid());
638 assertThat(projectReloaded.moduleUuid()).isEqualTo(project.moduleUuid());
639 assertThat(projectReloaded.moduleUuidPath()).isEqualTo(project.moduleUuidPath());
640 assertThat(projectReloaded.projectUuid()).isEqualTo(project.projectUuid());
641 assertThat(projectReloaded.parentProjectId()).isEqualTo(project.parentProjectId());
642 assertThat(projectReloaded.isEnabled()).isTrue();
644 ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
645 assertThat(moduleReloaded.getId()).isEqualTo(removedModule.getId());
646 assertThat(moduleReloaded.uuid()).isEqualTo(removedModule.uuid());
647 assertThat(moduleReloaded.moduleUuid()).isEqualTo(removedModule.moduleUuid());
648 assertThat(moduleReloaded.moduleUuidPath()).isEqualTo(removedModule.moduleUuidPath());
649 assertThat(moduleReloaded.projectUuid()).isEqualTo(removedModule.projectUuid());
650 assertThat(moduleReloaded.parentProjectId()).isEqualTo(removedModule.parentProjectId());
651 assertThat(moduleReloaded.isEnabled()).isTrue();
653 ComponentDto directoryReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir").get();
654 assertThat(directoryReloaded.getId()).isEqualTo(removedDirectory.getId());
655 assertThat(directoryReloaded.uuid()).isEqualTo(removedDirectory.uuid());
656 assertThat(directoryReloaded.moduleUuid()).isEqualTo(removedDirectory.moduleUuid());
657 assertThat(directoryReloaded.moduleUuidPath()).isEqualTo(removedDirectory.moduleUuidPath());
658 assertThat(directoryReloaded.projectUuid()).isEqualTo(removedDirectory.projectUuid());
659 assertThat(directoryReloaded.parentProjectId()).isEqualTo(removedDirectory.parentProjectId());
660 assertThat(directoryReloaded.name()).isEqualTo(removedDirectory.name());
661 assertThat(directoryReloaded.path()).isEqualTo(removedDirectory.path());
662 assertThat(directoryReloaded.isEnabled()).isTrue();
664 ComponentDto fileReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir/Foo.java").get();
665 assertThat(fileReloaded.getId()).isEqualTo(fileReloaded.getId());
666 assertThat(fileReloaded.uuid()).isEqualTo(removedFile.uuid());
667 assertThat(fileReloaded.moduleUuid()).isEqualTo(removedFile.moduleUuid());
668 assertThat(fileReloaded.moduleUuidPath()).isEqualTo(removedFile.moduleUuidPath());
669 assertThat(fileReloaded.projectUuid()).isEqualTo(removedFile.projectUuid());
670 assertThat(fileReloaded.parentProjectId()).isEqualTo(removedFile.parentProjectId());
671 assertThat(fileReloaded.name()).isEqualTo(removedFile.name());
672 assertThat(fileReloaded.path()).isEqualTo(removedFile.path());
673 assertThat(fileReloaded.isEnabled()).isTrue();
677 public void update_uuid_when_reactivating_removed_component() {
678 ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
679 dbClient.componentDao().insert(dbTester.getSession(), project);
680 ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey("MODULE_KEY").setName("Module");
681 ComponentDto removedModule = ComponentTesting.newModuleDto("EDCD", project).setKey("REMOVED_MODULE_KEY").setName("Removed Module").setEnabled(false);
682 dbClient.componentDao().insert(dbTester.getSession(), module, removedModule);
683 ComponentDto directory = ComponentTesting.newDirectory(module, "src/main/java/dir").setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir");
684 // The file was attached to another module
685 ComponentDto removedFile = ComponentTesting.newFileDto(removedModule, "DEFG").setPath("src/main/java/dir/Foo.java").setName("Foo.java")
686 .setKey("MODULE_KEY:src/main/java/dir/Foo.java").setEnabled(false);
687 dbClient.componentDao().insert(dbTester.getSession(), directory, removedFile);
688 dbTester.getSession().commit();
690 treeRootHolder.setRoot(
691 builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
694 builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
697 builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
698 .setPath("src/main/java/dir")
700 builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
701 .setPath("src/main/java/dir/Foo.java")
709 // Projects contains 4 components from the report + one removed module
710 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(5);
712 ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
714 ComponentDto fileReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY:src/main/java/dir/Foo.java").get();
715 assertThat(fileReloaded.getId()).isEqualTo(removedFile.getId());
716 assertThat(fileReloaded.uuid()).isEqualTo(removedFile.uuid());
717 assertThat(fileReloaded.moduleUuid()).isEqualTo(moduleReloaded.uuid());
718 assertThat(fileReloaded.moduleUuidPath()).isEqualTo(moduleReloaded.moduleUuidPath());
719 assertThat(fileReloaded.projectUuid()).isEqualTo(moduleReloaded.projectUuid());
720 assertThat(fileReloaded.parentProjectId()).isEqualTo(moduleReloaded.getId());
721 assertThat(fileReloaded.name()).isEqualTo(removedFile.name());
722 assertThat(fileReloaded.path()).isEqualTo(removedFile.path());
723 assertThat(fileReloaded.isEnabled()).isTrue();