Update gateway actuator metrics test to use @ClassPathExclusions to verify gateway doesn't fall over when actuator metrics are not added as project dependency
This commit is contained in:
@@ -24,12 +24,6 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
<exclusions> <!-- exclude metrics to verify that gateway doesn't fall over when it is not available -->
|
||||
<exclusion>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -60,6 +54,11 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-test-support</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
|
||||
@@ -45,6 +45,7 @@ import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
@Import(AdditionalRoutes.class)
|
||||
public class GatewaySampleApplication {
|
||||
|
||||
public static final String HELLO_FROM_FAKE_ACTUATOR_METRICS_GATEWAY_REQUESTS = "hello from fake /actuator/metrics/gateway.requests";
|
||||
@Value("${test.uri:http://httpbin.org:80}")
|
||||
String uri;
|
||||
|
||||
@@ -135,6 +136,14 @@ public class GatewaySampleApplication {
|
||||
return route;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> testWhenMetricPathIsNotMeet() {
|
||||
RouterFunction<ServerResponse> route = RouterFunctions.route(
|
||||
RequestPredicates.path("/actuator/metrics/gateway.requests"),
|
||||
request -> ServerResponse.ok().body(BodyInserters.fromObject(HELLO_FROM_FAKE_ACTUATOR_METRICS_GATEWAY_REQUESTS)));
|
||||
return route;
|
||||
}
|
||||
|
||||
static class Hello {
|
||||
String message;
|
||||
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
|
||||
package org.springframework.cloud.gateway.sample;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import org.junit.AfterClass;
|
||||
@@ -43,6 +46,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
@@ -181,6 +185,27 @@ public class GatewaySampleApplicationTests {
|
||||
.expectStatus().isOk();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actuatorMetrics() {
|
||||
contextLoads();
|
||||
webClient.get()
|
||||
.uri("http://localhost:" + managementPort
|
||||
+ "/actuator/metrics/gateway.requests")
|
||||
.exchange().expectStatus().isOk().expectBody().consumeWith(i -> {
|
||||
String body = new String(i.getResponseBodyContent());
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
JsonNode actualObj = mapper.readTree(body);
|
||||
JsonNode findValue = actualObj.findValue("name");
|
||||
assertEquals("Expected to find metric with name gateway.requests",
|
||||
"gateway.requests", findValue.asText());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@RibbonClient(name = "httpbin", configuration = RibbonConfig.class)
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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
|
||||
*
|
||||
* http://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.cloud.gateway.sample;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.gateway.sample.GatewaySampleApplicationTests.TestConfig;
|
||||
import org.springframework.cloud.test.ClassPathExclusions;
|
||||
import org.springframework.cloud.test.ModifiedClassPathRunner;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
@RunWith(ModifiedClassPathRunner.class)
|
||||
@ClassPathExclusions({ "micrometer-*.jar" })
|
||||
@DirtiesContext
|
||||
public class GatewaySampleApplicationWithoutMetricsTests {
|
||||
|
||||
static protected int port;
|
||||
|
||||
protected WebTestClient webClient;
|
||||
protected String baseUri;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
port = SocketUtils.findAvailableTcpPort();
|
||||
System.setProperty("server.port", Integer.toString(port));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
System.clearProperty("server.port");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
baseUri = "http://localhost:" + port;
|
||||
this.webClient = WebTestClient.bindToServer()
|
||||
.responseTimeout(Duration.ofSeconds(10)).baseUrl(baseUri).build();
|
||||
}
|
||||
|
||||
protected ConfigurableApplicationContext init(Class<?> config) {
|
||||
return new SpringApplicationBuilder().web(WebApplicationType.REACTIVE)
|
||||
.sources(GatewaySampleApplication.class, config).run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actuatorMetrics() {
|
||||
init(TestConfig.class);
|
||||
webClient.get().uri("/get").exchange().expectStatus().isOk();
|
||||
webClient.get()
|
||||
.uri("http://localhost:" + port + "/actuator/metrics/gateway.requests")
|
||||
.exchange().expectStatus().isOk().expectBody(String.class).isEqualTo(
|
||||
GatewaySampleApplication.HELLO_FROM_FAKE_ACTUATOR_METRICS_GATEWAY_REQUESTS);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user