private Mono<Void> invokeModelAttributeMethods(BindingContext bindingContext, List<InvocableHandlerMethod> modelMethods, ServerWebExchange exchange) { List<Mono<HandlerResult>> resultList = new ArrayList<>(); modelMethods.forEach(invocable -> resultList.add(invocable.invoke(exchange, bindingContext))); return Mono .zip(resultList, objectArray -> Arrays.stream(objectArray) .map(object -> handleResult(((HandlerResult) object), bindingContext)) .collect(Collectors.toList())) .flatMap(Mono::when); }
return Mono.zip(valueMonos, values -> { for (int i=0; i < values.length; i++) {
return Mono.zip(argMonos, values -> Stream.of(values).map(o -> o != NO_ARG_VALUE ? o : null).toArray());
@Test public void oneSourcePublisherCombined() { assertThat(Mono.zip(args -> (int) args[0], Mono.just(1)) .block()).isEqualTo(1); }
@Test public void allNonEmpty2() { assertThat(Mono.zip(args -> (int) args[0] + (int) args[1], Mono.just(1), Mono.just(2)) .block()).isEqualTo(3); }
@Test public void allNonEmptyIterable() { assertThat(Mono.zip(Arrays.asList(Mono.just(1), Mono.just(2)), args -> (int) args[0] + (int) args[1]) .block()).isEqualTo(3); }
@Test(timeout = 5000) public void castCheck() { Mono<String[]> mono = Mono.zip(a -> Arrays.copyOf(a, a.length, String[].class), Mono.just("hello"), Mono.just("world")); mono.subscribe(System.out::println); }
@Test public void allEmpty() { Assert.assertNull(Mono.zip(Mono.empty(), Mono.empty()) .block()); }
@Test public void noSourcePublisherCombined() { assertThat(Mono.zip(args -> (int) args[0] + (int) args[1]) .block()).isNull(); }
/** * Combine query params and form data for multipart form data from the body * of the request into a {@code Map<String, Object>} of values to use for * data binding purposes. * @param exchange the current exchange * @return a {@code Mono} with the values to bind * @see org.springframework.http.server.reactive.ServerHttpRequest#getQueryParams() * @see ServerWebExchange#getFormData() * @see ServerWebExchange#getMultipartData() */ public static Mono<Map<String, Object>> extractValuesToBind(ServerWebExchange exchange) { MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams(); Mono<MultiValueMap<String, String>> formData = exchange.getFormData(); Mono<MultiValueMap<String, Part>> multipartData = exchange.getMultipartData(); return Mono.zip(Mono.just(queryParams), formData, multipartData) .map(tuple -> { Map<String, Object> result = new TreeMap<>(); tuple.getT1().forEach((key, values) -> addBindValue(result, key, values)); tuple.getT2().forEach((key, values) -> addBindValue(result, key, values)); tuple.getT3().forEach((key, values) -> addBindValue(result, key, values)); return result; }); }
@Test public void emptySources() { AtomicBoolean cancelled = new AtomicBoolean(); Mono<String> empty1 = Mono.empty(); Mono<String> empty2 = Mono.empty(); Mono<String> empty3 = Mono.<String>empty().delaySubscription(Duration.ofMillis(500)) .doOnCancel(() -> cancelled.set(true)); Duration d = StepVerifier.create(Mono.zip(empty1, empty2, empty3)) .verifyComplete(); assertThat(cancelled).isTrue(); assertThat(d).isLessThan(Duration.ofMillis(500)); }
Mono<Request> createDefaultedRequest(String clientRegistrationId, Authentication authentication, ServerWebExchange exchange) { Mono<Authentication> defaultedAuthentication = Mono.justOrEmpty(authentication) .switchIfEmpty(currentAuthentication()); Mono<String> defaultedRegistrationId = Mono.justOrEmpty(clientRegistrationId) .switchIfEmpty(Mono.justOrEmpty(this.defaultClientRegistrationId)) .switchIfEmpty(clientRegistrationId(defaultedAuthentication)); Mono<Optional<ServerWebExchange>> defaultedExchange = Mono.justOrEmpty(exchange) .switchIfEmpty(currentServerWebExchange()).map(Optional::of) .defaultIfEmpty(Optional.empty()); return Mono.zip(defaultedRegistrationId, defaultedAuthentication, defaultedExchange) .map(t3 -> new Request(t3.getT1(), t3.getT2(), t3.getT3().orElse(null))); }
@Test//(timeout = 5000) public void all2NonEmpty() { Assert.assertEquals(Tuples.of(0L, 0L), Mono.zip(Mono.delay(Duration.ofMillis(150)), Mono.delay(Duration.ofMillis(250))) .block()); }
@Test public void whenIterableDoesntCombineErrors() { Exception boom1 = new NullPointerException("boom1"); Exception boom2 = new IllegalArgumentException("boom2"); StepVerifier.create(Mono.zip( Arrays.asList(Mono.just("foo"), Mono.<String>error(boom1), Mono.<String>error(boom2)), Tuples.fn3())) .verifyErrorMatches(e -> e == boom1); }
@Test public void whenMonoJust7() { StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2), Mono.just(3), Mono.just(4), Mono.just(5), Mono.just(6), Mono.just(7))) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3 && v.getT4() == 4 && v.getT5() == 5 && v.getT6() == 6 && v.getT7() == 7).isTrue()) .verifyComplete(); }
@Test public void whenMonoError() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.<Integer>error(new Exception("test1")), Mono.<Integer>error(new Exception("test2"))) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isTrue()) .then(() -> assertThat(mp.isSuccess()).isFalse()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyErrorSatisfies(e -> assertThat(e).hasMessage("test1")); }
@Test public void whenMonoJust() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void whenMonoJust3() { MonoProcessor<Tuple3<Integer, Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2), Mono.just(3)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3).isTrue()) .verifyComplete(); }
@Test public void someEmpty() { StepVerifier.withVirtualTime(() -> Mono.zip(Mono.delay(Duration.ofMillis(150)).then(), Mono.delay(Duration .ofMillis(250)))) .thenAwait(Duration.ofMillis(150)) .verifyComplete(); }
@Test public void whenMonoCallable() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.fromCallable(() -> 1), Mono.fromCallable(() -> 2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }