]> source.dussan.org Git - sonarqube.git/blob
3977c052c7c8a324cb3f0898fc0c719ee0afef96
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.step;
21
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;
38
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;
47
48
49 public class ReportPersistComponentsStepTest extends BaseStepTest {
50
51   private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
52
53   private static final String PROJECT_KEY = "PROJECT_KEY";
54
55   @Rule
56   public DbTester dbTester = DbTester.create(System2.INSTANCE);
57   @Rule
58   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
59   @Rule
60   public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
61
62   System2 system2 = mock(System2.class);
63
64   DbClient dbClient = dbTester.getDbClient();
65
66   Date now;
67
68   PersistComponentsStep underTest;
69
70   @Before
71   public void setup() throws Exception {
72     now = DATE_FORMAT.parse("2015-06-02");
73     when(system2.now()).thenReturn(now.getTime());
74
75     underTest = new PersistComponentsStep(dbClient, treeRootHolder, dbIdsRepository, system2);
76   }
77
78   @Override
79   protected ComputationStep step() {
80     return underTest;
81   }
82
83   @Test
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"))
88       .build();
89     Component directory = builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
90       .setPath("src/main/java/dir")
91       .addChildren(file)
92       .build();
93     Component module = builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
94       .setPath("module")
95       .setName("Module")
96       .setDescription("Module description")
97       .addChildren(directory)
98       .build();
99     Component project = builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
100       .setName("Project")
101       .setDescription("Project description")
102       .addChildren(module)
103       .build();
104     treeRootHolder.setRoot(project);
105
106     underTest.execute();
107
108     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
109
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);
122
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);
135
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);
148
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);
162
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());
167   }
168
169   @Test
170   public void persist_file_directly_attached_on_root_directory() {
171     treeRootHolder.setRoot(
172       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setName("Project")
173         .addChildren(
174           builder(DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":/").setPath("/")
175             .addChildren(
176               builder(FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":pom.xml").setPath("pom.xml")
177                 .build())
178             .build())
179         .build());
180
181     underTest.execute();
182
183     ComponentDto directory = dbClient.componentDao().selectByKey(dbTester.getSession(), "PROJECT_KEY:/").get();
184     assertThat(directory.name()).isEqualTo("/");
185     assertThat(directory.path()).isEqualTo("/");
186
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");
190   }
191
192   @Test
193   public void persist_unit_test() {
194     treeRootHolder.setRoot(
195       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
196         .setName("Project")
197         .addChildren(
198           builder(DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/test/java/dir")
199             .setPath("src/test/java/dir")
200             .addChildren(
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))
204                 .build())
205             .build())
206         .build());
207
208     underTest.execute();
209
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");
215   }
216
217   @Test
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();
225
226     treeRootHolder.setRoot(
227       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
228         .setName("Project")
229         .addChildren(
230           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
231             .setName("Module")
232             .addChildren(
233               builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
234                 .setPath("src/main/java/dir")
235                 .addChildren(
236                   builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
237                     .setPath("src/main/java/dir/Foo.java")
238                     .build())
239                 .build())
240             .build())
241         .build());
242
243     underTest.execute();
244
245     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
246
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());
250
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());
258
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());
264
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());
270   }
271
272   @Test
273   public void compute_parent_project_id() {
274     treeRootHolder.setRoot(
275       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
276         .setName("Project")
277         .addChildren(
278           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
279             .setName("Module")
280             .addChildren(
281               builder(Component.Type.MODULE, 3).setUuid("CDEF").setKey("SUB_MODULE_1_KEY")
282                 .setName("Sub Module 1")
283                 .addChildren(
284                   builder(Component.Type.MODULE, 4).setUuid("DEFG").setKey("SUB_MODULE_2_KEY")
285                     .setName("Sub Module 2")
286                     .addChildren(
287                       builder(DIRECTORY, 5).setUuid("EFGH").setKey("SUB_MODULE_2_KEY:src/main/java/dir")
288                         .setPath("src/main/java/dir")
289                         .build())
290                     .build())
291                 .build())
292             .build())
293         .build());
294
295     underTest.execute();
296
297     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(5);
298
299     Optional<ComponentDto> project = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY);
300     assertThat(project).isPresent();
301     assertThat(project.get().parentProjectId()).isNull();
302
303     Optional<ComponentDto> module = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY");
304     assertThat(module).isPresent();
305     assertThat(module.get().parentProjectId()).isEqualTo(project.get().getId());
306
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());
310
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());
314
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());
318   }
319
320   @Test
321   public void persist_multi_modules() {
322     treeRootHolder.setRoot(
323       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
324         .setName("Project")
325         .addChildren(
326           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_A")
327             .setName("Module A")
328             .addChildren(
329               builder(Component.Type.MODULE, 3).setUuid("DEFG").setKey("SUB_MODULE_A")
330                 .setName("Sub Module A")
331                 .build())
332             .build(),
333           builder(Component.Type.MODULE, 4).setUuid("CDEF").setKey("MODULE_B")
334             .setName("Module B")
335             .build())
336         .build());
337
338     underTest.execute();
339
340     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(4);
341
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();
346
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());
351
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());
356
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());
361   }
362
363   @Test
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();
373
374     treeRootHolder.setRoot(
375       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
376         .setName("Project")
377         .addChildren(
378           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
379             .setName("Module")
380             .addChildren(
381               builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
382                 .setPath("src/main/java/dir")
383                 .addChildren(
384                   builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
385                     .setPath("src/main/java/dir/Foo.java")
386                     .build())
387                 .build())
388             .build())
389         .build());
390
391     underTest.execute();
392
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());
398
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());
406
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());
414
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());
423
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());
432   }
433
434   @Test
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();
441
442     treeRootHolder.setRoot(
443       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
444         .setName("New project name")
445         .addChildren(
446           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
447             .setName("New module name")
448             .setPath("New path")
449             .build())
450         .build());
451
452     underTest.execute();
453
454     ComponentDto projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
455     assertThat(projectReloaded.name()).isEqualTo("New project name");
456
457     ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
458     assertThat(moduleReloaded.name()).isEqualTo("New module name");
459   }
460
461   @Test
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();
468
469     treeRootHolder.setRoot(
470       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
471         .setName("Project")
472         .setDescription("New project description")
473         .addChildren(
474           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
475             .setName("Module")
476             .setDescription("New module description")
477             .build())
478         .build());
479
480     underTest.execute();
481
482     ComponentDto projectReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), PROJECT_KEY).get();
483     assertThat(projectReloaded.description()).isEqualTo("New project description");
484
485     ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
486     assertThat(moduleReloaded.description()).isEqualTo("New module description");
487   }
488
489   @Test
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();
496
497     treeRootHolder.setRoot(
498       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
499         .setName("Project")
500         .addChildren(
501           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
502             .setName("Module")
503             .setPath("New path")
504             .build())
505         .build());
506
507     underTest.execute();
508
509     ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
510     assertThat(moduleReloaded.path()).isEqualTo("New path");
511   }
512
513   @Test
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();
524
525     treeRootHolder.setRoot(
526       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
527         .setName("Project")
528         .addChildren(
529           builder(Component.Type.MODULE, 2).setUuid("EDCB").setKey("MODULE_A")
530             .setName("Module A")
531             .addChildren(
532               builder(Component.Type.MODULE, 3).setUuid("BCDE").setKey("MODULE_B")
533                 .setName("Module B")
534                 .addChildren(
535                   builder(DIRECTORY, 4).setUuid("CDEF").setKey("MODULE_B:src/main/java/dir")
536                     .setPath("src/main/java/dir")
537                     .addChildren(
538                       builder(FILE, 5).setUuid("DEFG").setKey("MODULE_B:src/main/java/dir/Foo.java")
539                         .setPath("src/main/java/dir/Foo.java")
540                         .build())
541                     .build())
542                 .build())
543             .build())
544         .build());
545
546     underTest.execute();
547
548     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(5);
549
550     ComponentDto moduleAreloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_A").get();
551
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());
559
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());
567
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());
575   }
576
577   @Test
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();
585
586     treeRootHolder.setRoot(
587       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
588         .setName("New project name")
589         .build());
590
591     underTest.execute();
592
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);
596   }
597
598   @Test
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();
609
610     treeRootHolder.setRoot(
611       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
612         .setName("Project")
613         .addChildren(
614           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
615             .setName("Module")
616             .addChildren(
617               builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
618                 .setPath("src/main/java/dir")
619                 .addChildren(
620                   builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
621                     .setPath("src/main/java/dir/Foo.java")
622                     .build())
623                 .build())
624             .build())
625         .build());
626
627     underTest.execute();
628
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());
634
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();
643
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();
652
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();
663
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();
674   }
675
676   @Test
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();
689
690     treeRootHolder.setRoot(
691       builder(PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY)
692         .setName("Project")
693         .addChildren(
694           builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY")
695             .setName("Module")
696             .addChildren(
697               builder(DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir")
698                 .setPath("src/main/java/dir")
699                 .addChildren(
700                   builder(FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java")
701                     .setPath("src/main/java/dir/Foo.java")
702                     .build())
703                 .build())
704             .build())
705         .build());
706
707     underTest.execute();
708
709     // Projects contains 4 components from the report + one removed module
710     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(5);
711
712     ComponentDto moduleReloaded = dbClient.componentDao().selectByKey(dbTester.getSession(), "MODULE_KEY").get();
713
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();
724   }
725
726 }