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 17KB

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