@Test @SuppressWarnings("rawtypes") public void missingOptionalParamValue() throws Exception { ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultConversionService()); WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer); MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class); Object result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertFalse(((Optional) result).isPresent()); }
@SafeVarargs public final ArgResolver annotPresent(Class<? extends Annotation>... annotationTypes) { return new ArgResolver().annotPresent(annotationTypes); }
/** * Filter on method arguments that don't have the given annotation type(s). * @param annotationTypes the annotation types */ @SafeVarargs public final ArgResolver annotNotPresent(Class<? extends Annotation>... annotationTypes) { return new ArgResolver().annotNotPresent(annotationTypes); }
@Test @SuppressWarnings("rawtypes") public void missingOptionalParamList() throws Exception { ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultConversionService()); WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer); MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, List.class); Object result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertFalse(((Optional) result).isPresent()); }
@Test public void bindDataClass() throws Exception { testBindBar(this.testMethod.annotNotPresent(ModelAttribute.class).arg(Bar.class)); }
@Test @SuppressWarnings("unchecked") public void validationErrorToMono() throws Exception { MethodParameter parameter = this.testMethod .annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class); testValidationError(parameter, resolvedArgumentMono -> { Object value = resolvedArgumentMono.block(Duration.ofSeconds(5)); assertNotNull(value); assertTrue(value instanceof Mono); return (Mono<?>) value; }); }
@Test public void resolvePartNotAnnot() throws Exception { MockPart expected = new MockPart("part", "Hello World".getBytes()); MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); request.setContentType("multipart/form-data"); request.addPart(expected); webRequest = new ServletWebRequest(request); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof Part); assertEquals("Invalid result", expected, result); }
@Test // SPR-9079 public void isMultipartRequestHttpPut() throws Exception { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); MultipartFile expected = new MockMultipartFile("multipartFileList", "Hello World".getBytes()); request.addFile(expected); request.setMethod("PUT"); webRequest = new ServletWebRequest(request); MethodParameter param = this.testMethod .annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class); Object actual = resolver.resolveArgument(param, null, webRequest, null); assertTrue(actual instanceof List); assertEquals(expected, ((List<?>) actual).get(0)); }
@Test public void resolvePart() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); MockPart expected = new MockPart("pfile", "Hello World".getBytes()); request.setMethod("POST"); request.setContentType("multipart/form-data"); request.addPart(expected); webRequest = new ServletWebRequest(request); MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof Part); assertEquals("Invalid result", expected, result); }
@Test public void resolve() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().noName()).arg(Foo.class); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); Foo foo = new Foo(); this.exchange.getAttributes().put("foo", foo); mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(foo, mono.block()); }
@Test public void supports() throws Exception { ModelAttributeMethodArgumentResolver resolver = new ModelAttributeMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance(), false); MethodParameter param = this.testMethod.annotPresent(ModelAttribute.class).arg(Foo.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, Foo.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); assertFalse(resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class); assertFalse(resolver.supportsParameter(param)); }
@Test public void personNotRequired() { MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Person.class); ServerWebExchange exchange = createExchange(new MultipartBodyBuilder()); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); StepVerifier.create(result).verifyComplete(); }
@Test public void personRequired() { MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class); ServerWebExchange exchange = createExchange(new MultipartBodyBuilder()); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); StepVerifier.create(result).expectError(ServerWebInputException.class).verify(); }
/** * Resolve the argument also matching to the given type. * @param type the expected type */ public MethodParameter arg(ResolvableType type) { this.filters.add(p -> type.toString().equals(ResolvableType.forMethodParameter(p).toString())); return arg(); }
@Test(expected = ServletRequestBindingException.class) public void resolveArgumentMultipleMatches() throws Exception { getVariablesFor("var1").add("colors", "red"); getVariablesFor("var2").add("colors", "green"); MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class); this.resolver.resolveArgument(param, this.mavContainer, this.webRequest, null); }
@Test public void resolveArgumentDefaultValue() throws Exception { MethodParameter param = this.testMethod.annot(matrixAttribute().name("year")).arg(int.class); assertEquals("2013", resolver.resolveArgument(param, this.mavContainer, this.webRequest, null)); }
@Test public void resolveArgumentPathVariable() throws Exception { getVariablesFor("cars").add("year", "2006"); getVariablesFor("bikes").add("year", "2005"); MethodParameter param = this.testMethod.annot(matrixAttribute().name("year")).arg(int.class); assertEquals("2006", this.resolver.resolveArgument(param, this.mavContainer, this.webRequest, null)); }
@Test public void resolveArgumentNoParams() throws Exception { MethodParameter param = this.testMethod.annot(matrixAttribute().noName()) .arg(Map.class, String.class, String.class); @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO); assertEquals(Collections.emptyMap(), map); }
@Test public void validationErrorToSingle() throws Exception { MethodParameter parameter = this.testMethod .annotPresent(ModelAttribute.class).arg(Single.class, Foo.class); testValidationError(parameter, resolvedArgumentMono -> { Object value = resolvedArgumentMono.block(Duration.ofSeconds(5)); assertNotNull(value); assertTrue(value instanceof Single); return Mono.from(RxReactiveStreams.toPublisher((Single<?>) value)); }); }
@Test public void createAndBindToSingle() throws Exception { MethodParameter parameter = this.testMethod .annotPresent(ModelAttribute.class).arg(Single.class, Foo.class); testBindFoo("fooSingle", parameter, single -> { assertTrue(single.getClass().getName(), single instanceof Single); Object value = ((Single<?>) single).toBlocking().value(); assertEquals(Foo.class, value.getClass()); return (Foo) value; }); }