Search in sources :

Example 1 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 2 with Mono

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

the class MessageReaderArgumentResolverTests method emptyBody.

// More extensive "empty body" tests in RequestBody- and HttpEntityArgumentResolverTests
// SPR-9942
@Test
// SPR-9942
@SuppressWarnings("unchecked")
public void emptyBody() throws Exception {
    ServerWebExchange exchange = post("/path").contentType(MediaType.APPLICATION_JSON).toExchange();
    ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
    MethodParameter param = this.testMethod.arg(type);
    Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody(param, true, this.bindingContext, exchange).block();
    StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerWebInputException(org.springframework.web.server.ServerWebInputException) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 3 with Mono

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

the class ModelAttributeMethodArgumentResolverTests method testValidationError.

private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor) throws URISyntaxException {
    ServerWebExchange exchange = postForm("age=invalid");
    Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
    mono = valueMonoExtractor.apply(mono);
    StepVerifier.create(mono).consumeErrorWith(ex -> {
        assertTrue(ex instanceof WebExchangeBindException);
        WebExchangeBindException bindException = (WebExchangeBindException) ex;
        assertEquals(1, bindException.getErrorCount());
        assertTrue(bindException.hasFieldErrors("age"));
    }).verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) Single(rx.Single) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Duration(java.time.Duration) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Before(org.junit.Before) Validated(org.springframework.validation.annotation.Validated) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ResolvableMethod(org.springframework.web.method.ResolvableMethod) RxReactiveStreams(rx.RxReactiveStreams) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException)

Example 4 with Mono

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

the class ModelInitializerTests method basic.

@SuppressWarnings("unchecked")
@Test
public void basic() throws Exception {
    TestController controller = new TestController();
    Validator validator = mock(Validator.class);
    controller.setValidator(validator);
    List<SyncInvocableHandlerMethod> binderMethods = getBinderMethods(controller);
    List<InvocableHandlerMethod> attributeMethods = getAttributeMethods(controller);
    WebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    BindingContext bindingContext = new InitBinderBindingContext(bindingInitializer, binderMethods);
    this.modelInitializer.initModel(bindingContext, attributeMethods, this.exchange).block(Duration.ofMillis(5000));
    WebExchangeDataBinder binder = bindingContext.createDataBinder(this.exchange, "name");
    assertEquals(Collections.singletonList(validator), binder.getValidators());
    Map<String, Object> model = bindingContext.getModel().asMap();
    assertEquals(5, model.size());
    Object value = model.get("bean");
    assertEquals("Bean", ((TestBean) value).getName());
    value = model.get("monoBean");
    assertEquals("Mono Bean", ((Mono<TestBean>) value).block(Duration.ofMillis(5000)).getName());
    value = model.get("singleBean");
    assertEquals("Single Bean", ((Single<TestBean>) value).toBlocking().value().getName());
    value = model.get("voidMethodBean");
    assertEquals("Void Method Bean", ((TestBean) value).getName());
    value = model.get("voidMonoMethodBean");
    assertEquals("Void Mono Method Bean", ((TestBean) value).getName());
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Mono(reactor.core.publisher.Mono) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) BindingContext(org.springframework.web.reactive.BindingContext) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Validator(org.springframework.validation.Validator) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Test(org.junit.Test)

Example 5 with Mono

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

the class RequestBodyArgumentResolverTests method emptyBodyWithMono.

@Test
@SuppressWarnings("unchecked")
public void emptyBodyWithMono() throws Exception {
    MethodParameter param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class);
    StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param)).expectNextCount(0).expectError(ServerWebInputException.class).verify();
    param = this.testMethod.annot(requestBody().notRequired()).arg(Mono.class, String.class);
    StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param)).expectNextCount(0).expectComplete().verify();
}
Also used : ServerWebInputException(org.springframework.web.server.ServerWebInputException) Mono(reactor.core.publisher.Mono) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Aggregations

Mono (reactor.core.publisher.Mono)306 Test (org.junit.jupiter.api.Test)143 StepVerifier (reactor.test.StepVerifier)117 List (java.util.List)116 Flux (reactor.core.publisher.Flux)110 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)102 Collections (java.util.Collections)85 Map (java.util.Map)75 HttpStatus (org.springframework.http.HttpStatus)66 ServerWebExchange (org.springframework.web.server.ServerWebExchange)65 Duration (java.time.Duration)60 ArrayList (java.util.ArrayList)58 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)52 Test (org.junit.Test)51 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assert (org.springframework.util.Assert)49 Mockito.mock (org.mockito.Mockito.mock)48 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)46 Optional (java.util.Optional)44 StandardCharsets (java.nio.charset.StandardCharsets)43