3 * Copyright (C) 2009-2017 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 org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.config.MapSettings;
30 import org.sonar.api.config.Settings;
31 import org.sonar.api.utils.MessageException;
32 import org.sonar.scanner.WsTestUtil;
33 import org.sonar.scanner.bootstrap.ScannerWsClient;
34 import org.sonarqube.ws.QualityProfiles;
35 import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verifyNoMoreInteractions;
40 public class DefaultQualityProfileLoaderTest {
42 public ExpectedException exception = ExpectedException.none();
44 private ScannerWsClient wsClient = mock(ScannerWsClient.class);
45 private Settings settings = new MapSettings();
46 private DefaultQualityProfileLoader underTest = new DefaultQualityProfileLoader(settings, wsClient);
49 public void load_gets_profiles_for_specified_project_and_profile_name() throws IOException {
50 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo", createStreamOfProfiles("qp"));
51 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=bar", createStreamOfProfiles("qp"));
52 underTest.load("foo", "bar");
53 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo");
54 verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=bar");
58 public void load_gets_all_profiles_for_specified_project() throws IOException {
59 prepareCallWithResults();
60 underTest.load("foo", null);
61 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo");
65 public void load_encodes_url_parameters() throws IOException {
66 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?projectKey=foo%232", createStreamOfProfiles("qp"));
67 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=bar%232", createStreamOfProfiles("qp"));
68 underTest.load("foo#2", "bar#2");
69 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo%232");
70 verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=bar%232");
74 public void load_sets_organization_parameter_if_defined_in_settings() throws IOException {
75 settings.setProperty("sonar.organization", "my-org");
76 prepareCallWithResults();
77 underTest.load("foo", null);
78 verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo&organization=my-org");
82 public void loadDefault_gets_profiles_with_specified_name() throws IOException {
83 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true", createStreamOfProfiles("qp"));
84 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=foo", createStreamOfProfiles("qp"));
85 underTest.loadDefault("foo");
86 verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true");
87 verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=foo");
91 public void loadDefault_gets_all_default_profiles() throws IOException {
92 prepareCallWithResults();
93 underTest.loadDefault(null);
94 verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true");
98 public void loadDefault_sets_organization_parameter_if_defined_in_settings() throws IOException {
99 settings.setProperty("sonar.organization", "my-org");
100 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?defaults=true&organization=my-org", createStreamOfProfiles("qp"));
101 WsTestUtil.mockStream(wsClient, "/api/qualityprofiles/search.protobuf?profileName=foo&organization=my-org", createStreamOfProfiles("qp"));
102 underTest.loadDefault("foo");
103 verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true&organization=my-org");
104 verifyCalledPath("/api/qualityprofiles/search.protobuf?profileName=foo&organization=my-org");
108 public void load_throws_MessageException_if_no_profiles_are_available_for_specified_project() throws IOException {
109 prepareCallWithEmptyResults();
111 exception.expect(MessageException.class);
112 exception.expectMessage("No quality profiles");
114 underTest.load("project", null);
115 verifyNoMoreInteractions(wsClient);
118 private void verifyCalledPath(String expectedPath) {
119 WsTestUtil.verifyCall(wsClient, expectedPath);
122 private void prepareCallWithResults() throws IOException {
123 WsTestUtil.mockStream(wsClient, createStreamOfProfiles("qp"));
126 private void prepareCallWithEmptyResults() throws IOException {
127 WsTestUtil.mockStream(wsClient, createStreamOfProfiles());
130 private static InputStream createStreamOfProfiles(String... names) throws IOException {
131 ByteArrayOutputStream os = new ByteArrayOutputStream();
132 QualityProfiles.SearchWsResponse.Builder responseBuilder = QualityProfiles.SearchWsResponse.newBuilder();
134 for (String n : names) {
135 QualityProfile qp = QualityProfile.newBuilder().setKey(n).setName(n).setLanguage("lang").build();
136 responseBuilder.addProfiles(qp);
139 responseBuilder.build().writeTo(os);
140 return new ByteArrayInputStream(os.toByteArray());