Skip to content

Commit 2fe0a14

Browse files
authored
Add containsPointsSatisfying to metric data asserts (#8329)
1 parent 3751720 commit 2fe0a14

10 files changed

Lines changed: 289 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#### Testing
88

99
* Add `hasValueSatisfying` to `LongPointAssert` and `DoublePointAssert` for fuzzy value matching
10+
* Add `containsPointsSatisfying` to metric data asserts for "each given assertion must be
11+
satisfied by at least one point, extras allowed" checks on sum, gauge, histogram, exponential
12+
histogram, and summary data
1013

1114
## Version 1.61.0 (2026-04-10)
1215

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
Comparing source compatibility of opentelemetry-sdk-testing-1.62.0-SNAPSHOT.jar against opentelemetry-sdk-testing-1.61.0.jar
2+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.DoubleGaugeAssert (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.DoubleGaugeAssert containsPointsSatisfying(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.DoublePointAssert>)
5+
+++ NEW ANNOTATION: java.lang.SafeVarargs
6+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.DoubleGaugeAssert containsPointsSatisfying(java.lang.Iterable<? extends java.util.function.Consumer<? extends io.opentelemetry.sdk.testing.assertj.DoublePointAssert>>)
27
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.DoublePointAssert (not serializable)
38
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
49
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.DoublePointAssert hasValueSatisfying(java.util.function.Consumer<org.assertj.core.api.AbstractDoubleAssert<?>>)
10+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.DoubleSumAssert (not serializable)
11+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
12+
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.DoubleSumAssert containsPointsSatisfying(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.DoublePointAssert>)
13+
+++ NEW ANNOTATION: java.lang.SafeVarargs
14+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.DoubleSumAssert containsPointsSatisfying(java.lang.Iterable<? extends java.util.function.Consumer<? extends io.opentelemetry.sdk.testing.assertj.DoublePointAssert>>)
15+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.ExponentialHistogramAssert (not serializable)
16+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
17+
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.ExponentialHistogramAssert containsPointsSatisfying(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.ExponentialHistogramPointAssert>)
18+
+++ NEW ANNOTATION: java.lang.SafeVarargs
19+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ExponentialHistogramAssert containsPointsSatisfying(java.lang.Iterable<? extends java.util.function.Consumer<? extends io.opentelemetry.sdk.testing.assertj.ExponentialHistogramPointAssert>>)
20+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.HistogramAssert (not serializable)
21+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
22+
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.HistogramAssert containsPointsSatisfying(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.HistogramPointAssert>)
23+
+++ NEW ANNOTATION: java.lang.SafeVarargs
24+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.HistogramAssert containsPointsSatisfying(java.lang.Iterable<? extends java.util.function.Consumer<? extends io.opentelemetry.sdk.testing.assertj.HistogramPointAssert>>)
25+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.LongGaugeAssert (not serializable)
26+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
27+
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.LongGaugeAssert containsPointsSatisfying(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.LongPointAssert>)
28+
+++ NEW ANNOTATION: java.lang.SafeVarargs
29+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.LongGaugeAssert containsPointsSatisfying(java.lang.Iterable<? extends java.util.function.Consumer<? extends io.opentelemetry.sdk.testing.assertj.LongPointAssert>>)
530
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.LongPointAssert (not serializable)
631
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
732
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.LongPointAssert hasValueSatisfying(java.util.function.Consumer<org.assertj.core.api.AbstractLongAssert<?>>)
33+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.LongSumAssert (not serializable)
34+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
35+
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.LongSumAssert containsPointsSatisfying(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.LongPointAssert>)
36+
+++ NEW ANNOTATION: java.lang.SafeVarargs
37+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.LongSumAssert containsPointsSatisfying(java.lang.Iterable<? extends java.util.function.Consumer<? extends io.opentelemetry.sdk.testing.assertj.LongPointAssert>>)
38+
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.SummaryAssert (not serializable)
39+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
40+
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.SummaryAssert containsPointsSatisfying(java.util.function.Consumer[]<io.opentelemetry.sdk.testing.assertj.SummaryPointAssert>)
41+
+++ NEW ANNOTATION: java.lang.SafeVarargs
42+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.SummaryAssert containsPointsSatisfying(java.lang.Iterable<? extends java.util.function.Consumer<? extends io.opentelemetry.sdk.testing.assertj.SummaryPointAssert>>)

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/DoubleGaugeAssert.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,31 @@ public DoubleGaugeAssert hasPointsSatisfying(
4545
.satisfiesExactlyInAnyOrder(AssertUtil.toConsumers(assertions, DoublePointAssert::new));
4646
return this;
4747
}
48+
49+
/**
50+
* Asserts that for each given assertion, at least one point in the gauge satisfies it. Extra
51+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
52+
* assertions.
53+
*/
54+
@SafeVarargs
55+
@SuppressWarnings("varargs")
56+
public final DoubleGaugeAssert containsPointsSatisfying(
57+
Consumer<DoublePointAssert>... assertions) {
58+
return containsPointsSatisfying(Arrays.asList(assertions));
59+
}
60+
61+
/**
62+
* Asserts that for each given assertion, at least one point in the gauge satisfies it. Extra
63+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
64+
* assertions.
65+
*/
66+
public DoubleGaugeAssert containsPointsSatisfying(
67+
Iterable<? extends Consumer<DoublePointAssert>> assertions) {
68+
isNotNull();
69+
for (Consumer<DoublePointAssert> assertion : assertions) {
70+
assertThat(actual.getPoints())
71+
.anySatisfy(point -> assertion.accept(new DoublePointAssert(point)));
72+
}
73+
return this;
74+
}
4875
}

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/DoubleSumAssert.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,30 @@ public DoubleSumAssert hasPointsSatisfying(
8888
.satisfiesExactlyInAnyOrder(AssertUtil.toConsumers(assertions, DoublePointAssert::new));
8989
return this;
9090
}
91+
92+
/**
93+
* Asserts that for each given assertion, at least one point in the sum satisfies it. Extra points
94+
* that match none of the assertions are allowed, and a single point may satisfy multiple
95+
* assertions.
96+
*/
97+
@SafeVarargs
98+
@SuppressWarnings("varargs")
99+
public final DoubleSumAssert containsPointsSatisfying(Consumer<DoublePointAssert>... assertions) {
100+
return containsPointsSatisfying(Arrays.asList(assertions));
101+
}
102+
103+
/**
104+
* Asserts that for each given assertion, at least one point in the sum satisfies it. Extra points
105+
* that match none of the assertions are allowed, and a single point may satisfy multiple
106+
* assertions.
107+
*/
108+
public DoubleSumAssert containsPointsSatisfying(
109+
Iterable<? extends Consumer<DoublePointAssert>> assertions) {
110+
isNotNull();
111+
for (Consumer<DoublePointAssert> assertion : assertions) {
112+
assertThat(actual.getPoints())
113+
.anySatisfy(point -> assertion.accept(new DoublePointAssert(point)));
114+
}
115+
return this;
116+
}
91117
}

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/ExponentialHistogramAssert.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,31 @@ public ExponentialHistogramAssert hasPointsSatisfying(
7474
AssertUtil.toConsumers(assertions, ExponentialHistogramPointAssert::new));
7575
return this;
7676
}
77+
78+
/**
79+
* Asserts that for each given assertion, at least one point in the exponential histogram
80+
* satisfies it. Extra points that match none of the assertions are allowed, and a single point
81+
* may satisfy multiple assertions.
82+
*/
83+
@SafeVarargs
84+
@SuppressWarnings("varargs")
85+
public final ExponentialHistogramAssert containsPointsSatisfying(
86+
Consumer<ExponentialHistogramPointAssert>... assertions) {
87+
return containsPointsSatisfying(Arrays.asList(assertions));
88+
}
89+
90+
/**
91+
* Asserts that for each given assertion, at least one point in the exponential histogram
92+
* satisfies it. Extra points that match none of the assertions are allowed, and a single point
93+
* may satisfy multiple assertions.
94+
*/
95+
public ExponentialHistogramAssert containsPointsSatisfying(
96+
Iterable<? extends Consumer<ExponentialHistogramPointAssert>> assertions) {
97+
isNotNull();
98+
for (Consumer<ExponentialHistogramPointAssert> assertion : assertions) {
99+
assertThat(actual.getPoints())
100+
.anySatisfy(point -> assertion.accept(new ExponentialHistogramPointAssert(point)));
101+
}
102+
return this;
103+
}
77104
}

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/HistogramAssert.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,31 @@ public HistogramAssert hasPointsSatisfying(
7272
.satisfiesExactlyInAnyOrder(AssertUtil.toConsumers(assertions, HistogramPointAssert::new));
7373
return this;
7474
}
75+
76+
/**
77+
* Asserts that for each given assertion, at least one point in the histogram satisfies it. Extra
78+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
79+
* assertions.
80+
*/
81+
@SafeVarargs
82+
@SuppressWarnings("varargs")
83+
public final HistogramAssert containsPointsSatisfying(
84+
Consumer<HistogramPointAssert>... assertions) {
85+
return containsPointsSatisfying(Arrays.asList(assertions));
86+
}
87+
88+
/**
89+
* Asserts that for each given assertion, at least one point in the histogram satisfies it. Extra
90+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
91+
* assertions.
92+
*/
93+
public HistogramAssert containsPointsSatisfying(
94+
Iterable<? extends Consumer<HistogramPointAssert>> assertions) {
95+
isNotNull();
96+
for (Consumer<HistogramPointAssert> assertion : assertions) {
97+
assertThat(actual.getPoints())
98+
.anySatisfy(point -> assertion.accept(new HistogramPointAssert(point)));
99+
}
100+
return this;
101+
}
75102
}

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/LongGaugeAssert.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,30 @@ public LongGaugeAssert hasPointsSatisfying(
4444
.satisfiesExactlyInAnyOrder(AssertUtil.toConsumers(assertions, LongPointAssert::new));
4545
return this;
4646
}
47+
48+
/**
49+
* Asserts that for each given assertion, at least one point in the gauge satisfies it. Extra
50+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
51+
* assertions.
52+
*/
53+
@SafeVarargs
54+
@SuppressWarnings("varargs")
55+
public final LongGaugeAssert containsPointsSatisfying(Consumer<LongPointAssert>... assertions) {
56+
return containsPointsSatisfying(Arrays.asList(assertions));
57+
}
58+
59+
/**
60+
* Asserts that for each given assertion, at least one point in the gauge satisfies it. Extra
61+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
62+
* assertions.
63+
*/
64+
public LongGaugeAssert containsPointsSatisfying(
65+
Iterable<? extends Consumer<LongPointAssert>> assertions) {
66+
isNotNull();
67+
for (Consumer<LongPointAssert> assertion : assertions) {
68+
assertThat(actual.getPoints())
69+
.anySatisfy(point -> assertion.accept(new LongPointAssert(point)));
70+
}
71+
return this;
72+
}
4773
}

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/LongSumAssert.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,30 @@ public LongSumAssert hasPointsSatisfying(
8787
.satisfiesExactlyInAnyOrder(AssertUtil.toConsumers(assertions, LongPointAssert::new));
8888
return this;
8989
}
90+
91+
/**
92+
* Asserts that for each given assertion, at least one point in the sum satisfies it. Extra points
93+
* that match none of the assertions are allowed, and a single point may satisfy multiple
94+
* assertions.
95+
*/
96+
@SafeVarargs
97+
@SuppressWarnings("varargs")
98+
public final LongSumAssert containsPointsSatisfying(Consumer<LongPointAssert>... assertions) {
99+
return containsPointsSatisfying(Arrays.asList(assertions));
100+
}
101+
102+
/**
103+
* Asserts that for each given assertion, at least one point in the sum satisfies it. Extra points
104+
* that match none of the assertions are allowed, and a single point may satisfy multiple
105+
* assertions.
106+
*/
107+
public LongSumAssert containsPointsSatisfying(
108+
Iterable<? extends Consumer<LongPointAssert>> assertions) {
109+
isNotNull();
110+
for (Consumer<LongPointAssert> assertion : assertions) {
111+
assertThat(actual.getPoints())
112+
.anySatisfy(point -> assertion.accept(new LongPointAssert(point)));
113+
}
114+
return this;
115+
}
90116
}

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/SummaryAssert.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,30 @@ public SummaryAssert hasPointsSatisfying(
5252
.satisfiesExactlyInAnyOrder(AssertUtil.toConsumers(assertions, SummaryPointAssert::new));
5353
return this;
5454
}
55+
56+
/**
57+
* Asserts that for each given assertion, at least one point in the summary satisfies it. Extra
58+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
59+
* assertions.
60+
*/
61+
@SafeVarargs
62+
@SuppressWarnings("varargs")
63+
public final SummaryAssert containsPointsSatisfying(Consumer<SummaryPointAssert>... assertions) {
64+
return containsPointsSatisfying(Arrays.asList(assertions));
65+
}
66+
67+
/**
68+
* Asserts that for each given assertion, at least one point in the summary satisfies it. Extra
69+
* points that match none of the assertions are allowed, and a single point may satisfy multiple
70+
* assertions.
71+
*/
72+
public SummaryAssert containsPointsSatisfying(
73+
Iterable<? extends Consumer<SummaryPointAssert>> assertions) {
74+
isNotNull();
75+
for (Consumer<SummaryPointAssert> assertion : assertions) {
76+
assertThat(actual.getPoints())
77+
.anySatisfy(point -> assertion.accept(new SummaryPointAssert(point)));
78+
}
79+
return this;
80+
}
5581
}

sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/MetricAssertionsTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,4 +1299,70 @@ void summary_failure() {
12991299
point -> point.hasValuesSatisfying(value -> value.hasValue(3.0)))))
13001300
.isInstanceOf(AssertionError.class);
13011301
}
1302+
1303+
@Test
1304+
void containsPointsSatisfying() {
1305+
assertThat(DOUBLE_GAUGE_METRIC)
1306+
.hasDoubleGaugeSatisfying(
1307+
gauge -> gauge.containsPointsSatisfying(point -> point.hasValue(3.0)));
1308+
assertThat(LONG_GAUGE_METRIC)
1309+
.hasLongGaugeSatisfying(
1310+
gauge ->
1311+
gauge.containsPointsSatisfying(
1312+
point -> point.hasValue(Long.MAX_VALUE), point -> point.hasValue(1)));
1313+
assertThat(DOUBLE_SUM_METRIC)
1314+
.hasDoubleSumSatisfying(sum -> sum.containsPointsSatisfying(point -> point.hasValue(3.0)));
1315+
assertThat(LONG_SUM_METRIC)
1316+
.hasLongSumSatisfying(
1317+
sum -> sum.containsPointsSatisfying(point -> point.hasValue(Long.MAX_VALUE)));
1318+
assertThat(HISTOGRAM_METRIC)
1319+
.hasHistogramSatisfying(
1320+
histogram -> histogram.containsPointsSatisfying(point -> point.hasSum(15)));
1321+
assertThat(EXPONENTIAL_HISTOGRAM_METRIC)
1322+
.hasExponentialHistogramSatisfying(
1323+
histogram -> histogram.containsPointsSatisfying(point -> point.hasSum(10.0)));
1324+
assertThat(SUMMARY_METRIC)
1325+
.hasSummarySatisfying(
1326+
summary -> summary.containsPointsSatisfying(point -> point.hasSum(2.0)));
1327+
}
1328+
1329+
@Test
1330+
void containsPointsSatisfyingFailure() {
1331+
// single assertion that matches no point
1332+
assertThatThrownBy(
1333+
() ->
1334+
assertThat(DOUBLE_GAUGE_METRIC)
1335+
.hasDoubleGaugeSatisfying(
1336+
gauge -> gauge.containsPointsSatisfying(point -> point.hasValue(42.0))))
1337+
.isInstanceOf(AssertionError.class);
1338+
// one of multiple assertions unmatched
1339+
assertThatThrownBy(
1340+
() ->
1341+
assertThat(LONG_GAUGE_METRIC)
1342+
.hasLongGaugeSatisfying(
1343+
gauge ->
1344+
gauge.containsPointsSatisfying(
1345+
point -> point.hasValue(Long.MAX_VALUE),
1346+
point -> point.hasValue(42))))
1347+
.isInstanceOf(AssertionError.class);
1348+
assertThatThrownBy(
1349+
() ->
1350+
assertThat(HISTOGRAM_METRIC)
1351+
.hasHistogramSatisfying(
1352+
histogram -> histogram.containsPointsSatisfying(point -> point.hasSum(42))))
1353+
.isInstanceOf(AssertionError.class);
1354+
assertThatThrownBy(
1355+
() ->
1356+
assertThat(EXPONENTIAL_HISTOGRAM_METRIC)
1357+
.hasExponentialHistogramSatisfying(
1358+
histogram ->
1359+
histogram.containsPointsSatisfying(point -> point.hasSum(42.0))))
1360+
.isInstanceOf(AssertionError.class);
1361+
assertThatThrownBy(
1362+
() ->
1363+
assertThat(SUMMARY_METRIC)
1364+
.hasSummarySatisfying(
1365+
summary -> summary.containsPointsSatisfying(point -> point.hasSum(42.0))))
1366+
.isInstanceOf(AssertionError.class);
1367+
}
13021368
}

0 commit comments

Comments
 (0)