Search in sources :

Example 41 with BindingContext

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

the class SessionAttributeMethodArgumentResolverTests method resolve.

@Test
public void resolve() throws Exception {
    MethodParameter param = initMethodParameter(0);
    Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    StepVerifier.create(mono).expectError(ServerWebInputException.class).verify();
    Foo foo = new Foo();
    when(this.session.getAttribute("foo")).thenReturn(Optional.of(foo));
    mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertSame(foo, mono.block());
}
Also used : ServerWebInputException(org.springframework.web.server.ServerWebInputException) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 42 with BindingContext

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

the class SessionAttributeMethodArgumentResolverTests method resolveOptional.

@Test
public void resolveOptional() throws Exception {
    MethodParameter param = initMethodParameter(3);
    Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    assertFalse(((Optional<?>) mono.block()).isPresent());
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    BindingContext bindingContext = new BindingContext(initializer);
    Foo foo = new Foo();
    when(this.session.getAttribute("foo")).thenReturn(Optional.of(foo));
    mono = this.resolver.resolveArgument(param, bindingContext, this.exchange);
    assertNotNull(mono.block());
    assertEquals(Optional.class, mono.block().getClass());
    Optional<?> optional = (Optional<?>) mono.block();
    assertTrue(optional.isPresent());
    assertSame(foo, optional.get());
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Optional(java.util.Optional) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 43 with BindingContext

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

the class RequestParamMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
    this.resolver = new RequestParamMethodArgumentResolver(null, adapterRegistry, true);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    this.bindContext = new BindingContext(initializer);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Before(org.junit.Before)

Aggregations

BindingContext (org.springframework.web.reactive.BindingContext)43 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)16 MethodParameter (org.springframework.core.MethodParameter)14 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)10 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)9 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)8 WebDataBinder (org.springframework.web.bind.WebDataBinder)7 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)6 Mono (reactor.core.publisher.Mono)6 Map (java.util.Map)5 Before (org.junit.Before)5 HashMap (java.util.HashMap)4 Optional (java.util.Optional)4 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)4 ServerWebInputException (org.springframework.web.server.ServerWebInputException)4 Method (java.lang.reflect.Method)3 List (java.util.List)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 ReactiveAdapter (org.springframework.core.ReactiveAdapter)3