|
| 1 | +package org.springframework.boot.actuate.autoconfigure.metrics.export; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | +import java.time.Duration; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import org.apache.commons.lang3.ClassUtils; |
| 11 | +import org.junit.Assert; |
| 12 | + |
| 13 | +import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter; |
| 14 | + |
| 15 | +public class TestConfigsToPropertiesExposure { |
| 16 | + /** |
| 17 | + * Assertion to test if default methods of a given config are overridden by the adapter which implements it. |
| 18 | + * This can be an indicator for micrometer config fields, that have been forgotten to expose via spring properties. |
| 19 | + * Not overridden default methods in adapters are the most common cause of forgotten field exposure, because |
| 20 | + * they do not for force an override. |
| 21 | + * |
| 22 | + * @param config micrometer config |
| 23 | + * @param adapter adapter for properties {@link PropertiesConfigAdapter} |
| 24 | + * @param excludedConfigMethods config methods that should be excluded for the assertion |
| 25 | + */ |
| 26 | + public static void assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(Class<?> config, Class<? extends PropertiesConfigAdapter<?>> adapter, String... excludedConfigMethods) { |
| 27 | + List<String> configDefaultMethodNames = Arrays.stream(config.getDeclaredMethods()) |
| 28 | + .filter(method -> method.isDefault() && isSettableUsingProperties(method.getReturnType())) |
| 29 | + .map(Method::getName) |
| 30 | + .collect(Collectors.toList()); |
| 31 | + |
| 32 | + configDefaultMethodNames.removeAll(Arrays.stream(excludedConfigMethods).toList()); |
| 33 | + List<String> notOverriddenDefaultMethods = new ArrayList<>(configDefaultMethodNames); |
| 34 | + |
| 35 | + Class<?> currentClass = adapter; |
| 36 | + // loop through adapter class and superclasses to find not overridden config methods |
| 37 | + while (!Object.class.equals(currentClass)) { |
| 38 | + List<String> overriddenClassDefaultMethods = Arrays.stream(currentClass.getDeclaredMethods()) |
| 39 | + .map(Method::getName) |
| 40 | + .filter(configDefaultMethodNames::contains) |
| 41 | + .toList(); |
| 42 | + |
| 43 | + notOverriddenDefaultMethods.removeAll(overriddenClassDefaultMethods); |
| 44 | + currentClass = currentClass.getSuperclass(); |
| 45 | + } |
| 46 | + |
| 47 | + if (notOverriddenDefaultMethods.size() >= 1) { |
| 48 | + Assert.fail("Found config default methods that are not overridden by the related PropertiesConfigAdapter: \n" |
| 49 | + + notOverriddenDefaultMethods + "\n" |
| 50 | + + "This could be an indicator for not exposed properties fields.\n" |
| 51 | + + "Please check if the fields are meant to be exposed and if not, " |
| 52 | + + "exclude them from this test by providing them to the method."); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Guess if a class can be set using properties. |
| 58 | + * This will only catch the basic use cases regarding the micrometer configs to |
| 59 | + * filter out methods that are not likely to be designed to be set via properties. |
| 60 | + * <pre> |
| 61 | + * isSettableUsingProperties(String.class) = true |
| 62 | + * isSettableUsingProperties(boolean.class) = true |
| 63 | + * isSettableUsingProperties(Object.class) = false |
| 64 | + * </pre> |
| 65 | + * |
| 66 | + * @param clazz Class |
| 67 | + * @return is likely to be settable using properties |
| 68 | + */ |
| 69 | + private static boolean isSettableUsingProperties(Class<?> clazz) { |
| 70 | + if (Void.TYPE.equals(clazz)) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + if (ClassUtils.isPrimitiveOrWrapper(clazz)) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + return List.of(Duration.class, String.class).contains(clazz); |
| 79 | + } |
| 80 | +} |
0 commit comments