/** * Creates a Reactor {@link Context} that contains the {@code Mono<SecurityContext>} * that can be merged into another {@link Context} * @param securityContext the {@code Mono<SecurityContext>} to set in the returned * Reactor {@link Context} * @return a Reactor {@link Context} that contains the {@code Mono<SecurityContext>} */ public static Context withSecurityContext(Mono<? extends SecurityContext> securityContext) { return Context.of(SECURITY_CONTEXT_KEY, securityContext); }
/** * Creates a Reactor {@link Context} that contains the {@code Mono<TraceContextProvider>}. that can be merged into another * {@link Context}. * * @param supplier the {@link TraceContextProvider} to set in the returned Reactor {@link Context}. * @return a Reactor {@link Context} that contains the {@code Mono<TraceContextProvider>}. */ static Context withTraceContextProvider(TraceContextProvider supplier) { return Context.of(TraceContextProvider.class, supplier); }
private Context serverWebExchange() { return Context.of(ServerWebExchange.class, this.serverWebExchange); }
@Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { return chain.filter(exchange) .subscriberContext(Context.of(ServerWebExchange.class, exchange)); } }
@Test public void onOperatorErrorLocal() { BiFunction<Throwable, Object, Throwable> localHook = (e, v) -> new IllegalStateException("boom_" + v, e); Context c = Context.of(Hooks.KEY_ON_OPERATOR_ERROR, localHook); IllegalArgumentException failure = new IllegalArgumentException("foo"); final Throwable throwable = Operators.onOperatorError(null, failure, "foo", c); assertThat(throwable).isInstanceOf(IllegalStateException.class) .hasMessage("boom_foo") .hasCause(failure); }
@Test public void of2() throws Exception { Context c = Context.of(1, 100, 2, 200); assertThat(c).isInstanceOf(Context2.class); assertThat(c.isEmpty()).as("isEmpty").isFalse(); assertThat(c.stream()).hasSize(2); }
@Test public void ofApi() { assertThat(Context.of("test", 12)) .isInstanceOf(Context1.class) .hasToString("Context1{test=12}"); }
@Test public void discardTryOnNextPredicateFail() { List<Object> discarded = new ArrayList<>(); CoreSubscriber<Integer> actual = new AssertSubscriber<>( Context.of(Hooks.KEY_ON_DISCARD, (Consumer<?>) discarded::add)); FilterSubscriber<Integer> subscriber = new FilterSubscriber<>(actual, i -> { throw new IllegalStateException("boom"); }); subscriber.onSubscribe(Operators.emptySubscription()); subscriber.tryOnNext(1); assertThat(discarded).containsExactly(1); }
@Test public void notContainsAllOfMap() throws Exception { Map<String, String> expected = new HashMap<>(); expected.put("foo", "bar"); expected.put("other", "stuff"); assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.containsAllOf(expected)) .withMessage("Expected Context Context2{foo=bar, foobar=baz} to contain all " + "of {other=stuff, foo=bar}"); }
@Test public void putAllOf() { Context m = Context.of("A", 1, "B", 2, "C", 3); Context put = c.putAll(m); assertThat(put).isInstanceOf(ContextN.class); assertThat(put.stream().map(Map.Entry::getKey)) .containsExactlyInAnyOrder(1, 2, 3, 4, "A", "B", "C"); }
@Test public void notHasSize() throws Exception { assertContextExpectationFails(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")) .subscriberContext(Context.of("fails", true)), e -> e.hasSize(2)) .withMessageStartingWith("Expected Context of size 2, got 3 for Context Context3{"); }
@Test public void putAllOf() { Context m = Context.of("A", 1, "B", 2, "C", 3); Context put = c.putAll(m); assertThat(put).isInstanceOf(Context5.class) .hasToString("Context5{1=A, 2=B, A=1, B=2, C=3}"); }
@Test public void equalsIgnoresContext() { Signal<String> next1 = Signal.next("foo"); Signal<String> next2 = Signal.next("foo", Context.of("bar", "baz")); assertThat(next1.getContext().isEmpty()).as("next1 context empty").isTrue(); assertThat(next2.getContext().isEmpty()).as("next2 context not empty").isFalse(); assertThat(next1).isEqualTo(next2); } }
@Test public void monoSubscriberContextPutsAll() { StepVerifier.create( Mono.just("foo") .flatMap(v -> Mono.subscriberContext()) .subscriberContext(Context.of("foo", "bar", 1, "baz")) .subscriberContext(Context.of("initial", "value")) ) .expectNextMatches(c -> c.hasKey("foo") && c.hasKey(1) && c.hasKey("initial")) .verifyComplete(); }
@Test public void completeWithContextCreatesNewInstances() { Context context = Context.of("foo", "bar"); assertThat(Signal.complete(context)) .isNotSameAs(Signal.complete(context)) .isNotSameAs(Signal.complete()) .isEqualTo(Signal.complete()) .isEqualTo(Signal.complete(context)); }
@Test public void hasSize() throws Exception { assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.hasSize(2)); }
@Test public void contains() throws Exception { assertContextExpectation(s -> s.subscriberContext(Context.of("foo", "bar", "foobar", "baz")), e -> e.contains("foo", "bar")); }
@Test public void fluxSubscriberContextPutsAll() { StepVerifier.create( Flux.just("foo") .flatMap(v -> Mono.subscriberContext()) .subscriberContext(Context.of("foo", "bar", 1, "baz")) .subscriberContext(Context.of("initial", "value")) ) .expectNextMatches(c -> c.hasKey("foo") && c.hasKey(1) && c.hasKey("initial")) .verifyComplete(); }
@Test public void expectAccessibleContextWithInitialContext() { StepVerifierOptions stepVerifierOptions = StepVerifierOptions.create() .withInitialContext(Context.of("foo", "bar")); StepVerifier.create(Mono.just(1), stepVerifierOptions) .expectAccessibleContext() .contains("foo", "bar") .then() .expectNext(1) .verifyComplete(); }
@Test public void contextNotAccessibleWithEmptySubscriptionOnly() { StepVerifier.create(Flux.empty(), StepVerifierOptions.create().withInitialContext(Context.of("a", "b"))) .expectNoAccessibleContext() .verifyComplete(); } }