]> source.dussan.org Git - sonarqube.git/blob
8a11e425a86f6d44f73816b35dd946a32c72779d
[sonarqube.git] /
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.scanner.scan.branch;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyMap;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.function.Supplier;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Captor;
40 import org.mockito.MockitoAnnotations;
41 import org.sonar.api.batch.bootstrap.ProjectDefinition;
42 import org.sonar.api.batch.bootstrap.ProjectReactor;
43 import org.sonar.scanner.bootstrap.GlobalConfiguration;
44 import org.sonar.scanner.repository.settings.SettingsLoader;
45
46 public class BranchConfigurationProviderTest {
47   private BranchConfigurationProvider provider = new BranchConfigurationProvider();
48   private GlobalConfiguration globalConfiguration = mock(GlobalConfiguration.class);
49   private BranchConfigurationLoader loader = mock(BranchConfigurationLoader.class);
50   private BranchConfiguration config = mock(BranchConfiguration.class);
51   private ProjectBranches branches = mock(ProjectBranches.class);
52   private ProjectPullRequests pullRequests = mock(ProjectPullRequests.class);
53   private ProjectReactor reactor = mock(ProjectReactor.class);;
54   private Map<String, String> globalPropertiesMap = new HashMap<>();;
55   private Map<String, String> remoteProjectSettings;
56   private ProjectDefinition root = mock(ProjectDefinition.class);
57   private SettingsLoader settingsLoader = mock(SettingsLoader.class);
58
59   @Captor
60   private ArgumentCaptor<Supplier<Map<String, String>>> settingsCaptor;
61
62   @Before
63   public void setUp() {
64     MockitoAnnotations.initMocks(this);
65     when(globalConfiguration.getProperties()).thenReturn(globalPropertiesMap);
66     when(settingsLoader.load(anyString())).thenReturn(remoteProjectSettings);
67     when(reactor.getRoot()).thenReturn(root);
68   }
69
70   @Test
71   public void should_cache_config() {
72     BranchConfiguration configuration = provider.provide(null, globalConfiguration, reactor, settingsLoader, branches, pullRequests);
73     assertThat(provider.provide(null, globalConfiguration, reactor, settingsLoader, branches, pullRequests)).isSameAs(configuration);
74   }
75
76   @Test
77   public void should_use_loader() {
78     when(loader.load(eq(globalPropertiesMap), any(Supplier.class), eq(branches), eq(pullRequests))).thenReturn(config);
79
80     BranchConfiguration result = provider.provide(loader, globalConfiguration, reactor, settingsLoader, branches, pullRequests);
81
82     assertThat(result).isSameAs(config);
83   }
84
85   @Test
86   public void settings_should_include_command_line_args_with_highest_priority() {
87     when(globalConfiguration.getProperties()).thenReturn(Collections.singletonMap("key", "global"));
88     when(settingsLoader.load(anyString())).thenReturn(Collections.singletonMap("key", "settings"));
89     when(root.properties()).thenReturn(Collections.singletonMap("key", "root"));
90     provider.provide(loader, globalConfiguration, reactor, settingsLoader, branches, pullRequests);
91     verify(loader).load(anyMap(), settingsCaptor.capture(), any(ProjectBranches.class), any(ProjectPullRequests.class));
92
93     Map<String, String> map = settingsCaptor.getValue().get();
94     assertThat(map.get("key")).isEqualTo("root");
95   }
96
97   @Test
98   public void should_return_default_if_no_loader() {
99     BranchConfiguration result = provider.provide(null, globalConfiguration, reactor, settingsLoader, branches, pullRequests);
100
101     assertThat(result.targetScmBranch()).isNull();
102     assertThat(result.branchType()).isEqualTo(BranchType.LONG);
103   }
104 }