|
| 1 | +/* |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.gcloud.storage; |
| 18 | + |
| 19 | +import static com.google.gcloud.storage.Acl.Project.ProjectRole.VIEWERS; |
| 20 | +import static com.google.gcloud.storage.Acl.Role.READER; |
| 21 | +import static com.google.gcloud.storage.Acl.Role.WRITER; |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | + |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.common.collect.ImmutableMap; |
| 26 | +import com.google.gcloud.storage.Acl.Project; |
| 27 | +import com.google.gcloud.storage.Acl.User; |
| 28 | + |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +public class BlobTest { |
| 35 | + |
| 36 | + private static final List<Acl> ACL = ImmutableList.of( |
| 37 | + new Acl(User.ofAllAuthenticatedUsers(), READER), |
| 38 | + new Acl(new Project(VIEWERS, "p1"), WRITER)); |
| 39 | + private static final Integer COMPONENT_COUNT = 2; |
| 40 | + private static final String CONTENT_TYPE = "text/html"; |
| 41 | + private static final String CACHE_CONTROL = "cache"; |
| 42 | + private static final String CONTENT_DISPOSITION = "content-disposition"; |
| 43 | + private static final String CONTENT_ENCODING = "UTF-8"; |
| 44 | + private static final String CONTENT_LANGUAGE = "En"; |
| 45 | + private static final String CRC32 = "0xFF00"; |
| 46 | + private static final Long DELETE_TIME = System.currentTimeMillis(); |
| 47 | + private static final String ETAG = "0xFF00"; |
| 48 | + private static final Long GENERATION = 1L; |
| 49 | + private static final String ID = "B/N:1"; |
| 50 | + private static final String MD5 = "0xFF00"; |
| 51 | + private static final String MEDIA_LINK = "http://media/b/n"; |
| 52 | + private static final Map<String, String> METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); |
| 53 | + private static final Long META_GENERATION = 10L; |
| 54 | + private static final User OWNER = new User("user@gmail.com"); |
| 55 | + private static final String SELF_LINK = "http://storage/b/n"; |
| 56 | + private static final Long SIZE = 1024L; |
| 57 | + private static final Long UPDATE_TIME = DELETE_TIME - 1L; |
| 58 | + private static final Blob BLOB = Blob.builder("b", "n") |
| 59 | + .acl(ACL) |
| 60 | + .componentCount(COMPONENT_COUNT) |
| 61 | + .contentType(CONTENT_TYPE) |
| 62 | + .cacheControl(CACHE_CONTROL) |
| 63 | + .contentDisposition(CONTENT_DISPOSITION) |
| 64 | + .contentEncoding(CONTENT_ENCODING) |
| 65 | + .contentLanguage(CONTENT_LANGUAGE) |
| 66 | + .crc32c(CRC32) |
| 67 | + .deleteTime(DELETE_TIME) |
| 68 | + .etag(ETAG) |
| 69 | + .generation(GENERATION) |
| 70 | + .id(ID) |
| 71 | + .md5(MD5) |
| 72 | + .mediaLink(MEDIA_LINK) |
| 73 | + .metadata(METADATA) |
| 74 | + .metageneration(META_GENERATION) |
| 75 | + .owner(OWNER) |
| 76 | + .selfLink(SELF_LINK) |
| 77 | + .size(SIZE) |
| 78 | + .updateTime(UPDATE_TIME) |
| 79 | + .build(); |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testToBuilder() { |
| 83 | + compareBlobs(BLOB, BLOB.toBuilder().build()); |
| 84 | + Blob blob = BLOB.toBuilder().name("n2").bucket("b2").size(200L).build(); |
| 85 | + assertEquals("n2", blob.name()); |
| 86 | + assertEquals("b2", blob.bucket()); |
| 87 | + assertEquals(Long.valueOf(200), blob.size()); |
| 88 | + blob = blob.toBuilder().name("n").bucket("b").size(SIZE).build(); |
| 89 | + compareBlobs(BLOB, blob); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testOf() { |
| 94 | + Blob blob = Blob.of("b", "n"); |
| 95 | + assertEquals("b", blob.bucket()); |
| 96 | + assertEquals("n", blob.name()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testBuilder() { |
| 101 | + assertEquals("b", BLOB.bucket()); |
| 102 | + assertEquals("n", BLOB.name()); |
| 103 | + assertEquals(ACL, BLOB.acl()); |
| 104 | + assertEquals(COMPONENT_COUNT, BLOB.componentCount()); |
| 105 | + assertEquals(CONTENT_TYPE, BLOB.contentType()); |
| 106 | + assertEquals(CACHE_CONTROL, BLOB.cacheControl() ); |
| 107 | + assertEquals(CONTENT_DISPOSITION, BLOB.contentDisposition()); |
| 108 | + assertEquals(CONTENT_ENCODING, BLOB.contentEncoding()); |
| 109 | + assertEquals(CONTENT_LANGUAGE, BLOB.contentLanguage()); |
| 110 | + assertEquals(CRC32, BLOB.crc32c()); |
| 111 | + assertEquals(DELETE_TIME, BLOB.deleteTime()); |
| 112 | + assertEquals(ETAG, BLOB.etag()); |
| 113 | + assertEquals(GENERATION, BLOB.generation()); |
| 114 | + assertEquals(ID, BLOB.id()); |
| 115 | + assertEquals(MD5, BLOB.md5()); |
| 116 | + assertEquals(MEDIA_LINK, BLOB.mediaLink()); |
| 117 | + assertEquals(METADATA, BLOB.metadata()); |
| 118 | + assertEquals(META_GENERATION, BLOB.metageneration()); |
| 119 | + assertEquals(OWNER, BLOB.owner()); |
| 120 | + assertEquals(SELF_LINK, BLOB.selfLink()); |
| 121 | + assertEquals(SIZE, BLOB.size()); |
| 122 | + assertEquals(UPDATE_TIME, BLOB.updateTime()); |
| 123 | + } |
| 124 | + |
| 125 | + private void compareBlobs(Blob expected, Blob value) { |
| 126 | + assertEquals(expected, value); |
| 127 | + assertEquals(expected.bucket(), value.bucket()); |
| 128 | + assertEquals(expected.name(), value.name()); |
| 129 | + assertEquals(expected.acl(), value.acl()); |
| 130 | + assertEquals(expected.componentCount(), value.componentCount()); |
| 131 | + assertEquals(expected.contentType(), value.contentType()); |
| 132 | + assertEquals(expected.cacheControl(), value.cacheControl() ); |
| 133 | + assertEquals(expected.contentDisposition(), value.contentDisposition()); |
| 134 | + assertEquals(expected.contentEncoding(), value.contentEncoding()); |
| 135 | + assertEquals(expected.contentLanguage(), value.contentLanguage()); |
| 136 | + assertEquals(expected.crc32c(), value.crc32c()); |
| 137 | + assertEquals(expected.deleteTime(), value.deleteTime()); |
| 138 | + assertEquals(expected.etag(), value.etag()); |
| 139 | + assertEquals(expected.generation(), value.generation()); |
| 140 | + assertEquals(expected.id(), value.id()); |
| 141 | + assertEquals(expected.md5(), value.md5()); |
| 142 | + assertEquals(expected.mediaLink(), value.mediaLink()); |
| 143 | + assertEquals(expected.metadata(), value.metadata()); |
| 144 | + assertEquals(expected.metageneration(), value.metageneration()); |
| 145 | + assertEquals(expected.owner(), value.owner()); |
| 146 | + assertEquals(expected.selfLink(), value.selfLink()); |
| 147 | + assertEquals(expected.size(), value.size()); |
| 148 | + assertEquals(expected.updateTime(), value.updateTime()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testToPbAndFromPb() { |
| 153 | + compareBlobs(BLOB, Blob.fromPb(BLOB.toPb())); |
| 154 | + } |
| 155 | +} |
0 commit comments