Repository navigation
Expand file tree
/
Copy pathStructValueTest.java
More file actions
285 lines (234 loc) · 8.95 KB
/
Copy pathStructValueTest.java
File metadata and controls
285 lines (234 loc) · 8.95 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2023 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.common.values;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import com.google.testing.junit.testparameterinjector.TestParameters;
import dev.cel.bundle.Cel;
import dev.cel.bundle.CelFactory;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelOptions;
import dev.cel.common.internal.DynamicProto;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.StructType;
import java.util.Map;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(TestParameterInjector.class)
public final class StructValueTest {
private static final StructType CUSTOM_STRUCT_TYPE =
StructType.create(
"custom_struct",
ImmutableSet.of("data"),
fieldName -> fieldName.equals("data") ? Optional.of(SimpleType.INT) : Optional.empty());
private static final CelTypeProvider CUSTOM_STRUCT_TYPE_PROVIDER =
new CelTypeProvider() {
@Override
public ImmutableList<CelType> types() {
return ImmutableList.of(CUSTOM_STRUCT_TYPE);
}
@Override
public Optional<CelType> findType(String typeName) {
return typeName.equals(CUSTOM_STRUCT_TYPE.name())
? Optional.of(CUSTOM_STRUCT_TYPE)
: Optional.empty();
}
};
private static final CelValueProvider CUSTOM_STRUCT_VALUE_PROVIDER =
new CelValueProvider() {
@Override
public Optional<Object> newValue(String structType, Map<String, Object> fields) {
if (structType.equals(CUSTOM_STRUCT_TYPE.name())) {
return Optional.of(new CelCustomStructValue(fields));
}
return Optional.empty();
}
@Override
public CelValueConverter celValueConverter() {
return new CelValueConverter() {
@Override
public Object toRuntimeValue(Object value) {
if (value instanceof CustomPojo) {
return new CelCustomStructValue((CustomPojo) value);
}
return super.toRuntimeValue(value);
}
};
}
};
@Test
public void emptyStruct() {
CelCustomStructValue celCustomStruct = new CelCustomStructValue(0);
assertThat(celCustomStruct.value().getData()).isEqualTo(0L);
assertThat(celCustomStruct.isZeroValue()).isTrue();
}
@Test
public void constructStruct() {
CelCustomStructValue celCustomStruct = new CelCustomStructValue(5);
assertThat(celCustomStruct.value().getData()).isEqualTo(5L);
assertThat(celCustomStruct.isZeroValue()).isFalse();
}
@Test
public void selectField_success() {
CelCustomStructValue celCustomStruct = new CelCustomStructValue(5);
assertThat(celCustomStruct.select("data")).isEqualTo(5L);
}
@Test
public void selectField_nonExistentField_throws() {
CelCustomStructValue celCustomStruct = new CelCustomStructValue(5);
assertThrows(IllegalArgumentException.class, () -> celCustomStruct.select("bogus"));
}
@Test
@TestParameters("{fieldName: 'data', expectedResult: true}")
@TestParameters("{fieldName: 'bogus', expectedResult: false}")
public void findField_success(String fieldName, boolean expectedResult) {
CelCustomStructValue celCustomStruct = new CelCustomStructValue(5);
assertThat(celCustomStruct.find(fieldName).isPresent()).isEqualTo(expectedResult);
}
@Test
public void celTypeTest() {
CelCustomStructValue value = new CelCustomStructValue(0);
assertThat(value.celType()).isEqualTo(CUSTOM_STRUCT_TYPE);
}
@Test
public void evaluate_typeOfCustomStruct() throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.addVar("a", CUSTOM_STRUCT_TYPE)
.setTypeProvider(CUSTOM_STRUCT_TYPE_PROVIDER)
.setValueProvider(CUSTOM_STRUCT_VALUE_PROVIDER)
.build();
CelAbstractSyntaxTree ast = cel.compile("type(a) == custom_struct").getAst();
Object result = cel.createProgram(ast).eval(ImmutableMap.of("a", new CelCustomStructValue(20)));
assertThat(result).isEqualTo(true);
}
@Test
public void evaluate_usingCustomClass_createNewStruct() throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.setTypeProvider(CUSTOM_STRUCT_TYPE_PROVIDER)
.setValueProvider(CUSTOM_STRUCT_VALUE_PROVIDER)
.build();
CelAbstractSyntaxTree ast = cel.compile("custom_struct{data: 50}").getAst();
CustomPojo result = (CustomPojo) cel.createProgram(ast).eval();
assertThat(result.getData()).isEqualTo(50);
}
@Test
public void evaluate_usingCustomClass_asVariable() throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.addVar("a", CUSTOM_STRUCT_TYPE)
.setTypeProvider(CUSTOM_STRUCT_TYPE_PROVIDER)
.setValueProvider(CUSTOM_STRUCT_VALUE_PROVIDER)
.build();
CelAbstractSyntaxTree ast = cel.compile("a").getAst();
CustomPojo result =
(CustomPojo)
cel.createProgram(ast).eval(ImmutableMap.of("a", new CelCustomStructValue(10)));
assertThat(result.getData()).isEqualTo(10);
}
@Test
public void evaluate_usingCustomClass_asVariableSelectField() throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.addVar("a", CUSTOM_STRUCT_TYPE)
.setTypeProvider(CUSTOM_STRUCT_TYPE_PROVIDER)
.setValueProvider(CUSTOM_STRUCT_VALUE_PROVIDER)
.build();
CelAbstractSyntaxTree ast = cel.compile("a.data").getAst();
assertThat(cel.createProgram(ast).eval(ImmutableMap.of("a", new CelCustomStructValue(20))))
.isEqualTo(20L);
}
@Test
public void evaluate_usingCustomClass_selectField() throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.setTypeProvider(CUSTOM_STRUCT_TYPE_PROVIDER)
.setValueProvider(CUSTOM_STRUCT_VALUE_PROVIDER)
.build();
CelAbstractSyntaxTree ast = cel.compile("custom_struct{data: 5}.data").getAst();
Object result = cel.createProgram(ast).eval();
assertThat(result).isEqualTo(5L);
}
@Test
public void evaluate_usingMultipleProviders_selectFieldFromCustomClass() throws Exception {
Cel cel =
CelFactory.plannerCelBuilder()
.setTypeProvider(CUSTOM_STRUCT_TYPE_PROVIDER)
.setValueProvider(
CombinedCelValueProvider.combine(
ProtoMessageValueProvider.newInstance(
CelOptions.DEFAULT, DynamicProto.create(unused -> Optional.empty())),
CUSTOM_STRUCT_VALUE_PROVIDER))
.build();
CelAbstractSyntaxTree ast = cel.compile("custom_struct{data: 5}.data").getAst();
Object result = cel.createProgram(ast).eval();
assertThat(result).isEqualTo(5L);
}
// TODO: Bring back evaluate_usingMultipleProviders_selectFieldFromProtobufMessage
// once planner is exposed from factory
private static class CustomPojo {
private final long data;
CustomPojo(long data) {
this.data = data;
}
long getData() {
return data;
}
}
@SuppressWarnings("Immutable") // Test only
private static class CelCustomStructValue extends StructValue<String, CustomPojo> {
private final CustomPojo pojo;
@Override
public CustomPojo value() {
return pojo;
}
@Override
public boolean isZeroValue() {
return pojo.getData() == 0;
}
@Override
public CelType celType() {
return CUSTOM_STRUCT_TYPE;
}
@Override
public Object select(String field) {
return find(field)
.orElseThrow(() -> new IllegalArgumentException("Invalid field name: " + field));
}
@Override
public Optional<Object> find(String field) {
if (field.equals("data")) {
return Optional.of(pojo.getData());
}
return Optional.empty();
}
private CelCustomStructValue(Map<String, Object> fields) {
this((long) fields.get("data"));
}
private CelCustomStructValue(long data) {
this.pojo = new CustomPojo(data);
}
private CelCustomStructValue(CustomPojo pojo) {
this.pojo = pojo;
}
}
}