]> source.dussan.org Git - sonarqube.git/blob
7a2982e77f1962cc08b871196f1f4ae8a71a8282
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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;
21
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;
36
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verifyNoMoreInteractions;
39
40 public class DefaultQualityProfileLoaderTest {
41   @Rule
42   public ExpectedException exception = ExpectedException.none();
43
44   private ScannerWsClient wsClient = mock(ScannerWsClient.class);
45   private Settings settings = new MapSettings();
46   private DefaultQualityProfileLoader underTest = new DefaultQualityProfileLoader(settings, wsClient);
47
48   @Test
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");
55   }
56
57   @Test
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");
62   }
63
64   @Test
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");
71   }
72
73   @Test
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");
79   }
80
81   @Test
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");
88   }
89
90   @Test
91   public void loadDefault_gets_all_default_profiles() throws IOException {
92     prepareCallWithResults();
93     underTest.loadDefault(null);
94     verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true");
95   }
96
97   @Test
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");
105   }
106
107   @Test
108   public void load_throws_MessageException_if_no_profiles_are_available_for_specified_project() throws IOException {
109     prepareCallWithEmptyResults();
110
111     exception.expect(MessageException.class);
112     exception.expectMessage("No quality profiles");
113
114     underTest.load("project", null);
115     verifyNoMoreInteractions(wsClient);
116   }
117
118   private void verifyCalledPath(String expectedPath) {
119     WsTestUtil.verifyCall(wsClient, expectedPath);
120   }
121
122   private void prepareCallWithResults() throws IOException {
123     WsTestUtil.mockStream(wsClient, createStreamOfProfiles("qp"));
124   }
125
126   private void prepareCallWithEmptyResults() throws IOException {
127     WsTestUtil.mockStream(wsClient, createStreamOfProfiles());
128   }
129
130   private static InputStream createStreamOfProfiles(String... names) throws IOException {
131     ByteArrayOutputStream os = new ByteArrayOutputStream();
132     QualityProfiles.SearchWsResponse.Builder responseBuilder = QualityProfiles.SearchWsResponse.newBuilder();
133
134     for (String n : names) {
135       QualityProfile qp = QualityProfile.newBuilder().setKey(n).setName(n).setLanguage("lang").build();
136       responseBuilder.addProfiles(qp);
137     }
138
139     responseBuilder.build().writeTo(os);
140     return new ByteArrayInputStream(os.toByteArray());
141   }
142 }