-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCelTestContext.java
More file actions
225 lines (187 loc) · 7.51 KB
/
CelTestContext.java
File metadata and controls
225 lines (187 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// 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 dev.cel.testing.testrunner;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.TypeRegistry;
import dev.cel.bundle.Cel;
import dev.cel.bundle.CelFactory;
import dev.cel.common.CelDescriptorUtil;
import dev.cel.common.CelOptions;
import dev.cel.policy.CelPolicyParser;
import dev.cel.runtime.CelLateFunctionBindings;
import dev.cel.testing.utils.ProtoDescriptorUtils;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
/**
* The context class for a CEL test, holding configurations needed to create environments and
* evaluate CEL expressions and policies.
*/
@AutoValue
public abstract class CelTestContext {
private static final Cel DEFAULT_CEL = CelFactory.standardCelBuilder().build();
/**
* The CEL environment for the CEL test.
*
* <p>The CEL environment is created by extending the provided base CEL environment with the
* config file if provided.
*/
public abstract Cel cel();
/**
* The CEL policy parser for the CEL test.
*
* <p>A custom parser to be used for parsing CEL policies in scenarios where custom policy tags
* are used. If not provided, the default CEL policy parser will be used.
*/
public abstract Optional<CelPolicyParser> celPolicyParser();
/**
* The CEL options for the CEL test.
*
* <p>The CEL options are used to configure the {@link Cel} environment.
*/
public abstract CelOptions celOptions();
/**
* The late function bindings for the CEL test.
*
* <p>These bindings are used to provide functions which are to be consumed during the eval phase
* directly.
*/
public abstract Optional<CelLateFunctionBindings> celLateFunctionBindings();
/** Interface for transforming bindings before evaluation. */
@FunctionalInterface
public interface BindingTransformer {
ImmutableMap<String, Object> transform(ImmutableMap<String, Object> bindings) throws Exception;
}
/**
* The binding transformer for the CEL test.
*
* <p>This transformer is used to transform the bindings before evaluation.
*/
public abstract Optional<BindingTransformer> bindingTransformer();
/**
* The variable bindings for the CEL test.
*
* <p>These bindings are used to provide values for variables for which it is difficult to provide
* a value in the test suite file for example, using proto extensions or fetching the value from
* some other source.
*/
public abstract ImmutableMap<String, Object> variableBindings();
/**
* The result matcher for the CEL test.
*
* <p>This matcher is used to perform assertions on the result of a CEL test case.
*/
public abstract ResultMatcher resultMatcher();
/**
* The CEL expression to be tested. Could be a expression string or a policy/cel file path. This
* should only be used when invoking the runner library directly.
*/
public abstract Optional<CelExpressionSource> celExpression();
/**
* The config file for the CEL test.
*
* <p>The config file is used to provide a custom environment for the CEL test.
*/
public abstract Optional<String> configFile();
/**
* The file descriptor set path for the CEL test.
*
* <p>The file descriptor set path is used to provide proto descriptors for the CEL test.
*/
public abstract Optional<String> fileDescriptorSetPath();
abstract ImmutableSet<Descriptor> messageTypes();
abstract ImmutableSet<FileDescriptor> fileTypes();
@Memoized
public Optional<TypeRegistry> typeRegistry() {
if (messageTypes().isEmpty() && fileTypes().isEmpty() && !fileDescriptorSetPath().isPresent()) {
return Optional.empty();
}
TypeRegistry.Builder builder = TypeRegistry.newBuilder();
if (!messageTypes().isEmpty()) {
builder.add(messageTypes());
}
if (!fileTypes().isEmpty()) {
builder.add(
CelDescriptorUtil.getAllDescriptorsFromFileDescriptor(fileTypes())
.messageTypeDescriptors());
}
if (fileDescriptorSetPath().isPresent()) {
try {
builder.add(
ProtoDescriptorUtils.getAllDescriptorsFromJvm(fileDescriptorSetPath().get())
.messageTypeDescriptors());
} catch (IOException e) {
throw new IllegalStateException(
"Failed to load descriptors from path: " + fileDescriptorSetPath().get(), e);
}
}
return Optional.of(builder.build());
}
public abstract Optional<ExtensionRegistry> extensionRegistry();
/** Returns a builder for {@link CelTestContext} with the current instance's values. */
public abstract Builder toBuilder();
/** Returns a new builder for {@link CelTestContext}. */
public static CelTestContext.Builder newBuilder() {
return new AutoValue_CelTestContext.Builder()
.setCel(DEFAULT_CEL)
.setCelOptions(CelOptions.DEFAULT)
.setVariableBindings(ImmutableMap.of())
.setResultMatcher(new DefaultResultMatcher());
}
/** Builder for {@link CelTestContext}. */
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setCel(Cel cel);
public abstract Builder setCelPolicyParser(CelPolicyParser celPolicyParser);
public abstract Builder setCelOptions(CelOptions celOptions);
public abstract Builder setCelLateFunctionBindings(
CelLateFunctionBindings celLateFunctionBindings);
public abstract Builder setBindingTransformer(BindingTransformer bindingTransformer);
public abstract Builder setVariableBindings(Map<String, Object> variableBindings);
public abstract Builder setResultMatcher(ResultMatcher resultMatcher);
public abstract Builder setCelExpression(CelExpressionSource celExpression);
public abstract Builder setConfigFile(String configFile);
public abstract Builder setFileDescriptorSetPath(String fileDescriptorSetPath);
abstract ImmutableSet.Builder<Descriptor> messageTypesBuilder();
abstract ImmutableSet.Builder<FileDescriptor> fileTypesBuilder();
public Builder addMessageTypes(Descriptor... descriptors) {
for (Descriptor d : descriptors) {
messageTypesBuilder().add(d);
}
return this;
}
public Builder addMessageTypes(Iterable<Descriptor> descriptors) {
messageTypesBuilder().addAll(descriptors);
return this;
}
public Builder addFileTypes(FileDescriptor... fileDescriptors) {
for (FileDescriptor fd : fileDescriptors) {
fileTypesBuilder().add(fd);
}
return this;
}
public Builder addFileTypes(Iterable<FileDescriptor> fileDescriptors) {
fileTypesBuilder().addAll(fileDescriptors);
return this;
}
public abstract Builder setExtensionRegistry(ExtensionRegistry extensionRegistry);
public abstract CelTestContext build();
}
}