Skip to content

Commit 43e121c

Browse files
ChinmayMadeshicopybara-github
authored andcommitted
Internal Changes
PiperOrigin-RevId: 769102397
1 parent 6d92e81 commit 43e121c

18 files changed

Lines changed: 739 additions & 0 deletions

File tree

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
load("@rules_java//java:java_library.bzl", "java_library")
2+
load("//third_party/cel/expr:cel_test.bzl", "cel_test", "make_test_targets", "test_runtime")
3+
4+
package(
5+
default_applicable_licenses = ["//:license"],
6+
default_testonly = 1,
7+
)
8+
9+
java_library(
10+
name = "basic_user_test",
11+
srcs = ["BasicUserTest.java"],
12+
deps = [
13+
"//bundle:cel",
14+
"//runtime:function_binding",
15+
"//testing/testrunner:cel_test_context",
16+
"//testing/testrunner:cel_user_test_template",
17+
"@maven//:junit_junit",
18+
],
19+
)
20+
21+
java_library(
22+
name = "error_output_user_test",
23+
srcs = ["ErrorOutputUserTest.java"],
24+
deps = [
25+
"//testing/testrunner:cel_test_context",
26+
"//testing/testrunner:cel_user_test_template",
27+
"@maven//:junit_junit",
28+
],
29+
)
30+
31+
java_library(
32+
name = "context_proto_user_test",
33+
srcs = ["ContextProtoUserTest.java"],
34+
deps = [
35+
"//bundle:cel",
36+
"//checker:proto_type_mask",
37+
"//testing/testrunner:cel_test_context",
38+
"//testing/testrunner:cel_user_test_template",
39+
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
40+
"@maven//:junit_junit",
41+
],
42+
)
43+
44+
java_library(
45+
name = "late_function_binding_user_test",
46+
srcs = ["LateFunctionBindingUserTest.java"],
47+
deps = [
48+
"//bundle:cel",
49+
"//runtime",
50+
"//runtime:function_binding",
51+
"//testing/testrunner:cel_test_context",
52+
"//testing/testrunner:cel_user_test_template",
53+
"@maven//:junit_junit",
54+
],
55+
)
56+
57+
java_library(
58+
name = "custom_variable_binding_user_test",
59+
srcs = ["CustomVariableBindingUserTest.java"],
60+
deps = [
61+
"//bundle:cel",
62+
"//runtime:function_binding",
63+
"//testing/testrunner:cel_test_context",
64+
"//testing/testrunner:cel_user_test_template",
65+
"@maven//:com_google_guava_guava",
66+
"@maven//:junit_junit",
67+
],
68+
)
69+
70+
java_library(
71+
name = "custom_test_suite",
72+
srcs = ["CustomTestSuite.java"],
73+
deps = [
74+
"//testing/testrunner:annotations",
75+
"//testing/testrunner:cel_test_suite",
76+
"@maven//:com_google_guava_guava",
77+
],
78+
)
79+
80+
cel_test(
81+
name = "example_0_test",
82+
cel_expr = "basictest/expression.cel",
83+
config = "basictest/config.yaml",
84+
filegroup = "//codelab:testrunner_codelab_resources", # Users can use exports_files with appropriate visibility in which case filegroup is not required.
85+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
86+
test_suite = "basictest/testsuite.yaml",
87+
test_targets = [make_test_targets(
88+
runtime = test_runtime.JAVA,
89+
test_srcs = [":basic_user_test"],
90+
)],
91+
)
92+
93+
cel_test(
94+
name = "example_0_test_with_raw_expression",
95+
cel_expr = "foo(a) || bar(a) == true",
96+
config = "basictest/config.yaml",
97+
filegroup = "//codelab:testrunner_codelab_resources",
98+
is_raw_expr = True,
99+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
100+
test_suite = "basictest/testsuite.yaml",
101+
test_targets = [make_test_targets(
102+
runtime = test_runtime.JAVA,
103+
test_srcs = [":basic_user_test"],
104+
)],
105+
)
106+
107+
cel_test(
108+
name = "example_0_test_with_policy",
109+
cel_expr = "basictest/policy.yaml",
110+
config = "basictest/config.yaml",
111+
filegroup = "//codelab:testrunner_codelab_resources",
112+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
113+
test_suite = "basictest/testsuite.yaml",
114+
test_targets = [make_test_targets(
115+
runtime = test_runtime.JAVA,
116+
test_srcs = [":basic_user_test"],
117+
)],
118+
)
119+
120+
cel_test(
121+
name = "example_1_test",
122+
cel_expr = "foo(a) || bar(a) == true",
123+
config = "basictest/config.yaml",
124+
filegroup = "//codelab:testrunner_codelab_resources",
125+
is_raw_expr = True,
126+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
127+
test_suite = "unknownoutputtest/testsuite.yaml",
128+
test_targets = [make_test_targets(
129+
runtime = test_runtime.JAVA,
130+
test_srcs = [":basic_user_test"],
131+
)],
132+
)
133+
134+
cel_test(
135+
name = "example_2_test",
136+
cel_expr = "basictest/policy.yaml",
137+
config = "basictest/config.yaml",
138+
filegroup = "//codelab:testrunner_codelab_resources",
139+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
140+
test_suite = "erroroutputtest/testsuite.yaml",
141+
test_targets = [make_test_targets(
142+
runtime = test_runtime.JAVA,
143+
test_srcs = [":error_output_user_test"],
144+
)],
145+
)
146+
147+
cel_test(
148+
name = "example_4_test",
149+
cel_expr = "basictest/expression.cel",
150+
config = "basictest/config.yaml",
151+
filegroup = "//codelab:testrunner_codelab_resources",
152+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
153+
test_targets = [make_test_targets(
154+
runtime = test_runtime.JAVA,
155+
test_srcs = [":basic_user_test"],
156+
deps = [":custom_test_suite"],
157+
)],
158+
)
159+
160+
cel_test(
161+
name = "example_5_test",
162+
cel_expr = "foo(a) || bar(a) == true",
163+
config = "basictest/config.yaml",
164+
filegroup = "//codelab:testrunner_codelab_resources",
165+
is_raw_expr = True,
166+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
167+
test_suite = "customvariablebindingtest/testsuite.yaml",
168+
test_targets = [make_test_targets(
169+
runtime = test_runtime.JAVA,
170+
test_srcs = [":custom_variable_binding_user_test"],
171+
)],
172+
)
173+
174+
cel_test(
175+
name = "example_6_test",
176+
cel_expr = "contextprototest/policy.yaml",
177+
config = "contextprototest/config.yaml",
178+
filegroup = "//codelab:testrunner_codelab_resources",
179+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
180+
test_suite = "contextprototest/testsuite.yaml",
181+
test_targets = [make_test_targets(
182+
runtime = test_runtime.JAVA,
183+
test_srcs = [":context_proto_user_test"],
184+
)],
185+
)
186+
187+
cel_test(
188+
name = "example_7_test",
189+
cel_expr = "foo(a) || bar(a) == true",
190+
config = "basictest/config.yaml",
191+
filegroup = "//codelab:testrunner_codelab_resources",
192+
is_raw_expr = True,
193+
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
194+
test_suite = "basictest/testsuite.yaml",
195+
test_targets = [make_test_targets(
196+
runtime = test_runtime.JAVA,
197+
test_srcs = [":late_function_binding_user_test"],
198+
)],
199+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package codelab.testrunner;
16+
17+
import dev.cel.bundle.CelFactory;
18+
import dev.cel.runtime.CelFunctionBinding;
19+
import dev.cel.testing.testrunner.CelTestContext;
20+
import dev.cel.testing.testrunner.CelUserTestTemplate;
21+
import org.junit.runner.RunWith;
22+
import org.junit.runners.Parameterized;
23+
24+
/** A sample basic user test class demonstrating a basic user journey. */
25+
@RunWith(Parameterized.class)
26+
public class BasicUserTest extends CelUserTestTemplate {
27+
28+
public BasicUserTest() {
29+
super(
30+
CelTestContext.newBuilder()
31+
.setCel(
32+
CelFactory.standardCelBuilder()
33+
.addFunctionBindings(
34+
CelFunctionBinding.from(
35+
"foo_id", String.class, (String a) -> a.equals("foo")))
36+
.addFunctionBindings(
37+
CelFunctionBinding.from(
38+
"bar_id", String.class, (String a) -> a.equals("bar")))
39+
.build())
40+
.build());
41+
}
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package codelab.testrunner;
16+
17+
import dev.cel.bundle.CelFactory;
18+
import dev.cel.checker.ProtoTypeMask;
19+
import dev.cel.expr.conformance.proto3.TestAllTypes;
20+
import dev.cel.testing.testrunner.CelTestContext;
21+
import dev.cel.testing.testrunner.CelUserTestTemplate;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.Parameterized;
24+
25+
/**
26+
* This test demonstrates the use case where the fields of a proto are used as variables in the CEL
27+
* expression i.e. context_expr.
28+
*/
29+
@RunWith(Parameterized.class)
30+
public class ContextProtoUserTest extends CelUserTestTemplate {
31+
32+
public ContextProtoUserTest() {
33+
super(
34+
CelTestContext.newBuilder()
35+
.setCel(
36+
CelFactory.standardCelBuilder()
37+
.addFileTypes(TestAllTypes.getDescriptor().getFile())
38+
.addProtoTypeMasks(
39+
ProtoTypeMask.ofAllFields(TestAllTypes.getDescriptor().getFullName())
40+
.withFieldsAsVariableDeclarations())
41+
.build())
42+
.build());
43+
}
44+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package codelab.testrunner;
16+
17+
import com.google.common.collect.ImmutableMap;
18+
import com.google.common.collect.ImmutableSet;
19+
import dev.cel.testing.testrunner.Annotations.TestSuiteSupplier;
20+
import dev.cel.testing.testrunner.CelTestSuite;
21+
import dev.cel.testing.testrunner.CelTestSuite.CelTestSection.CelTestCase.Input.Binding;
22+
23+
public final class CustomTestSuite {
24+
25+
@TestSuiteSupplier
26+
public CelTestSuite getCelTestSuite() {
27+
return CelTestSuite.newBuilder()
28+
.setDescription("Basic Tests")
29+
.setName("basic_tests")
30+
.setSections(
31+
ImmutableSet.of(
32+
CelTestSuite.CelTestSection.newBuilder()
33+
.setName("basic_test_sections")
34+
.setDescription("Basic Tests Sections")
35+
.setTests(
36+
ImmutableSet.of(
37+
CelTestSuite.CelTestSection.CelTestCase.newBuilder()
38+
.setName("true_by_default")
39+
.setDescription(
40+
"Test that the default value of a function is true.")
41+
.setInput(
42+
CelTestSuite.CelTestSection.CelTestCase.Input.ofBindings(
43+
ImmutableMap.of("a", Binding.ofValue("foo"))))
44+
.setOutput(
45+
CelTestSuite.CelTestSection.CelTestCase.Output.ofResultValue(
46+
true))
47+
.build(),
48+
CelTestSuite.CelTestSection.CelTestCase.newBuilder()
49+
.setName("false_by_default")
50+
.setDescription(
51+
"Test that the default value of a function is false.")
52+
.setInput(
53+
CelTestSuite.CelTestSection.CelTestCase.Input.ofBindings(
54+
ImmutableMap.of("a", Binding.ofValue("baz"))))
55+
.setOutput(
56+
CelTestSuite.CelTestSection.CelTestCase.Output.ofResultValue(
57+
false))
58+
.build()))
59+
.build()))
60+
.build();
61+
}
62+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package codelab.testrunner;
16+
17+
import com.google.common.collect.ImmutableMap;
18+
import dev.cel.bundle.CelFactory;
19+
import dev.cel.runtime.CelFunctionBinding;
20+
import dev.cel.testing.testrunner.CelTestContext;
21+
import dev.cel.testing.testrunner.CelUserTestTemplate;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.Parameterized;
24+
25+
/**
26+
* A sample user test class demonstrating custom variable binding in which we don't provide the
27+
* input values from testsuite rather programmatically via {@link CelTestContext}.
28+
*/
29+
@RunWith(Parameterized.class)
30+
public class CustomVariableBindingUserTest extends CelUserTestTemplate {
31+
32+
public CustomVariableBindingUserTest() {
33+
super(
34+
CelTestContext.newBuilder()
35+
.setCel(
36+
CelFactory.standardCelBuilder()
37+
.addFunctionBindings(
38+
CelFunctionBinding.from(
39+
"foo_id", String.class, (String a) -> a.equals("foo")))
40+
.addFunctionBindings(
41+
CelFunctionBinding.from(
42+
"bar_id", String.class, (String a) -> a.equals("bar")))
43+
.build())
44+
.setVariableBindings(ImmutableMap.of("a", "foo"))
45+
.build());
46+
}
47+
}

0 commit comments

Comments
 (0)