use of org.springframework.boot.actuate.endpoint.InvocationContext in project spring-boot by spring-projects.
the class CachingOperationInvokerTests method targetAlwaysInvokedWithParameters.
@Test
void targetAlwaysInvokedWithParameters() {
OperationInvoker target = mock(OperationInvoker.class);
Map<String, Object> parameters = new HashMap<>();
parameters.put("test", "value");
parameters.put("something", null);
InvocationContext context = new InvocationContext(mock(SecurityContext.class), parameters);
given(target.invoke(context)).willReturn(new Object());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
invoker.invoke(context);
invoker.invoke(context);
invoker.invoke(context);
then(target).should(times(3)).invoke(context);
}
use of org.springframework.boot.actuate.endpoint.InvocationContext in project spring-boot by spring-projects.
the class CachingOperationInvokerTests method targetInvokedWithDifferentWebServerNamespace.
@Test
void targetInvokedWithDifferentWebServerNamespace() {
OperationInvoker target = mock(OperationInvoker.class);
Object expectedServer = new Object();
Object expectedManagement = new Object();
InvocationContext contextServer = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(), new WebServerNamespaceArgumentResolver(WebServerNamespace.SERVER));
InvocationContext contextManagement = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(), new WebServerNamespaceArgumentResolver(WebServerNamespace.MANAGEMENT));
given(target.invoke(contextServer)).willReturn(expectedServer);
given(target.invoke(contextManagement)).willReturn(expectedManagement);
CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
Object responseServer = invoker.invoke(contextServer);
assertThat(responseServer).isSameAs(expectedServer);
then(target).should(times(1)).invoke(contextServer);
Object responseManagement = invoker.invoke(contextManagement);
assertThat(responseManagement).isNotSameAs(responseServer);
then(target).should(times(1)).invoke(contextManagement);
}
use of org.springframework.boot.actuate.endpoint.InvocationContext in project spring-boot by spring-projects.
the class CachingOperationInvokerTests method targetInvokedWhenCacheExpires.
@Test
void targetInvokedWhenCacheExpires() throws InterruptedException {
OperationInvoker target = mock(OperationInvoker.class);
Map<String, Object> parameters = new HashMap<>();
InvocationContext context = new InvocationContext(mock(SecurityContext.class), parameters);
given(target.invoke(context)).willReturn(new Object());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 50L);
invoker.invoke(context);
long expired = System.currentTimeMillis() + 50;
while (System.currentTimeMillis() < expired) {
Thread.sleep(10);
}
invoker.invoke(context);
then(target).should(times(2)).invoke(context);
}
use of org.springframework.boot.actuate.endpoint.InvocationContext in project spring-boot by spring-projects.
the class CachingOperationInvokerTests method targetInvokedWhenCalledWithAndWithoutPrincipal.
@Test
void targetInvokedWhenCalledWithAndWithoutPrincipal() {
OperationInvoker target = mock(OperationInvoker.class);
Map<String, Object> parameters = new HashMap<>();
SecurityContext anonymous = mock(SecurityContext.class);
SecurityContext authenticated = mock(SecurityContext.class);
given(authenticated.getPrincipal()).willReturn(mock(Principal.class));
InvocationContext anonymousContext = new InvocationContext(anonymous, parameters);
Object anonymousResult = new Object();
given(target.invoke(anonymousContext)).willReturn(anonymousResult);
InvocationContext authenticatedContext = new InvocationContext(authenticated, parameters);
Object authenticatedResult = new Object();
given(target.invoke(authenticatedContext)).willReturn(authenticatedResult);
CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
assertThat(invoker.invoke(anonymousContext)).isEqualTo(anonymousResult);
assertThat(invoker.invoke(authenticatedContext)).isEqualTo(authenticatedResult);
assertThat(invoker.invoke(anonymousContext)).isEqualTo(anonymousResult);
assertThat(invoker.invoke(authenticatedContext)).isEqualTo(authenticatedResult);
then(target).should().invoke(anonymousContext);
then(target).should().invoke(authenticatedContext);
}
use of org.springframework.boot.actuate.endpoint.InvocationContext in project spring-boot by spring-projects.
the class CachingOperationInvokerTests method cacheInTtlWithFluxResponse.
@Test
void cacheInTtlWithFluxResponse() {
FluxOperationInvoker.invocations = new AtomicInteger();
FluxOperationInvoker target = new FluxOperationInvoker();
InvocationContext context = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
Object response = ((Flux<?>) invoker.invoke(context)).blockLast();
Object cachedResponse = ((Flux<?>) invoker.invoke(context)).blockLast();
assertThat(FluxOperationInvoker.invocations).hasValue(1);
assertThat(response).isSameAs(cachedResponse);
}
Aggregations