Search in sources :

Example 1 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class InvocableHandlerMethod method invoke.

/**
	 * Invoke the method for the given exchange.
	 * @param exchange the current exchange
	 * @param bindingContext the binding context to use
	 * @param providedArgs optional list of argument values to match by type
	 * @return Mono with a {@link HandlerResult}.
	 */
public Mono<HandlerResult> invoke(ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {
    return resolveArguments(exchange, bindingContext, providedArgs).then(args -> {
        try {
            Object value = doInvoke(args);
            HandlerResult result = new HandlerResult(this, value, getReturnType(), bindingContext);
            if (this.responseStatus != null) {
                exchange.getResponse().setStatusCode(this.responseStatus);
            }
            return Mono.just(result);
        } catch (InvocationTargetException ex) {
            return Mono.error(ex.getTargetException());
        } catch (Throwable ex) {
            return Mono.error(new IllegalStateException(getInvocationErrorMessage(args)));
        }
    });
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method invokeMethodWithValue.

@Test
public void invokeMethodWithValue() throws Exception {
    Mono<Object> resolvedValue = Mono.just("value1");
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method, resolverFor(resolvedValue));
    assertHandlerResultValue(mono, "success:value1");
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ResolvableMethod.on(org.springframework.web.method.ResolvableMethod.on) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) BindingContext(org.springframework.web.reactive.BindingContext) Mockito.when(org.mockito.Mockito.when) HandlerResult(org.springframework.web.reactive.HandlerResult) Assert.assertThat(org.junit.Assert.assertThat) HttpStatus(org.springframework.http.HttpStatus) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Mockito.any(org.mockito.Mockito.any) Method(java.lang.reflect.Method) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 3 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method invocationTargetExceptionIsUnwrapped.

@Test
public void invocationTargetExceptionIsUnwrapped() throws Exception {
    Method method = on(TestController.class).mockCall(TestController::exceptionMethod).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method);
    try {
        mono.block();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), is("boo"));
    }
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 4 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method invokeMethodWithNoArguments.

@Test
public void invokeMethodWithNoArguments() throws Exception {
    Method method = on(TestController.class).mockCall(TestController::noArgs).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method);
    assertHandlerResultValue(mono, "success");
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 5 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method invokeMethodWithResponseStatus.

@Test
public void invokeMethodWithResponseStatus() throws Exception {
    Method method = on(TestController.class).annotPresent(ResponseStatus.class).resolveMethod();
    Mono<HandlerResult> mono = invoke(new TestController(), method);
    assertHandlerResultValue(mono, "created");
    assertThat(this.exchange.getResponse().getStatusCode(), is(HttpStatus.CREATED));
}
Also used : ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

HandlerResult (org.springframework.web.reactive.HandlerResult)60 Test (org.junit.jupiter.api.Test)36 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)36 Method (java.lang.reflect.Method)24 MethodParameter (org.springframework.core.MethodParameter)23 BindingContext (org.springframework.web.reactive.BindingContext)20 Mono (reactor.core.publisher.Mono)20 HttpStatus (org.springframework.http.HttpStatus)19 StepVerifier (reactor.test.StepVerifier)19 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)18 Instant (java.time.Instant)17 Mockito.mock (org.mockito.Mockito.mock)17 UnsupportedMediaTypeStatusException (org.springframework.web.server.UnsupportedMediaTypeStatusException)17 List (java.util.List)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 ResolvableMethod (org.springframework.web.testfixture.method.ResolvableMethod)15 Duration (java.time.Duration)14 ArrayList (java.util.ArrayList)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Nullable (org.springframework.lang.Nullable)14