Search in sources :

Example 36 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method illegalArgumentExceptionIsWrappedWithInvocationDetails.

@Test
public void illegalArgumentExceptionIsWrappedWithInvocationDetails() throws Exception {
    Mono<Object> resolvedValue = Mono.just(1);
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method, resolverFor(resolvedValue));
    try {
        mono.block();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), is("Failed to invoke handler method with resolved arguments: " + "[0][type=java.lang.Integer][value=1] " + "on " + method.toGenericString()));
    }
}
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 37 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method resolverThrowsException.

@Test
public void resolverThrowsException() throws Exception {
    Mono<Object> resolvedValue = Mono.error(new UnsupportedMediaTypeStatusException("boo"));
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method, resolverFor(resolvedValue));
    try {
        mono.block();
        fail("Expected UnsupportedMediaTypeStatusException");
    } catch (UnsupportedMediaTypeStatusException ex) {
        assertThat(ex.getMessage(), is("Request failure [status: 415, reason: \"boo\"]"));
    }
}
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) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) HandlerResult(org.springframework.web.reactive.HandlerResult) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 38 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method noMatchingResolver.

@Test
public void noMatchingResolver() throws Exception {
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method);
    try {
        mono.block();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), is("No suitable resolver for argument 0 of type 'java.lang.String' " + "on " + method.toGenericString()));
    }
}
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 39 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class InvocableHandlerMethodTests method invokeMethodWithNoValue.

@Test
public void invokeMethodWithNoValue() throws Exception {
    Mono<Object> resolvedValue = Mono.empty();
    Method method = on(TestController.class).mockCall(o -> o.singleArg(null)).method();
    Mono<HandlerResult> mono = invoke(new TestController(), method, resolverFor(resolvedValue));
    assertHandlerResultValue(mono, "success:null");
}
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 40 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class FormHttpMessageWriter method write.

@Override
public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    if (contentType == null) {
        contentType = MediaType.APPLICATION_FORM_URLENCODED;
        outputMessage.getHeaders().setContentType(contentType);
    }
    Charset charset = getMediaTypeCharset(contentType);
    return Flux.from(inputStream).single().map(form -> generateForm(form, charset)).then(value -> {
        ByteBuffer byteBuffer = charset.encode(value);
        DataBuffer buffer = outputMessage.bufferFactory().wrap(byteBuffer);
        outputMessage.getHeaders().setContentLength(byteBuffer.remaining());
        return outputMessage.writeWith(Mono.just(buffer));
    });
}
Also used : Iterator(java.util.Iterator) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) Flux(reactor.core.publisher.Flux) URLEncoder(java.net.URLEncoder) List(java.util.List) Charset(java.nio.charset.Charset) Map(java.util.Map) ResolvableType(org.springframework.core.ResolvableType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Assert(org.springframework.util.Assert) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

Mono (reactor.core.publisher.Mono)40 Test (org.junit.Test)28 ServerWebExchange (org.springframework.web.server.ServerWebExchange)21 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)18 HttpStatus (org.springframework.http.HttpStatus)17 StepVerifier (reactor.test.StepVerifier)17 List (java.util.List)16 Assert.assertEquals (org.junit.Assert.assertEquals)14 Collections (java.util.Collections)12 BindingContext (org.springframework.web.reactive.BindingContext)12 Matchers.is (org.hamcrest.Matchers.is)11 Assert.assertThat (org.junit.Assert.assertThat)11 MethodParameter (org.springframework.core.MethodParameter)11 Map (java.util.Map)10 ResolvableType (org.springframework.core.ResolvableType)10 ServerWebInputException (org.springframework.web.server.ServerWebInputException)10 Publisher (org.reactivestreams.Publisher)9 Assert (org.springframework.util.Assert)9 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 MediaType (org.springframework.http.MediaType)8