Search in sources :

Example 71 with Context

use of reactor.util.context.Context in project spring-security by spring-projects.

the class CurrentSecurityContextArgumentResolverTests method metaAnnotationWhenDefaultSecurityContextThenInjectSecurityContext.

@Test
public void metaAnnotationWhenDefaultSecurityContextThenInjectSecurityContext() {
    MethodParameter parameter = ResolvableMethod.on(getClass()).named("currentCustomSecurityContext").build().arg(Mono.class, SecurityContext.class);
    Authentication auth = buildAuthenticationWithPrincipal("current_custom_security_context");
    Context context = ReactiveSecurityContextHolder.withAuthentication(auth);
    Mono<Object> argument = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange);
    SecurityContext securityContext = (SecurityContext) argument.subscriberContext(context).cast(Mono.class).block().block();
    assertThat(securityContext.getAuthentication()).isSameAs(auth);
    ReactiveSecurityContextHolder.clearContext();
}
Also used : BindingContext(org.springframework.web.reactive.BindingContext) CurrentSecurityContext(org.springframework.security.core.annotation.CurrentSecurityContext) Context(reactor.util.context.Context) SecurityContext(org.springframework.security.core.context.SecurityContext) Authentication(org.springframework.security.core.Authentication) Mono(reactor.core.publisher.Mono) CurrentSecurityContext(org.springframework.security.core.annotation.CurrentSecurityContext) SecurityContext(org.springframework.security.core.context.SecurityContext) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 72 with Context

use of reactor.util.context.Context in project spring-security by spring-projects.

the class CurrentSecurityContextArgumentResolverTests method resolveArgumentWithAuthenticationOptional1.

@Test
public void resolveArgumentWithAuthenticationOptional1() {
    MethodParameter parameter = ResolvableMethod.on(getClass()).named("securityContextWithDepthPropOptional").build().arg(Mono.class, Object.class);
    Authentication auth = buildAuthenticationWithPrincipal("auth_optional");
    Context context = ReactiveSecurityContextHolder.withAuthentication(auth);
    Mono<Object> argument = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange);
    Mono<Object> obj = (Mono<Object>) argument.subscriberContext(context).block();
    assertThat(obj.block()).isEqualTo("auth_optional");
    ReactiveSecurityContextHolder.clearContext();
}
Also used : BindingContext(org.springframework.web.reactive.BindingContext) CurrentSecurityContext(org.springframework.security.core.annotation.CurrentSecurityContext) Context(reactor.util.context.Context) SecurityContext(org.springframework.security.core.context.SecurityContext) Authentication(org.springframework.security.core.Authentication) Mono(reactor.core.publisher.Mono) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 73 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class OperatorsTest method onRejectedExecutionWithoutDataSignalDelegatesToErrorLocal.

@Test
public void onRejectedExecutionWithoutDataSignalDelegatesToErrorLocal() {
    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.onRejectedExecution(failure, null, null, null, c);
    assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("boom_null").hasNoSuppressedExceptions();
    assertThat(throwable.getCause()).isInstanceOf(RejectedExecutionException.class).hasMessage("Scheduler unavailable").hasCause(failure);
}
Also used : Arrays(java.util.Arrays) Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) RaceTestUtils(reactor.test.util.RaceTestUtils) Nullable(javax.annotation.Nullable) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) Iterator(java.util.Iterator) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Stream(java.util.stream.Stream) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) Context(reactor.util.context.Context) Test(org.junit.jupiter.api.Test)

Example 74 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class OperatorsTest method discardQueueWithClearStopsOnQueuePollingError.

@Test
public void discardQueueWithClearStopsOnQueuePollingError() {
    AtomicInteger discardedCount = new AtomicInteger();
    @SuppressWarnings("unchecked") Queue<Integer> q = Mockito.mock(Queue.class);
    Mockito.when(q.poll()).thenReturn(1, 2).thenThrow(new IllegalStateException("poll boom")).thenReturn(4, 5).thenReturn(null);
    Context hookContext = Operators.discardLocalAdapter(Integer.class, i -> discardedCount.incrementAndGet()).apply(Context.empty());
    Operators.onDiscardQueueWithClear(q, hookContext, null);
    assertThat(discardedCount).as("discarding stops").hasValue(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Context(reactor.util.context.Context) Arrays(java.util.Arrays) Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) RaceTestUtils(reactor.test.util.RaceTestUtils) Nullable(javax.annotation.Nullable) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) Iterator(java.util.Iterator) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Stream(java.util.stream.Stream) LoggerUtils(reactor.test.util.LoggerUtils) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Queue(java.util.Queue) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 75 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class OperatorsTest method onErrorDroppedLocal.

@Test
public void onErrorDroppedLocal() {
    AtomicReference<Throwable> hookState = new AtomicReference<>();
    Consumer<Throwable> localHook = hookState::set;
    Context c = Context.of(Hooks.KEY_ON_ERROR_DROPPED, localHook);
    Operators.onErrorDropped(new IllegalArgumentException("boom"), c);
    assertThat(hookState.get()).isInstanceOf(IllegalArgumentException.class).hasMessage("boom");
}
Also used : Context(reactor.util.context.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

Context (reactor.util.context.Context)101 Test (org.junit.jupiter.api.Test)83 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)31 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 AssertSubscriber (reactor.test.subscriber.AssertSubscriber)28 ArrayList (java.util.ArrayList)27 List (java.util.List)27 Subscription (org.reactivestreams.Subscription)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)26 Collections (java.util.Collections)25 Mono (reactor.core.publisher.Mono)25 Consumer (java.util.function.Consumer)24 Function (java.util.function.Function)24 CoreSubscriber (reactor.core.CoreSubscriber)24 Authentication (org.springframework.security.core.Authentication)23 SecurityContext (org.springframework.security.core.context.SecurityContext)23 Scannable (reactor.core.Scannable)23 Exceptions (reactor.core.Exceptions)22 TestLogger (reactor.test.util.TestLogger)21