|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.endpoint; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import org.springframework.boot.actuate.cache.CacheStatisticsProvider; |
| 28 | +import org.springframework.boot.actuate.cache.CaffeineCacheStatisticsProvider; |
| 29 | +import org.springframework.boot.actuate.cache.ConcurrentMapCacheStatisticsProvider; |
| 30 | +import org.springframework.boot.actuate.metrics.Metric; |
| 31 | +import org.springframework.cache.CacheManager; |
| 32 | +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.assertj.core.api.Assertions.entry; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for {@link CachePublicMetrics} |
| 39 | + * |
| 40 | + * @author Stephane Nicoll |
| 41 | + */ |
| 42 | +public class CachePublicMetricsTests { |
| 43 | + |
| 44 | + private Map<String, CacheManager> cacheManagers = new HashMap<String, CacheManager>(); |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setup() { |
| 48 | + this.cacheManagers.put("cacheManager", |
| 49 | + new ConcurrentMapCacheManager("foo", "bar")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void cacheMetricsWithMatchingProvider() { |
| 54 | + CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers, |
| 55 | + providers(new ConcurrentMapCacheStatisticsProvider())); |
| 56 | + Map<String, Number> metrics = metrics(cpm); |
| 57 | + assertThat(metrics).containsOnly(entry("cache.foo.size", 0L), |
| 58 | + entry("cache.bar.size", 0L)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void cacheMetricsWithNoMatchingProvider() { |
| 63 | + CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers, |
| 64 | + providers(new CaffeineCacheStatisticsProvider())); |
| 65 | + Map<String, Number> metrics = metrics(cpm); |
| 66 | + assertThat(metrics).isEmpty(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void cacheMetricsWithMultipleCacheManagers() { |
| 71 | + this.cacheManagers.put("anotherCacheManager", |
| 72 | + new ConcurrentMapCacheManager("foo")); |
| 73 | + CachePublicMetrics cpm = new CachePublicMetrics(this.cacheManagers, |
| 74 | + providers(new ConcurrentMapCacheStatisticsProvider())); |
| 75 | + Map<String, Number> metrics = metrics(cpm); |
| 76 | + assertThat(metrics).containsOnly(entry("cache.cacheManager_foo.size", 0L), |
| 77 | + entry("cache.bar.size", 0L), |
| 78 | + entry("cache.anotherCacheManager_foo.size", 0L)); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private Map<String, Number> metrics(CachePublicMetrics cpm) { |
| 83 | + Collection<Metric<?>> metrics = cpm.metrics(); |
| 84 | + assertThat(metrics).isNotNull(); |
| 85 | + Map<String, Number> result = new HashMap<String, Number>(); |
| 86 | + for (Metric<?> metric : metrics) { |
| 87 | + result.put(metric.getName(), metric.getValue()); |
| 88 | + } |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + private Collection<CacheStatisticsProvider<?>> providers( |
| 93 | + CacheStatisticsProvider<?>... providers) { |
| 94 | + return Arrays.asList(providers); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments