BAEL-5054 throw ServiceException when not OK
BAEL-5054 fix test name and tidy indent BAEL-5054 remove unnecessary changes BAEL-5054 restore main class name BAEL-5054 rename test to end with UnitTest
This commit is contained in:
parent
638d34924b
commit
82c3389ba0
|
@ -133,7 +133,7 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.reactive.Application</mainClass>
|
||||
<mainClass>com.baeldung.Spring5Application</mainClass>
|
||||
<layout>JAR</layout>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -27,21 +27,4 @@ public class UploadController {
|
|||
public Mono<HttpStatus> 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";
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.reactive.exception;
|
||||
|
||||
public class ServiceException extends RuntimeException{
|
||||
|
||||
public ServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -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<HttpStatus> uploadPdf(final Resource resource){
|
||||
public Mono<HttpStatus> uploadPdf(final Resource resource) {
|
||||
|
||||
final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri();
|
||||
Mono<HttpStatus> 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<HttpStatus> uploadMultipart(final MultipartFile multipartFile){
|
||||
public Mono<HttpStatus> 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<HttpStatus> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
logging.level.root=INFO
|
||||
|
||||
server.port=8080
|
||||
server.port=8081
|
||||
|
||||
logging.level.reactor.netty.http.client.HttpClient=DEBUG
|
|
@ -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<HttpStatus> 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<HttpStatus> result = tested.uploadMultipart(multipartFile);
|
||||
final HttpStatus status = result.block();
|
||||
assertThat(status).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue