Skip to content

Commit

Permalink
Attribute assertions should always contain the attr key (#5027)
Browse files Browse the repository at this point in the history
* improve attribute assertions to alwyas contain the attr key

* spotless

* change assertion

* Update sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/AttributeAssertionTest.java

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>

* fix build

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>
  • Loading branch information
breedx-splk and jack-berg committed Dec 9, 2022
1 parent ea50096 commit 79a601d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Expand Up @@ -37,6 +37,13 @@ static AttributeAssertion create(
// suppressing here.
@SuppressWarnings("NullAway")
static AbstractAssert<?, ?> attributeValueAssertion(AttributeKey<?> key, @Nullable Object value) {
AbstractAssert<? extends AbstractAssert<?, ?>, ?> abstractAssert = makeAssertion(key, value);
String description = "%s attribute '%s'";
return abstractAssert.as(description, key.getType(), key.getKey());
}

private static AbstractAssert<? extends AbstractAssert<?, ?>, ?> makeAssertion(
AttributeKey<?> key, Object value) {
switch (key.getType()) {
case STRING:
return assertThat((String) value);
Expand Down
@@ -0,0 +1,28 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.testing.assertj;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import io.opentelemetry.api.common.AttributeKey;
import org.assertj.core.api.AbstractAssert;
import org.junit.jupiter.api.Test;

class AttributeAssertionTest {

@Test
void nullAttr_errorMessageContainsAttrName() {
AttributeKey<String> key = AttributeKey.stringKey("flib");

assertThatThrownBy(
() ->
AttributeAssertion.create(key, AbstractAssert::isNotNull)
.getAssertion()
.accept(AttributeAssertion.attributeValueAssertion(key, null)))
.isInstanceOf(AssertionError.class)
.hasMessage("[STRING attribute 'flib'] \nExpecting actual not to be null");
}
}

0 comments on commit 79a601d

Please sign in to comment.