|
| 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.metrics; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import io.micrometer.core.instrument.Meter; |
| 24 | +import io.micrometer.core.instrument.Meter.Id; |
| 25 | +import io.micrometer.core.instrument.MeterRegistry; |
| 26 | +import io.micrometer.core.instrument.simple.SimpleCounter; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.mockito.BDDMockito.given; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link MetricsEndpoint}. |
| 35 | + * |
| 36 | + * @author Andy Wilkinson |
| 37 | + */ |
| 38 | +public class MetricsEndpointTests { |
| 39 | + |
| 40 | + private final MeterRegistry registry = mock(MeterRegistry.class); |
| 41 | + |
| 42 | + private final MetricsEndpoint endpoint = new MetricsEndpoint(this.registry); |
| 43 | + |
| 44 | + @Test |
| 45 | + public void listNamesHandlesEmptyListOfMeters() { |
| 46 | + given(this.registry.getMeters()).willReturn(Arrays.asList()); |
| 47 | + Map<String, List<String>> result = this.endpoint.listNames(); |
| 48 | + assertThat(result).containsOnlyKeys("names"); |
| 49 | + assertThat(result.get("names")).isEmpty(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void listNamesProducesListOfUniqueMeterNames() { |
| 54 | + List<Meter> meters = Arrays.asList(createCounter("com.example.foo"), |
| 55 | + createCounter("com.example.bar"), createCounter("com.example.foo")); |
| 56 | + given(this.registry.getMeters()).willReturn(meters); |
| 57 | + Map<String, List<String>> result = this.endpoint.listNames(); |
| 58 | + assertThat(result).containsOnlyKeys("names"); |
| 59 | + assertThat(result.get("names")).containsOnlyOnce("com.example.foo", |
| 60 | + "com.example.bar"); |
| 61 | + } |
| 62 | + |
| 63 | + private Meter createCounter(String name) { |
| 64 | + return new SimpleCounter(createMeterId(name)); |
| 65 | + } |
| 66 | + |
| 67 | + private Id createMeterId(String name) { |
| 68 | + Id id = mock(Id.class); |
| 69 | + given(id.getName()).willReturn(name); |
| 70 | + return id; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments