Skip to content

Commit df144f5

Browse files
committed
basic cel-java integration/test
1 parent 9d69f97 commit df144f5

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ commons-math3 = "org.apache.commons:commons-math3:3.6.1"
2727
conscrypt = "org.conscrypt:conscrypt-openjdk-uber:2.5.2"
2828
cronet-api = "org.chromium.net:cronet-api:119.6045.31"
2929
cronet-embedded = "org.chromium.net:cronet-embedded:119.6045.31"
30+
dev-cel = "dev.cel:cel:0.6.0"
3031
errorprone-annotations = "com.google.errorprone:error_prone_annotations:2.28.0"
3132
errorprone-core = "com.google.errorprone:error_prone_core:2.28.0"
3233
google-api-protos = "com.google.api.grpc:proto-google-common-protos:2.41.0"

xds/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ dependencies {
5252
project(':grpc-services'),
5353
project(':grpc-auth'),
5454
project(path: ':grpc-alts', configuration: 'shadow'),
55+
libraries.dev.cel,
5556
libraries.guava,
5657
libraries.gson,
5758
libraries.re2j,

xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,52 @@
1616

1717
package io.grpc.xds.internal.matchers;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.common.collect.ImmutableMap;
22+
import dev.cel.common.CelAbstractSyntaxTree;
23+
import dev.cel.runtime.CelEvaluationException;
24+
import dev.cel.runtime.CelRuntime;
25+
import dev.cel.runtime.CelRuntimeFactory;
1926
import java.util.function.Predicate;
2027

2128
/** Unified Matcher API: xds.type.matcher.v3.CelMatcher. */
2229
public class CelMatcher implements Predicate<HttpMatchInput> {
30+
private static final CelRuntime CEL_RUNTIME =
31+
CelRuntimeFactory.standardCelRuntimeBuilder().build();
32+
33+
private final CelRuntime.Program program;
2334
private final String description;
2435

25-
public CelMatcher(String description) {
26-
// TODO(sergiitk): cache parsed CEL expressions
36+
private CelMatcher(CelAbstractSyntaxTree ast, String description) throws CelEvaluationException {
37+
this.program = CEL_RUNTIME.createProgram(checkNotNull(ast));
2738
this.description = description != null ? description : "";
2839
}
2940

41+
public static CelMatcher create(CelAbstractSyntaxTree ast) throws CelEvaluationException {
42+
return new CelMatcher(ast, null);
43+
}
44+
45+
public static CelMatcher create(CelAbstractSyntaxTree ast, String description)
46+
throws CelEvaluationException {
47+
return new CelMatcher(ast, description);
48+
}
49+
3050
public String description() {
3151
return description;
3252
}
3353

3454
@Override
3555
public boolean test(HttpMatchInput httpMatchInput) {
36-
if (httpMatchInput.headers().keys().isEmpty()) {
56+
// if (httpMatchInput.headers().keys().isEmpty()) {
57+
// return false;
58+
// }
59+
// TODO(sergiitk): [IMPL] convert headers to cel args
60+
try {
61+
return (boolean) program.eval(ImmutableMap.of("my_var", "Hello World"));
62+
} catch (CelEvaluationException e) {
63+
// TODO(sergiitk): [IMPL] log cel error?
3764
return false;
3865
}
39-
// TODO(sergiitk): [IMPL] convert headers to cel args
40-
return true;
4166
}
4267
}

xds/src/test/java/io/grpc/xds/internal/matchers/CelMatcherTest.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,54 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import dev.cel.common.CelAbstractSyntaxTree;
22+
import dev.cel.common.CelValidationResult;
23+
import dev.cel.common.types.SimpleType;
24+
import dev.cel.compiler.CelCompiler;
25+
import dev.cel.compiler.CelCompilerFactory;
26+
import dev.cel.runtime.CelEvaluationException;
27+
import io.grpc.Metadata;
28+
import org.junit.Before;
2129
import org.junit.Test;
2230
import org.junit.runner.RunWith;
2331
import org.junit.runners.JUnit4;
2432

2533
@RunWith(JUnit4.class)
2634
public class CelMatcherTest {
35+
// Construct the compilation and runtime environments.
36+
// These instances are immutable and thus trivially thread-safe and amenable to caching.
37+
private static final CelCompiler CEL_COMPILER =
38+
CelCompilerFactory.standardCelCompilerBuilder().addVar("my_var", SimpleType.STRING).build();
39+
private static final CelValidationResult celProg1 =
40+
CEL_COMPILER.compile("type(my_var) == string");
41+
42+
CelAbstractSyntaxTree ast1;
43+
CelMatcher matcher1;
44+
45+
private static final HttpMatchInput fakeInput = new HttpMatchInput() {
46+
@Override
47+
public Metadata headers() {
48+
return new Metadata();
49+
}
50+
};
51+
52+
@Before
53+
public void setUp() throws Exception {
54+
ast1 = celProg1.getAst();
55+
matcher1 = CelMatcher.create(ast1);
56+
}
2757

2858
@Test
29-
public void construct() {
59+
public void construct() throws CelEvaluationException {
60+
assertThat(matcher1.description()).isEqualTo("");
61+
3062
String description = "Optional description";
31-
CelMatcher matcher = new CelMatcher(description);
63+
CelMatcher matcher = CelMatcher.create(ast1, description);
3264
assertThat(matcher.description()).isEqualTo(description);
65+
}
3366

34-
matcher = new CelMatcher(null);
35-
assertThat(matcher.description()).isEqualTo("");
67+
@Test
68+
public void testProgTrue() {
69+
assertThat(matcher1.test(fakeInput)).isTrue();
3670
}
3771
}

0 commit comments

Comments
 (0)