forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDataAssert.java
133 lines (119 loc) · 4.48 KB
/
EventDataAssert.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
/*
* 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.sdk.trace.data.EventData;
import java.time.Instant;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.assertj.core.api.AbstractAssert;
/** Assertions for {@link EventData}. */
public final class EventDataAssert extends AbstractAssert<EventDataAssert, EventData> {
EventDataAssert(@Nullable EventData actual) {
super(actual, EventDataAssert.class);
}
/** Asserts the event has the given name. */
public EventDataAssert hasName(String name) {
isNotNull();
if (!actual.getName().equals(name)) {
failWithActualExpectedAndMessage(
actual.getName(),
name,
"Expected event to have name <%s> but was <%s>",
name,
actual.getName());
}
return this;
}
/** Asserts the event has the given timestamp, in nanos. */
public EventDataAssert hasTimestamp(long timestampNanos) {
isNotNull();
if (actual.getEpochNanos() != timestampNanos) {
failWithActualExpectedAndMessage(
actual.getEpochNanos(),
timestampNanos,
"Expected event [%s] to have timestamp <%s> nanos but was <%s>",
actual.getName(),
timestampNanos,
actual.getEpochNanos());
}
return this;
}
/** Asserts the event has the given timestamp. */
@SuppressWarnings("PreferJavaTimeOverload")
public EventDataAssert hasTimestamp(long timestamp, TimeUnit unit) {
return hasTimestamp(unit.toNanos(timestamp));
}
/** Asserts the event has the given timestamp, in nanos. */
public EventDataAssert hasTimestamp(Instant timestamp) {
return hasTimestamp(TimeUnit.SECONDS.toNanos(timestamp.getEpochSecond()) + timestamp.getNano());
}
/** Asserts the event has the given attributes. */
public EventDataAssert hasAttributes(Attributes attributes) {
isNotNull();
if (!actual.getAttributes().equals(attributes)) {
failWithActualExpectedAndMessage(
actual.getAttributes(),
attributes,
"Expected event [%s] to have attributes <%s> but was <%s>",
actual.getName(),
attributes,
actual.getAttributes());
}
return this;
}
/** Asserts the event has attributes satisfying the given condition. */
public EventDataAssert hasAttributesSatisfying(Consumer<Attributes> attributes) {
isNotNull();
assertThat(actual.getAttributes()).as("attributes").satisfies(attributes);
return this;
}
/**
* Asserts the event has attributes matching all {@code assertions}. Assertions can be created
* using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*
* @since 1.21.0
*/
public EventDataAssert hasAttributesSatisfying(AttributeAssertion... assertions) {
return hasAttributesSatisfying(Arrays.asList(assertions));
}
/**
* Asserts the event has attributes matching all {@code assertions}. Assertions can be created
* using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*
* @since 1.21.0
*/
public EventDataAssert hasAttributesSatisfying(Iterable<AttributeAssertion> assertions) {
AssertUtil.assertAttributes(actual.getAttributes(), assertions);
return this;
}
/**
* Asserts the event has 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 EventDataAssert hasAttributesSatisfyingExactly(AttributeAssertion... assertions) {
return hasAttributesSatisfyingExactly(Arrays.asList(assertions));
}
/**
* Asserts the event has 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 EventDataAssert hasAttributesSatisfyingExactly(Iterable<AttributeAssertion> assertions) {
AssertUtil.assertAttributesExactly(actual.getAttributes(), assertions);
return this;
}
}