diff --git a/common/src/main/java/dev/cel/common/formats/YamlHelper.java b/common/src/main/java/dev/cel/common/formats/YamlHelper.java index 33e783dec..7a8215d2f 100644 --- a/common/src/main/java/dev/cel/common/formats/YamlHelper.java +++ b/common/src/main/java/dev/cel/common/formats/YamlHelper.java @@ -14,13 +14,16 @@ package dev.cel.common.formats; +import static com.google.common.collect.ImmutableMap.toImmutableMap; import static java.util.Arrays.stream; import static java.util.stream.Collectors.joining; import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableMap; import java.io.StringReader; import java.util.List; import java.util.Optional; +import java.util.function.Function; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; @@ -42,9 +45,13 @@ public enum YamlNodeType { LIST("tag:yaml.org,2002:seq"), ; + public static final ImmutableMap TAG_TO_NODE_TYPE = + stream(YamlNodeType.values()) + .collect(toImmutableMap(YamlNodeType::tag, Function.identity())); + private final String tag; - String tag() { + public String tag() { return tag; } @@ -97,6 +104,15 @@ public static boolean validateYamlType(Node node, YamlNodeType... expectedNodeTy return false; } + public static Double newDouble(ParserContext ctx, Node node) { + long id = ctx.collectMetadata(node); + if (!assertYamlType(ctx, id, node, YamlNodeType.DOUBLE)) { + return 0.0; + } + + return Double.parseDouble(((ScalarNode) node).getValue()); + } + public static Integer newInteger(ParserContext ctx, Node node) { long id = ctx.collectMetadata(node); if (!assertYamlType(ctx, id, node, YamlNodeType.INTEGER)) { diff --git a/testing/src/test/resources/policy/custom_variable_bindings/tests.yaml b/testing/src/test/resources/policy/custom_variable_bindings/tests.yaml index dac470503..dd924141c 100644 --- a/testing/src/test/resources/policy/custom_variable_bindings/tests.yaml +++ b/testing/src/test/resources/policy/custom_variable_bindings/tests.yaml @@ -12,11 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +name: "custom_variable_bindings" description: "Tests for custom variable bindings." -section: +sections: - name: "custom_variable_bindings" + description: "Tests for custom variable bindings." tests: - name: "true_by_default" + description: "Tests that the custom variable bindings are set to true by default." # The input for this test is configured programmatically in the test # class with a value of 1 (see CustomVariableBindingsUserTest.java). - output: "true" \ No newline at end of file + output: + expr: "true" \ No newline at end of file diff --git a/testing/src/test/resources/policy/late_function_binding/tests.yaml b/testing/src/test/resources/policy/late_function_binding/tests.yaml index 63cd3f092..cfdaa7ca3 100644 --- a/testing/src/test/resources/policy/late_function_binding/tests.yaml +++ b/testing/src/test/resources/policy/late_function_binding/tests.yaml @@ -12,17 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. +name: "late_function_binding_tests" description: "Tests for late function binding." -section: - - name: "late_function_binding" +sections: + - name: "late_function_binding_tests_section" + description: "Tests for late function binding." tests: - name: "true_by_default" + description: "Test that the default value of a late function binding is true." input: a: - value: "foo" - output: "true" + expr: "'foo'" + output: + value: true - name: "false_by_default" + description: "Test that the default value of a late function binding is false." input: a: value: "baz" - output: "false" \ No newline at end of file + output: + value: false \ No newline at end of file diff --git a/testing/src/test/resources/policy/nested_rule/eval_error_policy.yaml b/testing/src/test/resources/policy/nested_rule/eval_error_policy.yaml index 0fab7e5f0..2f1233ea1 100644 --- a/testing/src/test/resources/policy/nested_rule/eval_error_policy.yaml +++ b/testing/src/test/resources/policy/nested_rule/eval_error_policy.yaml @@ -30,9 +30,9 @@ rule: match: - condition: | resource.origin in variables.banned_regions && - !(resource.origin in variables.permitted_regions) && foo(resource.origin) + !(resource.origin in variables.permitted_regions) output: "{'banned': true}" - - condition: resource.origin in variables.permitted_regions && foo(resource.origin) + - condition: foo(resource.origin) && resource.origin in variables.permitted_regions output: "{'banned': false}" - output: "{'banned': true}" explanation: "'resource is in the banned region ' + resource.origin" \ No newline at end of file diff --git a/testing/src/test/resources/policy/nested_rule/eval_error_tests.yaml b/testing/src/test/resources/policy/nested_rule/eval_error_tests.yaml new file mode 100644 index 000000000..7c38c6b9c --- /dev/null +++ b/testing/src/test/resources/policy/nested_rule/eval_error_tests.yaml @@ -0,0 +1,29 @@ +# Copyright 2024 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. + +name: "eval_error" +description: evaluation error tests +sections: + - name: "eval_error" + description: "Tests for evaluation errors" + tests: + - name: "eval_error_no_matching_overload" + description: "No matching overload for function" + input: + resource: + value: + origin: "uk" + output: + error_set: + - "evaluation error: No matching overload for function 'foo'. Overload candidates: foo_id" diff --git a/testing/src/test/resources/policy/nested_rule/testrunner_tests.yaml b/testing/src/test/resources/policy/nested_rule/testrunner_tests.yaml new file mode 100644 index 000000000..414784ad8 --- /dev/null +++ b/testing/src/test/resources/policy/nested_rule/testrunner_tests.yaml @@ -0,0 +1,47 @@ +# Copyright 2024 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. + +name: "nested_rule" +description: Nested rule conformance tests +sections: + - name: "banned" + description: "Tests for the banned section." + tests: + - name: "restricted_origin" + description: "Tests that the ir origin is restricted." + input: + resource: + value: + origin: "ir" + output: + expr: "{'banned': true}" + - name: "by_default" + description: "Tests that the de origin is restricted." + input: + resource: + value: + origin: "de" + output: + expr: "{'banned': true}" + - name: "permitted" + description: "Tests for the permitted section." + tests: + - name: "valid_origin" + description: "Tests that the valid origin is permitted." + input: + resource: + value: + origin: "uk" + output: + expr: "{'banned': false}"