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>
This commit is contained in:
Mateusz Rzeszutek 2022-08-26 05:05:14 +02:00 committed by GitHub
parent 160af1c480
commit 169554bb50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 1 deletions

View File

@ -1,2 +1,5 @@
Comparing source compatibility of against
No changes.
*** 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)

View File

@ -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<AttributesAssert, Attributes> {
@ -183,6 +184,29 @@ public final class AttributesAssert extends AbstractAssert<AttributesAssert, Att
return this;
}
/** Asserts the attributes do not contain the given key. */
public AttributesAssert doesNotContainKey(AttributeKey<?> 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());

View File

@ -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)