From 8f08f2d6aaf1074a11701363b1dc22243f2e2b19 Mon Sep 17 00:00:00 2001 From: aozarov Date: Fri, 15 May 2015 15:27:44 -0700 Subject: [PATCH] add tests for bucket and blob --- .../com/google/gcloud/storage/Bucket.java | 43 ++++- .../java/com/google/gcloud/storage/Cors.java | 42 +++-- .../com/google/gcloud/storage/BlobTest.java | 155 ++++++++++++++++ .../com/google/gcloud/storage/BucketTest.java | 175 ++++++++++++++++++ 4 files changed, 394 insertions(+), 21 deletions(-) create mode 100644 src/test/java/com/google/gcloud/storage/BlobTest.java create mode 100644 src/test/java/com/google/gcloud/storage/BucketTest.java diff --git a/src/main/java/com/google/gcloud/storage/Bucket.java b/src/main/java/com/google/gcloud/storage/Bucket.java index 793ae0199d5c..85d601487c28 100644 --- a/src/main/java/com/google/gcloud/storage/Bucket.java +++ b/src/main/java/com/google/gcloud/storage/Bucket.java @@ -103,6 +103,23 @@ public Type type() { return type; } + @Override + public int hashCode() { + return Objects.hash(type); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + final DeleteRule other = (DeleteRule) obj; + return Objects.equals(toPb(), other.toPb()); + } + Rule toPb() { Rule rule = new Rule(); rule.setAction(new Rule.Action().setType(SUPPORTED_ACTION)); @@ -114,7 +131,7 @@ Rule toPb() { abstract void populateCondition(Rule.Condition condition); - private static DeleteRule fromPb(Rule rule) { + static DeleteRule fromPb(Rule rule) { if (rule.getAction() != null && SUPPORTED_ACTION.endsWith(rule.getAction().getType())) { Rule.Condition condition = rule.getCondition(); Integer age = condition.getAge(); @@ -346,6 +363,23 @@ public static Location of(String value) { return option == null ? new Location(value) : option.location; } + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + final Location other = (Location) obj; + return Objects.equals(this.value, other.value); + } + @Override public String toString() { return value(); @@ -412,7 +446,7 @@ public Builder notFoundPage(String notFoundPage) { return this; } - public Builder deleteRules(Iterable rules) { + public Builder deleteRules(Iterable rules) { this.deleteRules = ImmutableList.copyOf(rules); return this; } @@ -490,7 +524,7 @@ public String name() { return name; } - public Entity Owner() { + public Entity owner() { return owner; } @@ -510,7 +544,7 @@ public String notFoundPage() { return notFoundPage; } - public List deleteRules() { + public List deleteRules() { return deleteRules; } @@ -652,6 +686,7 @@ public Rule apply(DeleteRule deleteRule) { return deleteRule.toPb(); } })); + bucketPb.setLifecycle(lifecycle); } return bucketPb; } diff --git a/src/main/java/com/google/gcloud/storage/Cors.java b/src/main/java/com/google/gcloud/storage/Cors.java index 9cedbc0d3b4c..b1953aa5e0e4 100644 --- a/src/main/java/com/google/gcloud/storage/Cors.java +++ b/src/main/java/com/google/gcloud/storage/Cors.java @@ -130,17 +130,17 @@ public Builder maxAgeSeconds(Integer maxAgeSeconds) { } public Builder methods(Iterable methods) { - this.methods = ImmutableList.copyOf(methods); + this.methods = methods != null ? ImmutableList.copyOf(methods) : null; return this; } public Builder origins(Iterable origins) { - this.origins = ImmutableList.copyOf(origins); + this.origins = origins != null ? ImmutableList.copyOf(origins) : null; return this; } public Builder responseHeaders(Iterable headers) { - this.responseHeaders = ImmutableList.copyOf(headers); + this.responseHeaders = headers != null ? ImmutableList.copyOf(headers) : null; return this; } @@ -205,25 +205,33 @@ Bucket.Cors toPb() { Bucket.Cors pb = new Bucket.Cors(); pb.setMaxAgeSeconds(maxAgeSeconds); pb.setResponseHeader(responseHeaders); - pb.setMethod(newArrayList(transform(methods(), Functions.toStringFunction()))); - pb.setOrigin(newArrayList(transform(origins(), Functions.toStringFunction()))); + if (methods != null) { + pb.setMethod(newArrayList(transform(methods, Functions.toStringFunction()))); + } + if (origins != null) { + pb.setOrigin(newArrayList(transform(origins, Functions.toStringFunction()))); + } return pb; } static Cors fromPb(Bucket.Cors cors) { Builder builder = builder().maxAgeSeconds(cors.getMaxAgeSeconds()); - builder.methods(transform(cors.getMethod(), new Function() { - @Override - public Method apply(String name) { - return Method.valueOf(name.toUpperCase()); - } - })); - builder.origins(transform(cors.getOrigin(), new Function() { - @Override - public Origin apply(String value) { - return Origin.of(value); - } - })); + if (cors.getMethod() != null) { + builder.methods(transform(cors.getMethod(), new Function() { + @Override + public Method apply(String name) { + return Method.valueOf(name.toUpperCase()); + } + })); + } + if (cors.getOrigin() != null) { + builder.origins(transform(cors.getOrigin(), new Function() { + @Override + public Origin apply(String value) { + return Origin.of(value); + } + })); + } builder.responseHeaders(cors.getResponseHeader()); return builder.build(); } diff --git a/src/test/java/com/google/gcloud/storage/BlobTest.java b/src/test/java/com/google/gcloud/storage/BlobTest.java new file mode 100644 index 000000000000..6fb8f0243987 --- /dev/null +++ b/src/test/java/com/google/gcloud/storage/BlobTest.java @@ -0,0 +1,155 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * 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.gcloud.storage; + +import static com.google.gcloud.storage.Acl.Project.ProjectRole.VIEWERS; +import static com.google.gcloud.storage.Acl.Role.READER; +import static com.google.gcloud.storage.Acl.Role.WRITER; +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.gcloud.storage.Acl.Project; +import com.google.gcloud.storage.Acl.User; + +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +public class BlobTest { + + private static final List ACL = ImmutableList.of( + new Acl(User.ofAllAuthenticatedUsers(), READER), + new Acl(new Project(VIEWERS, "p1"), WRITER)); + private static final Integer COMPONENT_COUNT = 2; + private static final String CONTENT_TYPE = "text/html"; + private static final String CACHE_CONTROL = "cache"; + private static final String CONTENT_DISPOSITION = "content-disposition"; + private static final String CONTENT_ENCODING = "UTF-8"; + private static final String CONTENT_LANGUAGE = "En"; + private static final String CRC32 = "0xFF00"; + private static final Long DELETE_TIME = System.currentTimeMillis(); + private static final String ETAG = "0xFF00"; + private static final Long GENERATION = 1L; + private static final String ID = "B/N:1"; + private static final String MD5 = "0xFF00"; + private static final String MEDIA_LINK = "http://media/b/n"; + private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); + private static final Long META_GENERATION = 10L; + private static final User OWNER = new User("user@gmail.com"); + private static final String SELF_LINK = "http://storage/b/n"; + private static final Long SIZE = 1024L; + private static final Long UPDATE_TIME = DELETE_TIME - 1L; + private static final Blob BLOB = Blob.builder("b", "n") + .acl(ACL) + .componentCount(COMPONENT_COUNT) + .contentType(CONTENT_TYPE) + .cacheControl(CACHE_CONTROL) + .contentDisposition(CONTENT_DISPOSITION) + .contentEncoding(CONTENT_ENCODING) + .contentLanguage(CONTENT_LANGUAGE) + .crc32c(CRC32) + .deleteTime(DELETE_TIME) + .etag(ETAG) + .generation(GENERATION) + .id(ID) + .md5(MD5) + .mediaLink(MEDIA_LINK) + .metadata(METADATA) + .metageneration(META_GENERATION) + .owner(OWNER) + .selfLink(SELF_LINK) + .size(SIZE) + .updateTime(UPDATE_TIME) + .build(); + + @Test + public void testToBuilder() { + compareBlobs(BLOB, BLOB.toBuilder().build()); + Blob blob = BLOB.toBuilder().name("n2").bucket("b2").size(200L).build(); + assertEquals("n2", blob.name()); + assertEquals("b2", blob.bucket()); + assertEquals(Long.valueOf(200), blob.size()); + blob = blob.toBuilder().name("n").bucket("b").size(SIZE).build(); + compareBlobs(BLOB, blob); + } + + @Test + public void testOf() { + Blob blob = Blob.of("b", "n"); + assertEquals("b", blob.bucket()); + assertEquals("n", blob.name()); + } + + @Test + public void testBuilder() { + assertEquals("b", BLOB.bucket()); + assertEquals("n", BLOB.name()); + assertEquals(ACL, BLOB.acl()); + assertEquals(COMPONENT_COUNT, BLOB.componentCount()); + assertEquals(CONTENT_TYPE, BLOB.contentType()); + assertEquals(CACHE_CONTROL, BLOB.cacheControl() ); + assertEquals(CONTENT_DISPOSITION, BLOB.contentDisposition()); + assertEquals(CONTENT_ENCODING, BLOB.contentEncoding()); + assertEquals(CONTENT_LANGUAGE, BLOB.contentLanguage()); + assertEquals(CRC32, BLOB.crc32c()); + assertEquals(DELETE_TIME, BLOB.deleteTime()); + assertEquals(ETAG, BLOB.etag()); + assertEquals(GENERATION, BLOB.generation()); + assertEquals(ID, BLOB.id()); + assertEquals(MD5, BLOB.md5()); + assertEquals(MEDIA_LINK, BLOB.mediaLink()); + assertEquals(METADATA, BLOB.metadata()); + assertEquals(META_GENERATION, BLOB.metageneration()); + assertEquals(OWNER, BLOB.owner()); + assertEquals(SELF_LINK, BLOB.selfLink()); + assertEquals(SIZE, BLOB.size()); + assertEquals(UPDATE_TIME, BLOB.updateTime()); + } + + private void compareBlobs(Blob expected, Blob value) { + assertEquals(expected, value); + assertEquals(expected.bucket(), value.bucket()); + assertEquals(expected.name(), value.name()); + assertEquals(expected.acl(), value.acl()); + assertEquals(expected.componentCount(), value.componentCount()); + assertEquals(expected.contentType(), value.contentType()); + assertEquals(expected.cacheControl(), value.cacheControl() ); + assertEquals(expected.contentDisposition(), value.contentDisposition()); + assertEquals(expected.contentEncoding(), value.contentEncoding()); + assertEquals(expected.contentLanguage(), value.contentLanguage()); + assertEquals(expected.crc32c(), value.crc32c()); + assertEquals(expected.deleteTime(), value.deleteTime()); + assertEquals(expected.etag(), value.etag()); + assertEquals(expected.generation(), value.generation()); + assertEquals(expected.id(), value.id()); + assertEquals(expected.md5(), value.md5()); + assertEquals(expected.mediaLink(), value.mediaLink()); + assertEquals(expected.metadata(), value.metadata()); + assertEquals(expected.metageneration(), value.metageneration()); + assertEquals(expected.owner(), value.owner()); + assertEquals(expected.selfLink(), value.selfLink()); + assertEquals(expected.size(), value.size()); + assertEquals(expected.updateTime(), value.updateTime()); + } + + @Test + public void testToPbAndFromPb() { + compareBlobs(BLOB, Blob.fromPb(BLOB.toPb())); + } +} diff --git a/src/test/java/com/google/gcloud/storage/BucketTest.java b/src/test/java/com/google/gcloud/storage/BucketTest.java new file mode 100644 index 000000000000..c095a6291e49 --- /dev/null +++ b/src/test/java/com/google/gcloud/storage/BucketTest.java @@ -0,0 +1,175 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * 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.gcloud.storage; + +import static com.google.gcloud.storage.Acl.Project.ProjectRole.VIEWERS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import com.google.api.services.storage.model.Bucket.Lifecycle.Rule; +import com.google.common.collect.ImmutableList; +import com.google.gcloud.storage.Acl.Project; +import com.google.gcloud.storage.Acl.Role; +import com.google.gcloud.storage.Acl.User; +import com.google.gcloud.storage.Bucket.AgeDeleteRule; +import com.google.gcloud.storage.Bucket.CreatedBeforeDeleteRule; +import com.google.gcloud.storage.Bucket.DeleteRule; +import com.google.gcloud.storage.Bucket.DeleteRule.Type; +import com.google.gcloud.storage.Bucket.IsLiveDeleteRule; +import com.google.gcloud.storage.Bucket.Location; +import com.google.gcloud.storage.Bucket.NumNewerVersionsDeleteRule; +import com.google.gcloud.storage.Bucket.RawDeleteRule; +import com.google.gcloud.storage.Bucket.StorageClass; + +import org.junit.Test; + +import java.util.Collections; +import java.util.List; + +public class BucketTest { + + private static final List ACL = ImmutableList.of( + new Acl(User.ofAllAuthenticatedUsers(), Role.READER), + new Acl(new Project(VIEWERS, "p1"), Role.WRITER)); + private static final String ETAG = "0xFF00"; + private static final String ID = "B/N:1"; + private static final Long META_GENERATION = 10L; + private static final User OWNER = new User("user@gmail.com"); + private static final String SELF_LINK = "http://storage/b/n"; + private static final Long CREATE_TIME = System.currentTimeMillis(); + private static final List CORS = Collections.singletonList(Cors.builder().build()); + private static final List DEFAULT_ACL = + Collections.singletonList(new Acl(User.ofAllAuthenticatedUsers(), Role.WRITER)); + private static final List DELETE_RULES = + Collections.singletonList(new AgeDeleteRule(5)); + private static final String INDEX_PAGE = "index.html"; + private static final String NOT_FOUND_PAGE = "error.html"; + private static final Location LOCATION = Location.asia(); + private static final StorageClass STORAGE_CLASS = StorageClass.standard(); + private static final Boolean VERSIONING_ENABLED = true; + private static final Bucket BUCKET = Bucket.builder("b") + .acl(ACL) + .etag(ETAG) + .id(ID) + .metageneration(META_GENERATION) + .owner(OWNER) + .selfLink(SELF_LINK) + .cors(CORS) + .createTime(CREATE_TIME) + .defaultAcl(DEFAULT_ACL) + .deleteRules(DELETE_RULES) + .indexPage(INDEX_PAGE) + .notFoundPage(NOT_FOUND_PAGE) + .location(LOCATION) + .storageClass(STORAGE_CLASS) + .versioningEnabled(VERSIONING_ENABLED) + .build(); + + @Test + public void testToBuilder() { + compareBuckets(BUCKET, BUCKET.toBuilder().build()); + Bucket bucket = BUCKET.toBuilder().name("B").id("id").build(); + assertEquals("B", bucket.name()); + assertEquals("id", bucket.id()); + bucket = bucket.toBuilder().name("b").id(ID).build(); + compareBuckets(BUCKET, bucket); + } + + @Test + public void testOf() { + Bucket bucket = Bucket.of("bucket"); + assertEquals("bucket", bucket.name()); + } + + @Test + public void testBuilder() { + assertEquals("b", BUCKET.name()); + assertEquals(ACL, BUCKET.acl()); + assertEquals(ETAG, BUCKET.etag()); + assertEquals(ID, BUCKET.id()); + assertEquals(META_GENERATION, BUCKET.metageneration()); + assertEquals(OWNER, BUCKET.owner()); + assertEquals(SELF_LINK, BUCKET.selfLink()); + assertEquals(CREATE_TIME, BUCKET.createTime()); + assertEquals(CORS, BUCKET.cors()); + assertEquals(DEFAULT_ACL, BUCKET.defaultAcl()); + assertEquals(DELETE_RULES, BUCKET.deleteRules()); + assertEquals(INDEX_PAGE, BUCKET.indexPage()); + assertEquals(NOT_FOUND_PAGE, BUCKET.notFoundPage()); + assertEquals(LOCATION, BUCKET.location()); + assertEquals(STORAGE_CLASS, BUCKET.storageClass()); + assertEquals(VERSIONING_ENABLED, BUCKET.versioningEnabled()); + } + + @Test + public void testToPbAndFromPb() { + compareBuckets(BUCKET, Bucket.fromPb(BUCKET.toPb())); + } + + private void compareBuckets(Bucket expected, Bucket value) { + assertEquals(expected, value); + assertEquals(expected.name(), value.name()); + assertEquals(expected.acl(), value.acl()); + assertEquals(expected.etag(), value.etag()); + assertEquals(expected.id(), value.id()); + assertEquals(expected.metageneration(), value.metageneration()); + assertEquals(expected.owner(), value.owner()); + assertEquals(expected.selfLink(), value.selfLink()); + assertEquals(expected.createTime(), value.createTime()); + assertEquals(expected.cors(), value.cors()); + assertEquals(expected.defaultAcl(), value.defaultAcl()); + assertEquals(expected.deleteRules(), value.deleteRules()); + assertEquals(expected.indexPage(), value.indexPage()); + assertEquals(expected.notFoundPage(), value.notFoundPage()); + assertEquals(expected.location(), value.location()); + assertEquals(expected.storageClass(), value.storageClass()); + assertEquals(expected.versioningEnabled(), value.versioningEnabled()); + } + + public void testLocation() { + assertEquals("ASIA", Location.asia().value()); + assertEquals("EN", 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")); + } + + 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(Type.CREATE_BEFORE, createBeforeRule.type()); + NumNewerVersionsDeleteRule versionsRule = new NumNewerVersionsDeleteRule(2); + assertEquals(2, versionsRule.numNewerVersions()); + assertEquals(Type.NUM_NEWER_VERSIONS, versionsRule.type()); + IsLiveDeleteRule isLiveRule = new IsLiveDeleteRule(true); + assertTrue(isLiveRule.isLive()); + assertEquals(Type.IS_LIVE, isLiveRule.type()); + Rule rule = new Rule().set("a", "b"); + RawDeleteRule rawRule = new RawDeleteRule(rule); + assertEquals(Type.UNKNOWN, isLiveRule.type()); + ImmutableList rules = ImmutableList + .of(ageRule, createBeforeRule, versionsRule, isLiveRule, rawRule); + for (DeleteRule delRule : rules) { + assertEquals(delRule, DeleteRule.fromPb(delRule.toPb())); + } + } +}