|
1 | 1 | package com.faforever.api.map; |
2 | 2 |
|
3 | | -import com.faforever.api.config.FafApiProperties; |
4 | | -import com.faforever.api.config.TestWebSecurityConfig; |
5 | | -import com.faforever.api.player.PlayerService; |
6 | | -import com.fasterxml.jackson.databind.JsonNode; |
7 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
| 3 | +import com.faforever.api.AbstractIntegrationTest; |
8 | 4 | import com.google.common.io.ByteStreams; |
9 | 5 | import org.junit.jupiter.api.Test; |
10 | | -import org.junit.jupiter.api.extension.ExtendWith; |
11 | | -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
12 | | -import org.springframework.boot.test.mock.mockito.MockBean; |
13 | | -import org.springframework.context.annotation.Import; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
14 | 7 | import org.springframework.mock.web.MockMultipartFile; |
15 | | -import org.springframework.security.core.userdetails.UserDetailsService; |
16 | | -import org.springframework.test.context.junit.jupiter.SpringExtension; |
17 | | -import org.springframework.test.web.servlet.MockMvc; |
| 8 | +import org.springframework.security.test.context.support.WithUserDetails; |
| 9 | +import org.springframework.test.context.jdbc.Sql; |
18 | 10 |
|
19 | | -import javax.inject.Inject; |
20 | 11 | import java.io.InputStream; |
21 | 12 |
|
22 | 13 | import static org.hamcrest.Matchers.hasSize; |
23 | 14 | import static org.hamcrest.Matchers.is; |
24 | | -import static org.mockito.Matchers.anyString; |
25 | | -import static org.mockito.Mockito.when; |
26 | 15 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; |
27 | 16 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
28 | 17 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
29 | 18 |
|
30 | | -@ExtendWith(SpringExtension.class) |
31 | | -@WebMvcTest(MapsController.class) |
32 | | -@Import(TestWebSecurityConfig.class) |
33 | | -public class MapsControllerTest { |
| 19 | +@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/truncateTables.sql") |
| 20 | +@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultData.sql") |
| 21 | +@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepMapVersion.sql") |
| 22 | +public class MapsControllerTest extends AbstractIntegrationTest{ |
34 | 23 |
|
35 | | - private MockMvc mvc; |
36 | | - @MockBean |
37 | | - private UserDetailsService userDetailsService; |
38 | | - @MockBean |
39 | | - private MapService mapService; |
40 | | - @MockBean |
41 | | - private FafApiProperties fafApiProperties; |
42 | | - @MockBean |
43 | | - private PlayerService playerService; |
44 | | - @MockBean |
45 | | - private ObjectMapper objectMapper; |
46 | | - |
47 | | - @Inject |
48 | | - void init(MockMvc mvc) { |
49 | | - this.mvc = mvc; |
50 | | - } |
| 24 | + @Autowired |
| 25 | + MapRepository mapRepository; |
51 | 26 |
|
| 27 | + @WithUserDetails(AUTH_USER) |
52 | 28 | @Test |
53 | 29 | void fileMissing() throws Exception { |
54 | | - this.mvc.perform(multipart("/maps/upload")) |
| 30 | + mockMvc.perform(multipart("/maps/upload")) |
55 | 31 | .andExpect(status().isBadRequest()) |
56 | 32 | .andExpect(jsonPath("$.errors", hasSize(1))) |
57 | 33 | .andExpect(jsonPath("$.errors[0].title", is("org.springframework.web.multipart.support.MissingServletRequestPartException"))) |
58 | 34 | .andExpect(jsonPath("$.errors[0].detail", is("Required request part 'file' is not present"))); |
59 | 35 | } |
60 | 36 |
|
| 37 | + @WithUserDetails(AUTH_USER) |
61 | 38 | @Test |
62 | 39 | void jsonMetaDataMissing() throws Exception { |
63 | 40 | MockMultipartFile file = new MockMultipartFile("file", "filename.txt", "text/plain", "some xml".getBytes()); |
64 | 41 |
|
65 | | - this.mvc.perform(multipart("/maps/upload") |
| 42 | + mockMvc.perform(multipart("/maps/upload") |
66 | 43 | .file(file)) |
67 | 44 | .andExpect(status().isBadRequest()) |
68 | 45 | .andExpect(jsonPath("$.errors", hasSize(1))) |
69 | 46 | .andExpect(jsonPath("$.errors[0].title", is("org.springframework.web.bind.MissingServletRequestParameterException"))) |
70 | 47 | .andExpect(jsonPath("$.errors[0].detail", is("Required String parameter 'metadata' is not present"))); |
71 | 48 | } |
72 | 49 |
|
| 50 | + @WithUserDetails(AUTH_USER) |
73 | 51 | @Test |
74 | 52 | void successUpload() throws Exception { |
75 | | - FafApiProperties props = new FafApiProperties(); |
76 | 53 | String jsonString = "{}"; |
77 | | - ObjectMapper mapper = new ObjectMapper(); |
78 | | - JsonNode node = mapper.readTree(jsonString); |
79 | 54 |
|
80 | | - when(fafApiProperties.getMap()).thenReturn(props.getMap()); |
81 | | - when(objectMapper.readTree(anyString())).thenReturn(node); |
82 | 55 | String zipFile = "command_conquer_rush.v0007.zip"; |
83 | 56 | try (InputStream inputStream = loadMapResourceAsStream(zipFile)) { |
84 | 57 | MockMultipartFile file = new MockMultipartFile("file", |
85 | 58 | zipFile, |
86 | 59 | "application/zip", |
87 | 60 | ByteStreams.toByteArray(inputStream)); |
88 | 61 |
|
89 | | - this.mvc.perform(multipart("/maps/upload") |
| 62 | + mockMvc.perform(multipart("/maps/upload") |
90 | 63 | .file(file) |
91 | 64 | .param("metadata", jsonString) |
92 | 65 | ).andExpect(status().isOk()); |
|
0 commit comments