|
| 1 | +package org.springframework.boot.actuate.endpoint.annotation; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 9 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 10 | +import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension; |
| 11 | +import org.springframework.context.bootstrap.generator.infrastructure.nativex.NativeConfigurationRegistry; |
| 12 | +import org.springframework.context.bootstrap.generator.infrastructure.nativex.NativeReflectionEntry; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests for {@link EndpointNativeConfigurationProcessor}. |
| 18 | + * |
| 19 | + * @author Stephane Nicoll |
| 20 | + */ |
| 21 | +class EndpointNativeConfigurationProcessorTests { |
| 22 | + |
| 23 | + @Test |
| 24 | + void registerSimpleEndpoint() { |
| 25 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 26 | + beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition()); |
| 27 | + beanFactory.registerBeanDefinition("endpoint", BeanDefinitionBuilder.rootBeanDefinition(TestEndpoint.class).getBeanDefinition()); |
| 28 | + NativeConfigurationRegistry registry = process(beanFactory); |
| 29 | + List<NativeReflectionEntry> entries = registry.reflection().getEntries(); |
| 30 | + assertThat(entries).anySatisfy((entry) -> { |
| 31 | + assertThat(entry.getType()).isEqualTo(TestEndpoint.class); |
| 32 | + assertThat(entry.getMethods().stream().map(Method::getName)) |
| 33 | + .containsOnly("getAll", "get", "set", "delete"); |
| 34 | + }); |
| 35 | + assertThat(entries).hasSize(1); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void registerSimpleEndpointExtension() { |
| 40 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 41 | + beanFactory.registerBeanDefinition("noise", BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition()); |
| 42 | + beanFactory.registerBeanDefinition("endpoint", BeanDefinitionBuilder.rootBeanDefinition(TestEndpointWebExtension.class).getBeanDefinition()); |
| 43 | + NativeConfigurationRegistry registry = process(beanFactory); |
| 44 | + List<NativeReflectionEntry> entries = registry.reflection().getEntries(); |
| 45 | + assertThat(entries).anySatisfy((entry) -> { |
| 46 | + assertThat(entry.getType()).isEqualTo(TestEndpointWebExtension.class); |
| 47 | + assertThat(entry.getMethods().stream().map(Method::getName)).containsOnly("get"); |
| 48 | + }); |
| 49 | + assertThat(entries).hasSize(1); |
| 50 | + } |
| 51 | + |
| 52 | + private NativeConfigurationRegistry process(DefaultListableBeanFactory beanFactory) { |
| 53 | + NativeConfigurationRegistry registry = new NativeConfigurationRegistry(); |
| 54 | + new EndpointNativeConfigurationProcessor().process(beanFactory, registry); |
| 55 | + return registry; |
| 56 | + } |
| 57 | + |
| 58 | + @Endpoint(id = "test") |
| 59 | + @SuppressWarnings("unused") |
| 60 | + static class TestEndpoint { |
| 61 | + |
| 62 | + @ReadOperation |
| 63 | + public List<String> getAll() { |
| 64 | + return List.of(get()); |
| 65 | + } |
| 66 | + |
| 67 | + @ReadOperation |
| 68 | + public String get() { |
| 69 | + return "test"; |
| 70 | + } |
| 71 | + |
| 72 | + @WriteOperation |
| 73 | + public void set(String name) { |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @DeleteOperation |
| 78 | + public void delete(String name) { |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public void ignoredMethod(String name) { |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @EndpointWebExtension(endpoint = TestEndpoint.class) |
| 89 | + @SuppressWarnings("unused") |
| 90 | + static class TestEndpointWebExtension { |
| 91 | + |
| 92 | + @ReadOperation |
| 93 | + public String get() { |
| 94 | + return "web"; |
| 95 | + } |
| 96 | + |
| 97 | + public void noise(String name) { |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments