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.

ExternalProjectKeyAndOrganizationProviderTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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;
  21. import java.util.Optional;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.sonar.scanner.bootstrap.RawScannerProperties;
  25. import static java.util.Optional.of;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. import static org.mockito.Mockito.mock;
  28. import static org.mockito.Mockito.when;
  29. public class ExternalProjectKeyAndOrganizationProviderTest {
  30. private ExternalProjectKeyAndOrganizationLoader loader = mock(ExternalProjectKeyAndOrganizationLoader.class);
  31. private RawScannerProperties rawProperties = mock(RawScannerProperties.class);
  32. private ExternalProjectKeyAndOrganizationProvider underTest = new ExternalProjectKeyAndOrganizationProvider();
  33. @Before
  34. public void setUp() {
  35. when(loader.load()).thenReturn(of(new ExternalProjectKeyAndOrganization() {
  36. @Override
  37. public Optional<String> getProjectKey() {
  38. return of("some_key");
  39. }
  40. @Override
  41. public Optional<String> getOrganization() {
  42. return of("organization_key");
  43. }
  44. }));
  45. }
  46. @Test
  47. public void does_nothing_when_key_autodetection_is_disabled() {
  48. when(rawProperties.property("sonar.keys_autodetection.disabled")).thenReturn("true");
  49. ExternalProjectKeyAndOrganization result = underTest.provide(rawProperties, loader);
  50. assertThat(result).isInstanceOf(EmptyExternalProjectKeyAndOrganization.class);
  51. }
  52. @Test
  53. public void by_default_attempts_to_autodetect_keys_if_external_key_loader_detected() {
  54. when(rawProperties.property("sonar.keys_autodetection.disabled")).thenReturn(null);
  55. ExternalProjectKeyAndOrganization result = underTest.provide(rawProperties, loader);
  56. assertThat(result).isNotInstanceOf(EmptyExternalProjectKeyAndOrganization.class);
  57. assertThat(result.getProjectKey()).isEqualTo(of("some_key"));
  58. assertThat(result.getOrganization()).isEqualTo(of("organization_key"));
  59. }
  60. @Test
  61. public void by_default_does_nothing_when_no_external_key_loader_detected() {
  62. when(rawProperties.property("sonar.keys_autodetection.disabled")).thenReturn(null);
  63. ExternalProjectKeyAndOrganization result = underTest.provide(rawProperties, null);
  64. assertThat(result).isInstanceOf(EmptyExternalProjectKeyAndOrganization.class);
  65. }
  66. @Test
  67. public void attempts_to_autodetect_keys_if_autodection_is_explicitly_enabled() {
  68. when(rawProperties.property("sonar.keys_autodetection.disabled")).thenReturn("false");
  69. ExternalProjectKeyAndOrganization result = underTest.provide(rawProperties, loader);
  70. assertThat(result).isNotInstanceOf(EmptyExternalProjectKeyAndOrganization.class);
  71. assertThat(result.getProjectKey()).isEqualTo(of("some_key"));
  72. assertThat(result.getOrganization()).isEqualTo(of("organization_key"));
  73. }
  74. }