diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml
index 6a0a2d6fe3..136f31b49e 100644
--- a/spring-5-reactive-client/pom.xml
+++ b/spring-5-reactive-client/pom.xml
@@ -133,7 +133,7 @@
org.springframework.boot
spring-boot-maven-plugin
- com.baeldung.reactive.Application
+ com.baeldung.Spring5Application
JAR
diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java
deleted file mode 100644
index 65e497b519..0000000000
--- a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.baeldung.reactive;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-@SpringBootApplication
-public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
-}
-
diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java
index 71cea35685..08d6ff55ef 100644
--- a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java
+++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java
@@ -27,21 +27,4 @@ public class UploadController {
public Mono uploadMultipart(@RequestParam("file") final MultipartFile multipartFile) {
return uploadService.uploadMultipart(multipartFile);
}
-
-
- /**
- * Fake upload endpoint returning "OK" HttpStatus
- * @return "OK" HttpStatus
- */
- @PostMapping(path = "/external/upload")
- @ResponseBody
- public HttpStatus externalUpload() {
- return HttpStatus.OK;
- }
-
- @GetMapping("/trixi")
- public String returnTrixi() {
- return "Trixi";
-
- }
}
diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/exception/ServiceException.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/exception/ServiceException.java
new file mode 100644
index 0000000000..cd639ec1f9
--- /dev/null
+++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/exception/ServiceException.java
@@ -0,0 +1,8 @@
+package com.baeldung.reactive.exception;
+
+public class ServiceException extends RuntimeException{
+
+ public ServiceException(String message) {
+ super(message);
+ }
+}
diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java
index e31674282b..11409ce986 100644
--- a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java
+++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java
@@ -1,6 +1,7 @@
package com.baeldung.reactive.service;
+import com.baeldung.reactive.exception.ServiceException;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -20,50 +21,46 @@ public class ReactiveUploadService {
private final WebClient webClient;
private static final String EXTERNAL_UPLOAD_URL = "http://localhost:8080/external/upload";
- public ReactiveUploadService() {
- this.webClient = WebClient.create();
+ public ReactiveUploadService(final WebClient webClient) {
+ this.webClient = webClient;
}
- public Mono uploadPdf(final Resource resource){
+ public Mono uploadPdf(final Resource resource) {
final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri();
Mono httpStatusMono = webClient.post()
- .uri(url)
- .contentType(MediaType.APPLICATION_PDF)
- .body(BodyInserters.fromResource(resource))
- .exchangeToMono(response -> {
- if (response.statusCode().equals(HttpStatus.OK)) {
- return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
- } else {
- System.out.println("Failed to upload pdf. " + response.statusCode());
- }
- return null;
- });
+ .uri(url)
+ .contentType(MediaType.APPLICATION_PDF)
+ .body(BodyInserters.fromResource(resource))
+ .exchangeToMono(response -> {
+ if (response.statusCode().equals(HttpStatus.OK)) {
+ return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
+ } else {
+ throw new ServiceException("Error uploading file");
+ }
+ });
return httpStatusMono;
}
- public Mono uploadMultipart(final MultipartFile multipartFile){
+ public Mono uploadMultipart(final MultipartFile multipartFile) {
final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri();
final MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("file", multipartFile.getResource());
Mono httpStatusMono = webClient.post()
- .uri(url)
- .contentType(MediaType.MULTIPART_FORM_DATA)
- .body(BodyInserters.fromMultipartData(builder.build()))
- .exchangeToMono(response -> {
- if (response.statusCode().equals(HttpStatus.OK)) {
- return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
- } else {
- System.out.println("Failed to upload pdf. " + response.statusCode());
- }
- return null;
- });
+ .uri(url)
+ .contentType(MediaType.MULTIPART_FORM_DATA)
+ .body(BodyInserters.fromMultipartData(builder.build()))
+ .exchangeToMono(response -> {
+ if (response.statusCode().equals(HttpStatus.OK)) {
+ return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
+ } else {
+ throw new ServiceException("Error uploading file");
+ }
+ });
return httpStatusMono;
}
-
-
}
diff --git a/spring-5-reactive-client/src/main/resources/application.properties b/spring-5-reactive-client/src/main/resources/application.properties
index 7859537230..05033054b1 100644
--- a/spring-5-reactive-client/src/main/resources/application.properties
+++ b/spring-5-reactive-client/src/main/resources/application.properties
@@ -1,5 +1,5 @@
logging.level.root=INFO
-server.port=8080
+server.port=8081
logging.level.reactor.netty.http.client.HttpClient=DEBUG
\ No newline at end of file
diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java
new file mode 100644
index 0000000000..0ca59848d7
--- /dev/null
+++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/service/ReactiveUploadServiceUnitTest.java
@@ -0,0 +1,44 @@
+package com.baeldung.reactive.service;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ReactiveUploadServiceUnitTest {
+
+ private static final String BASE_URL = "http://localhost:8080/external/upload";
+
+ final WebClient webClientMock = WebClient.builder().baseUrl(BASE_URL)
+ .exchangeFunction(clientRequest -> Mono.just(ClientResponse.create(HttpStatus.OK)
+ .header("content-type", "application/json")
+ .build()))
+ .build();
+
+ private final ReactiveUploadService tested = new ReactiveUploadService(webClientMock);
+
+ @Test
+ void givenAPdf_whenUploadingWithWebClient_thenOK() {
+ final Resource file = mock(Resource.class);
+ final Mono result = tested.uploadPdf(file);
+ final HttpStatus status = result.block();
+ assertThat(status).isEqualTo(HttpStatus.OK);
+ }
+
+ @Test
+ void givenAMultipartPdf_whenUploadingWithWebClient_thenOK() {
+ final Resource file = mock(Resource.class);
+ final MultipartFile multipartFile = mock(MultipartFile.class);
+ when(multipartFile.getResource()).thenReturn(file);
+ final Mono result = tested.uploadMultipart(multipartFile);
+ final HttpStatus status = result.block();
+ assertThat(status).isEqualTo(HttpStatus.OK);
+ }
+}
\ No newline at end of file