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

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