Allow caching for an Endpoint operation with optional arguments
This commit makes sure that caching is enabled if an operation has nullable parameters and the actual invocation provides null values. Closes gh-11795
This commit is contained in:
@@ -28,6 +28,7 @@ import org.springframework.boot.actuate.endpoint.OperationType;
|
||||
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.lang.Nullable;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
@@ -40,6 +41,7 @@ import static org.mockito.Mockito.verify;
|
||||
* Tests for {@link CachingOperationInvokerAdvisor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CachingOperationInvokerAdvisorTests {
|
||||
|
||||
@@ -66,8 +68,9 @@ public class CachingOperationInvokerAdvisorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyWhenHasParametersShouldNotAddAdvise() {
|
||||
OperationParameters parameters = getParameters("getWithParameter", String.class);
|
||||
public void applyWhenHasAtLeaseOneMandatoryParameterShouldNotAddAdvise() {
|
||||
OperationParameters parameters = getParameters("getWithParameter", String.class,
|
||||
String.class);
|
||||
OperationInvoker advised = this.advisor.apply("foo", OperationType.READ,
|
||||
parameters, this.invoker);
|
||||
assertThat(advised).isSameAs(this.invoker);
|
||||
@@ -97,6 +100,18 @@ public class CachingOperationInvokerAdvisorTests {
|
||||
public void applyShouldAddCacheAdvise() {
|
||||
OperationParameters parameters = getParameters("get");
|
||||
given(this.timeToLive.apply(any())).willReturn(100L);
|
||||
assertAdviseIsApplied(parameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyWithAllOptionalParameterShouldAddAdvise() {
|
||||
OperationParameters parameters = getParameters("getWithAllOptionalParameters",
|
||||
String.class, String.class);
|
||||
given(this.timeToLive.apply(any())).willReturn(100L);
|
||||
assertAdviseIsApplied(parameters);
|
||||
}
|
||||
|
||||
private void assertAdviseIsApplied(OperationParameters parameters) {
|
||||
OperationInvoker advised = this.advisor.apply("foo", OperationType.READ,
|
||||
parameters, this.invoker);
|
||||
assertThat(advised).isInstanceOf(CachingOperationInvoker.class);
|
||||
@@ -123,7 +138,12 @@ public class CachingOperationInvokerAdvisorTests {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getWithParameter(String foo) {
|
||||
public String getWithParameter(@Nullable String foo, String bar) {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getWithAllOptionalParameters(@Nullable String foo,
|
||||
@Nullable String bar) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.actuate.endpoint.invoker.cache;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -50,10 +51,21 @@ public class CachingOperationInvokerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheInTtlRange() {
|
||||
Object expected = new Object();
|
||||
OperationInvoker target = mock(OperationInvoker.class);
|
||||
public void cacheInTtlRangeWithNoParameter() {
|
||||
assertCacheIsUsed(Collections.emptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheInTtlWithNullParameters() {
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
parameters.put("first", null);
|
||||
parameters.put("second", null);
|
||||
assertCacheIsUsed(parameters);
|
||||
}
|
||||
|
||||
private void assertCacheIsUsed(Map<String, Object> parameters) {
|
||||
OperationInvoker target = mock(OperationInvoker.class);
|
||||
Object expected = new Object();
|
||||
given(target.invoke(parameters)).willReturn(expected);
|
||||
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L);
|
||||
Object response = invoker.invoke(parameters);
|
||||
@@ -64,6 +76,20 @@ public class CachingOperationInvokerTests {
|
||||
verifyNoMoreInteractions(target);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void targetAlwaysInvokedWithArguments() {
|
||||
OperationInvoker target = mock(OperationInvoker.class);
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
parameters.put("test", "value");
|
||||
parameters.put("something", null);
|
||||
given(target.invoke(parameters)).willReturn(new Object());
|
||||
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L);
|
||||
invoker.invoke(parameters);
|
||||
invoker.invoke(parameters);
|
||||
invoker.invoke(parameters);
|
||||
verify(target, times(3)).invoke(parameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void targetInvokedWhenCacheExpires() throws InterruptedException {
|
||||
OperationInvoker target = mock(OperationInvoker.class);
|
||||
|
||||
Reference in New Issue
Block a user