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.

DefaultProjectSettingsLoaderTest.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.repository.settings;
  21. import java.io.IOException;
  22. import java.io.PipedInputStream;
  23. import java.io.PipedOutputStream;
  24. import java.util.Map;
  25. import org.junit.Test;
  26. import org.mockito.ArgumentCaptor;
  27. import org.sonar.scanner.http.DefaultScannerWsClient;
  28. import org.sonar.scanner.bootstrap.ScannerProperties;
  29. import org.sonarqube.ws.Settings;
  30. import org.sonarqube.ws.client.GetRequest;
  31. import org.sonarqube.ws.client.WsResponse;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.mockito.ArgumentMatchers.any;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.times;
  36. import static org.mockito.Mockito.verify;
  37. import static org.mockito.Mockito.when;
  38. public class DefaultProjectSettingsLoaderTest {
  39. private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
  40. private ScannerProperties properties = mock(ScannerProperties.class);
  41. private DefaultProjectSettingsLoader underTest = new DefaultProjectSettingsLoader(wsClient, properties);
  42. @Test
  43. public void loadProjectSettings() throws IOException {
  44. WsResponse response = mock(WsResponse.class);
  45. PipedOutputStream out = new PipedOutputStream();
  46. PipedInputStream in = new PipedInputStream(out);
  47. Settings.ValuesWsResponse.newBuilder()
  48. .addSettings(Settings.Setting.newBuilder()
  49. .setKey("abc").setValue("def")
  50. .build())
  51. .addSettings(Settings.Setting.newBuilder()
  52. .setKey("123").setValue("456")
  53. .build())
  54. .build()
  55. .writeTo(out);
  56. out.close();
  57. when(response.contentStream()).thenReturn(in);
  58. when(wsClient.call(any())).thenReturn(response);
  59. when(properties.getProjectKey()).thenReturn("project_key");
  60. Map<String, String> result = underTest.loadProjectSettings();
  61. ArgumentCaptor<GetRequest> argumentCaptor = ArgumentCaptor.forClass(GetRequest.class);
  62. verify(wsClient, times(1)).call(argumentCaptor.capture());
  63. assertThat(argumentCaptor.getValue().getPath()).isEqualTo("api/settings/values.protobuf?component=project_key");
  64. assertThat(result)
  65. .isNotNull()
  66. .hasSize(2)
  67. .containsEntry("abc", "def")
  68. .containsEntry("123", "456");
  69. }
  70. }