aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/AnalysisCacheLoaderTest.java
blob: 8ccdcafdb2d143eacc294d5505b1246e8eda3ec6 (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
/*
 * SonarQube
 * Copyright (C) 2009-2022 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.DeflaterInputStream;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.scanner.fs.InputProject;
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.sonarqube.ws.client.WsResponse;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.scanner.cache.AnalysisCacheLoader.CONTENT_ENCODING;

public class AnalysisCacheLoaderTest {
  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 AnalysisCacheLoader loader = new AnalysisCacheLoader(wsClient, project, branchConfiguration);

  @Before
  public void before() {
    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);
    AnalysisCacheMsg msg = loader.load().get();
    assertThat(msg).isEqualTo(expected);
  }

  @Test
  public void loads_compressed_content() throws IOException {
    AnalysisCacheMsg expected = AnalysisCacheMsg.newBuilder()
      .putMap("key", ByteString.copyFrom("value", StandardCharsets.UTF_8))
      .build();
    setCompressedResponse(expected);
    AnalysisCacheMsg msg = loader.load().get();
    assertThat(msg).isEqualTo(expected);
  }

  @Test
  public void returns_empty_if_404() {
    when(response.code()).thenReturn(404);
    assertThat(loader.load()).isEmpty();
  }

  private void setResponse(AnalysisCacheMsg msg) throws IOException {
    when(response.contentStream()).thenReturn(createInputStream(msg));
  }

  private void setCompressedResponse(AnalysisCacheMsg msg) throws IOException {
    when(response.contentStream()).thenReturn(new DeflaterInputStream(createInputStream(msg)));
    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);
    return new ByteArrayInputStream(serialized.toByteArray());
  }
}