Search in sources :

Example 6 with Mono

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

the class ViewResolutionResultHandler method resolveAsyncAttributes.

private Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {
    List<String> names = new ArrayList<>();
    List<Mono<?>> valueMonos = new ArrayList<>();
    for (Map.Entry<String, ?> entry : model.entrySet()) {
        ReactiveAdapter adapter = getAdapterRegistry().getAdapter(null, entry.getValue());
        if (adapter != null) {
            names.add(entry.getKey());
            if (adapter.isMultiValue()) {
                Flux<Object> value = Flux.from(adapter.toPublisher(entry.getValue()));
                valueMonos.add(value.collectList().defaultIfEmpty(Collections.emptyList()));
            } else {
                Mono<Object> value = Mono.from(adapter.toPublisher(entry.getValue()));
                valueMonos.add(value.defaultIfEmpty(NO_VALUE));
            }
        }
    }
    if (names.isEmpty()) {
        return Mono.empty();
    }
    return Mono.when(valueMonos, values -> {
        for (int i = 0; i < values.length; i++) {
            if (values[i] != NO_VALUE) {
                model.put(names.get(i), values[i]);
            } else {
                model.remove(names.get(i));
            }
        }
        return NO_VALUE;
    }).then();
}
Also used : HttpRequestPathHelper(org.springframework.web.server.support.HttpRequestPathHelper) Ordered(org.springframework.core.Ordered) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerResultHandlerSupport(org.springframework.web.reactive.result.HandlerResultHandlerSupport) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Model(org.springframework.ui.Model) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Locale(java.util.Locale) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) HandlerResultHandler(org.springframework.web.reactive.HandlerResultHandler) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ReactiveAdapter(org.springframework.core.ReactiveAdapter) ClassUtils(org.springframework.util.ClassUtils) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) Collectors(java.util.stream.Collectors) HandlerResult(org.springframework.web.reactive.HandlerResult) Flux(reactor.core.publisher.Flux) List(java.util.List) Collections(java.util.Collections) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) Mono(reactor.core.publisher.Mono) ArrayList(java.util.ArrayList) Map(java.util.Map) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 7 with Mono

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

the class DispatcherHandlerErrorTests method unknownReturnType.

@Test
public void unknownReturnType() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/unknown-return-type").toExchange();
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> {
        assertThat(error, instanceOf(IllegalStateException.class));
        assertThat(error.getMessage(), startsWith("No HandlerResultHandler"));
    }).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 8 with Mono

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

the class DefaultEntityResponseBuilderTests method bodyInserter.

@Test
public void bodyInserter() throws Exception {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
        DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
        return response.writeWith(Mono.just(buffer));
    };
    Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
    MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
    HandlerStrategies strategies = HandlerStrategies.empty().messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder())).build();
    StepVerifier.create(result).consumeNextWith(response -> {
        StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
        response.writeTo(exchange, strategies);
    }).expectComplete().verify();
    assertNotNull(exchange.getResponse().getBody());
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) CacheControl(org.springframework.http.CacheControl) ByteBuffer(java.nio.ByteBuffer) BodyInserter(org.springframework.web.reactive.function.BodyInserter) Assert.assertSame(org.junit.Assert.assertSame) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ResolvableType(org.springframework.core.ResolvableType) EnumSet(java.util.EnumSet) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Collections(java.util.Collections) Mono(reactor.core.publisher.Mono) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 9 with Mono

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

the class DefaultServerResponseBuilderTests method buildVoidPublisher.

@Test
public void buildVoidPublisher() throws Exception {
    Mono<Void> mono = Mono.empty();
    Mono<ServerResponse> result = ServerResponse.ok().build(mono);
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    when(exchange.getResponse()).thenReturn(response);
    HandlerStrategies strategies = mock(HandlerStrategies.class);
    result.then(res -> res.writeTo(exchange, strategies)).block();
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) HttpHeaders(org.springframework.http.HttpHeaders) ZonedDateTime(java.time.ZonedDateTime) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) CacheControl(org.springframework.http.CacheControl) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) URI(java.net.URI) Collections(java.util.Collections) EnumSet(java.util.EnumSet) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 10 with Mono

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

the class DispatcherHandlerErrorTests method responseBodyMessageConversionError.

@Test
public void responseBodyMessageConversionError() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.post("/request-body").accept(APPLICATION_JSON).body("body").toExchange();
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> assertThat(error, instanceOf(NotAcceptableStatusException.class))).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) Test(org.junit.Test)

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