Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hasAttributesSatisfying overload to AbstractPointAssert #6048

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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>)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -111,6 +112,13 @@ public final PointAssertT hasAttributesSatisfying(Iterable<AttributeAssertion> a
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -317,7 +318,7 @@ void doubleGauge() {
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"),
Expand Down Expand Up @@ -454,7 +455,19 @@ void doubleGauge() {
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
Expand Down Expand Up @@ -500,7 +513,7 @@ void doubleGaugeFailure() {
resource.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsEntry(AttributeKey.stringKey("dog"), "meow"))))
.containsEntry(stringKey("dog"), "meow"))))
.isInstanceOf(AssertionError.class);
assertThatThrownBy(
() ->
Expand Down Expand Up @@ -774,6 +787,29 @@ void doubleGaugeFailure() {
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
Expand Down