From 169554bb5028bd27f84c5d2cde8293974579eeb4 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Fri, 26 Aug 2022 05:05:14 +0200 Subject: [PATCH] Add AttributesAssert#doesNotContainKey() (#4723) * Add AttributesAssert#doesNotContainKey() * jApiCmp * Update sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/AttributesAssert.java Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com> Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com> --- .../opentelemetry-sdk-testing.txt | 5 +++- .../sdk/testing/assertj/AttributesAssert.java | 24 +++++++++++++++++++ .../testing/assertj/TraceAssertionsTest.java | 16 +++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt index df26146497..8c6945a050 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt @@ -1,2 +1,5 @@ Comparing source compatibility of against -No changes. \ No newline at end of file +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.AttributesAssert (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.AttributesAssert doesNotContainKey(io.opentelemetry.api.common.AttributeKey) + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.AttributesAssert doesNotContainKey(java.lang.String) diff --git a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/AttributesAssert.java b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/AttributesAssert.java index 00a4f19418..5e438f34f4 100644 --- a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/AttributesAssert.java +++ b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/AttributesAssert.java @@ -19,6 +19,7 @@ import java.util.function.Consumer; import javax.annotation.Nullable; import org.assertj.core.api.AbstractAssert; import org.assertj.core.error.ShouldContainKeys; +import org.assertj.core.error.ShouldNotContainKeys; /** Assertions for {@link Attributes}. */ public final class AttributesAssert extends AbstractAssert { @@ -183,6 +184,29 @@ public final class AttributesAssert extends AbstractAssert key) { + if (actual.get(key) != null) { + failWithMessage( + ShouldNotContainKeys.shouldNotContainKeys(actual, Collections.singleton(key)) + .create(info.description(), info.representation())); + } + return this; + } + + /** Asserts the attributes do not contain the given key. */ + public AttributesAssert doesNotContainKey(String key) { + boolean containsKey = + actual.asMap().keySet().stream() + .anyMatch(attributeKey -> attributeKey.getKey().equals(key)); + if (containsKey) { + failWithMessage( + ShouldNotContainKeys.shouldNotContainKeys(actual, Collections.singleton(key)) + .create(info.description(), info.representation())); + } + return this; + } + /** Asserts the attributes have no entries. */ public AttributesAssert isEmpty() { return isEqualTo(Attributes.empty()); diff --git a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java index 9c87b5df11..58b26fe65e 100644 --- a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java +++ b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/TraceAssertionsTest.java @@ -209,6 +209,8 @@ class TraceAssertionsTest { .containsEntryWithDoubleValuesOf("coins", Arrays.asList(0.01, 0.05, 0.1)) .containsKey(AttributeKey.stringKey("bear")) .containsKey("bear") + .doesNotContainKey(AttributeKey.stringKey("cat")) + .doesNotContainKey("cat") .containsOnly( attributeEntry("bear", "mya"), attributeEntry("warm", true), @@ -351,6 +353,20 @@ class TraceAssertionsTest { .hasAttributesSatisfying( attributes -> assertThat(attributes).containsKey("cat"))) .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + assertThat(SPAN1) + .hasAttributesSatisfying( + attributes -> + assertThat(attributes) + .doesNotContainKey(AttributeKey.stringKey("bear")))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + assertThat(SPAN1) + .hasAttributesSatisfying( + attributes -> assertThat(attributes).doesNotContainKey("bear"))) + .isInstanceOf(AssertionError.class); assertThatThrownBy( () -> assertThat(SPAN1)