|
| 1 | +// Copyright 2024 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; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | +import static org.junit.Assert.assertThrows; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import com.google.rpc.context.AttributeContext; |
| 22 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 23 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 24 | +import dev.cel.common.CelValidationException; |
| 25 | +import dev.cel.common.CelValidationResult; |
| 26 | +import dev.cel.parser.CelUnparser; |
| 27 | +import dev.cel.parser.CelUnparserFactory; |
| 28 | +import dev.cel.runtime.CelEvaluationException; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | + |
| 32 | +@RunWith(TestParameterInjector.class) |
| 33 | +public final class Exercise8Test { |
| 34 | + |
| 35 | + private final Exercise8 exercise8 = new Exercise8(); |
| 36 | + |
| 37 | + @Test |
| 38 | + public void validate_invalidTimestampLiteral_returnsError() throws Exception { |
| 39 | + CelAbstractSyntaxTree ast = exercise8.compile("timestamp('bad')"); |
| 40 | + |
| 41 | + CelValidationResult validationResult = exercise8.validate(ast); |
| 42 | + |
| 43 | + assertThat(validationResult.hasError()).isTrue(); |
| 44 | + assertThat(validationResult.getErrorString()) |
| 45 | + .isEqualTo( |
| 46 | + "ERROR: <input>:1:11: timestamp validation failed. Reason: evaluation error: Failed to" |
| 47 | + + " parse timestamp: invalid timestamp \"bad\"\n" |
| 48 | + + " | timestamp('bad')\n" |
| 49 | + + " | ..........^"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void validate_invalidDurationLiteral_returnsError() throws Exception { |
| 54 | + CelAbstractSyntaxTree ast = exercise8.compile("duration('bad')"); |
| 55 | + |
| 56 | + CelValidationResult validationResult = exercise8.validate(ast); |
| 57 | + |
| 58 | + assertThat(validationResult.hasError()).isTrue(); |
| 59 | + assertThat(validationResult.getErrorString()) |
| 60 | + .isEqualTo( |
| 61 | + "ERROR: <input>:1:10: duration validation failed. Reason: evaluation error: invalid" |
| 62 | + + " duration format\n" |
| 63 | + + " | duration('bad')\n" |
| 64 | + + " | .........^"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void validate_invalidRegexLiteral_returnsError() throws Exception { |
| 69 | + CelAbstractSyntaxTree ast = exercise8.compile("'text'.matches('**')"); |
| 70 | + |
| 71 | + CelValidationResult validationResult = exercise8.validate(ast); |
| 72 | + |
| 73 | + assertThat(validationResult.hasError()).isTrue(); |
| 74 | + assertThat(validationResult.getErrorString()) |
| 75 | + .isEqualTo( |
| 76 | + "ERROR: <input>:1:16: Regex validation failed. Reason: Dangling meta character '*' near" |
| 77 | + + " index 0\n" |
| 78 | + + "**\n" |
| 79 | + + "^\n" |
| 80 | + + " | 'text'.matches('**')\n" |
| 81 | + + " | ...............^"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void validate_listHasMixedLiterals_throws() throws Exception { |
| 86 | + CelAbstractSyntaxTree ast = exercise8.compile("3 in [1, 2, '3']"); |
| 87 | + |
| 88 | + // Note that `CelValidationResult` is the same result class used for the compilation path. This |
| 89 | + // means you could alternatively invoke `.getAst()` and handle `CelValidationException` as |
| 90 | + // usual. |
| 91 | + CelValidationResult validationResult = exercise8.validate(ast); |
| 92 | + |
| 93 | + CelValidationException e = assertThrows(CelValidationException.class, validationResult::getAst); |
| 94 | + assertThat(e) |
| 95 | + .hasMessageThat() |
| 96 | + .contains( |
| 97 | + "ERROR: <input>:1:13: expected type 'int' but found 'string'\n" |
| 98 | + + " | 3 in [1, 2, '3']\n" |
| 99 | + + " | ............^"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void optimize_constantFold_success() throws Exception { |
| 104 | + CelUnparser celUnparser = CelUnparserFactory.newUnparser(); |
| 105 | + CelAbstractSyntaxTree ast = exercise8.compile("(1 + 2 + 3 == x) && (x in [1, 2, x])"); |
| 106 | + |
| 107 | + CelAbstractSyntaxTree optimizedAst = exercise8.optimize(ast); |
| 108 | + |
| 109 | + assertThat(celUnparser.unparse(optimizedAst)).isEqualTo("6 == x"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void optimize_constantFold_evaluateError() throws Exception { |
| 114 | + CelAbstractSyntaxTree ast = |
| 115 | + exercise8.compile("request.headers.referer == 'https://' + 'cel.dev'"); |
| 116 | + CelAbstractSyntaxTree optimizedAst = exercise8.optimize(ast); |
| 117 | + ImmutableMap<String, AttributeContext.Request> runtimeParameters = |
| 118 | + ImmutableMap.of("request", AttributeContext.Request.getDefaultInstance()); |
| 119 | + |
| 120 | + CelEvaluationException e1 = |
| 121 | + assertThrows(CelEvaluationException.class, () -> exercise8.eval(ast, runtimeParameters)); |
| 122 | + CelEvaluationException e2 = |
| 123 | + assertThrows( |
| 124 | + CelEvaluationException.class, () -> exercise8.eval(optimizedAst, runtimeParameters)); |
| 125 | + // Note that the errors below differ by their source position. |
| 126 | + assertThat(e1) |
| 127 | + .hasMessageThat() |
| 128 | + .contains("evaluation error at <input>:15: key 'referer' is not present in map."); |
| 129 | + assertThat(e2) |
| 130 | + .hasMessageThat() |
| 131 | + .contains("evaluation error at <input>:0: key 'referer' is not present in map."); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void optimize_commonSubexpressionElimination_success() throws Exception { |
| 136 | + CelUnparser celUnparser = CelUnparserFactory.newUnparser(); |
| 137 | + CelAbstractSyntaxTree ast = |
| 138 | + exercise8.compile( |
| 139 | + "request.auth.claims.group == 'admin' || request.auth.claims.group == 'user'"); |
| 140 | + |
| 141 | + CelAbstractSyntaxTree optimizedAst = exercise8.optimize(ast); |
| 142 | + |
| 143 | + assertThat(celUnparser.unparse(optimizedAst)) |
| 144 | + .isEqualTo( |
| 145 | + "cel.@block([request.auth.claims.group], @index0 == \"admin\" || @index0 == \"user\")"); |
| 146 | + } |
| 147 | +} |
0 commit comments