|
|
|
@ -31,9 +31,14 @@ import org.springframework.boot.actuate.endpoint.annotation.DiscoveredEndpoint;
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.web.PathMapper;
|
|
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
|
|
|
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
|
|
|
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
|
|
|
|
import org.springframework.boot.test.context.runner.ContextConsumer;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
@ -41,21 +46,26 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
* Tests for {@link ControllerEndpointDiscoverer}.
|
|
|
|
|
*
|
|
|
|
|
* @author Phillip Webb
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
*/
|
|
|
|
|
public class ControllerEndpointDiscovererTests {
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public final ExpectedException thrown = ExpectedException.none();
|
|
|
|
|
|
|
|
|
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getEndpointsWhenNoEndpointBeansShouldReturnEmptyCollection() {
|
|
|
|
|
load(EmptyConfiguration.class,
|
|
|
|
|
(discoverer) -> assertThat(discoverer.getEndpoints()).isEmpty());
|
|
|
|
|
this.contextRunner.withUserConfiguration(EmptyConfiguration.class).run(
|
|
|
|
|
assertDiscoverer((discoverer) ->
|
|
|
|
|
assertThat(discoverer.getEndpoints()).isEmpty()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getEndpointsShouldIncludeControllerEndpoints() {
|
|
|
|
|
load(TestControllerEndpoint.class, (discoverer) -> {
|
|
|
|
|
this.contextRunner.withUserConfiguration(TestControllerEndpoint.class)
|
|
|
|
|
.run(assertDiscoverer((discoverer) -> {
|
|
|
|
|
Collection<ExposableControllerEndpoint> endpoints = discoverer.getEndpoints();
|
|
|
|
|
assertThat(endpoints).hasSize(1);
|
|
|
|
|
ExposableControllerEndpoint endpoint = endpoints.iterator().next();
|
|
|
|
@ -63,52 +73,82 @@ public class ControllerEndpointDiscovererTests {
|
|
|
|
|
assertThat(endpoint.getController())
|
|
|
|
|
.isInstanceOf(TestControllerEndpoint.class);
|
|
|
|
|
assertThat(endpoint).isInstanceOf(DiscoveredEndpoint.class);
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getEndpointsShouldDiscoverProxyControllerEndpoints() {
|
|
|
|
|
this.contextRunner.withUserConfiguration(TestProxyControllerEndpoint.class)
|
|
|
|
|
.withConfiguration(AutoConfigurations.of(
|
|
|
|
|
ValidationAutoConfiguration.class))
|
|
|
|
|
.run(assertDiscoverer((discoverer) -> {
|
|
|
|
|
Collection<ExposableControllerEndpoint> endpoints = discoverer.getEndpoints();
|
|
|
|
|
assertThat(endpoints).hasSize(1);
|
|
|
|
|
ExposableControllerEndpoint endpoint = endpoints.iterator().next();
|
|
|
|
|
assertThat(endpoint.getId()).isEqualTo("testcontroller");
|
|
|
|
|
assertThat(endpoint.getController())
|
|
|
|
|
.isInstanceOf(TestProxyControllerEndpoint.class);
|
|
|
|
|
assertThat(endpoint).isInstanceOf(DiscoveredEndpoint.class);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getEndpointsShouldIncludeRestControllerEndpoints() {
|
|
|
|
|
load(TestRestControllerEndpoint.class, (discoverer) -> {
|
|
|
|
|
this.contextRunner.withUserConfiguration(TestRestControllerEndpoint.class)
|
|
|
|
|
.run(assertDiscoverer((discoverer) -> {
|
|
|
|
|
Collection<ExposableControllerEndpoint> endpoints = discoverer.getEndpoints();
|
|
|
|
|
assertThat(endpoints).hasSize(1);
|
|
|
|
|
ExposableControllerEndpoint endpoint = endpoints.iterator().next();
|
|
|
|
|
assertThat(endpoint.getId()).isEqualTo("testrestcontroller");
|
|
|
|
|
assertThat(endpoint.getController())
|
|
|
|
|
.isInstanceOf(TestRestControllerEndpoint.class);
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getEndpointsShouldDiscoverProxyRestControllerEndpoints() {
|
|
|
|
|
this.contextRunner.withUserConfiguration(TestProxyRestControllerEndpoint.class)
|
|
|
|
|
.withConfiguration(AutoConfigurations.of(
|
|
|
|
|
ValidationAutoConfiguration.class))
|
|
|
|
|
.run(assertDiscoverer((discoverer) -> {
|
|
|
|
|
Collection<ExposableControllerEndpoint> endpoints = discoverer.getEndpoints();
|
|
|
|
|
assertThat(endpoints).hasSize(1);
|
|
|
|
|
ExposableControllerEndpoint endpoint = endpoints.iterator().next();
|
|
|
|
|
assertThat(endpoint.getId()).isEqualTo("testrestcontroller");
|
|
|
|
|
assertThat(endpoint.getController())
|
|
|
|
|
.isInstanceOf(TestProxyRestControllerEndpoint.class);
|
|
|
|
|
assertThat(endpoint).isInstanceOf(DiscoveredEndpoint.class);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getEndpointsShouldNotDiscoverRegularEndpoints() {
|
|
|
|
|
load(WithRegularEndpointConfiguration.class, (discoverer) -> {
|
|
|
|
|
this.contextRunner.withUserConfiguration(WithRegularEndpointConfiguration.class)
|
|
|
|
|
.run(assertDiscoverer((discoverer) -> {
|
|
|
|
|
Collection<ExposableControllerEndpoint> endpoints = discoverer.getEndpoints();
|
|
|
|
|
List<String> ids = endpoints.stream().map(ExposableEndpoint::getId)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
assertThat(ids).containsOnly("testcontroller", "testrestcontroller");
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void getEndpointWhenEndpointHasOperationsShouldThrowException() {
|
|
|
|
|
load(TestControllerWithOperation.class, (discoverer) -> {
|
|
|
|
|
this.contextRunner.withUserConfiguration(TestControllerWithOperation.class)
|
|
|
|
|
.run(assertDiscoverer((discoverer) -> {
|
|
|
|
|
this.thrown.expect(IllegalStateException.class);
|
|
|
|
|
this.thrown.expectMessage("ControllerEndpoints must not declare operations");
|
|
|
|
|
discoverer.getEndpoints();
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void load(Class<?> configuration,
|
|
|
|
|
private ContextConsumer<AssertableApplicationContext> assertDiscoverer(
|
|
|
|
|
Consumer<ControllerEndpointDiscoverer> consumer) {
|
|
|
|
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
|
|
|
|
configuration);
|
|
|
|
|
try {
|
|
|
|
|
return (context) -> {
|
|
|
|
|
ControllerEndpointDiscoverer discoverer = new ControllerEndpointDiscoverer(
|
|
|
|
|
context, PathMapper.useEndpointId(), Collections.emptyList());
|
|
|
|
|
consumer.accept(discoverer);
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
context.close();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
@ -128,11 +168,23 @@ public class ControllerEndpointDiscovererTests {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ControllerEndpoint(id = "testcontroller")
|
|
|
|
|
@Validated
|
|
|
|
|
static class TestProxyControllerEndpoint {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RestControllerEndpoint(id = "testrestcontroller")
|
|
|
|
|
static class TestRestControllerEndpoint {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RestControllerEndpoint(id = "testrestcontroller")
|
|
|
|
|
@Validated
|
|
|
|
|
static class TestProxyRestControllerEndpoint {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Endpoint(id = "test")
|
|
|
|
|
static class TestEndpoint {
|
|
|
|
|
|
|
|
|
|