3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.scan.branch;
22 import java.util.HashMap;
24 import java.util.function.Supplier;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Captor;
29 import org.mockito.MockitoAnnotations;
30 import org.sonar.api.batch.bootstrap.ProjectDefinition;
31 import org.sonar.api.batch.bootstrap.ProjectReactor;
32 import org.sonar.scanner.scan.ProjectConfiguration;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.ArgumentMatchers.eq;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
39 public class BranchConfigurationProviderTest {
40 private BranchConfigurationProvider provider = new BranchConfigurationProvider();
41 private ProjectConfiguration projectConfiguration = mock(ProjectConfiguration.class);
42 private BranchConfigurationLoader loader = mock(BranchConfigurationLoader.class);
43 private BranchConfiguration config = mock(BranchConfiguration.class);
44 private ProjectBranches branches = mock(ProjectBranches.class);
45 private ProjectPullRequests pullRequests = mock(ProjectPullRequests.class);
46 private ProjectReactor reactor = mock(ProjectReactor.class);
47 private Map<String, String> projectSettings = new HashMap<>();
48 private ProjectDefinition root = mock(ProjectDefinition.class);
51 private ArgumentCaptor<Supplier<Map<String, String>>> settingsCaptor;
55 MockitoAnnotations.initMocks(this);
56 when(projectConfiguration.getProperties()).thenReturn(projectSettings);
57 when(reactor.getRoot()).thenReturn(root);
61 public void should_cache_config() {
62 BranchConfiguration configuration = provider.provide(null, projectConfiguration, branches, pullRequests);
63 assertThat(provider.provide(null, projectConfiguration, branches, pullRequests)).isSameAs(configuration);
67 public void should_use_loader() {
68 when(loader.load(eq(projectSettings), eq(branches), eq(pullRequests))).thenReturn(config);
70 BranchConfiguration result = provider.provide(loader, projectConfiguration, branches, pullRequests);
72 assertThat(result).isSameAs(config);
76 public void should_return_default_if_no_loader() {
77 BranchConfiguration result = provider.provide(null, projectConfiguration, branches, pullRequests);
79 assertThat(result.targetBranchName()).isNull();
80 assertThat(result.branchType()).isEqualTo(BranchType.BRANCH);