|
| 1 | +// Copyright 2025 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 storage |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "cloud.google.com/go/storage/internal/apiv2/storagepb" |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + raw "google.golang.org/api/storage/v1" |
| 24 | + "google.golang.org/protobuf/testing/protocmp" |
| 25 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 26 | +) |
| 27 | + |
| 28 | +func TestToObjectContexts(t *testing.T) { |
| 29 | + now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) |
| 30 | + |
| 31 | + testCases := []struct { |
| 32 | + name string |
| 33 | + raw *raw.ObjectContexts |
| 34 | + want *ObjectContexts |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "nil raw object contexts", |
| 38 | + raw: nil, |
| 39 | + want: nil, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "empty custom contexts", |
| 43 | + raw: &raw.ObjectContexts{ |
| 44 | + Custom: map[string]raw.ObjectCustomContextPayload{}, |
| 45 | + }, |
| 46 | + want: &ObjectContexts{ |
| 47 | + Custom: map[string]ObjectCustomContextPayload{}, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "with custom contexts", |
| 52 | + raw: &raw.ObjectContexts{ |
| 53 | + Custom: map[string]raw.ObjectCustomContextPayload{ |
| 54 | + "key1": {Value: "value1", CreateTime: now.Format(time.RFC3339Nano), UpdateTime: now.Format(time.RFC3339Nano)}, |
| 55 | + "key2": {Value: "value2", CreateTime: now.Format(time.RFC3339Nano), UpdateTime: now.Format(time.RFC3339Nano)}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + want: &ObjectContexts{ |
| 59 | + Custom: map[string]ObjectCustomContextPayload{ |
| 60 | + "key1": {Value: "value1", CreateTime: now, UpdateTime: now}, |
| 61 | + "key2": {Value: "value2", CreateTime: now, UpdateTime: now}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tc := range testCases { |
| 68 | + t.Run(tc.name, func(t *testing.T) { |
| 69 | + got := toObjectContexts(tc.raw) |
| 70 | + if diff := cmp.Diff(tc.want, got); diff != "" { |
| 71 | + t.Errorf("toObjectContexts() mismatch (-want +got):\n%s", diff) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestToRawObjectContexts(t *testing.T) { |
| 78 | + testCases := []struct { |
| 79 | + name string |
| 80 | + obj *ObjectContexts |
| 81 | + want *raw.ObjectContexts |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "nil object contexts", |
| 85 | + obj: nil, |
| 86 | + want: nil, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "empty custom contexts", |
| 90 | + obj: &ObjectContexts{ |
| 91 | + Custom: map[string]ObjectCustomContextPayload{}, |
| 92 | + }, |
| 93 | + want: &raw.ObjectContexts{ |
| 94 | + Custom: map[string]raw.ObjectCustomContextPayload{}, |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "with custom contexts", |
| 99 | + obj: &ObjectContexts{ |
| 100 | + Custom: map[string]ObjectCustomContextPayload{ |
| 101 | + "key1": {Value: "value1"}, |
| 102 | + "key2": {Value: "value2", Delete: true}, // Should have NullFields |
| 103 | + }, |
| 104 | + }, |
| 105 | + want: &raw.ObjectContexts{ |
| 106 | + Custom: map[string]raw.ObjectCustomContextPayload{ |
| 107 | + "key1": {Value: "value1", ForceSendFields: []string{"key1"}}, |
| 108 | + "key2": {NullFields: []string{"key2"}}, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "with custom contexts, no delete", |
| 114 | + obj: &ObjectContexts{ |
| 115 | + Custom: map[string]ObjectCustomContextPayload{ |
| 116 | + "key1": {Value: "value1"}, |
| 117 | + "key2": {Value: "value2"}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + want: &raw.ObjectContexts{ |
| 121 | + Custom: map[string]raw.ObjectCustomContextPayload{ |
| 122 | + "key1": {Value: "value1", ForceSendFields: []string{"key1"}}, |
| 123 | + "key2": {Value: "value2", ForceSendFields: []string{"key2"}}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + for _, tc := range testCases { |
| 130 | + t.Run(tc.name, func(t *testing.T) { |
| 131 | + got := toRawObjectContexts(tc.obj) |
| 132 | + if diff := cmp.Diff(tc.want, got); diff != "" { |
| 133 | + t.Errorf("toRawObjectContexts() mismatch (-want +got): %s", diff) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestToObjectContextsFromProto(t *testing.T) { |
| 140 | + now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) |
| 141 | + |
| 142 | + testCases := []struct { |
| 143 | + name string |
| 144 | + proto *storagepb.ObjectContexts |
| 145 | + want *ObjectContexts |
| 146 | + }{ |
| 147 | + { |
| 148 | + name: "nil proto object contexts", |
| 149 | + proto: nil, |
| 150 | + want: nil, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "empty custom contexts", |
| 154 | + proto: &storagepb.ObjectContexts{ |
| 155 | + Custom: map[string]*storagepb.ObjectCustomContextPayload{}, |
| 156 | + }, |
| 157 | + want: &ObjectContexts{ |
| 158 | + Custom: map[string]ObjectCustomContextPayload{}, |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "with custom contexts", |
| 163 | + proto: &storagepb.ObjectContexts{ |
| 164 | + Custom: map[string]*storagepb.ObjectCustomContextPayload{ |
| 165 | + "key1": {Value: "value1", CreateTime: timestamppb.New(now), UpdateTime: timestamppb.New(now)}, |
| 166 | + "key2": {Value: "value2", CreateTime: timestamppb.New(now), UpdateTime: timestamppb.New(now)}, |
| 167 | + }, |
| 168 | + }, |
| 169 | + want: &ObjectContexts{ |
| 170 | + Custom: map[string]ObjectCustomContextPayload{ |
| 171 | + "key1": {Value: "value1", CreateTime: now, UpdateTime: now}, |
| 172 | + "key2": {Value: "value2", CreateTime: now, UpdateTime: now}, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + for _, tc := range testCases { |
| 179 | + t.Run(tc.name, func(t *testing.T) { |
| 180 | + got := toObjectContextsFromProto(tc.proto) |
| 181 | + if diff := cmp.Diff(tc.want, got); diff != "" { |
| 182 | + t.Errorf("toObjectContextsFromProto() mismatch (-want +got): %s", diff) |
| 183 | + } |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func TestToProtoObjectContexts(t *testing.T) { |
| 189 | + now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) |
| 190 | + |
| 191 | + testCases := []struct { |
| 192 | + name string |
| 193 | + obj *ObjectContexts |
| 194 | + want *storagepb.ObjectContexts |
| 195 | + }{ |
| 196 | + { |
| 197 | + name: "nil object contexts", |
| 198 | + obj: nil, |
| 199 | + want: nil, |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "empty custom contexts", |
| 203 | + obj: &ObjectContexts{ |
| 204 | + Custom: map[string]ObjectCustomContextPayload{}, |
| 205 | + }, |
| 206 | + want: &storagepb.ObjectContexts{ |
| 207 | + Custom: map[string]*storagepb.ObjectCustomContextPayload{}, |
| 208 | + }, |
| 209 | + }, |
| 210 | + { |
| 211 | + name: "with custom contexts", |
| 212 | + obj: &ObjectContexts{ |
| 213 | + Custom: map[string]ObjectCustomContextPayload{ |
| 214 | + "key1": {Value: "value1", CreateTime: now, UpdateTime: now}, |
| 215 | + "key2": {Value: "value2", Delete: true}, // Should be skipped in proto conversion |
| 216 | + "key3": {Value: "value3"}, |
| 217 | + }, |
| 218 | + }, |
| 219 | + want: &storagepb.ObjectContexts{ |
| 220 | + Custom: map[string]*storagepb.ObjectCustomContextPayload{ |
| 221 | + "key1": {Value: "value1"}, |
| 222 | + "key3": {Value: "value3"}, |
| 223 | + }, |
| 224 | + }, |
| 225 | + }, |
| 226 | + } |
| 227 | + |
| 228 | + for _, tc := range testCases { |
| 229 | + t.Run(tc.name, func(t *testing.T) { |
| 230 | + got := toProtoObjectContexts(tc.obj) |
| 231 | + if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" { |
| 232 | + t.Errorf("toProtoObjectContexts() mismatch (-want +got): %s", diff) |
| 233 | + } |
| 234 | + }) |
| 235 | + } |
| 236 | +} |
0 commit comments