@Test public void resolveWithName() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().name("specialFoo")).arg(); Foo foo = new Foo(); this.exchange.getAttributes().put("specialFoo", foo); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(foo, mono.block()); }
@Test public void resolveOptional() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().name("foo")).arg(Optional.class, Foo.class); 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(); this.exchange.getAttributes().put("foo", 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()); }
@Test public void resolveNotRequired() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().name("foo").notRequired()).arg(); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertNull(mono.block()); Foo foo = new Foo(); this.exchange.getAttributes().put("foo", foo); mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(foo, mono.block()); }
@Test // SPR-16158 public void resolveMonoParameter() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().noName()).arg(Mono.class, Foo.class); // Mono attribute Foo foo = new Foo(); Mono<Foo> fooMono = Mono.just(foo); this.exchange.getAttributes().put("fooMono", fooMono); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(fooMono, mono.block(Duration.ZERO)); // RxJava Single attribute Single<Foo> singleMono = Single.just(foo); this.exchange.getAttributes().clear(); this.exchange.getAttributes().put("fooMono", singleMono); mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); Object value = mono.block(Duration.ZERO); assertTrue(value instanceof Mono); assertSame(foo, ((Mono<?>) value).block(Duration.ZERO)); // No attribute --> Mono.empty this.exchange.getAttributes().clear(); mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(Mono.empty(), mono.block(Duration.ZERO)); }
@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 supportsParameter() throws Exception { assertTrue(this.resolver.supportsParameter( this.testMethod.annot(requestAttribute().noName()).arg(Foo.class))); // SPR-16158 assertTrue(this.resolver.supportsParameter( this.testMethod.annotPresent(RequestAttribute.class).arg(Mono.class, Foo.class))); assertFalse(this.resolver.supportsParameter( this.testMethod.annotNotPresent(RequestAttribute.class).arg())); }