Skip to content

Commit

Permalink
Merge pull request #185 from mziccard/fix-to-from-pb
Browse files Browse the repository at this point in the history
Add null checks to fromPb in BucketInfo and BlobInfo, updated tests
  • Loading branch information
aozarov committed Sep 28, 2015
2 parents 23ebbec + 90dd070 commit abb421e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand All @@ -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<DeleteRule> rules = ImmutableList
.of(ageRule, createBeforeRule, versionsRule, isLiveRule, rawRule);
for (DeleteRule delRule : rules) {
Expand Down

0 comments on commit abb421e

Please sign in to comment.