aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoaderTest.java
blob: 757b898b209743f4b325bdcd2bf1277cf443a02c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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.scanner.cache;

import com.google.protobuf.ByteString;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.zip.GZIPOutputStream;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.sonar.api.scanner.fs.InputProject;
import org.sonar.api.utils.MessageException;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.scanner.http.DefaultScannerWsClient;
import org.sonar.scanner.protocol.internal.ScannerInternal.SensorCacheEntry;
import org.sonar.scanner.protocol.internal.SensorCacheData;
import org.sonar.scanner.scan.branch.BranchConfiguration;
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.assertj.core.api.Assertions.entry;
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.DefaultAnalysisCacheLoader.CONTENT_ENCODING;

public class DefaultAnalysisCacheLoaderTest {
  private final static SensorCacheEntry MSG = SensorCacheEntry.newBuilder()
    .setKey("key")
    .setData(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);
  private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
  private final DefaultAnalysisCacheLoader loader = new DefaultAnalysisCacheLoader(wsClient, project, branchConfiguration);
  @Rule
  public LogTester logs = new LogTester();

  @Before
  public void before() {
    when(project.key()).thenReturn("myproject");
    when(wsClient.call(any())).thenReturn(response);
  }

  @Test
  public void loads_content_and_logs_size() throws IOException {
    setResponse(MSG);
    when(response.header("Content-Length")).thenReturn(Optional.of("123"));
    SensorCacheData msg = loader.load().get();
    assertThat(msg.getEntries()).containsOnly(entry(MSG.getKey(), MSG.getData()));
    assertRequestPath("api/analysis_cache/get?project=myproject");
    assertThat(logs.logs()).anyMatch(s -> s.startsWith("Load analysis cache (123 bytes)"));
  }

  @Test
  public void loads_content_for_branch() throws IOException {
    when(branchConfiguration.referenceBranchName()).thenReturn("name");

    setResponse(MSG);
    SensorCacheData msg = loader.load().get();

    assertThat(msg.getEntries()).containsOnly(entry(MSG.getKey(), MSG.getData()));
    assertRequestPath("api/analysis_cache/get?project=myproject&branch=name");
    assertThat(logs.logs()).anyMatch(s -> s.startsWith("Load analysis cache | time="));
  }

  @Test
  public void loads_compressed_content() throws IOException {
    setCompressedResponse(MSG);
    SensorCacheData msg = loader.load().get();
    assertThat(msg.getEntries()).containsOnly(entry(MSG.getKey(), MSG.getData()));
  }

  @Test
  public void returns_empty_if_404() {
    when(wsClient.call(any())).thenThrow(new HttpException("url", 404, "content"));
    assertThat(loader.load()).isEmpty();
    assertThat(logs.logs()).anyMatch(s -> s.startsWith("Load analysis cache (404) | time="));
  }

  @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(SensorCacheEntry msg) throws IOException {
    when(response.contentStream()).thenReturn(createInputStream(msg));
  }

  private void setCompressedResponse(SensorCacheEntry msg) throws IOException {
    when(response.contentStream()).thenReturn(createCompressedInputStream(msg));
    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(SensorCacheEntry analysisCacheMsg) throws IOException {
    ByteArrayOutputStream serialized = new ByteArrayOutputStream(analysisCacheMsg.getSerializedSize());
    analysisCacheMsg.writeDelimitedTo(serialized);
    return new ByteArrayInputStream(serialized.toByteArray());
  }

  private InputStream createCompressedInputStream(SensorCacheEntry analysisCacheMsg) throws IOException {
    ByteArrayOutputStream serialized = new ByteArrayOutputStream(analysisCacheMsg.getSerializedSize());
    GZIPOutputStream compressed = new GZIPOutputStream(serialized);
    analysisCacheMsg.writeDelimitedTo(compressed);
    compressed.close();
    return new ByteArrayInputStream(serialized.toByteArray());
  }
}