@ -17,20 +17,19 @@
package org.springframework.boot.actuate.endpoint.invoker.cache ;
import java.security.Principal ;
import java. time.Duration ;
import java. util.Arrays ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.Map ;
import org.junit.Rule ;
import org.junit.Test ;
import reactor.core.publisher.Flux ;
import reactor.core.publisher.Mono ;
import org.springframework.boot.actuate.endpoint.InvocationContext ;
import org.springframework.boot.actuate.endpoint.SecurityContext ;
import org.springframework.boot.actuate.endpoint.invoke.MissingParametersException ;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker ;
import org.springframework.boot.test.rule.OutputCapture ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException ;
@ -44,12 +43,11 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* Tests for { @link CachingOperationInvoker } .
*
* @author Stephane Nicoll
* @author Christoph Dreis
* @author Phillip Webb
* /
public class CachingOperationInvokerTests {
@Rule
public OutputCapture outputCapture = new OutputCapture ( ) ;
@Test
public void createInstanceWithTtlSetToZero ( ) {
assertThatIllegalArgumentException ( )
@ -72,17 +70,26 @@ public class CachingOperationInvokerTests {
@Test
public void cacheInTtlWithMonoResponse ( ) {
MonoOperationInvoker . invocations = 0 ;
MonoOperationInvoker target = new MonoOperationInvoker ( ) ;
InvocationContext context = new InvocationContext ( mock ( SecurityContext . class ) , Collections . emptyMap ( ) ) ;
CachingOperationInvoker invoker = new CachingOperationInvoker ( target , 500L ) ;
Object monoResponse = invoker . invoke ( context ) ;
assertThat ( monoResponse ) . isInstanceOf ( Mono . class ) ;
Object response = ( ( Mono ) monoResponse ) . block ( Duration . ofSeconds ( 30 ) ) ;
Object cachedMonoResponse = invoker . invoke ( context ) ;
assertThat ( cachedMonoResponse ) . isInstanceOf ( Mono . class ) ;
Object cachedResponse = ( ( Mono ) cachedMonoResponse ) . block ( Duration . ofSeconds ( 30 ) ) ;
Object response = ( ( Mono < ? > ) invoker . invoke ( context ) ) . block ( ) ;
Object cachedResponse = ( ( Mono < ? > ) invoker . invoke ( context ) ) . block ( ) ;
assertThat ( MonoOperationInvoker . invocations ) . isEqualTo ( 1 ) ;
assertThat ( response ) . isSameAs ( cachedResponse ) ;
}
@Test
public void cacheInTtlWithFluxResponse ( ) {
FluxOperationInvoker . invocations = 0 ;
FluxOperationInvoker target = new FluxOperationInvoker ( ) ;
InvocationContext context = new InvocationContext ( mock ( SecurityContext . class ) , Collections . emptyMap ( ) ) ;
CachingOperationInvoker invoker = new CachingOperationInvoker ( target , 500L ) ;
Object response = ( ( Flux < ? > ) invoker . invoke ( context ) ) . blockLast ( ) ;
Object cachedResponse = ( ( Flux < ? > ) invoker . invoke ( context ) ) . blockLast ( ) ;
assertThat ( FluxOperationInvoker . invocations ) . isEqualTo ( 1 ) ;
assertThat ( response ) . isSameAs ( cachedResponse ) ;
assertThat ( this . outputCapture . toString ( ) ) . containsOnlyOnce ( "invoked" ) ;
}
private void assertCacheIsUsed ( Map < String , Object > parameters ) {
@ -144,14 +151,28 @@ public class CachingOperationInvokerTests {
private static class MonoOperationInvoker implements OperationInvoker {
static int invocations ;
@Override
public Object invoke ( InvocationContext context ) throws MissingParametersException {
return Mono . fromCallable ( this : : printInvocation ) ;
return Mono . fromCallable ( ( ) - > {
invocations + + ;
return Mono . just ( "test" ) ;
} ) ;
}
private Mono < String > printInvocation ( ) {
System . out . println ( "MonoOperationInvoker invoked" ) ;
return Mono . just ( "test" ) ;
}
private static class FluxOperationInvoker implements OperationInvoker {
static int invocations ;
@Override
public Object invoke ( InvocationContext context ) throws MissingParametersException {
return Flux . fromIterable ( ( ) - > {
invocations + + ;
return Arrays . asList ( "spring" , "boot" ) . iterator ( ) ;
} ) ;
}
}