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.projectexport.component;
22 import com.google.common.collect.ImmutableSet;
23 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
24 import java.util.Date;
25 import java.util.List;
26 import org.junit.After;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.resources.Qualifiers;
30 import org.sonar.api.resources.Scopes;
31 import org.sonar.api.utils.System2;
32 import org.sonar.api.utils.log.LogTester;
33 import org.sonar.api.utils.log.LoggerLevel;
34 import org.sonar.ce.task.projectexport.steps.DumpElement;
35 import org.sonar.ce.task.projectexport.steps.FakeDumpWriter;
36 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
37 import org.sonar.ce.task.step.TestComputationStepContext;
38 import org.sonar.db.DbTester;
39 import org.sonar.db.component.ComponentDto;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.assertj.core.api.Assertions.assertThatThrownBy;
43 import static org.assertj.core.api.Assertions.tuple;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
47 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
49 public class ExportComponentsStepIT {
51 private static final String PROJECT_UUID = "PROJECT_UUID";
52 private static final ComponentDto PROJECT = new ComponentDto()
54 .setScope(Scopes.PROJECT)
55 .setQualifier(Qualifiers.PROJECT)
56 .setKey("the_project")
57 .setName("The Project")
58 .setDescription("The project description")
60 .setUuid(PROJECT_UUID)
61 .setUuidPath(UUID_PATH_OF_ROOT)
62 .setCreatedAt(new Date(1596749115856L))
63 .setBranchUuid(PROJECT_UUID);
65 private static final String FILE_UUID = "FILE_UUID";
66 private static final String FILE_UUID_PATH = PROJECT_UUID + FILE_UUID + UUID_PATH_SEPARATOR;
67 private static final ComponentDto FILE = new ComponentDto()
69 .setScope(Scopes.FILE)
70 .setQualifier(Qualifiers.FILE)
74 .setUuidPath(FILE_UUID_PATH)
76 .setCreatedAt(new Date(1596749148406L))
77 .setBranchUuid(PROJECT_UUID);
80 public DbTester dbTester = DbTester.create(System2.INSTANCE);
82 public LogTester logTester = new LogTester();
84 private final FakeDumpWriter dumpWriter = new FakeDumpWriter();
85 private final ProjectHolder projectHolder = mock(ProjectHolder.class);
86 private final MutableComponentRepository componentRepository = new ComponentRepositoryImpl();
87 private final ExportComponentsStep underTest = new ExportComponentsStep(dbTester.getDbClient(), projectHolder, componentRepository, dumpWriter);
90 public void tearDown() {
91 dbTester.getSession().close();
95 public void export_components_including_project() {
96 dbTester.components().insertPublicProject(PROJECT);
97 dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE);
99 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
101 underTest.execute(new TestComputationStepContext());
103 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("2 components exported");
104 List<ProjectDump.Component> components = dumpWriter.getWrittenMessagesOf(DumpElement.COMPONENTS);
105 assertThat(components).extracting(ProjectDump.Component::getQualifier, ProjectDump.Component::getUuid, ProjectDump.Component::getUuidPath)
106 .containsExactlyInAnyOrder(
107 tuple(Qualifiers.FILE, FILE_UUID, FILE_UUID_PATH),
108 tuple(Qualifiers.PROJECT, PROJECT_UUID, UUID_PATH_OF_ROOT));
112 public void execute_register_all_components_uuids_as_their_id_in_ComponentRepository() {
113 dbTester.components().insertPublicProject(PROJECT);
114 dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE);
116 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
118 underTest.execute(new TestComputationStepContext());
120 assertThat(ImmutableSet.of(
121 componentRepository.getRef(PROJECT.uuid()),
122 componentRepository.getRef(FILE.uuid()))).containsExactlyInAnyOrder(1L, 2L);
126 public void throws_ISE_if_error() {
127 dbTester.components().insertPublicProject(PROJECT);
128 dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE);
130 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
131 dumpWriter.failIfMoreThan(1, DumpElement.COMPONENTS);
133 assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
134 .isInstanceOf(IllegalStateException.class)
135 .hasMessage("Component Export failed after processing 1 components successfully");
139 public void getDescription_is_defined() {
140 assertThat(underTest.getDescription()).isEqualTo("Export components");