aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2022-03-21 13:20:18 -0500
committersonartech <sonartech@sonarsource.com>2022-03-24 20:03:07 +0000
commitf0768d2ff905feff76d40bb476832b4ddacff8c4 (patch)
tree113f78c2e66ceadbe6103cda6fc2dc38d783174a /sonar-scanner-engine/src
parent6abdf0edd97aa76ccc42d65ed0e30f401d75f79f (diff)
downloadsonarqube-f0768d2ff905feff76d40bb476832b4ddacff8c4.tar.gz
sonarqube-f0768d2ff905feff76d40bb476832b4ddacff8c4.zip
SONAR-16097 Rename 'previousAnalysisCache' to 'previousCache'
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java31
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java78
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java2
4 files changed, 85 insertions, 28 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java
index f582e122079..035d3cf0010 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java
@@ -21,10 +21,10 @@ package org.sonar.scanner.cache;
import java.io.IOException;
import java.io.InputStream;
-import java.net.HttpURLConnection;
import java.util.Optional;
import java.util.zip.InflaterInputStream;
import org.sonar.api.scanner.fs.InputProject;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.util.Protobuf;
import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
import org.sonar.scanner.protocol.internal.ScannerInternal;
@@ -32,6 +32,7 @@ import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.branch.BranchType;
import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.HttpException;
import org.sonarqube.ws.client.WsResponse;
/**
@@ -56,24 +57,26 @@ public class AnalysisCacheLoader {
String url = URL + "?project=" + project.key();
if (branchConfiguration.branchType() == BranchType.BRANCH && branchConfiguration.branchName() != null) {
url = url + "&branch=" + branchConfiguration.branchName();
+ } else if (branchConfiguration.isPullRequest()) {
+ url = url + "&pullRequest=" + branchConfiguration.pullRequestKey();
}
GetRequest request = new GetRequest(url).setHeader(ACCEPT_ENCODING, "gzip");
- try (WsResponse response = wsClient.call(request)) {
- if (response.code() == HttpURLConnection.HTTP_NOT_FOUND) {
- return Optional.empty();
+ try (WsResponse response = wsClient.call(request); InputStream is = response.contentStream()) {
+ Optional<String> contentEncoding = response.header(CONTENT_ENCODING);
+ if (contentEncoding.isPresent() && contentEncoding.get().equals("gzip")) {
+ return Optional.of(decompress(is));
+ } else {
+ return Optional.of(Protobuf.read(is, AnalysisCacheMsg.parser()));
}
- try (InputStream is = response.contentStream()) {
- Optional<String> contentEncoding = response.header(CONTENT_ENCODING);
- if (contentEncoding.isPresent() && contentEncoding.get().equals("gzip")) {
- return Optional.of(decompress(is));
- } else {
- return Optional.of(Protobuf.read(is, AnalysisCacheMsg.parser()));
- }
- } catch (IOException e) {
- throw new IllegalStateException("Failed to download cache", e);
+ } catch (HttpException e) {
+ if (e.code() == 404) {
+ return Optional.empty();
}
+ throw MessageException.of("Failed to download analysis cache: " + DefaultScannerWsClient.createErrorMessage(e));
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to download analysis cache", e);
}
}
@@ -81,7 +84,7 @@ public class AnalysisCacheLoader {
try (InflaterInputStream iis = new InflaterInputStream(is)) {
return Protobuf.read(iis, ScannerInternal.AnalysisCacheMsg.parser());
} catch (IOException e) {
- throw new IllegalStateException("Failed to decompress plugin cache", e);
+ throw new IllegalStateException("Failed to decompress analysis cache", e);
}
}
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java
index c3cd0ac0521..a4cd85157d6 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/ProjectSensorContext.java
@@ -199,7 +199,7 @@ public class ProjectSensorContext implements SensorContext {
}
@Override
- public ReadCache previousAnalysisCache() {
+ public ReadCache previousCache() {
return readCache;
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java
index 8ccdcafdb2d..1c1a04c7e08 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java
@@ -29,20 +29,29 @@ import java.util.Optional;
import java.util.zip.DeflaterInputStream;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.ArgumentCaptor;
import org.sonar.api.scanner.fs.InputProject;
+import org.sonar.api.utils.MessageException;
import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
-import org.sonar.scanner.protocol.internal.ScannerInternal;
import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg;
import org.sonar.scanner.scan.branch.BranchConfiguration;
+import org.sonar.scanner.scan.branch.BranchType;
+import org.sonarqube.ws.client.HttpException;
+import org.sonarqube.ws.client.WsRequest;
import org.sonarqube.ws.client.WsResponse;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.scanner.cache.AnalysisCacheLoader.CONTENT_ENCODING;
public class AnalysisCacheLoaderTest {
+ private final static AnalysisCacheMsg MSG = AnalysisCacheMsg.newBuilder()
+ .putMap("key", ByteString.copyFrom("value", StandardCharsets.UTF_8))
+ .build();
private final WsResponse response = mock(WsResponse.class);
private final DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class);
private final InputProject project = mock(InputProject.class);
@@ -51,35 +60,75 @@ public class AnalysisCacheLoaderTest {
@Before
public void before() {
+ when(project.key()).thenReturn("myproject");
when(wsClient.call(any())).thenReturn(response);
}
@Test
public void loads_content() throws IOException {
- ScannerInternal.AnalysisCacheMsg expected = ScannerInternal.AnalysisCacheMsg.newBuilder()
- .putMap("key", ByteString.copyFrom("value", StandardCharsets.UTF_8))
- .build();
- setResponse(expected);
+ setResponse(MSG);
AnalysisCacheMsg msg = loader.load().get();
- assertThat(msg).isEqualTo(expected);
+ assertThat(msg).isEqualTo(MSG);
+ assertRequestPath("api/analysis_cache/get?project=myproject");
+ }
+
+ @Test
+ public void loads_content_for_branch() throws IOException {
+ when(branchConfiguration.branchType()).thenReturn(BranchType.BRANCH);
+ when(branchConfiguration.branchName()).thenReturn("name");
+
+ setResponse(MSG);
+ AnalysisCacheMsg msg = loader.load().get();
+
+ assertThat(msg).isEqualTo(MSG);
+ assertRequestPath("api/analysis_cache/get?project=myproject&branch=name");
+ }
+
+ @Test
+ public void loads_content_for_pr() throws IOException {
+ when(branchConfiguration.isPullRequest()).thenReturn(true);
+ when(branchConfiguration.pullRequestKey()).thenReturn("key");
+ setResponse(MSG);
+ AnalysisCacheMsg msg = loader.load().get();
+ assertThat(msg).isEqualTo(MSG);
+ assertRequestPath("api/analysis_cache/get?project=myproject&pullRequest=key");
}
@Test
public void loads_compressed_content() throws IOException {
- AnalysisCacheMsg expected = AnalysisCacheMsg.newBuilder()
- .putMap("key", ByteString.copyFrom("value", StandardCharsets.UTF_8))
- .build();
- setCompressedResponse(expected);
+ setCompressedResponse(MSG);
AnalysisCacheMsg msg = loader.load().get();
- assertThat(msg).isEqualTo(expected);
+ assertThat(msg).isEqualTo(MSG);
}
@Test
public void returns_empty_if_404() {
- when(response.code()).thenReturn(404);
+ when(wsClient.call(any())).thenThrow(new HttpException("url", 404, "content"));
assertThat(loader.load()).isEmpty();
}
+ @Test
+ public void throw_error_if_http_exception_not_404() {
+ when(wsClient.call(any())).thenThrow(new HttpException("url", 401, "content"));
+ assertThatThrownBy(loader::load)
+ .isInstanceOf(MessageException.class)
+ .hasMessage("Failed to download analysis cache: HTTP code 401: content");
+ }
+
+ @Test
+ public void throw_error_if_cant_decompress_content() {
+ setInvalidCompressedResponse();
+ assertThatThrownBy(loader::load)
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("Failed to download analysis cache");
+ }
+
+ private void assertRequestPath(String expectedPath) {
+ ArgumentCaptor<WsRequest> requestCaptor = ArgumentCaptor.forClass(WsRequest.class);
+ verify(wsClient).call(requestCaptor.capture());
+ assertThat(requestCaptor.getValue().getPath()).isEqualTo(expectedPath);
+ }
+
private void setResponse(AnalysisCacheMsg msg) throws IOException {
when(response.contentStream()).thenReturn(createInputStream(msg));
}
@@ -89,6 +138,11 @@ public class AnalysisCacheLoaderTest {
when(response.header(CONTENT_ENCODING)).thenReturn(Optional.of("gzip"));
}
+ private void setInvalidCompressedResponse() {
+ when(response.contentStream()).thenReturn(new ByteArrayInputStream(new byte[] {1, 2, 3}));
+ when(response.header(CONTENT_ENCODING)).thenReturn(Optional.of("gzip"));
+ }
+
private InputStream createInputStream(AnalysisCacheMsg analysisCacheMsg) throws IOException {
ByteArrayOutputStream serialized = new ByteArrayOutputStream(analysisCacheMsg.getSerializedSize());
analysisCacheMsg.writeTo(serialized);
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
index 1063283716e..d40c7344bee 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
@@ -89,7 +89,7 @@ public class ModuleSensorContextTest {
assertThat(adaptor.canSkipUnchangedFiles()).isFalse();
assertThat(adaptor.nextCache()).isEqualTo(writeCache);
- assertThat(adaptor.previousAnalysisCache()).isEqualTo(readCache);
+ assertThat(adaptor.previousCache()).isEqualTo(readCache);
assertThat(adaptor.newIssue()).isNotNull();
assertThat(adaptor.newExternalIssue()).isNotNull();