]> source.dussan.org Git - sonarqube.git/blob
4b3d12f2be80a4f5943e2a2d1b606bfac3b43dd1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectexport.component;
21
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;
40
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;
48
49 public class ExportComponentsStepIT {
50
51   private static final String PROJECT_UUID = "PROJECT_UUID";
52   private static final ComponentDto PROJECT = new ComponentDto()
53     // no id yet
54     .setScope(Scopes.PROJECT)
55     .setQualifier(Qualifiers.PROJECT)
56     .setKey("the_project")
57     .setName("The Project")
58     .setDescription("The project description")
59     .setEnabled(true)
60     .setUuid(PROJECT_UUID)
61     .setUuidPath(UUID_PATH_OF_ROOT)
62     .setCreatedAt(new Date(1596749115856L))
63     .setBranchUuid(PROJECT_UUID);
64
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()
68     // no id yet
69     .setScope(Scopes.FILE)
70     .setQualifier(Qualifiers.FILE)
71     .setKey("the_file")
72     .setName("The File")
73     .setUuid(FILE_UUID)
74     .setUuidPath(FILE_UUID_PATH)
75     .setEnabled(true)
76     .setCreatedAt(new Date(1596749148406L))
77     .setBranchUuid(PROJECT_UUID);
78
79   @Rule
80   public DbTester dbTester = DbTester.create(System2.INSTANCE);
81   @Rule
82   public LogTester logTester = new LogTester();
83
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);
88
89   @After
90   public void tearDown() {
91     dbTester.getSession().close();
92   }
93
94   @Test
95   public void export_components_including_project() {
96     dbTester.components().insertPublicProject(PROJECT);
97     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE);
98     dbTester.commit();
99     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
100
101     underTest.execute(new TestComputationStepContext());
102
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));
109   }
110
111   @Test
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);
115     dbTester.commit();
116     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
117
118     underTest.execute(new TestComputationStepContext());
119
120     assertThat(ImmutableSet.of(
121       componentRepository.getRef(PROJECT.uuid()),
122       componentRepository.getRef(FILE.uuid()))).containsExactlyInAnyOrder(1L, 2L);
123   }
124
125   @Test
126   public void throws_ISE_if_error() {
127     dbTester.components().insertPublicProject(PROJECT);
128     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE);
129     dbTester.commit();
130     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(PROJECT));
131     dumpWriter.failIfMoreThan(1, DumpElement.COMPONENTS);
132
133     assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
134       .isInstanceOf(IllegalStateException.class)
135       .hasMessage("Component Export failed after processing 1 components successfully");
136   }
137
138   @Test
139   public void getDescription_is_defined() {
140     assertThat(underTest.getDescription()).isEqualTo("Export components");
141   }
142
143 }