@Test public void resolveArgument() { testResolveArgument(this.testMethod.arg(ServerWebExchange.class), this.exchange); testResolveArgument(this.testMethod.arg(ServerHttpRequest.class), this.exchange.getRequest()); testResolveArgument(this.testMethod.arg(ServerHttpResponse.class), this.exchange.getResponse()); testResolveArgument(this.testMethod.arg(HttpMethod.class), HttpMethod.GET); testResolveArgument(this.testMethod.arg(TimeZone.class), TimeZone.getDefault()); testResolveArgument(this.testMethod.arg(ZoneId.class), ZoneId.systemDefault()); }
@SuppressWarnings("unchecked") private <T> T resolveValue(ServerWebExchange exchange, ResolvableType type) { MethodParameter param = this.testMethod.arg(type); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); Object value = result.block(Duration.ofSeconds(5)); assertNotNull(value); assertTrue("Unexpected return value type: " + value.getClass(), param.getParameterType().isAssignableFrom(value.getClass())); return (T) value; }
@Test public void resolveWithMonoOnBindingResultAndModelAttribute() { this.expectedException.expectMessage("An @ModelAttribute and an Errors/BindingResult argument " + "cannot both be declared with an async type wrapper."); MethodParameter parameter = this.testMethod.arg(BindingResult.class); this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange) .block(Duration.ofMillis(5000)); }
@Test // SPR-16187 public void resolveWithBindingResultNotFound() { this.expectedException.expectMessage("An Errors/BindingResult argument is expected " + "immediately after the @ModelAttribute argument"); MethodParameter parameter = this.testMethod.arg(Errors.class); this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange) .block(Duration.ofMillis(5000)); }
@Test public void supportsParameter() throws Exception { assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Model.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Map.class, String.class, Object.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.arg(ModelMap.class))); assertFalse(this.resolver.supportsParameter(this.testMethod.arg(Object.class))); }
@Test public void supports() { MethodParameter parameter = this.testMethod.arg(Errors.class); assertTrue(this.resolver.supportsParameter(parameter)); parameter = this.testMethod.arg(BindingResult.class); assertTrue(this.resolver.supportsParameter(parameter)); parameter = this.testMethod.arg(ResolvableType.forClassWithGenerics(Mono.class, Errors.class)); assertTrue(this.resolver.supportsParameter(parameter)); parameter = this.testMethod.arg(String.class); assertFalse(this.resolver.supportsParameter(parameter)); }
@Test public void monoList() throws Exception { String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"; ResolvableType type = forClassWithGenerics(Mono.class, forClassWithGenerics(List.class, TestBean.class)); MethodParameter param = this.testMethod.arg(type); Mono<?> mono = resolveValue(param, body); List<?> list = (List<?>) mono.block(Duration.ofSeconds(5)); assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), list); }
@Test public void resolve() { BindingResult bindingResult = createBindingResult(new Foo(), "foo"); this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "foo", bindingResult); MethodParameter parameter = this.testMethod.arg(Errors.class); Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange) .block(Duration.ofMillis(5000)); assertSame(bindingResult, actual); }
@Test public void monoTestBean() throws Exception { String body = "{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}"; ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); Mono<Object> mono = resolveValue(param, body); assertEquals(new TestBean("FOOFOO", "BARBAR"), mono.block()); }
@Test public void testBean() throws Exception { String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}"; MethodParameter param = this.testMethod.arg(TestBean.class); TestBean value = resolveValue(param, body); assertEquals(new TestBean("f1", "b1"), value); }
@Test public void fluxTestBean() throws Exception { String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"; ResolvableType type = forClassWithGenerics(Flux.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); Flux<TestBean> flux = resolveValue(param, body); assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), flux.collectList().block()); }
@Test public void resolveWithMono() { BindingResult bindingResult = createBindingResult(new Foo(), "foo"); MonoProcessor<BindingResult> monoProcessor = MonoProcessor.create(); monoProcessor.onNext(bindingResult); this.bindingContext.getModel().asMap().put(BindingResult.MODEL_KEY_PREFIX + "foo", monoProcessor); MethodParameter parameter = this.testMethod.arg(Errors.class); Object actual = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange) .block(Duration.ofMillis(5000)); assertSame(bindingResult, actual); }
@Test public void list() throws Exception { String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"; ResolvableType type = forClassWithGenerics(List.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); List<?> list = resolveValue(param, body); assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), list); }
@Test public void resolveUriComponentsBuilder() { MethodParameter param = this.testMethod.arg(UriComponentsBuilder.class); Object value = this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(); assertNotNull(value); assertEquals(UriComponentsBuilder.class, value.getClass()); assertEquals("http://example.org:9999/next", ((UriComponentsBuilder) value).path("/next").toUriString()); }
@Test public void observableTestBean() throws Exception { String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"; ResolvableType type = forClassWithGenerics(Observable.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); Observable<?> observable = resolveValue(param, body); assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), observable.toList().toBlocking().first()); }
@Test public void rxJava2SingleTestBean() throws Exception { String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}"; ResolvableType type = forClassWithGenerics(io.reactivex.Single.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); io.reactivex.Single<TestBean> single = resolveValue(param, body); assertEquals(new TestBean("f1", "b1"), single.blockingGet()); }
@Test public void rxJava2MaybeTestBean() throws Exception { String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}"; ResolvableType type = forClassWithGenerics(Maybe.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); Maybe<TestBean> maybe = resolveValue(param, body); assertEquals(new TestBean("f1", "b1"), maybe.blockingGet()); }
@Test public void singleTestBean() throws Exception { String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}"; ResolvableType type = forClassWithGenerics(Single.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); Single<TestBean> single = resolveValue(param, body); assertEquals(new TestBean("f1", "b1"), single.toBlocking().value()); }
@Test public void supportsParameter() { assertFalse(this.resolver.supportsParameter(this.testMethod.arg(String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod .annot(matrixAttribute().noName()).arg(List.class, String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod .annot(matrixAttribute().name("year")).arg(int.class))); }
@Test public void supportsParameter() { assertFalse(this.resolver.supportsParameter(this.testMethod.arg(String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().noName()) .arg(Map.class, String.class, String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().noPathVar()) .arg(MultiValueMap.class, String.class, String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().pathVar("cars")) .arg(MultiValueMap.class, String.class, String.class))); assertFalse(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().name("name")) .arg(Map.class, String.class, String.class))); }