Handle WebServerNamespace in CachingOperationInvoker

Fixes gh-28882
This commit is contained in:
Madhura Bhave
2021-12-16 16:43:58 -08:00
parent d9d161cd6b
commit 4cc8012bfa
5 changed files with 105 additions and 20 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
import org.springframework.boot.actuate.endpoint.invoke.OperationParameters;
import org.springframework.boot.actuate.endpoint.invoke.reflect.OperationMethod;
import org.springframework.boot.actuate.endpoint.web.WebServerNamespace;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;
@@ -126,6 +127,23 @@ class CachingOperationInvokerAdvisorTests {
assertAdviseIsApplied(parameters);
}
@Test
void applyWithWebServerNamespaceShouldAddAdvise() {
OperationParameters parameters = getParameters("getWithServerNamespace", WebServerNamespace.class,
String.class);
given(this.timeToLive.apply(any())).willReturn(100L);
assertAdviseIsApplied(parameters);
}
@Test
void applyWithMandatoryCachedAndNonCachedShouldAddAdvise() {
OperationParameters parameters = getParameters("getWithServerNamespaceAndOtherMandatory",
WebServerNamespace.class, String.class);
OperationInvoker advised = this.advisor.apply(EndpointId.of("foo"), OperationType.READ, parameters,
this.invoker);
assertThat(advised).isSameAs(this.invoker);
}
private void assertAdviseIsApplied(OperationParameters parameters) {
OperationInvoker advised = this.advisor.apply(EndpointId.of("foo"), OperationType.READ, parameters,
this.invoker);
@@ -165,6 +183,14 @@ class CachingOperationInvokerAdvisorTests {
return "";
}
String getWithServerNamespace(WebServerNamespace serverNamespace, @Nullable String bar) {
return "";
}
String getWithServerNamespaceAndOtherMandatory(WebServerNamespace serverNamespace, String bar) {
return "";
}
}
}

View File

@@ -33,6 +33,7 @@ import org.springframework.boot.actuate.endpoint.InvocationContext;
import org.springframework.boot.actuate.endpoint.OperationArgumentResolver;
import org.springframework.boot.actuate.endpoint.SecurityContext;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
import org.springframework.boot.actuate.endpoint.web.WebServerNamespace;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -241,6 +242,26 @@ class CachingOperationInvokerTests {
verify(target, times(1)).invoke(contextV3);
}
@Test
public void targetInvokedWithDifferentWebServerNamespace() {
OperationInvoker target = mock(OperationInvoker.class);
Object expectedV2 = new Object();
Object expectedV3 = new Object();
InvocationContext contextV2 = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(),
new WebServerNamespaceArgumentResolver(WebServerNamespace.SERVER));
InvocationContext contextV3 = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap(),
new WebServerNamespaceArgumentResolver(WebServerNamespace.MANAGEMENT));
given(target.invoke(contextV2)).willReturn(expectedV2);
given(target.invoke(contextV3)).willReturn(expectedV3);
CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
Object response = invoker.invoke(contextV2);
assertThat(response).isSameAs(expectedV2);
verify(target, times(1)).invoke(contextV2);
Object cachedResponse = invoker.invoke(contextV3);
assertThat(cachedResponse).isNotSameAs(response);
verify(target, times(1)).invoke(contextV3);
}
private static class MonoOperationInvoker implements OperationInvoker {
static AtomicInteger invocations = new AtomicInteger();
@@ -287,4 +308,25 @@ class CachingOperationInvokerTests {
}
private static final class WebServerNamespaceArgumentResolver implements OperationArgumentResolver {
private final WebServerNamespace webServerNamespace;
private WebServerNamespaceArgumentResolver(WebServerNamespace webServerNamespace) {
this.webServerNamespace = webServerNamespace;
}
@SuppressWarnings("unchecked")
@Override
public <T> T resolve(Class<T> type) {
return (T) this.webServerNamespace;
}
@Override
public boolean canResolve(Class<?> type) {
return WebServerNamespace.class.equals(type);
}
}
}