]> source.dussan.org Git - sonarqube.git/blob
edfc14e5bd6378aa3464b707cdec4f6f6e0228ba
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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 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;
37
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.verifyNoMoreInteractions;
40 import static org.mockito.Mockito.when;
41
42 public class DefaultQualityProfileLoaderTest {
43   @Rule
44   public ExpectedException exception = ExpectedException.none();
45
46   private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
47   private ScanProperties properties = mock(ScanProperties.class);
48   private DefaultQualityProfileLoader underTest = new DefaultQualityProfileLoader(properties, wsClient);
49
50   @Test
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");
55   }
56
57   @Test
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");
62   }
63
64   @Test
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");
70   }
71
72   @Test
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"));
77
78     underTest.load("foo");
79
80     verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo");
81     verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true");
82   }
83
84   @Test
85   public void loadDefault_sets_organization_parameter_if_defined_in_settings() throws IOException {
86     when(properties.organizationKey()).thenReturn(Optional.of("my-org"));
87
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"));
91
92     underTest.load("foo");
93
94     verifyCalledPath("/api/qualityprofiles/search.protobuf?defaults=true&organization=my-org");
95   }
96
97   @Test
98   public void load_throws_MessageException_if_no_profiles_are_available_for_specified_project() throws IOException {
99     prepareCallWithEmptyResults();
100
101     exception.expect(MessageException.class);
102     exception.expectMessage("No quality profiles");
103
104     underTest.load("project");
105     verifyNoMoreInteractions(wsClient);
106   }
107
108   @Test
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);
112
113     exception.expect(MessageException.class);
114     exception.expectMessage("Failed to load the default quality profiles: No organization with key 'myorg'");
115
116     underTest.load("project");
117     verifyNoMoreInteractions(wsClient);
118   }
119
120   private void verifyCalledPath(String expectedPath) {
121     WsTestUtil.verifyCall(wsClient, expectedPath);
122   }
123
124   private void prepareCallWithResults() throws IOException {
125     WsTestUtil.mockStream(wsClient, createStreamOfProfiles("qp"));
126   }
127
128   private void prepareCallWithEmptyResults() throws IOException {
129     WsTestUtil.mockStream(wsClient, createStreamOfProfiles());
130   }
131
132   private static InputStream createStreamOfProfiles(String... names) throws IOException {
133     ByteArrayOutputStream os = new ByteArrayOutputStream();
134     Qualityprofiles.SearchWsResponse.Builder responseBuilder = Qualityprofiles.SearchWsResponse.newBuilder();
135
136     for (String n : names) {
137       QualityProfile qp = QualityProfile.newBuilder().setKey(n).setName(n).setLanguage("lang").build();
138       responseBuilder.addProfiles(qp);
139     }
140
141     responseBuilder.build().writeTo(os);
142     return new ByteArrayInputStream(os.toByteArray());
143   }
144 }