3 * Copyright (C) 2009-2019 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;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Optional;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.utils.MessageException;
31 import org.sonar.scanner.WsTestUtil;
32 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
33 import org.sonar.scanner.scan.ScanProperties;
34 import org.sonarqube.ws.Qualityprofiles;
35 import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile;
36 import org.sonarqube.ws.client.HttpException;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.verifyNoMoreInteractions;
40 import static org.mockito.Mockito.when;
42 public class DefaultQualityProfileLoaderTest {
44 public ExpectedException exception = ExpectedException.none();
46 private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
47 private ScanProperties properties = mock(ScanProperties.class);
48 private DefaultQualityProfileLoader underTest = new DefaultQualityProfileLoader(properties, wsClient);
51 public void load_gets_all_profiles_for_specified_project() throws IOException {
52 prepareCallWithResults();
53 underTest.load("foo");
54 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo");
58 public void load_encodes_url_parameters() throws IOException {
59 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232", createStreamOfProfiles("qp"));
60 underTest.load("foo#2");
61 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo%232");
65 public void load_sets_organization_parameter_if_defined_in_settings() throws IOException {
66 when(properties.organizationKey()).thenReturn(Optional.of("my-org"));
67 prepareCallWithResults();
68 underTest.load("foo");
69 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo&organization=my-org");
73 public void load_tries_default_if_no_profiles_found_for_project() throws IOException {
74 HttpException e = new HttpException("", 404, "{\"errors\":[{\"msg\":\"No project found with key 'foo'\"}]}");
75 WsTestUtil.mockException(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo", e);
76 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true", createStreamOfProfiles("qp"));
78 underTest.load("foo");
80 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo");
81 verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true");
85 public void loadDefault_sets_organization_parameter_if_defined_in_settings() throws IOException {
86 when(properties.organizationKey()).thenReturn(Optional.of("my-org"));
88 HttpException e = new HttpException("", 404, "{\"errors\":[{\"msg\":\"No organization with key 'myorg'\"}]}");
89 WsTestUtil.mockException(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo&organization=my-org", e);
90 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true&organization=my-org", createStreamOfProfiles("qp"));
92 underTest.load("foo");
94 verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true&organization=my-org");
98 public void load_throws_MessageException_if_no_profiles_are_available_for_specified_project() throws IOException {
99 prepareCallWithEmptyResults();
101 exception.expect(MessageException.class);
102 exception.expectMessage("No quality profiles");
104 underTest.load("project");
105 verifyNoMoreInteractions(wsClient);
109 public void load_throws_MessageException_if_organization_is_not_found_after_trying_default() throws IOException {
110 HttpException e = new HttpException("", 404, "{\"errors\":[{\"msg\":\"No organization with key 'myorg'\"}]}");
111 WsTestUtil.mockException(wsClient, e);
113 exception.expect(MessageException.class);
114 exception.expectMessage("Failed to load the default quality profiles: No organization with key 'myorg'");
116 underTest.load("project");
117 verifyNoMoreInteractions(wsClient);
120 private void verifyCalledPath(String expectedPath) {
121 WsTestUtil.verifyCall(wsClient, expectedPath);
124 private void prepareCallWithResults() throws IOException {
125 WsTestUtil.mockStream(wsClient, createStreamOfProfiles("qp"));
128 private void prepareCallWithEmptyResults() throws IOException {
129 WsTestUtil.mockStream(wsClient, createStreamOfProfiles());
132 private static InputStream createStreamOfProfiles(String... names) throws IOException {
133 ByteArrayOutputStream os = new ByteArrayOutputStream();
134 Qualityprofiles.SearchWsResponse.Builder responseBuilder = Qualityprofiles.SearchWsResponse.newBuilder();
136 for (String n : names) {
137 QualityProfile qp = QualityProfile.newBuilder().setKey(n).setName(n).setLanguage("lang").build();
138 responseBuilder.addProfiles(qp);
141 responseBuilder.build().writeTo(os);
142 return new ByteArrayInputStream(os.toByteArray());