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