Skip to content

Commit 0e888ad

Browse files
committed
Polishing
Signed-off-by: Sébastien Deleuze <[email protected]>
1 parent fd73765 commit 0e888ad

File tree

7 files changed

+33
-38
lines changed

7 files changed

+33
-38
lines changed

complete-kotlin/src/main/kotlin/com/example/uploadingfiles/FileUploadController.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.example.uploadingfiles
22

33
import com.example.uploadingfiles.storage.StorageFileNotFoundException
44
import com.example.uploadingfiles.storage.StorageService
5-
import org.springframework.beans.factory.annotation.Autowired
65
import org.springframework.core.io.Resource
76
import org.springframework.http.HttpHeaders
87
import org.springframework.http.ResponseEntity
@@ -12,14 +11,12 @@ import org.springframework.web.bind.annotation.*
1211
import org.springframework.web.multipart.MultipartFile
1312
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder
1413
import org.springframework.web.servlet.mvc.support.RedirectAttributes
15-
import java.io.IOException
1614
import java.util.stream.Collectors
1715

1816
@Controller
19-
class FileUploadController @Autowired constructor(private val storageService: StorageService) {
17+
class FileUploadController(private val storageService: StorageService) {
2018

2119
@GetMapping("/")
22-
@Throws(IOException::class)
2320
fun listUploadedFiles(model: Model): String {
2421
model.addAttribute("files", storageService.loadAll().map { path ->
2522
MvcUriComponentsBuilder.fromMethodName(
@@ -43,16 +40,13 @@ class FileUploadController @Autowired constructor(private val storageService: St
4340
).body(file)
4441
}
4542

46-
@PostMapping(path = ["/", ""])
47-
fun handleFileUpload(
48-
@RequestParam("file") file: MultipartFile,
49-
redirectAttributes: RedirectAttributes
50-
): String {
43+
@PostMapping("/")
44+
fun handleFileUpload(@RequestParam("file") file: MultipartFile,
45+
redirectAttributes: RedirectAttributes): String {
5146
storageService.store(file)
5247
redirectAttributes.addFlashAttribute(
5348
"message",
54-
"You successfully uploaded ${file.originalFilename}!"
55-
)
49+
"You successfully uploaded ${file.originalFilename}!")
5650

5751
return "redirect:/"
5852
}

complete-kotlin/src/main/kotlin/com/example/uploadingfiles/storage/FileSystemStorageService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java.nio.file.StandardCopyOption
1515
import java.util.stream.Stream
1616

1717
@Service
18-
class FileSystemStorageService @Autowired constructor(properties: StorageProperties) : StorageService {
18+
class FileSystemStorageService(properties: StorageProperties) : StorageService {
1919

2020
private val rootLocation: Path
2121

complete-kotlin/src/test/kotlin/com/example/uploadingfiles/FileUploadIntegrationTests.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ package com.example.uploadingfiles
33
import com.example.uploadingfiles.storage.StorageService
44
import org.assertj.core.api.Assertions.assertThat
55
import org.junit.jupiter.api.Test
6-
import org.mockito.ArgumentMatchers.any
76
import org.mockito.BDDMockito.given
8-
import org.mockito.BDDMockito.then
97
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder
109
import org.springframework.boot.test.context.SpringBootTest
11-
import org.springframework.boot.test.mock.mockito.MockBean
1210
import org.springframework.boot.test.web.client.TestRestTemplate
1311
import org.springframework.boot.test.web.server.LocalServerPort
12+
import org.springframework.boot.web.client.RestTemplateBuilder
1413
import org.springframework.core.io.ClassPathResource
1514
import org.springframework.http.HttpEntity
1615
import org.springframework.http.HttpHeaders
1716
import org.springframework.http.HttpStatus
1817
import org.springframework.http.MediaType
18+
import org.springframework.test.context.bean.override.mockito.MockitoBean
1919
import org.springframework.util.LinkedMultiValueMap
2020
import org.springframework.util.MultiValueMap
21-
import org.springframework.web.multipart.MultipartFile
21+
import java.net.http.HttpClient
2222

2323
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2424
class FileUploadIntegrationTests {
2525

2626
@Autowired
2727
private lateinit var restTemplate: TestRestTemplate
2828

29-
@MockBean
29+
@MockitoBean
3030
private lateinit var storageService: StorageService
3131

3232
@LocalServerPort
@@ -46,15 +46,14 @@ class FileUploadIntegrationTests {
4646

4747
val requestEntity = HttpEntity(map, headers)
4848

49-
// Create a TestRestTemplate that does not follow redirects (Spring Boot 3.5 defaults to following redirects)
50-
val client = java.net.http.HttpClient.newBuilder()
51-
.followRedirects(java.net.http.HttpClient.Redirect.NEVER)
52-
.build()
53-
val requestFactory = org.springframework.http.client.JdkClientHttpRequestFactory(client)
54-
val noRedirect = org.springframework.boot.web.client.RestTemplateBuilder()
55-
.rootUri("http://localhost:$port")
56-
.requestFactory { _: org.springframework.boot.web.client.ClientHttpRequestFactorySettings -> requestFactory }
57-
.build()
49+
50+
// Build a RestTemplate that does NOT follow redirects so we can assert 302/Location
51+
val noRedirect = RestTemplateBuilder()
52+
.rootUri("http://localhost:" + this.port)
53+
.requestFactoryBuilder(
54+
ClientHttpRequestFactoryBuilder.jdk()
55+
.withHttpClientCustomizer { it.followRedirects(HttpClient.Redirect.NEVER) }
56+
).build()
5857
val response = noRedirect.postForEntity(
5958
"/",
6059
requestEntity,

complete-kotlin/src/test/kotlin/com/example/uploadingfiles/FileUploadTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import org.mockito.BDDMockito.then
99
import org.springframework.beans.factory.annotation.Autowired
1010
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
1111
import org.springframework.boot.test.context.SpringBootTest
12-
import org.springframework.boot.test.mock.mockito.MockBean
1312
import org.springframework.mock.web.MockMultipartFile
13+
import org.springframework.test.context.bean.override.mockito.MockitoBean
1414
import org.springframework.test.web.servlet.MockMvc
1515
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
1616
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@@ -24,7 +24,7 @@ class FileUploadTests {
2424
@Autowired
2525
private lateinit var mvc: MockMvc
2626

27-
@MockBean
27+
@MockitoBean
2828
private lateinit var storageService: StorageService
2929

3030
@Test

complete/src/test/java/com/example/uploadingfiles/FileUploadIntegrationTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package com.example.uploadingfiles;
22

3+
import java.net.http.HttpClient;
34
import java.util.stream.Stream;
45

56
import org.junit.jupiter.api.Test;
67

78
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
810
import org.springframework.boot.test.context.SpringBootTest;
9-
import org.springframework.boot.test.mock.mockito.MockBean;
1011
import org.springframework.boot.test.web.client.TestRestTemplate;
1112
import org.springframework.boot.test.web.server.LocalServerPort;
13+
import org.springframework.boot.web.client.RestTemplateBuilder;
1214
import org.springframework.core.io.ClassPathResource;
1315
import org.springframework.http.HttpEntity;
1416
import org.springframework.http.HttpHeaders;
1517
import org.springframework.http.HttpStatus;
1618
import org.springframework.http.MediaType;
1719
import org.springframework.http.ResponseEntity;
20+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1821
import org.springframework.util.LinkedMultiValueMap;
1922
import org.springframework.util.MultiValueMap;
23+
import org.springframework.web.client.RestTemplate;
2024

2125
import static org.assertj.core.api.Assertions.assertThat;
2226
import static org.mockito.BDDMockito.given;
@@ -29,7 +33,7 @@ public class FileUploadIntegrationTests {
2933
@Autowired
3034
private TestRestTemplate restTemplate;
3135

32-
@MockBean
36+
@MockitoBean
3337
private StorageService storageService;
3438

3539
@LocalServerPort
@@ -50,13 +54,11 @@ public void shouldUploadFile() throws Exception {
5054
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
5155

5256
// Build a RestTemplate that does NOT follow redirects so we can assert 302/Location
53-
java.net.http.HttpClient client = java.net.http.HttpClient.newBuilder()
54-
.followRedirects(java.net.http.HttpClient.Redirect.NEVER)
55-
.build();
56-
org.springframework.http.client.JdkClientHttpRequestFactory requestFactory = new org.springframework.http.client.JdkClientHttpRequestFactory(client);
57-
org.springframework.web.client.RestTemplate noRedirect = new org.springframework.boot.web.client.RestTemplateBuilder()
57+
RestTemplate noRedirect = new RestTemplateBuilder()
5858
.rootUri("http://localhost:" + this.port)
59-
.requestFactory((org.springframework.boot.web.client.ClientHttpRequestFactorySettings s) -> requestFactory)
59+
.requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk()
60+
.withHttpClientCustomizer(customizer ->
61+
customizer.followRedirects(HttpClient.Redirect.NEVER)))
6062
.build();
6163

6264
ResponseEntity<String> response = noRedirect.postForEntity("/", requestEntity, String.class);

complete/src/test/java/com/example/uploadingfiles/FileUploadTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1111
import org.springframework.boot.test.context.SpringBootTest;
12-
import org.springframework.boot.test.mock.mockito.MockBean;
1312
import org.springframework.mock.web.MockMultipartFile;
13+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1414
import org.springframework.test.web.servlet.MockMvc;
1515

1616
import static org.mockito.BDDMockito.given;
@@ -31,7 +31,7 @@ public class FileUploadTests {
3131
@Autowired
3232
private MockMvc mvc;
3333

34-
@MockBean
34+
@MockitoBean
3535
private StorageService storageService;
3636

3737
@Test

test/run.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)