]> source.dussan.org Git - sonarqube.git/blob
13ce54c130fdbd3a2d0851443ffa42c43ad2b9dd
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.scan.branch;
21
22 import java.util.HashMap;
23 import java.util.Map;
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;
33
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;
38
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);
49
50   @Captor
51   private ArgumentCaptor<Supplier<Map<String, String>>> settingsCaptor;
52
53   @Before
54   public void setUp() {
55     MockitoAnnotations.initMocks(this);
56     when(projectConfiguration.getProperties()).thenReturn(projectSettings);
57     when(reactor.getRoot()).thenReturn(root);
58   }
59
60   @Test
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);
64   }
65
66   @Test
67   public void should_use_loader() {
68     when(loader.load(eq(projectSettings), eq(branches), eq(pullRequests))).thenReturn(config);
69
70     BranchConfiguration result = provider.provide(loader, projectConfiguration, branches, pullRequests);
71
72     assertThat(result).isSameAs(config);
73   }
74
75   @Test
76   public void should_return_default_if_no_loader() {
77     BranchConfiguration result = provider.provide(null, projectConfiguration, branches, pullRequests);
78
79     assertThat(result.targetBranchName()).isNull();
80     assertThat(result.branchType()).isEqualTo(BranchType.BRANCH);
81   }
82 }