Skip to content

Commit

Permalink
assertj magic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Aug 10, 2023
1 parent 6ceb8ce commit 16fc3d4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 16fc3d4

Please sign in to comment.