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.

PersistProjectLinksStepTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  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.ComponentDto;
  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 static org.assertj.core.api.Assertions.assertThat;
  45. import static org.assertj.core.api.Assertions.tuple;
  46. import static org.mockito.Mockito.mock;
  47. import static org.mockito.Mockito.verifyZeroInteractions;
  48. import static org.mockito.Mockito.when;
  49. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.CI;
  50. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.HOME;
  51. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.ISSUE;
  52. import static org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType.SCM;
  53. public class PersistProjectLinksStepTest extends BaseStepTest {
  54. @Rule
  55. public ExpectedException expectedException = ExpectedException.none();
  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 PersistProjectLinksStep underTest = new PersistProjectLinksStep(analysisMetadataHolder, db.getDbClient(), treeRootHolder, reportReader, UuidFactoryFast.getInstance());
  65. @Override
  66. protected ComputationStep step() {
  67. return underTest;
  68. }
  69. @Test
  70. public void no_effect_if_branch_is_not_main() {
  71. DbClient dbClient = mock(DbClient.class);
  72. TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
  73. BatchReportReader reportReader = mock(BatchReportReader.class);
  74. UuidFactory uuidFactory = mock(UuidFactory.class);
  75. mockBranch(false);
  76. PersistProjectLinksStep underTest = new PersistProjectLinksStep(analysisMetadataHolder, dbClient, treeRootHolder, reportReader, uuidFactory);
  77. underTest.execute(new TestComputationStepContext());
  78. verifyZeroInteractions(uuidFactory, reportReader, treeRootHolder, dbClient);
  79. }
  80. @Test
  81. public void add_links_on_project() {
  82. mockBranch(true);
  83. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
  84. // project
  85. reportReader.putComponent(ScannerReport.Component.newBuilder()
  86. .setRef(1)
  87. .setType(ComponentType.PROJECT)
  88. .addChildRef(2)
  89. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("http://www.sonarqube.org").build())
  90. .addLink(ScannerReport.ComponentLink.newBuilder().setType(SCM).setHref("https://github.com/SonarSource/sonar").build())
  91. .addLink(ScannerReport.ComponentLink.newBuilder().setType(ISSUE).setHref("http://jira.sonarsource.com/").build())
  92. .addLink(ScannerReport.ComponentLink.newBuilder().setType(CI).setHref("http://bamboo.ci.codehaus.org/browse/SONAR").build())
  93. .build());
  94. underTest.execute(new TestComputationStepContext());
  95. assertThat(db.getDbClient().projectLinkDao().selectByProjectUuid(db.getSession(), "ABCD"))
  96. .extracting(ProjectLinkDto::getType, ProjectLinkDto::getHref, ProjectLinkDto::getName)
  97. .containsExactlyInAnyOrder(
  98. tuple("homepage", "http://www.sonarqube.org", null),
  99. tuple("scm", "https://github.com/SonarSource/sonar", null),
  100. tuple("issue", "http://jira.sonarsource.com/", null),
  101. tuple("ci", "http://bamboo.ci.codehaus.org/browse/SONAR", null));
  102. }
  103. @Test
  104. public void nothing_to_do_when_link_already_exists() {
  105. mockBranch(true);
  106. ComponentDto project = db.components().insertPrivateProject(p -> p.setUuid("ABCD"));
  107. db.componentLinks().insertProvidedLink(project, l -> l.setType("homepage").setName("Home").setHref("http://www.sonarqube.org"));
  108. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
  109. reportReader.putComponent(ScannerReport.Component.newBuilder()
  110. .setRef(1)
  111. .setType(ComponentType.PROJECT)
  112. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("http://www.sonarqube.org").build())
  113. .build());
  114. underTest.execute(new TestComputationStepContext());
  115. assertThat(db.getDbClient().projectLinkDao().selectByProjectUuid(db.getSession(), "ABCD"))
  116. .extracting(ProjectLinkDto::getType, ProjectLinkDto::getHref)
  117. .containsExactlyInAnyOrder(tuple("homepage", "http://www.sonarqube.org"));
  118. }
  119. @Test
  120. public void do_not_add_links_on_module() {
  121. mockBranch(true);
  122. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
  123. reportReader.putComponent(ScannerReport.Component.newBuilder()
  124. .setRef(1)
  125. .setType(ComponentType.PROJECT)
  126. .addChildRef(2)
  127. .build());
  128. reportReader.putComponent(ScannerReport.Component.newBuilder()
  129. .setRef(2)
  130. .setType(ComponentType.MODULE)
  131. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("http://www.sonarqube.org").build())
  132. .build());
  133. underTest.execute(new TestComputationStepContext());
  134. assertThat(db.countRowsOfTable("project_links")).isZero();
  135. }
  136. @Test
  137. public void do_not_add_links_on_file() {
  138. mockBranch(true);
  139. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").addChildren(
  140. ReportComponent.builder(Component.Type.FILE, 2).setUuid("BCDE").build())
  141. .build());
  142. reportReader.putComponent(ScannerReport.Component.newBuilder()
  143. .setRef(1)
  144. .setType(ComponentType.PROJECT)
  145. .addChildRef(2)
  146. .build());
  147. reportReader.putComponent(ScannerReport.Component.newBuilder()
  148. .setRef(2)
  149. .setType(ComponentType.FILE)
  150. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("http://www.sonarqube.org").build())
  151. .build());
  152. underTest.execute(new TestComputationStepContext());
  153. assertThat(db.countRowsOfTable("project_links")).isZero();
  154. }
  155. @Test
  156. public void update_link() {
  157. mockBranch(true);
  158. ComponentDto project = db.components().insertPrivateProject(p -> p.setUuid("ABCD"));
  159. db.componentLinks().insertProvidedLink(project, l -> l.setType("homepage").setName("Home").setHref("http://www.sonar.org"));
  160. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
  161. reportReader.putComponent(ScannerReport.Component.newBuilder()
  162. .setRef(1)
  163. .setType(ComponentType.PROJECT)
  164. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("http://www.sonarqube.org").build())
  165. .build());
  166. underTest.execute(new TestComputationStepContext());
  167. assertThat(db.getDbClient().projectLinkDao().selectByProjectUuid(db.getSession(), "ABCD"))
  168. .extracting(ProjectLinkDto::getType, ProjectLinkDto::getHref)
  169. .containsExactlyInAnyOrder(tuple("homepage", "http://www.sonarqube.org"));
  170. }
  171. @Test
  172. public void delete_link() {
  173. mockBranch(true);
  174. ComponentDto project = db.components().insertPrivateProject(p -> p.setUuid("ABCD"));
  175. db.componentLinks().insertProvidedLink(project, l -> l.setType("homepage").setName("Home").setHref("http://www.sonar.org"));
  176. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
  177. reportReader.putComponent(ScannerReport.Component.newBuilder()
  178. .setRef(1)
  179. .setType(ComponentType.PROJECT)
  180. .build());
  181. underTest.execute(new TestComputationStepContext());
  182. assertThat(db.countRowsOfTable("project_links")).isZero();
  183. }
  184. @Test
  185. public void not_delete_custom_link() {
  186. mockBranch(true);
  187. ComponentDto project = db.components().insertPrivateProject(p -> p.setUuid("ABCD"));
  188. db.componentLinks().insertCustomLink(project);
  189. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
  190. reportReader.putComponent(ScannerReport.Component.newBuilder()
  191. .setRef(1)
  192. .setType(ComponentType.PROJECT)
  193. .build());
  194. underTest.execute(new TestComputationStepContext());
  195. assertThat(db.countRowsOfTable("project_links")).isEqualTo(1);
  196. }
  197. @Test
  198. public void fail_when_trying_to_add_same_link_type_multiple_times() {
  199. mockBranch(true);
  200. treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
  201. reportReader.putComponent(ScannerReport.Component.newBuilder()
  202. .setRef(1)
  203. .setType(ComponentType.PROJECT)
  204. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("http://www.sonarqube.org").build())
  205. .addLink(ScannerReport.ComponentLink.newBuilder().setType(HOME).setHref("http://www.sonarqube.org").build())
  206. .build());
  207. expectedException.expect(IllegalArgumentException.class);
  208. expectedException.expectMessage("Link of type 'homepage' has already been declared on component 'ABCD'");
  209. underTest.execute(new TestComputationStepContext());
  210. }
  211. private void mockBranch(boolean isMain) {
  212. Branch branch = Mockito.mock(Branch.class);
  213. when(branch.isMain()).thenReturn(isMain);
  214. analysisMetadataHolder.setBranch(branch);
  215. }
  216. }