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

Attribute assertions should always contain the attr key #5027

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,27 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.testing.assertj;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;

import io.opentelemetry.api.common.AttributeKey;
import java.util.function.Consumer;
import org.assertj.core.api.AbstractAssert;
import org.junit.jupiter.api.Test;

class AttributeAssertionTest {

@Test
void nullAttr_errorMessageContainsAttrName() {
AttributeKey<String> key = AttributeKey.stringKey("flib");
AttributeAssertion attrAssertion = AttributeAssertion.create(key, AbstractAssert::isNotNull);
AbstractAssert<?, ?> anAssert = AttributeAssertion.attributeValueAssertion(key, null);
Consumer<AbstractAssert<?, ?>> a = attrAssertion.getAssertion();
String message = assertThrows(AssertionError.class, () -> a.accept(anAssert)).getMessage();
assertThat(message).isEqualTo("[STRING attribute 'flib'] \nExpecting actual not to be null");
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
}
}