Search in sources :

Example 11 with InvocationContext

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);
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) HashMap(java.util.HashMap) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Test(org.junit.jupiter.api.Test)

Example 12 with InvocationContext

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);
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Test(org.junit.jupiter.api.Test)

Example 13 with InvocationContext

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);
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) HashMap(java.util.HashMap) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Test(org.junit.jupiter.api.Test)

Example 14 with InvocationContext

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);
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) HashMap(java.util.HashMap) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test)

Example 15 with InvocationContext

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) Flux(reactor.core.publisher.Flux) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext) Test(org.junit.jupiter.api.Test)

Aggregations

InvocationContext (org.springframework.boot.actuate.endpoint.InvocationContext)17 Test (org.junit.jupiter.api.Test)14 SecurityContext (org.springframework.boot.actuate.endpoint.SecurityContext)11 OperationInvoker (org.springframework.boot.actuate.endpoint.invoke.OperationInvoker)7 HashMap (java.util.HashMap)4 Principal (java.security.Principal)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Mono (reactor.core.publisher.Mono)2 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)1 MBeanException (javax.management.MBeanException)1 ReflectionException (javax.management.ReflectionException)1 ApiVersion (org.springframework.boot.actuate.endpoint.ApiVersion)1 InvalidEndpointRequestException (org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException)1 ExposableWebEndpoint (org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint)1 WebOperation (org.springframework.boot.actuate.endpoint.web.WebOperation)1 Flux (reactor.core.publisher.Flux)1