forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogRecordDataAssert.java
221 lines (201 loc) · 7.68 KB
/
LogRecordDataAssert.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.testing.assertj;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.resources.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Consumer;
import org.assertj.core.api.AbstractAssert;
/** Test assertions for {@link LogRecordData}. */
public class LogRecordDataAssert extends AbstractAssert<LogRecordDataAssert, LogRecordData> {
protected LogRecordDataAssert(LogRecordData actual) {
super(actual, LogRecordDataAssert.class);
}
/** Asserts the {@link Resource} associated with a log matches the expected value. */
public LogRecordDataAssert hasResource(Resource resource) {
isNotNull();
if (!actual.getResource().equals(resource)) {
failWithActualExpectedAndMessage(
actual,
"resource: " + resource,
"Expected log to have resource <%s> but found <%s>",
resource,
actual.getResource());
}
return this;
}
/**
* Asserts the {@link InstrumentationScopeInfo} associated with a log matches the expected value.
*/
public LogRecordDataAssert hasInstrumentationScope(
InstrumentationScopeInfo instrumentationScopeInfo) {
isNotNull();
if (!actual.getInstrumentationScopeInfo().equals(instrumentationScopeInfo)) {
failWithActualExpectedAndMessage(
actual,
"instrumentation scope: " + instrumentationScopeInfo,
"Expected log to have scope <%s> but found <%s>",
instrumentationScopeInfo,
actual.getInstrumentationScopeInfo());
}
return this;
}
/** Asserts the log has the given epoch timestamp. */
public LogRecordDataAssert hasEpochNanos(long epochNanos) {
isNotNull();
if (actual.getEpochNanos() != epochNanos) {
failWithActualExpectedAndMessage(
actual.getEpochNanos(),
epochNanos,
"Expected log to have epoch <%s> nanos but was <%s>",
epochNanos,
actual.getEpochNanos());
}
return this;
}
/** Asserts the log has the given span context. */
public LogRecordDataAssert hasSpanContext(SpanContext spanContext) {
isNotNull();
if (!actual.getSpanContext().equals(spanContext)) {
failWithActualExpectedAndMessage(
actual.getSpanContext(),
spanContext,
"Expected log to have span context <%s> nanos but was <%s>",
spanContext,
actual.getSpanContext());
}
return this;
}
/** Asserts the log has the given severity. */
public LogRecordDataAssert hasSeverity(Severity severity) {
isNotNull();
if (actual.getSeverity() != severity) {
failWithActualExpectedAndMessage(
actual.getSeverity(),
severity,
"Expected log to have severity <%s> but was <%s>",
severity,
actual.getSeverity());
}
return this;
}
/** Asserts the log has the given severity text. */
public LogRecordDataAssert hasSeverityText(String severityText) {
isNotNull();
if (!severityText.equals(actual.getSeverityText())) {
failWithActualExpectedAndMessage(
actual.getSeverityText(),
severityText,
"Expected log to have severity text <%s> but was <%s>",
severityText,
actual.getSeverityText());
}
return this;
}
/** Asserts the log has the given body. */
public LogRecordDataAssert hasBody(String body) {
isNotNull();
if (!actual.getBody().asString().equals(body)) {
failWithActualExpectedAndMessage(
actual.getBody(),
body,
"Expected log to have body <%s> but was <%s>",
body,
actual.getBody().asString());
}
return this;
}
/** Asserts the log has the given attributes. */
public LogRecordDataAssert hasAttributes(Attributes attributes) {
isNotNull();
if (!attributesAreEqual(attributes)) {
failWithActualExpectedAndMessage(
actual.getAttributes(),
attributes,
"Expected log to have attributes <%s> but was <%s>",
attributes,
actual.getAttributes());
}
return this;
}
/** Asserts the log has the given attributes. */
@SuppressWarnings({"rawtypes", "unchecked"})
@SafeVarargs
public final LogRecordDataAssert hasAttributes(
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 hasAttributes(attributes);
}
/** Asserts the log has attributes satisfying the given condition. */
public LogRecordDataAssert hasAttributesSatisfying(Consumer<Attributes> attributes) {
isNotNull();
assertThat(actual.getAttributes()).as("attributes").satisfies(attributes);
return this;
}
/**
* Asserts the log has attributes matching all {@code assertions}. Assertions can be created using
* methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*/
public LogRecordDataAssert hasAttributesSatisfying(AttributeAssertion... assertions) {
return hasAttributesSatisfying(Arrays.asList(assertions));
}
/**
* Asserts the log has attributes matching all {@code assertions}. Assertions can be created using
* methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*/
public LogRecordDataAssert hasAttributesSatisfying(Iterable<AttributeAssertion> assertions) {
AssertUtil.assertAttributes(actual.getAttributes(), assertions);
return myself;
}
/**
* Asserts the log has attributes matching all {@code assertions} and no more. Assertions can be
* created using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*/
public LogRecordDataAssert hasAttributesSatisfyingExactly(AttributeAssertion... assertions) {
return hasAttributesSatisfyingExactly(Arrays.asList(assertions));
}
/**
* Asserts the log has attributes matching all {@code assertions} and no more. Assertions can be
* created using methods like {@link OpenTelemetryAssertions#satisfies(AttributeKey,
* OpenTelemetryAssertions.LongAssertConsumer)}.
*/
public LogRecordDataAssert hasAttributesSatisfyingExactly(
Iterable<AttributeAssertion> assertions) {
AssertUtil.assertAttributesExactly(actual.getAttributes(), assertions);
return myself;
}
private boolean attributesAreEqual(Attributes attributes) {
// compare as maps, since implementations do not have equals that work correctly across
// implementations.
return actual.getAttributes().asMap().equals(attributes.asMap());
}
/** Asserts the log has the given total attributes. */
public LogRecordDataAssert hasTotalAttributeCount(int totalAttributeCount) {
isNotNull();
if (actual.getTotalAttributeCount() != totalAttributeCount) {
failWithActualExpectedAndMessage(
actual.getTotalAttributeCount(),
totalAttributeCount,
"Expected log to have recorded <%s> total attributes but did not",
totalAttributeCount);
}
return this;
}
}