3 * Copyright (C) 2009-2024 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.component.BranchPersister;
34 import org.sonar.ce.task.projectanalysis.component.Component;
35 import org.sonar.ce.task.projectanalysis.component.MutableDisabledComponentsHolder;
36 import org.sonar.ce.task.projectanalysis.component.ProjectPersister;
37 import org.sonar.ce.task.projectanalysis.component.ProjectViewAttributes;
38 import org.sonar.ce.task.projectanalysis.component.SubViewAttributes;
39 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
40 import org.sonar.ce.task.projectanalysis.component.ViewAttributes;
41 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
42 import org.sonar.ce.task.projectanalysis.component.VisitException;
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.audit.AuditPersister;
48 import org.sonar.db.component.ComponentDto;
49 import org.sonar.db.component.ComponentTesting;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.assertThatThrownBy;
53 import static org.mockito.ArgumentMatchers.any;
54 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
55 import static org.mockito.Mockito.mock;
56 import static org.mockito.Mockito.times;
57 import static org.mockito.Mockito.verify;
58 import static org.mockito.Mockito.when;
59 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.APPLICATION;
60 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO;
61 import static org.sonar.ce.task.projectanalysis.component.ViewsComponent.builder;
62 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
63 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
64 import static org.sonar.db.component.ComponentTesting.newSubPortfolio;
66 public class ViewsPersistComponentsStepIT extends BaseStepTest {
68 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
70 private static final String VIEW_KEY = "VIEW_KEY";
71 private static final String VIEW_NAME = "VIEW_NAME";
72 private static final String VIEW_DESCRIPTION = "view description";
73 private static final String VIEW_UUID = "VIEW_UUID";
74 private static final String SUBVIEW_1_KEY = "SUBVIEW_1_KEY";
75 private static final String SUBVIEW_1_NAME = "SUBVIEW_1_NAME";
76 private static final String SUBVIEW_1_DESCRIPTION = "subview 1 description";
77 private static final String SUBVIEW_1_UUID = "SUBVIEW_1_UUID";
78 private static final String PROJECT_VIEW_1_KEY = "PV1_KEY";
79 private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
80 private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
82 private final AuditPersister auditPersister = mock();
85 public DbTester dbTester = DbTester.create(System2.INSTANCE, auditPersister);
88 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
90 private final System2 system2 = mock(System2.class);
91 private final DbClient dbClient = dbTester.getDbClient();
93 private final MutableDisabledComponentsHolder disabledComponentsHolder = mock(MutableDisabledComponentsHolder.class, RETURNS_DEEP_STUBS);
94 private PersistComponentsStep underTest;
97 public void setup() throws Exception {
98 now = DATE_FORMAT.parse("2015-06-02");
99 when(system2.now()).thenReturn(now.getTime());
101 BranchPersister branchPersister = mock(BranchPersister.class);
102 ProjectPersister projectPersister = mock(ProjectPersister.class);
103 underTest = new PersistComponentsStep(dbClient, treeRootHolder, system2, disabledComponentsHolder, branchPersister, projectPersister);
107 protected ComputationStep step() {
112 public void persist_empty_view() {
113 treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build());
115 underTest.execute(new TestComputationStepContext());
117 assertRowsCountInTableComponents(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 assertRowsCountInTableComponents(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 assertRowsCountInTableComponents(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 * Assumption under test here is that application should always exists in the projects table.
160 * We should never have a state where application exists in Components table but not in Projects table
163 public void execute_whenApplicationDoesNotExistsAndTryingToInsertItInComponentsTable_throwException() {
164 ComponentDto project = dbTester.components().insertPrivateProject().getMainBranchComponent();
166 treeRootHolder.setRoot(
167 createViewBuilder(APPLICATION)
168 .addChildren(createProjectView1Builder(project, null).build())
171 assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
172 .isInstanceOf(VisitException.class);
176 public void persist_empty_subview() {
177 treeRootHolder.setRoot(
178 createViewBuilder(PORTFOLIO)
180 createSubView1Builder(null).build())
183 underTest.execute(new TestComputationStepContext());
185 assertRowsCountInTableComponents(2);
187 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
188 assertDtoIsView(viewDto);
190 ComponentDto sv1Dto = getComponentFromDb(SUBVIEW_1_KEY);
191 assertDtoIsSubView1(viewDto, sv1Dto);
195 public void persist_empty_subview_having_original_view_uuid() {
196 treeRootHolder.setRoot(
197 createViewBuilder(PORTFOLIO)
199 createSubView1Builder("ORIGINAL_UUID").build())
202 underTest.execute(new TestComputationStepContext());
204 assertRowsCountInTableComponents(2);
206 ComponentDto subView = getComponentFromDb(SUBVIEW_1_KEY);
207 assertThat(subView.getCopyComponentUuid()).isEqualTo("ORIGINAL_UUID");
211 public void persist_existing_empty_subview_under_existing_view() {
212 ComponentDto viewDto = newViewDto();
213 persistComponents(viewDto);
214 persistComponents(ComponentTesting.newSubPortfolio(viewDto, SUBVIEW_1_UUID, SUBVIEW_1_KEY).setName(SUBVIEW_1_NAME));
216 treeRootHolder.setRoot(
217 createViewBuilder(PORTFOLIO)
219 createSubView1Builder(null).build())
222 underTest.execute(new TestComputationStepContext());
224 assertRowsCountInTableComponents(2);
226 assertDtoNotUpdated(VIEW_KEY);
227 assertDtoNotUpdated(SUBVIEW_1_KEY);
231 public void persist_empty_subview_under_existing_view() {
232 persistComponents(newViewDto());
234 treeRootHolder.setRoot(
235 createViewBuilder(PORTFOLIO)
237 createSubView1Builder(null).build())
240 underTest.execute(new TestComputationStepContext());
242 assertRowsCountInTableComponents(2);
244 assertDtoNotUpdated(VIEW_KEY);
245 assertDtoIsSubView1(getComponentFromDb(VIEW_KEY), getComponentFromDb(SUBVIEW_1_KEY));
249 public void persist_project_view_under_subview() {
250 ComponentDto project = ComponentTesting.newPrivateProjectDto();
251 persistComponents(project);
253 treeRootHolder.setRoot(
254 createViewBuilder(PORTFOLIO)
256 createSubView1Builder(null)
258 createProjectView1Builder(project, null).build())
262 underTest.execute(new TestComputationStepContext());
264 assertRowsCountInTableComponents(4);
266 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
267 assertDtoIsView(viewDto);
268 ComponentDto subView1Dto = getComponentFromDb(SUBVIEW_1_KEY);
269 assertDtoIsSubView1(viewDto, subView1Dto);
270 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
271 assertDtoIsProjectView1(pv1Dto, viewDto, subView1Dto, project);
275 public void update_view_name_and_longName() {
276 ComponentDto viewDto = newViewDto().setLongName("another long name").setCreatedAt(now);
277 persistComponents(viewDto);
279 treeRootHolder.setRoot(createViewBuilder(PORTFOLIO).build());
281 underTest.execute(new TestComputationStepContext());
283 // commit functional transaction -> copies B-fields to A-fields
284 dbClient.componentDao().applyBChangesForBranchUuid(dbTester.getSession(), viewDto.uuid());
287 assertRowsCountInTableComponents(1);
288 ComponentDto newViewDto = getComponentFromDb(VIEW_KEY);
289 assertDtoIsView(newViewDto);
293 public void update_project_view() {
294 ComponentDto view = newViewDto();
295 ComponentDto project = ComponentTesting.newPrivateProjectDto();
296 persistComponents(view, project);
297 ComponentDto projectView = ComponentTesting.newProjectCopy(PROJECT_VIEW_1_UUID, project, view)
298 .setKey(PROJECT_VIEW_1_KEY)
301 persistComponents(projectView);
302 treeRootHolder.setRoot(
303 createViewBuilder(PORTFOLIO)
304 .addChildren(createProjectView1Builder(project, null).build())
306 underTest.execute(new TestComputationStepContext());
307 // commit functional transaction -> copies B-fields to A-fields
308 dbClient.componentDao().applyBChangesForBranchUuid(dbTester.getSession(), view.uuid());
311 assertRowsCountInTableComponents(3);
312 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
313 assertDtoIsProjectView1(pv1Dto, view, view, project);
317 public void execute_whenPortfolioProjectAndTechnicalProjectIsCreated_shouldStoreTwoAuditLogs() {
318 ComponentDto view = newViewDto();
319 ComponentDto project = ComponentTesting.newPrivateProjectDto();
320 persistComponents(view, project);
322 treeRootHolder.setRoot(
323 createViewBuilder(PORTFOLIO)
324 .addChildren(createProjectView1Builder(project, null).build())
327 underTest.execute(new TestComputationStepContext());
329 //We create audits for the portfolio and the project, not for the technical project
330 verify(auditPersister, times(2)).addComponent(any(), any());
334 public void update_copy_component_uuid_of_project_view() {
335 ComponentDto view = newViewDto();
336 ComponentDto project1 = newPrivateProjectDto("P1");
337 ComponentDto project2 = newPrivateProjectDto("P2");
338 persistComponents(view, project1, project2);
340 // Project view in DB is associated to project1
341 ComponentDto projectView = ComponentTesting.newProjectCopy(PROJECT_VIEW_1_UUID, project1, view)
342 .setKey(PROJECT_VIEW_1_KEY)
344 persistComponents(projectView);
346 treeRootHolder.setRoot(
347 createViewBuilder(PORTFOLIO)
348 // Project view in the View is linked to the first project2
349 .addChildren(createProjectView1Builder(project2, null).build())
352 underTest.execute(new TestComputationStepContext());
354 // commit functional transaction -> copies B-fields to A-fields
355 dbClient.componentDao().applyBChangesForBranchUuid(dbTester.getSession(), view.uuid());
358 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
359 // Project view should now be linked to project2
360 assertDtoIsProjectView1(pv1Dto, view, view, project2);
364 public void update_copy_component_uuid_of_sub_view() {
365 ComponentDto view = newViewDto();
366 ComponentDto subView = newSubViewDto(view).setCopyComponentUuid("OLD_COPY");
367 persistComponents(view, subView);
369 treeRootHolder.setRoot(
370 createViewBuilder(PORTFOLIO)
372 createSubView1Builder("NEW_COPY").build())
375 underTest.execute(new TestComputationStepContext());
377 // commit functional transaction -> copies B-fields to A-fields
378 dbClient.componentDao().applyBChangesForBranchUuid(dbTester.getSession(), view.uuid());
381 ComponentDto subViewReloaded = getComponentFromDb(SUBVIEW_1_KEY);
382 assertThat(subViewReloaded.getCopyComponentUuid()).isEqualTo("NEW_COPY");
386 public void persists_new_components_as_public_if_root_does_not_exist_yet_out_of_functional_transaction() {
387 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
388 treeRootHolder.setRoot(
389 createViewBuilder(PORTFOLIO)
391 createSubView1Builder(null)
393 createProjectView1Builder(project, null).build())
397 underTest.execute(new TestComputationStepContext());
399 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
400 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate()).isFalse());
404 public void persists_new_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
405 boolean isRootPrivate = new Random().nextBoolean();
406 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
407 ComponentDto view = newViewDto().setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
408 dbTester.components().insertComponent(view);
409 treeRootHolder.setRoot(
410 createViewBuilder(PORTFOLIO)
412 createSubView1Builder(null)
414 createProjectView1Builder(project, null).build())
418 underTest.execute(new TestComputationStepContext());
420 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
421 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
422 .describedAs("for uuid " + uuid)
423 .isEqualTo(isRootPrivate));
427 public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
428 boolean isRootPrivate = new Random().nextBoolean();
429 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
430 ComponentDto view = newViewDto().setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
431 dbTester.components().insertComponent(view);
432 ComponentDto subView = newSubPortfolio(view).setUuid("BCDE").setKey("MODULE").setPrivate(!isRootPrivate);
433 dbTester.components().insertComponent(subView);
434 dbTester.components().insertComponent(newProjectCopy("DEFG", project, view).setKey("DIR").setPrivate(isRootPrivate));
435 treeRootHolder.setRoot(
436 createViewBuilder(PORTFOLIO)
438 createSubView1Builder(null)
440 createProjectView1Builder(project, null).build())
444 underTest.execute(new TestComputationStepContext());
446 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID, subView.uuid(), "DEFG")
447 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
448 .describedAs("for uuid " + uuid)
449 .isEqualTo(isRootPrivate));
452 private static ViewsComponent.Builder createViewBuilder(ViewAttributes.Type viewType) {
453 return builder(Component.Type.VIEW, VIEW_KEY)
456 .setDescription(VIEW_DESCRIPTION)
457 .setViewAttributes(new ViewAttributes(viewType));
460 private ViewsComponent.Builder createSubView1Builder(@Nullable String originalViewUuid) {
461 return builder(Component.Type.SUBVIEW, SUBVIEW_1_KEY)
462 .setUuid(SUBVIEW_1_UUID)
463 .setName(SUBVIEW_1_NAME)
464 .setDescription(SUBVIEW_1_DESCRIPTION)
465 .setSubViewAttributes(new SubViewAttributes(originalViewUuid));
468 private static ViewsComponent.Builder createProjectView1Builder(ComponentDto project, Long analysisDate) {
469 return builder(Component.Type.PROJECT_VIEW, PROJECT_VIEW_1_KEY)
470 .setUuid(PROJECT_VIEW_1_UUID)
471 .setName(PROJECT_VIEW_1_NAME)
472 .setDescription("project view description is not persisted")
473 .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getKey(), analysisDate, null));
476 private void persistComponents(ComponentDto... componentDtos) {
477 dbTester.components().insertComponents(componentDtos);
480 private ComponentDto getComponentFromDb(String componentKey) {
481 return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get();
484 private void assertRowsCountInTableComponents(int rowCount) {
485 assertThat(dbTester.countRowsOfTable("components")).isEqualTo(rowCount);
488 private void assertDtoNotUpdated(String componentKey) {
489 assertThat(getComponentFromDb(componentKey).getCreatedAt()).isNotEqualTo(now);
492 private ComponentDto newViewDto() {
493 return ComponentTesting.newPortfolio(VIEW_UUID)
498 private ComponentDto newSubViewDto(ComponentDto rootView) {
499 return ComponentTesting.newSubPortfolio(rootView, SUBVIEW_1_UUID, SUBVIEW_1_KEY)
500 .setName(SUBVIEW_1_NAME);
504 * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()}
506 private void assertDtoIsView(ComponentDto dto) {
507 assertThat(dto.name()).isEqualTo(VIEW_NAME);
508 assertThat(dto.longName()).isEqualTo(VIEW_NAME);
509 assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION);
510 assertThat(dto.path()).isNull();
511 assertThat(dto.uuid()).isEqualTo(VIEW_UUID);
512 assertThat(dto.branchUuid()).isEqualTo(VIEW_UUID);
513 assertThat(dto.qualifier()).isEqualTo(Qualifiers.VIEW);
514 assertThat(dto.scope()).isEqualTo(Scopes.PROJECT);
515 assertThat(dto.getCopyComponentUuid()).isNull();
516 assertThat(dto.getCreatedAt()).isEqualTo(now);
520 * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()}
522 private void assertDtoIsApplication(ComponentDto dto) {
523 assertThat(dto.name()).isEqualTo(VIEW_NAME);
524 assertThat(dto.longName()).isEqualTo(VIEW_NAME);
525 assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION);
526 assertThat(dto.path()).isNull();
527 assertThat(dto.uuid()).isEqualTo(VIEW_UUID);
528 assertThat(dto.branchUuid()).isEqualTo(VIEW_UUID);
529 assertThat(dto.qualifier()).isEqualTo(Qualifiers.APP);
530 assertThat(dto.scope()).isEqualTo(Scopes.PROJECT);
531 assertThat(dto.getCopyComponentUuid()).isNull();
532 assertThat(dto.getCreatedAt()).isEqualTo(now);
536 * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto, Long)}
538 private void assertDtoIsSubView1(ComponentDto viewDto, ComponentDto sv1Dto) {
539 assertThat(sv1Dto.name()).isEqualTo(SUBVIEW_1_NAME);
540 assertThat(sv1Dto.longName()).isEqualTo(SUBVIEW_1_NAME);
541 assertThat(sv1Dto.description()).isEqualTo(SUBVIEW_1_DESCRIPTION);
542 assertThat(sv1Dto.path()).isNull();
543 assertThat(sv1Dto.uuid()).isEqualTo(SUBVIEW_1_UUID);
544 assertThat(sv1Dto.branchUuid()).isEqualTo(viewDto.uuid());
545 assertThat(sv1Dto.qualifier()).isEqualTo(Qualifiers.SUBVIEW);
546 assertThat(sv1Dto.scope()).isEqualTo(Scopes.PROJECT);
547 assertThat(sv1Dto.getCopyComponentUuid()).isNull();
548 assertThat(sv1Dto.getCreatedAt()).isEqualTo(now);
551 private void assertDtoIsProjectView1(ComponentDto pv1Dto, ComponentDto viewDto, ComponentDto parentViewDto, ComponentDto project) {
552 assertThat(pv1Dto.name()).isEqualTo(PROJECT_VIEW_1_NAME);
553 assertThat(pv1Dto.longName()).isEqualTo(PROJECT_VIEW_1_NAME);
554 assertThat(pv1Dto.description()).isNull();
555 assertThat(pv1Dto.path()).isNull();
556 assertThat(pv1Dto.uuid()).isEqualTo(PROJECT_VIEW_1_UUID);
557 assertThat(pv1Dto.branchUuid()).isEqualTo(viewDto.uuid());
558 assertThat(pv1Dto.qualifier()).isEqualTo(Qualifiers.PROJECT);
559 assertThat(pv1Dto.scope()).isEqualTo(Scopes.FILE);
560 assertThat(pv1Dto.getCopyComponentUuid()).isEqualTo(project.uuid());
561 assertThat(pv1Dto.getCreatedAt()).isEqualTo(now);