Skip to content

Commit 62b4c65

Browse files
committed
added tests
1 parent a254010 commit 62b4c65

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.owasp.esapi.errors.AccessControlException;
23+
import org.springframework.core.io.Resource;
24+
25+
import java.io.File;
26+
import java.net.MalformedURLException;
27+
import java.util.Set;
28+
29+
import static org.junit.jupiter.api.Assertions.*;
30+
31+
class DownloadServiceTest {
32+
private DownloadService downloadService;
33+
34+
@BeforeEach
35+
void setUp() {
36+
downloadService = new DownloadService();
37+
downloadService.init();
38+
}
39+
40+
@Test
41+
void getAllIndirectReferences_returnsNonEmptySet() {
42+
Set<String> indirectReferences = downloadService.getAllIndirectReferences();
43+
44+
assertNotNull(indirectReferences);
45+
assertFalse(indirectReferences.isEmpty());
46+
assertEquals(2, indirectReferences.size());
47+
}
48+
49+
@Test
50+
void getAllIndirectReferences_returnsUniqueReferences() {
51+
Set<String> indirectReferences = downloadService.getAllIndirectReferences();
52+
53+
assertEquals(2, indirectReferences.size());
54+
for (String reference : indirectReferences) {
55+
assertNotNull(reference);
56+
assertFalse(reference.isEmpty());
57+
}
58+
}
59+
60+
@Test
61+
void getFileByIndirectReference_withValidReference_returnsFile() throws AccessControlException {
62+
Set<String> indirectReferences = downloadService.getAllIndirectReferences();
63+
String validReference = indirectReferences.iterator().next();
64+
65+
File file = downloadService.getFileByIndirectReference(validReference);
66+
67+
assertNotNull(file);
68+
assertTrue(file.getName().equals("cover.pdf") || file.getName().equals("cover.jpg"));
69+
}
70+
71+
@Test
72+
void getFileByIndirectReference_withInvalidReference_throwsException() {
73+
String invalidReference = "invalid-reference-that-does-not-exist";
74+
75+
assertThrows(Exception.class, () -> {
76+
downloadService.getFileByIndirectReference(invalidReference);
77+
});
78+
}
79+
80+
@Test
81+
void getFileByIndirectReference_returnsCorrectFileForEachReference() throws AccessControlException {
82+
Set<String> indirectReferences = downloadService.getAllIndirectReferences();
83+
Set<String> expectedFileNames = Set.of("cover.pdf", "cover.jpg");
84+
Set<String> actualFileNames = new java.util.HashSet<>();
85+
86+
for (String reference : indirectReferences) {
87+
File file = downloadService.getFileByIndirectReference(reference);
88+
actualFileNames.add(file.getName());
89+
}
90+
91+
assertEquals(expectedFileNames, actualFileNames);
92+
}
93+
94+
@Test
95+
void loadAsResource_withNonExistentFile_returnsNull() throws MalformedURLException {
96+
Resource resource = downloadService.loadAsResource("non-existent-file.pdf");
97+
98+
assertNull(resource);
99+
}
100+
101+
@Test
102+
void loadAsResource_withFilename_createsUrlResource() throws MalformedURLException {
103+
String filename = "cover.pdf";
104+
105+
// The method creates a UrlResource but returns null if the resource doesn't exist
106+
// This tests the behavior when the file is not accessible
107+
Resource resource = downloadService.loadAsResource(filename);
108+
109+
// Resource is null because the file doesn't exist at the URL location
110+
assertNull(resource);
111+
}
112+
}

0 commit comments

Comments
 (0)