3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.step;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.Random;
25 import java.util.stream.Stream;
26 import javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.api.resources.Qualifiers;
31 import org.sonar.api.resources.Scopes;
32 import org.sonar.api.utils.System2;
33 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.ce.task.projectanalysis.component.BranchPersister;
35 import org.sonar.ce.task.projectanalysis.component.Component;
36 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
37 import org.sonar.ce.task.projectanalysis.component.MutableDisabledComponentsHolder;
38 import org.sonar.ce.task.projectanalysis.component.ProjectPersister;
39 import org.sonar.ce.task.projectanalysis.component.ProjectViewAttributes;
40 import org.sonar.ce.task.projectanalysis.component.SubViewAttributes;
41 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
42 import org.sonar.ce.task.projectanalysis.component.ViewAttributes;
43 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
44 import org.sonar.ce.task.step.ComputationStep;
45 import org.sonar.ce.task.step.TestComputationStepContext;
46 import org.sonar.db.DbClient;
47 import org.sonar.db.DbTester;
48 import org.sonar.db.component.ComponentDbTester;
49 import org.sonar.db.component.ComponentDto;
50 import org.sonar.db.component.ComponentTesting;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
54 import static org.mockito.Mockito.mock;
55 import static org.mockito.Mockito.when;
56 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.APPLICATION;
57 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO;
58 import static org.sonar.ce.task.projectanalysis.component.ViewsComponent.builder;
59 import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME;
60 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
61 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
62 import static org.sonar.db.component.ComponentTesting.newSubPortfolio;
64 public class ViewsPersistComponentsStepTest extends BaseStepTest {
66 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
68 private static final String VIEW_KEY = "VIEW_KEY";
69 private static final String VIEW_NAME = "VIEW_NAME";
70 private static final String VIEW_DESCRIPTION = "view description";
71 private static final String VIEW_UUID = "VIEW_UUID";
72 private static final String SUBVIEW_1_KEY = "SUBVIEW_1_KEY";
73 private static final String SUBVIEW_1_NAME = "SUBVIEW_1_NAME";
74 private static final String SUBVIEW_1_DESCRIPTION = "subview 1 description";
75 private static final String SUBVIEW_1_UUID = "SUBVIEW_1_UUID";
76 private static final String PROJECT_VIEW_1_KEY = "PV1_KEY";
77 private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
78 private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
81 public DbTester dbTester = DbTester.create(System2.INSTANCE);
84 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
86 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
88 private final System2 system2 = mock(System2.class);
89 private final DbClient dbClient = dbTester.getDbClient();
91 private final ComponentDbTester componentDbTester = new ComponentDbTester(dbTester);
92 private final MutableDisabledComponentsHolder disabledComponentsHolder = mock(MutableDisabledComponentsHolder.class, RETURNS_DEEP_STUBS);
93 private PersistComponentsStep underTest;
96 public void setup() throws Exception {
97 now = DATE_FORMAT.parse("2015-06-02");
98 when(system2.now()).thenReturn(now.getTime());
100 analysisMetadataHolder.setBranch(new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME));
101 BranchPersister branchPersister = mock(BranchPersister.class);
102 ProjectPersister projectPersister = mock(ProjectPersister.class);
103 underTest = new PersistComponentsStep(dbClient, treeRootHolder, system2, disabledComponentsHolder, analysisMetadataHolder, branchPersister, projectPersister);
107 protected ComputationStep step() {
112 public void persist_empty_view() {
113 treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build());
115 underTest.execute(new TestComputationStepContext());
117 assertRowsCountInTableProjects(1);
119 ComponentDto projectDto = getComponentFromDb(VIEW_KEY);
120 assertDtoIsView(projectDto);
124 public void persist_existing_empty_view() {
125 // most of the time view already exists since its supposed to be created when config is uploaded
126 persistComponents(newViewDto());
128 treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build());
130 underTest.execute(new TestComputationStepContext());
132 assertRowsCountInTableProjects(1);
134 assertDtoNotUpdated(VIEW_KEY);
138 public void persist_view_with_projectView() {
139 ComponentDto project = ComponentTesting.newPrivateProjectDto();
140 persistComponents(project);
142 treeRootHolder.setRoot(
143 createViewBuilder(PORTFOLIO)
144 .addChildren(createProjectView1Builder(project, null).build())
147 underTest.execute(new TestComputationStepContext());
149 assertRowsCountInTableProjects(3);
151 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
152 assertDtoIsView(viewDto);
154 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
155 assertDtoIsProjectView1(pv1Dto, viewDto, viewDto, project);
159 public void persist_application_with_projectView() {
160 ComponentDto project = ComponentTesting.newPrivateProjectDto();
161 persistComponents(project);
163 treeRootHolder.setRoot(
164 createViewBuilder(APPLICATION)
165 .addChildren(createProjectView1Builder(project, null).build())
168 underTest.execute(new TestComputationStepContext());
170 assertRowsCountInTableProjects(3);
172 ComponentDto applicationDto = getComponentFromDb(VIEW_KEY);
173 assertDtoIsApplication(applicationDto);
175 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
176 assertDtoIsProjectView1(pv1Dto, applicationDto, applicationDto, project);
180 public void persist_empty_subview() {
181 treeRootHolder.setRoot(
182 createViewBuilder(PORTFOLIO)
184 createSubView1Builder(null).build())
187 underTest.execute(new TestComputationStepContext());
189 assertRowsCountInTableProjects(2);
191 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
192 assertDtoIsView(viewDto);
194 ComponentDto sv1Dto = getComponentFromDb(SUBVIEW_1_KEY);
195 assertDtoIsSubView1(viewDto, sv1Dto);
199 public void persist_empty_subview_having_original_view_uuid() {
200 treeRootHolder.setRoot(
201 createViewBuilder(PORTFOLIO)
203 createSubView1Builder("ORIGINAL_UUID").build())
206 underTest.execute(new TestComputationStepContext());
208 assertRowsCountInTableProjects(2);
210 ComponentDto subView = getComponentFromDb(SUBVIEW_1_KEY);
211 assertThat(subView.getCopyComponentUuid()).isEqualTo("ORIGINAL_UUID");
215 public void persist_existing_empty_subview_under_existing_view() {
216 ComponentDto viewDto = newViewDto();
217 persistComponents(viewDto);
218 persistComponents(ComponentTesting.newSubPortfolio(viewDto, SUBVIEW_1_UUID, SUBVIEW_1_KEY).setName(SUBVIEW_1_NAME));
220 treeRootHolder.setRoot(
221 createViewBuilder(PORTFOLIO)
223 createSubView1Builder(null).build())
226 underTest.execute(new TestComputationStepContext());
228 assertRowsCountInTableProjects(2);
230 assertDtoNotUpdated(VIEW_KEY);
231 assertDtoNotUpdated(SUBVIEW_1_KEY);
235 public void persist_empty_subview_under_existing_view() {
236 persistComponents(newViewDto());
238 treeRootHolder.setRoot(
239 createViewBuilder(PORTFOLIO)
241 createSubView1Builder(null).build())
244 underTest.execute(new TestComputationStepContext());
246 assertRowsCountInTableProjects(2);
248 assertDtoNotUpdated(VIEW_KEY);
249 assertDtoIsSubView1(getComponentFromDb(VIEW_KEY), getComponentFromDb(SUBVIEW_1_KEY));
253 public void persist_project_view_under_subview() {
254 ComponentDto project = ComponentTesting.newPrivateProjectDto();
255 persistComponents(project);
257 treeRootHolder.setRoot(
258 createViewBuilder(PORTFOLIO)
260 createSubView1Builder(null)
262 createProjectView1Builder(project, null).build())
266 underTest.execute(new TestComputationStepContext());
268 assertRowsCountInTableProjects(4);
270 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
271 assertDtoIsView(viewDto);
272 ComponentDto subView1Dto = getComponentFromDb(SUBVIEW_1_KEY);
273 assertDtoIsSubView1(viewDto, subView1Dto);
274 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
275 assertDtoIsProjectView1(pv1Dto, viewDto, subView1Dto, project);
279 public void update_view_name_and_longName() {
280 ComponentDto viewDto = newViewDto().setLongName("another long name").setCreatedAt(now);
281 persistComponents(viewDto);
283 treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build());
285 underTest.execute(new TestComputationStepContext());
287 // commit functional transaction -> copies B-fields to A-fields
288 dbClient.componentDao().applyBChangesForRootComponentUuid(dbTester.getSession(), viewDto.uuid());
291 assertRowsCountInTableProjects(1);
292 ComponentDto newViewDto = getComponentFromDb(VIEW_KEY);
293 assertDtoIsView(newViewDto);
297 public void update_project_view() {
298 ComponentDto view = newViewDto();
299 ComponentDto project = ComponentTesting.newPrivateProjectDto();
300 persistComponents(view, project);
301 ComponentDto projectView = ComponentTesting.newProjectCopy(PROJECT_VIEW_1_UUID, project, view)
302 .setKey(PROJECT_VIEW_1_KEY)
305 persistComponents(projectView);
307 treeRootHolder.setRoot(
308 createViewBuilder(PORTFOLIO)
309 .addChildren(createProjectView1Builder(project, null).build())
312 underTest.execute(new TestComputationStepContext());
314 // commit functional transaction -> copies B-fields to A-fields
315 dbClient.componentDao().applyBChangesForRootComponentUuid(dbTester.getSession(), view.uuid());
318 assertRowsCountInTableProjects(3);
319 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
320 assertDtoIsProjectView1(pv1Dto, view, view, project);
324 public void update_copy_component_uuid_of_project_view() {
325 ComponentDto view = newViewDto();
326 ComponentDto project1 = newPrivateProjectDto("P1");
327 ComponentDto project2 = newPrivateProjectDto("P2");
328 persistComponents(view, project1, project2);
330 // Project view in DB is associated to project1
331 ComponentDto projectView = ComponentTesting.newProjectCopy(PROJECT_VIEW_1_UUID, project1, view)
332 .setKey(PROJECT_VIEW_1_KEY)
334 persistComponents(projectView);
336 treeRootHolder.setRoot(
337 createViewBuilder(PORTFOLIO)
338 // Project view in the View is linked to the first project2
339 .addChildren(createProjectView1Builder(project2, null).build())
342 underTest.execute(new TestComputationStepContext());
344 // commit functional transaction -> copies B-fields to A-fields
345 dbClient.componentDao().applyBChangesForRootComponentUuid(dbTester.getSession(), view.uuid());
348 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
349 // Project view should now be linked to project2
350 assertDtoIsProjectView1(pv1Dto, view, view, project2);
354 public void update_copy_component_uuid_of_sub_view() {
355 ComponentDto view = newViewDto();
356 ComponentDto subView = newSubViewDto(view).setCopyComponentUuid("OLD_COPY");
357 persistComponents(view, subView);
359 treeRootHolder.setRoot(
360 createViewBuilder(PORTFOLIO)
362 createSubView1Builder("NEW_COPY").build())
365 underTest.execute(new TestComputationStepContext());
367 // commit functional transaction -> copies B-fields to A-fields
368 dbClient.componentDao().applyBChangesForRootComponentUuid(dbTester.getSession(), view.uuid());
371 ComponentDto subViewReloaded = getComponentFromDb(SUBVIEW_1_KEY);
372 assertThat(subViewReloaded.getCopyComponentUuid()).isEqualTo("NEW_COPY");
376 public void persists_new_components_as_public_if_root_does_not_exist_yet_out_of_functional_transaction() {
377 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
378 treeRootHolder.setRoot(
379 createViewBuilder(PORTFOLIO)
381 createSubView1Builder(null)
383 createProjectView1Builder(project, null).build())
387 underTest.execute(new TestComputationStepContext());
389 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
390 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate()).isFalse());
394 public void persists_new_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
395 boolean isRootPrivate = new Random().nextBoolean();
396 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
397 ComponentDto view = newViewDto().setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
398 dbTester.components().insertComponent(view);
399 treeRootHolder.setRoot(
400 createViewBuilder(PORTFOLIO)
402 createSubView1Builder(null)
404 createProjectView1Builder(project, null).build())
408 underTest.execute(new TestComputationStepContext());
410 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
411 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
412 .describedAs("for uuid " + uuid)
413 .isEqualTo(isRootPrivate));
417 public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
418 boolean isRootPrivate = new Random().nextBoolean();
419 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
420 ComponentDto view = newViewDto().setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
421 dbTester.components().insertComponent(view);
422 ComponentDto subView = newSubPortfolio(view).setUuid("BCDE").setKey("MODULE").setPrivate(!isRootPrivate);
423 dbTester.components().insertComponent(subView);
424 dbTester.components().insertComponent(newProjectCopy("DEFG", project, view).setKey("DIR").setPrivate(isRootPrivate));
425 treeRootHolder.setRoot(
426 createViewBuilder(PORTFOLIO)
428 createSubView1Builder(null)
430 createProjectView1Builder(project, null).build())
434 underTest.execute(new TestComputationStepContext());
436 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID, subView.uuid(), "DEFG")
437 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
438 .describedAs("for uuid " + uuid)
439 .isEqualTo(isRootPrivate));
442 private static ViewsComponent.Builder createViewBuilder(ViewAttributes.Type viewType) {
443 return builder(Component.Type.VIEW, VIEW_KEY)
446 .setDescription(VIEW_DESCRIPTION)
447 .setViewAttributes(new ViewAttributes(viewType));
450 private ViewsComponent.Builder createSubView1Builder(@Nullable String originalViewUuid) {
451 return builder(Component.Type.SUBVIEW, SUBVIEW_1_KEY)
452 .setUuid(SUBVIEW_1_UUID)
453 .setName(SUBVIEW_1_NAME)
454 .setDescription(SUBVIEW_1_DESCRIPTION)
455 .setSubViewAttributes(new SubViewAttributes(originalViewUuid));
458 private static ViewsComponent.Builder createProjectView1Builder(ComponentDto project, Long analysisDate) {
459 return builder(Component.Type.PROJECT_VIEW, PROJECT_VIEW_1_KEY)
460 .setUuid(PROJECT_VIEW_1_UUID)
461 .setName(PROJECT_VIEW_1_NAME)
462 .setDescription("project view description is not persisted")
463 .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getKey(), analysisDate, null));
466 private void persistComponents(ComponentDto... componentDtos) {
467 componentDbTester.insertComponents(componentDtos);
470 private ComponentDto getComponentFromDb(String componentKey) {
471 return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get();
474 private void assertRowsCountInTableProjects(int rowCount) {
475 assertThat(dbTester.countRowsOfTable("components")).isEqualTo(rowCount);
478 private void assertDtoNotUpdated(String componentKey) {
479 assertThat(getComponentFromDb(componentKey).getCreatedAt()).isNotEqualTo(now);
482 private ComponentDto newViewDto() {
483 return ComponentTesting.newPortfolio(VIEW_UUID)
488 private ComponentDto newSubViewDto(ComponentDto rootView) {
489 return ComponentTesting.newSubPortfolio(rootView, SUBVIEW_1_UUID, SUBVIEW_1_KEY)
490 .setName(SUBVIEW_1_NAME);
494 * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()}
496 private void assertDtoIsView(ComponentDto dto) {
497 assertThat(dto.name()).isEqualTo(VIEW_NAME);
498 assertThat(dto.longName()).isEqualTo(VIEW_NAME);
499 assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION);
500 assertThat(dto.path()).isNull();
501 assertThat(dto.uuid()).isEqualTo(VIEW_UUID);
502 assertThat(dto.branchUuid()).isEqualTo(VIEW_UUID);
503 assertThat(dto.getRootUuid()).isEqualTo(VIEW_UUID);
504 assertThat(dto.moduleUuid()).isNull();
505 assertThat(dto.moduleUuidPath()).isEqualTo("." + dto.uuid() + ".");
506 assertThat(dto.qualifier()).isEqualTo(Qualifiers.VIEW);
507 assertThat(dto.scope()).isEqualTo(Scopes.PROJECT);
508 assertThat(dto.getCopyComponentUuid()).isNull();
509 assertThat(dto.getCreatedAt()).isEqualTo(now);
513 * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()}
515 private void assertDtoIsApplication(ComponentDto dto) {
516 assertThat(dto.name()).isEqualTo(VIEW_NAME);
517 assertThat(dto.longName()).isEqualTo(VIEW_NAME);
518 assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION);
519 assertThat(dto.path()).isNull();
520 assertThat(dto.uuid()).isEqualTo(VIEW_UUID);
521 assertThat(dto.branchUuid()).isEqualTo(VIEW_UUID);
522 assertThat(dto.getRootUuid()).isEqualTo(VIEW_UUID);
523 assertThat(dto.moduleUuid()).isNull();
524 assertThat(dto.moduleUuidPath()).isEqualTo("." + dto.uuid() + ".");
525 assertThat(dto.qualifier()).isEqualTo(Qualifiers.APP);
526 assertThat(dto.scope()).isEqualTo(Scopes.PROJECT);
527 assertThat(dto.getCopyComponentUuid()).isNull();
528 assertThat(dto.getCreatedAt()).isEqualTo(now);
532 * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto, Long)}
534 private void assertDtoIsSubView1(ComponentDto viewDto, ComponentDto sv1Dto) {
535 assertThat(sv1Dto.name()).isEqualTo(SUBVIEW_1_NAME);
536 assertThat(sv1Dto.longName()).isEqualTo(SUBVIEW_1_NAME);
537 assertThat(sv1Dto.description()).isEqualTo(SUBVIEW_1_DESCRIPTION);
538 assertThat(sv1Dto.path()).isNull();
539 assertThat(sv1Dto.uuid()).isEqualTo(SUBVIEW_1_UUID);
540 assertThat(sv1Dto.branchUuid()).isEqualTo(viewDto.uuid());
541 assertThat(sv1Dto.getRootUuid()).isEqualTo(viewDto.uuid());
542 assertThat(sv1Dto.moduleUuid()).isEqualTo(viewDto.uuid());
543 assertThat(sv1Dto.moduleUuidPath()).isEqualTo(viewDto.moduleUuidPath() + sv1Dto.uuid() + ".");
544 assertThat(sv1Dto.qualifier()).isEqualTo(Qualifiers.SUBVIEW);
545 assertThat(sv1Dto.scope()).isEqualTo(Scopes.PROJECT);
546 assertThat(sv1Dto.getCopyComponentUuid()).isNull();
547 assertThat(sv1Dto.getCreatedAt()).isEqualTo(now);
550 private void assertDtoIsProjectView1(ComponentDto pv1Dto, ComponentDto viewDto, ComponentDto parentViewDto, ComponentDto project) {
551 assertThat(pv1Dto.name()).isEqualTo(PROJECT_VIEW_1_NAME);
552 assertThat(pv1Dto.longName()).isEqualTo(PROJECT_VIEW_1_NAME);
553 assertThat(pv1Dto.description()).isNull();
554 assertThat(pv1Dto.path()).isNull();
555 assertThat(pv1Dto.uuid()).isEqualTo(PROJECT_VIEW_1_UUID);
556 assertThat(pv1Dto.branchUuid()).isEqualTo(viewDto.uuid());
557 assertThat(pv1Dto.getRootUuid()).isEqualTo(viewDto.uuid());
558 assertThat(pv1Dto.moduleUuid()).isEqualTo(parentViewDto.uuid());
559 assertThat(pv1Dto.moduleUuidPath()).isEqualTo(parentViewDto.moduleUuidPath() + pv1Dto.uuid() + ".");
560 assertThat(pv1Dto.qualifier()).isEqualTo(Qualifiers.PROJECT);
561 assertThat(pv1Dto.scope()).isEqualTo(Scopes.FILE);
562 assertThat(pv1Dto.getCopyComponentUuid()).isEqualTo(project.uuid());
563 assertThat(pv1Dto.getCreatedAt()).isEqualTo(now);