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 DispatcherHandlerErrorTests method unknownReturnType.
@Test
public void unknownReturnType() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.get("/unknown-return-type").toExchange();
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher).consumeErrorWith(error -> {
assertThat(error, instanceOf(IllegalStateException.class));
assertThat(error.getMessage(), startsWith("No HandlerResultHandler"));
}).verify();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method bodyInserter.
@Test
public void bodyInserter() throws Exception {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer = (response, strategies) -> {
byte[] bodyBytes = body.getBytes(UTF_8);
ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
return response.writeWith(Mono.just(buffer));
};
Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
HandlerStrategies strategies = HandlerStrategies.empty().messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder())).build();
StepVerifier.create(result).consumeNextWith(response -> {
StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
response.writeTo(exchange, strategies);
}).expectComplete().verify();
assertNotNull(exchange.getResponse().getBody());
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method buildVoidPublisher.
@Test
public void buildVoidPublisher() throws Exception {
Mono<Void> mono = Mono.empty();
Mono<ServerResponse> result = ServerResponse.ok().build(mono);
ServerWebExchange exchange = mock(ServerWebExchange.class);
MockServerHttpResponse response = new MockServerHttpResponse();
when(exchange.getResponse()).thenReturn(response);
HandlerStrategies strategies = mock(HandlerStrategies.class);
result.then(res -> res.writeTo(exchange, strategies)).block();
StepVerifier.create(response.getBody()).expectComplete().verify();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class DispatcherHandlerErrorTests method responseBodyMessageConversionError.
@Test
public void responseBodyMessageConversionError() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.post("/request-body").accept(APPLICATION_JSON).body("body").toExchange();
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher).consumeErrorWith(error -> assertThat(error, instanceOf(NotAcceptableStatusException.class))).verify();
}
Aggregations