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.

BranchConfigurationProviderTest.java 4.6KB

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