Search in sources :

Example 1 with OperationInvoker

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

the class CachingOperationInvokerTests method targetInvokedWithDifferentApiVersion.

@Test
void targetInvokedWithDifferentApiVersion() {
    OperationInvoker target = mock(OperationInvoker.class);
    Object expectedV2 = new Object();
    Object expectedV3 = new Object();
    InvocationContext contextV2 = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(), new ApiVersionArgumentResolver(ApiVersion.V2));
    InvocationContext contextV3 = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(), new ApiVersionArgumentResolver(ApiVersion.V3));
    given(target.invoke(contextV2)).willReturn(expectedV2);
    given(target.invoke(contextV3)).willReturn(expectedV3);
    CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
    Object responseV2 = invoker.invoke(contextV2);
    assertThat(responseV2).isSameAs(expectedV2);
    then(target).should().invoke(contextV2);
    Object responseV3 = invoker.invoke(contextV3);
    assertThat(responseV3).isNotSameAs(responseV2);
    then(target).should().invoke(contextV3);
}
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 2 with OperationInvoker

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

the class CachingOperationInvokerTests method assertCacheIsUsed.

private void assertCacheIsUsed(Map<String, Object> parameters, Principal principal) {
    OperationInvoker target = mock(OperationInvoker.class);
    Object expected = new Object();
    SecurityContext securityContext = mock(SecurityContext.class);
    if (principal != null) {
        given(securityContext.getPrincipal()).willReturn(principal);
    }
    InvocationContext context = new InvocationContext(securityContext, parameters);
    given(target.invoke(context)).willReturn(expected);
    CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
    Object response = invoker.invoke(context);
    assertThat(response).isSameAs(expected);
    then(target).should().invoke(context);
    Object cachedResponse = invoker.invoke(context);
    assertThat(cachedResponse).isSameAs(response);
    then(target).shouldHaveNoMoreInteractions();
}
Also used : OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) SecurityContext(org.springframework.boot.actuate.endpoint.SecurityContext) InvocationContext(org.springframework.boot.actuate.endpoint.InvocationContext)

Example 3 with OperationInvoker

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

the class CachingOperationInvokerTests method targetAlwaysInvokedWithDifferentPrincipals.

@Test
void targetAlwaysInvokedWithDifferentPrincipals() {
    OperationInvoker target = mock(OperationInvoker.class);
    Map<String, Object> parameters = new HashMap<>();
    SecurityContext securityContext = mock(SecurityContext.class);
    given(securityContext.getPrincipal()).willReturn(mock(Principal.class), mock(Principal.class), mock(Principal.class));
    InvocationContext context = new InvocationContext(securityContext, parameters);
    Object result1 = new Object();
    Object result2 = new Object();
    Object result3 = new Object();
    given(target.invoke(context)).willReturn(result1, result2, result3);
    CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
    assertThat(invoker.invoke(context)).isEqualTo(result1);
    assertThat(invoker.invoke(context)).isEqualTo(result2);
    assertThat(invoker.invoke(context)).isEqualTo(result3);
    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) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test)

Example 4 with OperationInvoker

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

the class CachingOperationInvokerAdvisorTests method applyWhenTimeToLiveIsZeroShouldNotAddAdvise.

@Test
void applyWhenTimeToLiveIsZeroShouldNotAddAdvise() {
    OperationParameters parameters = getParameters("get");
    given(this.timeToLive.apply(any())).willReturn(0L);
    OperationInvoker advised = this.advisor.apply(EndpointId.of("foo"), OperationType.READ, parameters, this.invoker);
    assertThat(advised).isSameAs(this.invoker);
    then(this.timeToLive).should().apply(EndpointId.of("foo"));
}
Also used : OperationParameters(org.springframework.boot.actuate.endpoint.invoke.OperationParameters) OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) Test(org.junit.jupiter.api.Test)

Example 5 with OperationInvoker

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

the class CachingOperationInvokerAdvisorTests method applyWhenTimeToLiveReturnsNullShouldNotAddAdvise.

@Test
void applyWhenTimeToLiveReturnsNullShouldNotAddAdvise() {
    OperationParameters parameters = getParameters("get");
    given(this.timeToLive.apply(any())).willReturn(null);
    OperationInvoker advised = this.advisor.apply(EndpointId.of("foo"), OperationType.READ, parameters, this.invoker);
    assertThat(advised).isSameAs(this.invoker);
    then(this.timeToLive).should().apply(EndpointId.of("foo"));
}
Also used : OperationParameters(org.springframework.boot.actuate.endpoint.invoke.OperationParameters) OperationInvoker(org.springframework.boot.actuate.endpoint.invoke.OperationInvoker) Test(org.junit.jupiter.api.Test)

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