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.

AnalysisMetadataHolderImplTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.analysis;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Arrays;
  25. import java.util.Optional;
  26. import java.util.stream.Stream;
  27. import javax.annotation.Nullable;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
  31. import org.sonar.core.platform.PlatformEditionProvider;
  32. import org.sonar.db.component.BranchType;
  33. import org.sonar.server.project.Project;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.when;
  38. import static org.sonar.core.platform.EditionProvider.Edition;
  39. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  40. @RunWith(DataProviderRunner.class)
  41. public class AnalysisMetadataHolderImplTest {
  42. private static final Analysis baseProjectAnalysis = new Analysis.Builder()
  43. .setUuid("uuid_1")
  44. .setCreatedAt(123456789L)
  45. .build();
  46. private static final long SOME_DATE = 10000000L;
  47. private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
  48. private final AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  49. @Test
  50. public void setUuid_throws_NPE_is_parameter_is_null() {
  51. assertThatThrownBy(() -> underTest.setUuid(null))
  52. .isInstanceOf(NullPointerException.class)
  53. .hasMessage("Analysis uuid can't be null");
  54. }
  55. @Test
  56. public void setUuid_throws_ISE_if_called_twice() {
  57. underTest.setUuid("org1");
  58. assertThatThrownBy(() -> underTest.setUuid("org1"))
  59. .isInstanceOf(IllegalStateException.class)
  60. .hasMessage("Analysis uuid has already been set");
  61. }
  62. @Test
  63. public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() {
  64. underTest.setAnalysisDate(SOME_DATE);
  65. assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
  66. }
  67. @Test
  68. public void getAnalysisDate_throws_ISE_when_holder_is_not_initialized() {
  69. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getAnalysisDate())
  70. .isInstanceOf(IllegalStateException.class)
  71. .hasMessage("Analysis date has not been set");
  72. }
  73. @Test
  74. public void setAnalysisDate_throws_ISE_when_called_twice() {
  75. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  76. underTest.setAnalysisDate(SOME_DATE);
  77. assertThatThrownBy(() -> underTest.setAnalysisDate(SOME_DATE))
  78. .isInstanceOf(IllegalStateException.class)
  79. .hasMessage("Analysis date has already been set");
  80. }
  81. @Test
  82. public void getForkDate_returns_date_with_same_time_as_the_one_set_with_setForkDate() {
  83. underTest.setForkDate(SOME_DATE);
  84. assertThat(underTest.getForkDate()).isEqualTo(SOME_DATE);
  85. }
  86. @Test
  87. public void getForkDate_throws_ISE_when_holder_is_not_initialized() {
  88. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getForkDate())
  89. .isInstanceOf(IllegalStateException.class)
  90. .hasMessage("Fork date has not been set");
  91. }
  92. @Test
  93. public void setForkDate_throws_ISE_when_called_twice() {
  94. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  95. underTest.setForkDate(SOME_DATE);
  96. assertThatThrownBy(() -> underTest.setForkDate(SOME_DATE))
  97. .isInstanceOf(IllegalStateException.class)
  98. .hasMessage("Fork date has already been set");
  99. }
  100. @Test
  101. public void hasAnalysisDateBeenSet_returns_false_when_holder_is_not_initialized() {
  102. assertThat(new AnalysisMetadataHolderImpl(editionProvider).hasAnalysisDateBeenSet()).isFalse();
  103. }
  104. @Test
  105. public void hasAnalysisDateBeenSet_returns_true_when_holder_date_is_set() {
  106. AnalysisMetadataHolderImpl holder = new AnalysisMetadataHolderImpl(editionProvider);
  107. holder.setAnalysisDate(46532);
  108. assertThat(holder.hasAnalysisDateBeenSet()).isTrue();
  109. }
  110. @Test
  111. public void isFirstAnalysis_return_true() {
  112. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  113. underTest.setBaseAnalysis(null);
  114. assertThat(underTest.isFirstAnalysis()).isTrue();
  115. }
  116. @Test
  117. public void isFirstAnalysis_return_false() {
  118. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  119. underTest.setBaseAnalysis(baseProjectAnalysis);
  120. assertThat(underTest.isFirstAnalysis()).isFalse();
  121. }
  122. @Test
  123. public void isFirstAnalysis_throws_ISE_when_base_project_snapshot_is_not_set() {
  124. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).isFirstAnalysis())
  125. .isInstanceOf(IllegalStateException.class)
  126. .hasMessage("Base project snapshot has not been set");
  127. }
  128. @Test
  129. public void baseProjectSnapshot_throws_ISE_when_base_project_snapshot_is_not_set() {
  130. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getBaseAnalysis())
  131. .isInstanceOf(IllegalStateException.class)
  132. .hasMessage("Base project snapshot has not been set");
  133. }
  134. @Test
  135. public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
  136. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  137. underTest.setBaseAnalysis(baseProjectAnalysis);
  138. assertThatThrownBy(() -> underTest.setBaseAnalysis(baseProjectAnalysis))
  139. .isInstanceOf(IllegalStateException.class)
  140. .hasMessage("Base project snapshot has already been set");
  141. }
  142. @Test
  143. public void isCrossProjectDuplicationEnabled_return_true() {
  144. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  145. underTest.setCrossProjectDuplicationEnabled(true);
  146. assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(true);
  147. }
  148. @Test
  149. public void isCrossProjectDuplicationEnabled_return_false() {
  150. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  151. underTest.setCrossProjectDuplicationEnabled(false);
  152. assertThat(underTest.isCrossProjectDuplicationEnabled()).isEqualTo(false);
  153. }
  154. @Test
  155. public void isCrossProjectDuplicationEnabled_throws_ISE_when_holder_is_not_initialized() {
  156. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).isCrossProjectDuplicationEnabled())
  157. .isInstanceOf(IllegalStateException.class)
  158. .hasMessage("Cross project duplication flag has not been set");
  159. }
  160. @Test
  161. public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
  162. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  163. underTest.setCrossProjectDuplicationEnabled(true);
  164. assertThatThrownBy(() -> underTest.setCrossProjectDuplicationEnabled(false))
  165. .isInstanceOf(IllegalStateException.class)
  166. .hasMessage("Cross project duplication flag has already been set");
  167. }
  168. @Test
  169. public void set_branch() {
  170. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  171. underTest.setBranch(new DefaultBranchImpl());
  172. assertThat(underTest.getBranch().getName()).isEqualTo("master");
  173. }
  174. @Test
  175. public void getBranch_throws_ISE_when_holder_is_not_initialized() {
  176. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getBranch())
  177. .isInstanceOf(IllegalStateException.class)
  178. .hasMessage("Branch has not been set");
  179. }
  180. @Test
  181. public void setBranch_throws_ISE_when_called_twice() {
  182. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  183. underTest.setBranch(new DefaultBranchImpl());
  184. assertThatThrownBy(() -> underTest.setBranch(new DefaultBranchImpl()))
  185. .isInstanceOf(IllegalStateException.class)
  186. .hasMessage("Branch has already been set");
  187. }
  188. @Test
  189. @UseDataProvider("anyEditionIncludingNone")
  190. public void setBranch_does_not_fail_if_main_branch_on_any_edition(@Nullable Edition edition) {
  191. when(editionProvider.get()).thenReturn(Optional.ofNullable(edition));
  192. Branch branch = mock(Branch.class);
  193. when(branch.isMain()).thenReturn(true);
  194. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  195. underTest.setBranch(branch);
  196. assertThat(underTest.getBranch()).isSameAs(branch);
  197. }
  198. @Test
  199. @UseDataProvider("anyEditionIncludingNoneButCommunity")
  200. public void setBranch_does_not_fail_if_non_main_on_any_edition_but_Community(@Nullable Edition edition) {
  201. when(editionProvider.get()).thenReturn(Optional.ofNullable(edition));
  202. Branch branch = mock(Branch.class);
  203. when(branch.isMain()).thenReturn(false);
  204. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  205. underTest.setBranch(branch);
  206. assertThat(underTest.getBranch()).isSameAs(branch);
  207. }
  208. @Test
  209. public void setBranch_fails_if_non_main_branch_on_Community_edition() {
  210. when(editionProvider.get()).thenReturn(Optional.of(Edition.COMMUNITY));
  211. Branch branch = mock(Branch.class);
  212. when(branch.isMain()).thenReturn(false);
  213. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  214. assertThatThrownBy(() -> underTest.setBranch(branch))
  215. .isInstanceOf(IllegalStateException.class)
  216. .hasMessage("Branches and Pull Requests are not supported in Community Edition");
  217. }
  218. @DataProvider
  219. public static Object[][] anyEditionIncludingNone() {
  220. return Stream.concat(
  221. Stream.of((Edition) null),
  222. Arrays.stream(Edition.values()))
  223. .map(t -> new Object[] {t})
  224. .toArray(Object[][]::new);
  225. }
  226. @DataProvider
  227. public static Object[][] anyEditionIncludingNoneButCommunity() {
  228. return Stream.concat(
  229. Stream.of((Edition) null),
  230. Arrays.stream(Edition.values())).filter(t -> t != Edition.COMMUNITY)
  231. .map(t -> new Object[] {t})
  232. .toArray(Object[][]::new);
  233. }
  234. @Test
  235. public void setPullRequestId() {
  236. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  237. String pullRequestId = "pr-123";
  238. underTest.setPullRequestKey(pullRequestId);
  239. assertThat(underTest.getPullRequestKey()).isEqualTo(pullRequestId);
  240. }
  241. @Test
  242. public void getPullRequestId_throws_ISE_when_holder_is_not_initialized() {
  243. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getPullRequestKey())
  244. .isInstanceOf(IllegalStateException.class)
  245. .hasMessage("Pull request key has not been set");
  246. }
  247. @Test
  248. public void setPullRequestId_throws_ISE_when_called_twice() {
  249. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  250. underTest.setPullRequestKey("pr-123");
  251. assertThatThrownBy(() -> underTest.setPullRequestKey("pr-234"))
  252. .isInstanceOf(IllegalStateException.class)
  253. .hasMessage("Pull request key has already been set");
  254. }
  255. @Test
  256. public void set_and_get_project() {
  257. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  258. Project project = Project.from(newPrivateProjectDto());
  259. underTest.setProject(project);
  260. assertThat(underTest.getProject()).isSameAs(project);
  261. }
  262. @Test
  263. public void getProject_throws_ISE_when_holder_is_not_initialized() {
  264. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getProject())
  265. .isInstanceOf(IllegalStateException.class)
  266. .hasMessage("Project has not been set");
  267. }
  268. @Test
  269. public void setProject_throws_ISE_when_called_twice() {
  270. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  271. underTest.setProject(Project.from(newPrivateProjectDto()));
  272. assertThatThrownBy(() -> underTest.setProject(Project.from(newPrivateProjectDto())))
  273. .isInstanceOf(IllegalStateException.class)
  274. .hasMessage("Project has already been set");
  275. }
  276. @Test
  277. public void getRootComponentRef() {
  278. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  279. underTest.setRootComponentRef(10);
  280. assertThat(underTest.getRootComponentRef()).isEqualTo(10);
  281. }
  282. @Test
  283. public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() {
  284. assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getRootComponentRef())
  285. .isInstanceOf(IllegalStateException.class)
  286. .hasMessage("Root component ref has not been set");
  287. }
  288. @Test
  289. public void setRootComponentRef_throws_ISE_when_called_twice() {
  290. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  291. underTest.setRootComponentRef(10);
  292. assertThatThrownBy(() -> underTest.setRootComponentRef(9))
  293. .isInstanceOf(IllegalStateException.class)
  294. .hasMessage("Root component ref has already been set");
  295. }
  296. @Test
  297. public void getPullRequestBranch_returns_true() {
  298. Branch branch = mock(Branch.class);
  299. when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
  300. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  301. underTest.setBranch(branch);
  302. assertThat(underTest.isPullRequest()).isTrue();
  303. }
  304. @Test
  305. public void setScmRevision_throws_ISE_when_called_twice() {
  306. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  307. underTest.setScmRevision("bd56dab");
  308. assertThatThrownBy(() -> underTest.setScmRevision("bd56dab"))
  309. .isInstanceOf(IllegalStateException.class)
  310. .hasMessage("ScmRevision has already been set");
  311. }
  312. @Test
  313. public void getScmRevision_returns_empty_if_scmRevision_is_not_initialized() {
  314. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  315. assertThat(underTest.getScmRevision()).isNotPresent();
  316. }
  317. @Test
  318. public void getScmRevision_returns_scmRevision_if_scmRevision_is_initialized() {
  319. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  320. underTest.setScmRevision("bd56dab");
  321. assertThat(underTest.getScmRevision()).hasValue("bd56dab");
  322. }
  323. @Test
  324. public void getScmRevision_does_not_return_empty_string() {
  325. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  326. underTest.setScmRevision("");
  327. assertThat(underTest.getScmRevision()).isEmpty();
  328. }
  329. @Test
  330. public void getScmRevision_does_not_return_blank_string() {
  331. AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
  332. underTest.setScmRevision(" ");
  333. assertThat(underTest.getScmRevision()).isEmpty();
  334. }
  335. @Test
  336. public void isBranch_returns_true_for_initialized_branch() {
  337. Branch branch = mock(Branch.class);
  338. when(branch.getType()).thenReturn(BranchType.BRANCH);
  339. underTest.setBranch(branch);
  340. assertThat(underTest.isBranch()).isTrue();
  341. }
  342. @Test
  343. public void isBranch_returns_false_for_pr() {
  344. Branch branch = mock(Branch.class);
  345. when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
  346. underTest.setBranch(branch);
  347. assertThat(underTest.isBranch()).isFalse();
  348. }
  349. @Test
  350. public void isBranch_throws_ISE_for_not_initialized_branch() {
  351. assertThatThrownBy(underTest::isBranch)
  352. .isInstanceOf(IllegalStateException.class)
  353. .hasMessage("Branch has not been set");
  354. }
  355. }