-
Notifications
You must be signed in to change notification settings - Fork 775
/
AttributeAssertion.java
66 lines (56 loc) · 2.31 KB
/
AttributeAssertion.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
/*
* 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 com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.AttributeKey;
import java.util.List;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.assertj.core.api.AbstractAssert;
/** An assertion on an attribute key. */
@AutoValue
public abstract class AttributeAssertion {
// This method is not type-safe! But because the constructor is private, we know it will only be
// created through our factories, which are type-safe.
@SuppressWarnings("unchecked")
static AttributeAssertion create(
AttributeKey<?> key, Consumer<? extends AbstractAssert<?, ?>> assertion) {
return new AutoValue_AttributeAssertion(key, (Consumer<AbstractAssert<?, ?>>) assertion);
}
abstract AttributeKey<?> getKey();
abstract Consumer<AbstractAssert<?, ?>> getAssertion();
// The return type of these assertions must match the parameters in methods like
// OpenTelemetryAssertions.satisfies.
// Our code is nullness annotated but assertj is not. NullAway seems to still treat the base class
// of OpenTelemetryAssertions as annotated though, so there seems to be no way to avoid
// suppressing here.
@SuppressWarnings("NullAway")
static AbstractAssert<?, ?> attributeValueAssertion(AttributeKey<?> key, @Nullable Object value) {
AbstractAssert<? extends AbstractAssert<?, ?>, ?> abstractAssert = makeAssertion(key, value);
String description = "%s attribute '%s'";
return abstractAssert.as(description, key.getType(), key.getKey());
}
private static AbstractAssert<? extends AbstractAssert<?, ?>, ?> makeAssertion(
AttributeKey<?> key, Object value) {
switch (key.getType()) {
case STRING:
return assertThat((String) value);
case BOOLEAN:
return assertThat((Boolean) value);
case LONG:
return assertThat((Long) value);
case DOUBLE:
return assertThat((Double) value);
case STRING_ARRAY:
case BOOLEAN_ARRAY:
case LONG_ARRAY:
case DOUBLE_ARRAY:
return assertThat((List<?>) value);
}
throw new IllegalArgumentException("Unknown type for key " + key);
}
AttributeAssertion() {}
}