Search in sources :

Example 6 with BindingContext

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

the class PathVariableMethodArgumentResolverTests method resolveArgument.

@Test
public void resolveArgument() throws Exception {
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "value");
    this.exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    BindingContext bindingContext = new BindingContext();
    Mono<Object> mono = this.resolver.resolveArgument(this.paramNamedString, bindingContext, this.exchange);
    Object result = mono.block();
    assertEquals("value", result);
}
Also used : HashMap(java.util.HashMap) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 7 with BindingContext

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

the class PathVariableMethodArgumentResolverTests method wrapEmptyWithOptional.

@Test
public void wrapEmptyWithOptional() throws Exception {
    BindingContext bindingContext = new BindingContext();
    Mono<Object> mono = this.resolver.resolveArgument(this.paramOptional, bindingContext, this.exchange);
    StepVerifier.create(mono).consumeNextWith(value -> {
        assertTrue(value instanceof Optional);
        assertFalse(((Optional<?>) value).isPresent());
    }).expectComplete().verify();
}
Also used : Optional(java.util.Optional) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 8 with BindingContext

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

the class ControllerAdviceTests method initBinderAdvice.

@Test
public void initBinderAdvice() throws Exception {
    ApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
    RequestMappingHandlerAdapter adapter = createAdapter(context);
    TestController controller = context.getBean(TestController.class);
    Validator validator = mock(Validator.class);
    controller.setValidator(validator);
    BindingContext bindingContext = handle(adapter, controller, "handle").getBindingContext();
    WebExchangeDataBinder binder = bindingContext.createDataBinder(this.exchange, "name");
    assertEquals(Collections.singletonList(validator), binder.getValidators());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) BindingContext(org.springframework.web.reactive.BindingContext) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Example 9 with BindingContext

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

the class CookieValueMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.refresh();
    ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
    this.resolver = new CookieValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
    this.bindingContext = new BindingContext();
    Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
    this.cookieParameter = new SynthesizingMethodParameter(method, 0);
    this.cookieStringParameter = new SynthesizingMethodParameter(method, 1);
    this.stringParameter = new SynthesizingMethodParameter(method, 2);
    this.cookieMonoParameter = new SynthesizingMethodParameter(method, 3);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) Method(java.lang.reflect.Method) BindingContext(org.springframework.web.reactive.BindingContext) Before(org.junit.Before)

Example 10 with BindingContext

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

the class PathVariableMethodArgumentResolverTests method resolveArgumentWrappedAsOptional.

@Test
public void resolveArgumentWrappedAsOptional() throws Exception {
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "value");
    this.exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultFormattingConversionService());
    BindingContext bindingContext = new BindingContext(initializer);
    Mono<Object> mono = this.resolver.resolveArgument(this.paramOptional, bindingContext, this.exchange);
    Object result = mono.block();
    assertEquals(Optional.of("value"), result);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) HashMap(java.util.HashMap) BindingContext(org.springframework.web.reactive.BindingContext) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

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