TestMono->PublisherProbe

This commit is contained in:
Rob Winch 2017-09-18 10:18:22 -05:00
parent 404a8e793e
commit bf49650251

View File

@ -19,7 +19,6 @@
package org.springframework.security.web.server.authorization; package org.springframework.security.web.server.authorization;
import java.security.Principal; import java.security.Principal;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -28,6 +27,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
import reactor.test.publisher.PublisherProbe;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.MockServerHttpResponse; import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
@ -57,16 +57,16 @@ public class ExceptionTranslationWebFilterTests {
@Mock @Mock
private AuthenticationEntryPoint entryPoint; private AuthenticationEntryPoint entryPoint;
private TestMono<Void> deniedMono = TestMono.create(); private PublisherProbe<Void> deniedPublisher = PublisherProbe.empty();
private TestMono<Void> entryPointMono = TestMono.create(); private PublisherProbe<Void> entryPointPublisher = PublisherProbe.empty();
private ExceptionTranslationWebFilter filter = new ExceptionTranslationWebFilter(); private ExceptionTranslationWebFilter filter = new ExceptionTranslationWebFilter();
@Before @Before
public void setup() { public void setup() {
when(this.exchange.getResponse()).thenReturn(new MockServerHttpResponse()); when(this.exchange.getResponse()).thenReturn(new MockServerHttpResponse());
when(this.deniedHandler.handle(any(), any())).thenReturn(this.deniedMono.mono()); when(this.deniedHandler.handle(any(), any())).thenReturn(this.deniedPublisher.mono());
when(this.entryPoint.commence(any(), any())).thenReturn(this.entryPointMono.mono()); when(this.entryPoint.commence(any(), any())).thenReturn(this.entryPointPublisher.mono());
this.filter.setAuthenticationEntryPoint(this.entryPoint); this.filter.setAuthenticationEntryPoint(this.entryPoint);
this.filter.setAccessDeniedHandler(this.deniedHandler); this.filter.setAccessDeniedHandler(this.deniedHandler);
@ -80,8 +80,8 @@ public class ExceptionTranslationWebFilterTests {
.expectComplete() .expectComplete()
.verify(); .verify();
assertThat(this.deniedMono.isInvoked()).isFalse(); this.deniedPublisher.assertWasNotSubscribed();
assertThat(this.entryPointMono.isInvoked()).isFalse(); this.entryPointPublisher.assertWasNotSubscribed();
} }
@Test @Test
@ -92,8 +92,8 @@ public class ExceptionTranslationWebFilterTests {
.expectError(IllegalArgumentException.class) .expectError(IllegalArgumentException.class)
.verify(); .verify();
assertThat(this.deniedMono.isInvoked()).isFalse(); this.deniedPublisher.assertWasNotSubscribed();
assertThat(this.entryPointMono.isInvoked()).isFalse(); this.entryPointPublisher.assertWasNotSubscribed();
} }
@Test @Test
@ -102,13 +102,13 @@ public class ExceptionTranslationWebFilterTests {
when(this.chain.filter(this.exchange)).thenReturn(Mono.error(new AccessDeniedException("Not Authorized"))); when(this.chain.filter(this.exchange)).thenReturn(Mono.error(new AccessDeniedException("Not Authorized")));
StepVerifier.create(this.filter.filter(this.exchange, this.chain)) StepVerifier.create(this.filter.filter(this.exchange, this.chain))
.expectComplete() .verifyComplete();
.verify();
assertThat(this.deniedMono.isInvoked()).isFalse(); this.deniedPublisher.assertWasNotSubscribed();
assertThat(this.entryPointMono.isInvoked()).isTrue(); this.entryPointPublisher.assertWasSubscribed();
} }
@Test @Test
public void filterWhenDefaultsAndAccessDeniedExceptionAndAuthenticatedThenForbidden() { public void filterWhenDefaultsAndAccessDeniedExceptionAndAuthenticatedThenForbidden() {
this.filter = new ExceptionTranslationWebFilter(); this.filter = new ExceptionTranslationWebFilter();
@ -146,8 +146,8 @@ public class ExceptionTranslationWebFilterTests {
.expectComplete() .expectComplete()
.verify(); .verify();
assertThat(this.deniedMono.isInvoked()).isTrue(); this.deniedPublisher.assertWasSubscribed();
assertThat(this.entryPointMono.isInvoked()).isFalse(); this.entryPointPublisher.assertWasNotSubscribed();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -159,20 +159,4 @@ public class ExceptionTranslationWebFilterTests {
public void setAuthenticationEntryPointWhenNullThenException() { public void setAuthenticationEntryPointWhenNullThenException() {
this.filter.setAuthenticationEntryPoint(null); this.filter.setAuthenticationEntryPoint(null);
} }
static class TestMono<T> {
private final AtomicBoolean invoked = new AtomicBoolean();
public Mono<T> mono() {
return Mono.<T>empty().doOnSubscribe(s -> this.invoked.set(true));
}
public boolean isInvoked() {
return this.invoked.get();
}
public static <T> TestMono<T> create() {
return new TestMono<T>();
}
}
} }