From bf4965025158706bf5b8c59d518c837e7bf679d7 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 18 Sep 2017 10:18:22 -0500 Subject: [PATCH] TestMono->PublisherProbe --- .../ExceptionTranslationWebFilterTests.java | 46 ++++++------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/webflux/src/test/java/org/springframework/security/web/server/authorization/ExceptionTranslationWebFilterTests.java b/webflux/src/test/java/org/springframework/security/web/server/authorization/ExceptionTranslationWebFilterTests.java index 3f8cc3baf2..6726635cd4 100644 --- a/webflux/src/test/java/org/springframework/security/web/server/authorization/ExceptionTranslationWebFilterTests.java +++ b/webflux/src/test/java/org/springframework/security/web/server/authorization/ExceptionTranslationWebFilterTests.java @@ -19,7 +19,6 @@ package org.springframework.security.web.server.authorization; import java.security.Principal; -import java.util.concurrent.atomic.AtomicBoolean; import org.junit.Before; import org.junit.Test; @@ -28,6 +27,7 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; +import reactor.test.publisher.PublisherProbe; import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.MockServerHttpResponse; @@ -57,16 +57,16 @@ public class ExceptionTranslationWebFilterTests { @Mock private AuthenticationEntryPoint entryPoint; - private TestMono deniedMono = TestMono.create(); - private TestMono entryPointMono = TestMono.create(); + private PublisherProbe deniedPublisher = PublisherProbe.empty(); + private PublisherProbe entryPointPublisher = PublisherProbe.empty(); private ExceptionTranslationWebFilter filter = new ExceptionTranslationWebFilter(); @Before public void setup() { when(this.exchange.getResponse()).thenReturn(new MockServerHttpResponse()); - when(this.deniedHandler.handle(any(), any())).thenReturn(this.deniedMono.mono()); - when(this.entryPoint.commence(any(), any())).thenReturn(this.entryPointMono.mono()); + when(this.deniedHandler.handle(any(), any())).thenReturn(this.deniedPublisher.mono()); + when(this.entryPoint.commence(any(), any())).thenReturn(this.entryPointPublisher.mono()); this.filter.setAuthenticationEntryPoint(this.entryPoint); this.filter.setAccessDeniedHandler(this.deniedHandler); @@ -80,8 +80,8 @@ public class ExceptionTranslationWebFilterTests { .expectComplete() .verify(); - assertThat(this.deniedMono.isInvoked()).isFalse(); - assertThat(this.entryPointMono.isInvoked()).isFalse(); + this.deniedPublisher.assertWasNotSubscribed(); + this.entryPointPublisher.assertWasNotSubscribed(); } @Test @@ -92,8 +92,8 @@ public class ExceptionTranslationWebFilterTests { .expectError(IllegalArgumentException.class) .verify(); - assertThat(this.deniedMono.isInvoked()).isFalse(); - assertThat(this.entryPointMono.isInvoked()).isFalse(); + this.deniedPublisher.assertWasNotSubscribed(); + this.entryPointPublisher.assertWasNotSubscribed(); } @Test @@ -102,13 +102,13 @@ public class ExceptionTranslationWebFilterTests { when(this.chain.filter(this.exchange)).thenReturn(Mono.error(new AccessDeniedException("Not Authorized"))); StepVerifier.create(this.filter.filter(this.exchange, this.chain)) - .expectComplete() - .verify(); + .verifyComplete(); - assertThat(this.deniedMono.isInvoked()).isFalse(); - assertThat(this.entryPointMono.isInvoked()).isTrue(); + this.deniedPublisher.assertWasNotSubscribed(); + this.entryPointPublisher.assertWasSubscribed(); } + @Test public void filterWhenDefaultsAndAccessDeniedExceptionAndAuthenticatedThenForbidden() { this.filter = new ExceptionTranslationWebFilter(); @@ -146,8 +146,8 @@ public class ExceptionTranslationWebFilterTests { .expectComplete() .verify(); - assertThat(this.deniedMono.isInvoked()).isTrue(); - assertThat(this.entryPointMono.isInvoked()).isFalse(); + this.deniedPublisher.assertWasSubscribed(); + this.entryPointPublisher.assertWasNotSubscribed(); } @Test(expected = IllegalArgumentException.class) @@ -159,20 +159,4 @@ public class ExceptionTranslationWebFilterTests { public void setAuthenticationEntryPointWhenNullThenException() { this.filter.setAuthenticationEntryPoint(null); } - - static class TestMono { - private final AtomicBoolean invoked = new AtomicBoolean(); - - public Mono mono() { - return Mono.empty().doOnSubscribe(s -> this.invoked.set(true)); - } - - public boolean isInvoked() { - return this.invoked.get(); - } - - public static TestMono create() { - return new TestMono(); - } - } }