| 1 | // Copyright 2018 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 | // http://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 types |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | |
| 21 | "google.golang.org/protobuf/proto" |
| 22 | |
| 23 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 24 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 25 | ) |
| 26 | |
| 27 | func TestJsonStructContains(t *testing.T) { |
| 28 | mapVal := NewJSONStruct(newTestRegistry(t), &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 29 | "first": structpb.NewStringValue("hello"), |
| 30 | "second": structpb.NewNumberValue(1)}}) |
| 31 | if !mapVal.Contains(String("first")).(Bool) { |
| 32 | t.Error("Expected map to contain key 'first'", mapVal) |
| 33 | } |
| 34 | if mapVal.Contains(String("firs")).(Bool) { |
| 35 | t.Error("Expected map contained non-existent key", mapVal) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestJsonStructConvertToNative_Json(t *testing.T) { |
| 40 | structVal := &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 41 | "first": structpb.NewStringValue("hello"), |
| 42 | "second": structpb.NewNumberValue(1)}} |
| 43 | mapVal := NewJSONStruct(newTestRegistry(t), structVal) |
| 44 | val, err := mapVal.ConvertToNative(JSONValueType) |
| 45 | if err != nil { |
| 46 | t.Error(err) |
| 47 | } |
| 48 | if !proto.Equal(val.(proto.Message), |
| 49 | &structpb.Value{Kind: &structpb.Value_StructValue{StructValue: structVal}}) { |
| 50 | t.Errorf("Got '%v', expected '%v'", val, structVal) |
| 51 | } |
| 52 | |
| 53 | strVal, err := mapVal.ConvertToNative(JSONStructType) |
| 54 | if err != nil { |
| 55 | t.Error(err) |
| 56 | } |
| 57 | if !proto.Equal(strVal.(proto.Message), structVal) { |
| 58 | t.Errorf("Got '%v', expected '%v'", strVal, structVal) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestJsonStructConvertToNative_Any(t *testing.T) { |
| 63 | structVal := &structpb.Struct{ |
| 64 | Fields: map[string]*structpb.Value{ |
| 65 | "first": structpb.NewStringValue("hello"), |
| 66 | "second": structpb.NewNumberValue(1)}} |
| 67 | mapVal := NewJSONStruct(newTestRegistry(t), structVal) |
| 68 | anyVal, err := mapVal.ConvertToNative(anyValueType) |
| 69 | if err != nil { |
| 70 | t.Error(err) |
| 71 | } |
| 72 | unpackedAny, err := anyVal.(*anypb.Any).UnmarshalNew() |
| 73 | if err != nil { |
| 74 | t.Fatalf("anyVal.UnmarshalNew() failed: %v", err) |
| 75 | } |
| 76 | if !proto.Equal(unpackedAny, mapVal.Value().(proto.Message)) { |
| 77 | t.Errorf("Messages were not equal, got '%v'", unpackedAny) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestJsonStructConvertToNative_Map(t *testing.T) { |
| 82 | structVal := &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 83 | "first": structpb.NewStringValue("hello"), |
| 84 | "second": structpb.NewStringValue("world"), |
| 85 | }} |
| 86 | mapVal := NewJSONStruct(newTestRegistry(t), structVal) |
| 87 | val, err := mapVal.ConvertToNative(reflect.TypeOf(map[string]string{})) |
| 88 | if err != nil { |
| 89 | t.Error(err) |
| 90 | } |
| 91 | if val.(map[string]string)["first"] != "hello" { |
| 92 | t.Error("Could not find key 'first' in map", val) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestJsonStructConvertToType(t *testing.T) { |
| 97 | mapVal := NewJSONStruct(newTestRegistry(t), |
| 98 | &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 99 | "first": structpb.NewStringValue("hello"), |
| 100 | "second": structpb.NewNumberValue(1)}}) |
| 101 | if mapVal.ConvertToType(MapType) != mapVal { |
| 102 | t.Error("Map could not be converted to a map.") |
| 103 | } |
| 104 | if mapVal.ConvertToType(TypeType) != MapType { |
| 105 | t.Error("Map did not indicate itself as map type.") |
| 106 | } |
| 107 | if !IsError(mapVal.ConvertToType(ListType)) { |
| 108 | t.Error("Got list, expected error.") |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestJsonStructEqual(t *testing.T) { |
| 113 | reg := newTestRegistry(t) |
| 114 | mapVal := NewJSONStruct(reg, |
| 115 | &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 116 | "first": structpb.NewStringValue("hello"), |
| 117 | "second": structpb.NewNumberValue(4)}}) |
| 118 | if mapVal.Equal(mapVal) != True { |
| 119 | t.Error("Map was not equal to itself.") |
| 120 | } |
| 121 | if mapVal.Equal(NewJSONStruct(reg, &structpb.Struct{})) != False { |
| 122 | t.Error("Map with key-value pairs was equal to empty map") |
| 123 | } |
| 124 | if IsError(mapVal.Equal(String(""))) { |
| 125 | t.Error("Map equal to a non-map type returned error, wanted 'false'") |
| 126 | } |
| 127 | |
| 128 | other := NewJSONStruct(reg, |
| 129 | &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 130 | "first": structpb.NewStringValue("hello"), |
| 131 | "second": structpb.NewNumberValue(1)}}) |
| 132 | if mapVal.Equal(other) != False { |
| 133 | t.Errorf("Got equals 'true', expected 'false' for '%v' == '%v'", |
| 134 | mapVal, other) |
| 135 | } |
| 136 | other = NewJSONStruct(reg, |
| 137 | &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 138 | "first": structpb.NewStringValue("hello"), |
| 139 | "third": structpb.NewNumberValue(4)}}) |
| 140 | if mapVal.Equal(other) != False { |
| 141 | t.Errorf("Got equals 'true', expected 'false' for '%v' == '%v'", |
| 142 | mapVal, other) |
| 143 | } |
| 144 | mismatch := NewDynamicMap(reg, |
| 145 | map[int]any{ |
| 146 | 1: "hello", |
| 147 | 2: "world"}) |
| 148 | if IsError(mapVal.Equal(mismatch)) { |
| 149 | t.Error("Key type mismatch resulted in error, wanted 'false'") |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestJsonStructGet(t *testing.T) { |
| 154 | if !IsError(NewJSONStruct(newTestRegistry(t), &structpb.Struct{}).Get(Int(1))) { |
| 155 | t.Error("Structs may only have string keys.") |
| 156 | } |
| 157 | |
| 158 | reg := newTestRegistry(t) |
| 159 | mapVal := NewJSONStruct(reg, |
| 160 | &structpb.Struct{Fields: map[string]*structpb.Value{ |
| 161 | "first": structpb.NewStringValue("hello"), |
| 162 | "second": structpb.NewNumberValue(4)}}) |
| 163 | |
| 164 | s := mapVal.Get(String("first")) |
| 165 | if s.Equal(String("hello")) != True { |
| 166 | t.Errorf("Got %v, wanted 'hello'", s) |
| 167 | } |
| 168 | |
| 169 | d := mapVal.Get(String("second")) |
| 170 | if d.Equal(Double(4.0)) != True { |
| 171 | t.Errorf("Got %v, wanted '4.0'", d) |
| 172 | } |
| 173 | |
| 174 | e, isError := mapVal.Get(String("third")).(*Err) |
| 175 | if !isError || e.Error() != "no such key: third" { |
| 176 | t.Errorf("Got %v, wanted no such key: third", e) |
| 177 | } |
| 178 | } |