Search in sources :

Example 1 with DefaultPayload

use of io.rsocket.util.DefaultPayload in project spring-security by spring-projects.

the class PayloadInterceptorRSocketTests method requestChannelWhenInterceptorCompletesThenAllPayloadsRetained.

// gh-9345
@Test
public void requestChannelWhenInterceptorCompletesThenAllPayloadsRetained() {
    ExecutorService executors = Executors.newSingleThreadExecutor();
    Payload payload = ByteBufPayload.create("data");
    Payload payloadTwo = ByteBufPayload.create("moredata");
    Payload payloadThree = ByteBufPayload.create("stillmoredata");
    Context ctx = Context.empty();
    Flux<Payload> payloads = this.payloadResult.flux();
    given(this.interceptor.intercept(any(), any())).willReturn(Mono.empty()).willReturn(Mono.error(() -> new AccessDeniedException("Access Denied")));
    given(this.delegate.requestChannel(any())).willAnswer((invocation) -> {
        Flux<Payload> input = invocation.getArgument(0);
        return Flux.from(input).switchOnFirst((signal, innerFlux) -> innerFlux.map(Payload::getDataUtf8).transform((data) -> Flux.<String>create((emitter) -> {
            Runnable run = () -> data.subscribe(new CoreSubscriber<String>() {

                @Override
                public void onSubscribe(Subscription s) {
                    s.request(3);
                }

                @Override
                public void onNext(String s) {
                    emitter.next(s);
                }

                @Override
                public void onError(Throwable t) {
                    emitter.error(t);
                }

                @Override
                public void onComplete() {
                    emitter.complete();
                }
            });
            executors.execute(run);
        })).map(DefaultPayload::create));
    });
    PayloadInterceptorRSocket interceptor = new PayloadInterceptorRSocket(this.delegate, Arrays.asList(this.interceptor), this.metadataMimeType, this.dataMimeType, ctx);
    StepVerifier.create(interceptor.requestChannel(payloads).doOnDiscard(Payload.class, Payload::release)).then(() -> this.payloadResult.assertSubscribers()).then(() -> this.payloadResult.emit(payload, payloadTwo, payloadThree)).assertNext((next) -> assertThat(next.getDataUtf8()).isEqualTo(payload.getDataUtf8())).verifyError(AccessDeniedException.class);
    verify(this.interceptor, times(2)).intercept(this.exchange.capture(), any());
    assertThat(this.exchange.getValue().getPayload()).isEqualTo(payloadTwo);
    verify(this.delegate).requestChannel(any());
}
Also used : Context(reactor.util.context.Context) SecurityContext(org.springframework.security.core.context.SecurityContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) PayloadInterceptor(org.springframework.security.rsocket.api.PayloadInterceptor) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Captor(org.mockito.Captor) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) Answer(org.mockito.stubbing.Answer) CoreSubscriber(reactor.core.CoreSubscriber) ArgumentCaptor(org.mockito.ArgumentCaptor) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RSocket(io.rsocket.RSocket) BDDMockito.given(org.mockito.BDDMockito.given) DefaultPayload(io.rsocket.util.DefaultPayload) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) ByteBufPayload(io.rsocket.util.ByteBufPayload) RSocketProxy(io.rsocket.util.RSocketProxy) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) PayloadInterceptorChain(org.springframework.security.rsocket.api.PayloadInterceptorChain) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) PayloadExchangeType(org.springframework.security.rsocket.api.PayloadExchangeType) Mockito.times(org.mockito.Mockito.times) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MimeTypeUtils(org.springframework.util.MimeTypeUtils) Executors(java.util.concurrent.Executors) PublisherProbe(reactor.test.publisher.PublisherProbe) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) PayloadExchange(org.springframework.security.rsocket.api.PayloadExchange) Flux(reactor.core.publisher.Flux) List(java.util.List) Payload(io.rsocket.Payload) SecurityContext(org.springframework.security.core.context.SecurityContext) Subscription(org.reactivestreams.Subscription) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Authentication(org.springframework.security.core.Authentication) Collections(java.util.Collections) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ExecutorService(java.util.concurrent.ExecutorService) CoreSubscriber(reactor.core.CoreSubscriber) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

Payload (io.rsocket.Payload)1 RSocket (io.rsocket.RSocket)1 WellKnownMimeType (io.rsocket.metadata.WellKnownMimeType)1 ByteBufPayload (io.rsocket.util.ByteBufPayload)1 DefaultPayload (io.rsocket.util.DefaultPayload)1 RSocketProxy (io.rsocket.util.RSocketProxy)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 Test (org.junit.jupiter.api.Test)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)1 BDDMockito.given (org.mockito.BDDMockito.given)1