|
| 1 | +// Copyright 2023 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 dev.cel.common.values; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | +import static org.junit.Assert.assertThrows; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 22 | +import dev.cel.common.CelDescriptorUtil; |
| 23 | +import dev.cel.common.internal.CelDescriptorPool; |
| 24 | +import dev.cel.common.internal.DefaultDescriptorPool; |
| 25 | +import dev.cel.common.types.StructTypeReference; |
| 26 | +import dev.cel.testing.testdata.proto2.MessagesProto2Extensions; |
| 27 | +import dev.cel.testing.testdata.proto2.Proto2Message; |
| 28 | +import dev.cel.testing.testdata.proto2.TestAllTypesProto.TestAllTypes; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | + |
| 32 | +@RunWith(TestParameterInjector.class) |
| 33 | +public final class ProtoMessageValueTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void emptyProtoMessage() { |
| 37 | + ProtoMessageValue protoMessageValue = |
| 38 | + ProtoMessageValue.create(TestAllTypes.getDefaultInstance(), DefaultDescriptorPool.INSTANCE); |
| 39 | + |
| 40 | + assertThat(protoMessageValue.value()).isEqualTo(TestAllTypes.getDefaultInstance()); |
| 41 | + assertThat(protoMessageValue.isZeroValue()).isTrue(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void constructProtoMessage() { |
| 46 | + TestAllTypes testAllTypes = |
| 47 | + TestAllTypes.newBuilder().setSingleBool(true).setSingleInt64(5L).build(); |
| 48 | + ProtoMessageValue protoMessageValue = |
| 49 | + ProtoMessageValue.create(testAllTypes, DefaultDescriptorPool.INSTANCE); |
| 50 | + |
| 51 | + assertThat(protoMessageValue.value()).isEqualTo(testAllTypes); |
| 52 | + assertThat(protoMessageValue.isZeroValue()).isFalse(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void hasField_fieldIsSet_success() { |
| 57 | + TestAllTypes testAllTypes = |
| 58 | + TestAllTypes.newBuilder() |
| 59 | + .setSingleBool(true) |
| 60 | + .setSingleInt64(5L) |
| 61 | + .addRepeatedInt64(5L) |
| 62 | + .build(); |
| 63 | + ProtoMessageValue protoMessageValue = |
| 64 | + ProtoMessageValue.create(testAllTypes, DefaultDescriptorPool.INSTANCE); |
| 65 | + |
| 66 | + assertThat(protoMessageValue.hasField("single_bool")).isTrue(); |
| 67 | + assertThat(protoMessageValue.hasField("single_int64")).isTrue(); |
| 68 | + assertThat(protoMessageValue.hasField("repeated_int64")).isTrue(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void hasField_fieldIsUnset_success() { |
| 73 | + ProtoMessageValue protoMessageValue = |
| 74 | + ProtoMessageValue.create(TestAllTypes.getDefaultInstance(), DefaultDescriptorPool.INSTANCE); |
| 75 | + |
| 76 | + assertThat(protoMessageValue.hasField("single_int32")).isFalse(); |
| 77 | + assertThat(protoMessageValue.hasField("single_uint64")).isFalse(); |
| 78 | + assertThat(protoMessageValue.hasField("repeated_int32")).isFalse(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void hasField_fieldIsUndeclared_throwsException() { |
| 83 | + ProtoMessageValue protoMessageValue = |
| 84 | + ProtoMessageValue.create(TestAllTypes.getDefaultInstance(), DefaultDescriptorPool.INSTANCE); |
| 85 | + |
| 86 | + IllegalArgumentException exception = |
| 87 | + assertThrows(IllegalArgumentException.class, () -> protoMessageValue.hasField("bogus")); |
| 88 | + assertThat(exception) |
| 89 | + .hasMessageThat() |
| 90 | + .isEqualTo( |
| 91 | + "field 'bogus' is not declared in message" |
| 92 | + + " 'dev.cel.testing.testdata.proto2.TestAllTypes'"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void hasField_extensionField_success() { |
| 97 | + CelDescriptorPool descriptorPool = |
| 98 | + DefaultDescriptorPool.create( |
| 99 | + CelDescriptorUtil.getAllDescriptorsFromFileDescriptor( |
| 100 | + ImmutableList.of(MessagesProto2Extensions.getDescriptor()))); |
| 101 | + Proto2Message proto2Message = |
| 102 | + Proto2Message.newBuilder().setExtension(MessagesProto2Extensions.int32Ext, 1).build(); |
| 103 | + |
| 104 | + ProtoMessageValue protoMessageValue = ProtoMessageValue.create(proto2Message, descriptorPool); |
| 105 | + |
| 106 | + assertThat(protoMessageValue.hasField("dev.cel.testing.testdata.proto2.int32_ext")).isTrue(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void hasField_extensionField_throwsWhenDescriptorMissing() { |
| 111 | + Proto2Message proto2Message = |
| 112 | + Proto2Message.newBuilder().setExtension(MessagesProto2Extensions.int32Ext, 1).build(); |
| 113 | + |
| 114 | + ProtoMessageValue protoMessageValue = |
| 115 | + ProtoMessageValue.create(proto2Message, DefaultDescriptorPool.INSTANCE); |
| 116 | + |
| 117 | + IllegalArgumentException exception = |
| 118 | + assertThrows( |
| 119 | + IllegalArgumentException.class, |
| 120 | + () -> protoMessageValue.hasField("dev.cel.testing.testdata.proto2.int32_ext")); |
| 121 | + assertThat(exception) |
| 122 | + .hasMessageThat() |
| 123 | + .isEqualTo( |
| 124 | + "field 'dev.cel.testing.testdata.proto2.int32_ext' is not declared in message" |
| 125 | + + " 'dev.cel.testing.testdata.proto2.Proto2Message'"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void celTypeTest() { |
| 130 | + ProtoMessageValue protoMessageValue = |
| 131 | + ProtoMessageValue.create(TestAllTypes.getDefaultInstance(), DefaultDescriptorPool.INSTANCE); |
| 132 | + |
| 133 | + assertThat(protoMessageValue.celType()) |
| 134 | + .isEqualTo(StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); |
| 135 | + } |
| 136 | +} |
0 commit comments