Skip to content

Commit

Permalink
Fix checkstyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
msobeck committed Feb 21, 2023
1 parent 93c46b8 commit 3ced29c
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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;

import java.lang.reflect.Method;
Expand All @@ -7,62 +23,74 @@
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ClassUtils;
import org.junit.Assert;

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

public class TestConfigsToPropertiesExposure {
/**
* Utility to test that all Micrometer config values are exposed via properties and
* settable using the corresponding {@link PropertiesConfigAdapter} implementation.
*
* @author Mirko Sobeck
*/
public final class TestConfigsToPropertiesExposure {

private TestConfigsToPropertiesExposure() {

}

private static final List<Class<?>> classesForPropertiesList = List.of(Boolean.class, Byte.class, Character.class,
Short.class, Integer.class, Long.class, Double.class, Float.class, String.class, Duration.class);

/**
* Assertion to test if default methods of a given config are overridden by the adapter which implements it.
* This can be an indicator for micrometer config fields, that have been forgotten to expose via spring properties.
* Not overridden default methods in adapters are the most common cause of forgotten field exposure, because
* Assertion to test if default methods of a given config are overridden by the
* adapter which implements it. This can be an indicator for micrometer config fields,
* that have been forgotten to expose via spring properties. Not overridden default
* methods in adapters are the most common cause of forgotten field exposure, because
* they do not for force an override.
*
* @param config micrometer config
* @param adapter adapter for properties {@link PropertiesConfigAdapter}
* @param excludedConfigMethods config methods that should be excluded for the assertion
* @param excludedConfigMethods config methods that should be excluded for the
* assertion
*/
public static void assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(Class<?> config, Class<? extends PropertiesConfigAdapter<?>> adapter, String... excludedConfigMethods) {
public static void assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(Class<?> config,
Class<? extends PropertiesConfigAdapter<?>> adapter, String... excludedConfigMethods) {
List<String> configDefaultMethodNames = Arrays.stream(config.getDeclaredMethods())
.filter(method -> method.isDefault() && isSettableUsingProperties(method.getReturnType()))
.map(Method::getName)
.collect(Collectors.toList());
.filter((method) -> method.isDefault() && isSettableUsingProperties(method.getReturnType()))
.map(Method::getName).collect(Collectors.toList());

configDefaultMethodNames.removeAll(Arrays.stream(excludedConfigMethods).toList());
List<String> notOverriddenDefaultMethods = new ArrayList<>(configDefaultMethodNames);

Class<?> currentClass = adapter;
// loop through adapter class and superclasses to find not overridden config methods
// loop through adapter class and superclasses
// to find not overridden config methods
while (!Object.class.equals(currentClass)) {
List<String> overriddenClassDefaultMethods = Arrays.stream(currentClass.getDeclaredMethods())
.map(Method::getName)
.filter(configDefaultMethodNames::contains)
.toList();
.map(Method::getName).filter(configDefaultMethodNames::contains).toList();

notOverriddenDefaultMethods.removeAll(overriddenClassDefaultMethods);
currentClass = currentClass.getSuperclass();
}

if (notOverriddenDefaultMethods.size() >= 1) {
Assert.fail("Found config default methods that are not overridden by the related PropertiesConfigAdapter: \n"
+ notOverriddenDefaultMethods + "\n"
+ "This could be an indicator for not exposed properties fields.\n"
+ "Please check if the fields are meant to be exposed and if not, "
+ "exclude them from this test by providing them to the method.");
Assert.fail(
"Found config default methods that are not overridden by the related PropertiesConfigAdapter: \n"
+ notOverriddenDefaultMethods + "\n"
+ "This could be an indicator for not exposed properties fields.\n"
+ "Please check if the fields are meant to be exposed and if not, "
+ "exclude them from this test by providing them to the method.");
}
}

/**
* Guess if a class can be set using properties.
* This will only catch the basic use cases regarding the micrometer configs to
* filter out methods that are not likely to be designed to be set via properties.
* <pre>
* Guess if a class can be set using properties. This will only catch the basic use
* cases regarding the micrometer configs to filter out methods that are not likely to
* be designed to be set via properties. <pre>
* isSettableUsingProperties(String.class) = true
* isSettableUsingProperties(boolean.class) = true
* isSettableUsingProperties(Object.class) = false
* </pre>
*
* @param clazz Class
* @return is likely to be settable using properties
*/
Expand All @@ -71,10 +99,11 @@ private static boolean isSettableUsingProperties(Class<?> clazz) {
return false;
}

if (ClassUtils.isPrimitiveOrWrapper(clazz)) {
if (clazz.isPrimitive()) {
return true;
}

return List.of(Duration.class, String.class).contains(clazz);
return classesForPropertiesList.contains(clazz);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micrometer.appoptics.AppOpticsConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link AppOpticsPropertiesConfigAdapter}.
Expand Down Expand Up @@ -72,7 +72,8 @@ void whenPropertiesFloorTimesIsSetAdapterFloorTimesReturnsIt() {

@Test
void allDefaultConfigMethodsAreOverriddenByAtlasPropertiesConfigAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(AppOpticsConfig.class, AppOpticsPropertiesConfigAdapter.class,
"connectTimeout");
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(AppOpticsConfig.class,
AppOpticsPropertiesConfigAdapter.class, "connectTimeout");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import com.netflix.spectator.atlas.AtlasConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link AtlasPropertiesConfigAdapter}.
Expand Down Expand Up @@ -120,8 +121,9 @@ void whenPropertiesEvalUriIsSetAdapterEvalUriReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(AtlasConfig.class, AtlasPropertiesConfigAdapter.class,
"lwcIgnorePublishStep", "initialPollingDelay", "autoStart", "lwcStep", "validTagCharacters");
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(AtlasConfig.class,
AtlasPropertiesConfigAdapter.class, "lwcIgnorePublishStep", "initialPollingDelay", "autoStart",
"lwcStep", "validTagCharacters");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micrometer.datadog.DatadogConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link DatadogPropertiesConfigAdapter}.
Expand Down Expand Up @@ -80,7 +80,7 @@ void whenPropertiesUriIsSetAdapterUriReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(DatadogConfig.class,
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(DatadogConfig.class,
DatadogPropertiesConfigAdapter.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import io.micrometer.dynatrace.DynatraceConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link DynatracePropertiesConfigAdapter}.
Expand Down Expand Up @@ -129,7 +130,8 @@ void defaultValues() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(DynatraceConfig.class, DynatracePropertiesConfigAdapter.class,
"documentType");
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(DynatraceConfig.class,
DynatracePropertiesConfigAdapter.class, "documentType");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import io.micrometer.elastic.ElasticConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link ElasticPropertiesConfigAdapter}.
Expand Down Expand Up @@ -101,8 +102,8 @@ void whenPropertiesApiKeyCredentialsIsSetAdapterPipelineReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(ElasticConfig.class, ElasticPropertiesConfigAdapter.class,
"documentType"
);
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(ElasticConfig.class,
ElasticPropertiesConfigAdapter.class, "documentType");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import io.micrometer.ganglia.GangliaConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link GangliaPropertiesConfigAdapter}.
Expand Down Expand Up @@ -85,7 +86,7 @@ void whenPropertiesPortIsSetAdapterPortReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(GangliaConfig.class,
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(GangliaConfig.class,
GangliaPropertiesConfigAdapter.class, "protocolVersion");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import io.micrometer.graphite.GraphiteProtocol;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link GraphitePropertiesConfigAdapter}.
Expand Down Expand Up @@ -98,7 +99,7 @@ void whenPropertiesTagsAsPrefixIsSetAdapterTagsAsPrefixReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(GraphiteConfig.class,
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(GraphiteConfig.class,
GraphitePropertiesConfigAdapter.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import io.micrometer.humio.HumioConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link HumioPropertiesConfigAdapter}.
Expand Down Expand Up @@ -55,7 +56,8 @@ void whenPropertiesUriIsSetAdapterUriReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(HumioConfig.class, HumioPropertiesConfigAdapter.class,
"connectTimeout");
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(HumioConfig.class,
HumioPropertiesConfigAdapter.class, "connectTimeout");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import io.micrometer.influx.InfluxConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link InfluxPropertiesConfigAdapter}.
Expand Down Expand Up @@ -62,6 +63,8 @@ void adaptInfluxV2BasicConfig() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(InfluxConfig.class, InfluxPropertiesConfigAdapter.class);
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(InfluxConfig.class,
InfluxPropertiesConfigAdapter.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import io.micrometer.jmx.JmxConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link JmxPropertiesConfigAdapter}.
Expand All @@ -47,7 +48,8 @@ void whenPropertiesDomainIsSetAdapterDomainReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(JmxConfig.class, JmxPropertiesConfigAdapter.class);
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(JmxConfig.class,
JmxPropertiesConfigAdapter.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micrometer.kairos.KairosConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link KairosPropertiesConfigAdapter}.
Expand Down Expand Up @@ -66,6 +66,8 @@ void whenPropertiesPasswordIsSetAdapterPasswordReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(KairosConfig.class, KairosPropertiesConfigAdapter.class);
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(KairosConfig.class,
KairosPropertiesConfigAdapter.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import io.micrometer.newrelic.NewRelicConfig;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapterTests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.actuate.autoconfigure.metrics.export.TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter;

/**
* Tests for {@link NewRelicPropertiesConfigAdapter}.
Expand Down Expand Up @@ -87,7 +87,7 @@ void whenPropertiesUriIsSetAdapterUriReturnsIt() {

@Test
void allConfigDefaultMethodsAreOverriddenByAdapter() {
assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(NewRelicConfig.class,
TestConfigsToPropertiesExposure.assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(NewRelicConfig.class,
NewRelicPropertiesConfigAdapter.class);
}

Expand Down
Loading

0 comments on commit 3ced29c

Please sign in to comment.