diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java index 7536f96e20f8..6c05c427ef6b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java @@ -441,23 +441,49 @@ public static Builder builder(String bucket, String name) { } static BlobInfo fromPb(StorageObject storageObject) { - Builder builder = new Builder() - .bucket(storageObject.getBucket()) - .cacheControl(storageObject.getCacheControl()) - .contentEncoding(storageObject.getContentEncoding()) - .crc32c(storageObject.getCrc32c()) - .contentType(storageObject.getContentType()) - .generation(storageObject.getGeneration()) - .md5(storageObject.getMd5Hash()) - .mediaLink(storageObject.getMediaLink()) - .metageneration(storageObject.getMetageneration()) - .name(storageObject.getName()) - .contentDisposition(storageObject.getContentDisposition()) - .componentCount(storageObject.getComponentCount()) - .contentLanguage(storageObject.getContentLanguage()) - .etag(storageObject.getEtag()) - .id(storageObject.getId()) - .selfLink(storageObject.getSelfLink()); + Builder builder = new Builder().bucket(storageObject.getBucket()).name(storageObject.getName()); + if (storageObject.getCacheControl() != null) { + builder.cacheControl(storageObject.getCacheControl()); + } + if (storageObject.getContentEncoding() != null) { + builder.contentEncoding(storageObject.getContentEncoding()); + } + if (storageObject.getCrc32c() != null) { + builder.crc32c(storageObject.getCrc32c()); + } + if (storageObject.getContentType() != null) { + builder.contentType(storageObject.getContentType()); + } + if (storageObject.getGeneration() != null) { + builder.generation(storageObject.getGeneration()); + } + if (storageObject.getMd5Hash() != null) { + builder.md5(storageObject.getMd5Hash()); + } + if (storageObject.getMediaLink() != null) { + builder.mediaLink(storageObject.getMediaLink()); + } + if (storageObject.getMetageneration() != null) { + builder.metageneration(storageObject.getMetageneration()); + } + if (storageObject.getContentDisposition() != null) { + builder.contentDisposition(storageObject.getContentDisposition()); + } + if (storageObject.getComponentCount() != null) { + builder.componentCount(storageObject.getComponentCount()); + } + if (storageObject.getContentLanguage() != null) { + builder.contentLanguage(storageObject.getContentLanguage()); + } + if (storageObject.getEtag() != null) { + builder.etag(storageObject.getEtag()); + } + if (storageObject.getId() != null) { + builder.id(storageObject.getId()); + } + if (storageObject.getSelfLink() != null) { + builder.selfLink(storageObject.getSelfLink()); + } if (storageObject.getMetadata() != null) { builder.metadata(storageObject.getMetadata()); } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java index 3f35fbc20e3a..ddd4665ca129 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BucketInfo.java @@ -692,14 +692,25 @@ public static Builder builder(String name) { } static BucketInfo fromPb(com.google.api.services.storage.model.Bucket bucketPb) { - Builder builder = new Builder() - .name(bucketPb.getName()) - .id(bucketPb.getId()) - .etag(bucketPb.getEtag()) - .metageneration(bucketPb.getMetageneration()) - .createTime(bucketPb.getTimeCreated().getValue()) - .location(Location.of(bucketPb.getLocation())) - .selfLink(bucketPb.getSelfLink()); + Builder builder = new Builder().name(bucketPb.getName()); + if (bucketPb.getId() != null) { + builder.id(bucketPb.getId()); + } + if (bucketPb.getEtag() != null) { + builder.etag(bucketPb.getEtag()); + } + if (bucketPb.getMetageneration() != null) { + builder.metageneration(bucketPb.getMetageneration()); + } + if (bucketPb.getSelfLink() != null) { + builder.selfLink(bucketPb.getSelfLink()); + } + if (bucketPb.getTimeCreated() != null) { + builder.createTime(bucketPb.getTimeCreated().getValue()); + } + if (bucketPb.getLocation() != null) { + builder.location(Location.of(bucketPb.getLocation())); + } if (bucketPb.getStorageClass() != null) { builder.storageClass(StorageClass.of(bucketPb.getStorageClass())); } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java index 018a59c9cfc4..bd5ac476c318 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java @@ -151,5 +151,7 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) { @Test public void testToPbAndFromPb() { compareBlobs(BLOB_INFO, BlobInfo.fromPb(BLOB_INFO.toPb())); + BlobInfo blobInfo = BlobInfo.of("b", "n"); + compareBlobs(blobInfo, BlobInfo.fromPb(blobInfo.toPb())); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java index c4f08fbd8b9b..09ba0e8cda8e 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java @@ -125,6 +125,8 @@ public void testBuilder() { @Test public void testToPbAndFromPb() { compareBuckets(BUCKET_INFO, BucketInfo.fromPb(BUCKET_INFO.toPb())); + BucketInfo bucketInfo = BucketInfo.of("b"); + compareBuckets(bucketInfo, BucketInfo.fromPb(bucketInfo.toPb())); } private void compareBuckets(BucketInfo expected, BucketInfo value) { @@ -147,21 +149,23 @@ private void compareBuckets(BucketInfo expected, BucketInfo value) { assertEquals(expected.versioningEnabled(), value.versioningEnabled()); } + @Test public void testLocation() { assertEquals("ASIA", Location.asia().value()); - assertEquals("EN", Location.eu().value()); + assertEquals("EU", Location.eu().value()); assertEquals("US", Location.us().value()); assertSame(Location.asia(), Location.of("asia")); - assertSame(Location.asia(), Location.of("EU")); - assertSame(Location.asia(), Location.of("uS")); + assertSame(Location.eu(), Location.of("EU")); + assertSame(Location.us(), Location.of("uS")); } + @Test public void testDeleteRules() { AgeDeleteRule ageRule = new AgeDeleteRule(10); assertEquals(10, ageRule.daysToLive()); assertEquals(Type.AGE, ageRule.type()); CreatedBeforeDeleteRule createBeforeRule = new CreatedBeforeDeleteRule(1); - assertEquals(10, createBeforeRule.timeMillis()); + assertEquals(1, createBeforeRule.timeMillis()); assertEquals(Type.CREATE_BEFORE, createBeforeRule.type()); NumNewerVersionsDeleteRule versionsRule = new NumNewerVersionsDeleteRule(2); assertEquals(2, versionsRule.numNewerVersions()); @@ -171,7 +175,7 @@ public void testDeleteRules() { assertEquals(Type.IS_LIVE, isLiveRule.type()); Rule rule = new Rule().set("a", "b"); RawDeleteRule rawRule = new RawDeleteRule(rule); - assertEquals(Type.UNKNOWN, isLiveRule.type()); + assertEquals(Type.UNKNOWN, rawRule.type()); ImmutableList rules = ImmutableList .of(ageRule, createBeforeRule, versionsRule, isLiveRule, rawRule); for (DeleteRule delRule : rules) {