|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.testing.assertj; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import io.opentelemetry.api.common.AttributeKey; |
| 11 | +import io.opentelemetry.api.common.Attributes; |
| 12 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 13 | +import io.opentelemetry.sdk.resources.Resource; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.function.Consumer; |
| 18 | +import javax.annotation.Nullable; |
| 19 | +import org.assertj.core.api.AbstractAssert; |
| 20 | + |
| 21 | +/** |
| 22 | + * Assertions for {@link Resource}. |
| 23 | + * |
| 24 | + * @since 1.23.0 |
| 25 | + */ |
| 26 | +public final class ResourceAssert extends AbstractAssert<ResourceAssert, Resource> { |
| 27 | + |
| 28 | + private final String label; |
| 29 | + |
| 30 | + ResourceAssert(Resource resource, String label) { |
| 31 | + super(resource, ResourceAssert.class); |
| 32 | + this.label = label; |
| 33 | + } |
| 34 | + |
| 35 | + /** Asserts the resource has a schemaUrl satisfying the given condition. */ |
| 36 | + // Workaround "passing @Nullable parameter 'schemaUrl' where @NonNull is required", Nullaway |
| 37 | + // seems to think assertThat is supposed to be passed NonNull even though we know that can't be |
| 38 | + // true for assertions. |
| 39 | + @SuppressWarnings("NullAway") |
| 40 | + public ResourceAssert hasSchemaUrl(@Nullable String schemaUrl) { |
| 41 | + isNotNull(); |
| 42 | + assertThat(actual.getSchemaUrl()).as("resource schema URL of %s", label).isEqualTo(schemaUrl); |
| 43 | + return this; |
| 44 | + } |
| 45 | + |
| 46 | + /** Asserts the resource has the given attribute. */ |
| 47 | + public <T> ResourceAssert hasAttribute(AttributeKey<T> key, T value) { |
| 48 | + return hasAttribute(OpenTelemetryAssertions.equalTo(key, value)); |
| 49 | + } |
| 50 | + |
| 51 | + /** Asserts the resource has an attribute matching the {@code attributeAssertion}. */ |
| 52 | + public ResourceAssert hasAttribute(AttributeAssertion attributeAssertion) { |
| 53 | + isNotNull(); |
| 54 | + |
| 55 | + Set<AttributeKey<?>> actualKeys = actual.getAttributes().asMap().keySet(); |
| 56 | + AttributeKey<?> key = attributeAssertion.getKey(); |
| 57 | + |
| 58 | + assertThat(actualKeys).as("resource attribute keys of %s", label).contains(key); |
| 59 | + |
| 60 | + Object value = actual.getAttributes().get(key); |
| 61 | + AbstractAssert<?, ?> assertion = AttributeAssertion.attributeValueAssertion(key, value); |
| 62 | + attributeAssertion.getAssertion().accept(assertion); |
| 63 | + |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** Asserts the resource has the given attributes. */ |
| 68 | + public ResourceAssert hasAttributes(Attributes attributes) { |
| 69 | + isNotNull(); |
| 70 | + if (!AssertUtil.attributesAreEqual(actual.getAttributes(), attributes)) { |
| 71 | + failWithActualExpectedAndMessage( |
| 72 | + actual.getAttributes(), |
| 73 | + attributes, |
| 74 | + "Expected resource of <%s> to have attributes <%s> but was <%s>", |
| 75 | + label, |
| 76 | + attributes, |
| 77 | + actual.getAttributes()); |
| 78 | + } |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + /** Asserts the resource has the given attributes. */ |
| 83 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 84 | + @SafeVarargs |
| 85 | + public final ResourceAssert hasAttributes(Map.Entry<? extends AttributeKey<?>, ?>... entries) { |
| 86 | + AttributesBuilder attributesBuilder = Attributes.builder(); |
| 87 | + for (Map.Entry<? extends AttributeKey<?>, ?> attr : entries) { |
| 88 | + attributesBuilder.put((AttributeKey) attr.getKey(), attr.getValue()); |
| 89 | + } |
| 90 | + Attributes attributes = attributesBuilder.build(); |
| 91 | + return hasAttributes(attributes); |
| 92 | + } |
| 93 | + |
| 94 | + /** Asserts the resource has attributes satisfying the given condition. */ |
| 95 | + public ResourceAssert hasAttributesSatisfying(Consumer<Attributes> attributes) { |
| 96 | + isNotNull(); |
| 97 | + OpenTelemetryAssertions.assertThat(actual.getAttributes()) |
| 98 | + .as("resource attributes of %s", label) |
| 99 | + .satisfies(attributes); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Asserts the event has attributes matching all {@code assertions}. Assertions can be created |
| 105 | + * using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey, |
| 106 | + * OpenTelemetryAssertions.LongAssertConsumer)}. |
| 107 | + */ |
| 108 | + public ResourceAssert hasAttributesSatisfying(AttributeAssertion... assertions) { |
| 109 | + return hasAttributesSatisfying(Arrays.asList(assertions)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Asserts the event has attributes matching all {@code assertions}. Assertions can be created |
| 114 | + * using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey, |
| 115 | + * OpenTelemetryAssertions.LongAssertConsumer)}. |
| 116 | + */ |
| 117 | + public ResourceAssert hasAttributesSatisfying(Iterable<AttributeAssertion> assertions) { |
| 118 | + AssertUtil.assertAttributes( |
| 119 | + actual.getAttributes(), assertions, String.format("resource of %s attribute keys", label)); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Asserts the resource has attributes matching all {@code assertions} and no more. Assertions can |
| 125 | + * be created using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey, |
| 126 | + * OpenTelemetryAssertions.LongAssertConsumer)}. |
| 127 | + */ |
| 128 | + public ResourceAssert hasAttributesSatisfyingExactly(AttributeAssertion... assertions) { |
| 129 | + return hasAttributesSatisfyingExactly(Arrays.asList(assertions)); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Asserts the resource has attributes matching all {@code assertions} and no more. Assertions can |
| 134 | + * be created using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey, |
| 135 | + * OpenTelemetryAssertions.LongAssertConsumer)}. |
| 136 | + */ |
| 137 | + public ResourceAssert hasAttributesSatisfyingExactly(Iterable<AttributeAssertion> assertions) { |
| 138 | + AssertUtil.assertAttributesExactly( |
| 139 | + actual.getAttributes(), assertions, String.format("resource of %s attribute keys", label)); |
| 140 | + return this; |
| 141 | + } |
| 142 | +} |
0 commit comments