add hasAttributesSatisfying overload to AbstractPointAssert (#6048)
This commit is contained in:
parent
f1fc1c75b8
commit
3bb221fb5d
|
@ -1,2 +1,5 @@
|
|||
Comparing source compatibility of against
|
||||
No changes.
|
||||
*** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.testing.assertj.AbstractPointAssert (not serializable)
|
||||
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
|
||||
GENERIC TEMPLATES: === PointAssertT:io.opentelemetry.sdk.testing.assertj.AbstractPointAssert<PointAssertT,PointT><PointAssertT,PointT>, === PointT:io.opentelemetry.sdk.metrics.data.PointData
|
||||
+++ NEW METHOD: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.AbstractPointAssert hasAttributesSatisfying(java.util.function.Consumer<io.opentelemetry.api.common.Attributes>)
|
||||
|
|
|
@ -14,6 +14,7 @@ import io.opentelemetry.sdk.metrics.data.PointData;
|
|||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import javax.annotation.Nullable;
|
||||
import org.assertj.core.api.AbstractAssert;
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
@ -111,6 +112,13 @@ public abstract class AbstractPointAssert<
|
|||
return myself;
|
||||
}
|
||||
|
||||
/** Asserts the point has attributes satisfying the given condition. */
|
||||
public final PointAssertT hasAttributesSatisfying(Consumer<Attributes> attributes) {
|
||||
isNotNull();
|
||||
assertThat(actual.getAttributes()).as("attributes").satisfies(attributes);
|
||||
return myself;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the point has attributes matching all {@code assertions} and no more. Assertions can be
|
||||
* created using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
package io.opentelemetry.sdk.testing.assertj;
|
||||
|
||||
import static io.opentelemetry.api.common.AttributeKey.stringKey;
|
||||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
|
||||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry;
|
||||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
|
||||
|
@ -60,9 +61,9 @@ class MetricAssertionsTest {
|
|||
private static final InstrumentationScopeInfo INSTRUMENTATION_SCOPE_INFO =
|
||||
InstrumentationScopeInfo.builder("opentelemetry").setVersion("1.0").build();
|
||||
|
||||
private static final AttributeKey<String> DOG = AttributeKey.stringKey("dog");
|
||||
private static final AttributeKey<String> BEAR = AttributeKey.stringKey("bear");
|
||||
private static final AttributeKey<String> CAT = AttributeKey.stringKey("cat");
|
||||
private static final AttributeKey<String> DOG = stringKey("dog");
|
||||
private static final AttributeKey<String> BEAR = stringKey("bear");
|
||||
private static final AttributeKey<String> CAT = stringKey("cat");
|
||||
private static final AttributeKey<Boolean> WARM = AttributeKey.booleanKey("warm");
|
||||
private static final AttributeKey<Long> TEMPERATURE = AttributeKey.longKey("temperature");
|
||||
private static final AttributeKey<Double> LENGTH = AttributeKey.doubleKey("length");
|
||||
|
@ -317,7 +318,7 @@ class MetricAssertionsTest {
|
|||
attributes ->
|
||||
assertThat(attributes)
|
||||
.hasSize(2)
|
||||
.containsEntry(AttributeKey.stringKey("dog"), "bark")
|
||||
.containsEntry(stringKey("dog"), "bark")
|
||||
.hasEntrySatisfying(DOG, value -> assertThat(value).hasSize(4))
|
||||
.hasEntrySatisfying(
|
||||
AttributeKey.booleanKey("dog is cute"),
|
||||
|
@ -454,7 +455,19 @@ class MetricAssertionsTest {
|
|||
equalTo(CONDITIONS, Arrays.asList(false, true)),
|
||||
equalTo(SCORES, Arrays.asList(0L, 1L)),
|
||||
equalTo(COINS, Arrays.asList(0.01, 0.05, 0.1)),
|
||||
satisfies(LENGTH, val -> val.isCloseTo(1, offset(0.3))))));
|
||||
satisfies(LENGTH, val -> val.isCloseTo(1, offset(0.3))))
|
||||
.hasAttributesSatisfying(
|
||||
attributes ->
|
||||
assertThat(attributes)
|
||||
.hasSize(8)
|
||||
.containsEntry(stringKey("bear"), "mya")
|
||||
.containsEntry("warm", true)
|
||||
.containsEntry("temperature", 30L)
|
||||
.containsEntry("colors", "red", "blue")
|
||||
.containsEntry("conditions", false, true)
|
||||
.containsEntry("scores", 0L, 1L)
|
||||
.containsEntry("coins", 0.01, 0.05, 0.1)
|
||||
.containsEntry("length", 1.2))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -500,7 +513,7 @@ class MetricAssertionsTest {
|
|||
resource.hasAttributesSatisfying(
|
||||
attributes ->
|
||||
assertThat(attributes)
|
||||
.containsEntry(AttributeKey.stringKey("dog"), "meow"))))
|
||||
.containsEntry(stringKey("dog"), "meow"))))
|
||||
.isInstanceOf(AssertionError.class);
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
|
@ -774,6 +787,29 @@ class MetricAssertionsTest {
|
|||
satisfies(
|
||||
COINS, val -> val.containsExactly(0.01, 0.05, 0.1))))))
|
||||
.isInstanceOf(AssertionError.class);
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
assertThat(DOUBLE_GAUGE_METRIC)
|
||||
.hasDoubleGaugeSatisfying(
|
||||
gauge ->
|
||||
gauge.hasPointsSatisfying(
|
||||
point -> point.hasAttributes(Attributes.empty()),
|
||||
point ->
|
||||
point.hasAttributesSatisfying(
|
||||
attributes ->
|
||||
assertThat(attributes)
|
||||
.hasSize(8)
|
||||
.containsEntry(
|
||||
stringKey("bear"),
|
||||
"WRONG BEAR NAME") // Failed here
|
||||
.containsEntry("warm", true)
|
||||
.containsEntry("temperature", 30L)
|
||||
.containsEntry("colors", "red", "blue")
|
||||
.containsEntry("conditions", false, true)
|
||||
.containsEntry("scores", 0L, 1L)
|
||||
.containsEntry("coins", 0.01, 0.05, 0.1)
|
||||
.containsEntry("length", 1.2)))))
|
||||
.isInstanceOf(AssertionError.class);
|
||||
}
|
||||
|
||||
// The above tests verify shared behavior in AbstractPointDataAssert and MetricDataAssert so we
|
||||
|
|
Loading…
Reference in New Issue