Java Code Examples for org.springframework.boot.actuate.endpoint.EndpointId

The following examples show how to use org.springframework.boot.actuate.endpoint.EndpointId. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: java-spring-web   Source File: SkipPatternConfigTest.java    License: Apache License 2.0 6 votes vote down vote up
private ExposableWebEndpoint createEndpoint(final String name) {
  return new ExposableWebEndpoint() {

    @Override
    public String getRootPath() {
      return name;
    }

    @Override
    public EndpointId getEndpointId() {
      return EndpointId.of(name);
    }

    @Override
    public boolean isEnableByDefault() {
      return false;
    }

    @Override
    public Collection<WebOperation> getOperations() {
      return null;
    }
  };
}
 
Example 2
@SuppressWarnings("unchecked")
void assertReadProperties(Class<?> testConfigClass) {
	load(testConfigClass, (discoverer) -> {
		Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
		assertThat(endpoints).containsKey(endpointId);

		ExposableWebEndpoint endpoint = endpoints.get(endpointId);
		assertThat(endpoint.getOperations()).hasSize(1);

		WebOperation operation = endpoint.getOperations().iterator().next();
		Object invoker = ReflectionTestUtils.getField(operation, "invoker");
		assertThat(invoker).isInstanceOf(ReflectiveOperationInvoker.class);

		Map<String, Properties> properties = (Map<String, Properties>) ((ReflectiveOperationInvoker) invoker).invoke(
				new InvocationContext(mock(SecurityContext.class), Collections.emptyMap()));
		assertThat(properties).hasSize(1);
	});
}
 
Example 3
private void load(Function<EndpointId, Long> timeToLive,
		PathMapper endpointPathMapper,
		Class<?> configuration,
		Consumer<WebEndpointDiscoverer> consumer) {

	try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configuration)) {
		ConversionServiceParameterValueMapper parameterMapper = new ConversionServiceParameterValueMapper(DefaultConversionService.getSharedInstance());
		EndpointMediaTypes mediaTypes = new EndpointMediaTypes(
				Collections.singletonList("application/json"),
				Collections.singletonList("application/json"));

		WebEndpointDiscoverer discoverer = new WebEndpointDiscoverer(context,
				parameterMapper,
				mediaTypes,
				Collections.singletonList(endpointPathMapper),
				Collections.singleton(new CachingOperationInvokerAdvisor(timeToLive)),
				Collections.emptyList());

		consumer.accept(discoverer);
	}
}
 
Example 4
protected String getHealthEndpointPath() {
	String health = pathMappedEndpoints.getPath(EndpointId.of("health"));
	if (StringUtils.hasText(health)) {
		return health;
	}
	String status = pathMappedEndpoints.getPath(EndpointId.of("status"));
	if (StringUtils.hasText(status)) {
		return status;
	}
	throw new IllegalStateException("Either health or status endpoint must be enabled!");
}
 
Example 5
@Test
public void test_mgmtPortPath() {
	webEndpoint.setBasePath("/admin");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/alive");
	publishApplicationReadyEvent(factory, 8080, 8081);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8081/admin");
	assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/alive");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/");
}
 
Example 6
@Test
public void test_default() {
	instanceProperties.setMetadata(singletonMap("instance", "test"));
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	publishApplicationReadyEvent(factory, 8080, null);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8080/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8080/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/");

	assertThat(app.getMetadata()).containsExactly(entry("contributor", "test"), entry("instance", "test"));
}
 
Example 7
@Test
public void test_ssl() {
	server.setSsl(new Ssl());
	server.getSsl().setEnabled(true);
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	publishApplicationReadyEvent(factory, 8080, null);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("https://" + getHostname() + ":8080/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("https://" + getHostname() + ":8080/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("https://" + getHostname() + ":8080/");
}
 
Example 8
@Test
public void test_ssl_management() {
	management.setSsl(new Ssl());
	management.getSsl().setEnabled(true);
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/alive");
	publishApplicationReadyEvent(factory, 8080, 9090);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("https://" + getHostname() + ":9090/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("https://" + getHostname() + ":9090/actuator/alive");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/");
}
 
Example 9
@Test
public void test_preferIpAddress_serveraddress_missing() {
	instanceProperties.setPreferIp(true);
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/application/alive");
	publishApplicationReadyEvent(factory, 8080, null);

	Application app = factory.createApplication();
	assertThat(app.getServiceUrl()).matches("http://\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}:8080/");
}
 
Example 10
@Test
public void test_preferIpAddress_managementaddress_missing() {
	instanceProperties.setPreferIp(true);
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/application/alive");
	publishApplicationReadyEvent(factory, 8080, 8081);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).matches("http://\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}:8081/actuator");
}
 
Example 11
@Test
public void test_preferIpAddress() throws UnknownHostException {
	instanceProperties.setPreferIp(true);
	server.setAddress(InetAddress.getByName("127.0.0.1"));
	management.setAddress(InetAddress.getByName("127.0.0.2"));
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	publishApplicationReadyEvent(factory, 8080, 8081);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://127.0.0.2:8081/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("http://127.0.0.2:8081/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://127.0.0.1:8080/");
}
 
Example 12
@Test
public void test_all_baseUrls() {
	instanceProperties.setManagementBaseUrl("http://management:8090");
	instanceProperties.setServiceBaseUrl("http://service:80");
	webEndpoint.setBasePath("/admin");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");

	Application app = factory.createApplication();
	assertThat(app.getServiceUrl()).isEqualTo("http://service:80/");
	assertThat(app.getManagementUrl()).isEqualTo("http://management:8090/admin");
	assertThat(app.getHealthUrl()).isEqualTo("http://management:8090/admin/health");
}
 
Example 13
@Test
public void test_service_baseUrl() {
	instanceProperties.setServiceBaseUrl("http://service:80");
	webEndpoint.setBasePath("/admin");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");

	Application app = factory.createApplication();
	assertThat(app.getServiceUrl()).isEqualTo("http://service:80/");
	assertThat(app.getManagementUrl()).isEqualTo("http://service:80/admin");
	assertThat(app.getHealthUrl()).isEqualTo("http://service:80/admin/health");
}
 
Example 14
@Test
public void test_service_path() {
	instanceProperties.setServiceBaseUrl("http://service:80");
	instanceProperties.setServicePath("app");
	webEndpoint.setBasePath("/admin");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");

	Application app = factory.createApplication();
	assertThat(app.getServiceUrl()).isEqualTo("http://service:80/app");
	assertThat(app.getManagementUrl()).isEqualTo("http://service:80/app/admin");
	assertThat(app.getHealthUrl()).isEqualTo("http://service:80/app/admin/health");
}
 
Example 15
@Test
public void should_use_application_uri() {
	when(this.pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	this.cfApplicationProperties.setUris(singletonList("application/Uppercase"));

	Application app = this.factory.createApplication();

	assertThat(app.getManagementUrl()).isEqualTo("http://application/Uppercase/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("http://application/Uppercase/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://application/Uppercase/");
}
 
Example 16
@Test
public void should_use_service_base_uri() {
	when(this.pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	this.cfApplicationProperties.setUris(singletonList("application/Uppercase"));
	this.instanceProperties.setServiceBaseUrl("https://serviceBaseUrl");

	Application app = this.factory.createApplication();

	assertThat(app.getManagementUrl()).isEqualTo("https://serviceBaseUrl/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("https://serviceBaseUrl/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("https://serviceBaseUrl/");
}
 
Example 17
@Test
public void test_contextPath_mgmtPath() {
	servletContext.setContextPath("app");
	webEndpoint.setBasePath("/admin");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");
	publishApplicationReadyEvent(factory, 8080, null);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin");
	assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app");
}
 
Example 18
@Test
public void test_contextPath_mgmtPortPath() {
	servletContext.setContextPath("app");
	webEndpoint.setBasePath("/admin");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");
	publishApplicationReadyEvent(factory, 8080, 8081);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8081/admin");
	assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app");
}
 
Example 19
@Test
public void test_contextPath() {
	servletContext.setContextPath("app");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	publishApplicationReadyEvent(factory, 80, null);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/app/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/app/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/app");
}
 
Example 20
@Test
public void test_servletPath() {
	when(dispatcherServletPath.getPrefix()).thenReturn("app");
	servletContext.setContextPath("srv");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	publishApplicationReadyEvent(factory, 80, null);

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/srv");
}
 
Example 21
@Test
public void test_servicePath() {
	servletContext.setContextPath("app");
	when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
	publishApplicationReadyEvent(factory, 80, null);
	instance.setServicePath("/servicePath/");

	Application app = factory.createApplication();
	assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/servicePath/app/actuator");
	assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/servicePath/app/actuator/health");
	assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/servicePath/app");
}
 
Example 22
AbstractEndpointTests(EndpointId endpointId) {
	this.endpointId = endpointId;
}
 
Example 23
void assertActuatorEndpointLoaded(Class<?> testConfigClass) {
	load(testConfigClass, (discoverer) -> {
		Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
		assertThat(endpoints).containsOnlyKeys(endpointId);
	});
}
 
Example 24
void assertActuatorEndpointNotLoaded(Class<?> testConfigClass) {
	load(testConfigClass, (discoverer) -> {
		Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
		assertThat(endpoints).doesNotContainKey(endpointId);
	});
}
 
Example 25
private void load(Class<?> configuration, Consumer<WebEndpointDiscoverer> consumer) {
	this.load((id) -> null, EndpointId::toString, configuration, consumer);
}
 
Example 26
private Map<EndpointId, ExposableWebEndpoint> mapEndpoints(Collection<ExposableWebEndpoint> endpoints) {
	Map<EndpointId, ExposableWebEndpoint> endpointById = new HashMap<>();
	endpoints.forEach((endpoint) -> endpointById.put(endpoint.getEndpointId(), endpoint));
	return endpointById;
}