Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions codelab/src/test/codelab/testrunner/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
load("@rules_java//java:java_library.bzl", "java_library")
load("//third_party/cel/expr:cel_test.bzl", "cel_test", "make_test_targets", "test_runtime")

package(
default_applicable_licenses = ["//:license"],
default_testonly = 1,
)

java_library(
name = "basic_user_test",
srcs = ["BasicUserTest.java"],
deps = [
"//bundle:cel",
"//runtime:function_binding",
"//testing/testrunner:cel_test_context",
"//testing/testrunner:cel_user_test_template",
"@maven//:junit_junit",
],
)

java_library(
name = "error_output_user_test",
srcs = ["ErrorOutputUserTest.java"],
deps = [
"//testing/testrunner:cel_test_context",
"//testing/testrunner:cel_user_test_template",
"@maven//:junit_junit",
],
)

java_library(
name = "context_proto_user_test",
srcs = ["ContextProtoUserTest.java"],
deps = [
"//bundle:cel",
"//checker:proto_type_mask",
"//testing/testrunner:cel_test_context",
"//testing/testrunner:cel_user_test_template",
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",
"@maven//:junit_junit",
],
)

java_library(
name = "late_function_binding_user_test",
srcs = ["LateFunctionBindingUserTest.java"],
deps = [
"//bundle:cel",
"//runtime",
"//runtime:function_binding",
"//testing/testrunner:cel_test_context",
"//testing/testrunner:cel_user_test_template",
"@maven//:junit_junit",
],
)

java_library(
name = "custom_variable_binding_user_test",
srcs = ["CustomVariableBindingUserTest.java"],
deps = [
"//bundle:cel",
"//runtime:function_binding",
"//testing/testrunner:cel_test_context",
"//testing/testrunner:cel_user_test_template",
"@maven//:com_google_guava_guava",
"@maven//:junit_junit",
],
)

java_library(
name = "custom_test_suite",
srcs = ["CustomTestSuite.java"],
deps = [
"//testing/testrunner:annotations",
"//testing/testrunner:cel_test_suite",
"@maven//:com_google_guava_guava",
],
)

cel_test(
name = "example_0_test",
cel_expr = "basictest/expression.cel",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources", # Users can use exports_files with appropriate visibility in which case filegroup is not required.
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "basictest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":basic_user_test"],
)],
)

cel_test(
name = "example_0_test_with_raw_expression",
cel_expr = "foo(a) || bar(a) == true",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
is_raw_expr = True,
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "basictest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":basic_user_test"],
)],
)

cel_test(
name = "example_0_test_with_policy",
cel_expr = "basictest/policy.yaml",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "basictest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":basic_user_test"],
)],
)

cel_test(
name = "example_1_test",
cel_expr = "foo(a) || bar(a) == true",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
is_raw_expr = True,
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "unknownoutputtest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":basic_user_test"],
)],
)

cel_test(
name = "example_2_test",
cel_expr = "basictest/policy.yaml",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "erroroutputtest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":error_output_user_test"],
)],
)

cel_test(
name = "example_4_test",
cel_expr = "basictest/expression.cel",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":basic_user_test"],
deps = [":custom_test_suite"],
)],
)

cel_test(
name = "example_5_test",
cel_expr = "foo(a) || bar(a) == true",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
is_raw_expr = True,
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "customvariablebindingtest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":custom_variable_binding_user_test"],
)],
)

cel_test(
name = "example_6_test",
cel_expr = "contextprototest/policy.yaml",
config = "contextprototest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "contextprototest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":context_proto_user_test"],
)],
)

cel_test(
name = "example_7_test",
cel_expr = "foo(a) || bar(a) == true",
config = "basictest/config.yaml",
filegroup = "//codelab:testrunner_codelab_resources",
is_raw_expr = True,
test_data_path = "//codelab/src/test/codelab/testrunner/resources",
test_suite = "basictest/testsuite.yaml",
test_targets = [make_test_targets(
runtime = test_runtime.JAVA,
test_srcs = [":late_function_binding_user_test"],
)],
)
42 changes: 42 additions & 0 deletions codelab/src/test/codelab/testrunner/BasicUserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package codelab.testrunner;

import dev.cel.bundle.CelFactory;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.testing.testrunner.CelTestContext;
import dev.cel.testing.testrunner.CelUserTestTemplate;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/** A sample basic user test class demonstrating a basic user journey. */
@RunWith(Parameterized.class)
public class BasicUserTest extends CelUserTestTemplate {

public BasicUserTest() {
super(
CelTestContext.newBuilder()
.setCel(
CelFactory.standardCelBuilder()
.addFunctionBindings(
CelFunctionBinding.from(
"foo_id", String.class, (String a) -> a.equals("foo")))
.addFunctionBindings(
CelFunctionBinding.from(
"bar_id", String.class, (String a) -> a.equals("bar")))
.build())
.build());
}
}
44 changes: 44 additions & 0 deletions codelab/src/test/codelab/testrunner/ContextProtoUserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package codelab.testrunner;

import dev.cel.bundle.CelFactory;
import dev.cel.checker.ProtoTypeMask;
import dev.cel.expr.conformance.proto3.TestAllTypes;
import dev.cel.testing.testrunner.CelTestContext;
import dev.cel.testing.testrunner.CelUserTestTemplate;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* This test demonstrates the use case where the fields of a proto are used as variables in the CEL
* expression i.e. context_expr.
*/
@RunWith(Parameterized.class)
public class ContextProtoUserTest extends CelUserTestTemplate {

public ContextProtoUserTest() {
super(
CelTestContext.newBuilder()
.setCel(
CelFactory.standardCelBuilder()
.addFileTypes(TestAllTypes.getDescriptor().getFile())
.addProtoTypeMasks(
ProtoTypeMask.ofAllFields(TestAllTypes.getDescriptor().getFullName())
.withFieldsAsVariableDeclarations())
.build())
.build());
}
}
62 changes: 62 additions & 0 deletions codelab/src/test/codelab/testrunner/CustomTestSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package codelab.testrunner;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import dev.cel.testing.testrunner.Annotations.TestSuiteSupplier;
import dev.cel.testing.testrunner.CelTestSuite;
import dev.cel.testing.testrunner.CelTestSuite.CelTestSection.CelTestCase.Input.Binding;

public final class CustomTestSuite {

@TestSuiteSupplier
public CelTestSuite getCelTestSuite() {
return CelTestSuite.newBuilder()
.setDescription("Basic Tests")
.setName("basic_tests")
.setSections(
ImmutableSet.of(
CelTestSuite.CelTestSection.newBuilder()
.setName("basic_test_sections")
.setDescription("Basic Tests Sections")
.setTests(
ImmutableSet.of(
CelTestSuite.CelTestSection.CelTestCase.newBuilder()
.setName("true_by_default")
.setDescription(
"Test that the default value of a function is true.")
.setInput(
CelTestSuite.CelTestSection.CelTestCase.Input.ofBindings(
ImmutableMap.of("a", Binding.ofValue("foo"))))
.setOutput(
CelTestSuite.CelTestSection.CelTestCase.Output.ofResultValue(
true))
.build(),
CelTestSuite.CelTestSection.CelTestCase.newBuilder()
.setName("false_by_default")
.setDescription(
"Test that the default value of a function is false.")
.setInput(
CelTestSuite.CelTestSection.CelTestCase.Input.ofBindings(
ImmutableMap.of("a", Binding.ofValue("baz"))))
.setOutput(
CelTestSuite.CelTestSection.CelTestCase.Output.ofResultValue(
false))
.build()))
.build()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package codelab.testrunner;

import com.google.common.collect.ImmutableMap;
import dev.cel.bundle.CelFactory;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.testing.testrunner.CelTestContext;
import dev.cel.testing.testrunner.CelUserTestTemplate;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* A sample user test class demonstrating custom variable binding in which we don't provide the
* input values from testsuite rather programmatically via {@link CelTestContext}.
*/
@RunWith(Parameterized.class)
public class CustomVariableBindingUserTest extends CelUserTestTemplate {

public CustomVariableBindingUserTest() {
super(
CelTestContext.newBuilder()
.setCel(
CelFactory.standardCelBuilder()
.addFunctionBindings(
CelFunctionBinding.from(
"foo_id", String.class, (String a) -> a.equals("foo")))
.addFunctionBindings(
CelFunctionBinding.from(
"bar_id", String.class, (String a) -> a.equals("bar")))
.build())
.setVariableBindings(ImmutableMap.of("a", "foo"))
.build());
}
}
Loading
Loading