Skip to content

Commit 3396968

Browse files
committed
Merge pull request #67 from aozarov/master
add tests for bucket and blob
2 parents 0265e0a + 6c5e215 commit 3396968

4 files changed

Lines changed: 394 additions & 21 deletions

File tree

src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ public Type type() {
103103
return type;
104104
}
105105

106+
@Override
107+
public int hashCode() {
108+
return Objects.hash(type);
109+
}
110+
111+
@Override
112+
public boolean equals(Object obj) {
113+
if (this == obj) {
114+
return true;
115+
}
116+
if (obj == null || getClass() != obj.getClass()) {
117+
return false;
118+
}
119+
final DeleteRule other = (DeleteRule) obj;
120+
return Objects.equals(toPb(), other.toPb());
121+
}
122+
106123
Rule toPb() {
107124
Rule rule = new Rule();
108125
rule.setAction(new Rule.Action().setType(SUPPORTED_ACTION));
@@ -114,7 +131,7 @@ Rule toPb() {
114131

115132
abstract void populateCondition(Rule.Condition condition);
116133

117-
private static DeleteRule fromPb(Rule rule) {
134+
static DeleteRule fromPb(Rule rule) {
118135
if (rule.getAction() != null && SUPPORTED_ACTION.endsWith(rule.getAction().getType())) {
119136
Rule.Condition condition = rule.getCondition();
120137
Integer age = condition.getAge();
@@ -346,6 +363,23 @@ public static Location of(String value) {
346363
return option == null ? new Location(value) : option.location;
347364
}
348365

366+
@Override
367+
public int hashCode() {
368+
return Objects.hash(value);
369+
}
370+
371+
@Override
372+
public boolean equals(Object obj) {
373+
if (this == obj) {
374+
return true;
375+
}
376+
if (obj == null || getClass() != obj.getClass()) {
377+
return false;
378+
}
379+
final Location other = (Location) obj;
380+
return Objects.equals(this.value, other.value);
381+
}
382+
349383
@Override
350384
public String toString() {
351385
return value();
@@ -412,7 +446,7 @@ public Builder notFoundPage(String notFoundPage) {
412446
return this;
413447
}
414448

415-
public Builder deleteRules(Iterable<DeleteRule> rules) {
449+
public Builder deleteRules(Iterable<? extends DeleteRule> rules) {
416450
this.deleteRules = ImmutableList.copyOf(rules);
417451
return this;
418452
}
@@ -490,7 +524,7 @@ public String name() {
490524
return name;
491525
}
492526

493-
public Entity Owner() {
527+
public Entity owner() {
494528
return owner;
495529
}
496530

@@ -510,7 +544,7 @@ public String notFoundPage() {
510544
return notFoundPage;
511545
}
512546

513-
public List<DeleteRule> deleteRules() {
547+
public List<? extends DeleteRule> deleteRules() {
514548
return deleteRules;
515549
}
516550

@@ -652,6 +686,7 @@ public Rule apply(DeleteRule deleteRule) {
652686
return deleteRule.toPb();
653687
}
654688
}));
689+
bucketPb.setLifecycle(lifecycle);
655690
}
656691
return bucketPb;
657692
}

src/main/java/com/google/gcloud/storage/Cors.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,17 @@ public Builder maxAgeSeconds(Integer maxAgeSeconds) {
130130
}
131131

132132
public Builder methods(Iterable<Method> methods) {
133-
this.methods = ImmutableList.copyOf(methods);
133+
this.methods = methods != null ? ImmutableList.copyOf(methods) : null;
134134
return this;
135135
}
136136

137137
public Builder origins(Iterable<Origin> origins) {
138-
this.origins = ImmutableList.copyOf(origins);
138+
this.origins = origins != null ? ImmutableList.copyOf(origins) : null;
139139
return this;
140140
}
141141

142142
public Builder responseHeaders(Iterable<String> headers) {
143-
this.responseHeaders = ImmutableList.copyOf(headers);
143+
this.responseHeaders = headers != null ? ImmutableList.copyOf(headers) : null;
144144
return this;
145145
}
146146

@@ -205,25 +205,33 @@ Bucket.Cors toPb() {
205205
Bucket.Cors pb = new Bucket.Cors();
206206
pb.setMaxAgeSeconds(maxAgeSeconds);
207207
pb.setResponseHeader(responseHeaders);
208-
pb.setMethod(newArrayList(transform(methods(), Functions.toStringFunction())));
209-
pb.setOrigin(newArrayList(transform(origins(), Functions.toStringFunction())));
208+
if (methods != null) {
209+
pb.setMethod(newArrayList(transform(methods, Functions.toStringFunction())));
210+
}
211+
if (origins != null) {
212+
pb.setOrigin(newArrayList(transform(origins, Functions.toStringFunction())));
213+
}
210214
return pb;
211215
}
212216

213217
static Cors fromPb(Bucket.Cors cors) {
214218
Builder builder = builder().maxAgeSeconds(cors.getMaxAgeSeconds());
215-
builder.methods(transform(cors.getMethod(), new Function<String, Method>() {
216-
@Override
217-
public Method apply(String name) {
218-
return Method.valueOf(name.toUpperCase());
219-
}
220-
}));
221-
builder.origins(transform(cors.getOrigin(), new Function<String, Origin>() {
222-
@Override
223-
public Origin apply(String value) {
224-
return Origin.of(value);
225-
}
226-
}));
219+
if (cors.getMethod() != null) {
220+
builder.methods(transform(cors.getMethod(), new Function<String, Method>() {
221+
@Override
222+
public Method apply(String name) {
223+
return Method.valueOf(name.toUpperCase());
224+
}
225+
}));
226+
}
227+
if (cors.getOrigin() != null) {
228+
builder.origins(transform(cors.getOrigin(), new Function<String, Origin>() {
229+
@Override
230+
public Origin apply(String value) {
231+
return Origin.of(value);
232+
}
233+
}));
234+
}
227235
builder.responseHeaders(cors.getResponseHeader());
228236
return builder.build();
229237
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)