use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolveOptionalParamValue.
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Throwable {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = new BindingContext(initializer);
webRequest.setBindingContext(binderFactory);
ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
Object result = resolver.resolveArgument(webRequest, param);
assertThat(result).isEqualTo(Optional.empty());
request.addParameter("name", "123");
result = resolver.resolveArgument(webRequest, param);
assertThat(result.getClass()).isEqualTo(Optional.class);
assertThat(((Optional) result).get()).isEqualTo(123);
}
use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method optionalMultipartFileWithoutMultipartRequest.
@Test
public void optionalMultipartFileWithoutMultipartRequest() throws Throwable {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = new BindingContext(initializer);
webRequest.setBindingContext(binderFactory);
ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
Object actual = resolver.resolveArgument(webRequest, param);
assertThat(actual).isEqualTo(Optional.empty());
}
use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method missingOptionalParamValue.
@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamValue() throws Throwable {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = new BindingContext(initializer);
webRequest.setBindingContext(binderFactory);
ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
Object result = resolver.resolveArgument(webRequest, param);
assertThat(result).isEqualTo(Optional.empty());
result = resolver.resolveArgument(webRequest, param);
assertThat(result.getClass()).isEqualTo(Optional.class);
assertThat(((Optional) result).isPresent()).isFalse();
}
use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolveOptionalParamArray.
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamArray() throws Throwable {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = new BindingContext(initializer);
webRequest.setBindingContext(binderFactory);
ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer[].class);
Object result = resolver.resolveArgument(webRequest, param);
assertThat(result).isEqualTo(Optional.empty());
request.addParameter("name", "123", "456");
result = resolver.resolveArgument(webRequest, param);
assertThat(result.getClass()).isEqualTo(Optional.class);
assertThat((Integer[]) ((Optional) result).get()).isEqualTo(new Integer[] { 123, 456 });
}
use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.
@Test
public void resolveOptionalMultipartFile() throws Throwable {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = new BindingContext(initializer);
webRequest.setBindingContext(binderFactory);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
request.addFile(expected);
webRequest = new ServletRequestContext(null, request, null);
webRequest.setBindingContext(new BindingContext(initializer));
ResolvableMethodParameter param = testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
Object result = resolver.resolveArgument(webRequest, param);
boolean condition = result instanceof Optional;
assertThat(condition).isTrue();
assertThat(((Optional<?>) result).get()).as("Invalid result").isEqualTo(expected);
}
Aggregations