Merge branch 'eugenp:master' into JAVA-9556
This commit is contained in:
commit
348bc58c5f
|
@ -3,23 +3,16 @@ package com.baeldung.reactive.errorhandling;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = MongoReactiveAutoConfiguration.class)
|
||||
@SpringBootApplication(exclude = {
|
||||
MongoReactiveAutoConfiguration.class,
|
||||
ReactiveSecurityAutoConfiguration.class,
|
||||
ReactiveUserDetailsServiceAutoConfiguration.class })
|
||||
public class ErrorHandlingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ErrorHandlingApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
|
||||
http.authorizeExchange()
|
||||
.anyExchange()
|
||||
.permitAll();
|
||||
http.csrf().disable();
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,44 +9,14 @@ import org.springframework.web.reactive.function.server.ServerRequest;
|
|||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class GlobalErrorAttributes extends DefaultErrorAttributes{
|
||||
|
||||
private HttpStatus status = HttpStatus.BAD_REQUEST;
|
||||
private String message = "please provide a name";
|
||||
public class GlobalErrorAttributes extends DefaultErrorAttributes {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
|
||||
Map<String, Object> map = super.getErrorAttributes(request, options);
|
||||
map.put("status", getStatus());
|
||||
map.put("message", getMessage());
|
||||
map.put("status", HttpStatus.BAD_REQUEST);
|
||||
map.put("message", "please provide a name");
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the status
|
||||
*/
|
||||
public HttpStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param status the status to set
|
||||
*/
|
||||
public void setStatus(HttpStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the message
|
||||
*/
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message the message to set
|
||||
*/
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.reactive.errorhandling;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler {
|
||||
|
||||
public Mono<ServerResponse> handleWithErrorReturn(ServerRequest request) {
|
||||
return sayHello(request)
|
||||
.onErrorReturn("Hello, Stranger")
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s));
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> handleWithErrorResumeAndDynamicFallback(ServerRequest request) {
|
||||
return sayHello(request)
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s))
|
||||
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " + e.getMessage()))
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s)));
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> handleWithErrorResumeAndFallbackMethod(ServerRequest request) {
|
||||
return sayHello(request)
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s))
|
||||
.onErrorResume(e -> sayHelloFallback()
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s)));
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> handleWithErrorResumeAndCustomException(ServerRequest request) {
|
||||
return ServerResponse.ok()
|
||||
.body(sayHello(request)
|
||||
.onErrorResume(e -> Mono.error(new NameRequiredException(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"please provide a name", e))), String.class);
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> handleWithGlobalErrorHandler(ServerRequest request) {
|
||||
return ServerResponse.ok()
|
||||
.body(sayHello(request), String.class);
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name").get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Mono<String> sayHelloFallback() {
|
||||
return Mono.just("Hello, Stranger");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.reactive.errorhandling;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
import static org.springframework.http.MediaType.TEXT_PLAIN;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
|
||||
|
||||
@Component
|
||||
public class Router {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routes(Handler handler) {
|
||||
return RouterFunctions
|
||||
.route(GET("/api/endpoint1").and(accept(TEXT_PLAIN)), handler::handleWithErrorReturn)
|
||||
.andRoute(GET("/api/endpoint2").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndFallbackMethod)
|
||||
.andRoute(GET("/api/endpoint3").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndDynamicFallback)
|
||||
.andRoute(GET("/api/endpoint4").and(accept(TEXT_PLAIN)), handler::handleWithErrorResumeAndCustomException)
|
||||
.andRoute(GET("/api/endpoint5").and(accept(TEXT_PLAIN)), handler::handleWithGlobalErrorHandler);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler1 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest1(ServerRequest request) {
|
||||
return sayHello(request).onErrorReturn("Hello, Stranger")
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s));
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name")
|
||||
.get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler2 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest2(ServerRequest request) {
|
||||
return
|
||||
sayHello(request)
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s))
|
||||
.onErrorResume(e -> sayHelloFallback()
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s)));
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name")
|
||||
.get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Mono<String> sayHelloFallback() {
|
||||
return Mono.just("Hello, Stranger");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler3 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest3(ServerRequest request) {
|
||||
return
|
||||
sayHello(request)
|
||||
.flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s))
|
||||
.onErrorResume(e -> (Mono.just("Hi, I looked around for your name but found: " +
|
||||
e.getMessage())).flatMap(s -> ServerResponse.ok()
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.bodyValue(s)));
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name")
|
||||
.get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.NameRequiredException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler4 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest4(ServerRequest request) {
|
||||
return ServerResponse.ok()
|
||||
.body(sayHello(request)
|
||||
.onErrorResume(e ->
|
||||
Mono.error(new NameRequiredException(
|
||||
HttpStatus.BAD_REQUEST, "please provide a name", e))), String.class);
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
try {
|
||||
return Mono.just("Hello, " + request.queryParam("name").get());
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.handlers;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class Handler5 {
|
||||
|
||||
public Mono<ServerResponse> handleRequest5(ServerRequest request) {
|
||||
return ServerResponse.ok()
|
||||
.body(sayHello(request), String.class);
|
||||
|
||||
}
|
||||
|
||||
private Mono<String> sayHello(ServerRequest request) {
|
||||
return Mono.just("Hello, " + request.queryParam("name").get());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler1;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router1 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest1(Handler1 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint1")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest1);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler2;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router2 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest2(Handler2 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint2")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest2);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler3;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router3 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest3(Handler3 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint3")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest3);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler4;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router4 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest4(Handler4 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint4")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest4);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.reactive.errorhandling.routers;
|
||||
|
||||
import com.baeldung.reactive.errorhandling.handlers.Handler5;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
@Component
|
||||
public class Router5 {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> routeRequest5(Handler5 handler) {
|
||||
return RouterFunctions.route(RequestPredicates.GET("/api/endpoint5")
|
||||
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), handler::handleRequest5);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,19 +7,13 @@ import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWeb
|
|||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
@WithMockUser
|
||||
@AutoConfigureWebTestClient(timeout = "10000")
|
||||
public class ErrorHandlingIntegrationTest {
|
||||
|
||||
|
@ -27,150 +21,107 @@ public class ErrorHandlingIntegrationTest {
|
|||
private WebTestClient webTestClient;
|
||||
|
||||
@Test
|
||||
public void givenErrorReturn_whenUsernamePresent_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint1?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenErrorReturn_whenNoUsername_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint1")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Stranger", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeFallback_whenUsernamePresent_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint2?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeFallback_whenNoUsername_thenOk() throws IOException {
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint2")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Stranger", s);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeDynamicValue_whenUsernamePresent_thenOk() throws IOException {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint3?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeDynamicValue_whenNoUsername_thenOk() throws IOException {
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint3")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hi, I looked around for your name but found: No value present", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeRethrow_whenUsernamePresent_thenOk() throws IOException {
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint4?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeRethrow_whenNoUsername_thenOk() throws IOException {
|
||||
public void givenErrorReturn_whenUsernamePresent_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint4")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isBadRequest()
|
||||
.expectHeader()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody()
|
||||
.jsonPath("$.message")
|
||||
.isNotEmpty()
|
||||
.jsonPath("$.message")
|
||||
.isEqualTo("please provide a name");
|
||||
.uri("/api/endpoint1?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hello, Tony");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() throws IOException {
|
||||
public void givenErrorReturn_whenNoUsername_thenOk() {
|
||||
|
||||
String s = webTestClient.get()
|
||||
.uri("/api/endpoint5?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.returnResult(String.class)
|
||||
.getResponseBody()
|
||||
.blockFirst();
|
||||
|
||||
assertEquals("Hello, Tony", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGlobalErrorHandling_whenNoUsername_thenOk() throws IOException {
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint5")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isBadRequest()
|
||||
.expectHeader()
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody()
|
||||
.jsonPath("$.message")
|
||||
.isNotEmpty()
|
||||
.jsonPath("$.message")
|
||||
.isEqualTo("please provide a name");
|
||||
.uri("/api/endpoint1")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hello, Stranger");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeFallback_whenUsernamePresent_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint2?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hello, Tony");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeFallback_whenNoUsername_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint2")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hello, Stranger");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeDynamicValue_whenUsernamePresent_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint3?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hello, Tony");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeDynamicValue_whenNoUsername_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint3")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hi, I looked around for your name but found: No value present");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeRethrow_whenUsernamePresent_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint4?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hello, Tony");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResumeRethrow_whenNoUsername_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint4")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectStatus().isBadRequest()
|
||||
.expectHeader().contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody().jsonPath("$.message").isEqualTo("please provide a name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGlobalErrorHandling_whenUsernamePresent_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint5?name={username}", "Tony")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectBody(String.class).isEqualTo("Hello, Tony");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGlobalErrorHandling_whenNoUsername_thenOk() {
|
||||
|
||||
webTestClient.get()
|
||||
.uri("/api/endpoint5")
|
||||
.accept(MediaType.TEXT_PLAIN)
|
||||
.exchange()
|
||||
.expectStatus().isBadRequest()
|
||||
.expectHeader().contentType(MediaType.APPLICATION_JSON)
|
||||
.expectBody().jsonPath("$.message").isEqualTo("please provide a name");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
<properties>
|
||||
<spring-statemachine-core.version>3.2.0</spring-statemachine-core.version>
|
||||
<spring-context.version>5.3.19</spring-context.version>
|
||||
<spring-test.version>4.3.7.RELEASE</spring-test.version>
|
||||
<spring-test.version>5.3.19</spring-test.version>
|
||||
<jayway.awaitility.version>1.7.0</jayway.awaitility.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt
|
|||
.stateEntry("S3", entryAction())
|
||||
.stateExit("S3", exitAction())
|
||||
.state("S4", executeAction(), errorAction())
|
||||
.stateDo("S5", executeAction());
|
||||
.state("S5", executeAction(), errorAction());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,32 +1,37 @@
|
|||
package com.baeldung.spring.statemachine;
|
||||
|
||||
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = SimpleStateMachineConfiguration.class)
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class StateMachineIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private StateMachine<String, String> stateMachine;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
stateMachine.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void whenSimpleStringStateMachineEvents_thenEndState() {
|
||||
assertEquals("SI", stateMachine.getState().getId());
|
||||
|
||||
|
@ -37,8 +42,8 @@ public class StateMachineIntegrationTest {
|
|||
assertEquals("S2", stateMachine.getState().getId());
|
||||
}
|
||||
|
||||
@Ignore("Fixing in JAVA-9808")
|
||||
@Test
|
||||
@Order(2)
|
||||
public void whenSimpleStringMachineActionState_thenActionExecuted() {
|
||||
|
||||
stateMachine.sendEvent("E3");
|
||||
|
@ -58,7 +63,7 @@ public class StateMachineIntegrationTest {
|
|||
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
|
||||
}
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
stateMachine.stop();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue