forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongExemplarAssert.java
143 lines (123 loc) · 5.2 KB
/
LongExemplarAssert.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* 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 io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.metrics.data.LongExemplarData;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import org.assertj.core.api.AbstractAssert;
/**
* Assertions for an exported {@link LongExemplarData}.
*
* @since 1.14.0
*/
public final class LongExemplarAssert extends AbstractAssert<LongExemplarAssert, LongExemplarData> {
LongExemplarAssert(@Nullable LongExemplarData actual) {
super(actual, LongExemplarAssert.class);
}
/** Asserts the exemplar has the given epoch timestamp, in nanos. */
public LongExemplarAssert hasEpochNanos(long expected) {
isNotNull();
assertThat(actual.getEpochNanos()).as("epochNanos").isEqualTo(expected);
return this;
}
/** Asserts the exemplar has the given span ID. */
public LongExemplarAssert hasSpanId(String expected) {
isNotNull();
assertThat(actual.getSpanContext().getSpanId()).as("spanId").isEqualTo(expected);
return this;
}
/** Asserts the exemplar has the given trace ID. */
public LongExemplarAssert hasTraceId(String expected) {
isNotNull();
assertThat(actual.getSpanContext().getTraceId()).as("traceId").isEqualTo(expected);
return this;
}
/** Asserts the exemplar has the given value. */
public LongExemplarAssert hasValue(long expected) {
isNotNull();
assertThat(actual.getValue()).as("value").isEqualTo(expected);
return this;
}
/** Asserts the exemplar has the given filtered attribute. */
public <T> LongExemplarAssert hasFilteredAttribute(AttributeKey<T> key, T value) {
return hasFilteredAttribute(OpenTelemetryAssertions.equalTo(key, value));
}
/** Asserts the exemplar has the given filtered attribute. */
public LongExemplarAssert hasFilteredAttribute(AttributeAssertion attributeAssertion) {
isNotNull();
Set<AttributeKey<?>> actualKeys = actual.getFilteredAttributes().asMap().keySet();
AttributeKey<?> key = attributeAssertion.getKey();
assertThat(actualKeys).as("attribute keys").contains(key);
Object value = actual.getFilteredAttributes().get(key);
AbstractAssert<?, ?> assertion = AttributeAssertion.attributeValueAssertion(key, value);
attributeAssertion.getAssertion().accept(assertion);
return this;
}
/** Asserts the exemplar has the given filtered attributes. */
public LongExemplarAssert hasFilteredAttributes(Attributes expected) {
isNotNull();
assertThat(actual.getFilteredAttributes()).as("filtered_attributes").isEqualTo(expected);
return this;
}
/** Asserts the exemplar has the given filtered attributes. */
@SuppressWarnings({"rawtypes", "unchecked"})
@SafeVarargs
public final LongExemplarAssert hasFilteredAttributes(
Map.Entry<? extends AttributeKey<?>, ?>... entries) {
AttributesBuilder attributesBuilder = Attributes.builder();
for (Map.Entry<? extends AttributeKey<?>, ?> attr : entries) {
attributesBuilder.put((AttributeKey) attr.getKey(), attr.getValue());
}
Attributes attributes = attributesBuilder.build();
return hasFilteredAttributes(attributes);
}
/**
* Asserts the exemplar has filtered attributes matching all {@code assertions}. Assertions can be
* created using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*/
public LongExemplarAssert hasFilteredAttributesSatisfying(AttributeAssertion... assertions) {
return hasFilteredAttributesSatisfying(Arrays.asList(assertions));
}
/**
* Asserts the exemplar has filtered attributes matching all {@code assertions}. Assertions can be
* created using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*/
public LongExemplarAssert hasFilteredAttributesSatisfying(
Iterable<AttributeAssertion> assertions) {
AssertUtil.assertAttributes(actual.getFilteredAttributes(), assertions);
return myself;
}
/**
* Asserts the exemplar has filtered attributes matching all {@code assertions} and no more.
* Assertions can be created using methods like {@link
* OpenTelemetryAssertions#satisfies(AttributeKey, OpenTelemetryAssertions.LongAssertConsumer)}.
*
* @since 1.21.0
*/
public LongExemplarAssert hasFilteredAttributesSatisfyingExactly(
AttributeAssertion... assertions) {
return hasFilteredAttributesSatisfying(Arrays.asList(assertions));
}
/**
* Asserts the exemplar has filtered attributes matching all {@code assertions} and no more.
* Assertions can be created using methods like {@link
* OpenTelemetryAssertions#satisfies(AttributeKey, OpenTelemetryAssertions.LongAssertConsumer)}.
*
* @since 1.21.0
*/
public LongExemplarAssert hasFilteredAttributesSatisfyingExactly(
Iterable<AttributeAssertion> assertions) {
AssertUtil.assertAttributesExactly(actual.getFilteredAttributes(), assertions);
return myself;
}
}