From 16fc3d4d94cd72ec569b22ba3b8a17109375ebd4 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 8 Aug 2023 09:54:52 +0200 Subject: [PATCH] assertj magic --- .../view/AdviceAttributesProcessor.java | 20 +------ .../internal/view/ViewRegistryTest.java | 55 ++++++++++++------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/view/AdviceAttributesProcessor.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/view/AdviceAttributesProcessor.java index 3965c998140..161dcd72832 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/view/AdviceAttributesProcessor.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/view/AdviceAttributesProcessor.java @@ -11,7 +11,6 @@ import io.opentelemetry.context.Context; import java.util.HashSet; import java.util.List; -import java.util.Objects; import java.util.Set; final class AdviceAttributesProcessor extends AttributesProcessor { @@ -34,25 +33,8 @@ public boolean usesContext() { return false; } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - AdviceAttributesProcessor that = (AdviceAttributesProcessor) o; - return attributeKeys.equals(that.attributeKeys); - } - - @Override - public int hashCode() { - return Objects.hash(attributeKeys); - } - @Override public String toString() { - return "AdviceAttributesProcessor{" + "attributeKeys=" + attributeKeys + '}'; + return "AdviceAttributesProcessor{attributeKeys=" + attributeKeys + '}'; } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/internal/view/ViewRegistryTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/internal/view/ViewRegistryTest.java index 39d273be674..abba1c32133 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/internal/view/ViewRegistryTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/internal/view/ViewRegistryTest.java @@ -7,11 +7,11 @@ import static io.opentelemetry.api.common.AttributeKey.stringKey; import static io.opentelemetry.sdk.metrics.internal.view.ViewRegistry.DEFAULT_REGISTERED_VIEW; -import static io.opentelemetry.sdk.metrics.internal.view.ViewRegistry.DEFAULT_VIEW; import static io.opentelemetry.sdk.metrics.internal.view.ViewRegistry.toGlobPatternPredicate; import static org.assertj.core.api.Assertions.assertThat; import io.github.netmikey.logunit.api.LogCapturer; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.internal.testing.slf4j.SuppressLogger; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.Aggregation; @@ -27,6 +27,7 @@ import io.opentelemetry.sdk.metrics.internal.state.MetricStorage; import java.util.Arrays; import java.util.Collections; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -480,15 +481,23 @@ void findViews_ApplyAdvice() { .setAttributes(Arrays.asList(stringKey("key1"), stringKey("key2"))) .build()), INSTRUMENTATION_SCOPE_INFO)) - .isEqualTo( - Collections.singletonList( - RegisteredView.create( - DEFAULT_REGISTERED_VIEW.getInstrumentSelector(), - DEFAULT_VIEW, - new AdviceAttributesProcessor( - Arrays.asList(stringKey("key1"), stringKey("key2"))), - MetricStorage.DEFAULT_MAX_CARDINALITY, - SourceInfo.noSourceInfo()))); + .hasSize(1) + .element(0) + .satisfies( + view -> { + assertThat(view) + .as("is the same as the default view, except the attributes processor") + .usingRecursiveComparison() + .ignoringFields("viewAttributesProcessor") + .isEqualTo(DEFAULT_REGISTERED_VIEW); + assertThat(view) + .as("has the advice attributes processor") + .extracting("viewAttributesProcessor") + .isInstanceOf(AdviceAttributesProcessor.class) + .extracting( + "attributeKeys", InstanceOfAssertFactories.collection(AttributeKey.class)) + .containsExactlyInAnyOrder(stringKey("key1"), stringKey("key2")); + }); // If there is no matching view and attributes advice was defined, use it - incompatible // aggregation case @@ -504,15 +513,23 @@ void findViews_ApplyAdvice() { .setAttributes(Arrays.asList(stringKey("key1"), stringKey("key2"))) .build()), INSTRUMENTATION_SCOPE_INFO)) - .isEqualTo( - Collections.singletonList( - RegisteredView.create( - DEFAULT_REGISTERED_VIEW.getInstrumentSelector(), - DEFAULT_REGISTERED_VIEW.getView(), - new AdviceAttributesProcessor( - Arrays.asList(stringKey("key1"), stringKey("key2"))), - DEFAULT_REGISTERED_VIEW.getCardinalityLimit(), - DEFAULT_REGISTERED_VIEW.getViewSourceInfo()))); + .hasSize(1) + .element(0) + .satisfies( + view -> { + assertThat(view) + .as("is the same as the default view, except the attributes processor") + .usingRecursiveComparison() + .ignoringFields("viewAttributesProcessor") + .isEqualTo(DEFAULT_REGISTERED_VIEW); + assertThat(view) + .as("has the advice attributes processor") + .extracting("viewAttributesProcessor") + .isInstanceOf(AdviceAttributesProcessor.class) + .extracting( + "attributeKeys", InstanceOfAssertFactories.collection(AttributeKey.class)) + .containsExactlyInAnyOrder(stringKey("key1"), stringKey("key2")); + }); // if advice is not defined, use the default view assertThat(