3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.v2.api.analysis.controller;
23 import java.nio.file.Path;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.io.TempDir;
26 import org.sonar.server.v2.api.analysis.service.ScannerEngineHandler;
27 import org.sonar.server.v2.api.analysis.service.ScannerEngineMetadata;
28 import org.springframework.test.web.servlet.MockMvc;
29 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
31 import static java.lang.String.format;
32 import static java.nio.file.Files.createTempFile;
33 import static java.nio.file.Files.write;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37 import static org.sonar.server.v2.WebApiEndpoints.SCANNER_ENGINE_ENDPOINT;
38 import static org.sonar.server.v2.api.ControllerTester.getMockMvc;
39 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
40 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
41 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
42 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
43 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
45 class DefaultScannerEngineControllerTest {
47 private final ScannerEngineHandler scannerEngineHandler = mock(ScannerEngineHandler.class);
49 private final MockMvc mockMvc = getMockMvc(new DefaultScannerEngineController(scannerEngineHandler));
52 void getEngine_shouldReturnScannerMetadataAsJson() throws Exception {
53 String anyName = "anyName";
54 String anyChecksum = "anyChecksum";
55 when(scannerEngineHandler.getScannerEngineMetadata()).thenReturn(new ScannerEngineMetadata(anyName, anyChecksum));
56 String expectedJson = format("{\"filename\":\"%s\",\"checksum\":\"%s\"}", anyName, anyChecksum);
58 mockMvc.perform(get(SCANNER_ENGINE_ENDPOINT))
61 content().json(expectedJson));
65 void getEngine_shouldDownloadScanner_whenHeaderIsOctetStream(@TempDir Path tempDir) throws Exception {
66 File scanner = createTempFile(tempDir, "scanner", ".jar").toFile();
67 byte[] anyBinary = {1, 2, 3};
68 write(scanner.toPath(), anyBinary);
69 when(scannerEngineHandler.getScannerEngine()).thenReturn(new File(scanner.toString()));
71 mockMvc.perform(get(SCANNER_ENGINE_ENDPOINT)
72 .header("Accept", APPLICATION_OCTET_STREAM_VALUE))
75 content().contentType(APPLICATION_OCTET_STREAM),
76 content().bytes(anyBinary));
80 void getEngine_shouldFail_whenScannerEngineNotFound() {
81 // Ideally we would like Spring to return a 404, but it's not the case at the moment. We suspect that it's because the Header Accept wants a binary file.
82 // So the Json corresponding to the NotFoundException is not sent and we have a 500 instead.
83 when(scannerEngineHandler.getScannerEngine()).thenReturn(new File("no-file"));
84 MockHttpServletRequestBuilder request = get(SCANNER_ENGINE_ENDPOINT).header("Accept", APPLICATION_OCTET_STREAM_VALUE);
85 assertThatThrownBy(() -> mockMvc.perform(request))
86 .hasMessageContaining("NotFoundException: Unable to find file: no-file");