Search in sources :

Example 11 with OperationInvoker

use of org.springframework.boot.actuate.endpoint.invoke.OperationInvoker 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 12 with OperationInvoker

use of org.springframework.boot.actuate.endpoint.invoke.OperationInvoker 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 13 with OperationInvoker

use of org.springframework.boot.actuate.endpoint.invoke.OperationInvoker 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 14 with OperationInvoker

use of org.springframework.boot.actuate.endpoint.invoke.OperationInvoker in project spring-boot by spring-projects.

the class DiscoveredOperationsFactory method createOperation.

private O createOperation(EndpointId endpointId, Object target, Method method, OperationType operationType, Class<? extends Annotation> annotationType) {
    MergedAnnotation<?> annotation = MergedAnnotations.from(method).get(annotationType);
    if (!annotation.isPresent()) {
        return null;
    }
    DiscoveredOperationMethod operationMethod = new DiscoveredOperationMethod(method, operationType, annotation.asAnnotationAttributes());
    OperationInvoker invoker = new ReflectiveOperationInvoker(target, operationMethod, this.parameterValueMapper);
    invoker = applyAdvisors(endpointId, operationMethod, invoker);
    return createOperation(endpointId, operationMethod, invoker);
}
Also used : ReflectiveOperationInvoker(org.springframework.boot.actuate.endpoint.invoke.reflect.ReflectiveOperationInvoker) OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) ReflectiveOperationInvoker(org.springframework.boot.actuate.endpoint.invoke.reflect.ReflectiveOperationInvoker)

Aggregations

OperationInvoker (org.springframework.boot.actuate.endpoint.invoke.OperationInvoker)14 Test (org.junit.jupiter.api.Test)11 InvocationContext (org.springframework.boot.actuate.endpoint.InvocationContext)7 SecurityContext (org.springframework.boot.actuate.endpoint.SecurityContext)7 OperationParameters (org.springframework.boot.actuate.endpoint.invoke.OperationParameters)5 HashMap (java.util.HashMap)4 Principal (java.security.Principal)2 ReflectiveOperationInvoker (org.springframework.boot.actuate.endpoint.invoke.reflect.ReflectiveOperationInvoker)1