Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit b8c1683

Browse files
committed
gh-3 Add Metrics tests
- add tests for each micro-service to verify that the Coherence metrics REST endpoint works - update Spring Boot version to `2.5.6`
1 parent 6871939 commit b8c1683

File tree

14 files changed

+509
-20
lines changed

14 files changed

+509
-20
lines changed

carts/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@
9595
<version>${version.lib.rest-assured}</version>
9696
<scope>test</scope>
9797
</dependency>
98+
<dependency>
99+
<groupId>org.springframework.boot</groupId>
100+
<artifactId>spring-boot-starter-webflux</artifactId>
101+
<scope>test</scope>
102+
</dependency>
98103
</dependencies>
99104

100105
<build>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* https://oss.oracle.com/licenses/upl.
6+
*/
7+
package com.oracle.coherence.examples.sockshop.spring.carts;
8+
9+
import org.junit.jupiter.api.MethodOrderer;
10+
import org.junit.jupiter.api.Order;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.TestMethodOrder;
13+
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
16+
import org.springframework.boot.test.context.SpringBootTest;
17+
import org.springframework.http.MediaType;
18+
import org.springframework.test.annotation.DirtiesContext;
19+
import org.springframework.test.web.reactive.server.WebTestClient;
20+
21+
/**
22+
* Integration test to ensure Coherence metrics are properly exposed when
23+
* property {@code coherence.metrics.http.enabled} is set to {@code true}.
24+
*
25+
* @author Gunnar Hillert
26+
*/
27+
@SpringBootTest(
28+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
29+
properties = {
30+
"coherence.metrics.http.enabled=true"
31+
}
32+
)
33+
@AutoConfigureWebTestClient
34+
@DirtiesContext
35+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
36+
public class CartMetricsTests {
37+
38+
public static final int COHERENCE_METRICS_PORT = 9612;
39+
public static final String COHERENCE_METRICS_URL = "http://localhost:" + COHERENCE_METRICS_PORT + "/metrics";
40+
public static final String CART_URL = "/carts/{cartId}/items";
41+
public static final String CART_ID = "C1";
42+
43+
@Autowired
44+
protected WebTestClient webTestClient;
45+
46+
@Test
47+
@Order(1)
48+
void addItemToCart() {
49+
webTestClient.post()
50+
.uri(CART_URL, CART_ID)
51+
.bodyValue(new Item("X1", 0, 10f))
52+
.exchange()
53+
.expectStatus().isCreated()
54+
.expectBody().jsonPath("itemId").isEqualTo("X1");
55+
}
56+
57+
@Test
58+
@Order(2)
59+
void verifyCoherenceMetrics() {
60+
this.webTestClient.get()
61+
.uri(COHERENCE_METRICS_URL + "/Coherence.Cache.Size?name=carts&tier=back")
62+
.accept(MediaType.APPLICATION_JSON)
63+
.exchange()
64+
.expectStatus().isOk()
65+
.expectBody()
66+
.consumeWith(System.out::println)
67+
.jsonPath("$.length()").isEqualTo(1)
68+
.jsonPath("$[0].tags.name").isEqualTo("carts")
69+
.jsonPath("$[0].value").isEqualTo(1);
70+
}
71+
}

catalog/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@
102102
<version>${version.lib.rest-assured}</version>
103103
<scope>test</scope>
104104
</dependency>
105+
<dependency>
106+
<groupId>org.springframework.boot</groupId>
107+
<artifactId>spring-boot-starter-webflux</artifactId>
108+
<scope>test</scope>
109+
</dependency>
105110
</dependencies>
106111

107112
<build>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* https://oss.oracle.com/licenses/upl.
6+
*/
7+
package com.oracle.coherence.examples.sockshop.spring.catalog;
8+
9+
import com.oracle.coherence.spring.configuration.annotation.CoherenceCache;
10+
import com.tangosol.net.NamedCache;
11+
import org.junit.jupiter.api.MethodOrderer;
12+
import org.junit.jupiter.api.Order;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.TestMethodOrder;
15+
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.http.MediaType;
20+
import org.springframework.test.annotation.DirtiesContext;
21+
import org.springframework.test.web.reactive.server.WebTestClient;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Integration test to ensure Coherence metrics are properly exposed when
27+
* property {@code coherence.metrics.http.enabled} is set to {@code true}.
28+
*
29+
* @author Gunnar Hillert
30+
*/
31+
@SpringBootTest(
32+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
33+
properties = {
34+
"coherence.metrics.http.enabled=true"
35+
}
36+
)
37+
@AutoConfigureWebTestClient
38+
@DirtiesContext
39+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
40+
public class CatalogMetricsTests {
41+
42+
@CoherenceCache
43+
private NamedCache<String, Sock> socks;
44+
45+
public static final int COHERENCE_METRICS_PORT = 9612;
46+
public static final String COHERENCE_METRICS_URL = "http://localhost:" + COHERENCE_METRICS_PORT + "/metrics";
47+
48+
@Autowired
49+
protected WebTestClient webTestClient;
50+
51+
@Test
52+
@Order(1)
53+
void addSocksToCatalog() {
54+
assertThat(this.socks.size()).isEqualTo(9);
55+
this.socks.put("example", new Sock());
56+
this.socks.put("another", new Sock());
57+
assertThat(this.socks.size()).isEqualTo(11);
58+
}
59+
60+
@Test
61+
@Order(2)
62+
void verifyCoherenceMetrics() {
63+
this.webTestClient.get()
64+
.uri(COHERENCE_METRICS_URL + "/Coherence.Cache.Size?name=socks&tier=back")
65+
.accept(MediaType.APPLICATION_JSON)
66+
.exchange()
67+
.expectStatus().isOk()
68+
.expectBody()
69+
.consumeWith(System.out::println)
70+
.jsonPath("$.length()").isEqualTo(1)
71+
.jsonPath("$[0].tags.name").isEqualTo("socks")
72+
.jsonPath("$[0].value").isEqualTo(11);
73+
}
74+
}

orders/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@
8888
<version>${version.lib.coherence-spring}</version>
8989
<scope>test</scope>
9090
</dependency>
91-
91+
<dependency>
92+
<groupId>org.springframework.boot</groupId>
93+
<artifactId>spring-boot-starter-webflux</artifactId>
94+
<scope>test</scope>
95+
</dependency>
9296
<dependency>
9397
<groupId>org.junit.jupiter</groupId>
9498
<artifactId>junit-jupiter-api</artifactId>

orders/src/test/java/com/oracle/coherence/examples/sockshop/spring/orders/CoherenceOrderResourceIT.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.net.URI;
2525
import java.time.LocalDate;
2626

27+
import static com.oracle.coherence.examples.sockshop.spring.orders.TestDataFactory.order;
2728
import static io.restassured.RestAssured.get;
2829
import static io.restassured.RestAssured.given;
2930
import static io.restassured.RestAssured.when;
@@ -74,7 +75,7 @@ protected void testGetMissingOrder() {
7475

7576
@Test
7677
protected void testGetOrder() {
77-
Order order = TestDataFactory.order("homer", 1);
78+
Order order = order("homer", 1);
7879
orders.saveOrder(order);
7980
Order saved = get("/orders/{orderId}", order.getOrderId()).as(Order.class, ObjectMapperType.JACKSON_2);
8081

@@ -83,9 +84,9 @@ protected void testGetOrder() {
8384

8485
@Test
8586
protected void testFindOrdersByCustomerId() {
86-
orders.saveOrder(TestDataFactory.order("homer", 1));
87-
orders.saveOrder(TestDataFactory.order("homer", 2));
88-
orders.saveOrder(TestDataFactory.order("marge", 5));
87+
orders.saveOrder(order("homer", 1));
88+
orders.saveOrder(order("homer", 2));
89+
orders.saveOrder(order("marge", 5));
8990

9091
given().
9192
queryParam("custId", "homer").
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* https://oss.oracle.com/licenses/upl.
6+
*/
7+
package com.oracle.coherence.examples.sockshop.spring.orders;
8+
9+
import java.net.URI;
10+
11+
import com.oracle.coherence.examples.sockshop.spring.orders.controller.support.NewOrderRequest;
12+
import org.junit.jupiter.api.MethodOrderer;
13+
import org.junit.jupiter.api.Order;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.TestMethodOrder;
16+
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
19+
import org.springframework.boot.test.context.SpringBootTest;
20+
import org.springframework.http.MediaType;
21+
import org.springframework.test.annotation.DirtiesContext;
22+
import org.springframework.test.web.reactive.server.WebTestClient;
23+
24+
/**
25+
* Integration test to ensure Coherence metrics are properly exposed when
26+
* property {@code coherence.metrics.http.enabled} is set to {@code true}.
27+
*
28+
* @author Gunnar Hillert
29+
*/
30+
@SpringBootTest(
31+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
32+
properties = {
33+
"coherence.metrics.http.enabled=true"
34+
}
35+
)
36+
@AutoConfigureWebTestClient
37+
@DirtiesContext
38+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
39+
public class OrdersMetricsTests {
40+
41+
public static final int COHERENCE_METRICS_PORT = 9612;
42+
public static final String COHERENCE_METRICS_URL = "http://localhost:" + COHERENCE_METRICS_PORT + "/metrics";
43+
public static final String ORDERS_URL = "/orders";
44+
45+
@Autowired
46+
protected WebTestClient webTestClient;
47+
48+
@Test
49+
@Order(1)
50+
void createOrder() {
51+
String baseUri = "";
52+
NewOrderRequest req = NewOrderRequest.builder()
53+
.customer(URI.create(baseUri + "/customers/homer"))
54+
.address(URI.create(baseUri + "/addresses/homer:1"))
55+
.card(URI.create(baseUri + "/cards/homer:1234"))
56+
.items(URI.create(baseUri + "/carts/homer/items"))
57+
.build();
58+
59+
webTestClient.post()
60+
.uri(ORDERS_URL)
61+
.bodyValue(req)
62+
.exchange()
63+
.expectStatus().isCreated()
64+
.expectBody()
65+
.jsonPath("total").isEqualTo(14.0f)
66+
.jsonPath("status").isEqualTo("CREATED");
67+
}
68+
69+
@Test
70+
@Order(2)
71+
void verifyCoherenceMetrics() {
72+
this.webTestClient.get()
73+
.uri(COHERENCE_METRICS_URL + "/Coherence.Cache.Size?name=orders&tier=back")
74+
.accept(MediaType.APPLICATION_JSON)
75+
.exchange()
76+
.expectStatus().isOk()
77+
.expectBody()
78+
.consumeWith(System.out::println)
79+
.jsonPath("$.length()").isEqualTo(1)
80+
.jsonPath("$[0].tags.name").isEqualTo("orders")
81+
.jsonPath("$[0].value").isEqualTo(1);
82+
}
83+
}

payment/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@
9595
<version>${version.lib.rest-assured}</version>
9696
<scope>test</scope>
9797
</dependency>
98+
<dependency>
99+
<groupId>org.springframework.boot</groupId>
100+
<artifactId>spring-boot-starter-webflux</artifactId>
101+
<scope>test</scope>
102+
</dependency>
98103
</dependencies>
99104

100105
<build>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* https://oss.oracle.com/licenses/upl.
6+
*/
7+
package com.oracle.coherence.examples.sockshop.spring.payment;
8+
9+
import org.junit.jupiter.api.MethodOrderer;
10+
import org.junit.jupiter.api.Order;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.TestMethodOrder;
13+
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
16+
import org.springframework.boot.test.context.SpringBootTest;
17+
import org.springframework.http.MediaType;
18+
import org.springframework.test.annotation.DirtiesContext;
19+
import org.springframework.test.web.reactive.server.WebTestClient;
20+
21+
import static com.oracle.coherence.examples.sockshop.spring.payment.TestDataFactory.paymentRequest;
22+
23+
/**
24+
* Integration test to ensure Coherence metrics are properly exposed when
25+
* property {@code coherence.metrics.http.enabled} is set to {@code true}.
26+
*
27+
* @author Gunnar Hillert
28+
*/
29+
@SpringBootTest(
30+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
31+
properties = {
32+
"coherence.metrics.http.enabled=true"
33+
}
34+
)
35+
@AutoConfigureWebTestClient
36+
@DirtiesContext
37+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
38+
public class PaymentMetricsTests {
39+
40+
public static final int COHERENCE_METRICS_PORT = 9612;
41+
public static final String COHERENCE_METRICS_URL = "http://localhost:" + COHERENCE_METRICS_PORT + "/metrics";
42+
public static final String PAYMENTS_URL = "/payments";
43+
44+
@Autowired
45+
protected WebTestClient webTestClient;
46+
47+
@Test
48+
@Order(1)
49+
void createPayment() {
50+
webTestClient.post()
51+
.uri(PAYMENTS_URL)
52+
.bodyValue(paymentRequest("A123", 50))
53+
.exchange()
54+
.expectStatus().isOk()
55+
.expectBody()
56+
.jsonPath("authorised").isEqualTo(true)
57+
.jsonPath("message").isEqualTo("Payment authorized.");
58+
}
59+
60+
@Test
61+
@Order(2)
62+
void verifyCoherenceMetrics() {
63+
this.webTestClient.get()
64+
.uri(COHERENCE_METRICS_URL + "/Coherence.Cache.Size?name=payments&tier=back")
65+
.accept(MediaType.APPLICATION_JSON)
66+
.exchange()
67+
.expectStatus().isOk()
68+
.expectBody()
69+
.consumeWith(System.out::println)
70+
.jsonPath("$.length()").isEqualTo(1)
71+
.jsonPath("$[0].tags.name").isEqualTo("payments")
72+
.jsonPath("$[0].value").isEqualTo(1);
73+
}
74+
}

0 commit comments

Comments
 (0)