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

Add hasResourceSatisfying to LogRecordDataAssert #5690

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Comparing source compatibility of against
No changes.
*** 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<io.opentelemetry.sdk.testing.assertj.ResourceAssert>)
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceAssert> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> DOG = AttributeKey.stringKey("dog");
private static final Attributes ATTRIBUTES =
Attributes.builder()
.put("bear", "mya")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down