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.batch.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.apache.commons.lang.mutable.MutableBoolean;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.sonar.api.utils.MessageException;
33 import org.sonar.batch.cache.WSLoader;
34 import org.sonar.batch.cache.WSLoaderResult;
35 import org.sonarqube.ws.WsBatch.WsProjectResponse;
36 import org.sonarqube.ws.client.HttpException;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Matchers.anyString;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
44 public class DefaultProjectRepositoriesLoaderTest {
45 private final static String PROJECT_KEY = "foo?";
47 public ExpectedException thrown = ExpectedException.none();
49 private DefaultProjectRepositoriesLoader loader;
50 private WSLoader wsLoader;
53 public void prepare() throws IOException {
54 wsLoader = mock(WSLoader.class);
55 InputStream is = mockData();
56 when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
57 loader = new DefaultProjectRepositoriesLoader(wsLoader);
61 public void continueOnError() {
62 when(wsLoader.loadStream(anyString())).thenThrow(IllegalStateException.class);
63 ProjectRepositories proj = loader.load(PROJECT_KEY, false, null);
64 assertThat(proj.exists()).isEqualTo(false);
68 public void parsingError() throws IOException {
69 InputStream is = mock(InputStream.class);
70 when(is.read()).thenThrow(IOException.class);
72 when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, false));
73 loader.load(PROJECT_KEY, false, null);
76 @Test(expected = IllegalStateException.class)
77 public void failFastHttpError() {
78 HttpException http = new HttpException("url", 403);
79 IllegalStateException e = new IllegalStateException("http error", http);
80 when(wsLoader.loadStream(anyString())).thenThrow(e);
81 loader.load(PROJECT_KEY, false, null);
85 public void failFastHttpErrorMessageException() {
86 thrown.expect(MessageException.class);
87 thrown.expectMessage("http error");
89 HttpException http = new HttpException("uri", 403);
90 MessageException e = MessageException.of("http error", http);
91 when(wsLoader.loadStream(anyString())).thenThrow(e);
92 loader.load(PROJECT_KEY, false, null);
96 public void passIssuesModeParameter() {
97 loader.load(PROJECT_KEY, false, null);
98 verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
100 loader.load(PROJECT_KEY, true, null);
101 verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F&issues_mode=true");
105 public void deserializeResponse() throws IOException {
106 MutableBoolean fromCache = new MutableBoolean();
107 loader.load(PROJECT_KEY, false, fromCache);
108 assertThat(fromCache.booleanValue()).isTrue();
112 public void passAndEncodeProjectKeyParameter() {
113 loader.load(PROJECT_KEY, false, null);
114 verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
117 private InputStream mockData() throws IOException {
118 ByteArrayOutputStream os = new ByteArrayOutputStream();
119 WsProjectResponse.Builder projectResponseBuilder = WsProjectResponse.newBuilder();
120 WsProjectResponse response = projectResponseBuilder.build();
121 response.writeTo(os);
123 return new ByteArrayInputStream(os.toByteArray());
127 public void readRealResponse() throws IOException {
128 InputStream is = getTestResource("project.protobuf");
129 when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
131 ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true, null);
132 FileData fd = proj.fileData("org.sonarsource.github:sonar-github-plugin",
133 "src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java");
135 assertThat(fd.revision()).isEqualTo("27bf2c54633d05c5df402bbe09471fe43bd9e2e5");
136 assertThat(fd.hash()).isEqualTo("edb6b3b9ab92d8dc53ba90ab86cd422e");
139 private InputStream getTestResource(String name) throws IOException {
140 return Resources.asByteSource(this.getClass().getResource(this.getClass().getSimpleName() + "/" + name))
141 .openBufferedStream();