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 com.google.common.io.Resources;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
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.Batch.WsProjectResponse;
35 import org.sonarqube.ws.client.HttpException;
36 import org.sonarqube.ws.client.WsRequest;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
43 public class DefaultProjectRepositoriesLoaderTest {
44 private final static String PROJECT_KEY = "foo?";
46 public ExpectedException thrown = ExpectedException.none();
48 private DefaultProjectRepositoriesLoader loader;
49 private ScannerWsClient wsClient;
52 public void prepare() throws IOException {
53 wsClient = mock(ScannerWsClient.class);
54 InputStream is = mockData();
55 WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
56 loader = new DefaultProjectRepositoriesLoader(wsClient);
60 public void continueOnError() {
61 when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class);
62 ProjectRepositories proj = loader.load(PROJECT_KEY, false, null);
63 assertThat(proj.exists()).isEqualTo(false);
67 public void parsingError() throws IOException {
68 InputStream is = mock(InputStream.class);
69 when(is.read()).thenThrow(IOException.class);
70 WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
71 loader.load(PROJECT_KEY, false, null);
74 @Test(expected = IllegalStateException.class)
75 public void failFastHttpError() {
76 HttpException http = new HttpException("url", 403, null);
77 IllegalStateException e = new IllegalStateException("http error", http);
78 WsTestUtil.mockException(wsClient, e);
79 loader.load(PROJECT_KEY, false, null);
83 public void failFastHttpErrorMessageException() {
84 thrown.expect(MessageException.class);
85 thrown.expectMessage("http error");
87 HttpException http = new HttpException("uri", 403, null);
88 MessageException e = MessageException.of("http error", http);
89 WsTestUtil.mockException(wsClient, e);
90 loader.load(PROJECT_KEY, false, null);
94 public void passIssuesModeParameter() {
95 loader.load(PROJECT_KEY, false, null);
96 WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F");
98 loader.load(PROJECT_KEY, true, null);
99 WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F&issues_mode=true");
103 public void deserializeResponse() throws IOException {
104 loader.load(PROJECT_KEY, false, null);
108 public void passAndEncodeProjectKeyParameter() {
109 loader.load(PROJECT_KEY, false, null);
110 WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F");
113 private InputStream mockData() throws IOException {
114 ByteArrayOutputStream os = new ByteArrayOutputStream();
115 WsProjectResponse.Builder projectResponseBuilder = WsProjectResponse.newBuilder();
116 WsProjectResponse response = projectResponseBuilder.build();
117 response.writeTo(os);
119 return new ByteArrayInputStream(os.toByteArray());
123 public void readRealResponse() throws IOException {
124 InputStream is = getTestResource("project.protobuf");
125 WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=org.sonarsource.github%3Asonar-github-plugin&issues_mode=true", is);
127 ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true, null);
128 FileData fd = proj.fileData("org.sonarsource.github:sonar-github-plugin",
129 "src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java");
131 assertThat(fd.revision()).isEqualTo("27bf2c54633d05c5df402bbe09471fe43bd9e2e5");
132 assertThat(fd.hash()).isEqualTo("edb6b3b9ab92d8dc53ba90ab86cd422e");
135 private InputStream getTestResource(String name) throws IOException {
136 return Resources.asByteSource(this.getClass().getResource(this.getClass().getSimpleName() + "/" + name))
137 .openBufferedStream();