3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.BatchWsClient;
34 import org.sonar.scanner.repository.DefaultProjectRepositoriesLoader;
35 import org.sonar.scanner.repository.FileData;
36 import org.sonar.scanner.repository.ProjectRepositories;
37 import org.sonarqube.ws.WsBatch.WsProjectResponse;
38 import org.sonarqube.ws.client.HttpException;
39 import org.sonarqube.ws.client.WsRequest;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Matchers.any;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
46 public class DefaultProjectRepositoriesLoaderTest {
47 private final static String PROJECT_KEY = "foo?";
49 public ExpectedException thrown = ExpectedException.none();
51 private DefaultProjectRepositoriesLoader loader;
52 private BatchWsClient wsClient;
55 public void prepare() throws IOException {
56 wsClient = mock(BatchWsClient.class);
57 InputStream is = mockData();
58 WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
59 loader = new DefaultProjectRepositoriesLoader(wsClient);
63 public void continueOnError() {
64 when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class);
65 ProjectRepositories proj = loader.load(PROJECT_KEY, false);
66 assertThat(proj.exists()).isEqualTo(false);
70 public void parsingError() throws IOException {
71 InputStream is = mock(InputStream.class);
72 when(is.read()).thenThrow(IOException.class);
73 WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is);
74 loader.load(PROJECT_KEY, false);
77 @Test(expected = IllegalStateException.class)
78 public void failFastHttpError() {
79 HttpException http = new HttpException("url", 403);
80 IllegalStateException e = new IllegalStateException("http error", http);
81 WsTestUtil.mockException(wsClient, e);
82 loader.load(PROJECT_KEY, false);
86 public void failFastHttpErrorMessageException() {
87 thrown.expect(MessageException.class);
88 thrown.expectMessage("http error");
90 HttpException http = new HttpException("uri", 403);
91 MessageException e = MessageException.of("http error", http);
92 WsTestUtil.mockException(wsClient, e);
93 loader.load(PROJECT_KEY, false);
97 public void passIssuesModeParameter() {
98 loader.load(PROJECT_KEY, false);
99 WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F");
101 loader.load(PROJECT_KEY, true);
102 WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F&issues_mode=true");
106 public void deserializeResponse() throws IOException {
107 loader.load(PROJECT_KEY, false);
111 public void passAndEncodeProjectKeyParameter() {
112 loader.load(PROJECT_KEY, false);
113 WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F");
116 private InputStream mockData() throws IOException {
117 ByteArrayOutputStream os = new ByteArrayOutputStream();
118 WsProjectResponse.Builder projectResponseBuilder = WsProjectResponse.newBuilder();
119 WsProjectResponse response = projectResponseBuilder.build();
120 response.writeTo(os);
122 return new ByteArrayInputStream(os.toByteArray());
126 public void readRealResponse() throws IOException {
127 InputStream is = getTestResource("project.protobuf");
128 WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=org.sonarsource.github%3Asonar-github-plugin&issues_mode=true", is);
130 ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true);
131 FileData fd = proj.fileData("org.sonarsource.github:sonar-github-plugin",
132 "src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java");
134 assertThat(fd.revision()).isEqualTo("27bf2c54633d05c5df402bbe09471fe43bd9e2e5");
135 assertThat(fd.hash()).isEqualTo("edb6b3b9ab92d8dc53ba90ab86cd422e");
138 private InputStream getTestResource(String name) throws IOException {
139 return Resources.asByteSource(this.getClass().getResource(this.getClass().getSimpleName() + "/" + name))
140 .openBufferedStream();