3 * Copyright (C) 2009-2023 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.projectanalysis.component.VisitException;
45 import org.sonar.ce.task.step.ComputationStep;
46 import org.sonar.ce.task.step.TestComputationStepContext;
47 import org.sonar.db.DbClient;
48 import org.sonar.db.DbTester;
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.assertj.core.api.Assertions.assertThatThrownBy;
54 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
55 import static org.mockito.Mockito.mock;
56 import static org.mockito.Mockito.when;
57 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.APPLICATION;
58 import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO;
59 import static org.sonar.ce.task.projectanalysis.component.ViewsComponent.builder;
60 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
61 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
62 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
63 import static org.sonar.db.component.ComponentTesting.newSubPortfolio;
65 public class ViewsPersistComponentsStepIT extends BaseStepTest {
67 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
69 private static final String VIEW_KEY = "VIEW_KEY";
70 private static final String VIEW_NAME = "VIEW_NAME";
71 private static final String VIEW_DESCRIPTION = "view description";
72 private static final String VIEW_UUID = "VIEW_UUID";
73 private static final String SUBVIEW_1_KEY = "SUBVIEW_1_KEY";
74 private static final String SUBVIEW_1_NAME = "SUBVIEW_1_NAME";
75 private static final String SUBVIEW_1_DESCRIPTION = "subview 1 description";
76 private static final String SUBVIEW_1_UUID = "SUBVIEW_1_UUID";
77 private static final String PROJECT_VIEW_1_KEY = "PV1_KEY";
78 private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
79 private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
82 public DbTester dbTester = DbTester.create(System2.INSTANCE);
85 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
87 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
89 private final System2 system2 = mock(System2.class);
90 private final DbClient dbClient = dbTester.getDbClient();
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_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 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 update_copy_component_uuid_of_project_view() {
318 ComponentDto view = newViewDto();
319 ComponentDto project1 = newPrivateProjectDto("P1");
320 ComponentDto project2 = newPrivateProjectDto("P2");
321 persistComponents(view, project1, project2);
323 // Project view in DB is associated to project1
324 ComponentDto projectView = ComponentTesting.newProjectCopy(PROJECT_VIEW_1_UUID, project1, view)
325 .setKey(PROJECT_VIEW_1_KEY)
327 persistComponents(projectView);
329 treeRootHolder.setRoot(
330 createViewBuilder(PORTFOLIO)
331 // Project view in the View is linked to the first project2
332 .addChildren(createProjectView1Builder(project2, null).build())
335 underTest.execute(new TestComputationStepContext());
337 // commit functional transaction -> copies B-fields to A-fields
338 dbClient.componentDao().applyBChangesForBranchUuid(dbTester.getSession(), view.uuid());
341 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
342 // Project view should now be linked to project2
343 assertDtoIsProjectView1(pv1Dto, view, view, project2);
347 public void update_copy_component_uuid_of_sub_view() {
348 ComponentDto view = newViewDto();
349 ComponentDto subView = newSubViewDto(view).setCopyComponentUuid("OLD_COPY");
350 persistComponents(view, subView);
352 treeRootHolder.setRoot(
353 createViewBuilder(PORTFOLIO)
355 createSubView1Builder("NEW_COPY").build())
358 underTest.execute(new TestComputationStepContext());
360 // commit functional transaction -> copies B-fields to A-fields
361 dbClient.componentDao().applyBChangesForBranchUuid(dbTester.getSession(), view.uuid());
364 ComponentDto subViewReloaded = getComponentFromDb(SUBVIEW_1_KEY);
365 assertThat(subViewReloaded.getCopyComponentUuid()).isEqualTo("NEW_COPY");
369 public void persists_new_components_as_public_if_root_does_not_exist_yet_out_of_functional_transaction() {
370 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
371 treeRootHolder.setRoot(
372 createViewBuilder(PORTFOLIO)
374 createSubView1Builder(null)
376 createProjectView1Builder(project, null).build())
380 underTest.execute(new TestComputationStepContext());
382 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
383 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate()).isFalse());
387 public void persists_new_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
388 boolean isRootPrivate = new Random().nextBoolean();
389 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
390 ComponentDto view = newViewDto().setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
391 dbTester.components().insertComponent(view);
392 treeRootHolder.setRoot(
393 createViewBuilder(PORTFOLIO)
395 createSubView1Builder(null)
397 createProjectView1Builder(project, null).build())
401 underTest.execute(new TestComputationStepContext());
403 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
404 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
405 .describedAs("for uuid " + uuid)
406 .isEqualTo(isRootPrivate));
410 public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
411 boolean isRootPrivate = new Random().nextBoolean();
412 ComponentDto project = dbTester.components().insertComponent(ComponentTesting.newPrivateProjectDto());
413 ComponentDto view = newViewDto().setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
414 dbTester.components().insertComponent(view);
415 ComponentDto subView = newSubPortfolio(view).setUuid("BCDE").setKey("MODULE").setPrivate(!isRootPrivate);
416 dbTester.components().insertComponent(subView);
417 dbTester.components().insertComponent(newProjectCopy("DEFG", project, view).setKey("DIR").setPrivate(isRootPrivate));
418 treeRootHolder.setRoot(
419 createViewBuilder(PORTFOLIO)
421 createSubView1Builder(null)
423 createProjectView1Builder(project, null).build())
427 underTest.execute(new TestComputationStepContext());
429 Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID, subView.uuid(), "DEFG")
430 .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
431 .describedAs("for uuid " + uuid)
432 .isEqualTo(isRootPrivate));
435 private static ViewsComponent.Builder createViewBuilder(ViewAttributes.Type viewType) {
436 return builder(Component.Type.VIEW, VIEW_KEY)
439 .setDescription(VIEW_DESCRIPTION)
440 .setViewAttributes(new ViewAttributes(viewType));
443 private ViewsComponent.Builder createSubView1Builder(@Nullable String originalViewUuid) {
444 return builder(Component.Type.SUBVIEW, SUBVIEW_1_KEY)
445 .setUuid(SUBVIEW_1_UUID)
446 .setName(SUBVIEW_1_NAME)
447 .setDescription(SUBVIEW_1_DESCRIPTION)
448 .setSubViewAttributes(new SubViewAttributes(originalViewUuid));
451 private static ViewsComponent.Builder createProjectView1Builder(ComponentDto project, Long analysisDate) {
452 return builder(Component.Type.PROJECT_VIEW, PROJECT_VIEW_1_KEY)
453 .setUuid(PROJECT_VIEW_1_UUID)
454 .setName(PROJECT_VIEW_1_NAME)
455 .setDescription("project view description is not persisted")
456 .setProjectViewAttributes(new ProjectViewAttributes(project.uuid(), project.getKey(), analysisDate, null));
459 private void persistComponents(ComponentDto... componentDtos) {
460 dbTester.components().insertComponents(componentDtos);
463 private ComponentDto getComponentFromDb(String componentKey) {
464 return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get();
467 private void assertRowsCountInTableComponents(int rowCount) {
468 assertThat(dbTester.countRowsOfTable("components")).isEqualTo(rowCount);
471 private void assertDtoNotUpdated(String componentKey) {
472 assertThat(getComponentFromDb(componentKey).getCreatedAt()).isNotEqualTo(now);
475 private ComponentDto newViewDto() {
476 return ComponentTesting.newPortfolio(VIEW_UUID)
481 private ComponentDto newSubViewDto(ComponentDto rootView) {
482 return ComponentTesting.newSubPortfolio(rootView, SUBVIEW_1_UUID, SUBVIEW_1_KEY)
483 .setName(SUBVIEW_1_NAME);
487 * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()}
489 private void assertDtoIsView(ComponentDto dto) {
490 assertThat(dto.name()).isEqualTo(VIEW_NAME);
491 assertThat(dto.longName()).isEqualTo(VIEW_NAME);
492 assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION);
493 assertThat(dto.path()).isNull();
494 assertThat(dto.uuid()).isEqualTo(VIEW_UUID);
495 assertThat(dto.branchUuid()).isEqualTo(VIEW_UUID);
496 assertThat(dto.qualifier()).isEqualTo(Qualifiers.VIEW);
497 assertThat(dto.scope()).isEqualTo(Scopes.PROJECT);
498 assertThat(dto.getCopyComponentUuid()).isNull();
499 assertThat(dto.getCreatedAt()).isEqualTo(now);
503 * Assertions to verify the DTO created from {@link #createViewBuilder(ViewAttributes.Type)} ()}
505 private void assertDtoIsApplication(ComponentDto dto) {
506 assertThat(dto.name()).isEqualTo(VIEW_NAME);
507 assertThat(dto.longName()).isEqualTo(VIEW_NAME);
508 assertThat(dto.description()).isEqualTo(VIEW_DESCRIPTION);
509 assertThat(dto.path()).isNull();
510 assertThat(dto.uuid()).isEqualTo(VIEW_UUID);
511 assertThat(dto.branchUuid()).isEqualTo(VIEW_UUID);
512 assertThat(dto.qualifier()).isEqualTo(Qualifiers.APP);
513 assertThat(dto.scope()).isEqualTo(Scopes.PROJECT);
514 assertThat(dto.getCopyComponentUuid()).isNull();
515 assertThat(dto.getCreatedAt()).isEqualTo(now);
519 * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto, Long)}
521 private void assertDtoIsSubView1(ComponentDto viewDto, ComponentDto sv1Dto) {
522 assertThat(sv1Dto.name()).isEqualTo(SUBVIEW_1_NAME);
523 assertThat(sv1Dto.longName()).isEqualTo(SUBVIEW_1_NAME);
524 assertThat(sv1Dto.description()).isEqualTo(SUBVIEW_1_DESCRIPTION);
525 assertThat(sv1Dto.path()).isNull();
526 assertThat(sv1Dto.uuid()).isEqualTo(SUBVIEW_1_UUID);
527 assertThat(sv1Dto.branchUuid()).isEqualTo(viewDto.uuid());
528 assertThat(sv1Dto.qualifier()).isEqualTo(Qualifiers.SUBVIEW);
529 assertThat(sv1Dto.scope()).isEqualTo(Scopes.PROJECT);
530 assertThat(sv1Dto.getCopyComponentUuid()).isNull();
531 assertThat(sv1Dto.getCreatedAt()).isEqualTo(now);
534 private void assertDtoIsProjectView1(ComponentDto pv1Dto, ComponentDto viewDto, ComponentDto parentViewDto, ComponentDto project) {
535 assertThat(pv1Dto.name()).isEqualTo(PROJECT_VIEW_1_NAME);
536 assertThat(pv1Dto.longName()).isEqualTo(PROJECT_VIEW_1_NAME);
537 assertThat(pv1Dto.description()).isNull();
538 assertThat(pv1Dto.path()).isNull();
539 assertThat(pv1Dto.uuid()).isEqualTo(PROJECT_VIEW_1_UUID);
540 assertThat(pv1Dto.branchUuid()).isEqualTo(viewDto.uuid());
541 assertThat(pv1Dto.qualifier()).isEqualTo(Qualifiers.PROJECT);
542 assertThat(pv1Dto.scope()).isEqualTo(Scopes.FILE);
543 assertThat(pv1Dto.getCopyComponentUuid()).isEqualTo(project.uuid());
544 assertThat(pv1Dto.getCreatedAt()).isEqualTo(now);