]> source.dussan.org Git - sonarqube.git/blob
daae92c11c21c931cc9fb0e2f60bcddeaadbc4ac
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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
22 import java.io.IOException;
23 import java.io.PipedInputStream;
24 import java.io.PipedOutputStream;
25 import java.util.Map;
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;
32
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;
39
40 public class DefaultGlobalSettingsLoaderTest {
41
42   private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
43   private DefaultGlobalSettingsLoader underTest = new DefaultGlobalSettingsLoader(wsClient);
44
45   @Test
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")
53         .build())
54       .addSettings(Settings.Setting.newBuilder()
55         .setKey("123").setValue("456")
56         .build())
57       .build()
58       .writeTo(out);
59     out.close();
60     when(response.contentStream()).thenReturn(in);
61     when(wsClient.call(any(GetRequest.class))).thenReturn(response);
62
63     Map<String, String> result = underTest.loadGlobalSettings();
64
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");
68     assertThat(result)
69       .isNotNull()
70       .hasSize(2)
71       .containsEntry("abc", "def")
72       .containsEntry("123", "456");
73   }
74 }