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.

BranchSupportTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.server.ce.queue;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import java.util.Collections;
  24. import java.util.Map;
  25. import java.util.Random;
  26. import java.util.stream.IntStream;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.junit.runner.RunWith;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.component.BranchDto;
  33. import org.sonar.db.component.ComponentDto;
  34. import org.sonar.db.organization.OrganizationDto;
  35. import org.sonar.server.ce.queue.BranchSupport.ComponentKey;
  36. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  37. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.when;
  41. import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
  42. @RunWith(DataProviderRunner.class)
  43. public class BranchSupportTest {
  44. private static final Map<String, String> NO_CHARACTERISTICS = Collections.emptyMap();
  45. @Rule
  46. public ExpectedException expectedException = ExpectedException.none();
  47. private BranchSupportDelegate branchSupportDelegate = mock(BranchSupportDelegate.class);
  48. private BranchSupport underTestNoBranch = new BranchSupport();
  49. private BranchSupport underTestWithBranch = new BranchSupport(branchSupportDelegate);
  50. @Test
  51. public void createComponentKey_of_main_branch() {
  52. String projectKey = randomAlphanumeric(12);
  53. ComponentKey componentKey = underTestNoBranch.createComponentKey(projectKey, NO_CHARACTERISTICS);
  54. assertThat(componentKey)
  55. .isEqualTo(underTestWithBranch.createComponentKey(projectKey, NO_CHARACTERISTICS));
  56. assertThat(componentKey.getKey()).isEqualTo(projectKey);
  57. assertThat(componentKey.getDbKey()).isEqualTo(projectKey);
  58. assertThat(componentKey.getMainBranchComponentKey()).isSameAs(componentKey);
  59. assertThat(componentKey.getBranch()).isEmpty();
  60. assertThat(componentKey.getPullRequestKey()).isEmpty();
  61. }
  62. @Test
  63. public void createComponentKey_delegates_to_delegate_if_characteristics_is_not_empty() {
  64. String projectKey = randomAlphanumeric(12);
  65. Map<String, String> nonEmptyMap = newRandomNonEmptyMap();
  66. ComponentKey expected = mock(ComponentKey.class);
  67. when(branchSupportDelegate.createComponentKey(projectKey, nonEmptyMap)).thenReturn(expected);
  68. ComponentKey componentKey = underTestWithBranch.createComponentKey(projectKey, nonEmptyMap);
  69. assertThat(componentKey).isSameAs(expected);
  70. }
  71. @Test
  72. public void createBranchComponent_fails_with_ISE_if_delegate_is_null() {
  73. DbSession dbSession = mock(DbSession.class);
  74. ComponentKey componentKey = mock(ComponentKey.class);
  75. OrganizationDto organization = new OrganizationDto();
  76. ComponentDto mainComponentDto = new ComponentDto();
  77. BranchDto mainComponentBranchDto = new BranchDto();
  78. expectedException.expect(IllegalStateException.class);
  79. expectedException.expectMessage("Current edition does not support branch feature");
  80. underTestNoBranch.createBranchComponent(dbSession, componentKey, organization, mainComponentDto, mainComponentBranchDto);
  81. }
  82. @Test
  83. public void createBranchComponent_delegates_to_delegate() {
  84. DbSession dbSession = mock(DbSession.class);
  85. ComponentKey componentKey = mock(ComponentKey.class);
  86. OrganizationDto organization = new OrganizationDto();
  87. ComponentDto mainComponentDto = new ComponentDto();
  88. ComponentDto expected = new ComponentDto();
  89. BranchDto mainComponentBranchDto = new BranchDto();
  90. when(branchSupportDelegate.createBranchComponent(dbSession, componentKey, organization, mainComponentDto, mainComponentBranchDto))
  91. .thenReturn(expected);
  92. ComponentDto dto = underTestWithBranch.createBranchComponent(dbSession, componentKey, organization, mainComponentDto, mainComponentBranchDto);
  93. assertThat(dto).isSameAs(expected);
  94. }
  95. @DataProvider
  96. public static Object[][] nullOrNonEmpty() {
  97. return new Object[][] {
  98. {null},
  99. {randomAlphabetic(5)},
  100. };
  101. }
  102. private static Map<String, String> newRandomNonEmptyMap() {
  103. return IntStream.range(0, 1 + new Random().nextInt(10)).boxed().collect(uniqueIndex(i -> "key_" + i, i -> "val_" + i));
  104. }
  105. }