-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProtoAdapterTest.java
More file actions
296 lines (273 loc) · 12.1 KB
/
Copy pathProtoAdapterTest.java
File metadata and controls
296 lines (273 loc) · 12.1 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
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2022 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.internal;
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.primitives.UnsignedLong;
import com.google.protobuf.Any;
import com.google.protobuf.BoolValue;
import com.google.protobuf.ByteString;
import com.google.protobuf.BytesValue;
import com.google.protobuf.DoubleValue;
import com.google.protobuf.Duration;
import com.google.protobuf.Empty;
import com.google.protobuf.FieldMask;
import com.google.protobuf.FloatValue;
import com.google.protobuf.Int64Value;
import com.google.protobuf.ListValue;
import com.google.protobuf.Message;
import com.google.protobuf.NullValue;
import com.google.protobuf.StringValue;
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.UInt64Value;
import com.google.protobuf.Value;
import com.google.type.Expr;
import dev.cel.common.CelOptions;
import dev.cel.common.values.CelByteString;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Enclosed.class)
public final class ProtoAdapterTest {
private static final DynamicProto DYNAMIC_PROTO =
DynamicProto.create(DefaultMessageFactory.INSTANCE);
@RunWith(Parameterized.class)
public static class BidirectionalConversionTest {
@Parameter(0)
public Object value;
@Parameter(1)
public Message proto;
@Parameters
public static List<Object[]> data() {
return Arrays.asList(
new Object[][] {
{
dev.cel.common.values.NullValue.NULL_VALUE,
Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()),
},
{true, BoolValue.of(true)},
{true, Any.pack(BoolValue.of(true))},
{true, Value.newBuilder().setBoolValue(true).build()},
{
CelByteString.copyFromUtf8("hello"), BytesValue.of(ByteString.copyFromUtf8("hello")),
},
{
CelByteString.copyFromUtf8("hello"),
Any.pack(BytesValue.of(ByteString.copyFromUtf8("hello"))),
},
{1.5D, DoubleValue.of(1.5D)},
{1.5D, Any.pack(DoubleValue.of(1.5D))},
{1.5D, Value.newBuilder().setNumberValue(1.5D).build()},
{
java.time.Duration.ofSeconds(123), Duration.newBuilder().setSeconds(123).build(),
},
{
java.time.Duration.ofSeconds(123),
Any.pack(Duration.newBuilder().setSeconds(123).build()),
},
{1L, Int64Value.of(1L)},
{1L, Any.pack(Int64Value.of(1L))},
{UnsignedLong.valueOf(1L), UInt64Value.of(1L)},
{"hello", StringValue.of("hello")},
{"hello", Any.pack(StringValue.of("hello"))},
{"hello", Value.newBuilder().setStringValue("hello").build()},
{
Arrays.asList("hello", "world"),
Any.pack(
ListValue.newBuilder()
.addValues(Value.newBuilder().setStringValue("hello"))
.addValues(Value.newBuilder().setStringValue("world"))
.build()),
},
{
ImmutableMap.of("hello", "world"),
Any.pack(
Struct.newBuilder()
.putFields("hello", Value.newBuilder().setStringValue("world").build())
.build()),
},
{
ImmutableMap.of(
"list_value",
ImmutableList.of(false, dev.cel.common.values.NullValue.NULL_VALUE)),
Struct.newBuilder()
.putFields(
"list_value",
Value.newBuilder()
.setListValue(
ListValue.newBuilder()
.addValues(Value.newBuilder().setBoolValue(false))
.addValues(Value.newBuilder().setNullValue(NullValue.NULL_VALUE)))
.build())
.build(),
},
{
Instant.ofEpochSecond(123), Timestamp.newBuilder().setSeconds(123).build(),
},
{
Instant.ofEpochSecond(123), Any.pack(Timestamp.newBuilder().setSeconds(123).build()),
},
{UnsignedLong.valueOf(1L), UInt64Value.of(1L)},
{UnsignedLong.valueOf(1L), Any.pack(UInt64Value.of(1L))},
{Empty.getDefaultInstance(), Empty.getDefaultInstance()},
{
FieldMask.newBuilder().addPaths("foo").build(),
FieldMask.newBuilder().addPaths("foo").build()
}
});
}
@Test
public void adaptValueToProto_bidirectionalConversion() {
DynamicProto dynamicProto = DynamicProto.create(DefaultMessageFactory.INSTANCE);
ProtoAdapter protoAdapter =
new ProtoAdapter(
dynamicProto,
CelOptions.current().build());
assertThat(protoAdapter.adaptValueToProto(value, proto.getDescriptorForType().getFullName()))
.isEqualTo(proto);
assertThat(protoAdapter.adaptProtoToValue(proto)).isEqualTo(value);
}
}
@RunWith(JUnit4.class)
public static class BidirectionalConversionCustomMessageTest {
@Test
public void adaptAnyValue_hermeticTypes_bidirectionalConversion() {
Expr expr = Expr.newBuilder().setExpression("test").build();
ProtoAdapter protoAdapter =
new ProtoAdapter(
DynamicProto.create(
(typeName) ->
typeName.equals(Expr.getDescriptor().getFullName())
? Optional.of(Expr.newBuilder())
: Optional.empty()),
CelOptions.DEFAULT);
assertThat(protoAdapter.adaptValueToProto(expr, Any.getDescriptor().getFullName()))
.isEqualTo(Any.pack(expr));
assertThat(protoAdapter.adaptProtoToValue(Any.pack(expr))).isEqualTo(expr);
}
}
@RunWith(JUnit4.class)
public static class AsymmetricConversionTest {
@Test
public void adaptValueToProto_asymmetricFloatConversion() {
ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT);
assertThat(protoAdapter.adaptValueToProto(1.5F, Any.getDescriptor().getFullName()))
.isEqualTo(Any.pack(FloatValue.of(1.5F)));
assertThat(protoAdapter.adaptProtoToValue(Any.pack(FloatValue.of(1.5F)))).isEqualTo(1.5D);
}
@Test
public void adaptValueToProto_asymmetricDoubleFloatConversion() {
ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT);
assertThat(protoAdapter.adaptValueToProto(1.5D, FloatValue.getDescriptor().getFullName()))
.isEqualTo(FloatValue.of(1.5F));
assertThat(protoAdapter.adaptProtoToValue(FloatValue.of(1.5F))).isEqualTo(1.5D);
}
@Test
public void adaptValueToProto_asymmetricFloatDoubleConversion() {
ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT);
assertThat(protoAdapter.adaptValueToProto(1.5F, DoubleValue.getDescriptor().getFullName()))
.isEqualTo(DoubleValue.of(1.5D));
}
@Test
public void adaptValueToProto_asymmetricJsonConversion() {
ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT);
assertThat(
protoAdapter.adaptValueToProto(
UnsignedLong.valueOf(1L), Value.getDescriptor().getFullName()))
.isEqualTo(Value.newBuilder().setNumberValue(1).build());
assertThat(
protoAdapter.adaptValueToProto(
UnsignedLong.fromLongBits(-1L), Value.getDescriptor().getFullName()))
.isEqualTo(Value.newBuilder().setStringValue(Long.toUnsignedString(-1L)).build());
assertThat(protoAdapter.adaptValueToProto(1L, Value.getDescriptor().getFullName()))
.isEqualTo(Value.newBuilder().setNumberValue(1).build());
assertThat(
protoAdapter.adaptValueToProto(Long.MAX_VALUE, Value.getDescriptor().getFullName()))
.isEqualTo(Value.newBuilder().setStringValue(Long.toString(Long.MAX_VALUE)).build());
assertThat(
protoAdapter.adaptValueToProto(
CelByteString.copyFromUtf8("foo"), Value.getDescriptor().getFullName()))
.isEqualTo(Value.newBuilder().setStringValue("Zm9v").build());
}
@Test
public void adaptValueToProto_unsupportedJsonConversion() {
ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT);
assertThrows(
ClassCastException.class,
() ->
protoAdapter.adaptValueToProto(
ImmutableMap.of(1, 1), Any.getDescriptor().getFullName()));
}
@Test
public void adaptValueToProto_unsupportedJsonListConversion() {
ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT);
assertThrows(
ClassCastException.class,
() ->
protoAdapter.adaptValueToProto(
ImmutableMap.of(1, 1), ListValue.getDescriptor().getFullName()));
}
@Test
public void adaptValueToProto_unsupportedConversion() {
ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT);
assertThrows(
IllegalStateException.class,
() -> protoAdapter.adaptValueToProto("Hello", Expr.getDescriptor().getFullName()));
}
@Test
public void signedUint32Converter_bidiSignedLong() {
BidiConverter<Number, Number> converter = ProtoAdapter.SIGNED_UINT32_CONVERTER;
assertThat(converter.forwardConverter().convert(12)).isEqualTo(12L);
assertThat(converter.forwardConverter().convert(Integer.MIN_VALUE))
.isEqualTo(Integer.MAX_VALUE + 1L);
assertThat(converter.backwardConverter().convert(12L)).isEqualTo(12);
assertThat(converter.backwardConverter().convert(Integer.MAX_VALUE + 1L))
.isEqualTo(Integer.MIN_VALUE);
}
@Test
public void unsignedUint32Converter_bidiUnsignedLong() {
BidiConverter<Number, Number> converter = ProtoAdapter.UNSIGNED_UINT32_CONVERTER;
assertThat(converter.forwardConverter().convert(12)).isEqualTo(UnsignedLong.valueOf(12L));
assertThat(converter.forwardConverter().convert(Integer.MIN_VALUE))
.isEqualTo(UnsignedLong.fromLongBits(Integer.MAX_VALUE + 1L));
assertThat(converter.backwardConverter().convert(UnsignedLong.valueOf(12L))).isEqualTo(12);
assertThat(
converter
.backwardConverter()
.convert(UnsignedLong.fromLongBits(Integer.MAX_VALUE + 1L)))
.isEqualTo(Integer.MIN_VALUE);
}
@Test
public void unsignedUint64Converter() {
BidiConverter<Number, Number> converter = ProtoAdapter.UNSIGNED_UINT64_CONVERTER;
assertThat(converter.forwardConverter().convert(12L)).isEqualTo(UnsignedLong.valueOf(12L));
assertThat(converter.forwardConverter().convert(Long.MIN_VALUE))
.isEqualTo(UnsignedLong.fromLongBits(Long.MIN_VALUE));
assertThat(converter.backwardConverter().convert(UnsignedLong.valueOf(12L))).isEqualTo(12L);
assertThat(converter.backwardConverter().convert(UnsignedLong.fromLongBits(Long.MIN_VALUE)))
.isEqualTo(Long.MIN_VALUE);
}
}
}