3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.repository.settings;
22 import java.io.IOException;
23 import java.io.PipedInputStream;
24 import java.io.PipedOutputStream;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
29 import org.sonarqube.ws.Settings;
30 import org.sonarqube.ws.client.GetRequest;
31 import org.sonarqube.ws.client.WsResponse;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.times;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
40 public class DefaultGlobalSettingsLoaderTest {
42 private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
43 private DefaultGlobalSettingsLoader underTest = new DefaultGlobalSettingsLoader(wsClient);
46 public void loadGlobalSettings() throws IOException {
47 WsResponse response = mock(WsResponse.class);
48 PipedOutputStream out = new PipedOutputStream();
49 PipedInputStream in = new PipedInputStream(out);
50 Settings.ValuesWsResponse.newBuilder()
51 .addSettings(Settings.Setting.newBuilder()
52 .setKey("abc").setValue("def")
54 .addSettings(Settings.Setting.newBuilder()
55 .setKey("123").setValue("456")
60 when(response.contentStream()).thenReturn(in);
61 when(wsClient.call(any(GetRequest.class))).thenReturn(response);
63 Map<String, String> result = underTest.loadGlobalSettings();
65 ArgumentCaptor<GetRequest> argumentCaptor = ArgumentCaptor.forClass(GetRequest.class);
66 verify(wsClient, times(1)).call(argumentCaptor.capture());
67 assertThat(argumentCaptor.getValue().getPath()).isEqualTo("api/settings/values.protobuf");
71 .containsEntry("abc", "def")
72 .containsEntry("123", "456");