forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCelBasedSamplingExpressionTest.java
More file actions
88 lines (70 loc) · 3.46 KB
/
CelBasedSamplingExpressionTest.java
File metadata and controls
88 lines (70 loc) · 3.46 KB
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.sampler.cel;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelValidationException;
import dev.cel.compiler.CelCompilerFactory;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import org.junit.jupiter.api.Test;
final class CelBasedSamplingExpressionTest {
@Test
void testThatThrowsOnNullParameter() throws CelValidationException {
CelAbstractSyntaxTree ast =
CelCompilerFactory.standardCelCompilerBuilder().build().compile("1 == 1").getAst();
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> new CelBasedSamplingExpression(ast, null));
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> new CelBasedSamplingExpression(null, Sampler.alwaysOn()));
}
@Test
void testToString() throws CelValidationException {
CelAbstractSyntaxTree ast =
CelCompilerFactory.standardCelCompilerBuilder().build().compile("1 == 1").getAst();
CelBasedSamplingExpression celExpression =
new CelBasedSamplingExpression(ast, Sampler.alwaysOn());
String expected = "CelBasedSamplingExpression{expression='1 == 1', delegate=AlwaysOnSampler}";
assertThat(celExpression.toString()).isEqualTo(expected);
}
@Test
void testEquals() throws CelValidationException {
CelBasedSamplingExpression celExpressionOneEqualsOne1 =
new CelBasedSamplingExpression(
CelCompilerFactory.standardCelCompilerBuilder().build().compile("1 == 1").getAst(),
Sampler.alwaysOn());
assertThat(celExpressionOneEqualsOne1).isNotEqualTo(null);
CelBasedSamplingExpression celExpressionOneEqualsOne2 =
new CelBasedSamplingExpression(
CelCompilerFactory.standardCelCompilerBuilder().build().compile("1 == 1").getAst(),
Sampler.alwaysOn());
assertThat(celExpressionOneEqualsOne1).isEqualTo(celExpressionOneEqualsOne2);
CelBasedSamplingExpression celExpressionTwoEqualsTwo =
new CelBasedSamplingExpression(
CelCompilerFactory.standardCelCompilerBuilder().build().compile("2 == 2").getAst(),
Sampler.alwaysOn());
assertThat(celExpressionOneEqualsOne1).isNotEqualTo(celExpressionTwoEqualsTwo);
CelBasedSamplingExpression celExpressionOneEqualsOneSamplerOff =
new CelBasedSamplingExpression(
CelCompilerFactory.standardCelCompilerBuilder().build().compile("1 == 1").getAst(),
Sampler.alwaysOff());
assertThat(celExpressionOneEqualsOne1).isNotEqualTo(celExpressionOneEqualsOneSamplerOff);
}
@Test
void testHashCode() throws CelValidationException {
CelBasedSamplingExpression celExpression1 =
new CelBasedSamplingExpression(
CelCompilerFactory.standardCelCompilerBuilder().build().compile("1 == 1").getAst(),
Sampler.alwaysOn());
int expectedHashCode1 = celExpression1.hashCode();
int expectedHashCode2 = celExpression1.hashCode();
assertThat(expectedHashCode1).isEqualTo(expectedHashCode2);
CelBasedSamplingExpression celExpression2 =
new CelBasedSamplingExpression(
CelCompilerFactory.standardCelCompilerBuilder().build().compile("1 == 1").getAst(),
Sampler.alwaysOn());
assertThat(expectedHashCode1).isEqualTo(celExpression2.hashCode());
}
}