diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-11-25 10:54:56 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-11-30 19:01:45 +0100 |
commit | bba16edb777a69137b3122dfbc5b898049f274c5 (patch) | |
tree | 17be034a5b93b109d2dd1148398225f77875e56b /sonar-batch | |
parent | 08dbeeee650d4792b6aaabb109ccd9504d282804 (diff) | |
download | sonarqube-bba16edb777a69137b3122dfbc5b898049f274c5.tar.gz sonarqube-bba16edb777a69137b3122dfbc5b898049f274c5.zip |
SONAR-7054 use ws-client in batch
Diffstat (limited to 'sonar-batch')
24 files changed, 669 insertions, 857 deletions
diff --git a/sonar-batch/pom.xml b/sonar-batch/pom.xml index e77766e5205..db6efc2bc68 100644 --- a/sonar-batch/pom.xml +++ b/sonar-batch/pom.xml @@ -100,10 +100,6 @@ <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> - <dependency> - <groupId>com.github.kevinsawicki</groupId> - <artifactId>http-request</artifactId> - </dependency> <!-- For HTML Report --> <dependency> <groupId>org.freemarker</groupId> @@ -118,7 +114,6 @@ <scope>test</scope> <version>${project.version}</version> </dependency> - <dependency> <groupId>com.google.code.bean-matchers</groupId> <artifactId>bean-matchers</artifactId> diff --git a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisWSLoaderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisWSLoaderProvider.java index ca0f521d42e..328478d3f87 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisWSLoaderProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisWSLoaderProvider.java @@ -21,19 +21,19 @@ package org.sonar.batch.analysis; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.batch.AnalysisMode; -import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.cache.WSLoader; import org.sonar.batch.cache.WSLoader.LoadStrategy; import org.sonar.home.cache.PersistentCache; +import org.sonarqube.ws.client.WsClient; public class AnalysisWSLoaderProvider extends ProviderAdapter { private WSLoader wsLoader; - public WSLoader provide(AnalysisProperties props, AnalysisMode mode, PersistentCache cache, ServerClient client) { + public WSLoader provide(AnalysisMode mode, PersistentCache cache, WsClient client) { if (wsLoader == null) { // recreate cache directory if needed for this analysis cache.reconfigure(); - wsLoader = new WSLoader(getStrategy(mode), cache, client, props); + wsLoader = new WSLoader(getStrategy(mode), cache, client); } return wsLoader; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginInstaller.java index 88003026d8e..56b4199e8e6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginInstaller.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginInstaller.java @@ -23,10 +23,12 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang.CharUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.SonarPlugin; @@ -39,6 +41,11 @@ import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.RemotePlugin; import org.sonar.core.platform.RemotePluginFile; import org.sonar.home.cache.FileCache; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.WsResponse; + +import static java.lang.String.format; /** * Downloads the plugins installed on server and stores them in a local user cache @@ -52,13 +59,13 @@ public class BatchPluginInstaller implements PluginInstaller { private final WSLoader wsLoader; private final FileCache fileCache; private final BatchPluginPredicate pluginPredicate; - private final ServerClient serverClient; + private final WsClient wsClient; - public BatchPluginInstaller(WSLoader wsLoader, ServerClient serverClient, FileCache fileCache, BatchPluginPredicate pluginPredicate) { + public BatchPluginInstaller(WSLoader wsLoader, WsClient wsClient, FileCache fileCache, BatchPluginPredicate pluginPredicate) { this.wsLoader = wsLoader; this.fileCache = fileCache; this.pluginPredicate = pluginPredicate; - this.serverClient = serverClient; + this.wsClient = wsClient; } @Override @@ -137,14 +144,17 @@ public class BatchPluginInstaller implements PluginInstaller { @Override public void download(String filename, File toFile) throws IOException { - String url = "/deploy/plugins/" + key + "/" + filename; + String url = format("/deploy/plugins/%s/%s", key, filename); if (LOG.isDebugEnabled()) { - LOG.debug("Download {} to {}", url, toFile.getAbsolutePath()); + LOG.debug("Download plugin {} to {}", filename, toFile); } else { LOG.info("Download {}", filename); } - serverClient.download(url, toFile); + WsResponse response = wsClient.wsConnector().call(new GetRequest(url)); + try (InputStream stream = response.getContentStream()) { + FileUtils.copyInputStreamToFile(stream, toFile); + } } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java index 035c7f489a9..40b5cacb9d1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java @@ -91,7 +91,7 @@ public class GlobalContainer extends ComponentContainer { CachesManager.class, GlobalSettings.class, - ServerClient.class, + new WsClientProvider(), DefaultServer.class, new GlobalTempFolderProvider(), DefaultHttpDownloader.class, diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java deleted file mode 100644 index ec34bfb7cef..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nullable; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringEscapeUtils; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.utils.HttpDownloader; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonar.core.util.DefaultHttpDownloader; - -/** - * Replace the deprecated org.sonar.batch.ServerMetadata - * TODO extends Server when removing the deprecated org.sonar.batch.ServerMetadata - * - * @since 3.4 - */ -@BatchSide -public class ServerClient { - private static final String GET = "GET"; - private GlobalProperties props; - private DefaultHttpDownloader.BaseHttpDownloader downloader; - - public ServerClient(GlobalProperties settings, EnvironmentInformation env) { - this.props = settings; - this.downloader = new DefaultHttpDownloader.BaseHttpDownloader(settings.properties(), env.toString()); - } - - public String getURL() { - return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/"); - } - - public URI getURI(String pathStartingWithSlash) { - Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /: " + pathStartingWithSlash); - String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash); - return URI.create(getURL() + path); - } - - public void download(String pathStartingWithSlash, File toFile) { - download(pathStartingWithSlash, toFile, null, null); - } - - public void download(String pathStartingWithSlash, File toFile, @Nullable Integer connectTimeoutMillis, @Nullable Integer readTimeoutMillis) { - try { - InputStream is = load(pathStartingWithSlash, GET, false, connectTimeoutMillis, readTimeoutMillis); - Files.copy(is, toFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch (HttpDownloader.HttpException he) { - throw handleHttpException(he.getUri().toString(), he.getResponseCode(), he.getResponseContent(), he); - } catch (IOException e) { - throw new IllegalStateException(String.format("Unable to download '%s' to: %s", pathStartingWithSlash, toFile), e); - } - } - - public String downloadString(String pathStartingWithSlash) { - return downloadString(pathStartingWithSlash, GET, true, null); - } - - public String downloadString(String pathStartingWithSlash, String requestMethod, boolean wrapHttpException, @Nullable Integer timeoutMillis) { - InputStream is = load(pathStartingWithSlash, requestMethod, wrapHttpException, null, timeoutMillis); - try { - return new String(IOUtils.toByteArray(is), "UTF-8"); - } catch (IOException e) { - throw new IllegalStateException(String.format("Unable to request: %s", pathStartingWithSlash), e); - } - } - - /** - * @throws IllegalStateException on I/O error, not limited to the network connection and if HTTP response code > 400 and wrapHttpException is true - * @throws HttpDownloader.HttpException if HTTP response code > 400 and wrapHttpException is false - */ - public InputStream load(String pathStartingWithSlash, String requestMethod, boolean wrapHttpException, @Nullable Integer connectTimeoutMs, - @Nullable Integer readTimeoutMs) { - URI uri = getURI(pathStartingWithSlash); - - try { - if (Strings.isNullOrEmpty(getLogin())) { - return downloader.newInputSupplier(uri, requestMethod, connectTimeoutMs, readTimeoutMs).getInput(); - } else { - return downloader.newInputSupplier(uri, requestMethod, getLogin(), getPassword(), connectTimeoutMs, readTimeoutMs).getInput(); - } - } catch (HttpDownloader.HttpException he) { - if (wrapHttpException) { - throw handleHttpException(he.getUri().toString(), he.getResponseCode(), he.getResponseContent(), he); - } else { - throw he; - } - } catch (IOException e) { - throw new IllegalStateException(String.format("Unable to request: %s", uri), e); - } - } - - public RuntimeException handleHttpException(String url, int responseCode, String responseContent, Exception he) { - if (responseCode == 401) { - return MessageException.of(String.format(getMessageWhenNotAuthorized(), CoreProperties.LOGIN, CoreProperties.PASSWORD), he); - } - if (responseCode == 403) { - // SONAR-4397 Details are in response content - return MessageException.of(tryParseAsJsonError(responseContent), he); - } - return MessageException.of(String.format("Fail to execute request [code=%s, url=%s]: %s", responseCode, url, responseContent), he); - } - - private static String tryParseAsJsonError(String responseContent) { - try { - JsonParser parser = new JsonParser(); - JsonObject obj = parser.parse(responseContent).getAsJsonObject(); - JsonArray errors = obj.getAsJsonArray("errors"); - List<String> errorMessages = new ArrayList<>(); - for (JsonElement e : errors) { - errorMessages.add(e.getAsJsonObject().get("msg").getAsString()); - } - return Joiner.on(", ").join(errorMessages); - } catch (Exception e) { - return responseContent; - } - } - - public String getMessageWhenNotAuthorized() { - if (Strings.isNullOrEmpty(getLogin()) && Strings.isNullOrEmpty(getPassword())) { - return "Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties %s and %s."; - } - return "Not authorized. Please check the properties %s and %s."; - } - - public String getLogin() { - return props.property(CoreProperties.LOGIN); - } - - public String getPassword() { - return props.property(CoreProperties.PASSWORD); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsClientLoggingInterceptor.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsClientLoggingInterceptor.java new file mode 100644 index 00000000000..f94bf084ee9 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsClientLoggingInterceptor.java @@ -0,0 +1,90 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.bootstrap; + +import com.google.common.base.Joiner; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.squareup.okhttp.Interceptor; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.CoreProperties; +import org.sonar.api.utils.MessageException; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; + +import static java.lang.String.format; + +public class WsClientLoggingInterceptor implements Interceptor { + + private static final Logger LOG = Loggers.get(WsClientLoggingInterceptor.class); + + @Override + public Response intercept(Chain chain) throws IOException { + Response response = logAndSendRequest(chain); + if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) { + if (StringUtils.isBlank(response.request().header("Authorization"))) { + // not authenticated - see https://jira.sonarsource.com/browse/SONAR-4048 + throw MessageException.of(format("Not authorized. Analyzing this project requires to be authenticated. " + + "Please provide the values of the properties %s and %s.", CoreProperties.LOGIN, CoreProperties.PASSWORD)); + } + // credentials are not valid + throw MessageException.of(format("Not authorized. Please check the properties %s and %s.", + CoreProperties.LOGIN, CoreProperties.PASSWORD)); + } + if (response.code() == HttpURLConnection.HTTP_FORBIDDEN) { + // SONAR-4397 Details are in response content + throw MessageException.of(tryParseAsJsonError(response.body().string())); + } + return response; + } + + private Response logAndSendRequest(Chain chain) throws IOException { + Request request = chain.request(); + Response response; + Profiler profiler = Profiler.createIfDebug(LOG).startTrace(format("%s %s", request.method(), request.url())); + response = chain.proceed(request); + profiler.stopDebug(format("%s %d %s", request.method(), response.code(), request.url())); + return response; + } + + private static String tryParseAsJsonError(String responseContent) { + try { + JsonParser parser = new JsonParser(); + JsonObject obj = parser.parse(responseContent).getAsJsonObject(); + JsonArray errors = obj.getAsJsonArray("errors"); + List<String> errorMessages = new ArrayList<>(); + for (JsonElement e : errors) { + errorMessages.add(e.getAsJsonObject().get("msg").getAsString()); + } + return Joiner.on(", ").join(errorMessages); + } catch (Exception e) { + return responseContent; + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsClientProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsClientProvider.java new file mode 100644 index 00000000000..78e50cc2ca2 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsClientProvider.java @@ -0,0 +1,63 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.bootstrap; + +import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.BatchSide; +import org.sonar.batch.bootstrapper.EnvironmentInformation; +import org.sonarqube.ws.client.HttpConnector; +import org.sonarqube.ws.client.HttpWsClient; +import org.sonarqube.ws.client.WsClient; + +import static java.lang.Integer.parseInt; +import static java.lang.String.valueOf; +import static org.apache.commons.lang.StringUtils.defaultIfBlank; + +@BatchSide +public class WsClientProvider extends ProviderAdapter { + + static final int CONNECT_TIMEOUT_MS = 5_000; + static final String READ_TIMEOUT_SEC_PROPERTY = "sonar.ws.timeout"; + static final int DEFAULT_READ_TIMEOUT_SEC = 60; + + private HttpWsClient wsClient; + + public synchronized WsClient provide(final GlobalProperties settings, final EnvironmentInformation env) { + if (wsClient == null) { + String url = defaultIfBlank(settings.property("sonar.host.url"), CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE); + HttpConnector.Builder builder = new HttpConnector.Builder(); + + // TODO proxy + + String timeoutSec = defaultIfBlank(settings.property(READ_TIMEOUT_SEC_PROPERTY), valueOf(DEFAULT_READ_TIMEOUT_SEC)); + builder + .readTimeoutMilliseconds(parseInt(timeoutSec) * 1_000) + .connectTimeoutMilliseconds(CONNECT_TIMEOUT_MS) + .userAgent(env.toString()) + .url(url) + .credentials(settings.property(CoreProperties.LOGIN), settings.property(CoreProperties.PASSWORD)) + .interceptor(new WsClientLoggingInterceptor()); + + wsClient = new HttpWsClient(builder.build()); + } + return wsClient; + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/StrategyWSLoaderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/cache/StrategyWSLoaderProvider.java index e64015db867..e7c573753df 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/StrategyWSLoaderProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/cache/StrategyWSLoaderProvider.java @@ -20,10 +20,9 @@ package org.sonar.batch.cache; import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.cache.WSLoader.LoadStrategy; import org.sonar.home.cache.PersistentCache; +import org.sonarqube.ws.client.WsClient; public class StrategyWSLoaderProvider extends ProviderAdapter { private final LoadStrategy strategy; @@ -33,9 +32,9 @@ public class StrategyWSLoaderProvider extends ProviderAdapter { this.strategy = strategy; } - public WSLoader provide(PersistentCache cache, ServerClient client, GlobalProperties globalProps) { + public WSLoader provide(PersistentCache cache, WsClient client) { if (wsLoader == null) { - wsLoader = new WSLoader(strategy, cache, client, globalProps); + wsLoader = new WSLoader(strategy, cache, client); } return wsLoader; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoader.java b/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoader.java index 13845f0808e..69e7c14538f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoader.java @@ -21,56 +21,53 @@ package org.sonar.batch.cache; import java.io.IOException; import java.io.InputStream; +import java.io.Reader; import java.nio.charset.StandardCharsets; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; -import org.sonar.api.utils.HttpDownloader.HttpException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.bootstrap.ServerClient; -import org.sonar.batch.bootstrap.UserProperties; import org.sonar.home.cache.PersistentCache; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpException; +import org.sonarqube.ws.client.WsClient; import static org.sonar.batch.cache.WSLoader.ServerStatus.ACCESSIBLE; import static org.sonar.batch.cache.WSLoader.ServerStatus.NOT_ACCESSIBLE; import static org.sonar.batch.cache.WSLoader.ServerStatus.UNKNOWN; public class WSLoader { - static final String SONAR_WS_TIMEOUT_PROPS = "sonar.ws.timeout"; private static final Logger LOG = Loggers.get(WSLoader.class); private static final String FAIL_MSG = "Server is not accessible and data is not cached"; - private static final int CONNECT_TIMEOUT = 5_000; - private static final int DEFAULT_READ_TIMEOUT = 60_000; - private static final String REQUEST_METHOD = "GET"; public enum ServerStatus { - UNKNOWN, ACCESSIBLE, NOT_ACCESSIBLE; + UNKNOWN, ACCESSIBLE, NOT_ACCESSIBLE } public enum LoadStrategy { - SERVER_FIRST, CACHE_FIRST, SERVER_ONLY, CACHE_ONLY; + SERVER_FIRST, CACHE_FIRST, SERVER_ONLY, CACHE_ONLY } private final LoadStrategy defautLoadStrategy; - private final ServerClient client; + private final WsClient client; private final PersistentCache cache; - private final UserProperties userProperties; private ServerStatus serverStatus; private DataLoader<String> stringServerLoader = new DataLoader<String>() { @Override public String load(String id) throws IOException { - InputStream is = client.load(id, REQUEST_METHOD, true, CONNECT_TIMEOUT, getReadTimeout()); - String str = IOUtils.toString(is, StandardCharsets.UTF_8); - try { - cache.put(id, str.getBytes(StandardCharsets.UTF_8)); - } catch (IOException e) { - throw new IllegalStateException("Error saving to WS cache", e); + GetRequest getRequest = new GetRequest(id); + try (Reader reader = client.wsConnector().call(getRequest).getContentReader()) { + String str = IOUtils.toString(reader); + try { + cache.put(id, str.getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + throw new IllegalStateException("Error saving to WS cache", e); + } + return str; } - return str; } - }; private DataLoader<String> stringCacheLoader = new DataLoader<String>() { @@ -83,13 +80,14 @@ public class WSLoader { private DataLoader<InputStream> streamServerLoader = new DataLoader<InputStream>() { @Override public InputStream load(String id) throws IOException { - InputStream is = client.load(id, REQUEST_METHOD, true, CONNECT_TIMEOUT, getReadTimeout()); - try { - cache.put(id, is); - } catch (IOException e) { - throw new IllegalStateException("Error saving to WS cache", e); + GetRequest getRequest = new GetRequest(id); + try (InputStream is = client.wsConnector().call(getRequest).getContentStream()) { + try { + cache.put(id, is); + } catch (IOException e) { + throw new IllegalStateException("Error saving to WS cache", e); + } } - is.close(); return cache.getStream(id); } }; @@ -101,7 +99,7 @@ public class WSLoader { } }; - private class NotAvailableException extends Exception { + private static class NotAvailableException extends Exception { private static final long serialVersionUID = 1L; public NotAvailableException(String message) { @@ -113,18 +111,13 @@ public class WSLoader { } } - public WSLoader(LoadStrategy strategy, PersistentCache cache, ServerClient client, UserProperties settings) { + public WSLoader(LoadStrategy strategy, PersistentCache cache, WsClient client) { this.defautLoadStrategy = strategy; - this.userProperties = settings; this.serverStatus = UNKNOWN; this.cache = cache; this.client = client; } - private int getReadTimeout() { - return userProperties.properties().containsKey(SONAR_WS_TIMEOUT_PROPS) ? (Integer.parseInt(userProperties.property(SONAR_WS_TIMEOUT_PROPS)) * 1000) : DEFAULT_READ_TIMEOUT; - } - @Nonnull public WSLoaderResult<InputStream> loadStream(String id) { return load(id, defautLoadStrategy, streamServerLoader, streamCacheLoader); @@ -200,7 +193,7 @@ public class WSLoader { throw new IllegalStateException(FAIL_MSG, serverNotAvailable.getCause()); } } - throw new IllegalStateException("Server is not available", serverNotAvailable.getCause()); + throw new IllegalStateException("Server is not available: " + client.wsConnector().baseUrl(), serverNotAvailable.getCause()); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java index 36c3324374b..209dd29712f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java @@ -19,71 +19,63 @@ */ package org.sonar.batch.report; -import com.github.kevinsawicki.http.HttpRequest; import com.google.common.annotations.VisibleForTesting; -import com.google.gson.Gson; -import java.io.BufferedWriter; +import com.google.common.io.Files; +import com.squareup.okhttp.HttpUrl; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStreamWriter; import java.io.Writer; -import java.net.MalformedURLException; -import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.util.Date; -import javax.annotation.CheckForNull; +import java.util.LinkedHashMap; +import java.util.Map; import javax.annotation.Nullable; import org.apache.commons.io.FileUtils; import org.picocontainer.Startable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.batch.BatchSide; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; -import org.sonar.api.platform.Server; import org.sonar.api.utils.TempFolder; import org.sonar.api.utils.ZipUtils; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.text.JsonWriter; import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.protocol.output.BatchReportWriter; import org.sonar.batch.scan.ImmutableProjectReactor; -import org.sonar.batch.util.BatchUtils; +import org.sonarqube.ws.WsCe; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.ce.SubmitWsRequest; -import static java.lang.String.format; +import static org.apache.commons.lang.StringUtils.defaultIfBlank; @BatchSide public class ReportPublisher implements Startable { - private static final Logger LOG = LoggerFactory.getLogger(ReportPublisher.class); + private static final Logger LOG = Loggers.get(ReportPublisher.class); + public static final String KEEP_REPORT_PROP_KEY = "sonar.batch.keepReport"; public static final String VERBOSE_KEY = "sonar.verbose"; - public static final String DUMP_REPORT_PROP_KEY = "sonar.batch.dumpReportDir"; - public static final String JSON_DETAILS_FILE = "analysis-details.json"; + public static final String METADATA_DUMP_FILENAME = "analysis-details.json"; - private final ServerClient serverClient; - private final Server server; private final Settings settings; + private final WsClient wsClient; + private final AnalysisContextReportPublisher contextPublisher; private final ImmutableProjectReactor projectReactor; private final DefaultAnalysisMode analysisMode; private final TempFolder temp; - private final AnalysisContextReportPublisher contextPublisher; - - private ReportPublisherStep[] publishers; + private final ReportPublisherStep[] publishers; private File reportDir; private BatchReportWriter writer; - public ReportPublisher(Settings settings, ServerClient serverClient, Server server, AnalysisContextReportPublisher contextPublisher, + public ReportPublisher(Settings settings, WsClient wsClient, AnalysisContextReportPublisher contextPublisher, ImmutableProjectReactor projectReactor, DefaultAnalysisMode analysisMode, TempFolder temp, ReportPublisherStep[] publishers) { - this.serverClient = serverClient; - this.server = server; + this.settings = settings; + this.wsClient = wsClient; this.contextPublisher = contextPublisher; this.projectReactor = projectReactor; - this.settings = settings; this.analysisMode = analysisMode; this.temp = temp; this.publishers = publishers; @@ -101,7 +93,7 @@ public class ReportPublisher implements Startable { if (!settings.getBoolean(KEEP_REPORT_PROP_KEY) && !settings.getBoolean(VERBOSE_KEY)) { FileUtils.deleteQuietly(reportDir); } else { - LOG.info("Batch report generated in " + reportDir); + LOG.info("Analysis report generated in " + reportDir); } } @@ -117,22 +109,22 @@ public class ReportPublisher implements Startable { // If this is a issues mode analysis then we should not upload reports String taskId = null; if (!analysisMode.isIssues()) { - File report = prepareReport(); + File report = generateReportFile(); if (!analysisMode.isMediumTest()) { - taskId = sendOrDumpReport(report); + taskId = upload(report); } } - logSuccess(LoggerFactory.getLogger(getClass()), taskId); + logSuccess(taskId); } - private File prepareReport() { + private File generateReportFile() { try { long startTime = System.currentTimeMillis(); for (ReportPublisherStep publisher : publishers) { publisher.publish(writer); } long stopTime = System.currentTimeMillis(); - LOG.info("Analysis reports generated in " + (stopTime - startTime) + "ms, dir size=" + FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(reportDir))); + LOG.info("Analysis report generated in " + (stopTime - startTime) + "ms, dir size=" + FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(reportDir))); startTime = System.currentTimeMillis(); File reportZip = temp.newFile("batch-report", ".zip"); @@ -141,129 +133,81 @@ public class ReportPublisher implements Startable { LOG.info("Analysis reports compressed in " + (stopTime - startTime) + "ms, zip size=" + FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(reportZip))); return reportZip; } catch (IOException e) { - throw new IllegalStateException("Unable to prepare batch report", e); + throw new IllegalStateException("Unable to prepare analysis report", e); } } - @CheckForNull + /** + * Uploads the report file to server and returns the generated task id + */ @VisibleForTesting - String sendOrDumpReport(File report) { - ProjectDefinition projectDefinition = projectReactor.getRoot(); - String effectiveKey = projectDefinition.getKeyWithBranch(); - String relativeUrl = String.format("/api/ce/submit?projectKey=%s&projectName=%s&projectBranch=%s", - projectDefinition.getKey(), BatchUtils.encodeForUrl(projectDefinition.getName()), BatchUtils.encodeForUrl(projectDefinition.getBranch())); - - String dumpDirLocation = settings.getString(DUMP_REPORT_PROP_KEY); - if (dumpDirLocation == null) { - return uploadMultiPartReport(report, relativeUrl); - } else { - dumpReport(dumpDirLocation, effectiveKey, relativeUrl, report); - return null; - } - } - - private String uploadMultiPartReport(File report, String relativeUrl) { - LOG.debug("Publish results"); + String upload(File report) { + LOG.debug("Upload report"); long startTime = System.currentTimeMillis(); - URL url; - try { - url = new URL(serverClient.getURL() + relativeUrl); - } catch (MalformedURLException e) { - throw new IllegalArgumentException("Invalid URL", e); - } - HttpRequest request = HttpRequest.post(url); - request.trustAllCerts(); - request.trustAllHosts(); - request.header("User-Agent", format("SonarQube %s", server.getVersion())); - request.basic(serverClient.getLogin(), serverClient.getPassword()); - request.part("report", null, "application/octet-stream", report); - if (!request.ok()) { - throw serverClient.handleHttpException(url.toString(), request.code(), request.body(), null); - } + ProjectDefinition projectDefinition = projectReactor.getRoot(); + SubmitWsRequest submitRequest = new SubmitWsRequest(); + submitRequest.setProjectKey(projectDefinition.getKey()); + submitRequest.setProjectName(projectDefinition.getName()); + submitRequest.setProjectBranch(projectDefinition.getBranch()); + submitRequest.setReport(report); + WsCe.SubmitResponse submitResponse = wsClient.computeEngine().submit(submitRequest); long stopTime = System.currentTimeMillis(); - LOG.info("Analysis reports sent to server in " + (stopTime - startTime) + "ms"); - String responseStr = request.body(); - SubmitResponse response = new Gson().fromJson(responseStr, SubmitResponse.class); - return response.getTaskId(); - } - - private static class SubmitResponse { - private String taskId; - - public String getTaskId() { - return taskId; - } - - public void setTaskId(String taskId) { - this.taskId = taskId; - } - } - - private static void dumpReport(String dumpDirLocation, String projectKey, String relativeUrl, File report) { - LOG.debug("Dump report to file"); - try { - dumpReportImpl(dumpDirLocation, projectKey, relativeUrl, report); - } catch (IOException | URISyntaxException e) { - LOG.error("Failed to dump report to directory " + dumpDirLocation, e); - } - } - - private static void dumpReportImpl(String dumpDirLocation, String projectKey, String relativeUrl, File report) throws IOException, URISyntaxException { - File dumpDir = new File(dumpDirLocation); - if (!dumpDir.exists() || !dumpDir.isDirectory()) { - LOG.warn("Report dump directory '{}' does not exist or is not a directory", dumpDirLocation); - return; - } - long dateTime = new Date().getTime(); - File dumpedZip = new File(dumpDir, format("batch-report_%s_%s.zip", projectKey, dateTime)); - FileUtils.copyFile(report, new FileOutputStream(dumpedZip)); - File dumpedMetadata = new File(dumpDir, format("batch-report_%s_%s.txt", projectKey, dateTime)); - FileUtils.write(dumpedMetadata, relativeUrl); - LOG.info("Batch report dumped to {}", dumpedZip.getAbsolutePath()); + LOG.info("Analysis report uploaded in " + (stopTime - startTime) + "ms"); + return submitResponse.getTaskId(); } @VisibleForTesting - void logSuccess(Logger logger, @Nullable String taskId) { - if (analysisMode.isIssues() || analysisMode.isMediumTest()) { - logger.info("ANALYSIS SUCCESSFUL"); - + void logSuccess(@Nullable String taskId) { + if (taskId == null) { + LOG.info("ANALYSIS SUCCESSFUL"); } else { - String baseUrl = settings.getString(CoreProperties.SERVER_BASE_URL); - if (baseUrl.equals(settings.getDefaultValue(CoreProperties.SERVER_BASE_URL))) { - // If server base URL was not configured in Sonar server then is is better to take URL configured on batch side - baseUrl = serverClient.getURL(); - } - if (!baseUrl.endsWith("/")) { - baseUrl += "/"; - } + Map<String, String> metadata = new LinkedHashMap<>(); String effectiveKey = projectReactor.getRoot().getKeyWithBranch(); - String url = baseUrl + "dashboard/index/" + effectiveKey; - logger.info("ANALYSIS SUCCESSFUL, you can browse {}", url); - logger.info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report."); - String taskUrl = null; - if (taskId != null) { - taskUrl = baseUrl + "api/ce/task?id=" + taskId; - logger.info("More about the report processing at {}", taskUrl); - } - - writeJson(url, taskUrl); + metadata.put("projectKey", effectiveKey); + + URL dashboardUrl = HttpUrl.parse(publicUrl()).newBuilder() + .addPathSegment("dashboard").addPathSegment("index").addPathSegment(effectiveKey) + .build() + .url(); + metadata.put("dashboardUrl", dashboardUrl.toExternalForm()); + + URL taskUrl = HttpUrl.parse(publicUrl()).newBuilder() + .addPathSegment("api").addPathSegment("ce").addPathSegment("task") + .addQueryParameter("id", taskId) + .build() + .url(); + metadata.put("ceTaskId", taskId); + metadata.put("ceTaskUrl", taskUrl.toExternalForm()); + + LOG.info("ANALYSIS SUCCESSFUL, you can browse {}", dashboardUrl); + LOG.info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report"); + LOG.info("More about the report processing at {}", taskUrl); + + dumpMetadata(metadata); } } - private void writeJson(String dashboardUrl, @Nullable String taskUrl) { - File exportFile = new File(projectReactor.getRoot().getWorkDir(), JSON_DETAILS_FILE); - try (Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile), StandardCharsets.UTF_8))) { + private void dumpMetadata(Map<String, String> metadata) { + File file = new File(projectReactor.getRoot().getWorkDir(), METADATA_DUMP_FILENAME); + try (Writer output = Files.newWriter(file, StandardCharsets.UTF_8)) { JsonWriter json = JsonWriter.of(output); json.beginObject(); - json.prop("dashboardUrl", dashboardUrl); - if (taskUrl != null) { - json.prop("ceTaskUrl", taskUrl); + for (Map.Entry<String, String> entry : metadata.entrySet()) { + json.prop(entry.getKey(), entry.getValue()); } json.endObject(); - LOG.debug("Analysis URLs written to {}", exportFile); + LOG.debug("Report metadata written to {}", file); } catch (IOException e) { - throw new IllegalStateException("Unable to write analysis URLs in file " + exportFile.getAbsolutePath(), e); + throw new IllegalStateException("Unable to dump " + file, e); } } + + /** + * The public URL is optionally configured on server. If not, then the regular URL is returned. + * See https://jira.sonarsource.com/browse/SONAR-4239 + */ + private String publicUrl() { + return defaultIfBlank(settings.getString(CoreProperties.SERVER_BASE_URL), wsClient.wsConnector().baseUrl()); + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java index b31741f1115..3a00c304aaa 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java @@ -21,12 +21,12 @@ package org.sonar.batch.repository; import com.google.common.base.Throwables; -import org.sonar.api.utils.HttpDownloader.HttpException; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; import java.util.Date; import java.util.Map; @@ -42,6 +42,7 @@ import org.sonar.batch.util.BatchUtils; import org.sonarqube.ws.WsBatch.WsProjectResponse; import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath; import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings; +import org.sonarqube.ws.client.HttpException; public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader { private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class); @@ -85,7 +86,7 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad for (Throwable t : Throwables.getCausalChain(e)) { if (t instanceof HttpException) { HttpException http = (HttpException) t; - return http.getResponseCode() != 404; + return http.code() != HttpURLConnection.HTTP_NOT_FOUND; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java index bd382543e52..8f6e0dd5e07 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java @@ -32,9 +32,10 @@ import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; -import java.util.Collection; import java.util.List; +import static com.google.common.base.Preconditions.checkState; + public class DefaultQualityProfileLoader implements QualityProfileLoader { private static final String WS_URL = "/api/qualityprofiles/search.protobuf"; @@ -62,12 +63,6 @@ public class DefaultQualityProfileLoader implements QualityProfileLoader { return loadResource(url, fromCache); } - private static void validate(Collection<QualityProfile> profiles) { - if (profiles == null || profiles.isEmpty()) { - throw new IllegalStateException("No quality profiles has been found this project, you probably don't have any language plugin suitable for this analysis."); - } - } - private List<QualityProfile> loadResource(String url, @Nullable MutableBoolean fromCache) { WSLoaderResult<InputStream> result = wsLoader.loadStream(url); if (fromCache != null) { @@ -85,7 +80,8 @@ public class DefaultQualityProfileLoader implements QualityProfileLoader { } List<QualityProfile> profilesList = profiles.getProfilesList(); - validate(profilesList); + checkState(profilesList != null && !profilesList.isEmpty(), + "No quality profiles has been found this project, you probably don't have any language plugin suitable for this analysis."); return profilesList; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/analysis/AnalysisWSLoaderProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/analysis/AnalysisWSLoaderProviderTest.java index 7624edf7b28..5687ebdc27c 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/analysis/AnalysisWSLoaderProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/analysis/AnalysisWSLoaderProviderTest.java @@ -19,17 +19,15 @@ */ package org.sonar.batch.analysis; -import java.util.HashMap; -import java.util.Map; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.sonar.api.batch.AnalysisMode; -import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.cache.WSLoader; import org.sonar.batch.cache.WSLoader.LoadStrategy; import org.sonar.home.cache.PersistentCache; +import org.sonarqube.ws.client.WsClient; import static org.assertj.core.api.Assertions.assertThat; @@ -38,27 +36,22 @@ public class AnalysisWSLoaderProviderTest { private PersistentCache cache; @Mock - private ServerClient client; + private WsClient client; @Mock private AnalysisMode mode; private AnalysisWSLoaderProvider loaderProvider; - private Map<String, String> propMap; - private AnalysisProperties props; @Before public void setUp() { MockitoAnnotations.initMocks(this); loaderProvider = new AnalysisWSLoaderProvider(); - propMap = new HashMap<>(); } @Test public void testDefault() { - props = new AnalysisProperties(propMap, null); - - WSLoader loader = loaderProvider.provide(props, mode, cache, client); + WSLoader loader = loaderProvider.provide(mode, cache, client); assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.SERVER_ONLY); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java index 654616811d5..fdc159569a0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginInstallerTest.java @@ -19,18 +19,17 @@ */ package org.sonar.batch.bootstrap; -import org.sonar.batch.cache.WSLoaderResult; - -import org.sonar.batch.cache.WSLoader; +import java.io.File; +import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.sonar.batch.cache.WSLoader; +import org.sonar.batch.cache.WSLoaderResult; import org.sonar.core.platform.RemotePlugin; import org.sonar.home.cache.FileCache; - -import java.io.File; -import java.util.List; +import org.sonarqube.ws.client.WsClient; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; @@ -48,15 +47,15 @@ public class BatchPluginInstallerTest { public ExpectedException thrown = ExpectedException.none(); FileCache fileCache = mock(FileCache.class); - ServerClient serverClient = mock(ServerClient.class); + WsClient wsClient = mock(WsClient.class); BatchPluginPredicate pluginPredicate = mock(BatchPluginPredicate.class); @Test public void listRemotePlugins() { WSLoader wsLoader = mock(WSLoader.class); - when(wsLoader.loadString("/deploy/plugins/index.txt")).thenReturn(new WSLoaderResult<String>("checkstyle\nsqale", true)); - BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, serverClient, fileCache, pluginPredicate); + when(wsLoader.loadString("/deploy/plugins/index.txt")).thenReturn(new WSLoaderResult<>("checkstyle\nsqale", true)); + BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, wsClient, fileCache, pluginPredicate); List<RemotePlugin> remotePlugins = installer.listRemotePlugins(); assertThat(remotePlugins).extracting("key").containsOnly("checkstyle", "sqale"); @@ -68,7 +67,7 @@ public class BatchPluginInstallerTest { when(fileCache.get(eq("checkstyle-plugin.jar"), eq("fakemd5_1"), any(FileCache.Downloader.class))).thenReturn(pluginJar); WSLoader wsLoader = mock(WSLoader.class); - BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, serverClient, fileCache, pluginPredicate); + BatchPluginInstaller installer = new BatchPluginInstaller(wsLoader, wsClient, fileCache, pluginPredicate); RemotePlugin remote = new RemotePlugin("checkstyle").setFile("checkstyle-plugin.jar", "fakemd5_1"); File file = installer.download(remote); @@ -83,6 +82,6 @@ public class BatchPluginInstallerTest { WSLoader wsLoader = mock(WSLoader.class); doThrow(new IllegalStateException()).when(wsLoader).loadString("/deploy/plugins/index.txt"); - new BatchPluginInstaller(wsLoader, serverClient, fileCache, pluginPredicate).installRemotes(); + new BatchPluginInstaller(wsLoader, wsClient, fileCache, pluginPredicate).installRemotes(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java deleted file mode 100644 index ef7f4c58a1d..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.sonar.batch.util.BatchUtils; - -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; - -import org.junit.After; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; -import org.sonar.batch.bootstrapper.EnvironmentInformation; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class ServerClientTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - @Rule - public ExpectedException thrown = ExpectedException.none(); - - private MockHttpServer server = null; - private GlobalProperties bootstrapProps = mock(GlobalProperties.class); - - @After - public void stopServer() { - if (server != null) { - server.stop(); - } - } - - @Test - public void should_remove_url_ending_slash() { - GlobalProperties settings = mock(GlobalProperties.class); - when(settings.property("sonar.host.url")).thenReturn("http://localhost:8080/sonar/"); - ServerClient client = new ServerClient(settings, new EnvironmentInformation("Junit", "4")); - - assertThat(client.getURL()).isEqualTo("http://localhost:8080/sonar"); - } - - @Test - public void should_request_url() throws Exception { - startServer(null, "this is the content"); - assertThat(newServerClient().downloadString("/foo")).isEqualTo("this is the content"); - } - - @Test - public void should_escape_html_from_url() throws Exception { - startServer(null, "this is the content"); - assertThat(newServerClient().downloadString("/<foo>")).isEqualTo("this is the content"); - } - - @Test - public void should_download_file() throws Exception { - startServer(null, "this is the content"); - File file = temp.newFile(); - newServerClient().download("/foo", file); - assertThat(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)).isEqualTo("this is the content"); - } - - @Test - public void should_fail_if_unauthorized_with_no_login_password() throws Exception { - startServer(401, null); - thrown.expectMessage("Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties sonar.login and sonar.password."); - newServerClient().downloadString("/foo"); - } - - @Test - public void should_fail_if_unauthorized_with_login_password_provided() throws Exception { - startServer(401, null); - - when(bootstrapProps.property(eq("sonar.login"))).thenReturn("login"); - when(bootstrapProps.property(eq("sonar.password"))).thenReturn("password"); - - thrown.expectMessage("Not authorized. Please check the properties sonar.login and sonar.password"); - newServerClient().downloadString("/foo"); - } - - @Test - public void should_display_json_error_when_403() throws Exception { - startServer(403, "{\"errors\":[{\"msg\":\"Insufficient privileges\"}]}"); - thrown.expectMessage("Insufficient privileges"); - newServerClient().downloadString("/foo"); - } - - @Test - public void should_fail_if_error() throws Exception { - startServer(500, null); - thrown.expectMessage("Fail to execute request [code=500, url=http://localhost:" + server.getPort() + "/foo]"); - newServerClient().downloadString("/foo"); - } - - @Test - public void string_encode() { - assertThat(BatchUtils.encodeForUrl("my value")).isEqualTo("my+value"); - } - - private ServerClient newServerClient() { - when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort()); - return new ServerClient(bootstrapProps, new EnvironmentInformation("Junit", "4")); - } - - private void startServer(Integer responseStatus, String responseData) throws Exception { - server = new MockHttpServer(); - server.start(); - - if (responseStatus != null) { - server.setMockResponseStatus(responseStatus); - } - if (responseData != null) { - server.setMockResponseData(responseData); - } - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WsClientLoggingInterceptorTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WsClientLoggingInterceptorTest.java new file mode 100644 index 00000000000..4655371e88e --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WsClientLoggingInterceptorTest.java @@ -0,0 +1,129 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.bootstrap; + +import com.squareup.okhttp.Interceptor; +import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.Protocol; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; +import com.squareup.okhttp.ResponseBody; +import java.util.List; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.MessageException; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class WsClientLoggingInterceptorTest { + + @Rule + public LogTester logTester = new LogTester(); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + WsClientLoggingInterceptor underTest = new WsClientLoggingInterceptor(); + Interceptor.Chain chain = mock(Interceptor.Chain.class); + + @Test + public void log_and_profile_request_if_debug_level() throws Exception { + Request request = newRequest(); + Response response = newResponse(request, 200, ""); + when(chain.request()).thenReturn(request); + when(chain.proceed(request)).thenReturn(response); + + logTester.setLevel(LoggerLevel.DEBUG); + Response result = underTest.intercept(chain); + + // do not fail the execution -> interceptor returns the response + assertThat(result).isSameAs(response); + + // check logs + List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG); + assertThat(debugLogs).hasSize(1); + assertThat(debugLogs.get(0)).contains("GET 200 https://localhost:9000/api/issues/search | time="); + List<String> traceLogs = logTester.logs(LoggerLevel.TRACE); + assertThat(traceLogs).hasSize(1); + assertThat(traceLogs.get(0)).isEqualTo("GET https://localhost:9000/api/issues/search"); + } + + @Test + public void fail_if_requires_authentication() throws Exception { + expectedException.expect(MessageException.class); + expectedException + .expectMessage("Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties sonar.login and sonar.password."); + + Request request = newRequest(); + Response response = newResponse(request, 401, ""); + when(chain.request()).thenReturn(request); + when(chain.proceed(request)).thenReturn(response); + + underTest.intercept(chain); + } + + @Test + public void fail_if_credentials_are_not_valid() throws Exception { + expectedException.expect(MessageException.class); + expectedException.expectMessage("Not authorized. Please check the properties sonar.login and sonar.password."); + + Request request = new Request.Builder() + .url("https://localhost:9000/api/issues/search") + .header("Authorization", "Basic BAD_CREDENTIALS") + .get() + .build(); + Response response = newResponse(request, 401, ""); + when(chain.request()).thenReturn(request); + when(chain.proceed(request)).thenReturn(response); + + underTest.intercept(chain); + } + + @Test + public void fail_if_requires_permission() throws Exception { + expectedException.expect(MessageException.class); + expectedException.expectMessage("missing scan permission, missing another permission"); + + Request request = newRequest(); + Response response = newResponse(request, 403, "{\"errors\":[{\"msg\":\"missing scan permission\"}, {\"msg\":\"missing another permission\"}]}"); + when(chain.request()).thenReturn(request); + when(chain.proceed(request)).thenReturn(response); + + underTest.intercept(chain); + } + + private Request newRequest() { + return new Request.Builder().url("https://localhost:9000/api/issues/search").get().build(); + } + + private Response newResponse(Request getRequest, int code, String jsonBody) { + return new Response.Builder().request(getRequest) + .code(code) + .protocol(Protocol.HTTP_1_1) + .body(ResponseBody.create(MediaType.parse("application/json"), jsonBody)) + .build(); + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WsClientProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WsClientProviderTest.java new file mode 100644 index 00000000000..ac5a9c86683 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/WsClientProviderTest.java @@ -0,0 +1,81 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.bootstrap; + +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.sonar.batch.bootstrapper.EnvironmentInformation; +import org.sonarqube.ws.client.HttpConnector; +import org.sonarqube.ws.client.WsClient; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.batch.bootstrap.WsClientProvider.CONNECT_TIMEOUT_MS; +import static org.sonar.batch.bootstrap.WsClientProvider.DEFAULT_READ_TIMEOUT_SEC; + +public class WsClientProviderTest { + + WsClientProvider underTest = new WsClientProvider(); + EnvironmentInformation env = new EnvironmentInformation("Maven Plugin", "2.3"); + + @Test + public void provide_client_with_default_settings() { + GlobalProperties settings = new GlobalProperties(new HashMap<String, String>()); + + WsClient client = underTest.provide(settings, env); + + assertThat(client).isNotNull(); + HttpConnector httpConnector = (HttpConnector) client.wsConnector(); + assertThat(httpConnector.baseUrl()).isEqualTo("http://localhost:9000/"); + assertThat(httpConnector.okHttpClient().getProxy()).isNull(); + assertThat(httpConnector.okHttpClient().getConnectTimeout()).isEqualTo(5_000); + assertThat(httpConnector.okHttpClient().getReadTimeout()).isEqualTo(60_000); + assertThat(httpConnector.userAgent()).isEqualTo("Maven Plugin/2.3"); + assertThat(httpConnector.okHttpClient().interceptors()) + .hasSize(1) + .hasOnlyElementsOfType(WsClientLoggingInterceptor.class); + } + + @Test + public void provide_client_with_custom_settings() { + Map<String, String> props = new HashMap<>(); + props.put("sonar.host.url", "https://here/sonarqube"); + props.put("sonar.login", "theLogin"); + props.put("sonar.password", "thePassword"); + props.put("sonar.ws.timeout", "42"); + GlobalProperties settings = new GlobalProperties(props); + + WsClient client = underTest.provide(settings, env); + + assertThat(client).isNotNull(); + HttpConnector httpConnector = (HttpConnector) client.wsConnector(); + assertThat(httpConnector.baseUrl()).isEqualTo("https://here/sonarqube/"); + assertThat(httpConnector.okHttpClient().getProxy()).isNull(); + assertThat(httpConnector.userAgent()).isEqualTo("Maven Plugin/2.3"); + } + + @Test + public void build_singleton() { + GlobalProperties settings = new GlobalProperties(new HashMap<String, String>()); + WsClient first = underTest.provide(settings, env); + WsClient second = underTest.provide(settings, env); + assertThat(first).isSameAs(second); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/DefaultProjectCacheStatusTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/DefaultProjectCacheStatusTest.java index 8d86733a01d..831a0622c1d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/DefaultProjectCacheStatusTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/DefaultProjectCacheStatusTest.java @@ -19,24 +19,22 @@ */ package org.sonar.batch.cache; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.assertj.core.api.Assertions.assertThat; import com.google.common.io.Files; -import org.junit.rules.ExpectedException; - import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Date; - +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.junit.Rule; -import org.junit.Before; -import org.sonar.batch.bootstrap.ServerClient; import org.sonar.home.cache.PersistentCache; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class DefaultProjectCacheStatusTest { @Rule public TemporaryFolder tmp = new TemporaryFolder(); @@ -45,13 +43,10 @@ public class DefaultProjectCacheStatusTest { public ExpectedException exception = ExpectedException.none(); ProjectCacheStatus cacheStatus; - PersistentCache cache; - ServerClient client; + PersistentCache cache = mock(PersistentCache.class); @Before public void setUp() { - client = mock(ServerClient.class); - cache = mock(PersistentCache.class); when(cache.getDirectory()).thenReturn(tmp.getRoot().toPath()); cacheStatus = new DefaultProjectCacheStatus(cache); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectSyncContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectSyncContainerTest.java index 15833a06ad5..560ce56a173 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectSyncContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/ProjectSyncContainerTest.java @@ -19,22 +19,20 @@ */ package org.sonar.batch.cache; -import org.sonar.batch.protocol.input.ProjectRepositories; - +import java.util.HashMap; +import org.junit.Test; import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.bootstrap.ServerClient; +import org.sonar.batch.protocol.input.ProjectRepositories; +import org.sonar.core.platform.ComponentContainer; import org.sonar.home.cache.PersistentCache; - -import java.util.HashMap; +import org.sonarqube.ws.client.WsClient; import static org.mockito.Mockito.mock; -import org.sonar.core.platform.ComponentContainer; -import org.junit.Test; public class ProjectSyncContainerTest { private ComponentContainer createParentContainer() { PersistentCache cache = mock(PersistentCache.class); - ServerClient server = mock(ServerClient.class); + WsClient server = mock(WsClient.class); GlobalProperties globalProps = new GlobalProperties(new HashMap<String, String>()); ComponentContainer parent = new ComponentContainer(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/StrategyWSLoaderProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/StrategyWSLoaderProviderTest.java index 5d9ed445822..4134b7f6653 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/StrategyWSLoaderProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/StrategyWSLoaderProviderTest.java @@ -23,33 +23,28 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.cache.WSLoader.LoadStrategy; import org.sonar.home.cache.PersistentCache; +import org.sonarqube.ws.client.WsClient; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; public class StrategyWSLoaderProviderTest { @Mock private PersistentCache cache; @Mock - private ServerClient client; - - private GlobalProperties globalProps; + private WsClient client; @Before public void setUp() { MockitoAnnotations.initMocks(this); - globalProps = mock(GlobalProperties.class); } @Test public void testStrategy() { StrategyWSLoaderProvider provider = new StrategyWSLoaderProvider(LoadStrategy.CACHE_FIRST); - WSLoader wsLoader = provider.provide(cache, client, globalProps); + WSLoader wsLoader = provider.provide(cache, client); assertThat(wsLoader.getDefaultStrategy()).isEqualTo(LoadStrategy.CACHE_FIRST); } @@ -57,8 +52,8 @@ public class StrategyWSLoaderProviderTest { @Test public void testSingleton() { StrategyWSLoaderProvider provider = new StrategyWSLoaderProvider(LoadStrategy.CACHE_FIRST); - WSLoader wsLoader = provider.provide(cache, client, globalProps); + WSLoader wsLoader = provider.provide(cache, client); - assertThat(provider.provide(null, null, null)).isEqualTo(wsLoader); + assertThat(provider.provide(null, null)).isEqualTo(wsLoader); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java index c75259b5b36..2db585445f8 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderTest.java @@ -19,34 +19,28 @@ */ package org.sonar.batch.cache; -import com.google.common.collect.ImmutableMap; import java.io.IOException; import java.io.InputStream; -import java.net.URI; import org.apache.commons.io.IOUtils; import org.hamcrest.Matchers; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.InOrder; -import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.sonar.api.utils.HttpDownloader; -import org.sonar.batch.bootstrap.ServerClient; -import org.sonar.batch.bootstrap.UserProperties; import org.sonar.batch.cache.WSLoader.LoadStrategy; import org.sonar.home.cache.PersistentCache; +import org.sonarqube.ws.client.HttpException; +import org.sonarqube.ws.client.MockWsResponse; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.WsConnector; +import org.sonarqube.ws.client.WsRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; -import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -58,35 +52,20 @@ public class WSLoaderTest { private final static String cacheValue = "cache"; private final static String serverValue = "server"; - @Mock - private ServerClient client; - @Mock - private PersistentCache cache; @Rule public ExpectedException exception = ExpectedException.none(); - private UserProperties props; - @Before - public void setUp() throws IOException { - MockitoAnnotations.initMocks(this); - when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenReturn(IOUtils.toInputStream(serverValue)); - when(cache.getString(ID)).thenReturn(cacheValue); - when(client.getURI(anyString())).thenAnswer(new Answer<URI>() { - @Override - public URI answer(InvocationOnMock invocation) throws Throwable { - return new URI((String) invocation.getArguments()[0]); - } - }); - props = mock(UserProperties.class); - } + WsClient ws = mock(WsClient.class, Mockito.RETURNS_DEEP_STUBS); + PersistentCache cache = mock(PersistentCache.class); @Test public void dont_retry_server_offline() throws IOException { turnServerOffline(); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + when(cache.getString(ID)).thenReturn(cacheValue); + WSLoader underTest = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); - assertResult(loader.loadString(ID), cacheValue, true); - assertResult(loader.loadString(ID), cacheValue, true); + assertResult(underTest.loadString(ID), cacheValue, true); + assertResult(underTest.loadString(ID), cacheValue, true); assertUsedServer(1); assertUsedCache(2); @@ -94,84 +73,67 @@ public class WSLoaderTest { @Test public void get_stream_from_cache() throws IOException { - InputStream is = mock(InputStream.class); + InputStream is = IOUtils.toInputStream("is"); when(cache.getStream(ID)).thenReturn(is); - WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, client, props); - WSLoaderResult<InputStream> result = loader.loadStream(ID); - assertThat(result.get()).isEqualTo(is); - verify(cache).getStream(ID); - verifyNoMoreInteractions(cache, client); - } - - @Test - public void put_stream_in_cache() throws IOException { - InputStream is1 = mock(InputStream.class); - InputStream is2 = mock(InputStream.class); - - when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenReturn(is1); - when(cache.getStream(ID)).thenReturn(is2); - - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, ws); WSLoaderResult<InputStream> result = loader.loadStream(ID); - assertThat(result.get()).isEqualTo(is2); - verify(client).load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt()); - verify(cache).put(ID, is1); + assertThat(result.get()).isEqualTo(is); verify(cache).getStream(ID); - - verifyNoMoreInteractions(cache, client); + verifyNoMoreInteractions(cache, ws); } @Test - public void default_timeout() throws IOException { - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); - loader.loadStream(ID); - - verify(client).load(anyString(), anyString(), anyBoolean(), anyInt(), eq(60_000)); - - verifyNoMoreInteractions(client); - } - - @Test - public void change_timeout() throws IOException { - when(props.properties()).thenReturn(ImmutableMap.of(WSLoader.SONAR_WS_TIMEOUT_PROPS, "20")); - when(props.property(WSLoader.SONAR_WS_TIMEOUT_PROPS)).thenReturn("20"); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); - loader.loadStream(ID); - - verify(client).load(anyString(), anyString(), anyBoolean(), anyInt(), eq(20_000)); - - verifyNoMoreInteractions(client); + public void put_stream_in_cache() throws IOException { + InputStream input = IOUtils.toInputStream("is"); + + when(ws.wsConnector().call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(input)); + when(cache.getStream(ID)).thenReturn(input); + + // SERVER_FIRST -> load from server then put to cache + WSLoader underTest = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); + WSLoaderResult<InputStream> result = underTest.loadStream(ID); + assertThat(result.get()).isEqualTo(input); + + WsConnector wsConnector = ws.wsConnector(); + InOrder inOrder = inOrder(wsConnector, cache); + inOrder.verify(wsConnector).call(any(WsRequest.class)); + inOrder.verify(cache).put(eq(ID), any(InputStream.class)); + inOrder.verify(cache).getStream(ID); + verifyNoMoreInteractions(cache, wsConnector); } @Test public void test_cache_strategy_fallback() throws IOException { turnCacheEmpty(); - WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, client, props); + when(ws.wsConnector().call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue)); + WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, ws); assertResult(loader.loadString(ID), serverValue, false); - InOrder inOrder = Mockito.inOrder(client, cache); + InOrder inOrder = inOrder(ws.wsConnector(), cache); inOrder.verify(cache).getString(ID); - inOrder.verify(client).load(eq(ID), anyString(), anyBoolean(), anyInt(), anyInt()); + inOrder.verify(ws.wsConnector()).call(any(WsRequest.class)); } @Test public void test_server_strategy_fallback() throws IOException { turnServerOffline(); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + when(cache.getString(ID)).thenReturn(cacheValue); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); assertResult(loader.loadString(ID), cacheValue, true); - InOrder inOrder = Mockito.inOrder(client, cache); - inOrder.verify(client).load(eq(ID), anyString(), anyBoolean(), anyInt(), anyInt()); + InOrder inOrder = inOrder(ws.wsConnector(), cache); + inOrder.verify(ws.wsConnector()).call(any(WsRequest.class)); inOrder.verify(cache).getString(ID); } @Test public void test_put_cache() throws IOException { - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + when(ws.wsConnector().call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue)); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); loader.loadString(ID); verify(cache).put(ID, serverValue.getBytes()); } @@ -181,7 +143,7 @@ public class WSLoaderTest { turnServerOffline(); when(cache.getString(ID)).thenThrow(new NullPointerException()); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); try { loader.loadString(ID); @@ -196,7 +158,7 @@ public class WSLoaderTest { public void test_throw_cache_exception() throws IOException { when(cache.getString(ID)).thenThrow(new IllegalStateException()); - WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, ws); try { loader.loadString(ID); @@ -209,12 +171,12 @@ public class WSLoaderTest { @Test public void test_throw_http_exceptions() { - HttpDownloader.HttpException httpException = mock(HttpDownloader.HttpException.class); + HttpException httpException = new HttpException("url", 500, "Internal Error"); IllegalStateException wrapperException = new IllegalStateException(httpException); - when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenThrow(wrapperException); + when(ws.wsConnector().call(any(WsRequest.class))).thenThrow(wrapperException); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); try { loader.loadString(ID); @@ -230,9 +192,9 @@ public class WSLoaderTest { turnServerOffline(); exception.expect(IllegalStateException.class); - exception.expectMessage(Matchers.is("Server is not available")); + exception.expectMessage(Matchers.containsString("Server is not available")); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, ws); loader.loadString(ID); } @@ -244,7 +206,7 @@ public class WSLoaderTest { exception.expect(IllegalStateException.class); exception.expectMessage(Matchers.is("Server is not accessible and data is not cached")); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); loader.loadString(ID); } @@ -255,13 +217,14 @@ public class WSLoaderTest { exception.expect(IllegalStateException.class); exception.expectMessage(Matchers.is("Data is not cached")); - WSLoader loader = new WSLoader(LoadStrategy.CACHE_ONLY, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.CACHE_ONLY, cache, ws); loader.loadString(ID); } @Test public void test_server_strategy() throws IOException { - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + when(ws.wsConnector().call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue)); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); assertResult(loader.loadString(ID), serverValue, false); // should not fetch from cache @@ -272,13 +235,14 @@ public class WSLoaderTest { @Test(expected = IllegalStateException.class) public void test_server_only() throws IOException { turnServerOffline(); - WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, client, props); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, ws); loader.loadString(ID); } @Test public void test_string() { - WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, props); + when(ws.wsConnector().call(any(WsRequest.class))).thenReturn(new MockWsResponse().setContent(serverValue)); + WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, ws); assertResult(loader.loadString(ID), serverValue, false); } @@ -287,14 +251,7 @@ public class WSLoaderTest { } private void assertUsedServer(int times) { - verify(client, times(times)).load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt()); - } - - private void assertResult(WSLoaderResult<InputStream> result, byte[] expected, boolean fromCache) throws IOException { - byte[] content = IOUtils.toByteArray(result.get()); - assertThat(result).isNotNull(); - assertThat(content).isEqualTo(expected); - assertThat(result.isFromCache()).isEqualTo(fromCache); + verify(ws.wsConnector(), times(times)).call(any(WsRequest.class)); } private void assertResult(WSLoaderResult<String> result, String expected, boolean fromCache) { @@ -304,7 +261,7 @@ public class WSLoaderTest { } private void turnServerOffline() { - when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenThrow(new IllegalStateException()); + when(ws.wsConnector().call(any(WsRequest.class))).thenThrow(new IllegalStateException()); } private void turnCacheEmpty() throws IOException { diff --git a/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderWithServerTest.java b/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderWithServerTest.java deleted file mode 100644 index cd5b2e3b052..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/cache/WSLoaderWithServerTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.bootstrap.MockHttpServer; -import org.sonar.batch.bootstrap.ServerClient; -import org.sonar.batch.bootstrap.Slf4jLogger; -import org.sonar.batch.bootstrap.UserProperties; -import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonar.batch.cache.WSLoader.LoadStrategy; -import org.sonar.home.cache.DirectoryLock; -import org.sonar.home.cache.PersistentCache; -import org.sonar.home.cache.TTLCacheInvalidation; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class WSLoaderWithServerTest { - private static final String RESPONSE_STRING = "this is the content"; - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - private MockHttpServer server; - private PersistentCache cache; - private ServerClient client; - private WSLoader loader; - private UserProperties userProps; - - @Before - public void setUp() throws Exception { - server = new MockHttpServer(); - server.start(); - - GlobalProperties bootstrapProps = mock(GlobalProperties.class); - when(bootstrapProps.property("sonar.host.url")).thenReturn("http://localhost:" + server.getPort()); - - client = new ServerClient(bootstrapProps, new EnvironmentInformation("Junit", "4")); - cache = new PersistentCache(temp.getRoot().toPath(), new TTLCacheInvalidation(100_000L), new Slf4jLogger(), mock(DirectoryLock.class)); - userProps = mock(UserProperties.class); - } - - @After - public void tearDown() { - if (server != null) { - server.stop(); - } - } - - @Test - public void testCacheOnly() { - loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, client, userProps); - makeRequests(); - - loader = new WSLoader(LoadStrategy.CACHE_ONLY, cache, client, userProps); - makeRequests(); - assertThat(server.getNumberRequests()).isEqualTo(3); - } - - @Test - public void testCacheFirst() { - loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, client, userProps); - makeRequests(); - assertThat(server.getNumberRequests()).isEqualTo(1); - } - - @Test - public void testServerFirst() { - loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client, userProps); - makeRequests(); - assertThat(server.getNumberRequests()).isEqualTo(3); - } - - @Test - public void testCacheStrategyDisabled() { - loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, client, userProps); - makeRequests(); - assertThat(server.getNumberRequests()).isEqualTo(3); - } - - private void makeRequests() { - server.setMockResponseData(RESPONSE_STRING); - assertThat(loader.loadString("/foo").get()).isEqualTo(RESPONSE_STRING); - assertThat(loader.loadString("/foo").get()).isEqualTo(RESPONSE_STRING); - assertThat(loader.loadString("/foo").get()).isEqualTo(RESPONSE_STRING); - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java index 75b8af6dc51..d1c5e1a93c6 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java @@ -19,132 +19,126 @@ */ package org.sonar.batch.report; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.slf4j.Logger; +import org.mockito.Mockito; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; -import org.sonar.api.platform.Server; import org.sonar.api.utils.TempFolder; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.scan.ImmutableProjectReactor; +import org.sonar.test.JsonAssert; +import org.sonarqube.ws.client.WsClient; -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - +import static org.apache.commons.io.FileUtils.readFileToString; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class ReportPublisherTest { - private DefaultAnalysisMode mode; - - private ImmutableProjectReactor reactor; - - private Settings settings; - - private ProjectDefinition root; + @Rule + public LogTester logTester = new LogTester(); @Rule public TemporaryFolder temp = new TemporaryFolder(); + DefaultAnalysisMode mode = mock(DefaultAnalysisMode.class); + Settings settings = new Settings(); + WsClient wsClient = mock(WsClient.class, Mockito.RETURNS_DEEP_STUBS); + ImmutableProjectReactor reactor = mock(ImmutableProjectReactor.class); + ProjectDefinition root; + AnalysisContextReportPublisher contextPublisher = mock(AnalysisContextReportPublisher.class); + @Before public void setUp() { - settings = new Settings(); - mode = mock(DefaultAnalysisMode.class); - reactor = mock(ImmutableProjectReactor.class); root = ProjectDefinition.create().setKey("struts").setWorkDir(temp.getRoot()); when(reactor.getRoot()).thenReturn(root); + when(wsClient.wsConnector().baseUrl()).thenReturn("https://localhost"); } @Test - public void should_log_successful_analysis() { - settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, - mock(TempFolder.class), new ReportPublisherStep[0]); - - Logger logger = mock(Logger.class); - job.logSuccess(logger, null); - - verify(logger).info("ANALYSIS SUCCESSFUL, you can browse {}", "http://myserver/dashboard/index/struts"); - verify(logger).info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report."); - verifyNoMoreInteractions(logger); + public void log_and_dump_information_about_report_uploading() throws IOException { + ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); + + underTest.logSuccess("TASK-123"); + + assertThat(logTester.logs(LoggerLevel.INFO)) + .contains("ANALYSIS SUCCESSFUL, you can browse https://localhost/dashboard/index/struts") + .contains("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report") + .contains("More about the report processing at https://localhost/api/ce/task?id=TASK-123"); + + File detailsFile = new File(temp.getRoot(), "analysis-details.json"); + JsonAssert.assertJson(readFileToString(detailsFile)).isSimilarTo("{" + + "\"projectKey\": \"struts\"," + + "\"dashboardUrl\": \"https://localhost/dashboard/index/struts\"," + + "\"ceTaskId\": \"TASK-123\"," + + "\"ceTaskUrl\": \"https://localhost/api/ce/task?id=TASK-123\"" + + "}" + ); } @Test - public void should_log_successful_analysis_with_ce_task() { - settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, - mock(TempFolder.class), new ReportPublisherStep[0]); - - Logger logger = mock(Logger.class); - job.logSuccess(logger, "abc123"); - - verify(logger).info("ANALYSIS SUCCESSFUL, you can browse {}", "http://myserver/dashboard/index/struts"); - verify(logger).info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report."); - verify(logger).info("More about the report processing at {}", "http://myserver/api/ce/task?id=abc123"); - verifyNoMoreInteractions(logger); + public void log_public_url_if_defined() throws IOException { + settings.setProperty(CoreProperties.SERVER_BASE_URL, "https://publicserver/sonarqube"); + ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); + + underTest.logSuccess("TASK-123"); + + assertThat(logTester.logs(LoggerLevel.INFO)) + .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard/index/struts") + .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123"); + + File detailsFile = new File(temp.getRoot(), "analysis-details.json"); + JsonAssert.assertJson(readFileToString(detailsFile)).isSimilarTo("{" + + "\"projectKey\": \"struts\"," + + "\"dashboardUrl\": \"https://publicserver/sonarqube/dashboard/index/struts\"," + + "\"ceTaskId\": \"TASK-123\"," + + "\"ceTaskUrl\": \"https://publicserver/sonarqube/api/ce/task?id=TASK-123\"" + + "}" + ); } @Test - public void should_write_json_file() throws IOException { - settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); - - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, - mock(TempFolder.class), new ReportPublisherStep[0]); - job.logSuccess(mock(Logger.class), "abc123"); - - File jsonFile = new File(temp.getRoot(), "analysis-details.json"); - assertThat(jsonFile).exists(); + public void log_but_not_dump_information_when_report_is_not_uploaded() { + ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); - String jsonFileContent = new String(Files.readAllBytes(jsonFile.toPath()), StandardCharsets.UTF_8); - String expectedContent = "\"dashboardUrl\":\"http://myserver/dashboard/index/struts\",\"ceTaskUrl\":\"http://myserver/api/ce/task?id=abc123\""; - assertThat(jsonFileContent).contains(expectedContent); + underTest.logSuccess(/* report not uploaded, no server task */null); - } - - @Test - public void should_log_successful_issues_analysis() { - when(mode.isIssues()).thenReturn(true); - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, - mock(TempFolder.class), new ReportPublisherStep[0]); + assertThat(logTester.logs(LoggerLevel.INFO)) + .contains("ANALYSIS SUCCESSFUL") + .doesNotContain("dashboard/index"); - Logger logger = mock(Logger.class); - job.logSuccess(logger, null); - - verify(logger).info("ANALYSIS SUCCESSFUL"); - verifyNoMoreInteractions(logger); + File detailsFile = new File(temp.getRoot(), "analysis-details.json"); + assertThat(detailsFile).doesNotExist(); } @Test - public void should_not_delete_report() throws IOException { - settings.setProperty("sonar.verbose", true); + public void should_not_delete_report_if_property_is_set() throws IOException { + settings.setProperty("sonar.batch.keepReport", true); Path reportDir = temp.getRoot().toPath().resolve("batch-report"); Files.createDirectory(reportDir); - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, - mock(TempFolder.class), new ReportPublisherStep[0]); + ReportPublisher underTest = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); - job.start(); - job.stop(); + underTest.start(); + underTest.stop(); assertThat(reportDir).isDirectory(); } @Test - public void should_delete_report() throws IOException { + public void should_delete_report_by_default() throws IOException { Path reportDir = temp.getRoot().toPath().resolve("batch-report"); Files.createDirectory(reportDir); - ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, - mock(TempFolder.class), new ReportPublisherStep[0]); + ReportPublisher job = new ReportPublisher(settings, wsClient, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]); job.start(); job.stop(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java index b8c9012399a..e0eebd486d3 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java @@ -19,25 +19,22 @@ */ package org.sonar.batch.repository; -import org.sonar.api.utils.MessageException; - import com.google.common.io.Resources; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.net.URI; - -import org.sonar.api.utils.HttpDownloader.HttpException; import org.apache.commons.lang.mutable.MutableBoolean; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonar.api.utils.MessageException; import org.sonar.batch.cache.WSLoader; import org.sonar.batch.cache.WSLoaderResult; import org.sonarqube.ws.WsBatch.WsProjectResponse; +import org.sonarqube.ws.client.HttpException; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; @@ -56,7 +53,7 @@ public class DefaultProjectRepositoriesLoaderTest { public void prepare() throws IOException { wsLoader = mock(WSLoader.class); InputStream is = mockData(); - when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<InputStream>(is, true)); + when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true)); loader = new DefaultProjectRepositoriesLoader(wsLoader); } @@ -72,13 +69,13 @@ public class DefaultProjectRepositoriesLoaderTest { InputStream is = mock(InputStream.class); when(is.read()).thenThrow(IOException.class); - when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<InputStream>(is, false)); + when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, false)); loader.load(PROJECT_KEY, false, null); } @Test(expected = IllegalStateException.class) public void failFastHttpError() { - HttpException http = new HttpException(URI.create("uri"), 403); + HttpException http = new HttpException("url", 403, "Forbidden"); IllegalStateException e = new IllegalStateException("http error", http); when(wsLoader.loadStream(anyString())).thenThrow(e); loader.load(PROJECT_KEY, false, null); @@ -89,7 +86,7 @@ public class DefaultProjectRepositoriesLoaderTest { thrown.expect(MessageException.class); thrown.expectMessage("http error"); - HttpException http = new HttpException(URI.create("uri"), 403); + HttpException http = new HttpException("uri", 403, "Forbidden"); MessageException e = MessageException.of("http error", http); when(wsLoader.loadStream(anyString())).thenThrow(e); loader.load(PROJECT_KEY, false, null); |