|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com |
| 3 | + * |
| 4 | + * This file is part of the Java Security project. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package de.dominikschadow.javasecurity.downloads; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 23 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 24 | +import org.springframework.core.io.ByteArrayResource; |
| 25 | +import org.springframework.core.io.Resource; |
| 26 | +import org.springframework.test.web.servlet.MockMvc; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.net.MalformedURLException; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 35 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 36 | + |
| 37 | +@WebMvcTest(controllers = DownloadController.class) |
| 38 | +class DownloadControllerTest { |
| 39 | + @Autowired |
| 40 | + private MockMvc mockMvc; |
| 41 | + |
| 42 | + @MockitoBean |
| 43 | + private DownloadService downloadService; |
| 44 | + |
| 45 | + @Test |
| 46 | + void index_returnsIndexViewWithIndirectReferences() throws Exception { |
| 47 | + Set<String> indirectReferences = Set.of("ref1", "ref2"); |
| 48 | + when(downloadService.getAllIndirectReferences()).thenReturn(indirectReferences); |
| 49 | + |
| 50 | + mockMvc.perform(get("/")) |
| 51 | + .andExpect(status().isOk()) |
| 52 | + .andExpect(view().name("index")) |
| 53 | + .andExpect(model().attributeExists("indirectReferences")) |
| 54 | + .andExpect(model().attribute("indirectReferences", containsInAnyOrder("ref1", "ref2"))); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void download_withValidReference_returnsResource() throws Exception { |
| 59 | + String indirectReference = "validRef"; |
| 60 | + String filename = "test.pdf"; |
| 61 | + File mockFile = new File(filename); |
| 62 | + Resource mockResource = new ByteArrayResource("test content".getBytes()); |
| 63 | + |
| 64 | + when(downloadService.getFileByIndirectReference(indirectReference)).thenReturn(mockFile); |
| 65 | + when(downloadService.loadAsResource(filename)).thenReturn(mockResource); |
| 66 | + |
| 67 | + mockMvc.perform(get("/download").param("name", indirectReference)) |
| 68 | + .andExpect(status().isOk()) |
| 69 | + .andExpect(content().contentType("application/pdf")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void download_withMalformedUrl_returnsNotFound() throws Exception { |
| 74 | + String indirectReference = "malformedRef"; |
| 75 | + String filename = "test.pdf"; |
| 76 | + File mockFile = new File(filename); |
| 77 | + |
| 78 | + when(downloadService.getFileByIndirectReference(indirectReference)).thenReturn(mockFile); |
| 79 | + when(downloadService.loadAsResource(filename)).thenThrow(new MalformedURLException("Invalid URL")); |
| 80 | + |
| 81 | + mockMvc.perform(get("/download").param("name", indirectReference)) |
| 82 | + .andExpect(status().isNotFound()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void download_withJpgFile_returnsCorrectContentType() throws Exception { |
| 87 | + String indirectReference = "jpgRef"; |
| 88 | + String filename = "image.jpg"; |
| 89 | + File mockFile = new File(filename); |
| 90 | + Resource mockResource = new ByteArrayResource("image content".getBytes()); |
| 91 | + |
| 92 | + when(downloadService.getFileByIndirectReference(indirectReference)).thenReturn(mockFile); |
| 93 | + when(downloadService.loadAsResource(filename)).thenReturn(mockResource); |
| 94 | + |
| 95 | + mockMvc.perform(get("/download").param("name", indirectReference)) |
| 96 | + .andExpect(status().isOk()) |
| 97 | + .andExpect(content().contentType("image/jpeg")); |
| 98 | + } |
| 99 | +} |
0 commit comments