]> source.dussan.org Git - sonarqube.git/blob
67a2285b2f5e21e420263bc63ef113b5c997aae0
[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.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;
41
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;
49
50 public class ExportComponentsStepIT {
51
52   private static final String PROJECT_UUID = "PROJECT_UUID";
53   private static final ComponentDto PROJECT = new ComponentDto()
54     // no id yet
55     .setScope(Scopes.PROJECT)
56     .setQualifier(Qualifiers.PROJECT)
57     .setKey("the_project")
58     .setName("The Project")
59     .setDescription("The project description")
60     .setEnabled(true)
61     .setUuid(PROJECT_UUID)
62     .setUuidPath(UUID_PATH_OF_ROOT)
63     .setCreatedAt(new Date(1596749115856L))
64     .setBranchUuid(PROJECT_UUID);
65
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()
69     // no id yet
70     .setScope(Scopes.FILE)
71     .setQualifier(Qualifiers.FILE)
72     .setKey("the_file")
73     .setName("The File")
74     .setUuid(FILE_UUID)
75     .setUuidPath(FILE_UUID_PATH)
76     .setEnabled(true)
77     .setCreatedAt(new Date(1596749148406L))
78     .setBranchUuid(PROJECT_UUID);
79
80   @Rule
81   public DbTester dbTester = DbTester.create(System2.INSTANCE);
82   @Rule
83   public LogTester logTester = new LogTester();
84
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);
89
90   @Before
91   public void before() {
92     logTester.setLevel(Level.DEBUG);
93   }
94
95   @After
96   public void tearDown() {
97     dbTester.getSession().close();
98   }
99
100   @Test
101   public void export_components_including_project() {
102     dbTester.components().insertPublicProject(PROJECT).getMainBranchComponent();
103     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true);
104     dbTester.commit();
105     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(PROJECT));
106
107     underTest.execute(new TestComputationStepContext());
108
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));
115   }
116
117   @Test
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);
121     dbTester.commit();
122     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(PROJECT));
123
124     underTest.execute(new TestComputationStepContext());
125
126     assertThat(ImmutableSet.of(
127       componentRepository.getRef(PROJECT.uuid()),
128       componentRepository.getRef(FILE.uuid()))).containsExactlyInAnyOrder(1L, 2L);
129   }
130
131   @Test
132   public void throws_ISE_if_error() {
133     dbTester.components().insertPublicProject(PROJECT).getMainBranchComponent();
134     dbTester.getDbClient().componentDao().insert(dbTester.getSession(), FILE, true);
135     dbTester.commit();
136     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(PROJECT));
137     dumpWriter.failIfMoreThan(1, DumpElement.COMPONENTS);
138
139     assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
140       .isInstanceOf(IllegalStateException.class)
141       .hasMessage("Component Export failed after processing 1 components successfully");
142   }
143
144   @Test
145   public void getDescription_is_defined() {
146     assertThat(underTest.getDescription()).isEqualTo("Export components");
147   }
148
149 }