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.

PersistProjectLinksStepIT.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.projectanalysis.step;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.mockito.Mockito;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  27. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  28. import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
  29. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  30. import org.sonar.ce.task.projectanalysis.component.Component;
  31. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  32. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  33. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  34. import org.sonar.ce.task.step.ComputationStep;
  35. import org.sonar.ce.task.step.TestComputationStepContext;
  36. import org.sonar.core.util.UuidFactory;
  37. import org.sonar.core.util.UuidFactoryFast;
  38. import org.sonar.db.DbClient;
  39. import org.sonar.db.DbTester;
  40. import org.sonar.db.component.ProjectData;
  41. import org.sonar.db.component.ProjectLinkDto;
  42. import org.sonar.scanner.protocol.output.ScannerReport;
  43. import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType;
  44. import org.sonar.server.project.Project;
  45. import static org.assertj.core.api.Assertions.assertThat;
  46. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  47. import static org.assertj.core.api.Assertions.tuple;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.verifyNoInteractions;
  50. import static org.mockito.Mockito.when;
  51. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.CI;
  52. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.HOME;
  53. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.ISSUE;
  54. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.SCM;
  55. public class PersistProjectLinksStepIT extends BaseStepTest {
  56. @Rule
  57. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  58. @Rule
  59. public DbTester db = DbTester.create(System2.INSTANCE);
  60. @Rule
  61. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  62. @Rule
  63. public BatchReportReaderRule reportReader = new BatchReportReaderRule();
  64. private ProjectData project;
  65. private PersistProjectLinksStep underTest = new PersistProjectLinksStep(analysisMetadataHolder, db.getDbClient(), treeRootHolder, reportReader, UuidFactoryFast.getInstance());
  66. @Override
  67. protected ComputationStep step() {
  68. return underTest;
  69. }
  70. @Before
  71. public void setup(){
  72. this.project = db.components().insertPrivateProject();
  73. analysisMetadataHolder.setProject(Project.fromProjectDtoWithTags(project.getProjectDto()));
  74. }
  75. @Test
  76. public void no_effect_if_branch_is_not_main() {
  77. DbClient dbClient = mock(DbClient.class);
  78. TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
  79. BatchReportReader reportReader = mock(BatchReportReader.class);
  80. UuidFactory uuidFactory = mock(UuidFactory.class);
  81. mockBranch(false);
  82. PersistProjectLinksStep underTest = new PersistProjectLinksStep(analysisMetadataHolder, dbClient, treeRootHolder, reportReader, uuidFactory);
  83. underTest.execute(new TestComputationStepContext());
  84. verifyNoInteractions(uuidFactory, reportReader, treeRootHolder, dbClient);
  85. }
  86. @Test
  87. public void add_links_on_project() {
  88. mockBranch(true);
  89. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).build());
  90. // project
  91. reportReader.putComponent(ScannerReport.Component.newBuilder()
  92. .setRef(1)
  93. .setType(ComponentType.PROJECT)
  94. .addChildRef(2)
  95. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("https://www.sonarsource.com/products/sonarqube").build())
  96. .addLink(ScannerReport.ComponentLink.newBuilder().setType(SCM).setHref("https://github.com/SonarSource/sonar").build())
  97. .addLink(ScannerReport.ComponentLink.newBuilder().setType(ISSUE).setHref("http://jira.sonarsource.com/").build())
  98. .addLink(ScannerReport.ComponentLink.newBuilder().setType(CI).setHref("http://bamboo.ci.codehaus.org/browse/SONAR").build())
  99. .build());
  100. underTest.execute(new TestComputationStepContext());
  101. assertThat(db.getDbClient().projectLinkDao().selectByProjectUuid(db.getSession(), project.projectUuid()))
  102. .extracting(ProjectLinkDto::getType, ProjectLinkDto::getHref, ProjectLinkDto::getName)
  103. .containsExactlyInAnyOrder(
  104. tuple("homepage", "https://www.sonarsource.com/products/sonarqube", null),
  105. tuple("scm", "https://github.com/SonarSource/sonar", null),
  106. tuple("issue", "http://jira.sonarsource.com/", null),
  107. tuple("ci", "http://bamboo.ci.codehaus.org/browse/SONAR", null));
  108. }
  109. @Test
  110. public void nothing_to_do_when_link_already_exists() {
  111. mockBranch(true);
  112. db.projectLinks().insertProvidedLink(project.getProjectDto(), l -> l.setType("homepage").setName("Home").setHref("https://www.sonarsource.com/products/sonarqube"));
  113. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).build());
  114. reportReader.putComponent(ScannerReport.Component.newBuilder()
  115. .setRef(1)
  116. .setType(ComponentType.PROJECT)
  117. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("https://www.sonarsource.com/products/sonarqube").build())
  118. .build());
  119. underTest.execute(new TestComputationStepContext());
  120. assertThat(db.getDbClient().projectLinkDao().selectByProjectUuid(db.getSession(), project.projectUuid()))
  121. .extracting(ProjectLinkDto::getType, ProjectLinkDto::getHref)
  122. .containsExactlyInAnyOrder(tuple("homepage", "https://www.sonarsource.com/products/sonarqube"));
  123. }
  124. @Test
  125. public void do_not_add_links_on_module() {
  126. mockBranch(true);
  127. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).build());
  128. reportReader.putComponent(ScannerReport.Component.newBuilder()
  129. .setRef(1)
  130. .setType(ComponentType.PROJECT)
  131. .addChildRef(2)
  132. .build());
  133. reportReader.putComponent(ScannerReport.Component.newBuilder()
  134. .setRef(2)
  135. .setType(ComponentType.MODULE)
  136. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("https://www.sonarsource.com/products/sonarqube").build())
  137. .build());
  138. underTest.execute(new TestComputationStepContext());
  139. assertThat(db.countRowsOfTable("project_links")).isZero();
  140. }
  141. @Test
  142. public void do_not_add_links_on_file() {
  143. mockBranch(true);
  144. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).addChildren(
  145. ReportComponent.builder(Component.Type.FILE, 2).setUuid("BCDE").build())
  146. .build());
  147. reportReader.putComponent(ScannerReport.Component.newBuilder()
  148. .setRef(1)
  149. .setType(ComponentType.PROJECT)
  150. .addChildRef(2)
  151. .build());
  152. reportReader.putComponent(ScannerReport.Component.newBuilder()
  153. .setRef(2)
  154. .setType(ComponentType.FILE)
  155. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("https://www.sonarsource.com/products/sonarqube").build())
  156. .build());
  157. underTest.execute(new TestComputationStepContext());
  158. assertThat(db.countRowsOfTable("project_links")).isZero();
  159. }
  160. @Test
  161. public void update_link() {
  162. mockBranch(true);
  163. analysisMetadataHolder.setProject(Project.fromProjectDtoWithTags(project.getProjectDto()));
  164. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).build());
  165. reportReader.putComponent(ScannerReport.Component.newBuilder()
  166. .setRef(1)
  167. .setType(ComponentType.PROJECT)
  168. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("https://www.sonarsource.com/products/sonarqube").build())
  169. .build());
  170. underTest.execute(new TestComputationStepContext());
  171. assertThat(db.getDbClient().projectLinkDao().selectByProjectUuid(db.getSession(), project.getProjectDto().getUuid()))
  172. .extracting(ProjectLinkDto::getType, ProjectLinkDto::getHref)
  173. .containsExactlyInAnyOrder(tuple("homepage", "https://www.sonarsource.com/products/sonarqube"));
  174. }
  175. @Test
  176. public void delete_link() {
  177. mockBranch(true);
  178. db.projectLinks().insertProvidedLink(project.getProjectDto(), l -> l.setType("homepage").setName("Home").setHref("http://www.sonar.org"));
  179. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).build());
  180. reportReader.putComponent(ScannerReport.Component.newBuilder()
  181. .setRef(1)
  182. .setType(ComponentType.PROJECT)
  183. .build());
  184. underTest.execute(new TestComputationStepContext());
  185. assertThat(db.countRowsOfTable("project_links")).isZero();
  186. }
  187. @Test
  188. public void not_delete_custom_link() {
  189. mockBranch(true);
  190. db.projectLinks().insertCustomLink(project.getProjectDto());
  191. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).build());
  192. reportReader.putComponent(ScannerReport.Component.newBuilder()
  193. .setRef(1)
  194. .setType(ComponentType.PROJECT)
  195. .build());
  196. underTest.execute(new TestComputationStepContext());
  197. assertThat(db.countRowsOfTable("project_links")).isOne();
  198. }
  199. @Test
  200. public void fail_when_trying_to_add_same_link_type_multiple_times() {
  201. mockBranch(true);
  202. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.getMainBranchComponent().uuid()).build());
  203. reportReader.putComponent(ScannerReport.Component.newBuilder()
  204. .setRef(1)
  205. .setType(ComponentType.PROJECT)
  206. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("https://www.sonarsource.com/products/sonarqube").build())
  207. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("https://www.sonarsource.com/products/sonarqube").build())
  208. .build());
  209. assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
  210. .isInstanceOf(IllegalArgumentException.class)
  211. .hasMessage("Link of type 'homepage' has already been declared on component '%s'".formatted(project.projectUuid()));
  212. }
  213. private void mockBranch(boolean isMain) {
  214. Branch branch = Mockito.mock(Branch.class);
  215. when(branch.isMain()).thenReturn(isMain);
  216. analysisMetadataHolder.setBranch(branch);
  217. }
  218. }