use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class ViewResolutionResultHandler method resolveAsyncAttributes.
private Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {
List<String> names = new ArrayList<>();
List<Mono<?>> valueMonos = new ArrayList<>();
for (Map.Entry<String, ?> entry : model.entrySet()) {
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(null, entry.getValue());
if (adapter != null) {
names.add(entry.getKey());
if (adapter.isMultiValue()) {
Flux<Object> value = Flux.from(adapter.toPublisher(entry.getValue()));
valueMonos.add(value.collectList().defaultIfEmpty(Collections.emptyList()));
} else {
Mono<Object> value = Mono.from(adapter.toPublisher(entry.getValue()));
valueMonos.add(value.defaultIfEmpty(NO_VALUE));
}
}
}
if (names.isEmpty()) {
return Mono.empty();
}
return Mono.when(valueMonos, values -> {
for (int i = 0; i < values.length; i++) {
if (values[i] != NO_VALUE) {
model.put(names.get(i), values[i]);
} else {
model.remove(names.get(i));
}
}
return NO_VALUE;
}).then();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class MessageReaderArgumentResolverTests method emptyBody.
// More extensive "empty body" tests in RequestBody- and HttpEntityArgumentResolverTests
// SPR-9942
@Test
// SPR-9942
@SuppressWarnings("unchecked")
public void emptyBody() throws Exception {
ServerWebExchange exchange = post("/path").contentType(MediaType.APPLICATION_JSON).toExchange();
ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
MethodParameter param = this.testMethod.arg(type);
Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody(param, true, this.bindingContext, exchange).block();
StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class ModelAttributeMethodArgumentResolverTests method testValidationError.
private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor) throws URISyntaxException {
ServerWebExchange exchange = postForm("age=invalid");
Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
mono = valueMonoExtractor.apply(mono);
StepVerifier.create(mono).consumeErrorWith(ex -> {
assertTrue(ex instanceof WebExchangeBindException);
WebExchangeBindException bindException = (WebExchangeBindException) ex;
assertEquals(1, bindException.getErrorCount());
assertTrue(bindException.hasFieldErrors("age"));
}).verify();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class ModelInitializerTests method basic.
@SuppressWarnings("unchecked")
@Test
public void basic() throws Exception {
TestController controller = new TestController();
Validator validator = mock(Validator.class);
controller.setValidator(validator);
List<SyncInvocableHandlerMethod> binderMethods = getBinderMethods(controller);
List<InvocableHandlerMethod> attributeMethods = getAttributeMethods(controller);
WebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
BindingContext bindingContext = new InitBinderBindingContext(bindingInitializer, binderMethods);
this.modelInitializer.initModel(bindingContext, attributeMethods, this.exchange).block(Duration.ofMillis(5000));
WebExchangeDataBinder binder = bindingContext.createDataBinder(this.exchange, "name");
assertEquals(Collections.singletonList(validator), binder.getValidators());
Map<String, Object> model = bindingContext.getModel().asMap();
assertEquals(5, model.size());
Object value = model.get("bean");
assertEquals("Bean", ((TestBean) value).getName());
value = model.get("monoBean");
assertEquals("Mono Bean", ((Mono<TestBean>) value).block(Duration.ofMillis(5000)).getName());
value = model.get("singleBean");
assertEquals("Single Bean", ((Single<TestBean>) value).toBlocking().value().getName());
value = model.get("voidMethodBean");
assertEquals("Void Method Bean", ((TestBean) value).getName());
value = model.get("voidMonoMethodBean");
assertEquals("Void Mono Method Bean", ((TestBean) value).getName());
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class RequestBodyArgumentResolverTests method emptyBodyWithMono.
@Test
@SuppressWarnings("unchecked")
public void emptyBodyWithMono() throws Exception {
MethodParameter param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class);
StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param)).expectNextCount(0).expectError(ServerWebInputException.class).verify();
param = this.testMethod.annot(requestBody().notRequired()).arg(Mono.class, String.class);
StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param)).expectNextCount(0).expectComplete().verify();
}
Aggregations