From 328778aac4c651cb0040b625f8297c09a9db0f3d Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Mon, 7 Aug 2023 17:38:44 -0500 Subject: [PATCH] Add hasResourceSatisfying to LogRecordDataAssert --- .../opentelemetry-sdk-testing.txt | 4 +- .../testing/assertj/LogRecordDataAssert.java | 13 ++++ .../testing/assertj/LogAssertionsTest.java | 66 ++++++++++++++++++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt index df26146497b..28a32e83f11 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt @@ -1,2 +1,4 @@ Comparing source compatibility of against -No changes. \ No newline at end of file +*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.LogRecordDataAssert hasResourceSatisfying(java.util.function.Consumer) diff --git a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/LogRecordDataAssert.java b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/LogRecordDataAssert.java index 0988a07fd00..7489cb135b5 100644 --- a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/LogRecordDataAssert.java +++ b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/LogRecordDataAssert.java @@ -46,6 +46,19 @@ public LogRecordDataAssert hasResource(Resource resource) { return this; } + /** + * Asserts the log has a resource satisfying the given condition. + * + * @since 1.29.0 + */ + public LogRecordDataAssert hasResourceSatisfying(Consumer resource) { + isNotNull(); + resource.accept( + new ResourceAssert( + actual.getResource(), String.format("log [%s]", actual.getBody().asString()))); + return this; + } + /** * Asserts the {@link InstrumentationScopeInfo} associated with a log matches the expected value. */ diff --git a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java index 48155c8e2ba..15da07a5214 100644 --- a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java +++ b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java @@ -5,10 +5,10 @@ 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; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; import static org.assertj.core.api.Assertions.assertThatThrownBy; import io.opentelemetry.api.common.AttributeKey; @@ -27,11 +27,13 @@ public class LogAssertionsTest { private static final Resource RESOURCE = - Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); + Resource.create(Attributes.builder().put("dog", "bark").put("dog is cute", true).build()); private static final InstrumentationScopeInfo INSTRUMENTATION_SCOPE_INFO = InstrumentationScopeInfo.create("instrumentation_library"); private static final String TRACE_ID = "00000000000000010000000000000002"; private static final String SPAN_ID = "0000000000000003"; + + private static final AttributeKey DOG = AttributeKey.stringKey("dog"); private static final Attributes ATTRIBUTES = Attributes.builder() .put("bear", "mya") @@ -64,6 +66,36 @@ public class LogAssertionsTest { void passing() { assertThat(LOG_DATA) .hasResource(RESOURCE) + .hasResourceSatisfying( + resource -> + resource + .hasSchemaUrl(null) + .hasAttribute(DOG, "bark") + .hasAttributes( + Attributes.of(DOG, "bark", AttributeKey.booleanKey("dog is cute"), true)) + .hasAttributes( + attributeEntry("dog", "bark"), attributeEntry("dog is cute", true)) + .hasAttributesSatisfying( + attributes -> + assertThat(attributes) + .hasSize(2) + .containsEntry(AttributeKey.stringKey("dog"), "bark") + .hasEntrySatisfying(DOG, value -> assertThat(value).hasSize(4)) + .hasEntrySatisfying( + AttributeKey.booleanKey("dog is cute"), + value -> assertThat(value).isTrue()))) + .hasResourceSatisfying( + resource -> + resource.hasAttributesSatisfying(satisfies(DOG, val -> val.isEqualTo("bark")))) + .hasResourceSatisfying( + resource -> + resource.hasAttributesSatisfyingExactly( + equalTo(DOG, "bark"), equalTo(AttributeKey.booleanKey("dog is cute"), true))) + .hasResourceSatisfying( + resource -> + resource.hasAttributesSatisfyingExactly( + satisfies(DOG, val -> val.startsWith("bar")), + satisfies(AttributeKey.booleanKey("dog is cute"), val -> val.isTrue()))) .hasInstrumentationScope(INSTRUMENTATION_SCOPE_INFO) .hasTimestamp(100) .hasObservedTimestamp(200) @@ -132,6 +164,36 @@ void passing() { @Test void failure() { assertThatThrownBy(() -> assertThat(LOG_DATA).hasResource(Resource.empty())); + assertThatThrownBy( + () -> + assertThat(LOG_DATA) + .hasResourceSatisfying(resource -> resource.hasSchemaUrl("http://example.com"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + assertThat(LOG_DATA) + .hasResourceSatisfying(resource -> resource.hasAttribute(DOG, "meow"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + assertThat(LOG_DATA) + .hasResourceSatisfying( + resource -> resource.hasAttributes(Attributes.of(DOG, "bark")))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + assertThat(LOG_DATA) + .hasResourceSatisfying( + resource -> resource.hasAttributes(attributeEntry("dog is cute", true)))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + assertThat(LOG_DATA) + .hasResourceSatisfying( + resource -> + resource.hasAttributesSatisfying( + attributes -> assertThat(attributes).hasSize(1)))) + .isInstanceOf(AssertionError.class); assertThatThrownBy( () -> assertThat(LOG_DATA).hasInstrumentationScope(InstrumentationScopeInfo.empty())); assertThatThrownBy(() -> assertThat(LOG_DATA).hasTimestamp(200));