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.

ReferenceBranchSupplierTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.scanner.repository;
  21. import java.nio.file.Path;
  22. import java.nio.file.Paths;
  23. import java.time.Instant;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  27. import org.sonar.api.batch.scm.ScmProvider;
  28. import org.sonar.api.notifications.AnalysisWarnings;
  29. import org.sonar.scanner.scan.branch.BranchConfiguration;
  30. import org.sonar.scanner.scan.branch.BranchType;
  31. import org.sonar.scanner.scan.branch.ProjectBranches;
  32. import org.sonar.scanner.scm.ScmConfiguration;
  33. import org.sonarqube.ws.NewCodePeriods;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.ArgumentMatchers.anyString;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.times;
  38. import static org.mockito.Mockito.verify;
  39. import static org.mockito.Mockito.verifyNoInteractions;
  40. import static org.mockito.Mockito.verifyNoMoreInteractions;
  41. import static org.mockito.Mockito.when;
  42. public class ReferenceBranchSupplierTest {
  43. private final static String PROJECT_KEY = "project";
  44. private final static String BRANCH_KEY = "branch";
  45. private final static Path BASE_DIR = Paths.get("root");
  46. private final NewCodePeriodLoader newCodePeriodLoader = mock(NewCodePeriodLoader.class);
  47. private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  48. private final DefaultInputProject project = mock(DefaultInputProject.class);
  49. private final ProjectBranches projectBranches = mock(ProjectBranches.class);
  50. private final ReferenceBranchSupplier referenceBranchSupplier = new ReferenceBranchSupplier(newCodePeriodLoader, branchConfiguration, project, projectBranches);
  51. @Before
  52. public void setUp() {
  53. when(projectBranches.isEmpty()).thenReturn(false);
  54. when(project.key()).thenReturn(PROJECT_KEY);
  55. when(project.getBaseDir()).thenReturn(BASE_DIR);
  56. }
  57. @Test
  58. public void returns_reference_branch_when_set() {
  59. when(branchConfiguration.branchType()).thenReturn(BranchType.BRANCH);
  60. when(branchConfiguration.branchName()).thenReturn(BRANCH_KEY);
  61. when(newCodePeriodLoader.load(PROJECT_KEY, BRANCH_KEY)).thenReturn(createResponse(NewCodePeriods.NewCodePeriodType.REFERENCE_BRANCH, "master"));
  62. assertThat(referenceBranchSupplier.get()).isEqualTo("master");
  63. }
  64. @Test
  65. public void uses_default_branch_if_no_branch_specified() {
  66. when(branchConfiguration.branchType()).thenReturn(BranchType.BRANCH);
  67. when(branchConfiguration.branchName()).thenReturn(null);
  68. when(projectBranches.defaultBranchName()).thenReturn("default");
  69. when(newCodePeriodLoader.load(PROJECT_KEY, "default")).thenReturn(createResponse(NewCodePeriods.NewCodePeriodType.REFERENCE_BRANCH, "master"));
  70. assertThat(referenceBranchSupplier.get()).isEqualTo("master");
  71. }
  72. @Test
  73. public void returns_null_if_no_branches() {
  74. when(projectBranches.isEmpty()).thenReturn(true);
  75. assertThat(referenceBranchSupplier.get()).isNull();
  76. verify(branchConfiguration).isPullRequest();
  77. verify(projectBranches).isEmpty();
  78. verifyNoMoreInteractions(branchConfiguration);
  79. verifyNoInteractions(newCodePeriodLoader);
  80. }
  81. @Test
  82. public void returns_null_if_reference_branch_is_the_branch_being_analyzed() {
  83. when(branchConfiguration.branchType()).thenReturn(BranchType.BRANCH);
  84. when(branchConfiguration.branchName()).thenReturn(BRANCH_KEY);
  85. when(newCodePeriodLoader.load(PROJECT_KEY, BRANCH_KEY)).thenReturn(createResponse(NewCodePeriods.NewCodePeriodType.REFERENCE_BRANCH, BRANCH_KEY));
  86. assertThat(referenceBranchSupplier.get()).isNull();
  87. verify(branchConfiguration, times(2)).branchName();
  88. verify(branchConfiguration).isPullRequest();
  89. verify(newCodePeriodLoader).load(PROJECT_KEY, BRANCH_KEY);
  90. verifyNoMoreInteractions(branchConfiguration);
  91. }
  92. @Test
  93. public void returns_null_if_pull_request() {
  94. when(branchConfiguration.isPullRequest()).thenReturn(true);
  95. assertThat(referenceBranchSupplier.get()).isNull();
  96. verify(branchConfiguration).isPullRequest();
  97. verifyNoInteractions(newCodePeriodLoader);
  98. verifyNoMoreInteractions(branchConfiguration);
  99. }
  100. @Test
  101. public void returns_null_if_new_code_period_is_not_ref() {
  102. when(branchConfiguration.isPullRequest()).thenReturn(true);
  103. when(branchConfiguration.branchName()).thenReturn(BRANCH_KEY);
  104. when(newCodePeriodLoader.load(PROJECT_KEY, BRANCH_KEY)).thenReturn(createResponse(NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "2"));
  105. assertThat(referenceBranchSupplier.get()).isNull();
  106. }
  107. private NewCodePeriods.ShowWSResponse createResponse(NewCodePeriods.NewCodePeriodType type, String value) {
  108. return NewCodePeriods.ShowWSResponse.newBuilder()
  109. .setType(type)
  110. .setValue(value)
  111. .build();
  112. }
  113. }