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.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.slf4j.event.Level;
31 import org.sonar.api.resources.Qualifiers;
32 import org.sonar.api.resources.Scopes;
33 import org.sonar.api.testfixtures.log.LogTester;
34 import org.sonar.api.utils.System2;
35 import org.sonar.ce.task.projectexport.steps.DumpElement;
36 import org.sonar.ce.task.projectexport.steps.FakeDumpWriter;
37 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
38 import org.sonar.ce.task.step.TestComputationStepContext;
39 import org.sonar.db.DbTester;
40 import org.sonar.db.component.ComponentDto;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.assertj.core.api.Assertions.tuple;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
48 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
50 public class ExportComponentsStepIT {
52 private static final String PROJECT_UUID = "PROJECT_UUID";
53 private static final ComponentDto PROJECT = new ComponentDto()
55 .setScope(Scopes.PROJECT)
56 .setQualifier(Qualifiers.PROJECT)
57 .setKey("the_project")
58 .setName("The Project")
59 .setDescription("The project description")
61 .setUuid(PROJECT_UUID)
62 .setUuidPath(UUID_PATH_OF_ROOT)
63 .setCreatedAt(new Date(1596749115856L))
64 .setBranchUuid(PROJECT_UUID);
66 private static final String FILE_UUID = "FILE_UUID";
67 private static final String FILE_UUID_PATH = PROJECT_UUID + FILE_UUID + UUID_PATH_SEPARATOR;
68 private static final ComponentDto FILE = new ComponentDto()
70 .setScope(Scopes.FILE)
71 .setQualifier(Qualifiers.FILE)
75 .setUuidPath(FILE_UUID_PATH)
77 .setCreatedAt(new Date(1596749148406L))
78 .setBranchUuid(PROJECT_UUID);
81 public DbTester dbTester = DbTester.create(System2.INSTANCE);
83 public LogTester logTester = new LogTester();
85 private final FakeDumpWriter dumpWriter = new FakeDumpWriter();
86 private final ProjectHolder projectHolder = mock(ProjectHolder.class);
87 private final MutableComponentRepository componentRepository = new ComponentRepositoryImpl();
88 private final ExportComponentsStep underTest = new ExportComponentsStep(dbTester.getDbClient(), projectHolder, componentRepository, dumpWriter);
91 public void before() {
92 logTester.setLevel(Level.DEBUG);
96 public void tearDown() {
97 dbTester.getSession().close();
101 public void export_components_including_project() {
102 dbTester.components().insertPublicProject(PROJECT).getMainBranchComponent();
103 dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true);
105 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(PROJECT));
107 underTest.execute(new TestComputationStepContext());
109 assertThat(logTester.logs(Level.DEBUG)).contains("2 components exported");
110 List<ProjectDump.Component> components = dumpWriter.getWrittenMessagesOf(DumpElement.COMPONENTS);
111 assertThat(components).extracting(ProjectDump.Component::getQualifier, ProjectDump.Component::getUuid, ProjectDump.Component::getUuidPath)
112 .containsExactlyInAnyOrder(
113 tuple(Qualifiers.FILE, FILE_UUID, FILE_UUID_PATH),
114 tuple(Qualifiers.PROJECT, PROJECT_UUID, UUID_PATH_OF_ROOT));
118 public void execute_register_all_components_uuids_as_their_id_in_ComponentRepository() {
119 dbTester.components().insertPublicProject(PROJECT).getMainBranchComponent();
120 dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true);
122 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(PROJECT));
124 underTest.execute(new TestComputationStepContext());
126 assertThat(ImmutableSet.of(
127 componentRepository.getRef(PROJECT.uuid()),
128 componentRepository.getRef(FILE.uuid()))).containsExactlyInAnyOrder(1L, 2L);
132 public void throws_ISE_if_error() {
133 dbTester.components().insertPublicProject(PROJECT).getMainBranchComponent();
134 dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true);
136 when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(PROJECT));
137 dumpWriter.failIfMoreThan(1, DumpElement.COMPONENTS);
139 assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
140 .isInstanceOf(IllegalStateException.class)
141 .hasMessage("Component Export failed after processing 1 components successfully");
145 public void getDescription_is_defined() {
146 assertThat(underTest.getDescription()).isEqualTo("Export components");