Skip to content

Commit 9a1eea6

Browse files
committed
Minor fixes to bigquery functional classes
- Remove periods from javadoc return paragraphs - Add options to load methods - Implement reload using load - Document that reload can return null - Better unit tests
1 parent 7944f16 commit 9a1eea6

7 files changed

Lines changed: 141 additions & 26 deletions

File tree

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public void testReload() throws Exception {
9595
assertEquals(updatedInfo, updatedDataset.info());
9696
}
9797

98+
@Test
99+
public void testReloadNull() throws Exception {
100+
expect(bigquery.getDataset(DATASET_ID.dataset())).andReturn(null);
101+
replay(bigquery);
102+
assertNull(dataset.reload());
103+
}
104+
98105
@Test
99106
public void testReloadWithOptions() throws Exception {
100107
DatasetInfo updatedInfo = DATASET_INFO.toBuilder().description("Description").build();

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ public void testReload() throws Exception {
116116
assertEquals(updatedInfo, updatedJob.info());
117117
}
118118

119+
@Test
120+
public void testReloadNull() throws Exception {
121+
expect(bigquery.getJob(JOB_INFO.jobId().job())).andReturn(null);
122+
replay(bigquery);
123+
assertNull(job.reload());
124+
}
125+
119126
@Test
120127
public void testReloadWithOptions() throws Exception {
121128
JobInfo updatedInfo = JOB_INFO.toBuilder().etag("etag").build();

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ public void testReload() throws Exception {
121121
assertEquals(updatedInfo, updatedTable.info());
122122
}
123123

124+
@Test
125+
public void testReloadNull() throws Exception {
126+
expect(bigquery.getTable(TABLE_INFO.tableId())).andReturn(null);
127+
replay(bigquery);
128+
assertNull(table.reload());
129+
}
130+
124131
@Test
125132
public void testReloadWithOptions() throws Exception {
126133
TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build();

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,18 @@ public Blob(Storage storage, BlobInfo info) {
156156

157157
/**
158158
* Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to
159-
* get the latest blob information.
159+
* get the latest blob information. Returns {@code null} if the blob does not exist.
160160
*
161161
* @param storage the storage service used for issuing requests
162162
* @param bucket bucket's name
163+
* @param options blob get options
163164
* @param blob blob's name
164-
* @return the {@code Blob} object or {@code null} if not found.
165+
* @return the {@code Blob} object or {@code null} if not found
165166
* @throws StorageException upon failure
166167
*/
167-
public static Blob load(Storage storage, String bucket, String blob) {
168-
return load(storage, BlobId.of(bucket, blob));
168+
public static Blob load(Storage storage, String bucket, String blob,
169+
Storage.BlobGetOption... options) {
170+
return load(storage, BlobId.of(bucket, blob), options);
169171
}
170172

171173
/**
@@ -174,11 +176,12 @@ public static Blob load(Storage storage, String bucket, String blob) {
174176
*
175177
* @param storage the storage service used for issuing requests
176178
* @param blobId blob's identifier
177-
* @return the {@code Blob} object or {@code null} if not found.
179+
* @param options blob get options
180+
* @return the {@code Blob} object or {@code null} if not found
178181
* @throws StorageException upon failure
179182
*/
180-
public static Blob load(Storage storage, BlobId blobId) {
181-
BlobInfo info = storage.get(blobId);
183+
public static Blob load(Storage storage, BlobId blobId, Storage.BlobGetOption... options) {
184+
BlobInfo info = storage.get(blobId, options);
182185
return info != null ? new Blob(storage, info) : null;
183186
}
184187

@@ -221,14 +224,14 @@ public byte[] content(Storage.BlobSourceOption... options) {
221224
}
222225

223226
/**
224-
* Fetches current blob's latest information.
227+
* Fetches current blob's latest information. Returns {@code null} if the blob does not exist.
225228
*
226229
* @param options blob read options
227-
* @return a {@code Blob} object with latest information
230+
* @return a {@code Blob} object with latest information or {@code null} if not found
228231
* @throws StorageException upon failure
229232
*/
230233
public Blob reload(BlobSourceOption... options) {
231-
return new Blob(storage, storage.get(info.blobId(), toGetOptions(info, options)));
234+
return Blob.load(storage, info.blobId(), toGetOptions(info, options));
232235
}
233236

234237
/**
@@ -368,7 +371,7 @@ public Storage storage() {
368371
* @param storage the storage service used to issue the request
369372
* @param blobs the blobs to get
370373
* @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has
371-
* been denied the corresponding item in the list is {@code null}.
374+
* been denied the corresponding item in the list is {@code null}
372375
* @throws StorageException upon failure
373376
*/
374377
public static List<Blob> get(final Storage storage, BlobId... blobs) {
@@ -397,7 +400,7 @@ public Blob apply(BlobInfo blobInfo) {
397400
* @param storage the storage service used to issue the request
398401
* @param infos the blobs to update
399402
* @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has
400-
* been denied the corresponding item in the list is {@code null}.
403+
* been denied the corresponding item in the list is {@code null}
401404
* @throws StorageException upon failure
402405
*/
403406
public static List<Blob> update(final Storage storage, BlobInfo... infos) {
@@ -422,7 +425,7 @@ public Blob apply(BlobInfo blobInfo) {
422425
* @param blobs the blobs to delete
423426
* @return an immutable list of booleans. If a blob has been deleted the corresponding item in the
424427
* list is {@code true}. If a blob was not found, deletion failed or access to the resource
425-
* was denied the corresponding item is {@code false}.
428+
* was denied the corresponding item is {@code false}
426429
* @throws StorageException upon failure
427430
*/
428431
public static List<Boolean> delete(Storage storage, BlobId... blobs) {

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,12 @@ public Bucket(Storage storage, BucketInfo info) {
210210
*
211211
* @param storage the storage service used for issuing requests
212212
* @param bucket bucket's name
213-
* @return the {@code Bucket} object or {@code null} if not found.
213+
* @param options blob get options
214+
* @return the {@code Bucket} object or {@code null} if not found
214215
* @throws StorageException upon failure
215216
*/
216-
public static Bucket load(Storage storage, String bucket) {
217-
BucketInfo info = storage.get(bucket);
217+
public static Bucket load(Storage storage, String bucket, Storage.BucketGetOption... options) {
218+
BucketInfo info = storage.get(bucket, options);
218219
return info != null ? new Bucket(storage, info) : null;
219220
}
220221

@@ -239,14 +240,14 @@ public boolean exists(BucketSourceOption... options) {
239240
}
240241

241242
/**
242-
* Fetches current bucket's latest information.
243+
* Fetches current bucket's latest information. Returns {@code null} if the bucket does not exist.
243244
*
244245
* @param options bucket read options
245-
* @return a {@code Bucket} object with latest information
246+
* @return a {@code Bucket} object with latest information or {@code null} if not found
246247
* @throws StorageException upon failure
247248
*/
248249
public Bucket reload(BucketSourceOption... options) {
249-
return new Bucket(storage, storage.get(info.name(), toGetOptions(info, options)));
250+
return Bucket.load(storage, info.name(), toGetOptions(info, options));
250251
}
251252

252253
/**
@@ -307,7 +308,7 @@ public Blob get(String blob, BlobGetOption... options) {
307308
* @param blobName1 first blob to get
308309
* @param blobName2 second blob to get
309310
* @param blobNames other blobs to get
310-
* @return an immutable list of {@code Blob} objects.
311+
* @return an immutable list of {@code Blob} objects
311312
* @throws StorageException upon failure
312313
*/
313314
public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
@@ -337,7 +338,7 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
337338
* @param contentType the blob content type. If {@code null} then
338339
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
339340
* @param options options for blob creation
340-
* @return a complete blob information.
341+
* @return a complete blob information
341342
* @throws StorageException upon failure
342343
*/
343344
public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) {
@@ -356,7 +357,7 @@ public Blob create(String blob, byte[] content, String contentType, BlobTargetOp
356357
* @param contentType the blob content type. If {@code null} then
357358
* {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used.
358359
* @param options options for blob creation
359-
* @return a complete blob information.
360+
* @return a complete blob information
360361
* @throws StorageException upon failure
361362
*/
362363
public Blob create(String blob, InputStream content, String contentType,

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
public class BlobTest {
4747

48-
private static final BlobInfo BLOB_INFO = BlobInfo.builder("b", "n").build();
48+
private static final BlobInfo BLOB_INFO = BlobInfo.builder("b", "n").metageneration(42L).build();
4949
private static final BlobId[] BLOB_ID_ARRAY = {BlobId.of("b1", "n1"),
5050
BlobId.of("b2", "n2"), BlobId.of("b3", "n3")};
5151
private static final BlobInfo[] BLOB_INFO_ARRAY = {BlobInfo.builder("b1", "n1").build(),
@@ -105,6 +105,24 @@ public void testReload() throws Exception {
105105
assertEquals(updatedInfo, updatedBlob.info());
106106
}
107107

108+
@Test
109+
public void testReloadNull() throws Exception {
110+
expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(null);
111+
replay(storage);
112+
assertNull(blob.reload());
113+
}
114+
115+
@Test
116+
public void testReloadWithOptions() throws Exception {
117+
BlobInfo updatedInfo = BLOB_INFO.toBuilder().cacheControl("c").build();
118+
Storage.BlobGetOption[] options = {Storage.BlobGetOption.metagenerationMatch(42L)};
119+
expect(storage.get(BLOB_INFO.blobId(), options)).andReturn(updatedInfo);
120+
replay(storage);
121+
Blob updatedBlob = blob.reload(Blob.BlobSourceOption.metagenerationMatch());
122+
assertSame(storage, updatedBlob.storage());
123+
assertEquals(updatedInfo, updatedBlob.info());
124+
}
125+
108126
@Test
109127
public void testUpdate() throws Exception {
110128
BlobInfo updatedInfo = BLOB_INFO.toBuilder().cacheControl("c").build();
@@ -285,18 +303,53 @@ public void testDeleteSome() throws Exception {
285303

286304
@Test
287305
public void testLoadFromString() throws Exception {
288-
expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO);
306+
expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(BLOB_INFO);
289307
replay(storage);
290308
Blob loadedBlob = Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name());
291309
assertEquals(BLOB_INFO, loadedBlob.info());
292310
}
293311

294312
@Test
295313
public void testLoadFromId() throws Exception {
296-
expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO);
314+
expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(BLOB_INFO);
297315
replay(storage);
298316
Blob loadedBlob = Blob.load(storage, BLOB_INFO.blobId());
299317
assertNotNull(loadedBlob);
300318
assertEquals(BLOB_INFO, loadedBlob.info());
301319
}
320+
321+
@Test
322+
public void testLoadFromStringNull() throws Exception {
323+
expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(null);
324+
replay(storage);
325+
assertNull(Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name()));
326+
}
327+
328+
@Test
329+
public void testLoadFromIdNull() throws Exception {
330+
expect(storage.get(BLOB_INFO.blobId(), new Storage.BlobGetOption[0])).andReturn(null);
331+
replay(storage);
332+
assertNull(Blob.load(storage, BLOB_INFO.blobId()));
333+
}
334+
335+
@Test
336+
public void testLoadFromStringWithOptions() throws Exception {
337+
expect(storage.get(BLOB_INFO.blobId(), Storage.BlobGetOption.generationMatch(42L)))
338+
.andReturn(BLOB_INFO);
339+
replay(storage);
340+
Blob loadedBlob = Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name(),
341+
Storage.BlobGetOption.generationMatch(42L));
342+
assertEquals(BLOB_INFO, loadedBlob.info());
343+
}
344+
345+
@Test
346+
public void testLoadFromIdWithOptions() throws Exception {
347+
expect(storage.get(BLOB_INFO.blobId(), Storage.BlobGetOption.generationMatch(42L)))
348+
.andReturn(BLOB_INFO);
349+
replay(storage);
350+
Blob loadedBlob =
351+
Blob.load(storage, BLOB_INFO.blobId(), Storage.BlobGetOption.generationMatch(42L));
352+
assertNotNull(loadedBlob);
353+
assertEquals(BLOB_INFO, loadedBlob.info());
354+
}
302355
}

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.assertEquals;
2525
import static org.junit.Assert.assertFalse;
2626
import static org.junit.Assert.assertNotNull;
27+
import static org.junit.Assert.assertNull;
2728
import static org.junit.Assert.assertSame;
2829
import static org.junit.Assert.assertTrue;
2930

@@ -47,7 +48,7 @@
4748

4849
public class BucketTest {
4950

50-
private static final BucketInfo BUCKET_INFO = BucketInfo.of("b");
51+
private static final BucketInfo BUCKET_INFO = BucketInfo.builder("b").metageneration(42L).build();
5152
private static final Iterable<BlobInfo> BLOB_INFO_RESULTS = ImmutableList.of(
5253
BlobInfo.builder("b", "n1").build(),
5354
BlobInfo.builder("b", "n2").build(),
@@ -100,6 +101,24 @@ public void testReload() throws Exception {
100101
assertEquals(updatedInfo, updatedBucket.info());
101102
}
102103

104+
@Test
105+
public void testReloadNull() throws Exception {
106+
expect(storage.get(BUCKET_INFO.name())).andReturn(null);
107+
replay(storage);
108+
assertNull(bucket.reload());
109+
}
110+
111+
@Test
112+
public void testReloadWithOptions() throws Exception {
113+
BucketInfo updatedInfo = BUCKET_INFO.toBuilder().notFoundPage("p").build();
114+
expect(storage.get(updatedInfo.name(), Storage.BucketGetOption.metagenerationMatch(42L)))
115+
.andReturn(updatedInfo);
116+
replay(storage);
117+
Bucket updatedBucket = bucket.reload(Bucket.BucketSourceOption.metagenerationMatch());
118+
assertSame(storage, updatedBucket.storage());
119+
assertEquals(updatedInfo, updatedBucket.info());
120+
}
121+
103122
@Test
104123
public void testUpdate() throws Exception {
105124
BucketInfo updatedInfo = BUCKET_INFO.toBuilder().notFoundPage("p").build();
@@ -223,4 +242,22 @@ public void testLoad() throws Exception {
223242
assertNotNull(loadedBucket);
224243
assertEquals(BUCKET_INFO, loadedBucket.info());
225244
}
245+
246+
@Test
247+
public void testLoadNull() throws Exception {
248+
expect(storage.get(BUCKET_INFO.name())).andReturn(null);
249+
replay(storage);
250+
assertNull(Bucket.load(storage, BUCKET_INFO.name()));
251+
}
252+
253+
@Test
254+
public void testLoadWithOptions() throws Exception {
255+
expect(storage.get(BUCKET_INFO.name(), Storage.BucketGetOption.fields()))
256+
.andReturn(BUCKET_INFO);
257+
replay(storage);
258+
Bucket loadedBucket =
259+
Bucket.load(storage, BUCKET_INFO.name(), Storage.BucketGetOption.fields());
260+
assertNotNull(loadedBucket);
261+
assertEquals(BUCKET_INFO, loadedBucket.info());
262+
}
226263
}

0 commit comments

Comments
 (0)