]> source.dussan.org Git - sonarqube.git/blob
7dd7d3449c901f1dbddf4eb754ed95b2aab004f2
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.step;
21
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.Optional;
25 import java.util.Set;
26 import java.util.function.Consumer;
27 import java.util.stream.Stream;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.sonar.api.utils.DateUtils;
32 import org.sonar.api.utils.System2;
33 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.ce.task.projectanalysis.analysis.TestBranch;
35 import org.sonar.ce.task.projectanalysis.component.BranchPersister;
36 import org.sonar.ce.task.projectanalysis.component.Component;
37 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
38 import org.sonar.ce.task.projectanalysis.component.FileAttributes;
39 import org.sonar.ce.task.projectanalysis.component.MutableDisabledComponentsHolder;
40 import org.sonar.ce.task.projectanalysis.component.ProjectPersister;
41 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
42 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
43 import org.sonar.ce.task.step.ComputationStep;
44 import org.sonar.ce.task.step.TestComputationStepContext;
45 import org.sonar.db.DbClient;
46 import org.sonar.db.DbTester;
47 import org.sonar.db.component.ComponentDto;
48 import org.sonar.db.component.ComponentTesting;
49 import org.sonar.server.project.Project;
50
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.when;
55 import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
56 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
57 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
58 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
59 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
60 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
61 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
62 import static org.sonar.db.component.ComponentTesting.newDirectory;
63
64 public class ReportPersistComponentsStepIT extends BaseStepTest {
65
66   private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
67   private static final String PROJECT_KEY = "PROJECT_KEY";
68
69   @Rule
70   public DbTester db = DbTester.create(System2.INSTANCE);
71   @Rule
72   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
73   @Rule
74   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
75
76   private final System2 system2 = mock(System2.class);
77   private final DbClient dbClient = db.getDbClient();
78   private Date now;
79   private final MutableDisabledComponentsHolder disabledComponentsHolder = mock(MutableDisabledComponentsHolder.class, RETURNS_DEEP_STUBS);
80   private PersistComponentsStep underTest;
81
82   @Before
83   public void setup() throws Exception {
84     now = DATE_FORMAT.parse("2015-06-02");
85     when(system2.now()).thenReturn(now.getTime());
86
87     BranchPersister branchPersister = mock(BranchPersister.class);
88     ProjectPersister projectPersister = mock(ProjectPersister.class);
89     underTest = new PersistComponentsStep(dbClient, treeRootHolder, system2, disabledComponentsHolder, analysisMetadataHolder, branchPersister, projectPersister);
90   }
91
92   @Override
93   protected ComputationStep step() {
94     return underTest;
95   }
96
97   @Test
98   public void persist_components() {
99     ComponentDto projectDto = prepareProject();
100     Component file = builder(FILE, 4).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java")
101       .setName("src/main/java/dir/Foo.java")
102       .setShortName("Foo.java")
103       .setFileAttributes(new FileAttributes(false, "java", 1))
104       .build();
105     Component directory = builder(DIRECTORY, 3).setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir")
106       .setName("src/main/java/dir")
107       .setShortName("dir")
108       .addChildren(file)
109       .build();
110     Component treeRoot = asTreeRoot(projectDto)
111       .addChildren(directory)
112       .build();
113     treeRootHolder.setRoot(treeRoot);
114
115     underTest.execute(new TestComputationStepContext());
116
117     assertThat(db.countRowsOfTable("components")).isEqualTo(3);
118
119     ComponentDto directoryDto = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir").get();
120     assertThat(directoryDto.name()).isEqualTo("dir");
121     assertThat(directoryDto.longName()).isEqualTo("src/main/java/dir");
122     assertThat(directoryDto.description()).isNull();
123     assertThat(directoryDto.path()).isEqualTo("src/main/java/dir");
124     assertThat(directoryDto.uuid()).isEqualTo("CDEF");
125     assertThat(directoryDto.getUuidPath()).isEqualTo(UUID_PATH_SEPARATOR + projectDto.uuid() + UUID_PATH_SEPARATOR);
126     assertThat(directoryDto.branchUuid()).isEqualTo(projectDto.uuid());
127     assertThat(directoryDto.qualifier()).isEqualTo("DIR");
128     assertThat(directoryDto.scope()).isEqualTo("DIR");
129     assertThat(directoryDto.getCreatedAt()).isEqualTo(now);
130
131     ComponentDto fileDto = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java").get();
132     assertThat(fileDto.name()).isEqualTo("Foo.java");
133     assertThat(fileDto.longName()).isEqualTo("src/main/java/dir/Foo.java");
134     assertThat(fileDto.description()).isNull();
135     assertThat(fileDto.path()).isEqualTo("src/main/java/dir/Foo.java");
136     assertThat(fileDto.language()).isEqualTo("java");
137     assertThat(fileDto.uuid()).isEqualTo("DEFG");
138     assertThat(fileDto.getUuidPath()).isEqualTo(directoryDto.getUuidPath() + directoryDto.uuid() + UUID_PATH_SEPARATOR);
139     assertThat(fileDto.branchUuid()).isEqualTo(projectDto.uuid());
140     assertThat(fileDto.qualifier()).isEqualTo("FIL");
141     assertThat(fileDto.scope()).isEqualTo("FIL");
142     assertThat(fileDto.getCreatedAt()).isEqualTo(now);
143   }
144
145   @Test
146   public void persist_components_of_existing_branch() {
147     ComponentDto branch = prepareBranch("feature/foo");
148     Component file = builder(FILE, 4).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java")
149       .setName("src/main/java/dir/Foo.java")
150       .setShortName("Foo.java")
151       .setFileAttributes(new FileAttributes(false, "java", 1))
152       .build();
153     Component directory = builder(DIRECTORY, 3).setUuid("CDEF")
154       .setKey("PROJECT_KEY:src/main/java/dir")
155       .setName("src/main/java/dir")
156       .setShortName("dir")
157       .addChildren(file)
158       .build();
159     Component treeRoot = asTreeRoot(branch)
160       .addChildren(directory)
161       .build();
162     treeRootHolder.setRoot(treeRoot);
163
164     underTest.execute(new TestComputationStepContext());
165
166     // 3 in this branch plus the project
167     assertThat(db.countRowsOfTable("components")).isEqualTo(4);
168
169     ComponentDto directoryDto = dbClient.componentDao().selectByKeyAndBranch(db.getSession(), "PROJECT_KEY:src/main/java/dir", "feature/foo").get();
170     assertThat(directoryDto.name()).isEqualTo("dir");
171     assertThat(directoryDto.longName()).isEqualTo("src/main/java/dir");
172     assertThat(directoryDto.description()).isNull();
173     assertThat(directoryDto.path()).isEqualTo("src/main/java/dir");
174     assertThat(directoryDto.uuid()).isEqualTo("CDEF");
175     assertThat(directoryDto.getUuidPath()).isEqualTo(UUID_PATH_SEPARATOR + branch.uuid() + UUID_PATH_SEPARATOR);
176     assertThat(directoryDto.branchUuid()).isEqualTo(branch.uuid());
177     assertThat(directoryDto.qualifier()).isEqualTo("DIR");
178     assertThat(directoryDto.scope()).isEqualTo("DIR");
179     assertThat(directoryDto.getCreatedAt()).isEqualTo(now);
180
181     ComponentDto fileDto = dbClient.componentDao().selectByKeyAndBranch(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java", "feature/foo").get();
182     assertThat(fileDto.name()).isEqualTo("Foo.java");
183     assertThat(fileDto.longName()).isEqualTo("src/main/java/dir/Foo.java");
184     assertThat(fileDto.description()).isNull();
185     assertThat(fileDto.path()).isEqualTo("src/main/java/dir/Foo.java");
186     assertThat(fileDto.language()).isEqualTo("java");
187     assertThat(fileDto.uuid()).isEqualTo("DEFG");
188     assertThat(fileDto.getUuidPath()).isEqualTo(directoryDto.getUuidPath() + directoryDto.uuid() + UUID_PATH_SEPARATOR);
189     assertThat(fileDto.branchUuid()).isEqualTo(branch.uuid());
190     assertThat(fileDto.qualifier()).isEqualTo("FIL");
191     assertThat(fileDto.scope()).isEqualTo("FIL");
192     assertThat(fileDto.getCreatedAt()).isEqualTo(now);
193   }
194
195   @Test
196   public void persist_file_directly_attached_on_root_directory() {
197     ComponentDto projectDto = prepareProject();
198     treeRootHolder.setRoot(
199       asTreeRoot(projectDto)
200         .addChildren(
201           builder(FILE, 2).setUuid("DEFG").setKey(projectDto.getKey() + ":pom.xml")
202             .setName("pom.xml")
203             .build())
204         .build());
205
206     underTest.execute(new TestComputationStepContext());
207
208     assertThat(dbClient.componentDao().selectByKey(db.getSession(), projectDto.getKey() + ":/")).isNotPresent();
209
210     ComponentDto file = dbClient.componentDao().selectByKey(db.getSession(), projectDto.getKey() + ":pom.xml").get();
211     assertThat(file.name()).isEqualTo("pom.xml");
212     assertThat(file.path()).isEqualTo("pom.xml");
213   }
214
215   @Test
216   public void persist_unit_test() {
217     ComponentDto projectDto = prepareProject();
218     treeRootHolder.setRoot(
219       asTreeRoot(projectDto)
220         .addChildren(
221           builder(DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/test/java/dir")
222             .setName("src/test/java/dir")
223             .addChildren(
224               builder(FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/test/java/dir/FooTest.java")
225                 .setName("src/test/java/dir/FooTest.java")
226                 .setShortName("FooTest.java")
227                 .setFileAttributes(new FileAttributes(true, null, 1))
228                 .build())
229             .build())
230         .build());
231
232     underTest.execute(new TestComputationStepContext());
233
234     ComponentDto file = dbClient.componentDao().selectByKey(db.getSession(), PROJECT_KEY + ":src/test/java/dir/FooTest.java").get();
235     assertThat(file.name()).isEqualTo("FooTest.java");
236     assertThat(file.longName()).isEqualTo("src/test/java/dir/FooTest.java");
237     assertThat(file.path()).isEqualTo("src/test/java/dir/FooTest.java");
238     assertThat(file.qualifier()).isEqualTo("UTS");
239     assertThat(file.scope()).isEqualTo("FIL");
240   }
241
242   @Test
243   public void update_file_to_directory_change_scope() {
244     ComponentDto project = prepareProject();
245     ComponentDto directory = ComponentTesting.newDirectory(project, "src").setUuid("CDEF").setKey("PROJECT_KEY:src");
246     ComponentDto file = ComponentTesting.newFileDto(project, directory, "DEFG").setPath("src/foo").setName("foo")
247       .setKey("PROJECT_KEY:src/foo");
248     dbClient.componentDao().insert(db.getSession(), Set.of(directory, file), true);
249     db.getSession().commit();
250
251     assertThat(dbClient.componentDao().selectByKey(db.getSession(), PROJECT_KEY + ":src/foo").get().scope()).isEqualTo("FIL");
252
253     treeRootHolder.setRoot(
254       asTreeRoot(project)
255         .addChildren(
256           builder(DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src")
257             .setName("src")
258             .addChildren(
259               builder(DIRECTORY, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/foo")
260                 .setName("foo")
261                 .addChildren(
262                   builder(FILE, 4).setUuid("HIJK").setKey(PROJECT_KEY + ":src/foo/FooTest.java")
263                     .setName("src/foo/FooTest.java")
264                     .setShortName("FooTest.java")
265                     .setFileAttributes(new FileAttributes(false, null, 1))
266                     .build())
267                 .build())
268             .build())
269         .build());
270
271     underTest.execute(new TestComputationStepContext());
272
273     // commit the functional transaction
274     dbClient.componentDao().applyBChangesForBranchUuid(db.getSession(), project.uuid());
275     db.commit();
276
277     assertThat(dbClient.componentDao().selectByKey(db.getSession(), PROJECT_KEY + ":src/foo").get().scope()).isEqualTo("DIR");
278   }
279
280   @Test
281   public void persist_only_new_components() {
282     // Project and module already exists
283     ComponentDto project = prepareProject();
284     db.getSession().commit();
285
286     treeRootHolder.setRoot(
287       builder(PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey())
288         .setName("Project")
289         .addChildren(
290           builder(DIRECTORY, 3).setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir")
291             .setName("src/main/java/dir")
292             .addChildren(
293               builder(FILE, 4).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java")
294                 .setName("src/main/java/dir/Foo.java")
295                 .setShortName("Foo.java")
296                 .build())
297             .build())
298         .build());
299
300     underTest.execute(new TestComputationStepContext());
301
302     assertThat(db.countRowsOfTable("components")).isEqualTo(3);
303
304     ComponentDto projectReloaded = dbClient.componentDao().selectByKey(db.getSession(), project.getKey()).get();
305     assertThat(projectReloaded.uuid()).isEqualTo(project.uuid());
306     assertThat(projectReloaded.getUuidPath()).isEqualTo(UUID_PATH_OF_ROOT);
307
308     ComponentDto directory = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir").get();
309     assertThat(directory.getUuidPath()).isEqualTo(directory.getUuidPath());
310     assertThat(directory.branchUuid()).isEqualTo(project.uuid());
311
312     ComponentDto file = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java").get();
313     assertThat(file.getUuidPath()).isEqualTo(file.getUuidPath());
314     assertThat(file.branchUuid()).isEqualTo(project.uuid());
315   }
316
317   @Test
318   public void nothing_to_persist() {
319     ComponentDto project = prepareProject();
320     ComponentDto directory = ComponentTesting.newDirectory(project, "src/main/java/dir").setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir");
321     ComponentDto file = ComponentTesting.newFileDto(project, directory, "DEFG").setPath("src/main/java/dir/Foo.java").setName("Foo.java")
322       .setKey("PROJECT_KEY:src/main/java/dir/Foo.java");
323     dbClient.componentDao().insert(db.getSession(), Set.of(directory, file), true);
324     db.getSession().commit();
325
326     treeRootHolder.setRoot(
327       builder(PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey())
328         .setName("Project")
329         .addChildren(
330           builder(DIRECTORY, 3).setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir")
331             .setName("src/main/java/dir")
332             .addChildren(
333               builder(FILE, 4).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java")
334                 .setName("src/main/java/dir/Foo.java")
335                 .build())
336             .build())
337         .build());
338
339     underTest.execute(new TestComputationStepContext());
340
341     assertThat(db.countRowsOfTable("components")).isEqualTo(3);
342     assertThat(dbClient.componentDao().selectByKey(db.getSession(), project.getKey()).get().uuid()).isEqualTo(project.uuid());
343     assertThat(dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir").get().uuid()).isEqualTo(directory.uuid());
344     assertThat(dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java").get().uuid()).isEqualTo(file.uuid());
345
346     ComponentDto projectReloaded = dbClient.componentDao().selectByKey(db.getSession(), project.getKey()).get();
347     assertThat(projectReloaded.uuid()).isEqualTo(project.uuid());
348     assertThat(projectReloaded.branchUuid()).isEqualTo(project.branchUuid());
349
350     ComponentDto directoryReloaded = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir").get();
351     assertThat(directoryReloaded.uuid()).isEqualTo(directory.uuid());
352     assertThat(directoryReloaded.getUuidPath()).isEqualTo(directory.getUuidPath());
353     assertThat(directoryReloaded.branchUuid()).isEqualTo(directory.branchUuid());
354     assertThat(directoryReloaded.name()).isEqualTo(directory.name());
355     assertThat(directoryReloaded.path()).isEqualTo(directory.path());
356
357     ComponentDto fileReloaded = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java").get();
358     assertThat(fileReloaded.uuid()).isEqualTo(file.uuid());
359     assertThat(fileReloaded.getUuidPath()).isEqualTo(file.getUuidPath());
360     assertThat(fileReloaded.branchUuid()).isEqualTo(file.branchUuid());
361     assertThat(fileReloaded.name()).isEqualTo(file.name());
362     assertThat(fileReloaded.path()).isEqualTo(file.path());
363   }
364
365   @Test
366   public void do_not_update_created_at_on_existing_component() {
367     Date oldDate = DateUtils.parseDate("2015-01-01");
368     ComponentDto project = prepareProject(p -> p.setCreatedAt(oldDate));
369     db.getSession().commit();
370
371     treeRootHolder.setRoot(
372       builder(PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey())
373         .build());
374
375     underTest.execute(new TestComputationStepContext());
376
377     Optional<ComponentDto> projectReloaded = dbClient.componentDao().selectByUuid(db.getSession(), project.uuid());
378     assertThat(projectReloaded.get().getCreatedAt()).isNotEqualTo(now);
379   }
380
381   @Test
382   public void persist_components_that_were_previously_removed() {
383     ComponentDto project = prepareProject();
384     ComponentDto removedDirectory = ComponentTesting.newDirectory(project, "src/main/java/dir")
385       .setLongName("src/main/java/dir")
386       .setName("dir")
387       .setUuid("CDEF")
388       .setKey("PROJECT_KEY:src/main/java/dir")
389       .setEnabled(false);
390     ComponentDto removedFile = ComponentTesting.newFileDto(project, removedDirectory, "DEFG")
391       .setPath("src/main/java/dir/Foo.java")
392       .setLongName("src/main/java/dir/Foo.java")
393       .setName("Foo.java")
394       .setKey("PROJECT_KEY:src/main/java/dir/Foo.java")
395       .setEnabled(false);
396     dbClient.componentDao().insert(db.getSession(), Set.of(removedDirectory, removedFile), true);
397     db.getSession().commit();
398
399     treeRootHolder.setRoot(
400       builder(PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey())
401         .setName("Project")
402         .addChildren(
403           builder(DIRECTORY, 3).setUuid("CDEF").setKey("PROJECT_KEY:src/main/java/dir")
404             .setName("src/main/java/dir")
405             .setShortName("dir")
406             .addChildren(
407               builder(FILE, 4).setUuid("DEFG").setKey("PROJECT_KEY:src/main/java/dir/Foo.java")
408                 .setName("src/main/java/dir/Foo.java")
409                 .setShortName("Foo.java")
410                 .build())
411             .build())
412         .build());
413
414     underTest.execute(new TestComputationStepContext());
415
416     assertThat(db.countRowsOfTable("components")).isEqualTo(3);
417     assertThat(dbClient.componentDao().selectByKey(db.getSession(), project.getKey()).get().uuid()).isEqualTo(project.uuid());
418     assertThat(dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir").get().uuid()).isEqualTo(removedDirectory.uuid());
419     assertThat(dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java").get().uuid()).isEqualTo(removedFile.uuid());
420     assertExistButDisabled(removedDirectory.getKey(), removedFile.getKey());
421
422     // commit the functional transaction
423     dbClient.componentDao().applyBChangesForBranchUuid(db.getSession(), project.uuid());
424
425     ComponentDto projectReloaded = dbClient.componentDao().selectByKey(db.getSession(), project.getKey()).get();
426     assertThat(projectReloaded.uuid()).isEqualTo(project.uuid());
427     assertThat(projectReloaded.getUuidPath()).isEqualTo(project.getUuidPath());
428     assertThat(projectReloaded.branchUuid()).isEqualTo(project.branchUuid());
429     assertThat(projectReloaded.isEnabled()).isTrue();
430
431     ComponentDto directoryReloaded = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir").get();
432     assertThat(directoryReloaded.uuid()).isEqualTo(removedDirectory.uuid());
433     assertThat(directoryReloaded.getUuidPath()).isEqualTo(removedDirectory.getUuidPath());
434     assertThat(directoryReloaded.branchUuid()).isEqualTo(removedDirectory.branchUuid());
435     assertThat(directoryReloaded.name()).isEqualTo(removedDirectory.name());
436     assertThat(directoryReloaded.longName()).isEqualTo(removedDirectory.longName());
437     assertThat(directoryReloaded.path()).isEqualTo(removedDirectory.path());
438     assertThat(directoryReloaded.isEnabled()).isTrue();
439
440     ComponentDto fileReloaded = dbClient.componentDao().selectByKey(db.getSession(), "PROJECT_KEY:src/main/java/dir/Foo.java").get();
441     assertThat(fileReloaded.uuid()).isEqualTo(removedFile.uuid());
442     assertThat(fileReloaded.getUuidPath()).isEqualTo(removedFile.getUuidPath());
443     assertThat(fileReloaded.branchUuid()).isEqualTo(removedFile.branchUuid());
444     assertThat(fileReloaded.name()).isEqualTo(removedFile.name());
445     assertThat(fileReloaded.path()).isEqualTo(removedFile.path());
446     assertThat(fileReloaded.isEnabled()).isTrue();
447   }
448
449   private void assertExistButDisabled(String... keys) {
450     for (String key : keys) {
451       ComponentDto dto = dbClient.componentDao().selectByKey(db.getSession(), key).get();
452       assertThat(dto.isEnabled()).isFalse();
453     }
454   }
455
456   @Test
457   public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
458     ComponentDto project = prepareProject(p -> p.setPrivate(true));
459     ComponentDto dir = db.components().insertComponent(newDirectory(project, "DEFG", "Directory").setKey("DIR").setPrivate(true));
460     treeRootHolder.setRoot(createSampleProjectComponentTree(project));
461
462     underTest.execute(new TestComputationStepContext());
463
464     Stream.of(project.uuid(), dir.uuid())
465       .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(db.getSession(), uuid).get().isPrivate())
466         .describedAs("for uuid " + uuid)
467         .isTrue());
468   }
469
470   private ReportComponent createSampleProjectComponentTree(ComponentDto project) {
471     return createSampleProjectComponentTree(project.uuid(), project.getKey());
472   }
473
474   private ReportComponent createSampleProjectComponentTree(String projectUuid, String projectKey) {
475     return builder(PROJECT, 1).setUuid(projectUuid).setKey(projectKey)
476       .setName("Project")
477       .addChildren(
478         builder(Component.Type.DIRECTORY, 3).setUuid("DEFG").setKey("DIR")
479           .setName("Directory")
480           .addChildren(
481             builder(FILE, 4).setUuid("CDEF").setKey("FILE")
482               .setName("file")
483               .build())
484           .build())
485       .build();
486   }
487
488   private ReportComponent.Builder asTreeRoot(ComponentDto project) {
489     return builder(PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey()).setName(project.name());
490   }
491
492   private ComponentDto prepareProject() {
493     return prepareProject(defaults());
494   }
495
496   private ComponentDto prepareProject(Consumer<ComponentDto> populators) {
497     ComponentDto dto = db.components().insertPrivateProject(populators).getMainBranchComponent();
498     analysisMetadataHolder.setProject(Project.from(dto));
499     analysisMetadataHolder.setBranch(new DefaultBranchImpl(DEFAULT_MAIN_BRANCH_NAME));
500     return dto;
501   }
502
503   private ComponentDto prepareBranch(String branchName) {
504     ComponentDto projectDto = db.components().insertPublicProject().getMainBranchComponent();
505     ComponentDto branchDto = db.components().insertProjectBranch(projectDto, b -> b.setKey(branchName));
506     analysisMetadataHolder.setProject(Project.from(projectDto));
507     analysisMetadataHolder.setBranch(new TestBranch(branchName));
508     return branchDto;
509   }
510
511   private static <T> Consumer<T> defaults() {
512     return t -> {
513     };
514   }
515
516 }