/
ITReadMaskTest.java
427 lines (376 loc) · 17 KB
/
ITReadMaskTest.java
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* 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
*
* http://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 com.google.cloud.storage.it;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assume.assumeTrue;
import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketFixture;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Rpo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobField;
import com.google.cloud.storage.Storage.BlobGetOption;
import com.google.cloud.storage.Storage.BlobListOption;
import com.google.cloud.storage.Storage.BlobTargetOption;
import com.google.cloud.storage.Storage.BucketField;
import com.google.cloud.storage.Storage.BucketGetOption;
import com.google.cloud.storage.Storage.BucketListOption;
import com.google.cloud.storage.Storage.ComposeRequest;
import com.google.cloud.storage.StorageFixture;
import com.google.cloud.storage.conformance.retry.ParallelParameterized;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Enclosed.class)
public final class ITReadMaskTest {
@ClassRule(order = 1)
public static final StorageFixture sfH = StorageFixture.defaultHttp();
@ClassRule(order = 1)
public static final StorageFixture sfG = StorageFixture.defaultGrpc();
@ClassRule(order = 2)
public static final BucketFixture bucketFixture =
BucketFixture.newBuilder()
.setHandle(sfH::getInstance)
.setBucketNameFmtString("java-storage-grpc-%s")
.build();
private static Storage sh;
private static Storage sg;
private static BlobId blobId;
@BeforeClass
public static void beforeClass() {
sh = sfH.getInstance();
sg = sfG.getInstance();
String bucket = bucketFixture.getBucketInfo().getName();
BlobId blobId1 = BlobId.of(bucket, objName("001"));
BlobId blobId2 = BlobId.of(bucket, objName("002"));
BlobId blobId3 = BlobId.of(bucket, objName("003"));
BlobId blobId4 = BlobId.of(bucket, objName("004"));
BlobInfo info1 = BlobInfo.newBuilder(blobId1).setMetadata(ImmutableMap.of("pow", "1")).build();
BlobInfo info2 = BlobInfo.newBuilder(blobId2).setMetadata(ImmutableMap.of("pow", "2")).build();
BlobInfo info3 = BlobInfo.newBuilder(blobId3).setMetadata(ImmutableMap.of("pow", "3")).build();
BlobInfo info4 = BlobInfo.newBuilder(blobId4).setMetadata(ImmutableMap.of("pow", "4")).build();
sh.create(info1, "A".getBytes(StandardCharsets.UTF_8), BlobTargetOption.doesNotExist());
ComposeRequest c2 =
ComposeRequest.newBuilder()
.addSource(blobId1.getName(), blobId1.getName())
.setTarget(info2)
.setTargetOptions(BlobTargetOption.doesNotExist())
.build();
ComposeRequest c3 =
ComposeRequest.newBuilder()
.addSource(blobId2.getName(), blobId2.getName())
.setTarget(info3)
.setTargetOptions(BlobTargetOption.doesNotExist())
.build();
ComposeRequest c4 =
ComposeRequest.newBuilder()
.addSource(blobId3.getName(), blobId3.getName())
.setTarget(info4)
.setTargetOptions(BlobTargetOption.doesNotExist())
.build();
sh.compose(c2);
sh.compose(c3);
sh.compose(c4);
blobId = blobId1;
}
@RunWith(ParallelParameterized.class)
public static final class BucketReadMask {
private final BucketField field;
private final LazyAssertion<BucketInfo> assertion;
public BucketReadMask(Args<BucketField, BucketInfo> arg) {
this.field = arg.f;
this.assertion = arg.assertion;
}
@Test
public void get() {
BucketInfo bucketJson = getBucket(sh);
BucketInfo bucketGrpc = getBucket(sg);
assertion.validate(bucketJson, bucketGrpc);
}
@Test
public void list() {
List<BucketInfo> bucketsJson = listBuckets(sh);
List<BucketInfo> bucketsGrpc = listBuckets(sg);
assertion.pairwiseList().validate(bucketsJson, bucketsGrpc);
}
@Parameters(name = "{0}")
public static Iterable<Args<BucketField, BucketInfo>> parameters() {
ImmutableList<Args<BucketField, BucketInfo>> args =
ImmutableList.of(
new Args<>(
BucketField.ACL, LazyAssertion.skip("Waiting for b/217922398 to be fixed")),
new Args<>(BucketField.AUTOCLASS, LazyAssertion.equal()),
new Args<>(BucketField.BILLING, LazyAssertion.equal()),
new Args<>(BucketField.CORS, LazyAssertion.equal()),
new Args<>(BucketField.CUSTOM_PLACEMENT_CONFIG, LazyAssertion.equal()),
new Args<>(
BucketField.DEFAULT_EVENT_BASED_HOLD,
(jsonT, grpcT) -> {
assertThat(jsonT.getDefaultEventBasedHold()).isNull();
assertThat(grpcT.getDefaultEventBasedHold()).isFalse();
}),
new Args<>(
BucketField.DEFAULT_OBJECT_ACL,
(jsonT, grpcT) -> {
assertThat(jsonT.getDefaultAcl()).isNotEmpty();
assertThat(grpcT.getDefaultAcl()).isNull();
}),
new Args<>(BucketField.ENCRYPTION, LazyAssertion.equal()),
new Args<>(
BucketField.ETAG, LazyAssertion.skip("Waiting for b/217922398 to be fixed")),
new Args<>(BucketField.IAMCONFIGURATION, LazyAssertion.equal()),
new Args<>(BucketField.ID, LazyAssertion.equal()),
new Args<>(BucketField.LABELS, LazyAssertion.equal()),
new Args<>(BucketField.LIFECYCLE, LazyAssertion.equal()),
new Args<>(BucketField.LOCATION, LazyAssertion.equal()),
new Args<>(BucketField.LOCATION_TYPE, LazyAssertion.equal()),
new Args<>(BucketField.LOGGING, LazyAssertion.equal()),
new Args<>(BucketField.METAGENERATION, LazyAssertion.equal()),
new Args<>(BucketField.NAME, LazyAssertion.equal()),
new Args<>(BucketField.OWNER, LazyAssertion.equal()),
new Args<>(BucketField.RETENTION_POLICY, LazyAssertion.equal()),
new Args<>(
BucketField.RPO,
(jsonT, grpcT) -> {
assertThat(jsonT.getRpo()).isEqualTo(Rpo.DEFAULT);
// TODO: cleanup allowed null value in mid nov
assertThat(grpcT.getRpo()).isAnyOf(Rpo.DEFAULT, null);
}),
new Args<>(
BucketField.SELF_LINK,
(jsonT, grpcT) -> {
assertThat(jsonT.getSelfLink()).isNotEmpty();
assertThat(grpcT.getSelfLink()).isNull();
}),
new Args<>(BucketField.STORAGE_CLASS, LazyAssertion.equal()),
new Args<>(BucketField.TIME_CREATED, LazyAssertion.equal()),
new Args<>(BucketField.UPDATED, LazyAssertion.equal()),
new Args<>(BucketField.VERSIONING, LazyAssertion.equal()),
new Args<>(BucketField.WEBSITE, LazyAssertion.equal()));
List<String> argsDefined =
args.stream().map(Args::getF).map(Enum::name).sorted().collect(Collectors.toList());
List<String> definedFields =
Arrays.stream(BucketField.values()).map(Enum::name).sorted().collect(Collectors.toList());
assertThat(argsDefined).containsExactlyElementsIn(definedFields);
return args;
}
private BucketInfo getBucket(Storage s) {
return s.get(blobId.getBucket(), BucketGetOption.fields(field)).asBucketInfo();
}
private List<BucketInfo> listBuckets(Storage s) {
Page<Bucket> p =
s.list(BucketListOption.prefix(blobId.getBucket()), BucketListOption.fields(field));
return StreamSupport.stream(p.iterateAll().spliterator(), false)
.map(Bucket::asBucketInfo)
.collect(Collectors.toList());
}
}
@RunWith(ParallelParameterized.class)
public static final class BlobReadMask {
private final BlobField field;
private final LazyAssertion<BlobInfo> assertion;
public BlobReadMask(Args<BlobField, BlobInfo> args) {
this.field = args.f;
this.assertion = args.assertion;
}
@Test
public void get() {
BlobInfo blobJson = getBlob(sh);
BlobInfo blobGrpc = getBlob(sg);
assertion.validate(blobJson, blobGrpc);
}
@Test
public void list() {
List<BlobInfo> blobsJson = listBlobs(sh);
List<BlobInfo> blobsGrpc = listBlobs(sg);
assertion.pairwiseList().validate(blobsJson, blobsGrpc);
}
@Parameters(name = "{0}")
public static Iterable<Args<BlobField, BlobInfo>> parameters() {
ImmutableList<Args<BlobField, BlobInfo>> args =
ImmutableList.of(
new Args<>(BlobField.ACL, LazyAssertion.skip("Waiting for b/217922398 to be fixed")),
new Args<>(BlobField.BUCKET, LazyAssertion.equal()),
new Args<>(
BlobField.CACHE_CONTROL,
LazyAssertion.apiaryNullGrpcDefault("", BlobInfo::getCacheControl)),
// for non-composed objects, json and grpc differ in their resulting values. For json,
// a null will be returned whereas for grpc we will get the type default value which
// in this case is 0. The only possible way we could guard against this would be if
// the proto changed component_count to proto3_optional forcing it to generate a
// hasComponentCount.
new Args<>(
BlobField.COMPONENT_COUNT,
(jsonT, grpcT) -> {
if (grpcT.getComponentCount() == 0) {
assertThat(jsonT.getComponentCount()).isNull();
} else {
assertThat(grpcT.getComponentCount()).isEqualTo(jsonT.getComponentCount());
}
}),
new Args<>(
BlobField.CONTENT_DISPOSITION,
LazyAssertion.apiaryNullGrpcDefault("", BlobInfo::getContentDisposition)),
new Args<>(
BlobField.CONTENT_ENCODING,
LazyAssertion.apiaryNullGrpcDefault("", BlobInfo::getContentEncoding)),
new Args<>(
BlobField.CONTENT_LANGUAGE,
LazyAssertion.apiaryNullGrpcDefault("", BlobInfo::getContentLanguage)),
// we'd expect this to follow the patter of the other Content-* headers, however via
// the json api GCS will default null contentType to application/octet-stream. Note,
// however it doesn't carry this forward to composed objects so a composed object can
// have a null/empty content-type.
new Args<>(
BlobField.CONTENT_TYPE,
(jsonT, grpcT) -> {
assertThat(jsonT.getContentType()).isAnyOf("application/octet-stream", null);
assertThat(grpcT.getContentType()).isAnyOf("application/octet-stream", "");
}),
new Args<>(BlobField.CRC32C, LazyAssertion.equal()),
new Args<>(BlobField.CUSTOMER_ENCRYPTION, LazyAssertion.equal()),
new Args<>(BlobField.CUSTOM_TIME, LazyAssertion.equal()),
new Args<>(BlobField.ETAG, LazyAssertion.skip("Waiting for b/217922398 to be fixed")),
new Args<>(
BlobField.EVENT_BASED_HOLD,
LazyAssertion.apiaryNullGrpcDefault(false, BlobInfo::getEventBasedHold)),
new Args<>(BlobField.GENERATION, LazyAssertion.equal()),
new Args<>(
BlobField.ID,
(jsonT, grpcT) -> {
assertThat(jsonT.getGeneratedId()).isNotEmpty();
assertThat(grpcT.getGeneratedId()).isNull();
}),
new Args<>(
BlobField.KIND,
(jsonT, grpcT) -> {
// pass - we don't expose kind in the public surface
}),
new Args<>(BlobField.KMS_KEY_NAME, LazyAssertion.equal()),
new Args<>(BlobField.MD5HASH, LazyAssertion.equal()),
new Args<>(
BlobField.MEDIA_LINK,
(jsonT, grpcT) -> {
assertThat(jsonT.getMediaLink()).isNotEmpty();
assertThat(grpcT.getMediaLink()).isNull();
}),
new Args<>(BlobField.METADATA, LazyAssertion.equal()),
new Args<>(BlobField.METAGENERATION, LazyAssertion.equal()),
new Args<>(BlobField.NAME, LazyAssertion.equal()),
new Args<>(BlobField.OWNER, LazyAssertion.equal()),
new Args<>(BlobField.RETENTION_EXPIRATION_TIME, LazyAssertion.equal()),
new Args<>(
BlobField.SELF_LINK,
(jsonT, grpcT) -> {
assertThat(jsonT.getSelfLink()).isNotEmpty();
assertThat(grpcT.getSelfLink()).isNull();
}),
new Args<>(BlobField.SIZE, LazyAssertion.equal()),
new Args<>(BlobField.STORAGE_CLASS, LazyAssertion.equal()),
new Args<>(
BlobField.TEMPORARY_HOLD,
LazyAssertion.apiaryNullGrpcDefault(false, BlobInfo::getTemporaryHold)),
new Args<>(BlobField.TIME_CREATED, LazyAssertion.equal()),
new Args<>(BlobField.TIME_DELETED, LazyAssertion.equal()),
new Args<>(BlobField.TIME_STORAGE_CLASS_UPDATED, LazyAssertion.equal()),
new Args<>(BlobField.UPDATED, LazyAssertion.equal()));
List<String> argsDefined =
args.stream().map(Args::getF).map(Enum::name).sorted().collect(Collectors.toList());
List<String> definedFields =
Arrays.stream(BlobField.values()).map(Enum::name).sorted().collect(Collectors.toList());
assertThat(argsDefined).containsExactlyElementsIn(definedFields);
return args;
}
private BlobInfo getBlob(Storage s) {
return s.get(blobId, BlobGetOption.fields(field)).asBlobInfo();
}
private List<BlobInfo> listBlobs(Storage s) {
Page<Blob> p =
s.list(
blobId.getBucket(),
BlobListOption.prefix(ITReadMaskTest.class.getSimpleName()),
BlobListOption.fields(field));
return StreamSupport.stream(p.iterateAll().spliterator(), false)
.map(Blob::asBlobInfo)
.collect(Collectors.toList());
}
}
private static String objName(String name) {
return String.format("%s/%s", ITReadMaskTest.class.getSimpleName(), name);
}
private static final class Args<F, T> {
private final F f;
private final LazyAssertion<T> assertion;
Args(F f, LazyAssertion<T> assertion) {
this.f = f;
this.assertion = assertion;
}
F getF() {
return f;
}
@Override
public String toString() {
return f.toString();
}
}
@FunctionalInterface
private interface LazyAssertion<T> {
void validate(T jsonT, T grpcT) throws AssertionError;
default LazyAssertion<List<T>> pairwiseList() {
LazyAssertion<T> self = this;
return (jsonTs, grpcTs) -> {
final int length = Math.min(jsonTs.size(), grpcTs.size());
int idx = 0;
for (; idx < length; idx++) {
T jT = jsonTs.get(idx);
T gT = grpcTs.get(idx);
self.validate(jT, gT);
}
assertThat(idx).isEqualTo(jsonTs.size());
assertThat(idx).isEqualTo(grpcTs.size());
assertThat(jsonTs.size()).isEqualTo(length);
assertThat(grpcTs.size()).isEqualTo(length);
};
}
static <X> LazyAssertion<X> equal() {
return (a, g) -> assertThat(g).isEqualTo(a);
}
static <X> LazyAssertion<X> skip(String message) {
return (a, g) -> assumeTrue(message, false);
}
static <X, F> LazyAssertion<X> apiaryNullGrpcDefault(F def, Function<X, F> extractor) {
return (jsonT, grpcT) -> {
assertThat(extractor.apply(jsonT)).isNull();
assertThat(extractor.apply(grpcT)).isEqualTo(def);
};
}
}
}