You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExportAnalysesStepIT.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.analysis;
  21. import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
  22. import com.tngtech.java.junit.dataprovider.DataProvider;
  23. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  24. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  25. import java.util.List;
  26. import java.util.Set;
  27. import javax.annotation.Nullable;
  28. import org.junit.Before;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.slf4j.event.Level;
  33. import org.sonar.api.resources.Qualifiers;
  34. import org.sonar.api.resources.Scopes;
  35. import org.sonar.api.testfixtures.log.LogTester;
  36. import org.sonar.api.utils.System2;
  37. import org.sonar.ce.task.projectexport.component.ComponentRepositoryImpl;
  38. import org.sonar.ce.task.projectexport.steps.DumpElement;
  39. import org.sonar.ce.task.projectexport.steps.FakeDumpWriter;
  40. import org.sonar.ce.task.projectexport.steps.ProjectHolder;
  41. import org.sonar.ce.task.step.TestComputationStepContext;
  42. import org.sonar.db.DbTester;
  43. import org.sonar.db.component.ComponentDto;
  44. import org.sonar.db.component.SnapshotDto;
  45. import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
  46. import static org.apache.commons.lang3.StringUtils.defaultString;
  47. import static org.assertj.core.api.Assertions.assertThat;
  48. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  49. import static org.mockito.Mockito.mock;
  50. import static org.mockito.Mockito.when;
  51. import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
  52. import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
  53. @RunWith(DataProviderRunner.class)
  54. public class ExportAnalysesStepIT {
  55. private static final String PROJECT_UUID = "PROJECT_UUID";
  56. private static final ComponentDto PROJECT = new ComponentDto()
  57. // no id yet
  58. .setScope(Scopes.PROJECT)
  59. .setQualifier(Qualifiers.PROJECT)
  60. .setKey("the_project")
  61. .setName("The Project")
  62. .setDescription("The project description")
  63. .setEnabled(true)
  64. .setUuid(PROJECT_UUID)
  65. .setUuidPath(UUID_PATH_OF_ROOT)
  66. .setBranchUuid(PROJECT_UUID);
  67. private static final String DIR_UUID = "DIR_UUID";
  68. private static final String UUID_PATH = UUID_PATH_OF_ROOT + UUID_PATH_SEPARATOR + DIR_UUID;
  69. private static final ComponentDto DIR = new ComponentDto()
  70. // no id yet
  71. .setScope(Scopes.PROJECT)
  72. .setQualifier(Qualifiers.DIRECTORY)
  73. .setKey("the_dir")
  74. .setName("The Dir")
  75. .setDescription("description of dir")
  76. .setEnabled(true)
  77. .setUuid(DIR_UUID)
  78. .setUuidPath(UUID_PATH)
  79. .setBranchUuid(PROJECT_UUID);
  80. private static final String FILE_UUID = "FILE_UUID";
  81. private static final ComponentDto FILE = new ComponentDto()
  82. // no id yet
  83. .setScope(Scopes.FILE)
  84. .setQualifier(Qualifiers.FILE)
  85. .setKey("the_file")
  86. .setName("The File")
  87. .setUuid(FILE_UUID)
  88. .setUuidPath(UUID_PATH + UUID_PATH_SEPARATOR + FILE_UUID)
  89. .setEnabled(true)
  90. .setBranchUuid(PROJECT_UUID);
  91. @Rule
  92. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  93. @Rule
  94. public LogTester logTester = new LogTester();
  95. private final ComponentRepositoryImpl componentRepository = new ComponentRepositoryImpl();
  96. private final FakeDumpWriter dumpWriter = new FakeDumpWriter();
  97. private final ProjectHolder projectHolder = mock(ProjectHolder.class);
  98. private final ExportAnalysesStep underTest = new ExportAnalysesStep(dbTester.getDbClient(), projectHolder, componentRepository, dumpWriter);
  99. @Before
  100. public void setUp() {
  101. logTester.setLevel(Level.DEBUG);
  102. ComponentDto projectDto = dbTester.components().insertPublicProject(PROJECT).getMainBranchComponent();
  103. componentRepository.register(1, projectDto.uuid(), false);
  104. dbTester.getDbClient().componentDao().insert(dbTester.getSession(), Set.of(DIR, FILE), true);
  105. dbTester.commit();
  106. when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDtoByMainBranch(projectDto));
  107. }
  108. @Test
  109. public void getDescription_is_defined() {
  110. assertThat(underTest.getDescription()).isEqualTo("Export analyses");
  111. }
  112. @Test
  113. @UseDataProvider("versionAndBuildStringCombinations")
  114. public void export_analyses(@Nullable String version, @Nullable String buildString) {
  115. SnapshotDto firstAnalysis = newAnalysis("U_1", 1_450_000_000_000L, PROJECT.uuid(), "1.0", false, "1.0.2.3", 1_450_000_000_000L);
  116. SnapshotDto secondAnalysis = newAnalysis("U_4", 1_460_000_000_000L, PROJECT.uuid(), "1.1", true, "1.1.3.4", 1_460_000_000_000L);
  117. SnapshotDto thirdAnalysis = newAnalysis("U_7", 1_460_000_000_000L, PROJECT.uuid(), version, true, buildString, 1_470_000_000_000L);
  118. dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), firstAnalysis, secondAnalysis, thirdAnalysis);
  119. dbTester.commit();
  120. underTest.execute(new TestComputationStepContext());
  121. assertThat(logTester.logs(Level.DEBUG)).contains("3 analyses exported");
  122. List<ProjectDump.Analysis> analyses = dumpWriter.getWrittenMessagesOf(DumpElement.ANALYSES);
  123. assertThat(analyses).hasSize(3);
  124. assertAnalysis(analyses.get(0), PROJECT, firstAnalysis);
  125. assertAnalysis(analyses.get(1), PROJECT, secondAnalysis);
  126. assertAnalysis(analyses.get(2), PROJECT, thirdAnalysis);
  127. }
  128. @DataProvider
  129. public static Object[][] versionAndBuildStringCombinations() {
  130. String version = randomAlphabetic(7);
  131. String buildString = randomAlphabetic(12);
  132. return new Object[][] {
  133. {null, null},
  134. {version, null},
  135. {null, buildString},
  136. {version, buildString},
  137. {"", ""},
  138. {version, ""},
  139. {"", buildString},
  140. };
  141. }
  142. @Test
  143. public void export_analyses_by_ordering_by_technical_creation_date() {
  144. SnapshotDto firstAnalysis = newAnalysis("U_1", 1_450_000_000_000L, PROJECT.uuid(), "1.0", false, "1.0.2.3", 3_000_000_000_000L);
  145. SnapshotDto secondAnalysis = newAnalysis("U_4", 1_460_000_000_000L, PROJECT.uuid(), "1.1", true, "1.1.3.4", 1_000_000_000_000L);
  146. SnapshotDto thirdAnalysis = newAnalysis("U_7", 1_460_500_000_000L, PROJECT.uuid(), null, true, null, 2_000_000_000_000L);
  147. dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), firstAnalysis, secondAnalysis, thirdAnalysis);
  148. dbTester.commit();
  149. underTest.execute(new TestComputationStepContext());
  150. List<ProjectDump.Analysis> analyses = dumpWriter.getWrittenMessagesOf(DumpElement.ANALYSES);
  151. assertAnalysis(analyses.get(0), PROJECT, secondAnalysis);
  152. assertAnalysis(analyses.get(1), PROJECT, thirdAnalysis);
  153. assertAnalysis(analyses.get(2), PROJECT, firstAnalysis);
  154. }
  155. @Test
  156. public void export_provisioned_projects_without_any_analyses() {
  157. underTest.execute(new TestComputationStepContext());
  158. List<ProjectDump.Analysis> analyses = dumpWriter.getWrittenMessagesOf(DumpElement.ANALYSES);
  159. assertThat(analyses).isEmpty();
  160. assertThat(logTester.logs(Level.DEBUG)).contains("0 analyses exported");
  161. }
  162. @Test
  163. public void throws_ISE_if_error() {
  164. SnapshotDto firstAnalysis = newAnalysis("U_1", 1_450_000_000_000L, PROJECT.uuid(), "1.0", false, "1.0.2.3", 1);
  165. SnapshotDto secondAnalysis = newAnalysis("U_4", 1_460_000_000_000L, PROJECT.uuid(), "1.1", true, "1.1.3.4", 2);
  166. dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), firstAnalysis, secondAnalysis);
  167. dbTester.commit();
  168. dumpWriter.failIfMoreThan(1, DumpElement.ANALYSES);
  169. assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
  170. .isInstanceOf(IllegalStateException.class)
  171. .hasMessage("Analysis Export failed after processing 1 analyses successfully");
  172. }
  173. private static SnapshotDto newAnalysis(String uuid, long date, String componentUuid, @Nullable String version, boolean isLast, @Nullable String buildString, long analysisDate) {
  174. return new SnapshotDto()
  175. .setUuid(uuid)
  176. .setCreatedAt(date)
  177. .setRootComponentUuid(componentUuid)
  178. .setProjectVersion(version)
  179. .setBuildString(buildString)
  180. .setLast(isLast)
  181. .setStatus(SnapshotDto.STATUS_PROCESSED)
  182. .setAnalysisDate(analysisDate);
  183. }
  184. private static void assertAnalysis(ProjectDump.Analysis analysis, ComponentDto component, SnapshotDto dto) {
  185. assertThat(analysis.getUuid()).isEqualTo(dto.getUuid());
  186. assertThat(analysis.getComponentRef()).isOne();
  187. assertThat(analysis.getDate()).isEqualTo(dto.getCreatedAt());
  188. assertThat(analysis.getProjectVersion()).isEqualTo(defaultString(dto.getProjectVersion()));
  189. assertThat(analysis.getBuildString()).isEqualTo(defaultString(dto.getBuildString()));
  190. }
  191. }