-
Notifications
You must be signed in to change notification settings - Fork 775
/
AssertUtil.java
87 lines (74 loc) · 3.09 KB
/
AssertUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.testing.assertj;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
import org.assertj.core.api.AbstractAssert;
final class AssertUtil {
/**
* Converts from consumers of our assertions provided by the user to consumers of the data for use
* with satisfiesExactlyInAnyOrder.
*/
static <T, U extends AbstractAssert<U, T>> Consumer<T>[] toConsumers(
Iterable<? extends Consumer<U>> assertions, Function<T, U> assertionFactory) {
Stream.Builder<Consumer<T>> builder = Stream.builder();
for (Consumer<U> assertion : assertions) {
builder.add(item -> assertion.accept(assertionFactory.apply(item)));
}
@SuppressWarnings({"rawtypes", "unchecked"})
Consumer<T>[] consumers = builder.build().toArray(Consumer[]::new);
return consumers;
}
static void assertAttributes(Attributes actual, Iterable<AttributeAssertion> assertions) {
assertAttributes(actual, assertions, "attribute keys");
}
static void assertAttributes(
Attributes actual, Iterable<AttributeAssertion> assertions, String name) {
Set<AttributeKey<?>> actualKeys = actual.asMap().keySet();
Set<AttributeKey<?>> checkedKeys = new HashSet<>();
for (AttributeAssertion attributeAssertion : assertions) {
AttributeKey<?> key = attributeAssertion.getKey();
Object value = actual.get(key);
if (value != null) {
checkedKeys.add(key);
}
AbstractAssert<?, ?> assertion = AttributeAssertion.attributeValueAssertion(key, value);
attributeAssertion.getAssertion().accept(assertion);
}
assertThat(actualKeys).as(name).containsAll(checkedKeys);
}
static void assertAttributesExactly(Attributes actual, Iterable<AttributeAssertion> assertions) {
assertAttributesExactly(actual, assertions, "attribute keys");
}
static void assertAttributesExactly(
Attributes actual, Iterable<AttributeAssertion> assertions, String name) {
Set<AttributeKey<?>> actualKeys = actual.asMap().keySet();
Set<AttributeKey<?>> checkedKeys = new HashSet<>();
for (AttributeAssertion attributeAssertion : assertions) {
AttributeKey<?> key = attributeAssertion.getKey();
Object value = actual.get(key);
if (value != null) {
checkedKeys.add(key);
}
AbstractAssert<?, ?> assertion = AttributeAssertion.attributeValueAssertion(key, value);
attributeAssertion.getAssertion().accept(assertion);
}
assertThat(actualKeys).as(name).containsExactlyInAnyOrderElementsOf(checkedKeys);
}
/**
* Compares {@link Attributes} as maps since currently attributes cannot be compared across
* implementations.
*/
static boolean attributesAreEqual(Attributes actual, Attributes other) {
return actual.asMap().equals(other.asMap());
}
private AssertUtil() {}
}