Skip to content

Commit

Permalink
Merge pull request spring-projects#33743 from msobeck
Browse files Browse the repository at this point in the history
* spring-projectsgh-33743:
  Polish "Test Micrometer config to property exposure"
  Test Micrometer config to property exposure

Closes spring-projectsgh-33743
  • Loading branch information
wilkinsona committed Jul 21, 2023
2 parents df033ef + c2b43e7 commit 7955e16
Show file tree
Hide file tree
Showing 24 changed files with 253 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,10 @@
class AppOpticsPropertiesConfigAdapterTests
extends StepRegistryPropertiesConfigAdapterTests<AppOpticsProperties, AppOpticsPropertiesConfigAdapter> {

AppOpticsPropertiesConfigAdapterTests() {
super(AppOpticsPropertiesConfigAdapter.class);
}

@Override
protected AppOpticsProperties createProperties() {
return new AppOpticsProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@

import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link AtlasPropertiesConfigAdapter}.
*
* @author Mirko Sobeck
*/
class AtlasPropertiesConfigAdapterTests {
class AtlasPropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<AtlasProperties, AtlasPropertiesConfigAdapter> {

AtlasPropertiesConfigAdapterTests() {
super(AtlasPropertiesConfigAdapter.class);
}

@Test
void whenPropertiesStepIsSetAdapterStepReturnsIt() {
Expand Down Expand Up @@ -116,4 +123,11 @@ void whenPropertiesEvalUriIsSetAdapterEvalUriReturnsIt() {
.isEqualTo("https://atlas.example.com/evaluate");
}

@Test
@Override
protected void adapterOverridesAllConfigMethods() {
adapterOverridesAllConfigMethodsExcept("autoStart", "commonTags", "debugRegistry", "publisher", "rollupPolicy",
"validTagCharacters");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,8 @@ void defaultValuesAreConsistent() {
assertThat(properties.getUri()).isEqualTo(config.uri());
assertThat(properties.getMeterTimeToLive()).isEqualTo(config.meterTTL());
assertThat(properties.isLwcEnabled()).isEqualTo(config.lwcEnabled());
assertThat(properties.isLwcIgnorePublishStep()).isEqualTo(config.lwcIgnorePublishStep());
assertThat(properties.getLwcStep()).isEqualTo(config.lwcStep());
assertThat(properties.getConfigRefreshFrequency()).isEqualTo(config.configRefreshFrequency());
assertThat(properties.getConfigTimeToLive()).isEqualTo(config.configTTL());
assertThat(properties.getConfigUri()).isEqualTo(config.configUri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
class DatadogPropertiesConfigAdapterTests
extends StepRegistryPropertiesConfigAdapterTests<DatadogProperties, DatadogPropertiesConfigAdapter> {

DatadogPropertiesConfigAdapterTests() {
super(DatadogPropertiesConfigAdapter.class);
}

@Override
protected DatadogProperties createProperties() {
return new DatadogProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.micrometer.dynatrace.DynatraceApiVersion;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand All @@ -29,7 +31,12 @@
* @author Andy Wilkinson
* @author Georg Pirklbauer
*/
class DynatracePropertiesConfigAdapterTests {
class DynatracePropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<DynatraceProperties, DynatracePropertiesConfigAdapter> {

DynatracePropertiesConfigAdapterTests() {
super(DynatracePropertiesConfigAdapter.class);
}

@Test
void whenPropertiesUriIsSetAdapterUriReturnsIt() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,14 +18,21 @@

import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ElasticPropertiesConfigAdapter}.
*
* @author Andy Wilkinson
*/
class ElasticPropertiesConfigAdapterTests {
class ElasticPropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<ElasticProperties, ElasticPropertiesConfigAdapter> {

ElasticPropertiesConfigAdapterTests() {
super(ElasticPropertiesConfigAdapter.class);
}

@Test
void whenPropertiesHostsIsSetAdapterHostsReturnsIt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
import info.ganglia.gmetric4j.gmetric.GMetric.UDPAddressingMode;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link GangliaPropertiesConfigAdapter}.
*
* @author Mirko Sobeck
*/
class GangliaPropertiesConfigAdapterTests {
class GangliaPropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<GangliaProperties, GangliaPropertiesConfigAdapter> {

GangliaPropertiesConfigAdapterTests() {
super(GangliaPropertiesConfigAdapter.class);
}

@Test
void whenPropertiesEnabledIsSetAdapterEnabledReturnsIt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
import io.micrometer.graphite.GraphiteProtocol;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link GraphitePropertiesConfigAdapter}.
*
* @author Mirko Sobeck
*/
class GraphitePropertiesConfigAdapterTests {
class GraphitePropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<GraphiteProperties, GraphitePropertiesConfigAdapter> {

GraphitePropertiesConfigAdapterTests() {
super(GraphitePropertiesConfigAdapter.class);
}

@Test
void whenPropertiesEnabledIsSetAdapterEnabledReturnsIt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@

import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link HumioPropertiesConfigAdapter}.
*
* @author Andy Wilkinson
*/
class HumioPropertiesConfigAdapterTests {
class HumioPropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<HumioProperties, HumioPropertiesConfigAdapter> {

HumioPropertiesConfigAdapterTests() {
super(HumioPropertiesConfigAdapter.class);
}

@Test
void whenApiTokenIsSetAdapterApiTokenReturnsIt() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,14 +19,21 @@
import io.micrometer.influx.InfluxApiVersion;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link InfluxPropertiesConfigAdapter}.
*
* @author Stephane Nicoll
*/
class InfluxPropertiesConfigAdapterTests {
class InfluxPropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<InfluxProperties, InfluxPropertiesConfigAdapter> {

InfluxPropertiesConfigAdapterTests() {
super(InfluxPropertiesConfigAdapter.class);
}

@Test
void adaptInfluxV1BasicConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@

import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link JmxPropertiesConfigAdapter}.
*
* @author Mirko Sobeck
*/
class JmxPropertiesConfigAdapterTests {
class JmxPropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<JmxProperties, JmxPropertiesConfigAdapter> {

JmxPropertiesConfigAdapterTests() {
super(JmxPropertiesConfigAdapter.class);
}

@Test
void whenPropertiesStepIsSetAdapterStepReturnsIt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
class KairosPropertiesConfigAdapterTests
extends StepRegistryPropertiesConfigAdapterTests<KairosProperties, KairosPropertiesConfigAdapter> {

KairosPropertiesConfigAdapterTests() {
super(KairosPropertiesConfigAdapter.class);
}

@Override
protected KairosProperties createProperties() {
return new KairosProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
class NewRelicPropertiesConfigAdapterTests
extends StepRegistryPropertiesConfigAdapterTests<NewRelicProperties, NewRelicPropertiesConfigAdapter> {

NewRelicPropertiesConfigAdapterTests() {
super(NewRelicPropertiesConfigAdapter.class);
}

@Override
protected NewRelicProperties createProperties() {
return new NewRelicProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@
import io.micrometer.prometheus.HistogramFlavor;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.AbstractPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link PrometheusPropertiesConfigAdapter}.
*
* @author Mirko Sobeck
*/
class PrometheusPropertiesConfigAdapterTests {
class PrometheusPropertiesConfigAdapterTests
extends AbstractPropertiesConfigAdapterTests<PrometheusProperties, PrometheusPropertiesConfigAdapter> {

PrometheusPropertiesConfigAdapterTests() {
super(PrometheusPropertiesConfigAdapter.class);
}

@Test
void whenPropertiesDescriptionsIsSetAdapterDescriptionsReturnsIt() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.metrics.export.properties;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import io.micrometer.core.instrument.config.validate.Validated;
import org.junit.jupiter.api.Test;

import org.springframework.core.annotation.AnnotatedElementUtils;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Base class for testing properties config adapters.
*
* @param <P> the properties used by the adapter
* @param <A> the adapter under test
* @author Andy Wilkinson
* @author Mirko Sobeck
*/
public abstract class AbstractPropertiesConfigAdapterTests<P, A extends PropertiesConfigAdapter<P>> {

private final Class<? extends A> adapter;

protected AbstractPropertiesConfigAdapterTests(Class<? extends A> adapter) {
this.adapter = adapter;
}

@Test
protected void adapterOverridesAllConfigMethods() {
adapterOverridesAllConfigMethodsExcept();
}

protected final void adapterOverridesAllConfigMethodsExcept(String... nonConfigMethods) {
Class<?> config = findImplementedConfig();
Set<String> expectedConfigMethodNames = Arrays.stream(config.getDeclaredMethods())
.filter(Method::isDefault)
.filter(this::hasNoParameters)
.filter(this::isNotValidationMethod)
.filter(this::isNotDeprecated)
.map(Method::getName)
.collect(Collectors.toCollection(TreeSet::new));
expectedConfigMethodNames.removeAll(Arrays.asList(nonConfigMethods));
Set<String> actualConfigMethodNames = new TreeSet<>();
Class<?> currentClass = this.adapter;
while (!Object.class.equals(currentClass)) {
actualConfigMethodNames.addAll(Arrays.stream(currentClass.getDeclaredMethods())
.map(Method::getName)
.filter(expectedConfigMethodNames::contains)
.collect(Collectors.toList()));
currentClass = currentClass.getSuperclass();
}
assertThat(actualConfigMethodNames).containsExactlyInAnyOrderElementsOf(expectedConfigMethodNames);
}

private Class<?> findImplementedConfig() {
Class<?>[] interfaces = this.adapter.getInterfaces();
if (interfaces.length == 1) {
return interfaces[0];
}
throw new IllegalStateException(this.adapter + " is not a config implementation");
}

private boolean isNotDeprecated(Method method) {
return !AnnotatedElementUtils.hasAnnotation(method, Deprecated.class);
}

private boolean hasNoParameters(Method method) {
return method.getParameterCount() == 0;
}

private boolean isNotValidationMethod(Method method) {
return !Validated.class.equals(method.getReturnType());
}

}
Loading

0 comments on commit 7955e16

Please sign in to comment.