Commit c72df104 authored by Stephane Nicoll's avatar Stephane Nicoll

Improve caches endpoint format

This commit adds an intermediate "caches" element so that additional
cache manager attributes can be added in the future.

Closes gh-13079
parent d77c4c83
......@@ -68,9 +68,9 @@ public class CachesEndpointDocumentationTests extends MockMvcEndpointDocumentati
.andDo(MockMvcRestDocumentation.document("caches/all", responseFields(
fieldWithPath("cacheManagers")
.description("Cache managers keyed by id."),
fieldWithPath("cacheManagers.*").description(
fieldWithPath("cacheManagers.*.caches").description(
"Caches in the application context keyed by " + "name."))
.andWithPrefix("cacheManagers.*.*.",
.andWithPrefix("cacheManagers.*.caches.*.",
fieldWithPath("target").description(
"Fully qualified name of the native cache."))));
}
......
......@@ -67,7 +67,10 @@ public class CachesEndpoint {
cacheManagerDescriptors.put(cacheName,
new CacheDescriptor(entry.getTarget()));
});
return new CachesReport(descriptors);
Map<String, CacheManagerDescriptor> cacheManagerDescriptors = new LinkedHashMap<>();
descriptors.forEach((name, entries) ->
cacheManagerDescriptors.put(name, new CacheManagerDescriptor(entries)));
return new CachesReport(cacheManagerDescriptors);
}
/**
......@@ -160,18 +163,36 @@ public class CachesEndpoint {
*/
public static final class CachesReport {
private final Map<String, Map<String, CacheDescriptor>> cacheManagers;
private final Map<String, CacheManagerDescriptor> cacheManagers;
public CachesReport(Map<String, Map<String, CacheDescriptor>> cacheManagers) {
public CachesReport(Map<String, CacheManagerDescriptor> cacheManagers) {
this.cacheManagers = cacheManagers;
}
public Map<String, Map<String, CacheDescriptor>> getCacheManagers() {
public Map<String, CacheManagerDescriptor> getCacheManagers() {
return this.cacheManagers;
}
}
/**
* Description of a {@link CacheManager}, primarily intended for serialization to
* JSON.
*/
public static final class CacheManagerDescriptor {
private final Map<String, CacheDescriptor> caches;
public CacheManagerDescriptor(Map<String, CacheDescriptor> caches) {
this.caches = caches;
}
public Map<String, CacheDescriptor> getCaches() {
return this.caches;
}
}
/**
* Basic description of a {@link Cache}, primarily intended for serialization to JSON.
*/
......
......@@ -26,8 +26,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.actuate.cache.CachesEndpoint.CacheDescriptor;
import org.springframework.boot.actuate.cache.CachesEndpoint.CacheEntry;
import org.springframework.boot.actuate.cache.CachesEndpoint.CacheManagerDescriptor;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
......@@ -53,14 +53,14 @@ public class CachesEndpointTests {
public void allCachesWithSingleCacheManager() {
CachesEndpoint endpoint = new CachesEndpoint(Collections.singletonMap("test",
new ConcurrentMapCacheManager("a", "b")));
Map<String, Map<String, CacheDescriptor>> allDescriptors = endpoint.caches()
Map<String, CacheManagerDescriptor> allDescriptors = endpoint.caches()
.getCacheManagers();
assertThat(allDescriptors).containsOnlyKeys("test");
Map<String, CacheDescriptor> descriptors = allDescriptors.get("test");
assertThat(descriptors).containsOnlyKeys("a", "b");
assertThat(descriptors.get("a").getTarget())
CacheManagerDescriptor descriptors = allDescriptors.get("test");
assertThat(descriptors.getCaches()).containsOnlyKeys("a", "b");
assertThat(descriptors.getCaches().get("a").getTarget())
.isEqualTo(ConcurrentHashMap.class.getName());
assertThat(descriptors.get("b").getTarget())
assertThat(descriptors.getCaches().get("b").getTarget())
.isEqualTo(ConcurrentHashMap.class.getName());
}
......@@ -70,11 +70,11 @@ public class CachesEndpointTests {
cacheManagers.put("test", new ConcurrentMapCacheManager("a", "b"));
cacheManagers.put("another", new ConcurrentMapCacheManager("a", "c"));
CachesEndpoint endpoint = new CachesEndpoint(cacheManagers);
Map<String, Map<String, CacheDescriptor>> allDescriptors = endpoint.caches()
Map<String, CacheManagerDescriptor> allDescriptors = endpoint.caches()
.getCacheManagers();
assertThat(allDescriptors).containsOnlyKeys("test", "another");
assertThat(allDescriptors.get("test")).containsOnlyKeys("a", "b");
assertThat(allDescriptors.get("another")).containsOnlyKeys("a", "c");
assertThat(allDescriptors.get("test").getCaches()).containsOnlyKeys("a", "b");
assertThat(allDescriptors.get("another").getCaches()).containsOnlyKeys("a", "c");
}
@Test
......
......@@ -49,13 +49,13 @@ public class CachesEndpointWebIntegrationTests {
@Test
public void allCaches() {
client.get().uri("/actuator/caches").exchange().expectStatus().isOk().expectBody()
.jsonPath("cacheManagers.one.a.target")
.jsonPath("cacheManagers.one.caches.a.target")
.isEqualTo(ConcurrentHashMap.class.getName())
.jsonPath("cacheManagers.one.b.target")
.jsonPath("cacheManagers.one.caches.b.target")
.isEqualTo(ConcurrentHashMap.class.getName())
.jsonPath("cacheManagers.two.a.target")
.jsonPath("cacheManagers.two.caches.a.target")
.isEqualTo(ConcurrentHashMap.class.getName())
.jsonPath("cacheManagers.two.c.target")
.jsonPath("cacheManagers.two.caches.c.target")
.isEqualTo(ConcurrentHashMap.class.getName());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment