From 452e27495dfae67fb43adc96b020fc1bbaa10961 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 2 Mar 2016 15:52:06 +0100 Subject: [PATCH 01/44] Handle eventally consistent blob lists in storage ITs --- .../gcloud/storage/it/ITStorageTest.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java index 8e954de57e68..1bdd14dd79f8 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java @@ -28,6 +28,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterators; import com.google.common.collect.Lists; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; @@ -287,8 +288,8 @@ public void testGetBlobFailNonExistingGeneration() { assertTrue(remoteBlob.delete()); } - @Test - public void testListBlobsSelectedFields() { + @Test(timeout = 5000) + public void testListBlobsSelectedFields() throws InterruptedException { String[] blobNames = {"test-list-blobs-selected-fields-blob1", "test-list-blobs-selected-fields-blob2"}; ImmutableMap metadata = ImmutableMap.of("k", "v"); @@ -307,10 +308,18 @@ public void testListBlobsSelectedFields() { Page page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"), Storage.BlobListOption.fields(BlobField.METADATA)); - int index = 0; + // Listing blobs is eventually consistent, we loop until the list is of the expected size. The + // test fails if timeout is reached. + while (Iterators.size(page.values().iterator()) != 2) { + Thread.sleep(500); + page = storage.list(BUCKET, + Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"), + Storage.BlobListOption.fields(BlobField.METADATA)); + } + Set blobSet = ImmutableSet.of(blobNames[0], blobNames[1]); for (Blob remoteBlob : page.values()) { assertEquals(BUCKET, remoteBlob.bucket()); - assertEquals(blobNames[index++], remoteBlob.name()); + assertTrue(blobSet.contains(remoteBlob.name())); assertEquals(metadata, remoteBlob.metadata()); assertNull(remoteBlob.contentType()); } @@ -318,8 +327,8 @@ public void testListBlobsSelectedFields() { assertTrue(remoteBlob2.delete()); } - @Test - public void testListBlobsEmptySelectedFields() { + @Test(timeout = 5000) + public void testListBlobsEmptySelectedFields() throws InterruptedException { String[] blobNames = {"test-list-blobs-empty-selected-fields-blob1", "test-list-blobs-empty-selected-fields-blob2"}; BlobInfo blob1 = BlobInfo.builder(BUCKET, blobNames[0]) @@ -335,17 +344,25 @@ public void testListBlobsEmptySelectedFields() { Page page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-empty-selected-fields-blob"), Storage.BlobListOption.fields()); - int index = 0; + // Listing blobs is eventually consistent, we loop until the list is of the expected size. The + // test fails if timeout is reached. + while (Iterators.size(page.values().iterator()) != 2) { + Thread.sleep(500); + page = storage.list(BUCKET, + Storage.BlobListOption.prefix("test-list-blobs-empty-selected-fields-blob"), + Storage.BlobListOption.fields()); + } + Set blobSet = ImmutableSet.of(blobNames[0], blobNames[1]); for (Blob remoteBlob : page.values()) { assertEquals(BUCKET, remoteBlob.bucket()); - assertEquals(blobNames[index++], remoteBlob.name()); + assertTrue(blobSet.contains(remoteBlob.name())); assertNull(remoteBlob.contentType()); } assertTrue(remoteBlob1.delete()); assertTrue(remoteBlob2.delete()); } - @Test + @Test(timeout = 10000) public void testListBlobsVersioned() throws ExecutionException, InterruptedException { String bucketName = RemoteGcsHelper.generateBucketName(); Bucket bucket = storage.create(BucketInfo.builder(bucketName).versioningEnabled(true).build()); @@ -366,6 +383,14 @@ public void testListBlobsVersioned() throws ExecutionException, InterruptedExcep Page page = storage.list(bucketName, Storage.BlobListOption.prefix("test-list-blobs-versioned-blob"), Storage.BlobListOption.versions(true)); + // Listing blobs is eventually consistent, we loop until the list is of the expected size. The + // test fails if timeout is reached. + while (Iterators.size(page.values().iterator()) != 3) { + Thread.sleep(500); + page = storage.list(bucketName, + Storage.BlobListOption.prefix("test-list-blobs-versioned-blob"), + Storage.BlobListOption.versions(true)); + } Set blobSet = ImmutableSet.of(blobNames[0], blobNames[1]); for (Blob remoteBlob : page.values()) { assertEquals(bucketName, remoteBlob.bucket()); From 622dcc9055f082b44ed32abfda9e585c7e76ce8e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 3 Mar 2016 10:13:17 +0100 Subject: [PATCH 02/44] Replace values().iterator() with iterateAll() in blob list ITs --- .../gcloud/storage/it/ITStorageTest.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java index 1bdd14dd79f8..38521ae5e137 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java @@ -101,13 +101,12 @@ public static void afterClass() throws ExecutionException, InterruptedException @Test(timeout = 5000) public void testListBuckets() throws InterruptedException { - Iterator bucketIterator = - storage.list(Storage.BucketListOption.prefix(BUCKET), - Storage.BucketListOption.fields()).values().iterator(); + Iterator bucketIterator = storage.list(Storage.BucketListOption.prefix(BUCKET), + Storage.BucketListOption.fields()).iterateAll(); while (!bucketIterator.hasNext()) { Thread.sleep(500); bucketIterator = storage.list(Storage.BucketListOption.prefix(BUCKET), - Storage.BucketListOption.fields()).values().iterator(); + Storage.BucketListOption.fields()).iterateAll(); } while (bucketIterator.hasNext()) { Bucket remoteBucket = bucketIterator.next(); @@ -310,14 +309,16 @@ public void testListBlobsSelectedFields() throws InterruptedException { Storage.BlobListOption.fields(BlobField.METADATA)); // Listing blobs is eventually consistent, we loop until the list is of the expected size. The // test fails if timeout is reached. - while (Iterators.size(page.values().iterator()) != 2) { + while (Iterators.size(page.iterateAll()) != 2) { Thread.sleep(500); page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"), Storage.BlobListOption.fields(BlobField.METADATA)); } Set blobSet = ImmutableSet.of(blobNames[0], blobNames[1]); - for (Blob remoteBlob : page.values()) { + Iterator iterator = page.iterateAll(); + while (iterator.hasNext()) { + Blob remoteBlob = iterator.next(); assertEquals(BUCKET, remoteBlob.bucket()); assertTrue(blobSet.contains(remoteBlob.name())); assertEquals(metadata, remoteBlob.metadata()); @@ -346,14 +347,16 @@ public void testListBlobsEmptySelectedFields() throws InterruptedException { Storage.BlobListOption.fields()); // Listing blobs is eventually consistent, we loop until the list is of the expected size. The // test fails if timeout is reached. - while (Iterators.size(page.values().iterator()) != 2) { + while (Iterators.size(page.iterateAll()) != 2) { Thread.sleep(500); page = storage.list(BUCKET, Storage.BlobListOption.prefix("test-list-blobs-empty-selected-fields-blob"), Storage.BlobListOption.fields()); } Set blobSet = ImmutableSet.of(blobNames[0], blobNames[1]); - for (Blob remoteBlob : page.values()) { + Iterator iterator = page.iterateAll(); + while (iterator.hasNext()) { + Blob remoteBlob = iterator.next(); assertEquals(BUCKET, remoteBlob.bucket()); assertTrue(blobSet.contains(remoteBlob.name())); assertNull(remoteBlob.contentType()); @@ -362,7 +365,7 @@ public void testListBlobsEmptySelectedFields() throws InterruptedException { assertTrue(remoteBlob2.delete()); } - @Test(timeout = 10000) + @Test(timeout = 15000) public void testListBlobsVersioned() throws ExecutionException, InterruptedException { String bucketName = RemoteGcsHelper.generateBucketName(); Bucket bucket = storage.create(BucketInfo.builder(bucketName).versioningEnabled(true).build()); @@ -385,14 +388,16 @@ public void testListBlobsVersioned() throws ExecutionException, InterruptedExcep Storage.BlobListOption.versions(true)); // Listing blobs is eventually consistent, we loop until the list is of the expected size. The // test fails if timeout is reached. - while (Iterators.size(page.values().iterator()) != 3) { + while (Iterators.size(page.iterateAll()) != 3) { Thread.sleep(500); page = storage.list(bucketName, Storage.BlobListOption.prefix("test-list-blobs-versioned-blob"), Storage.BlobListOption.versions(true)); } Set blobSet = ImmutableSet.of(blobNames[0], blobNames[1]); - for (Blob remoteBlob : page.values()) { + Iterator iterator = page.iterateAll(); + while (iterator.hasNext()) { + Blob remoteBlob = iterator.next(); assertEquals(bucketName, remoteBlob.bucket()); assertTrue(blobSet.contains(remoteBlob.name())); assertNotNull(remoteBlob.generation()); From 8dd8786a5e9a38c7738889a2a6bca23848d3a3ee Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 4 Mar 2016 15:32:20 +0100 Subject: [PATCH 03/44] Remove redundant throws from services method signatures --- .../com/google/gcloud/bigquery/BigQuery.java | 53 ++++---- .../google/gcloud/bigquery/BigQueryImpl.java | 59 ++++----- .../com/google/gcloud/spi/BigQueryRpc.java | 87 +++++++++--- .../google/gcloud/spi/DefaultBigQueryRpc.java | 45 +++---- .../com/google/gcloud/spi/DatastoreRpc.java | 40 +++++- .../gcloud/spi/DefaultDatastoreRpc.java | 14 +- .../resourcemanager/ResourceManager.java | 2 +- .../gcloud/spi/DefaultResourceManagerRpc.java | 13 +- .../google/gcloud/spi/ResourceManagerRpc.java | 56 ++++++-- .../google/gcloud/spi/DefaultStorageRpc.java | 24 ++-- .../com/google/gcloud/spi/StorageRpc.java | 125 ++++++++++++++---- .../com/google/gcloud/storage/Storage.java | 44 +++--- 12 files changed, 364 insertions(+), 198 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 4b5d3ef0c81a..986c595e350d 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -457,35 +457,35 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * * @throws BigQueryException upon failure */ - Dataset create(DatasetInfo dataset, DatasetOption... options) throws BigQueryException; + Dataset create(DatasetInfo dataset, DatasetOption... options); /** * Creates a new table. * * @throws BigQueryException upon failure */ - Table create(TableInfo table, TableOption... options) throws BigQueryException; + Table create(TableInfo table, TableOption... options); /** * Creates a new job. * * @throws BigQueryException upon failure */ - Job create(JobInfo job, JobOption... options) throws BigQueryException; + Job create(JobInfo job, JobOption... options); /** * Returns the requested dataset or {@code null} if not found. * * @throws BigQueryException upon failure */ - Dataset getDataset(String datasetId, DatasetOption... options) throws BigQueryException; + Dataset getDataset(String datasetId, DatasetOption... options); /** * Returns the requested dataset or {@code null} if not found. * * @throws BigQueryException upon failure */ - Dataset getDataset(DatasetId datasetId, DatasetOption... options) throws BigQueryException; + Dataset getDataset(DatasetId datasetId, DatasetOption... options); /** * Lists the project's datasets. This method returns partial information on each dataset @@ -495,7 +495,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * * @throws BigQueryException upon failure */ - Page listDatasets(DatasetListOption... options) throws BigQueryException; + Page listDatasets(DatasetListOption... options); /** * Deletes the requested dataset. @@ -503,7 +503,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * @return {@code true} if dataset was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean delete(String datasetId, DatasetDeleteOption... options) throws BigQueryException; + boolean delete(String datasetId, DatasetDeleteOption... options); /** * Deletes the requested dataset. @@ -511,7 +511,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * @return {@code true} if dataset was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean delete(DatasetId datasetId, DatasetDeleteOption... options) throws BigQueryException; + boolean delete(DatasetId datasetId, DatasetDeleteOption... options); /** * Deletes the requested table. @@ -519,7 +519,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * @return {@code true} if table was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean delete(String datasetId, String tableId) throws BigQueryException; + boolean delete(String datasetId, String tableId); /** * Deletes the requested table. @@ -527,35 +527,35 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * @return {@code true} if table was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean delete(TableId tableId) throws BigQueryException; + boolean delete(TableId tableId); /** * Updates dataset information. * * @throws BigQueryException upon failure */ - Dataset update(DatasetInfo dataset, DatasetOption... options) throws BigQueryException; + Dataset update(DatasetInfo dataset, DatasetOption... options); /** * Updates table information. * * @throws BigQueryException upon failure */ - Table update(TableInfo table, TableOption... options) throws BigQueryException; + Table update(TableInfo table, TableOption... options); /** * Returns the requested table or {@code null} if not found. * * @throws BigQueryException upon failure */ - Table getTable(String datasetId, String tableId, TableOption... options) throws BigQueryException; + Table getTable(String datasetId, String tableId, TableOption... options); /** * Returns the requested table or {@code null} if not found. * * @throws BigQueryException upon failure */ - Table getTable(TableId tableId, TableOption... options) throws BigQueryException; + Table getTable(TableId tableId, TableOption... options); /** * Lists the tables in the dataset. This method returns partial information on each table @@ -566,7 +566,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * * @throws BigQueryException upon failure */ - Page listTables(String datasetId, TableListOption... options) throws BigQueryException; + Page
listTables(String datasetId, TableListOption... options); /** * Lists the tables in the dataset. This method returns partial information on each table @@ -577,14 +577,14 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * * @throws BigQueryException upon failure */ - Page
listTables(DatasetId datasetId, TableListOption... options) throws BigQueryException; + Page
listTables(DatasetId datasetId, TableListOption... options); /** * Sends an insert all request. * * @throws BigQueryException upon failure */ - InsertAllResponse insertAll(InsertAllRequest request) throws BigQueryException; + InsertAllResponse insertAll(InsertAllRequest request); /** * Lists the table's rows. @@ -592,36 +592,35 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * @throws BigQueryException upon failure */ Page> listTableData(String datasetId, String tableId, - TableDataListOption... options) throws BigQueryException; + TableDataListOption... options); /** * Lists the table's rows. * * @throws BigQueryException upon failure */ - Page> listTableData(TableId tableId, TableDataListOption... options) - throws BigQueryException; + Page> listTableData(TableId tableId, TableDataListOption... options); /** * Returns the requested job or {@code null} if not found. * * @throws BigQueryException upon failure */ - Job getJob(String jobId, JobOption... options) throws BigQueryException; + Job getJob(String jobId, JobOption... options); /** * Returns the requested job or {@code null} if not found. * * @throws BigQueryException upon failure */ - Job getJob(JobId jobId, JobOption... options) throws BigQueryException; + Job getJob(JobId jobId, JobOption... options); /** * Lists the jobs. * * @throws BigQueryException upon failure */ - Page listJobs(JobListOption... options) throws BigQueryException; + Page listJobs(JobListOption... options); /** * Sends a job cancel request. This call will return immediately. The job status can then be @@ -632,7 +631,7 @@ Page> listTableData(TableId tableId, TableDataListOption... opt * found * @throws BigQueryException upon failure */ - boolean cancel(String jobId) throws BigQueryException; + boolean cancel(String jobId); /** * Sends a job cancel request. This call will return immediately. The job status can then be @@ -643,21 +642,21 @@ Page> listTableData(TableId tableId, TableDataListOption... opt * found * @throws BigQueryException upon failure */ - boolean cancel(JobId tableId) throws BigQueryException; + boolean cancel(JobId tableId); /** * Runs the query associated with the request. * * @throws BigQueryException upon failure */ - QueryResponse query(QueryRequest request) throws BigQueryException; + QueryResponse query(QueryRequest request); /** * Returns results of the query associated with the provided job. * * @throws BigQueryException upon failure */ - QueryResponse getQueryResults(JobId job, QueryResultsOption... options) throws BigQueryException; + QueryResponse getQueryResults(JobId job, QueryResultsOption... options); /** * Returns a channel to write data to be inserted into a BigQuery table. Data format and other diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java index 1158dd86c83d..ce881c6ea079 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java @@ -153,7 +153,7 @@ public QueryResult nextPage() { } @Override - public Dataset create(DatasetInfo dataset, DatasetOption... options) throws BigQueryException { + public Dataset create(DatasetInfo dataset, DatasetOption... options) { final com.google.api.services.bigquery.model.Dataset datasetPb = dataset.setProjectId(options().projectId()).toPb(); final Map optionsMap = optionMap(options); @@ -171,7 +171,7 @@ public com.google.api.services.bigquery.model.Dataset call() { } @Override - public Table create(TableInfo table, TableOption... options) throws BigQueryException { + public Table create(TableInfo table, TableOption... options) { final com.google.api.services.bigquery.model.Table tablePb = table.setProjectId(options().projectId()).toPb(); final Map optionsMap = optionMap(options); @@ -189,7 +189,7 @@ public com.google.api.services.bigquery.model.Table call() { } @Override - public Job create(JobInfo job, JobOption... options) throws BigQueryException { + public Job create(JobInfo job, JobOption... options) { final com.google.api.services.bigquery.model.Job jobPb = job.setProjectId(options().projectId()).toPb(); final Map optionsMap = optionMap(options); @@ -207,13 +207,12 @@ public com.google.api.services.bigquery.model.Job call() { } @Override - public Dataset getDataset(String datasetId, DatasetOption... options) throws BigQueryException { + public Dataset getDataset(String datasetId, DatasetOption... options) { return getDataset(DatasetId.of(datasetId), options); } @Override - public Dataset getDataset(final DatasetId datasetId, DatasetOption... options) - throws BigQueryException { + public Dataset getDataset(final DatasetId datasetId, DatasetOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Dataset answer = @@ -230,7 +229,7 @@ public com.google.api.services.bigquery.model.Dataset call() { } @Override - public Page listDatasets(DatasetListOption... options) throws BigQueryException { + public Page listDatasets(DatasetListOption... options) { return listDatasets(options(), optionMap(options)); } @@ -261,13 +260,12 @@ public Dataset apply(com.google.api.services.bigquery.model.Dataset dataset) { } @Override - public boolean delete(String datasetId, DatasetDeleteOption... options) throws BigQueryException { + public boolean delete(String datasetId, DatasetDeleteOption... options) { return delete(DatasetId.of(datasetId), options); } @Override - public boolean delete(final DatasetId datasetId, DatasetDeleteOption... options) - throws BigQueryException { + public boolean delete(final DatasetId datasetId, DatasetDeleteOption... options) { final Map optionsMap = optionMap(options); try { return runWithRetries(new Callable() { @@ -282,12 +280,12 @@ public Boolean call() { } @Override - public boolean delete(String datasetId, String tableId) throws BigQueryException { + public boolean delete(String datasetId, String tableId) { return delete(TableId.of(datasetId, tableId)); } @Override - public boolean delete(final TableId tableId) throws BigQueryException { + public boolean delete(final TableId tableId) { try { return runWithRetries(new Callable() { @Override @@ -301,7 +299,7 @@ public Boolean call() { } @Override - public Dataset update(DatasetInfo dataset, DatasetOption... options) throws BigQueryException { + public Dataset update(DatasetInfo dataset, DatasetOption... options) { final com.google.api.services.bigquery.model.Dataset datasetPb = dataset.setProjectId(options().projectId()).toPb(); final Map optionsMap = optionMap(options); @@ -319,7 +317,7 @@ public com.google.api.services.bigquery.model.Dataset call() { } @Override - public Table update(TableInfo table, TableOption... options) throws BigQueryException { + public Table update(TableInfo table, TableOption... options) { final com.google.api.services.bigquery.model.Table tablePb = table.setProjectId(options().projectId()).toPb(); final Map optionsMap = optionMap(options); @@ -337,13 +335,12 @@ public com.google.api.services.bigquery.model.Table call() { } @Override - public Table getTable(final String datasetId, final String tableId, TableOption... options) - throws BigQueryException { + public Table getTable(final String datasetId, final String tableId, TableOption... options) { return getTable(TableId.of(datasetId, tableId), options); } @Override - public Table getTable(final TableId tableId, TableOption... options) throws BigQueryException { + public Table getTable(final TableId tableId, TableOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Table answer = @@ -360,14 +357,12 @@ public com.google.api.services.bigquery.model.Table call() { } @Override - public Page
listTables(String datasetId, TableListOption... options) - throws BigQueryException { + public Page
listTables(String datasetId, TableListOption... options) { return listTables(datasetId, options(), optionMap(options)); } @Override - public Page
listTables(DatasetId datasetId, TableListOption... options) - throws BigQueryException { + public Page
listTables(DatasetId datasetId, TableListOption... options) { return listTables(datasetId.dataset(), options(), optionMap(options)); } @@ -399,7 +394,7 @@ public Table apply(com.google.api.services.bigquery.model.Table table) { } @Override - public InsertAllResponse insertAll(InsertAllRequest request) throws BigQueryException { + public InsertAllResponse insertAll(InsertAllRequest request) { final TableId tableId = request.table(); final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest(); requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues()); @@ -418,13 +413,12 @@ public Rows apply(RowToInsert rowToInsert) { @Override public Page> listTableData(String datasetId, String tableId, - TableDataListOption... options) throws BigQueryException { + TableDataListOption... options) { return listTableData(TableId.of(datasetId, tableId), options(), optionMap(options)); } @Override - public Page> listTableData(TableId tableId, TableDataListOption... options) - throws BigQueryException { + public Page> listTableData(TableId tableId, TableDataListOption... options) { return listTableData(tableId, options(), optionMap(options)); } @@ -459,12 +453,12 @@ public List apply(TableRow rowPb) { } @Override - public Job getJob(String jobId, JobOption... options) throws BigQueryException { + public Job getJob(String jobId, JobOption... options) { return getJob(JobId.of(jobId), options); } @Override - public Job getJob(final JobId jobId, JobOption... options) throws BigQueryException { + public Job getJob(final JobId jobId, JobOption... options) { final Map optionsMap = optionMap(options); try { com.google.api.services.bigquery.model.Job answer = @@ -481,7 +475,7 @@ public com.google.api.services.bigquery.model.Job call() { } @Override - public Page listJobs(JobListOption... options) throws BigQueryException { + public Page listJobs(JobListOption... options) { return listJobs(options(), optionMap(options)); } @@ -508,12 +502,12 @@ public Job apply(com.google.api.services.bigquery.model.Job job) { } @Override - public boolean cancel(String jobId) throws BigQueryException { + public boolean cancel(String jobId) { return cancel(JobId.of(jobId)); } @Override - public boolean cancel(final JobId jobId) throws BigQueryException { + public boolean cancel(final JobId jobId) { try { return runWithRetries(new Callable() { @Override @@ -527,7 +521,7 @@ public Boolean call() { } @Override - public QueryResponse query(final QueryRequest request) throws BigQueryException { + public QueryResponse query(final QueryRequest request) { try { com.google.api.services.bigquery.model.QueryResponse results = runWithRetries(new Callable() { @@ -566,8 +560,7 @@ public com.google.api.services.bigquery.model.QueryResponse call() { } @Override - public QueryResponse getQueryResults(JobId job, QueryResultsOption... options) - throws BigQueryException { + public QueryResponse getQueryResults(JobId job, QueryResultsOption... options) { Map optionsMap = optionMap(options); return getQueryResults(job, options(), optionsMap); } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java index 6062e19950e0..a1935e5ab136 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java @@ -100,7 +100,7 @@ public Y y() { * * @throws BigQueryException upon failure */ - Dataset getDataset(String datasetId, Map options) throws BigQueryException; + Dataset getDataset(String datasetId, Map options); /** * Lists the project's datasets. Partial information is returned on a dataset (datasetReference, @@ -108,13 +108,28 @@ public Y y() { * * @throws BigQueryException upon failure */ - Tuple> listDatasets(Map options) throws BigQueryException; + Tuple> listDatasets(Map options); - Dataset create(Dataset dataset, Map options) throws BigQueryException; + /** + * Creates a new dataset. + * + * @throws BigQueryException upon failure + */ + Dataset create(Dataset dataset, Map options); - Table create(Table table, Map options) throws BigQueryException; + /** + * Creates a new table. + * + * @throws BigQueryException upon failure + */ + Table create(Table table, Map options); - Job create(Job job, Map options) throws BigQueryException; + /** + * Creates a new job. + * + * @throws BigQueryException upon failure + */ + Job create(Job job, Map options); /** * Delete the requested dataset. @@ -122,18 +137,28 @@ public Y y() { * @return {@code true} if dataset was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean deleteDataset(String datasetId, Map options) throws BigQueryException; + boolean deleteDataset(String datasetId, Map options); - Dataset patch(Dataset dataset, Map options) throws BigQueryException; + /** + * Updates dataset information. + * + * @throws BigQueryException upon failure + */ + Dataset patch(Dataset dataset, Map options); - Table patch(Table table, Map options) throws BigQueryException; + /** + * Updates table information. + * + * @throws BigQueryException upon failure + */ + Table patch(Table table, Map options); /** * Returns the requested table or {@code null} if not found. * * @throws BigQueryException upon failure */ - Table getTable(String datasetId, String tableId, Map options) throws BigQueryException; + Table getTable(String datasetId, String tableId, Map options); /** * Lists the dataset's tables. Partial information is returned on a table (tableReference, @@ -141,8 +166,7 @@ public Y y() { * * @throws BigQueryException upon failure */ - Tuple> listTables(String dataset, Map options) - throws BigQueryException; + Tuple> listTables(String dataset, Map options); /** * Delete the requested table. @@ -150,27 +174,37 @@ Tuple> listTables(String dataset, Map options * @return {@code true} if table was deleted, {@code false} if it was not found * @throws BigQueryException upon failure */ - boolean deleteTable(String datasetId, String tableId) throws BigQueryException; + boolean deleteTable(String datasetId, String tableId); + /** + * Sends an insert all request. + * + * @throws BigQueryException upon failure + */ TableDataInsertAllResponse insertAll(String datasetId, String tableId, - TableDataInsertAllRequest request) throws BigQueryException; + TableDataInsertAllRequest request); + /** + * Lists the table's rows. + * + * @throws BigQueryException upon failure + */ Tuple> listTableData(String datasetId, String tableId, - Map options) throws BigQueryException; + Map options); /** * Returns the requested job or {@code null} if not found. * * @throws BigQueryException upon failure */ - Job getJob(String jobId, Map options) throws BigQueryException; + Job getJob(String jobId, Map options); /** * Lists the project's jobs. * * @throws BigQueryException upon failure */ - Tuple> listJobs(Map options) throws BigQueryException; + Tuple> listJobs(Map options); /** * Sends a job cancel request. This call will return immediately, and the client will need to poll @@ -180,12 +214,21 @@ Tuple> listTableData(String datasetId, String tableId * found * @throws BigQueryException upon failure */ - boolean cancel(String jobId) throws BigQueryException; + boolean cancel(String jobId); - GetQueryResultsResponse getQueryResults(String jobId, Map options) - throws BigQueryException; + /** + * Returns results of the query associated with the provided job. + * + * @throws BigQueryException upon failure + */ + GetQueryResultsResponse getQueryResults(String jobId, Map options); - QueryResponse query(QueryRequest request) throws BigQueryException; + /** + * Runs the query associated with the request. + * + * @throws BigQueryException upon failure + */ + QueryResponse query(QueryRequest request); /** * Opens a resumable upload session to load data into a BigQuery table and returns an upload URI. @@ -193,7 +236,7 @@ GetQueryResultsResponse getQueryResults(String jobId, Map options) * @param configuration load configuration * @throws BigQueryException upon failure */ - String open(JobConfiguration configuration) throws BigQueryException; + String open(JobConfiguration configuration); /** * Uploads the provided data to the resumable upload session at the specified position. @@ -207,5 +250,5 @@ GetQueryResultsResponse getQueryResults(String jobId, Map options) * @throws BigQueryException upon failure */ void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, - boolean last) throws BigQueryException; + boolean last); } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java index b57f1dc8a128..a5c44129955a 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java @@ -90,7 +90,7 @@ private static BigQueryException translate(IOException exception) { } @Override - public Dataset getDataset(String datasetId, Map options) throws BigQueryException { + public Dataset getDataset(String datasetId, Map options) { try { return bigquery.datasets() .get(this.options.projectId(), datasetId) @@ -106,8 +106,7 @@ public Dataset getDataset(String datasetId, Map options) throws BigQu } @Override - public Tuple> listDatasets(Map options) - throws BigQueryException { + public Tuple> listDatasets(Map options) { try { DatasetList datasetsList = bigquery.datasets() .list(this.options.projectId()) @@ -135,7 +134,7 @@ public Dataset apply(DatasetList.Datasets datasetPb) { } @Override - public Dataset create(Dataset dataset, Map options) throws BigQueryException { + public Dataset create(Dataset dataset, Map options) { try { return bigquery.datasets().insert(this.options.projectId(), dataset) .setFields(FIELDS.getString(options)) @@ -146,8 +145,7 @@ public Dataset create(Dataset dataset, Map options) throws BigQueryEx } @Override - public Table create(Table table, Map options) - throws BigQueryException { + public Table create(Table table, Map options) { try { // unset the type, as it is output only table.setType(null); @@ -161,7 +159,7 @@ public Table create(Table table, Map options) } @Override - public Job create(Job job, Map options) throws BigQueryException { + public Job create(Job job, Map options) { try { return bigquery.jobs() .insert(this.options.projectId(), job) @@ -173,7 +171,7 @@ public Job create(Job job, Map options) throws BigQueryException { } @Override - public boolean deleteDataset(String datasetId, Map options) throws BigQueryException { + public boolean deleteDataset(String datasetId, Map options) { try { bigquery.datasets().delete(this.options.projectId(), datasetId) .setDeleteContents(DELETE_CONTENTS.getBoolean(options)) @@ -189,7 +187,7 @@ public boolean deleteDataset(String datasetId, Map options) throws Bi } @Override - public Dataset patch(Dataset dataset, Map options) throws BigQueryException { + public Dataset patch(Dataset dataset, Map options) { try { DatasetReference reference = dataset.getDatasetReference(); return bigquery.datasets() @@ -202,7 +200,7 @@ public Dataset patch(Dataset dataset, Map options) throws BigQueryExc } @Override - public Table patch(Table table, Map options) throws BigQueryException { + public Table patch(Table table, Map options) { try { // unset the type, as it is output only table.setType(null); @@ -217,8 +215,7 @@ public Table patch(Table table, Map options) throws BigQueryException } @Override - public Table getTable(String datasetId, String tableId, Map options) - throws BigQueryException { + public Table getTable(String datasetId, String tableId, Map options) { try { return bigquery.tables() .get(this.options.projectId(), datasetId, tableId) @@ -234,8 +231,7 @@ public Table getTable(String datasetId, String tableId, Map options) } @Override - public Tuple> listTables(String datasetId, Map options) - throws BigQueryException { + public Tuple> listTables(String datasetId, Map options) { try { TableList tableList = bigquery.tables() .list(this.options.projectId(), datasetId) @@ -262,7 +258,7 @@ public Table apply(TableList.Tables tablePb) { } @Override - public boolean deleteTable(String datasetId, String tableId) throws BigQueryException { + public boolean deleteTable(String datasetId, String tableId) { try { bigquery.tables().delete(this.options.projectId(), datasetId, tableId).execute(); return true; @@ -277,7 +273,7 @@ public boolean deleteTable(String datasetId, String tableId) throws BigQueryExce @Override public TableDataInsertAllResponse insertAll(String datasetId, String tableId, - TableDataInsertAllRequest request) throws BigQueryException { + TableDataInsertAllRequest request) { try { return bigquery.tabledata() .insertAll(this.options.projectId(), datasetId, tableId, request) @@ -289,7 +285,7 @@ public TableDataInsertAllResponse insertAll(String datasetId, String tableId, @Override public Tuple> listTableData(String datasetId, String tableId, - Map options) throws BigQueryException { + Map options) { try { TableDataList tableDataList = bigquery.tabledata() .list(this.options.projectId(), datasetId, tableId) @@ -306,7 +302,7 @@ public Tuple> listTableData(String datasetId, String } @Override - public Job getJob(String jobId, Map options) throws BigQueryException { + public Job getJob(String jobId, Map options) { try { return bigquery.jobs() .get(this.options.projectId(), jobId) @@ -322,7 +318,7 @@ public Job getJob(String jobId, Map options) throws BigQueryException } @Override - public Tuple> listJobs(Map options) throws BigQueryException { + public Tuple> listJobs(Map options) { try { JobList jobsList = bigquery.jobs() .list(this.options.projectId()) @@ -363,7 +359,7 @@ public Job apply(JobList.Jobs jobPb) { } @Override - public boolean cancel(String jobId) throws BigQueryException { + public boolean cancel(String jobId) { try { bigquery.jobs().cancel(this.options.projectId(), jobId).execute(); return true; @@ -377,8 +373,7 @@ public boolean cancel(String jobId) throws BigQueryException { } @Override - public GetQueryResultsResponse getQueryResults(String jobId, Map options) - throws BigQueryException { + public GetQueryResultsResponse getQueryResults(String jobId, Map options) { try { return bigquery.jobs().getQueryResults(this.options.projectId(), jobId) .setMaxResults(MAX_RESULTS.getLong(options)) @@ -397,7 +392,7 @@ public GetQueryResultsResponse getQueryResults(String jobId, Map opti } @Override - public QueryResponse query(QueryRequest request) throws BigQueryException { + public QueryResponse query(QueryRequest request) { try { return bigquery.jobs().query(this.options.projectId(), request).execute(); } catch (IOException ex) { @@ -406,7 +401,7 @@ public QueryResponse query(QueryRequest request) throws BigQueryException { } @Override - public String open(JobConfiguration configuration) throws BigQueryException { + public String open(JobConfiguration configuration) { try { Job loadJob = new Job().setConfiguration(configuration); StringBuilder builder = new StringBuilder() @@ -429,7 +424,7 @@ public String open(JobConfiguration configuration) throws BigQueryException { @Override public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, - boolean last) throws BigQueryException { + boolean last) { try { GenericUrl url = new GenericUrl(uploadId); HttpRequest httpRequest = bigquery.getRequestFactory().buildPutRequest(url, diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java index fd916e0a1c87..4d4540c70196 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java @@ -35,16 +35,46 @@ */ public interface DatastoreRpc { - AllocateIdsResponse allocateIds(AllocateIdsRequest request) throws DatastoreException; + /** + * Sends an allocate IDs request. + * + * @throws DatastoreException upon failure + */ + AllocateIdsResponse allocateIds(AllocateIdsRequest request); + /** + * Sends a begin transaction request. + * + * @throws DatastoreException upon failure + */ BeginTransactionResponse beginTransaction(BeginTransactionRequest request) throws DatastoreException; - CommitResponse commit(CommitRequest request) throws DatastoreException; + /** + * Sends a commit request. + * + * @throws DatastoreException upon failure + */ + CommitResponse commit(CommitRequest request); - LookupResponse lookup(LookupRequest request) throws DatastoreException; + /** + * Sends a lookup request. + * + * @throws DatastoreException upon failure + */ + LookupResponse lookup(LookupRequest request); - RollbackResponse rollback(RollbackRequest request) throws DatastoreException; + /** + * Sends a rollback request. + * + * @throws DatastoreException upon failure + */ + RollbackResponse rollback(RollbackRequest request); - RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreException; + /** + * Sends a request to run a query. + * + * @throws DatastoreException upon failure + */ + RunQueryResponse runQuery(RunQueryRequest request); } diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index c82ff9689f68..f679cc0d5826 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -111,8 +111,7 @@ private static DatastoreException translate( } @Override - public AllocateIdsResponse allocateIds(AllocateIdsRequest request) - throws DatastoreException { + public AllocateIdsResponse allocateIds(AllocateIdsRequest request) { try { return client.allocateIds(request); } catch (com.google.api.services.datastore.client.DatastoreException ex) { @@ -121,8 +120,7 @@ public AllocateIdsResponse allocateIds(AllocateIdsRequest request) } @Override - public BeginTransactionResponse beginTransaction(BeginTransactionRequest request) - throws DatastoreException { + public BeginTransactionResponse beginTransaction(BeginTransactionRequest request) { try { return client.beginTransaction(request); } catch (com.google.api.services.datastore.client.DatastoreException ex) { @@ -131,7 +129,7 @@ public BeginTransactionResponse beginTransaction(BeginTransactionRequest request } @Override - public CommitResponse commit(CommitRequest request) throws DatastoreException { + public CommitResponse commit(CommitRequest request) { try { return client.commit(request); } catch (com.google.api.services.datastore.client.DatastoreException ex) { @@ -140,7 +138,7 @@ public CommitResponse commit(CommitRequest request) throws DatastoreException { } @Override - public LookupResponse lookup(LookupRequest request) throws DatastoreException { + public LookupResponse lookup(LookupRequest request) { try { return client.lookup(request); } catch (com.google.api.services.datastore.client.DatastoreException ex) { @@ -149,7 +147,7 @@ public LookupResponse lookup(LookupRequest request) throws DatastoreException { } @Override - public RollbackResponse rollback(RollbackRequest request) throws DatastoreException { + public RollbackResponse rollback(RollbackRequest request) { try { return client.rollback(request); } catch (com.google.api.services.datastore.client.DatastoreException ex) { @@ -158,7 +156,7 @@ public RollbackResponse rollback(RollbackRequest request) throws DatastoreExcept } @Override - public RunQueryResponse runQuery(RunQueryRequest request) throws DatastoreException { + public RunQueryResponse runQuery(RunQueryRequest request) { try { return client.runQuery(request); } catch (com.google.api.services.datastore.client.DatastoreException ex) { diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index 3d27f2a33ac8..f9ddf6872414 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -168,7 +168,7 @@ public static ProjectListOption fields(ProjectField... fields) { } /** - * Create a new project. + * Creates a new project. * *

Initially, the project resource is owned by its creator exclusively. The creator can later * grant permission to others to read or update the project. Several APIs are activated diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java index 61c622fa0c33..d30cd2df3627 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java @@ -38,7 +38,7 @@ private static ResourceManagerException translate(IOException exception) { } @Override - public Project create(Project project) throws ResourceManagerException { + public Project create(Project project) { try { return resourceManager.projects().create(project).execute(); } catch (IOException ex) { @@ -47,7 +47,7 @@ public Project create(Project project) throws ResourceManagerException { } @Override - public void delete(String projectId) throws ResourceManagerException { + public void delete(String projectId) { try { resourceManager.projects().delete(projectId).execute(); } catch (IOException ex) { @@ -56,7 +56,7 @@ public void delete(String projectId) throws ResourceManagerException { } @Override - public Project get(String projectId, Map options) throws ResourceManagerException { + public Project get(String projectId, Map options) { try { return resourceManager.projects() .get(projectId) @@ -74,8 +74,7 @@ public Project get(String projectId, Map options) throws ResourceMana } @Override - public Tuple> list(Map options) - throws ResourceManagerException { + public Tuple> list(Map options) { try { ListProjectsResponse response = resourceManager.projects() .list() @@ -92,7 +91,7 @@ public Tuple> list(Map options) } @Override - public void undelete(String projectId) throws ResourceManagerException { + public void undelete(String projectId) { try { resourceManager.projects().undelete(projectId).execute(); } catch (IOException ex) { @@ -101,7 +100,7 @@ public void undelete(String projectId) throws ResourceManagerException { } @Override - public Project replace(Project project) throws ResourceManagerException { + public Project replace(Project project) { try { return resourceManager.projects().update(project.getProjectId(), project).execute(); } catch (IOException ex) { diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java index 52dfc2d2368e..e1d72a6a373e 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java @@ -75,17 +75,51 @@ public Y y() { } } - Project create(Project project) throws ResourceManagerException; - - void delete(String projectId) throws ResourceManagerException; - - Project get(String projectId, Map options) throws ResourceManagerException; - - Tuple> list(Map options) throws ResourceManagerException; - - void undelete(String projectId) throws ResourceManagerException; - - Project replace(Project project) throws ResourceManagerException; + /** + * Creates a new project. + * + * @throws ResourceManagerException upon failure + */ + Project create(Project project); + + /** + * Marks the project identified by the specified project ID for deletion. + * + * @throws ResourceManagerException upon failure + */ + void delete(String projectId); + + /** + * Retrieves the project identified by the specified project ID. Returns {@code null} if the + * project is not found or if the user doesn't have read permissions for the project. + * + * @throws ResourceManagerException upon failure + */ + Project get(String projectId, Map options); + + /** + * Lists the projects visible to the current user. + * + * @throws ResourceManagerException upon failure + */ + Tuple> list(Map options); + + /** + * Restores the project identified by the specified project ID. Undelete will only succeed if the + * project has a lifecycle state of {@code DELETE_REQUESTED} state. The caller must have modify + * permissions for this project. + * + * @throws ResourceManagerException upon failure + */ + void undelete(String projectId); + + /** + * Replaces the attributes of the project. The caller must have modify permissions for this + * project. + * + * @throws ResourceManagerException upon failure + */ + Project replace(Project project); // TODO(ajaykannan): implement "Organization" functionality when available (issue #319) } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java index dc84a1de5559..3d7febfd556b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java @@ -99,7 +99,7 @@ private static StorageException translate(GoogleJsonError exception) { } @Override - public Bucket create(Bucket bucket, Map options) throws StorageException { + public Bucket create(Bucket bucket, Map options) { try { return storage.buckets() .insert(this.options.projectId(), bucket) @@ -114,7 +114,7 @@ public Bucket create(Bucket bucket, Map options) throws StorageExcept @Override public StorageObject create(StorageObject storageObject, final InputStream content, - Map options) throws StorageException { + Map options) { try { Storage.Objects.Insert insert = storage.objects() .insert(storageObject.getBucket(), storageObject, @@ -296,7 +296,7 @@ private Storage.Objects.Delete deleteRequest(StorageObject blob, Map @Override public StorageObject compose(Iterable sources, StorageObject target, - Map targetOptions) throws StorageException { + Map targetOptions) { ComposeRequest request = new ComposeRequest(); if (target.getContentType() == null) { target.setContentType("application/octet-stream"); @@ -327,8 +327,7 @@ public StorageObject compose(Iterable sources, StorageObject targ } @Override - public byte[] load(StorageObject from, Map options) - throws StorageException { + public byte[] load(StorageObject from, Map options) { try { Storage.Objects.Get getRequest = storage.objects() .get(from.getBucket(), from.getName()) @@ -347,7 +346,7 @@ public byte[] load(StorageObject from, Map options) } @Override - public BatchResponse batch(BatchRequest request) throws StorageException { + public BatchResponse batch(BatchRequest request) { List>>> partitionedToDelete = Lists.partition(request.toDelete, MAX_BATCH_DELETES); Iterator>>> iterator = partitionedToDelete.iterator(); @@ -437,7 +436,7 @@ public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) { @Override public Tuple read(StorageObject from, Map options, long position, - int bytes) throws StorageException { + int bytes) { try { Get req = storage.objects() .get(from.getBucket(), from.getName()) @@ -460,7 +459,7 @@ public Tuple read(StorageObject from, Map options, lo @Override public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, - boolean last) throws StorageException { + boolean last) { try { GenericUrl url = new GenericUrl(uploadId); HttpRequest httpRequest = storage.getRequestFactory().buildPutRequest(url, @@ -501,8 +500,7 @@ public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destO } @Override - public String open(StorageObject object, Map options) - throws StorageException { + public String open(StorageObject object, Map options) { try { Insert req = storage.objects().insert(object.getBucket(), object); GenericUrl url = req.buildHttpRequest().getUrl(); @@ -538,16 +536,16 @@ public String open(StorageObject object, Map options) } @Override - public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException { + public RewriteResponse openRewrite(RewriteRequest rewriteRequest) { return rewrite(rewriteRequest, null); } @Override - public RewriteResponse continueRewrite(RewriteResponse previousResponse) throws StorageException { + public RewriteResponse continueRewrite(RewriteResponse previousResponse) { return rewrite(previousResponse.rewriteRequest, previousResponse.rewriteToken); } - private RewriteResponse rewrite(RewriteRequest req, String token) throws StorageException { + private RewriteResponse rewrite(RewriteRequest req, String token) { try { Long maxBytesRewrittenPerCall = req.megabytesRewrittenPerCall != null ? req.megabytesRewrittenPerCall * MEGABYTE : null; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java index e15a27114810..c76fc0f2d3b8 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java @@ -217,57 +217,134 @@ public int hashCode() { } } - Bucket create(Bucket bucket, Map options) throws StorageException; + /** + * Creates a new bucket. + * + * @throws StorageException upon failure + */ + Bucket create(Bucket bucket, Map options); - StorageObject create(StorageObject object, InputStream content, Map options) - throws StorageException; + /** + * Creates a new storage object. + * + * @throws StorageException upon failure + */ + StorageObject create(StorageObject object, InputStream content, Map options); - Tuple> list(Map options) throws StorageException; + /** + * Lists the project's buckets. + * + * @throws StorageException upon failure + */ + Tuple> list(Map options); - Tuple> list(String bucket, Map options) - throws StorageException; + /** + * Lists the bucket's blobs. + * + * @throws StorageException upon failure + */ + Tuple> list(String bucket, Map options); /** * Returns the requested bucket or {@code null} if not found. * * @throws StorageException upon failure */ - Bucket get(Bucket bucket, Map options) throws StorageException; + Bucket get(Bucket bucket, Map options); /** * Returns the requested storage object or {@code null} if not found. * * @throws StorageException upon failure */ - StorageObject get(StorageObject object, Map options) - throws StorageException; + StorageObject get(StorageObject object, Map options); - Bucket patch(Bucket bucket, Map options) throws StorageException; + /** + * Updates bucket information. + * + * @throws StorageException upon failure + */ + Bucket patch(Bucket bucket, Map options); - StorageObject patch(StorageObject storageObject, Map options) - throws StorageException; + /** + * Updates the storage object's information. Original metadata are merged with metadata in the + * provided {@code storageObject}. + * + * @throws StorageException upon failure + */ + StorageObject patch(StorageObject storageObject, Map options); - boolean delete(Bucket bucket, Map options) throws StorageException; + /** + * Deletes the requested bucket. + * + * @return {@code true} if the bucket was deleted, {@code false} if it was not found + * @throws StorageException upon failure + */ + boolean delete(Bucket bucket, Map options); - boolean delete(StorageObject object, Map options) throws StorageException; + /** + * Deletes the requested storage object. + * + * @return {@code true} if the storage object was deleted, {@code false} if it was not found + * @throws StorageException upon failure + */ + boolean delete(StorageObject object, Map options); - BatchResponse batch(BatchRequest request) throws StorageException; + /** + * Sends a batch request. + * + * @throws StorageException upon failure + */ + BatchResponse batch(BatchRequest request); + /** + * Sends a compose request. + * + * @throws StorageException upon failure + */ StorageObject compose(Iterable sources, StorageObject target, - Map targetOptions) throws StorageException; + Map targetOptions); - byte[] load(StorageObject storageObject, Map options) - throws StorageException; + /** + * Reads all the bytes from a storage object. + * + * @throws StorageException upon failure + */ + byte[] load(StorageObject storageObject, Map options); - Tuple read(StorageObject from, Map options, long position, int bytes) - throws StorageException; + /** + * Reads the given amount of bytes from a storage object at the given position. + * + * @throws StorageException upon failure + */ + Tuple read(StorageObject from, Map options, long position, int bytes); - String open(StorageObject object, Map options) throws StorageException; + /** + * Opens a resumable upload channel for a given storage object. + * + * @throws StorageException upon failure + */ + String open(StorageObject object, Map options); + /** + * Writes the provided bytes to a storage object at the provided location. + * + * @throws StorageException upon failure + */ void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, - boolean last) throws StorageException; + boolean last); - RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException; + /** + * Sends a rewrite request to open a rewrite channel. + * + * @throws StorageException upon failure + */ + RewriteResponse openRewrite(RewriteRequest rewriteRequest); - RewriteResponse continueRewrite(RewriteResponse previousResponse) throws StorageException; + /** + * Continues rewriting on an already open rewrite channel. + * + * @throws StorageException + */ + RewriteResponse continueRewrite(RewriteResponse previousResponse); } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 98f3450b7f10..065bcca7c9e8 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -1215,7 +1215,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx } /** - * Create a new bucket. + * Creates a new bucket. * * @return a complete bucket * @throws StorageException upon failure @@ -1223,7 +1223,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Bucket create(BucketInfo bucketInfo, BucketTargetOption... options); /** - * Create a new blob with no content. + * Creates a new blob with no content. * * @return a [@code Blob} with complete information * @throws StorageException upon failure @@ -1231,7 +1231,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Blob create(BlobInfo blobInfo, BlobTargetOption... options); /** - * Create a new blob. Direct upload is used to upload {@code content}. For large content, + * Creates a new blob. Direct upload is used to upload {@code content}. For large content, * {@link #writer} is recommended as it uses resumable upload. MD5 and CRC32C hashes of * {@code content} are computed and used for validating transferred data. * @@ -1242,7 +1242,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Blob create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options); /** - * Create a new blob. Direct upload is used to upload {@code content}. For large content, + * Creates a new blob. Direct upload is used to upload {@code content}. For large content, * {@link #writer} is recommended as it uses resumable upload. By default any md5 and crc32c * values in the given {@code blobInfo} are ignored unless requested via the * {@code BlobWriteOption.md5Match} and {@code BlobWriteOption.crc32cMatch} options. The given @@ -1254,49 +1254,49 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Blob create(BlobInfo blobInfo, InputStream content, BlobWriteOption... options); /** - * Return the requested bucket or {@code null} if not found. + * Returns the requested bucket or {@code null} if not found. * * @throws StorageException upon failure */ Bucket get(String bucket, BucketGetOption... options); /** - * Return the requested blob or {@code null} if not found. + * Returns the requested blob or {@code null} if not found. * * @throws StorageException upon failure */ Blob get(String bucket, String blob, BlobGetOption... options); /** - * Return the requested blob or {@code null} if not found. + * Returns the requested blob or {@code null} if not found. * * @throws StorageException upon failure */ Blob get(BlobId blob, BlobGetOption... options); /** - * Return the requested blob or {@code null} if not found. + * Returns the requested blob or {@code null} if not found. * * @throws StorageException upon failure */ Blob get(BlobId blob); /** - * List the project's buckets. + * Lists the project's buckets. * * @throws StorageException upon failure */ Page list(BucketListOption... options); /** - * List the bucket's blobs. + * Lists the bucket's blobs. * * @throws StorageException upon failure */ Page list(String bucket, BlobListOption... options); /** - * Update bucket information. + * Updates bucket information. * * @return the updated bucket * @throws StorageException upon failure @@ -1304,7 +1304,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Bucket update(BucketInfo bucketInfo, BucketTargetOption... options); /** - * Update blob information. Original metadata are merged with metadata in the provided + * Updates blob information. Original metadata are merged with metadata in the provided * {@code blobInfo}. To replace metadata instead you first have to unset them. Unsetting metadata * can be done by setting the provided {@code blobInfo}'s metadata to {@code null}. * @@ -1321,7 +1321,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Blob update(BlobInfo blobInfo, BlobTargetOption... options); /** - * Update blob information. Original metadata are merged with metadata in the provided + * Updates blob information. Original metadata are merged with metadata in the provided * {@code blobInfo}. To replace metadata instead you first have to unset them. Unsetting metadata * can be done by setting the provided {@code blobInfo}'s metadata to {@code null}. * @@ -1338,7 +1338,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Blob update(BlobInfo blobInfo); /** - * Delete the requested bucket. + * Deletes the requested bucket. * * @return {@code true} if bucket was deleted, {@code false} if it was not found * @throws StorageException upon failure @@ -1346,7 +1346,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx boolean delete(String bucket, BucketSourceOption... options); /** - * Delete the requested blob. + * Deletes the requested blob. * * @return {@code true} if blob was deleted, {@code false} if it was not found * @throws StorageException upon failure @@ -1354,7 +1354,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx boolean delete(String bucket, String blob, BlobSourceOption... options); /** - * Delete the requested blob. + * Deletes the requested blob. * * @return {@code true} if blob was deleted, {@code false} if it was not found * @throws StorageException upon failure @@ -1362,7 +1362,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx boolean delete(BlobId blob, BlobSourceOption... options); /** - * Delete the requested blob. + * Deletes the requested blob. * * @return {@code true} if blob was deleted, {@code false} if it was not found * @throws StorageException upon failure @@ -1370,7 +1370,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx boolean delete(BlobId blob); /** - * Send a compose request. + * Sends a compose request. * * @return the composed blob * @throws StorageException upon failure @@ -1422,7 +1422,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx byte[] readAllBytes(BlobId blob, BlobSourceOption... options); /** - * Send a batch request. + * Sends a batch request. * * @return the batch response * @throws StorageException upon failure @@ -1430,7 +1430,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx BatchResponse submit(BatchRequest batchRequest); /** - * Return a channel for reading the blob's content. The blob's latest generation is read. If the + * Returns a channel for reading the blob's content. The blob's latest generation is read. If the * blob changes while reading (i.e. {@link BlobInfo#etag()} changes), subsequent calls to * {@code blobReadChannel.read(ByteBuffer)} may throw {@link StorageException}. * @@ -1443,7 +1443,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx ReadChannel reader(String bucket, String blob, BlobSourceOption... options); /** - * Return a channel for reading the blob's content. If {@code blob.generation()} is set + * Returns a channel for reading the blob's content. If {@code blob.generation()} is set * data corresponding to that generation is read. If {@code blob.generation()} is {@code null} * the blob's latest generation is read. If the blob changes while reading (i.e. * {@link BlobInfo#etag()} changes), subsequent calls to {@code blobReadChannel.read(ByteBuffer)} @@ -1459,7 +1459,7 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx ReadChannel reader(BlobId blob, BlobSourceOption... options); /** - * Create a blob and return a channel for writing its content. By default any md5 and crc32c + * Creates a blob and return a channel for writing its content. By default any md5 and crc32c * values in the given {@code blobInfo} are ignored unless requested via the * {@code BlobWriteOption.md5Match} and {@code BlobWriteOption.crc32cMatch} options. * From c486452b4c81da7f077e2573b1f29dee3994bd86 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Sun, 6 Mar 2016 08:54:43 -0800 Subject: [PATCH 04/44] Add NoAuthCredentials --- .../com/google/gcloud/AuthCredentials.java | 34 +++++++++++++++++++ .../com/google/gcloud/ServiceOptions.java | 7 ++-- .../gcloud/datastore/DatastoreTest.java | 2 ++ .../testing/LocalResourceManagerHelper.java | 11 ++++-- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index fc5d74d0896c..f4f6b6938531 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -243,6 +243,33 @@ public RestorableState capture() { } } + public static class NoAuthCredentials extends AuthCredentials { + + private static final AuthCredentials INSTANCE = new NoAuthCredentials(); + private static final NoAuthCredentialsState STATE = new NoAuthCredentialsState(); + + private static class NoAuthCredentialsState + implements RestorableState, Serializable { + + private static final long serialVersionUID = -4022100563954640465L; + + @Override + public AuthCredentials restore() { + return INSTANCE; + } + } + + @Override + public GoogleCredentials credentials() { + return null; + } + + @Override + public RestorableState capture() { + return STATE; + } + } + public abstract GoogleCredentials credentials(); public static AuthCredentials createForAppEngine() { @@ -281,6 +308,13 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey return new ServiceAccountAuthCredentials(account, privateKey); } + /** + * Creates a placeholder denoting that no credentials should be used. + */ + public static AuthCredentials noAuth() { + return NoAuthCredentials.INSTANCE; + } + /** * Creates Service Account Credentials given a stream for credentials in JSON format. * diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 31e543809464..d45069434a26 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -523,9 +523,10 @@ public RetryParams retryParams() { * options. */ public HttpRequestInitializer httpRequestInitializer() { - final HttpRequestInitializer delegate = authCredentials() != null - ? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes())) - : null; + final HttpRequestInitializer delegate = + authCredentials() != null && authCredentials.credentials() != null + ? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes())) + : null; return new HttpRequestInitializer() { @Override public void initialize(HttpRequest httpRequest) throws IOException { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index a289610fe841..5d106fc7d31d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -32,6 +32,7 @@ import com.google.api.services.datastore.DatastoreV1.RunQueryResponse; import com.google.common.collect.Iterators; import com.google.common.collect.Lists; +import com.google.gcloud.AuthCredentials; import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.Query.ResultType; import com.google.gcloud.datastore.StructuredQuery.OrderBy; @@ -128,6 +129,7 @@ public void setUp() { options = DatastoreOptions.builder() .projectId(PROJECT_ID) .host("http://localhost:" + PORT) + .authCredentials(AuthCredentials.noAuth()) .retryParams(RetryParams.noRetries()) .build(); datastore = options.service(); diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java index a077eb6144a5..cda2dd1e00ea 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java @@ -12,6 +12,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteStreams; +import com.google.gcloud.AuthCredentials; import com.google.gcloud.resourcemanager.ResourceManagerOptions; import com.sun.net.httpserver.Headers; @@ -550,17 +551,21 @@ private LocalResourceManagerHelper() { } /** - * Creates a LocalResourceManagerHelper object that listens to requests on the local machine. + * Creates a {@code LocalResourceManagerHelper} object that listens to requests on the local + * machine. */ public static LocalResourceManagerHelper create() { return new LocalResourceManagerHelper(); } /** - * Returns a ResourceManagerOptions instance that sets the host to use the mock server. + * Returns a {@link ResourceManagerOptions} instance that sets the host to use the mock server. */ public ResourceManagerOptions options() { - return ResourceManagerOptions.builder().host("http://localhost:" + port).build(); + return ResourceManagerOptions.builder() + .host("http://localhost:" + port) + .authCredentials(AuthCredentials.noAuth()) + .build(); } /** From 8c353ecbc62a9f8d7784ba8f99ee2f3cf15a4c00 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 7 Mar 2016 08:27:49 -0800 Subject: [PATCH 05/44] Add javadoc --- .../com/google/gcloud/AuthCredentials.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index f4f6b6938531..1f8a56f8e68c 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -132,6 +132,12 @@ public RestorableState capture() { } } + /** + * Represents service account credentials. + * + * @see + * User accounts and service accounts + */ public static class ServiceAccountAuthCredentials extends AuthCredentials { private final String account; @@ -195,6 +201,14 @@ public RestorableState capture() { } } + /** + * Represents Application Default Credentials, which are credentials that are inferred from the + * runtime environment. + * + * @see + * Google Application Default Credentials + */ public static class ApplicationDefaultAuthCredentials extends AuthCredentials { private GoogleCredentials googleCredentials; @@ -243,6 +257,11 @@ public RestorableState capture() { } } + /** + * Represents that requests sent to the server should not be authenticated. This is typically + * useful when using the local service emulators, such as {@code LocalGcdHelper} and + * {@code LocalResourceManagerHelper}. + */ public static class NoAuthCredentials extends AuthCredentials { private static final AuthCredentials INSTANCE = new NoAuthCredentials(); @@ -309,7 +328,9 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey } /** - * Creates a placeholder denoting that no credentials should be used. + * Creates a placeholder denoting that no credentials should be used. This is typically useful + * when using the local service emulators, such as {@code LocalGcdHelper} and + * {@code LocalResourceManagerHelper}. */ public static AuthCredentials noAuth() { return NoAuthCredentials.INSTANCE; From 0c4af89295be8709d63776ee4c65d4d64fa282ce Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 7 Mar 2016 08:50:26 -0800 Subject: [PATCH 06/44] change NoAuthCredentials javadoc wording --- .../src/main/java/com/google/gcloud/AuthCredentials.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index 1f8a56f8e68c..6f9e09ca04bc 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -258,9 +258,9 @@ public RestorableState capture() { } /** - * Represents that requests sent to the server should not be authenticated. This is typically - * useful when using the local service emulators, such as {@code LocalGcdHelper} and - * {@code LocalResourceManagerHelper}. + * A placeholder for credentials to signify that requests sent to the server should not be + * authenticated. This is typically useful when using the local service emulators, such as + * {@code LocalGcdHelper} and {@code LocalResourceManagerHelper}. */ public static class NoAuthCredentials extends AuthCredentials { From cded23436465bb5fb1233eb5cd6740b4561357f9 Mon Sep 17 00:00:00 2001 From: aozarov Date: Mon, 7 Mar 2016 15:28:21 -0800 Subject: [PATCH 07/44] Fix writes with 0 length --- .../google/gcloud/spi/DefaultStorageRpc.java | 10 +++++++- .../gcloud/storage/it/ITStorageTest.java | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java index 3d7febfd556b..4d9214ef5929 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java @@ -461,12 +461,20 @@ public Tuple read(StorageObject from, Map options, lo public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) { try { + if (length == 0 && !last) { + return; + } GenericUrl url = new GenericUrl(uploadId); HttpRequest httpRequest = storage.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length)); long limit = destOffset + length; StringBuilder range = new StringBuilder("bytes "); - range.append(destOffset).append('-').append(limit - 1).append('/'); + if (length == 0) { + range.append('*'); + } else { + range.append(destOffset).append('-').append(limit - 1); + } + range.append('/'); if (last) { range.append(limit); } else { diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java index 38521ae5e137..a411cdd60bfb 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java @@ -823,6 +823,29 @@ public void testReadAndWriteChannels() throws IOException { assertTrue(storage.delete(BUCKET, blobName)); } + @Test + public void testReadAndWriteChannelsWithDifferentFileSize() throws IOException { + String blobNamePrefix = "test-read-and-write-channels-blob-"; + int[] blobSizes = {0, 700, 1024 * 256, 2 * 1024 * 1024, 4 * 1024 * 1024, 4 * 1024 * 1024 + 1}; + Random rnd = new Random(); + for (int blobSize : blobSizes) { + String blobName = blobNamePrefix + blobSize; + BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build(); + byte[] bytes = new byte[blobSize]; + rnd.nextBytes(bytes); + try (WriteChannel writer = storage.writer(blob)) { + writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT)); + } + ByteBuffer readBytes; + try (ReadChannel reader = storage.reader(blob.blobId())) { + readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length); + reader.read(readBytes); + } + assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array()); + assertTrue(storage.delete(BUCKET, blobName)); + } + } + @Test public void testReadAndWriteCaptureChannels() throws IOException { String blobName = "test-read-and-write-capture-channels-blob"; From 36ebabda8ae8f075e5b83128344c5a92c84c3715 Mon Sep 17 00:00:00 2001 From: aozarov Date: Mon, 7 Mar 2016 17:00:03 -0800 Subject: [PATCH 08/44] Fix test and issue #723 --- .../google/gcloud/BaseServiceException.java | 23 ++++++++++++------- .../google/gcloud/spi/DefaultStorageRpc.java | 7 +++++- .../gcloud/storage/BlobReadChannel.java | 2 +- .../gcloud/storage/it/ITStorageTest.java | 15 ++++++++---- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java index 579340f1256e..365243904436 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java @@ -97,13 +97,17 @@ public BaseServiceException(IOException exception, boolean idempotent) { String debugInfo = null; if (exception instanceof GoogleJsonResponseException) { GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails(); - Error error = error(jsonError); - code = error.code; - reason = error.reason; - if (reason != null) { - GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0); - location = errorInfo.getLocation(); - debugInfo = (String) errorInfo.get("debugInfo"); + if (jsonError != null) { + Error error = error(jsonError); + code = error.code; + reason = error.reason; + if (reason != null) { + GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0); + location = errorInfo.getLocation(); + debugInfo = (String) errorInfo.get("debugInfo"); + } + } else { + code = ((GoogleJsonResponseException) exception).getStatusCode(); } } this.code = code; @@ -207,7 +211,10 @@ protected static Error error(GoogleJsonError error) { protected static String message(IOException exception) { if (exception instanceof GoogleJsonResponseException) { - return ((GoogleJsonResponseException) exception).getDetails().getMessage(); + GoogleJsonError details = ((GoogleJsonResponseException) exception).getDetails(); + if (details != null) { + return details.getMessage(); + } } return exception.getMessage(); } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java index 4d9214ef5929..cac47b4bd248 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java @@ -31,6 +31,7 @@ import static com.google.gcloud.spi.StorageRpc.Option.PREFIX; import static com.google.gcloud.spi.StorageRpc.Option.VERSIONS; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; +import static javax.servlet.http.HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE; import com.google.api.client.googleapis.batch.json.JsonBatchCallback; import com.google.api.client.googleapis.json.GoogleJsonError; @@ -453,7 +454,11 @@ public Tuple read(StorageObject from, Map options, lo String etag = req.getLastResponseHeaders().getETag(); return Tuple.of(etag, output.toByteArray()); } catch (IOException ex) { - throw translate(ex); + StorageException serviceException = translate(ex); + if (serviceException.code() == SC_REQUESTED_RANGE_NOT_SATISFIABLE) { + return Tuple.of(null, new byte[0]); + } + throw serviceException; } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java index 2b9643434ecc..52b8c39321da 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java @@ -127,7 +127,7 @@ public Tuple call() { return storageRpc.read(storageObject, requestOptions, position, toRead); } }, serviceOptions.retryParams(), StorageImpl.EXCEPTION_HANDLER); - if (lastEtag != null && !Objects.equals(result.x(), lastEtag)) { + if (result.y().length > 0 && lastEtag != null && !Objects.equals(result.x(), lastEtag)) { StringBuilder messageBuilder = new StringBuilder(); messageBuilder.append("Blob ").append(blob).append(" was updated while reading"); throw new StorageException(0, messageBuilder.toString()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java index a411cdd60bfb..b62a54aff818 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java @@ -54,6 +54,7 @@ import org.junit.Test; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -834,14 +835,18 @@ public void testReadAndWriteChannelsWithDifferentFileSize() throws IOException { byte[] bytes = new byte[blobSize]; rnd.nextBytes(bytes); try (WriteChannel writer = storage.writer(blob)) { - writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT)); + writer.write(ByteBuffer.wrap(bytes)); } - ByteBuffer readBytes; + ByteArrayOutputStream output = new ByteArrayOutputStream(); try (ReadChannel reader = storage.reader(blob.blobId())) { - readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length); - reader.read(readBytes); + ByteBuffer buffer = ByteBuffer.allocate(64 * 1024); + while (reader.read(buffer) > 0) { + buffer.flip(); + output.write(buffer.array(), 0, buffer.limit()); + buffer.clear(); + } } - assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array()); + assertArrayEquals(bytes, output.toByteArray()); assertTrue(storage.delete(BUCKET, blobName)); } } From d930b4bbc2833d82ce84c13e3c6704695aebd91c Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 8 Mar 2016 12:29:17 +0100 Subject: [PATCH 09/44] Remove BlobListOption.recursive option and fix delimiter handling - Add BlobListOption.currentDirectory option that sets the '/' delimiter - Add isDirectory method to BlobInfo objects - Change StorageRpc.list(bucket) method to add prefixes to the list of storage objects - Add unit and integration tests --- .../google/gcloud/spi/DefaultStorageRpc.java | 21 ++++++- .../com/google/gcloud/spi/StorageRpc.java | 2 +- .../java/com/google/gcloud/storage/Blob.java | 6 ++ .../com/google/gcloud/storage/BlobInfo.java | 27 ++++++++ .../com/google/gcloud/storage/Storage.java | 16 +++-- .../google/gcloud/storage/StorageImpl.java | 3 +- .../google/gcloud/storage/StorageOptions.java | 31 +--------- .../google/gcloud/storage/BlobInfoTest.java | 62 +++++++++++++++++++ .../com/google/gcloud/storage/BlobTest.java | 38 +++++++++++- .../gcloud/storage/SerializationTest.java | 1 - .../gcloud/storage/StorageImplTest.java | 16 +++++ .../gcloud/storage/it/ITStorageTest.java | 46 ++++++++++++++ 12 files changed, 228 insertions(+), 41 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java index cac47b4bd248..fcbfe5514d1e 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java @@ -57,8 +57,10 @@ import com.google.api.services.storage.model.ComposeRequest.SourceObjects.ObjectPreconditions; import com.google.api.services.storage.model.Objects; import com.google.api.services.storage.model.StorageObject; +import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.gcloud.storage.StorageException; @@ -67,6 +69,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -151,7 +154,7 @@ public Tuple> list(Map options) { } @Override - public Tuple> list(String bucket, Map options) { + public Tuple> list(final String bucket, Map options) { try { Objects objects = storage.objects() .list(bucket) @@ -163,8 +166,20 @@ public Tuple> list(String bucket, Map .setPageToken(PAGE_TOKEN.getString(options)) .setFields(FIELDS.getString(options)) .execute(); - return Tuple.>of( - objects.getNextPageToken(), objects.getItems()); + Iterable storageObjects = Iterables.concat( + objects.getItems() != null ? objects.getItems() : ImmutableList.of(), + objects.getPrefixes() != null + ? Lists.transform(objects.getPrefixes(), new Function() { + @Override + public StorageObject apply(String prefix) { + return new StorageObject() + .set("isDirectory", true) + .setBucket(bucket) + .setName(prefix) + .setSize(BigInteger.ZERO); + } + }) : ImmutableList.of()); + return Tuple.of(objects.getNextPageToken(), storageObjects); } catch (IOException ex) { throw translate(ex); } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java index c76fc0f2d3b8..ab4a7a9d0acb 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java @@ -344,7 +344,7 @@ void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, /** * Continues rewriting on an already open rewrite channel. * - * @throws StorageException + * @throws StorageException upon failure */ RewriteResponse continueRewrite(RewriteResponse previousResponse); } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index aea424ca4063..79ad3804faf0 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -290,6 +290,12 @@ Builder updateTime(Long updateTime) { return this; } + @Override + Builder isDirectory(boolean isDirectory) { + infoBuilder.isDirectory(isDirectory); + return this; + } + @Override public Blob build() { return new Blob(storage, infoBuilder); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java index 54fabe87d766..9d981aeeafda 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java @@ -78,6 +78,7 @@ public StorageObject apply(BlobInfo blobInfo) { private final String contentDisposition; private final String contentLanguage; private final Integer componentCount; + private final boolean isDirectory; /** * This class is meant for internal use only. Users are discouraged from using this class. @@ -187,6 +188,8 @@ public abstract static class Builder { abstract Builder updateTime(Long updateTime); + abstract Builder isDirectory(boolean isDirectory); + /** * Creates a {@code BlobInfo} object. */ @@ -215,6 +218,7 @@ static final class BuilderImpl extends Builder { private Long metageneration; private Long deleteTime; private Long updateTime; + private Boolean isDirectory; BuilderImpl(BlobId blobId) { this.blobId = blobId; @@ -241,6 +245,7 @@ static final class BuilderImpl extends Builder { metageneration = blobInfo.metageneration; deleteTime = blobInfo.deleteTime; updateTime = blobInfo.updateTime; + isDirectory = blobInfo.isDirectory; } @Override @@ -364,6 +369,12 @@ Builder updateTime(Long updateTime) { return this; } + @Override + Builder isDirectory(boolean isDirectory) { + this.isDirectory = isDirectory; + return this; + } + @Override public BlobInfo build() { checkNotNull(blobId); @@ -392,6 +403,7 @@ public BlobInfo build() { metageneration = builder.metageneration; deleteTime = builder.deleteTime; updateTime = builder.updateTime; + isDirectory = firstNonNull(builder.isDirectory, Boolean.FALSE); } /** @@ -588,6 +600,18 @@ public Long updateTime() { return updateTime; } + /** + * Returns {@code true} if the current blob represents a directory. This can only happen if the + * blob is returned by {@link Storage#list(String, Storage.BlobListOption...)} when the + * {@link Storage.BlobListOption#currentDirectory()} option is used. If {@code true} only + * {@link #blobId()} and {@link #size()} are set for the current blob: {@link BlobId#name()} ends + * with the '/' character, {@link BlobId#generation()} returns {@code null} and {@link #size()} is + * {@code 0}. + */ + public boolean isDirectory() { + return isDirectory; + } + /** * Returns a builder for the current blob. */ @@ -761,6 +785,9 @@ public Acl apply(ObjectAccessControl objectAccessControl) { } })); } + if (storageObject.get("isDirectory") != null) { + builder.isDirectory(Boolean.TRUE); + } return builder.build(); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 065bcca7c9e8..e1ab337cdbdf 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -694,10 +694,17 @@ public static BlobListOption prefix(String prefix) { } /** - * Returns an option to specify whether blob listing should include subdirectories or not. + * If specified, results are returned in a directory-like mode. Blobs whose names, aside from + * a possible {@link #prefix(String)}, do not contain the '/' delimiter are returned as is. + * Blobs whose names, aside from a possible {@link #prefix(String)}, contain the '/' delimiter, + * will have their name truncated after the delimiter and will be returned as {@link Blob} + * objects where only {@link Blob#blobId()}, {@link Blob#size()} and {@link Blob#isDirectory()} + * are set. For such directory blobs, ({@link BlobId#generation()} returns {@code null}), + * {@link Blob#size()} returns {@code 0} while {@link Blob#isDirectory()} returns {@code true}. + * Duplicate directory blobs are omitted. */ - public static BlobListOption recursive(boolean recursive) { - return new BlobListOption(StorageRpc.Option.DELIMITER, recursive); + public static BlobListOption currentDirectory() { + return new BlobListOption(StorageRpc.Option.DELIMITER, true); } /** @@ -1289,7 +1296,8 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx Page list(BucketListOption... options); /** - * Lists the bucket's blobs. + * Lists the bucket's blobs. If the {@link BlobListOption#currentDirectory()} option is provided, + * results are returned in a directory-like mode. * * @throws StorageException upon failure */ diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index de77cba021a1..788072905871 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -76,6 +76,7 @@ final class StorageImpl extends BaseService implements Storage { private static final byte[] EMPTY_BYTE_ARRAY = {}; private static final String EMPTY_BYTE_ARRAY_MD5 = "1B2M2Y8AsgTpgAmY7PhCfg=="; private static final String EMPTY_BYTE_ARRAY_CRC32C = "AAAAAA=="; + private static final String PATH_DELIMITER = "/"; private static final Function, Boolean> DELETE_FUNCTION = new Function, Boolean>() { @@ -669,7 +670,7 @@ private static void addToOptionMap(StorageRpc.Option getOption, StorageRpc.O } Boolean value = (Boolean) temp.remove(DELIMITER); if (Boolean.TRUE.equals(value)) { - temp.put(DELIMITER, options().pathDelimiter()); + temp.put(DELIMITER, PATH_DELIMITER); } if (useAsSource) { addToOptionMap(IF_GENERATION_MATCH, IF_SOURCE_GENERATION_MATCH, generation, temp); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java index bd30cb173366..ca9b8944ea8b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java @@ -16,14 +16,12 @@ package com.google.gcloud.storage; -import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableSet; import com.google.gcloud.ServiceOptions; import com.google.gcloud.spi.DefaultStorageRpc; import com.google.gcloud.spi.StorageRpc; import com.google.gcloud.spi.StorageRpcFactory; -import java.util.Objects; import java.util.Set; public class StorageOptions extends ServiceOptions { @@ -31,9 +29,6 @@ public class StorageOptions extends ServiceOptions SCOPES = ImmutableSet.of(GCS_SCOPE); - private static final String DEFAULT_PATH_DELIMITER = "/"; - - private final String pathDelimiter; public static class DefaultStorageFactory implements StorageFactory { @@ -58,24 +53,10 @@ public StorageRpc create(StorageOptions options) { public static class Builder extends ServiceOptions.Builder { - private String pathDelimiter; - private Builder() {} private Builder(StorageOptions options) { super(options); - pathDelimiter = options.pathDelimiter; - } - - /** - * Sets the path delimiter for the storage service. - * - * @param pathDelimiter the path delimiter to set - * @return the builder - */ - public Builder pathDelimiter(String pathDelimiter) { - this.pathDelimiter = pathDelimiter; - return this; } @Override @@ -86,7 +67,6 @@ public StorageOptions build() { private StorageOptions(Builder builder) { super(StorageFactory.class, StorageRpcFactory.class, builder); - pathDelimiter = MoreObjects.firstNonNull(builder.pathDelimiter, DEFAULT_PATH_DELIMITER); } @SuppressWarnings("unchecked") @@ -106,13 +86,6 @@ protected Set scopes() { return SCOPES; } - /** - * Returns the storage service's path delimiter. - */ - public String pathDelimiter() { - return pathDelimiter; - } - /** * Returns a default {@code StorageOptions} instance. */ @@ -128,7 +101,7 @@ public Builder toBuilder() { @Override public int hashCode() { - return baseHashCode() ^ Objects.hash(pathDelimiter); + return baseHashCode(); } @Override @@ -137,7 +110,7 @@ public boolean equals(Object obj) { return false; } StorageOptions other = (StorageOptions) obj; - return baseEquals(other) && Objects.equals(pathDelimiter, other.pathDelimiter); + return baseEquals(other); } public static Builder builder() { diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java index a1cc01f4287c..029181c6c07b 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java @@ -20,7 +20,11 @@ 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 static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import com.google.api.services.storage.model.StorageObject; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gcloud.storage.Acl.Project; @@ -28,6 +32,7 @@ import org.junit.Test; +import java.math.BigInteger; import java.util.List; import java.util.Map; @@ -76,6 +81,10 @@ public class BlobInfoTest { .size(SIZE) .updateTime(UPDATE_TIME) .build(); + private static final BlobInfo DIRECTORY_INFO = BlobInfo.builder("b", "n/") + .size(0L) + .isDirectory(true) + .build(); @Test public void testToBuilder() { @@ -118,6 +127,30 @@ public void testBuilder() { assertEquals(SELF_LINK, BLOB_INFO.selfLink()); assertEquals(SIZE, BLOB_INFO.size()); assertEquals(UPDATE_TIME, BLOB_INFO.updateTime()); + assertFalse(BLOB_INFO.isDirectory()); + assertEquals("b", DIRECTORY_INFO.bucket()); + assertEquals("n/", DIRECTORY_INFO.name()); + assertNull(DIRECTORY_INFO.acl()); + assertNull(DIRECTORY_INFO.componentCount()); + assertNull(DIRECTORY_INFO.contentType()); + assertNull(DIRECTORY_INFO.cacheControl()); + assertNull(DIRECTORY_INFO.contentDisposition()); + assertNull(DIRECTORY_INFO.contentEncoding()); + assertNull(DIRECTORY_INFO.contentLanguage()); + assertNull(DIRECTORY_INFO.crc32c()); + assertNull(DIRECTORY_INFO.deleteTime()); + assertNull(DIRECTORY_INFO.etag()); + assertNull(DIRECTORY_INFO.generation()); + assertNull(DIRECTORY_INFO.id()); + assertNull(DIRECTORY_INFO.md5()); + assertNull(DIRECTORY_INFO.mediaLink()); + assertNull(DIRECTORY_INFO.metadata()); + assertNull(DIRECTORY_INFO.metageneration()); + assertNull(DIRECTORY_INFO.owner()); + assertNull(DIRECTORY_INFO.selfLink()); + assertEquals(0L, (long) DIRECTORY_INFO.size()); + assertNull(DIRECTORY_INFO.updateTime()); + assertTrue(DIRECTORY_INFO.isDirectory()); } private void compareBlobs(BlobInfo expected, BlobInfo value) { @@ -151,6 +184,35 @@ public void testToPbAndFromPb() { compareBlobs(BLOB_INFO, BlobInfo.fromPb(BLOB_INFO.toPb())); BlobInfo blobInfo = BlobInfo.builder(BlobId.of("b", "n")).build(); compareBlobs(blobInfo, BlobInfo.fromPb(blobInfo.toPb())); + StorageObject object = new StorageObject() + .setName("n/") + .setBucket("b") + .setSize(BigInteger.ZERO) + .set("isDirectory", true); + blobInfo = BlobInfo.fromPb(object); + assertEquals("b", blobInfo.bucket()); + assertEquals("n/", blobInfo.name()); + assertNull(blobInfo.acl()); + assertNull(blobInfo.componentCount()); + assertNull(blobInfo.contentType()); + assertNull(blobInfo.cacheControl()); + assertNull(blobInfo.contentDisposition()); + assertNull(blobInfo.contentEncoding()); + assertNull(blobInfo.contentLanguage()); + assertNull(blobInfo.crc32c()); + assertNull(blobInfo.deleteTime()); + assertNull(blobInfo.etag()); + assertNull(blobInfo.generation()); + assertNull(blobInfo.id()); + assertNull(blobInfo.md5()); + assertNull(blobInfo.mediaLink()); + assertNull(blobInfo.metadata()); + assertNull(blobInfo.metageneration()); + assertNull(blobInfo.owner()); + assertNull(blobInfo.selfLink()); + assertEquals(0L, (long) blobInfo.size()); + assertNull(blobInfo.updateTime()); + assertTrue(blobInfo.isDirectory()); } @Test diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java index c7508593f8c9..5a6173c08199 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java @@ -95,6 +95,10 @@ public class BlobTest { .updateTime(UPDATE_TIME) .build(); private static final BlobInfo BLOB_INFO = BlobInfo.builder("b", "n").metageneration(42L).build(); + private static final BlobInfo DIRECTORY_INFO = BlobInfo.builder("b", "n/") + .size(0L) + .isDirectory(true) + .build(); private Storage storage; private Blob blob; @@ -305,18 +309,20 @@ public void testSignUrl() throws Exception { @Test public void testToBuilder() { - expect(storage.options()).andReturn(mockOptions).times(4); + expect(storage.options()).andReturn(mockOptions).times(6); replay(storage); Blob fullBlob = new Blob(storage, new BlobInfo.BuilderImpl(FULL_BLOB_INFO)); assertEquals(fullBlob, fullBlob.toBuilder().build()); Blob simpleBlob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO)); assertEquals(simpleBlob, simpleBlob.toBuilder().build()); + Blob directory = new Blob(storage, new BlobInfo.BuilderImpl(DIRECTORY_INFO)); + assertEquals(directory, directory.toBuilder().build()); } @Test public void testBuilder() { initializeExpectedBlob(4); - expect(storage.options()).andReturn(mockOptions).times(2); + expect(storage.options()).andReturn(mockOptions).times(4); replay(storage); Blob.Builder builder = new Blob.Builder(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO))); Blob blob = builder.acl(ACL) @@ -360,5 +366,33 @@ public void testBuilder() { assertEquals(SELF_LINK, blob.selfLink()); assertEquals(SIZE, blob.size()); assertEquals(UPDATE_TIME, blob.updateTime()); + assertFalse(blob.isDirectory()); + builder = new Blob.Builder(new Blob(storage, new BlobInfo.BuilderImpl(DIRECTORY_INFO))); + blob = builder.blobId(BlobId.of("b", "n/")) + .isDirectory(true) + .size(0L) + .build(); + assertEquals("b", blob.bucket()); + assertEquals("n/", blob.name()); + assertNull(blob.acl()); + assertNull(blob.componentCount()); + assertNull(blob.contentType()); + assertNull(blob.cacheControl()); + assertNull(blob.contentDisposition()); + assertNull(blob.contentEncoding()); + assertNull(blob.contentLanguage()); + assertNull(blob.crc32c()); + assertNull(blob.deleteTime()); + assertNull(blob.etag()); + assertNull(blob.id()); + assertNull(blob.md5()); + assertNull(blob.mediaLink()); + assertNull(blob.metadata()); + assertNull(blob.metageneration()); + assertNull(blob.owner()); + assertNull(blob.selfLink()); + assertEquals(0L, (long) blob.size()); + assertNull(blob.updateTime()); + assertTrue(blob.isDirectory()); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index c9b957bb936a..ac096375b120 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -90,7 +90,6 @@ public void testServiceOptions() throws Exception { .projectId("p2") .retryParams(RetryParams.defaultInstance()) .authCredentials(null) - .pathDelimiter(":") .build(); serializedCopy = serializeAndDeserialize(options); assertEquals(options, serializedCopy); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java index 612664de14ae..4050e7d6267b 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java @@ -719,6 +719,22 @@ public void testListBlobsWithEmptyFields() { assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class)); } + @Test + public void testListBlobsCurrentDirectory() { + String cursor = "cursor"; + Map options = ImmutableMap.of(StorageRpc.Option.DELIMITER, "/"); + ImmutableList blobInfoList = ImmutableList.of(BLOB_INFO1, BLOB_INFO2); + Tuple> result = + Tuple.of(cursor, Iterables.transform(blobInfoList, BlobInfo.INFO_TO_PB_FUNCTION)); + EasyMock.expect(storageRpcMock.list(BUCKET_NAME1, options)).andReturn(result); + EasyMock.replay(storageRpcMock); + initializeService(); + ImmutableList blobList = ImmutableList.of(expectedBlob1, expectedBlob2); + Page page = storage.list(BUCKET_NAME1, Storage.BlobListOption.currentDirectory()); + assertEquals(cursor, page.nextPageCursor()); + assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class)); + } + @Test public void testUpdateBucket() { BucketInfo updatedBucketInfo = BUCKET_INFO1.toBuilder().indexPage("some-page").build(); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java index b62a54aff818..563a621c48fb 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java @@ -411,6 +411,52 @@ public void testListBlobsVersioned() throws ExecutionException, InterruptedExcep } } + @Test(timeout = 5000) + public void testListBlobsCurrentDirectory() throws InterruptedException { + String directoryName = "test-list-blobs-current-directory/"; + String subdirectoryName = "subdirectory/"; + String[] blobNames = {directoryName + subdirectoryName + "blob1", + directoryName + "blob2"}; + BlobInfo blob1 = BlobInfo.builder(BUCKET, blobNames[0]) + .contentType(CONTENT_TYPE) + .build(); + BlobInfo blob2 = BlobInfo.builder(BUCKET, blobNames[1]) + .contentType(CONTENT_TYPE) + .build(); + Blob remoteBlob1 = storage.create(blob1, BLOB_BYTE_CONTENT); + Blob remoteBlob2 = storage.create(blob2, BLOB_BYTE_CONTENT); + assertNotNull(remoteBlob1); + assertNotNull(remoteBlob2); + Page page = storage.list(BUCKET, + Storage.BlobListOption.prefix("test-list-blobs-current-directory/"), + Storage.BlobListOption.currentDirectory()); + // Listing blobs is eventually consistent, we loop until the list is of the expected size. The + // test fails if timeout is reached. + while (Iterators.size(page.iterateAll()) != 2) { + Thread.sleep(500); + page = storage.list(BUCKET, + Storage.BlobListOption.prefix("test-list-blobs-current-directory/"), + Storage.BlobListOption.currentDirectory()); + } + Iterator iterator = page.iterateAll(); + while (iterator.hasNext()) { + Blob remoteBlob = iterator.next(); + assertEquals(BUCKET, remoteBlob.bucket()); + if (remoteBlob.name().equals(blobNames[1])) { + assertEquals(CONTENT_TYPE, remoteBlob.contentType()); + assertEquals(BLOB_BYTE_CONTENT.length, (long) remoteBlob.size()); + assertFalse(remoteBlob.isDirectory()); + } else if (remoteBlob.name().equals(directoryName + subdirectoryName)) { + assertEquals(0L, (long) remoteBlob.size()); + assertTrue(remoteBlob.isDirectory()); + } else { + fail("Unexpected blob with name " + remoteBlob.name()); + } + } + assertTrue(remoteBlob1.delete()); + assertTrue(remoteBlob2.delete()); + } + @Test public void testUpdateBlob() { String blobName = "test-update-blob"; From 0f4a7a0fd9d2927fd35c09712e7f531411322739 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 8 Mar 2016 18:41:22 +0100 Subject: [PATCH 10/44] Release version 0.1.5 --- gcloud-java-bigquery/pom.xml | 2 +- gcloud-java-contrib/pom.xml | 2 +- gcloud-java-core/pom.xml | 2 +- gcloud-java-datastore/pom.xml | 2 +- gcloud-java-examples/pom.xml | 2 +- gcloud-java-resourcemanager/pom.xml | 2 +- gcloud-java-storage/pom.xml | 2 +- gcloud-java/pom.xml | 2 +- pom.xml | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index 34ddd7679e97..ac42c839bdb7 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 gcloud-java-bigquery diff --git a/gcloud-java-contrib/pom.xml b/gcloud-java-contrib/pom.xml index dd976991e2af..78e7670d3ae8 100644 --- a/gcloud-java-contrib/pom.xml +++ b/gcloud-java-contrib/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 gcloud-java-contrib diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index d07a567b7e5a..8525fde68174 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 gcloud-java-core diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index 452986ba5ea3..67e72245df25 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 gcloud-java-datastore diff --git a/gcloud-java-examples/pom.xml b/gcloud-java-examples/pom.xml index 862d48c89eaa..9586536e3624 100644 --- a/gcloud-java-examples/pom.xml +++ b/gcloud-java-examples/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 gcloud-java-examples diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index 40a865f4db68..513da0b67b7e 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 gcloud-java-resourcemanager diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index f18283b70bc8..01b8f5e01c3f 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 gcloud-java-storage diff --git a/gcloud-java/pom.xml b/gcloud-java/pom.xml index adfa716fe27b..927455dfb159 100644 --- a/gcloud-java/pom.xml +++ b/gcloud-java/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5-SNAPSHOT + 0.1.5 diff --git a/pom.xml b/pom.xml index 28bcba708f7f..b656f5853972 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.gcloud gcloud-java-pom pom - 0.1.5-SNAPSHOT + 0.1.5 GCloud Java https://github.com/GoogleCloudPlatform/gcloud-java From 1f1f4b05c8a5073bcf91da1160da5aac16cab96a Mon Sep 17 00:00:00 2001 From: travis-ci Date: Tue, 8 Mar 2016 18:26:13 +0000 Subject: [PATCH 11/44] Updating version in README files. [ci skip] --- README.md | 6 +++--- gcloud-java-bigquery/README.md | 6 +++--- gcloud-java-contrib/README.md | 6 +++--- gcloud-java-core/README.md | 6 +++--- gcloud-java-datastore/README.md | 6 +++--- gcloud-java-examples/README.md | 6 +++--- gcloud-java-resourcemanager/README.md | 6 +++--- gcloud-java-storage/README.md | 6 +++--- gcloud-java/README.md | 6 +++--- 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 32a0f539425e..68c624c37489 100644 --- a/README.md +++ b/README.md @@ -29,16 +29,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java:0.1.4' +compile 'com.google.gcloud:gcloud-java:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.5" ``` Example Applications diff --git a/gcloud-java-bigquery/README.md b/gcloud-java-bigquery/README.md index 58633ba635f9..3387cd8c4f41 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-bigquery - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-bigquery:0.1.4' +compile 'com.google.gcloud:gcloud-java-bigquery:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.5" ``` Example Application diff --git a/gcloud-java-contrib/README.md b/gcloud-java-contrib/README.md index 7a935192891d..426417d54e87 100644 --- a/gcloud-java-contrib/README.md +++ b/gcloud-java-contrib/README.md @@ -16,16 +16,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-contrib - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-contrib:0.1.4' +compile 'com.google.gcloud:gcloud-java-contrib:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-contrib" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java-contrib" % "0.1.5" ``` Java Versions diff --git a/gcloud-java-core/README.md b/gcloud-java-core/README.md index bc9463b9cc2b..fc5f481f8ec3 100644 --- a/gcloud-java-core/README.md +++ b/gcloud-java-core/README.md @@ -19,16 +19,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-core - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-core:0.1.4' +compile 'com.google.gcloud:gcloud-java-core:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.1.5" ``` Troubleshooting diff --git a/gcloud-java-datastore/README.md b/gcloud-java-datastore/README.md index dd341ba244c3..0d89a0a07e3e 100644 --- a/gcloud-java-datastore/README.md +++ b/gcloud-java-datastore/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-datastore - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-datastore:0.1.4' +compile 'com.google.gcloud:gcloud-java-datastore:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-datastore" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java-datastore" % "0.1.5" ``` Example Application diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 59fbca11e219..7d54b8f3e1a9 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -19,16 +19,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-examples - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-examples:0.1.4' +compile 'com.google.gcloud:gcloud-java-examples:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.1.5" ``` To run examples from your command line: diff --git a/gcloud-java-resourcemanager/README.md b/gcloud-java-resourcemanager/README.md index cd48d6699311..94037e27a709 100644 --- a/gcloud-java-resourcemanager/README.md +++ b/gcloud-java-resourcemanager/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-resourcemanager - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-resourcemanager:0.1.4' +compile 'com.google.gcloud:gcloud-java-resourcemanager:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.5" ``` Example Application diff --git a/gcloud-java-storage/README.md b/gcloud-java-storage/README.md index f7973544bba2..0ee05b31c10c 100644 --- a/gcloud-java-storage/README.md +++ b/gcloud-java-storage/README.md @@ -22,16 +22,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java-storage - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java-storage:0.1.4' +compile 'com.google.gcloud:gcloud-java-storage:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.1.5" ``` Example Application diff --git a/gcloud-java/README.md b/gcloud-java/README.md index c51b4e8fe7bc..e296d0c0c565 100644 --- a/gcloud-java/README.md +++ b/gcloud-java/README.md @@ -27,16 +27,16 @@ If you are using Maven, add this to your pom.xml file com.google.gcloud gcloud-java - 0.1.4 + 0.1.5 ``` If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.gcloud:gcloud-java:0.1.4' +compile 'com.google.gcloud:gcloud-java:0.1.5' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.4" +libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.5" ``` Troubleshooting From aa6c173871113775725082517ba113fc0306a619 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 8 Mar 2016 20:27:32 +0100 Subject: [PATCH 12/44] Update version to 0.1.6-SNAPSHOT --- gcloud-java-bigquery/pom.xml | 2 +- gcloud-java-contrib/pom.xml | 2 +- gcloud-java-core/pom.xml | 2 +- gcloud-java-datastore/pom.xml | 2 +- gcloud-java-examples/pom.xml | 2 +- gcloud-java-resourcemanager/pom.xml | 2 +- gcloud-java-storage/pom.xml | 2 +- gcloud-java/pom.xml | 2 +- pom.xml | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index ac42c839bdb7..9a2137cb987d 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT gcloud-java-bigquery diff --git a/gcloud-java-contrib/pom.xml b/gcloud-java-contrib/pom.xml index 78e7670d3ae8..bd4a6458dc38 100644 --- a/gcloud-java-contrib/pom.xml +++ b/gcloud-java-contrib/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT gcloud-java-contrib diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index 8525fde68174..6d0ed675b423 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT gcloud-java-core diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index 67e72245df25..977b6db22b14 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT gcloud-java-datastore diff --git a/gcloud-java-examples/pom.xml b/gcloud-java-examples/pom.xml index 9586536e3624..111308658c2e 100644 --- a/gcloud-java-examples/pom.xml +++ b/gcloud-java-examples/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT gcloud-java-examples diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index 513da0b67b7e..c10691d3b07d 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT gcloud-java-resourcemanager diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index 01b8f5e01c3f..d5f0f6d98660 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT gcloud-java-storage diff --git a/gcloud-java/pom.xml b/gcloud-java/pom.xml index 927455dfb159..19536faa8c8d 100644 --- a/gcloud-java/pom.xml +++ b/gcloud-java/pom.xml @@ -10,7 +10,7 @@ com.google.gcloud gcloud-java-pom - 0.1.5 + 0.1.6-SNAPSHOT diff --git a/pom.xml b/pom.xml index b656f5853972..b9d979c4d467 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.gcloud gcloud-java-pom pom - 0.1.5 + 0.1.6-SNAPSHOT GCloud Java https://github.com/GoogleCloudPlatform/gcloud-java From de9b0d60eee8079223feb6f1f30b069e91e67431 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 9 Mar 2016 12:34:48 +0100 Subject: [PATCH 13/44] Refactor Storage's delimiter support - Minor fixes to javadoc - Favor firstNonNull over ternary operator when possible - Move prefix-object transformer to static function - Simplify StorageOptions equals method --- .../google/gcloud/spi/DefaultStorageRpc.java | 31 +++++++++++-------- .../com/google/gcloud/storage/BlobInfo.java | 4 +-- .../com/google/gcloud/storage/Storage.java | 16 +++++----- .../google/gcloud/storage/StorageOptions.java | 6 +--- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java index fcbfe5514d1e..ec4447d406cb 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java @@ -14,6 +14,7 @@ package com.google.gcloud.spi; +import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER; import static com.google.gcloud.spi.StorageRpc.Option.FIELDS; import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_MATCH; @@ -58,7 +59,6 @@ import com.google.api.services.storage.model.Objects; import com.google.api.services.storage.model.StorageObject; import com.google.common.base.Function; -import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -167,24 +167,29 @@ public Tuple> list(final String bucket, Map storageObjects = Iterables.concat( - objects.getItems() != null ? objects.getItems() : ImmutableList.of(), + firstNonNull(objects.getItems(), ImmutableList.of()), objects.getPrefixes() != null - ? Lists.transform(objects.getPrefixes(), new Function() { - @Override - public StorageObject apply(String prefix) { - return new StorageObject() - .set("isDirectory", true) - .setBucket(bucket) - .setName(prefix) - .setSize(BigInteger.ZERO); - } - }) : ImmutableList.of()); + ? Lists.transform(objects.getPrefixes(), objectFromPrefix(bucket)) + : ImmutableList.of()); return Tuple.of(objects.getNextPageToken(), storageObjects); } catch (IOException ex) { throw translate(ex); } } + private static Function objectFromPrefix(final String bucket) { + return new Function() { + @Override + public StorageObject apply(String prefix) { + return new StorageObject() + .set("isDirectory", true) + .setBucket(bucket) + .setName(prefix) + .setSize(BigInteger.ZERO); + } + }; + } + @Override public Bucket get(Bucket bucket, Map options) { try { @@ -549,7 +554,7 @@ public String open(StorageObject object, Map options) { HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object)); httpRequest.getHeaders().set("X-Upload-Content-Type", - MoreObjects.firstNonNull(object.getContentType(), "application/octet-stream")); + firstNonNull(object.getContentType(), "application/octet-stream")); HttpResponse response = httpRequest.execute(); if (response.getStatusCode() != 200) { GoogleJsonError error = new GoogleJsonError(); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java index 9d981aeeafda..cf509c8f0961 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobInfo.java @@ -603,7 +603,7 @@ public Long updateTime() { /** * Returns {@code true} if the current blob represents a directory. This can only happen if the * blob is returned by {@link Storage#list(String, Storage.BlobListOption...)} when the - * {@link Storage.BlobListOption#currentDirectory()} option is used. If {@code true} only + * {@link Storage.BlobListOption#currentDirectory()} option is used. When this is the case only * {@link #blobId()} and {@link #size()} are set for the current blob: {@link BlobId#name()} ends * with the '/' character, {@link BlobId#generation()} returns {@code null} and {@link #size()} is * {@code 0}. @@ -785,7 +785,7 @@ public Acl apply(ObjectAccessControl objectAccessControl) { } })); } - if (storageObject.get("isDirectory") != null) { + if (storageObject.containsKey("isDirectory")) { builder.isDirectory(Boolean.TRUE); } return builder.build(); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index e1ab337cdbdf..0ee18f541284 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -694,14 +694,14 @@ public static BlobListOption prefix(String prefix) { } /** - * If specified, results are returned in a directory-like mode. Blobs whose names, aside from - * a possible {@link #prefix(String)}, do not contain the '/' delimiter are returned as is. - * Blobs whose names, aside from a possible {@link #prefix(String)}, contain the '/' delimiter, - * will have their name truncated after the delimiter and will be returned as {@link Blob} - * objects where only {@link Blob#blobId()}, {@link Blob#size()} and {@link Blob#isDirectory()} - * are set. For such directory blobs, ({@link BlobId#generation()} returns {@code null}), - * {@link Blob#size()} returns {@code 0} while {@link Blob#isDirectory()} returns {@code true}. - * Duplicate directory blobs are omitted. + * If specified, results are returned in a directory-like mode. Blobs whose names, after a + * possible {@link #prefix(String)}, do not contain the '/' delimiter are returned as is. Blobs + * whose names, after a possible {@link #prefix(String)}, contain the '/' delimiter, will have + * their name truncated after the delimiter and will be returned as {@link Blob} objects where + * only {@link Blob#blobId()}, {@link Blob#size()} and {@link Blob#isDirectory()} are set. For + * such directory blobs, ({@link BlobId#generation()} returns {@code null}), {@link Blob#size()} + * returns {@code 0} while {@link Blob#isDirectory()} returns {@code true}. Duplicate directory + * blobs are omitted. */ public static BlobListOption currentDirectory() { return new BlobListOption(StorageRpc.Option.DELIMITER, true); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java index ca9b8944ea8b..86ce18eb71ec 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java @@ -106,11 +106,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (!(obj instanceof StorageOptions)) { - return false; - } - StorageOptions other = (StorageOptions) obj; - return baseEquals(other); + return obj instanceof StorageOptions && baseEquals((StorageOptions) obj); } public static Builder builder() { From 407c43604ea02902a94c4e26397ddd8e6ab4705a Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 10 Mar 2016 21:22:21 +0100 Subject: [PATCH 14/44] Create service-specific spi packages --- .../com/google/gcloud/bigquery/BigQuery.java | 2 +- .../google/gcloud/bigquery/BigQueryImpl.java | 2 +- .../gcloud/bigquery/BigQueryOptions.java | 6 +- .../com/google/gcloud/bigquery/Option.java | 2 +- .../{ => bigquery}/spi/BigQueryRpc.java | 2 +- .../spi/BigQueryRpcFactory.java | 3 +- .../spi/DefaultBigQueryRpc.java | 14 +- .../gcloud/bigquery/BigQueryImplTest.java | 6 +- .../google/gcloud/bigquery/OptionTest.java | 2 +- .../bigquery/TableDataWriteChannelTest.java | 4 +- .../gcloud/datastore/DatastoreImpl.java | 2 +- .../gcloud/datastore/DatastoreOptions.java | 6 +- .../{ => datastore}/spi/DatastoreRpc.java | 2 +- .../spi/DatastoreRpcFactory.java | 3 +- .../spi/DefaultDatastoreRpc.java | 2 +- .../datastore/DatastoreOptionsTest.java | 4 +- .../gcloud/datastore/DatastoreTest.java | 4 +- .../examples/bigquery/BigQueryExample.java | 2 +- .../examples/storage/StorageExample.java | 2 +- .../google/gcloud/resourcemanager/Option.java | 2 +- .../resourcemanager/ResourceManager.java | 2 +- .../resourcemanager/ResourceManagerImpl.java | 4 +- .../ResourceManagerOptions.java | 6 +- .../spi/DefaultResourceManagerRpc.java | 16 +-- .../spi/ResourceManagerRpc.java | 2 +- .../spi/ResourceManagerRpcFactory.java | 3 +- .../LocalResourceManagerHelperTest.java | 6 +- .../ResourceManagerImplTest.java | 4 +- .../java/com/google/gcloud/storage/Blob.java | 4 +- .../gcloud/storage/BlobReadChannel.java | 4 +- .../gcloud/storage/BlobWriteChannel.java | 2 +- .../com/google/gcloud/storage/Bucket.java | 2 +- .../com/google/gcloud/storage/CopyWriter.java | 6 +- .../com/google/gcloud/storage/Option.java | 2 +- .../com/google/gcloud/storage/Storage.java | 4 +- .../google/gcloud/storage/StorageImpl.java | 24 ++-- .../google/gcloud/storage/StorageOptions.java | 6 +- .../{ => storage}/spi/DefaultStorageRpc.java | 134 ++++++++---------- .../gcloud/{ => storage}/spi/StorageRpc.java | 2 +- .../{ => storage}/spi/StorageRpcFactory.java | 3 +- .../gcloud/storage/BlobReadChannelTest.java | 4 +- .../gcloud/storage/BlobWriteChannelTest.java | 4 +- .../google/gcloud/storage/CopyWriterTest.java | 8 +- .../com/google/gcloud/storage/OptionTest.java | 2 +- .../gcloud/storage/SerializationTest.java | 2 +- .../gcloud/storage/StorageImplTest.java | 6 +- 46 files changed, 159 insertions(+), 175 deletions(-) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/{ => bigquery}/spi/BigQueryRpc.java (99%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/{ => bigquery}/spi/BigQueryRpcFactory.java (90%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/{ => bigquery}/spi/DefaultBigQueryRpc.java (97%) rename gcloud-java-datastore/src/main/java/com/google/gcloud/{ => datastore}/spi/DatastoreRpc.java (98%) rename gcloud-java-datastore/src/main/java/com/google/gcloud/{ => datastore}/spi/DatastoreRpcFactory.java (90%) rename gcloud-java-datastore/src/main/java/com/google/gcloud/{ => datastore}/spi/DefaultDatastoreRpc.java (99%) rename gcloud-java-resourcemanager/src/main/java/com/google/gcloud/{ => resourcemanager}/spi/DefaultResourceManagerRpc.java (84%) rename gcloud-java-resourcemanager/src/main/java/com/google/gcloud/{ => resourcemanager}/spi/ResourceManagerRpc.java (98%) rename gcloud-java-resourcemanager/src/main/java/com/google/gcloud/{ => resourcemanager}/spi/ResourceManagerRpcFactory.java (90%) rename gcloud-java-storage/src/main/java/com/google/gcloud/{ => storage}/spi/DefaultStorageRpc.java (79%) rename gcloud-java-storage/src/main/java/com/google/gcloud/{ => storage}/spi/StorageRpc.java (99%) rename gcloud-java-storage/src/main/java/com/google/gcloud/{ => storage}/spi/StorageRpcFactory.java (91%) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 986c595e350d..3acaacaf42e5 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -25,7 +25,7 @@ import com.google.common.collect.Sets; import com.google.gcloud.Page; import com.google.gcloud.Service; -import com.google.gcloud.spi.BigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.util.List; import java.util.Set; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java index ce881c6ea079..27f4af5d5007 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java @@ -35,7 +35,7 @@ import com.google.gcloud.PageImpl.NextPageFetcher; import com.google.gcloud.RetryHelper; import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert; -import com.google.gcloud.spi.BigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.util.List; import java.util.Map; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryOptions.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryOptions.java index 71d43cfbe565..d48cf646f349 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryOptions.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryOptions.java @@ -18,9 +18,9 @@ import com.google.common.collect.ImmutableSet; import com.google.gcloud.ServiceOptions; -import com.google.gcloud.spi.BigQueryRpc; -import com.google.gcloud.spi.BigQueryRpcFactory; -import com.google.gcloud.spi.DefaultBigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpcFactory; +import com.google.gcloud.bigquery.spi.DefaultBigQueryRpc; import java.util.Set; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java index d88820fe5a29..3fdc27ecab99 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java @@ -19,7 +19,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.MoreObjects; -import com.google.gcloud.spi.BigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpc; import java.io.Serializable; import java.util.Objects; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/BigQueryRpc.java similarity index 99% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/BigQueryRpc.java index a1935e5ab136..d0b740e9e390 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/BigQueryRpc.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.bigquery.spi; import com.google.api.services.bigquery.model.Dataset; import com.google.api.services.bigquery.model.GetQueryResultsResponse; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpcFactory.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/BigQueryRpcFactory.java similarity index 90% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpcFactory.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/BigQueryRpcFactory.java index 2706868756a5..1323ec0624f4 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/BigQueryRpcFactory.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/BigQueryRpcFactory.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.bigquery.spi; import com.google.gcloud.bigquery.BigQueryOptions; +import com.google.gcloud.spi.ServiceRpcFactory; /** * An interface for BigQuery RPC factory. diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/DefaultBigQueryRpc.java similarity index 97% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/DefaultBigQueryRpc.java index a5c44129955a..f73517086a02 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/DefaultBigQueryRpc.java @@ -12,14 +12,14 @@ * the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.bigquery.spi; -import static com.google.gcloud.spi.BigQueryRpc.Option.DELETE_CONTENTS; -import static com.google.gcloud.spi.BigQueryRpc.Option.FIELDS; -import static com.google.gcloud.spi.BigQueryRpc.Option.MAX_RESULTS; -import static com.google.gcloud.spi.BigQueryRpc.Option.PAGE_TOKEN; -import static com.google.gcloud.spi.BigQueryRpc.Option.START_INDEX; -import static com.google.gcloud.spi.BigQueryRpc.Option.TIMEOUT; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.DELETE_CONTENTS; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.FIELDS; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.MAX_RESULTS; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.PAGE_TOKEN; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.START_INDEX; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.TIMEOUT; import static java.net.HttpURLConnection.HTTP_CREATED; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import static java.net.HttpURLConnection.HTTP_OK; diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java index 385ee6dcc8bd..305745e72da9 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java @@ -40,9 +40,9 @@ import com.google.gcloud.RetryParams; import com.google.gcloud.WriteChannel; import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert; -import com.google.gcloud.spi.BigQueryRpc; -import com.google.gcloud.spi.BigQueryRpc.Tuple; -import com.google.gcloud.spi.BigQueryRpcFactory; +import com.google.gcloud.bigquery.spi.BigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpc.Tuple; +import com.google.gcloud.bigquery.spi.BigQueryRpcFactory; import org.easymock.Capture; import org.easymock.EasyMock; diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java index 225fc284b203..2c89ececedb8 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/OptionTest.java @@ -18,7 +18,7 @@ import static org.junit.Assert.assertEquals; -import com.google.gcloud.spi.BigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpc; import org.junit.Test; diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDataWriteChannelTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDataWriteChannelTest.java index 6b7edcd76db1..4c1be470ff57 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDataWriteChannelTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDataWriteChannelTest.java @@ -32,8 +32,8 @@ import com.google.gcloud.RestorableState; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.BigQueryRpc; -import com.google.gcloud.spi.BigQueryRpcFactory; +import com.google.gcloud.bigquery.spi.BigQueryRpc; +import com.google.gcloud.bigquery.spi.BigQueryRpcFactory; import org.easymock.Capture; import org.easymock.CaptureType; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java index 92d18ed4787c..49a5728a4da9 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java @@ -26,7 +26,7 @@ import com.google.gcloud.RetryHelper; import com.google.gcloud.RetryHelper.RetryHelperException; import com.google.gcloud.RetryParams; -import com.google.gcloud.spi.DatastoreRpc; +import com.google.gcloud.datastore.spi.DatastoreRpc; import com.google.protobuf.ByteString; import java.util.Arrays; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java index 2ec0f2be8f2b..db1a5f800ce8 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java @@ -24,9 +24,9 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.gcloud.ServiceOptions; -import com.google.gcloud.spi.DatastoreRpc; -import com.google.gcloud.spi.DatastoreRpcFactory; -import com.google.gcloud.spi.DefaultDatastoreRpc; +import com.google.gcloud.datastore.spi.DatastoreRpc; +import com.google.gcloud.datastore.spi.DatastoreRpcFactory; +import com.google.gcloud.datastore.spi.DefaultDatastoreRpc; import java.lang.reflect.Method; import java.util.Iterator; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DatastoreRpc.java similarity index 98% rename from gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java rename to gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DatastoreRpc.java index 4d4540c70196..002078550d1f 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DatastoreRpc.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.datastore.spi; import com.google.api.services.datastore.DatastoreV1.AllocateIdsRequest; import com.google.api.services.datastore.DatastoreV1.AllocateIdsResponse; diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpcFactory.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DatastoreRpcFactory.java similarity index 90% rename from gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpcFactory.java rename to gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DatastoreRpcFactory.java index 1815dda30f5d..0979b2203037 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DatastoreRpcFactory.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DatastoreRpcFactory.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.datastore.spi; import com.google.gcloud.datastore.DatastoreOptions; +import com.google.gcloud.spi.ServiceRpcFactory; /** * An interface for Datastore RPC factory. diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java similarity index 99% rename from gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java rename to gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java index f679cc0d5826..093322fa4117 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/spi/DefaultDatastoreRpc.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.datastore.spi; import com.google.api.services.datastore.DatastoreV1.AllocateIdsRequest; import com.google.api.services.datastore.DatastoreV1.AllocateIdsResponse; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index 284a9d322793..b0d6e8d7800b 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -23,8 +23,8 @@ import static org.junit.Assert.assertTrue; import com.google.gcloud.datastore.testing.LocalGcdHelper; -import com.google.gcloud.spi.DatastoreRpc; -import com.google.gcloud.spi.DatastoreRpcFactory; +import com.google.gcloud.datastore.spi.DatastoreRpc; +import com.google.gcloud.datastore.spi.DatastoreRpcFactory; import org.easymock.EasyMock; import org.junit.Before; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 5d106fc7d31d..1886c67e22a8 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -39,8 +39,8 @@ import com.google.gcloud.datastore.StructuredQuery.Projection; import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; import com.google.gcloud.datastore.testing.LocalGcdHelper; -import com.google.gcloud.spi.DatastoreRpc; -import com.google.gcloud.spi.DatastoreRpcFactory; +import com.google.gcloud.datastore.spi.DatastoreRpc; +import com.google.gcloud.datastore.spi.DatastoreRpcFactory; import com.google.protobuf.ByteString; import org.easymock.EasyMock; diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/BigQueryExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/BigQueryExample.java index c8fbe7289f9c..fe27ee3cf63b 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/BigQueryExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/BigQueryExample.java @@ -43,7 +43,7 @@ import com.google.gcloud.bigquery.TableInfo; import com.google.gcloud.bigquery.ViewDefinition; import com.google.gcloud.bigquery.WriteChannelConfiguration; -import com.google.gcloud.spi.BigQueryRpc.Tuple; +import com.google.gcloud.bigquery.spi.BigQueryRpc.Tuple; import java.nio.channels.FileChannel; import java.nio.file.Paths; diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java index e73cfc427129..a10b5ef71817 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java @@ -20,7 +20,7 @@ import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; import com.google.gcloud.ReadChannel; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.StorageRpc.Tuple; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; import com.google.gcloud.storage.Blob; import com.google.gcloud.storage.BlobId; import com.google.gcloud.storage.BlobInfo; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java index f48c057ba049..72d62d7fc224 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java @@ -19,7 +19,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.MoreObjects; -import com.google.gcloud.spi.ResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.io.Serializable; import java.util.Objects; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index f9ddf6872414..a463937f875c 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -20,7 +20,7 @@ import com.google.common.collect.Sets; import com.google.gcloud.Page; import com.google.gcloud.Service; -import com.google.gcloud.spi.ResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import java.util.Set; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java index 4176b4e610ba..fb699dcb06f0 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java @@ -29,8 +29,8 @@ import com.google.gcloud.PageImpl; import com.google.gcloud.PageImpl.NextPageFetcher; import com.google.gcloud.RetryHelper.RetryHelperException; -import com.google.gcloud.spi.ResourceManagerRpc; -import com.google.gcloud.spi.ResourceManagerRpc.Tuple; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Tuple; import java.util.Map; import java.util.concurrent.Callable; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerOptions.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerOptions.java index 5c0c4baf1ecb..c744864147c2 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerOptions.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerOptions.java @@ -18,9 +18,9 @@ import com.google.common.collect.ImmutableSet; import com.google.gcloud.ServiceOptions; -import com.google.gcloud.spi.DefaultResourceManagerRpc; -import com.google.gcloud.spi.ResourceManagerRpc; -import com.google.gcloud.spi.ResourceManagerRpcFactory; +import com.google.gcloud.resourcemanager.spi.DefaultResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpcFactory; import java.util.Set; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java similarity index 84% rename from gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java rename to gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java index d30cd2df3627..19e40698a847 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java @@ -1,9 +1,5 @@ -package com.google.gcloud.spi; +package com.google.gcloud.resourcemanager.spi; -import static com.google.gcloud.spi.ResourceManagerRpc.Option.FIELDS; -import static com.google.gcloud.spi.ResourceManagerRpc.Option.FILTER; -import static com.google.gcloud.spi.ResourceManagerRpc.Option.PAGE_SIZE; -import static com.google.gcloud.spi.ResourceManagerRpc.Option.PAGE_TOKEN; import static java.net.HttpURLConnection.HTTP_FORBIDDEN; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; @@ -60,7 +56,7 @@ public Project get(String projectId, Map options) { try { return resourceManager.projects() .get(projectId) - .setFields(FIELDS.getString(options)) + .setFields(Option.FIELDS.getString(options)) .execute(); } catch (IOException ex) { ResourceManagerException translated = translate(ex); @@ -78,10 +74,10 @@ public Tuple> list(Map options) { try { ListProjectsResponse response = resourceManager.projects() .list() - .setFields(FIELDS.getString(options)) - .setFilter(FILTER.getString(options)) - .setPageSize(PAGE_SIZE.getInt(options)) - .setPageToken(PAGE_TOKEN.getString(options)) + .setFields(Option.FIELDS.getString(options)) + .setFilter(Option.FILTER.getString(options)) + .setPageSize(Option.PAGE_SIZE.getInt(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) .execute(); return Tuple.>of( response.getNextPageToken(), response.getProjects()); diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpc.java similarity index 98% rename from gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java rename to gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpc.java index e1d72a6a373e..54531edd5ed5 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpc.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.resourcemanager.spi; import com.google.api.services.cloudresourcemanager.model.Project; import com.google.gcloud.resourcemanager.ResourceManagerException; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpcFactory.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpcFactory.java similarity index 90% rename from gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpcFactory.java rename to gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpcFactory.java index c2c607c0c205..4dbd1a00d4c7 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpcFactory.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpcFactory.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.resourcemanager.spi; import com.google.gcloud.resourcemanager.ResourceManagerOptions; +import com.google.gcloud.spi.ServiceRpcFactory; /** * An interface for Resource Manager RPC factory. diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java index a3418fff98ab..328db315e414 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java @@ -9,9 +9,9 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper; -import com.google.gcloud.spi.DefaultResourceManagerRpc; -import com.google.gcloud.spi.ResourceManagerRpc; -import com.google.gcloud.spi.ResourceManagerRpc.Tuple; +import com.google.gcloud.resourcemanager.spi.DefaultResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Tuple; import org.junit.AfterClass; import org.junit.Before; diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java index 1bc233311a4d..92f5d2a18a0f 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java @@ -31,8 +31,8 @@ import com.google.gcloud.resourcemanager.ResourceManager.ProjectGetOption; import com.google.gcloud.resourcemanager.ResourceManager.ProjectListOption; import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper; -import com.google.gcloud.spi.ResourceManagerRpc; -import com.google.gcloud.spi.ResourceManagerRpcFactory; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; +import com.google.gcloud.resourcemanager.spi.ResourceManagerRpcFactory; import org.easymock.EasyMock; import org.junit.AfterClass; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index 79ad3804faf0..2cd81af5793d 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -24,8 +24,8 @@ import com.google.common.base.Function; import com.google.gcloud.ReadChannel; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpc.Tuple; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; import com.google.gcloud.storage.Storage.BlobTargetOption; import com.google.gcloud.storage.Storage.BlobWriteOption; import com.google.gcloud.storage.Storage.CopyRequest; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java index 52b8c39321da..f9c6f912563d 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java @@ -23,8 +23,8 @@ import com.google.gcloud.ReadChannel; import com.google.gcloud.RestorableState; import com.google.gcloud.RetryHelper; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpc.Tuple; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; import java.io.IOException; import java.io.Serializable; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannel.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannel.java index d1d12ec77638..30b0ec870f51 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannel.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannel.java @@ -23,7 +23,7 @@ import com.google.gcloud.RestorableState; import com.google.gcloud.RetryHelper; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc; import java.util.Map; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java index d318626f4207..3cb8af4ef044 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java @@ -26,7 +26,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.gcloud.Page; -import com.google.gcloud.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc; import com.google.gcloud.storage.Storage.BlobGetOption; import com.google.gcloud.storage.Storage.BucketTargetOption; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java index 7eb91d0910a2..62b39e005369 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java @@ -22,9 +22,9 @@ import com.google.gcloud.Restorable; import com.google.gcloud.RestorableState; import com.google.gcloud.RetryHelper; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpc.RewriteRequest; -import com.google.gcloud.spi.StorageRpc.RewriteResponse; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.RewriteRequest; +import com.google.gcloud.storage.spi.StorageRpc.RewriteResponse; import java.io.Serializable; import java.util.Map; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java index 2ec8426bfa9f..65c55da7efc8 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Option.java @@ -19,7 +19,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.MoreObjects; -import com.google.gcloud.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc; import java.io.Serializable; import java.util.Objects; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 0ee18f541284..6b2e9266f24b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -29,8 +29,8 @@ import com.google.gcloud.ReadChannel; import com.google.gcloud.Service; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpc.Tuple; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; import java.io.InputStream; import java.io.Serializable; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index 788072905871..d58c9e43aea9 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -19,15 +19,15 @@ import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; import static com.google.gcloud.RetryHelper.runWithRetries; -import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER; -import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_NOT_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_NOT_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.DELIMITER; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_GENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_GENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_METAGENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_METAGENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_GENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.api.services.storage.model.StorageObject; @@ -48,9 +48,9 @@ import com.google.gcloud.PageImpl.NextPageFetcher; import com.google.gcloud.ReadChannel; import com.google.gcloud.RetryHelper.RetryHelperException; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpc.RewriteResponse; -import com.google.gcloud.spi.StorageRpc.Tuple; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.RewriteResponse; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; import java.io.ByteArrayInputStream; import java.io.InputStream; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java index 86ce18eb71ec..e7e1c2778fa9 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java @@ -18,9 +18,9 @@ import com.google.common.collect.ImmutableSet; import com.google.gcloud.ServiceOptions; -import com.google.gcloud.spi.DefaultStorageRpc; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpcFactory; +import com.google.gcloud.storage.spi.DefaultStorageRpc; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpcFactory; import java.util.Set; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java similarity index 79% rename from gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java rename to gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java index ec4447d406cb..71cd51da1e38 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java @@ -12,25 +12,9 @@ * the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.storage.spi; import static com.google.common.base.MoreObjects.firstNonNull; -import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER; -import static com.google.gcloud.spi.StorageRpc.Option.FIELDS; -import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_NOT_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_NOT_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH; -import static com.google.gcloud.spi.StorageRpc.Option.MAX_RESULTS; -import static com.google.gcloud.spi.StorageRpc.Option.PAGE_TOKEN; -import static com.google.gcloud.spi.StorageRpc.Option.PREDEFINED_ACL; -import static com.google.gcloud.spi.StorageRpc.Option.PREDEFINED_DEFAULT_OBJECT_ACL; -import static com.google.gcloud.spi.StorageRpc.Option.PREFIX; -import static com.google.gcloud.spi.StorageRpc.Option.VERSIONS; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import static javax.servlet.http.HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE; @@ -108,8 +92,8 @@ public Bucket create(Bucket bucket, Map options) { return storage.buckets() .insert(this.options.projectId(), bucket) .setProjection(DEFAULT_PROJECTION) - .setPredefinedAcl(PREDEFINED_ACL.getString(options)) - .setPredefinedDefaultObjectAcl(PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) + .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) + .setPredefinedDefaultObjectAcl(Option.PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -125,11 +109,11 @@ public StorageObject create(StorageObject storageObject, final InputStream conte new InputStreamContent(storageObject.getContentType(), content)); insert.getMediaHttpUploader().setDirectUploadEnabled(true); return insert.setProjection(DEFAULT_PROJECTION) - .setPredefinedAcl(PREDEFINED_ACL.getString(options)) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)) + .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -142,10 +126,10 @@ public Tuple> list(Map options) { Buckets buckets = storage.buckets() .list(this.options.projectId()) .setProjection(DEFAULT_PROJECTION) - .setPrefix(PREFIX.getString(options)) - .setMaxResults(MAX_RESULTS.getLong(options)) - .setPageToken(PAGE_TOKEN.getString(options)) - .setFields(FIELDS.getString(options)) + .setPrefix(Option.PREFIX.getString(options)) + .setMaxResults(Option.MAX_RESULTS.getLong(options)) + .setPageToken(Option.PAGE_TOKEN.getString(options)) + .setFields(Option.FIELDS.getString(options)) .execute(); return Tuple.>of(buckets.getNextPageToken(), buckets.getItems()); } catch (IOException ex) { @@ -159,12 +143,12 @@ public Tuple> list(final String bucket, Map storageObjects = Iterables.concat( firstNonNull(objects.getItems(), ImmutableList.of()), @@ -196,9 +180,9 @@ public Bucket get(Bucket bucket, Map options) { return storage.buckets() .get(bucket.getName()) .setProjection(DEFAULT_PROJECTION) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setFields(FIELDS.getString(options)) + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setFields(Option.FIELDS.getString(options)) .execute(); } catch (IOException ex) { StorageException serviceException = translate(ex); @@ -228,11 +212,11 @@ private Storage.Objects.Get getRequest(StorageObject object, Map opti .get(object.getBucket(), object.getName()) .setGeneration(object.getGeneration()) .setProjection(DEFAULT_PROJECTION) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)) - .setFields(FIELDS.getString(options)); + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) + .setFields(Option.FIELDS.getString(options)); } @Override @@ -241,10 +225,10 @@ public Bucket patch(Bucket bucket, Map options) { return storage.buckets() .patch(bucket.getName(), bucket) .setProjection(DEFAULT_PROJECTION) - .setPredefinedAcl(PREDEFINED_ACL.getString(options)) - .setPredefinedDefaultObjectAcl(PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) + .setPredefinedDefaultObjectAcl(Option.PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -265,11 +249,11 @@ private Storage.Objects.Patch patchRequest(StorageObject storageObject, Map options) { try { storage.buckets() .delete(bucket.getName()) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) .execute(); return true; } catch (IOException ex) { @@ -309,10 +293,10 @@ private Storage.Objects.Delete deleteRequest(StorageObject blob, Map return storage.objects() .delete(blob.getBucket(), blob.getName()) .setGeneration(blob.getGeneration()) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)); } @Override @@ -339,8 +323,8 @@ public StorageObject compose(Iterable sources, StorageObject targ try { return storage.objects() .compose(target.getBucket(), target.getName(), request) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(targetOptions)) - .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(targetOptions)) + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(targetOptions)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(targetOptions)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -353,10 +337,10 @@ public byte[] load(StorageObject from, Map options) { Storage.Objects.Get getRequest = storage.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)); ByteArrayOutputStream out = new ByteArrayOutputStream(); getRequest.getMediaHttpDownloader().setDirectDownloadEnabled(true); getRequest.executeMediaAndDownloadTo(out); @@ -462,10 +446,10 @@ public Tuple read(StorageObject from, Map options, lo Get req = storage.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)); StringBuilder range = new StringBuilder(); range.append("bytes=").append(position).append("-").append(position + bytes - 1); req.getRequestHeaders().setRange(range.toString()); @@ -589,15 +573,15 @@ private RewriteResponse rewrite(RewriteRequest req, String token) { .setRewriteToken(token) .setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall) .setProjection(DEFAULT_PROJECTION) - .setIfSourceMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(req.sourceOptions)) + .setIfSourceMetagenerationMatch(Option.IF_SOURCE_METAGENERATION_MATCH.getLong(req.sourceOptions)) .setIfSourceMetagenerationNotMatch( - IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(req.sourceOptions)) - .setIfSourceGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(req.sourceOptions)) - .setIfSourceGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(req.sourceOptions)) - .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(req.targetOptions)) - .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(req.targetOptions)) - .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(req.targetOptions)) - .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(req.targetOptions)) + Option.IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(req.sourceOptions)) + .setIfSourceGenerationMatch(Option.IF_SOURCE_GENERATION_MATCH.getLong(req.sourceOptions)) + .setIfSourceGenerationNotMatch(Option.IF_SOURCE_GENERATION_NOT_MATCH.getLong(req.sourceOptions)) + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(req.targetOptions)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(req.targetOptions)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(req.targetOptions)) + .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(req.targetOptions)) .execute(); return new RewriteResponse( req, diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java similarity index 99% rename from gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java rename to gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java index ab4a7a9d0acb..d239a475a6dd 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.storage.spi; import static com.google.common.base.MoreObjects.firstNonNull; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpcFactory.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpcFactory.java similarity index 91% rename from gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpcFactory.java rename to gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpcFactory.java index f4959d617d17..19b98e6273db 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpcFactory.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpcFactory.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package com.google.gcloud.spi; +package com.google.gcloud.storage.spi; +import com.google.gcloud.spi.ServiceRpcFactory; import com.google.gcloud.storage.StorageOptions; /** diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java index 5dc947df51f8..1b0f36a864a2 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java @@ -30,8 +30,8 @@ import com.google.gcloud.ReadChannel; import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpcFactory; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpcFactory; import org.junit.After; import org.junit.Before; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelTest.java index e499f6b9de52..18ec64a9575f 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelTest.java @@ -34,8 +34,8 @@ import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpcFactory; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpcFactory; import org.easymock.Capture; import org.easymock.CaptureType; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java index 1b1ffd987de6..ad4a04c34127 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java @@ -27,10 +27,10 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpc.RewriteRequest; -import com.google.gcloud.spi.StorageRpc.RewriteResponse; -import com.google.gcloud.spi.StorageRpcFactory; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.RewriteRequest; +import com.google.gcloud.storage.spi.StorageRpc.RewriteResponse; +import com.google.gcloud.storage.spi.StorageRpcFactory; import org.easymock.EasyMock; import org.junit.After; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java index 2703ddb401c5..5924174ab138 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/OptionTest.java @@ -18,7 +18,7 @@ import static org.junit.Assert.assertEquals; -import com.google.gcloud.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc; import org.junit.Test; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index ac096375b120..7894f8fdee3c 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -26,7 +26,7 @@ import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc; import com.google.gcloud.storage.Acl.Project.ProjectRole; import org.junit.Test; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java index 4050e7d6267b..428f770ae381 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java @@ -37,9 +37,9 @@ import com.google.gcloud.RetryParams; import com.google.gcloud.ServiceOptions; import com.google.gcloud.WriteChannel; -import com.google.gcloud.spi.StorageRpc; -import com.google.gcloud.spi.StorageRpc.Tuple; -import com.google.gcloud.spi.StorageRpcFactory; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; +import com.google.gcloud.storage.spi.StorageRpcFactory; import com.google.gcloud.storage.Storage.CopyRequest; import org.easymock.Capture; From fd7d84d59c7dbff375268d07570dd46a45d4b2a0 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 10 Mar 2016 23:02:32 +0100 Subject: [PATCH 15/44] Reorder and add imports --- .../bigquery/spi/DefaultBigQueryRpc.java | 12 +- .../datastore/DatastoreOptionsTest.java | 2 +- .../gcloud/datastore/DatastoreTest.java | 2 +- .../examples/storage/StorageExample.java | 2 +- .../spi/DefaultResourceManagerRpc.java | 14 +- .../LocalResourceManagerHelperTest.java | 2 +- .../ResourceManagerImplTest.java | 2 +- .../java/com/google/gcloud/storage/Blob.java | 4 +- .../com/google/gcloud/storage/Bucket.java | 2 +- .../gcloud/storage/spi/DefaultStorageRpc.java | 132 ++++++++++-------- .../gcloud/storage/SerializationTest.java | 2 +- .../gcloud/storage/StorageImplTest.java | 2 +- 12 files changed, 101 insertions(+), 77 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/DefaultBigQueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/DefaultBigQueryRpc.java index f73517086a02..71712bda7806 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/DefaultBigQueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/spi/DefaultBigQueryRpc.java @@ -14,11 +14,14 @@ package com.google.gcloud.bigquery.spi; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.ALL_DATASETS; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.ALL_USERS; import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.DELETE_CONTENTS; import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.FIELDS; import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.MAX_RESULTS; import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.PAGE_TOKEN; import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.START_INDEX; +import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.STATE_FILTER; import static com.google.gcloud.bigquery.spi.BigQueryRpc.Option.TIMEOUT; import static java.net.HttpURLConnection.HTTP_CREATED; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; @@ -110,9 +113,10 @@ public Tuple> listDatasets(Map options) { try { DatasetList datasetsList = bigquery.datasets() .list(this.options.projectId()) - .setAll(Option.ALL_DATASETS.getBoolean(options)) + .setAll(ALL_DATASETS.getBoolean(options)) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) + .setPageToken(PAGE_TOKEN.getString(options)) .execute(); Iterable datasets = datasetsList.getDatasets(); return Tuple.of(datasetsList.getNextPageToken(), @@ -322,9 +326,9 @@ public Tuple> listJobs(Map options) { try { JobList jobsList = bigquery.jobs() .list(this.options.projectId()) - .setAllUsers(Option.ALL_USERS.getBoolean(options)) - .setFields(Option.FIELDS.getString(options)) - .setStateFilter(Option.STATE_FILTER.>get(options)) + .setAllUsers(ALL_USERS.getBoolean(options)) + .setFields(FIELDS.getString(options)) + .setStateFilter(STATE_FILTER.>get(options)) .setMaxResults(MAX_RESULTS.getLong(options)) .setPageToken(PAGE_TOKEN.getString(options)) .setProjection(DEFAULT_PROJECTION) diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index b0d6e8d7800b..1d188c7f4e94 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -22,9 +22,9 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import com.google.gcloud.datastore.testing.LocalGcdHelper; import com.google.gcloud.datastore.spi.DatastoreRpc; import com.google.gcloud.datastore.spi.DatastoreRpcFactory; +import com.google.gcloud.datastore.testing.LocalGcdHelper; import org.easymock.EasyMock; import org.junit.Before; diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 1886c67e22a8..e3829a2e71ce 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -38,9 +38,9 @@ import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.Projection; import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; -import com.google.gcloud.datastore.testing.LocalGcdHelper; import com.google.gcloud.datastore.spi.DatastoreRpc; import com.google.gcloud.datastore.spi.DatastoreRpcFactory; +import com.google.gcloud.datastore.testing.LocalGcdHelper; import com.google.protobuf.ByteString; import org.easymock.EasyMock; diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java index a10b5ef71817..a7260134202d 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java @@ -20,7 +20,6 @@ import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; import com.google.gcloud.ReadChannel; import com.google.gcloud.WriteChannel; -import com.google.gcloud.storage.spi.StorageRpc.Tuple; import com.google.gcloud.storage.Blob; import com.google.gcloud.storage.BlobId; import com.google.gcloud.storage.BlobInfo; @@ -31,6 +30,7 @@ import com.google.gcloud.storage.Storage.CopyRequest; import com.google.gcloud.storage.Storage.SignUrlOption; import com.google.gcloud.storage.StorageOptions; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; import java.io.FileOutputStream; import java.io.IOException; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java index 19e40698a847..2ef0d8c65ff2 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java @@ -1,5 +1,9 @@ package com.google.gcloud.resourcemanager.spi; +import static com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Option.FIELDS; +import static com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Option.FILTER; +import static com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Option.PAGE_SIZE; +import static com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Option.PAGE_TOKEN; import static java.net.HttpURLConnection.HTTP_FORBIDDEN; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; @@ -56,7 +60,7 @@ public Project get(String projectId, Map options) { try { return resourceManager.projects() .get(projectId) - .setFields(Option.FIELDS.getString(options)) + .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { ResourceManagerException translated = translate(ex); @@ -74,10 +78,10 @@ public Tuple> list(Map options) { try { ListProjectsResponse response = resourceManager.projects() .list() - .setFields(Option.FIELDS.getString(options)) - .setFilter(Option.FILTER.getString(options)) - .setPageSize(Option.PAGE_SIZE.getInt(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) + .setFields(FIELDS.getString(options)) + .setFilter(FILTER.getString(options)) + .setPageSize(PAGE_SIZE.getInt(options)) + .setPageToken(PAGE_TOKEN.getString(options)) .execute(); return Tuple.>of( response.getNextPageToken(), response.getProjects()); diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java index 328db315e414..c9b2970a4efa 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java @@ -8,10 +8,10 @@ import static org.junit.Assert.fail; import com.google.common.collect.ImmutableMap; -import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper; import com.google.gcloud.resourcemanager.spi.DefaultResourceManagerRpc; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Tuple; +import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper; import org.junit.AfterClass; import org.junit.Before; diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java index 92f5d2a18a0f..5b172d6a070e 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java @@ -30,9 +30,9 @@ import com.google.gcloud.resourcemanager.ResourceManager.ProjectField; import com.google.gcloud.resourcemanager.ResourceManager.ProjectGetOption; import com.google.gcloud.resourcemanager.ResourceManager.ProjectListOption; -import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpcFactory; +import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper; import org.easymock.EasyMock; import org.junit.AfterClass; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index 2cd81af5793d..4c8e935f7071 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -24,12 +24,12 @@ import com.google.common.base.Function; import com.google.gcloud.ReadChannel; import com.google.gcloud.WriteChannel; -import com.google.gcloud.storage.spi.StorageRpc; -import com.google.gcloud.storage.spi.StorageRpc.Tuple; import com.google.gcloud.storage.Storage.BlobTargetOption; import com.google.gcloud.storage.Storage.BlobWriteOption; import com.google.gcloud.storage.Storage.CopyRequest; import com.google.gcloud.storage.Storage.SignUrlOption; +import com.google.gcloud.storage.spi.StorageRpc; +import com.google.gcloud.storage.spi.StorageRpc.Tuple; import java.io.IOException; import java.io.ObjectInputStream; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java index 3cb8af4ef044..5df305ff371c 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java @@ -26,9 +26,9 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.gcloud.Page; -import com.google.gcloud.storage.spi.StorageRpc; import com.google.gcloud.storage.Storage.BlobGetOption; import com.google.gcloud.storage.Storage.BucketTargetOption; +import com.google.gcloud.storage.spi.StorageRpc; import java.io.IOException; import java.io.InputStream; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java index 71cd51da1e38..aa6085e161ed 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java @@ -15,6 +15,22 @@ package com.google.gcloud.storage.spi; import static com.google.common.base.MoreObjects.firstNonNull; +import static com.google.gcloud.storage.spi.StorageRpc.Option.DELIMITER; +import static com.google.gcloud.storage.spi.StorageRpc.Option.FIELDS; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_GENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_GENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_METAGENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_METAGENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_GENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH; +import static com.google.gcloud.storage.spi.StorageRpc.Option.MAX_RESULTS; +import static com.google.gcloud.storage.spi.StorageRpc.Option.PAGE_TOKEN; +import static com.google.gcloud.storage.spi.StorageRpc.Option.PREDEFINED_ACL; +import static com.google.gcloud.storage.spi.StorageRpc.Option.PREDEFINED_DEFAULT_OBJECT_ACL; +import static com.google.gcloud.storage.spi.StorageRpc.Option.PREFIX; +import static com.google.gcloud.storage.spi.StorageRpc.Option.VERSIONS; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; import static javax.servlet.http.HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE; @@ -92,8 +108,8 @@ public Bucket create(Bucket bucket, Map options) { return storage.buckets() .insert(this.options.projectId(), bucket) .setProjection(DEFAULT_PROJECTION) - .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) - .setPredefinedDefaultObjectAcl(Option.PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) + .setPredefinedAcl(PREDEFINED_ACL.getString(options)) + .setPredefinedDefaultObjectAcl(PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -109,11 +125,11 @@ public StorageObject create(StorageObject storageObject, final InputStream conte new InputStreamContent(storageObject.getContentType(), content)); insert.getMediaHttpUploader().setDirectUploadEnabled(true); return insert.setProjection(DEFAULT_PROJECTION) - .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) + .setPredefinedAcl(PREDEFINED_ACL.getString(options)) + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -126,10 +142,10 @@ public Tuple> list(Map options) { Buckets buckets = storage.buckets() .list(this.options.projectId()) .setProjection(DEFAULT_PROJECTION) - .setPrefix(Option.PREFIX.getString(options)) - .setMaxResults(Option.MAX_RESULTS.getLong(options)) - .setPageToken(Option.PAGE_TOKEN.getString(options)) - .setFields(Option.FIELDS.getString(options)) + .setPrefix(PREFIX.getString(options)) + .setMaxResults(MAX_RESULTS.getLong(options)) + .setPageToken(PAGE_TOKEN.getString(options)) + .setFields(FIELDS.getString(options)) .execute(); return Tuple.>of(buckets.getNextPageToken(), buckets.getItems()); } catch (IOException ex) { @@ -143,12 +159,12 @@ public Tuple> list(final String bucket, Map storageObjects = Iterables.concat( firstNonNull(objects.getItems(), ImmutableList.of()), @@ -180,9 +196,9 @@ public Bucket get(Bucket bucket, Map options) { return storage.buckets() .get(bucket.getName()) .setProjection(DEFAULT_PROJECTION) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setFields(Option.FIELDS.getString(options)) + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { StorageException serviceException = translate(ex); @@ -212,11 +228,11 @@ private Storage.Objects.Get getRequest(StorageObject object, Map opti .get(object.getBucket(), object.getName()) .setGeneration(object.getGeneration()) .setProjection(DEFAULT_PROJECTION) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) - .setFields(Option.FIELDS.getString(options)); + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)) + .setFields(FIELDS.getString(options)); } @Override @@ -225,10 +241,10 @@ public Bucket patch(Bucket bucket, Map options) { return storage.buckets() .patch(bucket.getName(), bucket) .setProjection(DEFAULT_PROJECTION) - .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) - .setPredefinedDefaultObjectAcl(Option.PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setPredefinedAcl(PREDEFINED_ACL.getString(options)) + .setPredefinedDefaultObjectAcl(PREDEFINED_DEFAULT_OBJECT_ACL.getString(options)) + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -249,11 +265,11 @@ private Storage.Objects.Patch patchRequest(StorageObject storageObject, Map options) { try { storage.buckets() .delete(bucket.getName()) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) .execute(); return true; } catch (IOException ex) { @@ -293,10 +309,10 @@ private Storage.Objects.Delete deleteRequest(StorageObject blob, Map return storage.objects() .delete(blob.getBucket(), blob.getName()) .setGeneration(blob.getGeneration()) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)); + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); } @Override @@ -323,8 +339,8 @@ public StorageObject compose(Iterable sources, StorageObject targ try { return storage.objects() .compose(target.getBucket(), target.getName(), request) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(targetOptions)) - .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(targetOptions)) + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(targetOptions)) + .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(targetOptions)) .execute(); } catch (IOException ex) { throw translate(ex); @@ -337,10 +353,10 @@ public byte[] load(StorageObject from, Map options) { Storage.Objects.Get getRequest = storage.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)); + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); ByteArrayOutputStream out = new ByteArrayOutputStream(); getRequest.getMediaHttpDownloader().setDirectDownloadEnabled(true); getRequest.executeMediaAndDownloadTo(out); @@ -446,10 +462,10 @@ public Tuple read(StorageObject from, Map options, lo Get req = storage.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) - .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) - .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)); + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)); StringBuilder range = new StringBuilder(); range.append("bytes=").append(position).append("-").append(position + bytes - 1); req.getRequestHeaders().setRange(range.toString()); @@ -573,15 +589,15 @@ private RewriteResponse rewrite(RewriteRequest req, String token) { .setRewriteToken(token) .setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall) .setProjection(DEFAULT_PROJECTION) - .setIfSourceMetagenerationMatch(Option.IF_SOURCE_METAGENERATION_MATCH.getLong(req.sourceOptions)) + .setIfSourceMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(req.sourceOptions)) .setIfSourceMetagenerationNotMatch( - Option.IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(req.sourceOptions)) - .setIfSourceGenerationMatch(Option.IF_SOURCE_GENERATION_MATCH.getLong(req.sourceOptions)) - .setIfSourceGenerationNotMatch(Option.IF_SOURCE_GENERATION_NOT_MATCH.getLong(req.sourceOptions)) - .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(req.targetOptions)) - .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(req.targetOptions)) - .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(req.targetOptions)) - .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(req.targetOptions)) + IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(req.sourceOptions)) + .setIfSourceGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(req.sourceOptions)) + .setIfSourceGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(req.sourceOptions)) + .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(req.targetOptions)) + .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(req.targetOptions)) + .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(req.targetOptions)) + .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(req.targetOptions)) .execute(); return new RewriteResponse( req, diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index 7894f8fdee3c..ad13b14ae4e2 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -26,8 +26,8 @@ import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; import com.google.gcloud.WriteChannel; -import com.google.gcloud.storage.spi.StorageRpc; import com.google.gcloud.storage.Acl.Project.ProjectRole; +import com.google.gcloud.storage.spi.StorageRpc; import org.junit.Test; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java index 428f770ae381..9a306b2b03c6 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java @@ -37,10 +37,10 @@ import com.google.gcloud.RetryParams; import com.google.gcloud.ServiceOptions; import com.google.gcloud.WriteChannel; +import com.google.gcloud.storage.Storage.CopyRequest; import com.google.gcloud.storage.spi.StorageRpc; import com.google.gcloud.storage.spi.StorageRpc.Tuple; import com.google.gcloud.storage.spi.StorageRpcFactory; -import com.google.gcloud.storage.Storage.CopyRequest; import org.easymock.Capture; import org.easymock.EasyMock; From a595f6a0db96e2c9d146a594021ba4d7eda43aff Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 10 Mar 2016 16:36:26 +0100 Subject: [PATCH 16/44] Use groups to separate packages in javadoc's index --- .../java/com/google/gcloud/package-info.java | 20 +++++++++++++++++++ pom.xml | 14 +++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 gcloud-java-core/src/main/java/com/google/gcloud/package-info.java diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/package-info.java b/gcloud-java-core/src/main/java/com/google/gcloud/package-info.java new file mode 100644 index 000000000000..215264675dc2 --- /dev/null +++ b/gcloud-java-core/src/main/java/com/google/gcloud/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2016 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. + */ + +/** + * Core classes for the {@code gcloud-java} library. + */ +package com.google.gcloud; \ No newline at end of file diff --git a/pom.xml b/pom.xml index b9d979c4d467..b6e8d3470edb 100644 --- a/pom.xml +++ b/pom.xml @@ -389,6 +389,20 @@ protected true ${project.build.directory}/javadoc + + + Main packages + com.google.gcloud* + + + SPI packages + com.google.gcloud.spi + + + Example packages + com.google.gcloud.examples* + + From 90d0917dde9acea5e85781b45b9d8bf210b90645 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 10 Mar 2016 18:50:34 +0100 Subject: [PATCH 17/44] Add group for testing packages --- .../src/main/java/com/google/gcloud/package-info.java | 2 +- pom.xml | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/package-info.java b/gcloud-java-core/src/main/java/com/google/gcloud/package-info.java index 215264675dc2..d527640c99f9 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/package-info.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/package-info.java @@ -17,4 +17,4 @@ /** * Core classes for the {@code gcloud-java} library. */ -package com.google.gcloud; \ No newline at end of file +package com.google.gcloud; diff --git a/pom.xml b/pom.xml index b6e8d3470edb..c0a0e9bfcffe 100644 --- a/pom.xml +++ b/pom.xml @@ -394,6 +394,10 @@ Main packages com.google.gcloud* + + Test helpers packages + com.google.gcloud.bigquery.testing:com.google.gcloud.datastore.testing:com.google.gcloud.resourcemanager.testing:com.google.gcloud.storage.testing + SPI packages com.google.gcloud.spi From 24a712ae2972b428987b8d1d6b74b7968e35dfc8 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 10 Mar 2016 20:14:00 +0100 Subject: [PATCH 18/44] Rename Main packages to API packages, reorder groups, add service's SPIs --- pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index c0a0e9bfcffe..b7766ac0e5c6 100644 --- a/pom.xml +++ b/pom.xml @@ -391,21 +391,21 @@ ${project.build.directory}/javadoc - Main packages + API packages com.google.gcloud* Test helpers packages com.google.gcloud.bigquery.testing:com.google.gcloud.datastore.testing:com.google.gcloud.resourcemanager.testing:com.google.gcloud.storage.testing - - SPI packages - com.google.gcloud.spi - Example packages com.google.gcloud.examples* + + SPI packages + com.google.gcloud.spi:com.google.gcloud.bigquery.spi:com.google.gcloud.datastore.spi:com.google.gcloud.resourcemanager.spi:com.google.gcloud.storage.spi + From ac1ba668cb933093235407d4eb04f4138e7fbf9c Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 11 Mar 2016 18:51:15 +0100 Subject: [PATCH 19/44] Add more detailed javadoc to Blob and Storage signUrl --- .../java/com/google/gcloud/storage/Blob.java | 30 ++++++++++++++++- .../com/google/gcloud/storage/Storage.java | 33 +++++++++++++++---- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index 4c8e935f7071..cb8b35a2ce88 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -22,6 +22,7 @@ import com.google.api.services.storage.model.StorageObject; import com.google.common.base.Function; +import com.google.gcloud.AuthCredentials; import com.google.gcloud.ReadChannel; import com.google.gcloud.WriteChannel; import com.google.gcloud.storage.Storage.BlobTargetOption; @@ -456,13 +457,40 @@ public WriteChannel writer(BlobWriteOption... options) { * Generates a signed URL for this blob. If you want to allow access to for a fixed amount of time * for this blob, you can use this method to generate a URL that is only valid within a certain * time period. This is particularly useful if you don't want publicly accessible blobs, but don't - * want to require users to explicitly log in. + * want to require users to explicitly log in. Signing a URL requires a service account + * and its associated key. If a {@link AuthCredentials.ServiceAccountAuthCredentials} was passed + * to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials + * are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then + * {@code signUrl} will use that service account and associated key to sign the URL. If this + * is not the case, a service account with associated key can be passed to {@code signUrl} using + * the {@link SignUrlOption#serviceAccount(AuthCredentials.ServiceAccountAuthCredentials)} option. + * + *

Example usage of creating a signed URL that is valid for 2 weeks: + *

 {@code
+   * blob.signUrl(14, TimeUnit.DAYS);
+   * }
+ * + *

Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()} + * option: + *

 {@code
+   * blob.signUrl(14, TimeUnit.DAYS, SignUrlOption.serviceAccount(
+   *     AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
+   * }
* * @param duration time until the signed URL expires, expressed in {@code unit}. The finer * granularity supported is 1 second, finer granularities will be truncated * @param unit time unit of the {@code duration} parameter * @param options optional URL signing options * @return a signed URL for this bucket and the specified options + * @throws IllegalArgumentException if + * {@link SignUrlOption#serviceAccount(AuthCredentials.ServiceAccountAuthCredentials)} was not + * used and no service account was provided to {@link StorageOptions} + * @throws IllegalArgumentException if the key associated to the provided service account is + * invalid + * @throws IllegalArgumentException if {@link SignUrlOption#withMd5()} option is used and + * {@link #md5()} is {@code null} + * @throws IllegalArgumentException if {@link SignUrlOption#withContentType()} option is used and + * {@link #contentType()} is {@code null} * @see Signed-URLs */ public URL signUrl(long duration, TimeUnit unit, SignUrlOption... options) { diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 6b2e9266f24b..f673a3ac5902 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -24,6 +24,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; import com.google.gcloud.Page; import com.google.gcloud.ReadChannel; @@ -1476,23 +1477,43 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx WriteChannel writer(BlobInfo blobInfo, BlobWriteOption... options); /** - * Generates a signed URL for a blob. - * If you have a blob that you want to allow access to for a fixed - * amount of time, you can use this method to generate a URL that - * is only valid within a certain time period. - * This is particularly useful if you don't want publicly - * accessible blobs, but don't want to require users to explicitly log in. + * Generates a signed URL for a blob. If you have a blob that you want to allow access to for a + * fixed amount of time, you can use this method to generate a URL that is only valid within a + * certain time period. This is particularly useful if you don't want publicly accessible blobs, + * but don't want to require users to explicitly log in. Signing a URL requires a service account + * and its associated key. If a {@link ServiceAccountAuthCredentials} was passed to + * {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials are + * being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then + * {@code signUrl} will use that service account and associated key to sign the URL. If this + * is not the case, a service account with associated key can be passed to {@code signUrl} using + * the {@code SignUrlOption.serviceAccount()} option. * *

Example usage of creating a signed URL that is valid for 2 weeks: *

 {@code
    * service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS);
    * }
* + *

Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()} + * option: + *

 {@code
+   * service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS,
+   *     SignUrlOption.serviceAccount(
+   *         AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
+   * }
+ * * @param blobInfo the blob associated with the signed URL * @param duration time until the signed URL expires, expressed in {@code unit}. The finest * granularity supported is 1 second, finer granularities will be truncated * @param unit time unit of the {@code duration} parameter * @param options optional URL signing options + * @throws IllegalArgumentException if {@code SignUrlOption.serviceAccount()} was not used and no + * service account was provided to {@link StorageOptions} + * @throws IllegalArgumentException if the key associated to the provided service account is + * invalid + * @throws IllegalArgumentException if {@code SignUrlOption.withMd5()} option is used and + * {@code blobInfo.md5()} is {@code null} + * @throws IllegalArgumentException if {@code SignUrlOption.withContentType()} option is used and + * {@code blobInfo.contentType()} is {@code null} * @see Signed-URLs */ URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOption... options); From 8a3b4f2194c6c3789e6dc4212beade7457382677 Mon Sep 17 00:00:00 2001 From: JP Martin Date: Fri, 11 Mar 2016 10:31:29 -0800 Subject: [PATCH 20/44] Remove final from Blob So we can mock it in test classes. Also make equals and hashCode final. --- .../src/main/java/com/google/gcloud/storage/Blob.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index 4c8e935f7071..0c92a1f50594 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -49,7 +49,7 @@ * {@link BlobInfo}. *

*/ -public final class Blob extends BlobInfo { +public class Blob extends BlobInfo { private static final long serialVersionUID = -6806832496717441434L; @@ -482,13 +482,13 @@ public Builder toBuilder() { } @Override - public boolean equals(Object obj) { + public final boolean equals(Object obj) { return obj instanceof Blob && Objects.equals(toPb(), ((Blob) obj).toPb()) && Objects.equals(options, ((Blob) obj).options); } @Override - public int hashCode() { + public final int hashCode() { return Objects.hash(super.hashCode(), options); } From ddb1adee1e560b4001d335e3d7804f5723140854 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Sun, 13 Mar 2016 12:13:43 +0100 Subject: [PATCH 21/44] Rephrase signUrl javadoc for better clarity --- .../java/com/google/gcloud/storage/Blob.java | 25 +++++++++++-------- .../com/google/gcloud/storage/Storage.java | 12 ++++++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index cb8b35a2ce88..c60a703eda91 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -454,16 +454,21 @@ public WriteChannel writer(BlobWriteOption... options) { } /** - * Generates a signed URL for this blob. If you want to allow access to for a fixed amount of time - * for this blob, you can use this method to generate a URL that is only valid within a certain - * time period. This is particularly useful if you don't want publicly accessible blobs, but don't - * want to require users to explicitly log in. Signing a URL requires a service account - * and its associated key. If a {@link AuthCredentials.ServiceAccountAuthCredentials} was passed - * to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials - * are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then - * {@code signUrl} will use that service account and associated key to sign the URL. If this - * is not the case, a service account with associated key can be passed to {@code signUrl} using - * the {@link SignUrlOption#serviceAccount(AuthCredentials.ServiceAccountAuthCredentials)} option. + * Generates a signed URL for this blob. If you want to allow access for a fixed amount of time to + * this blob, you can use this method to generate a URL that is only valid within a certain time + * period. This is particularly useful if you don't want publicly accessible blobs, but don't want + * to require users to explicitly log in. Signing a URL requires a service account + * and its associated private key. If a {@link AuthCredentials.ServiceAccountAuthCredentials} was + * passed to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default + * credentials are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} + * is set, then {@code signUrl} will use that service account and associated key to sign the URL. + * If the credentials passed to {@link StorageOptions} do not expose a private key (this is the + * case for App Engine credentials, Compute Engine credentials and Google Cloud SDK credentials) + * then {@code signUrl} will throw an {@link IllegalArgumentException} unless a service account + * with associated key is passed using the {@code SignUrlOption.serviceAccount()} option. The + * service account and private key passed with {@code SignUrlOption.serviceAccount()} have + * priority over any credentials set with + * {@link StorageOptions.Builder#authCredentials(AuthCredentials)}. * *

Example usage of creating a signed URL that is valid for 2 weeks: *

 {@code
diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
index f673a3ac5902..91f7578d7f89 100644
--- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
+++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
@@ -1481,12 +1481,16 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx
    * fixed amount of time, you can use this method to generate a URL that is only valid within a
    * certain time period. This is particularly useful if you don't want publicly accessible blobs,
    * but don't want to require users to explicitly log in. Signing a URL requires a service account
-   * and its associated key. If a {@link ServiceAccountAuthCredentials} was passed to
+   * and its associated private key. If a {@link ServiceAccountAuthCredentials} was passed to
    * {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials are
    * being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
-   * {@code signUrl} will use that service account and associated key to sign the URL. If this
-   * is not the case, a service account with associated key can be passed to {@code signUrl} using
-   * the {@code SignUrlOption.serviceAccount()} option.
+   * {@code signUrl} will use that service account and associated key to sign the URL. If the
+   * credentials passed to {@link StorageOptions} do not expose a private key (this is the case for
+   * App Engine credentials, Compute Engine credentials and Google Cloud SDK credentials) then
+   * {@code signUrl} will throw an {@link IllegalArgumentException} unless a service account with
+   * associated key is passed using the {@code SignUrlOption.serviceAccount()} option. The service
+   * account and private key passed with {@code SignUrlOption.serviceAccount()} have priority over
+   * any credentials set with {@link StorageOptions.Builder#authCredentials(AuthCredentials)}.
    *
    * 

Example usage of creating a signed URL that is valid for 2 weeks: *

 {@code

From d97c1887f4e24d1b9e03a3743d8481594041cede Mon Sep 17 00:00:00 2001
From: Marco Ziccardi 
Date: Mon, 14 Mar 2016 17:23:52 +0100
Subject: [PATCH 22/44] Rename maxResults to pageSize

---
 gcloud-java-bigquery/README.md                |  2 +-
 .../com/google/gcloud/bigquery/BigQuery.java  | 38 +++++++++----------
 .../google/gcloud/bigquery/QueryRequest.java  | 28 +++++++-------
 .../gcloud/bigquery/BigQueryImplTest.java     | 34 ++++++++---------
 .../google/gcloud/bigquery/DatasetTest.java   |  4 +-
 .../gcloud/bigquery/QueryRequestTest.java     | 10 ++---
 .../gcloud/bigquery/SerializationTest.java    |  4 +-
 .../com/google/gcloud/bigquery/TableTest.java |  4 +-
 .../gcloud/bigquery/it/ITBigQueryTest.java    |  6 +--
 .../snippets/InsertDataAndQueryTable.java     |  2 +-
 .../com/google/gcloud/storage/Storage.java    | 12 +++---
 .../gcloud/storage/SerializationTest.java     |  2 +-
 .../gcloud/storage/StorageImplTest.java       | 28 +++++++-------
 13 files changed, 87 insertions(+), 87 deletions(-)

diff --git a/gcloud-java-bigquery/README.md b/gcloud-java-bigquery/README.md
index 3387cd8c4f41..81b5db71bcac 100644
--- a/gcloud-java-bigquery/README.md
+++ b/gcloud-java-bigquery/README.md
@@ -185,7 +185,7 @@ Then add the following code to run the query and wait for the result:
 QueryRequest queryRequest =
     QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id")
         .maxWaitTime(60000L)
-        .maxResults(1000L)
+        .pageSize(1000L)
         .build();
 // Request query to be executed and wait for results
 QueryResponse queryResponse = bigquery.query(queryRequest);
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java
index 3acaacaf42e5..e06c8d86ee5f 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java
@@ -171,10 +171,10 @@ private DatasetListOption(BigQueryRpc.Option option, Object value) {
     }
 
     /**
-     * Returns an option to specify the maximum number of datasets to be returned.
+     * Returns an option to specify the maximum number of datasets returned per page.
      */
-    public static DatasetListOption maxResults(long maxResults) {
-      return new DatasetListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
+    public static DatasetListOption pageSize(long pageSize) {
+      return new DatasetListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
     }
 
     /**
@@ -246,11 +246,11 @@ private TableListOption(BigQueryRpc.Option option, Object value) {
     }
 
     /**
-     * Returns an option to specify the maximum number of tables to be returned.
+     * Returns an option to specify the maximum number of tables returned per page.
      */
-    public static TableListOption maxResults(long maxResults) {
-      checkArgument(maxResults >= 0);
-      return new TableListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
+    public static TableListOption pageSize(long pageSize) {
+      checkArgument(pageSize >= 0);
+      return new TableListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
     }
 
     /**
@@ -295,11 +295,11 @@ private TableDataListOption(BigQueryRpc.Option option, Object value) {
     }
 
     /**
-     * Returns an option to specify the maximum number of rows to be returned.
+     * Returns an option to specify the maximum number of rows returned per page.
      */
-    public static TableDataListOption maxResults(long maxResults) {
-      checkArgument(maxResults >= 0);
-      return new TableDataListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
+    public static TableDataListOption pageSize(long pageSize) {
+      checkArgument(pageSize >= 0);
+      return new TableDataListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
     }
 
     /**
@@ -352,11 +352,11 @@ public String apply(JobStatus.State state) {
     }
 
     /**
-     * Returns an option to specify the maximum number of jobs to be returned.
+     * Returns an option to specify the maximum number of jobs returned per page.
      */
-    public static JobListOption maxResults(long maxResults) {
-      checkArgument(maxResults >= 0);
-      return new JobListOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
+    public static JobListOption pageSize(long pageSize) {
+      checkArgument(pageSize >= 0);
+      return new JobListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
     }
 
     /**
@@ -418,11 +418,11 @@ private QueryResultsOption(BigQueryRpc.Option option, Object value) {
     }
 
     /**
-     * Returns an option to specify the maximum number of rows to be returned.
+     * Returns an option to specify the maximum number of rows returned per page.
      */
-    public static QueryResultsOption maxResults(long maxResults) {
-      checkArgument(maxResults >= 0);
-      return new QueryResultsOption(BigQueryRpc.Option.MAX_RESULTS, maxResults);
+    public static QueryResultsOption pageSize(long pageSize) {
+      checkArgument(pageSize >= 0);
+      return new QueryResultsOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
     }
 
     /**
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java
index 5f99f3c5b4ee..b3522a2a6ba3 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java
@@ -40,7 +40,7 @@
  * QueryRequest request = QueryRequest.builder("SELECT field FROM table")
  *     .defaultDataset(DatasetId.of("dataset"))
  *     .maxWaitTime(60000L)
- *     .maxResults(1000L)
+ *     .pageSize(1000L)
  *     .build();
  * QueryResponse response = bigquery.query(request);
  * while (!response.jobCompleted()) {
@@ -65,7 +65,7 @@ public class QueryRequest implements Serializable {
   private static final long serialVersionUID = -8727328332415880852L;
 
   private final String query;
-  private final Long maxResults;
+  private final Long pageSize;
   private final DatasetId defaultDataset;
   private final Long maxWaitTime;
   private final Boolean dryRun;
@@ -74,7 +74,7 @@ public class QueryRequest implements Serializable {
   public static final class Builder {
 
     private String query;
-    private Long maxResults;
+    private Long pageSize;
     private DatasetId defaultDataset;
     private Long maxWaitTime;
     private Boolean dryRun;
@@ -96,8 +96,8 @@ public Builder query(String query) {
      * query result set is large. In addition to this limit, responses are also limited to 10 MB.
      * By default, there is no maximum row count, and only the byte limit applies.
      */
-    public Builder maxResults(Long maxResults) {
-      this.maxResults = maxResults;
+    public Builder pageSize(Long pageSize) {
+      this.pageSize = pageSize;
       return this;
     }
 
@@ -157,7 +157,7 @@ public QueryRequest build() {
 
   private QueryRequest(Builder builder) {
     query = builder.query;
-    maxResults = builder.maxResults;
+    pageSize = builder.pageSize;
     defaultDataset = builder.defaultDataset;
     maxWaitTime = builder.maxWaitTime;
     dryRun = builder.dryRun;
@@ -174,8 +174,8 @@ public String query() {
   /**
    * Returns the maximum number of rows of data to return per page of results.
    */
-  public Long maxResults() {
-    return maxResults;
+  public Long pageSize() {
+    return pageSize;
   }
 
   /**
@@ -224,7 +224,7 @@ public Boolean useQueryCache() {
   public Builder toBuilder() {
     return new Builder()
         .query(query)
-        .maxResults(maxResults)
+        .pageSize(pageSize)
         .defaultDataset(defaultDataset)
         .maxWaitTime(maxWaitTime)
         .dryRun(dryRun)
@@ -235,7 +235,7 @@ public Builder toBuilder() {
   public String toString() {
     return MoreObjects.toStringHelper(this)
         .add("query", query)
-        .add("maxResults", maxResults)
+        .add("pageSize", pageSize)
         .add("defaultDataset", defaultDataset)
         .add("maxWaitTime", maxWaitTime)
         .add("dryRun", dryRun)
@@ -245,7 +245,7 @@ public String toString() {
 
   @Override
   public int hashCode() {
-    return Objects.hash(query, maxResults, defaultDataset, maxWaitTime, dryRun, useQueryCache);
+    return Objects.hash(query, pageSize, defaultDataset, maxWaitTime, dryRun, useQueryCache);
   }
 
   @Override
@@ -264,8 +264,8 @@ QueryRequest setProjectId(String projectId) {
   com.google.api.services.bigquery.model.QueryRequest toPb() {
     com.google.api.services.bigquery.model.QueryRequest queryRequestPb =
         new com.google.api.services.bigquery.model.QueryRequest().setQuery(query);
-    if (maxResults != null) {
-      queryRequestPb.setMaxResults(maxResults);
+    if (pageSize != null) {
+      queryRequestPb.setMaxResults(pageSize);
     }
     if (defaultDataset != null) {
       queryRequestPb.setDefaultDataset(defaultDataset.toPb());
@@ -299,7 +299,7 @@ public static QueryRequest of(String query) {
   static QueryRequest fromPb(com.google.api.services.bigquery.model.QueryRequest queryRequestPb) {
     Builder builder = builder(queryRequestPb.getQuery());
     if (queryRequestPb.getMaxResults() != null) {
-      builder.maxResults(queryRequestPb.getMaxResults());
+      builder.pageSize(queryRequestPb.getMaxResults());
     }
     if (queryRequestPb.getDefaultDataset() != null) {
       builder.defaultDataset(DatasetId.fromPb(queryRequestPb.getDefaultDataset()));
diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java
index 305745e72da9..b398f238386a 100644
--- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java
+++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java
@@ -148,12 +148,12 @@ public class BigQueryImplTest {
   private static final TableRow TABLE_ROW =
       new TableRow().setF(ImmutableList.of(BOOLEAN_FIELD, INTEGER_FIELD));
   private static final QueryRequest QUERY_REQUEST = QueryRequest.builder("SQL")
-      .maxResults(42L)
+      .pageSize(42L)
       .useQueryCache(false)
       .defaultDataset(DatasetId.of(DATASET))
       .build();
   private static final QueryRequest QUERY_REQUEST_WITH_PROJECT = QueryRequest.builder("SQL")
-      .maxResults(42L)
+      .pageSize(42L)
       .useQueryCache(false)
       .defaultDataset(DatasetId.of(PROJECT, DATASET))
       .build();
@@ -170,8 +170,8 @@ public class BigQueryImplTest {
       BigQuery.DatasetListOption.all();
   private static final BigQuery.DatasetListOption DATASET_LIST_PAGE_TOKEN =
       BigQuery.DatasetListOption.startPageToken("cursor");
-  private static final BigQuery.DatasetListOption DATASET_LIST_MAX_RESULTS =
-      BigQuery.DatasetListOption.maxResults(42L);
+  private static final BigQuery.DatasetListOption DATASET_LIST_PAGE_SIZE =
+      BigQuery.DatasetListOption.pageSize(42L);
   private static final Map DATASET_LIST_OPTIONS = ImmutableMap.of(
       BigQueryRpc.Option.ALL_DATASETS, true,
       BigQueryRpc.Option.PAGE_TOKEN, "cursor",
@@ -188,8 +188,8 @@ public class BigQueryImplTest {
       BigQuery.TableOption.fields(BigQuery.TableField.SCHEMA, BigQuery.TableField.ETAG);
 
   // Table list options
-  private static final BigQuery.TableListOption TABLE_LIST_MAX_RESULTS =
-      BigQuery.TableListOption.maxResults(42L);
+  private static final BigQuery.TableListOption TABLE_LIST_PAGE_SIZE =
+      BigQuery.TableListOption.pageSize(42L);
   private static final BigQuery.TableListOption TABLE_LIST_PAGE_TOKEN =
       BigQuery.TableListOption.startPageToken("cursor");
   private static final Map TABLE_LIST_OPTIONS = ImmutableMap.of(
@@ -197,8 +197,8 @@ public class BigQueryImplTest {
       BigQueryRpc.Option.PAGE_TOKEN, "cursor");
 
   // TableData list options
-  private static final BigQuery.TableDataListOption TABLE_DATA_LIST_MAX_RESULTS =
-      BigQuery.TableDataListOption.maxResults(42L);
+  private static final BigQuery.TableDataListOption TABLE_DATA_LIST_PAGE_SIZE =
+      BigQuery.TableDataListOption.pageSize(42L);
   private static final BigQuery.TableDataListOption TABLE_DATA_LIST_PAGE_TOKEN =
       BigQuery.TableDataListOption.startPageToken("cursor");
   private static final BigQuery.TableDataListOption TABLE_DATA_LIST_START_INDEX =
@@ -221,8 +221,8 @@ public class BigQueryImplTest {
       BigQuery.JobListOption.stateFilter(JobStatus.State.DONE, JobStatus.State.PENDING);
   private static final BigQuery.JobListOption JOB_LIST_PAGE_TOKEN =
       BigQuery.JobListOption.startPageToken("cursor");
-  private static final BigQuery.JobListOption JOB_LIST_MAX_RESULTS =
-      BigQuery.JobListOption.maxResults(42L);
+  private static final BigQuery.JobListOption JOB_LIST_PAGE_SIZE =
+      BigQuery.JobListOption.pageSize(42L);
   private static final Map JOB_LIST_OPTIONS = ImmutableMap.of(
       BigQueryRpc.Option.ALL_USERS, true,
       BigQueryRpc.Option.STATE_FILTER, ImmutableList.of("done", "pending"),
@@ -236,8 +236,8 @@ public class BigQueryImplTest {
       BigQuery.QueryResultsOption.startIndex(1024L);
   private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_PAGE_TOKEN =
       BigQuery.QueryResultsOption.startPageToken("cursor");
-  private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_MAX_RESULTS =
-      BigQuery.QueryResultsOption.maxResults(0L);
+  private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_PAGE_SIZE =
+      BigQuery.QueryResultsOption.pageSize(0L);
   private static final Map QUERY_RESULTS_OPTIONS = ImmutableMap.of(
       BigQueryRpc.Option.TIMEOUT, 42L,
       BigQueryRpc.Option.START_INDEX, 1024L,
@@ -388,7 +388,7 @@ public void testListDatasetsWithOptions() {
     EasyMock.expect(bigqueryRpcMock.listDatasets(DATASET_LIST_OPTIONS)).andReturn(result);
     EasyMock.replay(bigqueryRpcMock);
     Page page = bigquery.listDatasets(DATASET_LIST_ALL, DATASET_LIST_PAGE_TOKEN,
-        DATASET_LIST_MAX_RESULTS);
+        DATASET_LIST_PAGE_SIZE);
     assertEquals(cursor, page.nextPageCursor());
     assertArrayEquals(datasetList.toArray(), Iterables.toArray(page.values(), DatasetInfo.class));
   }
@@ -560,7 +560,7 @@ public void testListTablesWithOptions() {
         Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION));
     EasyMock.expect(bigqueryRpcMock.listTables(DATASET, TABLE_LIST_OPTIONS)).andReturn(result);
     EasyMock.replay(bigqueryRpcMock);
-    Page
page = bigquery.listTables(DATASET, TABLE_LIST_MAX_RESULTS, TABLE_LIST_PAGE_TOKEN); + Page
page = bigquery.listTables(DATASET, TABLE_LIST_PAGE_SIZE, TABLE_LIST_PAGE_TOKEN); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), Table.class)); } @@ -733,7 +733,7 @@ public void testListTableDataWithOptions() { EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); Page> page = bigquery.listTableData(DATASET, TABLE, - TABLE_DATA_LIST_MAX_RESULTS, TABLE_DATA_LIST_PAGE_TOKEN, TABLE_DATA_LIST_START_INDEX); + TABLE_DATA_LIST_PAGE_SIZE, TABLE_DATA_LIST_PAGE_TOKEN, TABLE_DATA_LIST_START_INDEX); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(tableData.toArray(), Iterables.toArray(page.values(), List.class)); } @@ -859,7 +859,7 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { EasyMock.expect(bigqueryRpcMock.listJobs(JOB_LIST_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listJobs(JOB_LIST_ALL_USERS, JOB_LIST_STATE_FILTER, - JOB_LIST_PAGE_TOKEN, JOB_LIST_MAX_RESULTS); + JOB_LIST_PAGE_TOKEN, JOB_LIST_PAGE_SIZE); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), Job.class)); } @@ -1012,7 +1012,7 @@ public void testGetQueryResultsWithOptions() { EasyMock.replay(bigqueryRpcMock); bigquery = options.service(); QueryResponse response = bigquery.getQueryResults(queryJob, QUERY_RESULTS_OPTION_TIME, - QUERY_RESULTS_OPTION_INDEX, QUERY_RESULTS_OPTION_MAX_RESULTS, + QUERY_RESULTS_OPTION_INDEX, QUERY_RESULTS_OPTION_PAGE_SIZE, QUERY_RESULTS_OPTION_PAGE_TOKEN); assertEquals(queryJob, response.jobId()); assertEquals(true, response.jobCompleted()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java index 373291021b23..dd03b7899ebc 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java @@ -260,11 +260,11 @@ public void testListWithOptions() throws Exception { new Table(serviceMockReturnsOptions, new Table.BuilderImpl(TABLE_INFO3))); PageImpl
expectedPage = new PageImpl<>(null, "c", tableResults); expect(bigquery.options()).andReturn(mockOptions); - expect(bigquery.listTables(DATASET_INFO.datasetId(), BigQuery.TableListOption.maxResults(10L))) + expect(bigquery.listTables(DATASET_INFO.datasetId(), BigQuery.TableListOption.pageSize(10L))) .andReturn(expectedPage); replay(bigquery); initializeDataset(); - Page
tablePage = dataset.list(BigQuery.TableListOption.maxResults(10L)); + Page
tablePage = dataset.list(BigQuery.TableListOption.pageSize(10L)); assertArrayEquals(tableResults.toArray(), Iterables.toArray(tablePage.values(), Table.class)); assertEquals(expectedPage.nextPageCursor(), tablePage.nextPageCursor()); } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryRequestTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryRequestTest.java index 370b4d614cbf..7875dee9e315 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryRequestTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryRequestTest.java @@ -29,13 +29,13 @@ public class QueryRequestTest { private static final DatasetId DATASET_ID = DatasetId.of("dataset"); private static final Boolean USE_QUERY_CACHE = true; private static final Boolean DRY_RUN = false; - private static final Long MAX_RESULTS = 42L; + private static final Long PAGE_SIZE = 42L; private static final Long MAX_WAIT_TIME = 42000L; private static final QueryRequest QUERY_REQUEST = QueryRequest.builder(QUERY) .useQueryCache(USE_QUERY_CACHE) .defaultDataset(DATASET_ID) .dryRun(DRY_RUN) - .maxResults(MAX_RESULTS) + .pageSize(PAGE_SIZE) .maxWaitTime(MAX_WAIT_TIME) .build(); @@ -65,7 +65,7 @@ public void testBuilder() { assertEquals(USE_QUERY_CACHE, QUERY_REQUEST.useQueryCache()); assertEquals(DATASET_ID, QUERY_REQUEST.defaultDataset()); assertEquals(DRY_RUN, QUERY_REQUEST.dryRun()); - assertEquals(MAX_RESULTS, QUERY_REQUEST.maxResults()); + assertEquals(PAGE_SIZE, QUERY_REQUEST.pageSize()); assertEquals(MAX_WAIT_TIME, QUERY_REQUEST.maxWaitTime()); thrown.expect(NullPointerException.class); QueryRequest.builder(null); @@ -78,7 +78,7 @@ public void testOf() { assertNull(request.useQueryCache()); assertNull(request.defaultDataset()); assertNull(request.dryRun()); - assertNull(request.maxResults()); + assertNull(request.pageSize()); assertNull(request.maxWaitTime()); thrown.expect(NullPointerException.class); QueryRequest.of(null); @@ -102,7 +102,7 @@ private void compareQueryRequest(QueryRequest expected, QueryRequest value) { assertEquals(expected.useQueryCache(), value.useQueryCache()); assertEquals(expected.defaultDataset(), value.defaultDataset()); assertEquals(expected.dryRun(), value.dryRun()); - assertEquals(expected.maxResults(), value.maxResults()); + assertEquals(expected.pageSize(), value.pageSize()); assertEquals(expected.maxWaitTime(), value.maxWaitTime()); } } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index d877bff2138c..254c8954bf30 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -207,7 +207,7 @@ public class SerializationTest { .useQueryCache(true) .defaultDataset(DATASET_ID) .dryRun(false) - .maxResults(42L) + .pageSize(42L) .maxWaitTime(10L) .build(); private static final QueryResult QUERY_RESULT = QueryResult.builder() @@ -261,7 +261,7 @@ public void testModelAndRequests() throws Exception { INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(), BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), - BigQuery.TableListOption.maxResults(42L), BigQuery.JobOption.fields(), + BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(), BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB}; for (Serializable obj : objects) { Object copy = serializeAndDeserialize(obj); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java index 4866ee9ab8ec..c7828ebeadf4 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java @@ -286,11 +286,11 @@ public void testListWithOptions() throws Exception { initializeExpectedTable(1); expect(bigquery.options()).andReturn(mockOptions); PageImpl> tableDataPage = new PageImpl<>(null, "c", ROWS); - expect(bigquery.listTableData(TABLE_ID1, BigQuery.TableDataListOption.maxResults(10L))) + expect(bigquery.listTableData(TABLE_ID1, BigQuery.TableDataListOption.pageSize(10L))) .andReturn(tableDataPage); replay(bigquery); initializeTable(); - Page> dataPage = table.list(BigQuery.TableDataListOption.maxResults(10L)); + Page> dataPage = table.list(BigQuery.TableDataListOption.pageSize(10L)); Iterator> tableDataIterator = tableDataPage.values().iterator(); Iterator> dataIterator = dataPage.values().iterator(); assertTrue(Iterators.elementsEqual(tableDataIterator, dataIterator)); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java index 63a0551ece33..50780b4fc9a9 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java @@ -348,7 +348,7 @@ public void testCreateExternalTable() throws InterruptedException { + tableName) .defaultDataset(DatasetId.of(DATASET)) .maxWaitTime(60000L) - .maxResults(1000L) + .pageSize(1000L) .build(); QueryResponse response = bigquery.query(request); while (!response.jobCompleted()) { @@ -411,7 +411,7 @@ public void testCreateViewTable() throws InterruptedException { QueryRequest request = QueryRequest.builder("SELECT * FROM " + tableName) .defaultDataset(DatasetId.of(DATASET)) .maxWaitTime(60000L) - .maxResults(1000L) + .pageSize(1000L) .build(); QueryResponse response = bigquery.query(request); while (!response.jobCompleted()) { @@ -662,7 +662,7 @@ public void testQuery() throws InterruptedException { QueryRequest request = QueryRequest.builder(query) .defaultDataset(DatasetId.of(DATASET)) .maxWaitTime(60000L) - .maxResults(1000L) + .pageSize(1000L) .build(); QueryResponse response = bigquery.query(request); while (!response.jobCompleted()) { diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java index f421bc832441..ba2d1291b229 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java @@ -84,7 +84,7 @@ public static void main(String... args) throws InterruptedException { // Create a query request QueryRequest queryRequest = QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id") .maxWaitTime(60000L) - .maxResults(1000L) + .pageSize(1000L) .build(); // Request query to be executed and wait for results QueryResponse queryResponse = bigquery.query(queryRequest); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 6b2e9266f24b..c098c7ddc5d3 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -626,10 +626,10 @@ private BucketListOption(StorageRpc.Option option, Object value) { } /** - * Returns an option to specify the maximum number of buckets to be returned. + * Returns an option to specify the maximum number of buckets returned per page. */ - public static BucketListOption maxResults(long maxResults) { - return new BucketListOption(StorageRpc.Option.MAX_RESULTS, maxResults); + public static BucketListOption pageSize(long pageSize) { + return new BucketListOption(StorageRpc.Option.MAX_RESULTS, pageSize); } /** @@ -672,10 +672,10 @@ private BlobListOption(StorageRpc.Option option, Object value) { } /** - * Returns an option to specify the maximum number of blobs to be returned. + * Returns an option to specify the maximum number of blobs returned per page. */ - public static BlobListOption maxResults(long maxResults) { - return new BlobListOption(StorageRpc.Option.MAX_RESULTS, maxResults); + public static BlobListOption pageSize(long pageSize) { + return new BlobListOption(StorageRpc.Option.MAX_RESULTS, pageSize); } /** diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index ad13b14ae4e2..efa56d9e39b2 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -64,7 +64,7 @@ public class SerializationTest { private static final PageImpl PAGE_RESULT = new PageImpl<>(null, "c", Collections.singletonList(BLOB)); private static final Storage.BlobListOption BLOB_LIST_OPTIONS = - Storage.BlobListOption.maxResults(100); + Storage.BlobListOption.pageSize(100); private static final Storage.BlobSourceOption BLOB_SOURCE_OPTIONS = Storage.BlobSourceOption.generationMatch(1); private static final Storage.BlobTargetOption BLOB_TARGET_OPTIONS = diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java index 9a306b2b03c6..38b4bb58e77f 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java @@ -181,8 +181,8 @@ public class StorageImplTest { StorageRpc.Option.IF_SOURCE_GENERATION_MATCH, BLOB_SOURCE_GENERATION.value()); // Bucket list options - private static final Storage.BucketListOption BUCKET_LIST_MAX_RESULT = - Storage.BucketListOption.maxResults(42L); + private static final Storage.BucketListOption BUCKET_LIST_PAGE_SIZE = + Storage.BucketListOption.pageSize(42L); private static final Storage.BucketListOption BUCKET_LIST_PREFIX = Storage.BucketListOption.prefix("prefix"); private static final Storage.BucketListOption BUCKET_LIST_FIELDS = @@ -190,12 +190,12 @@ public class StorageImplTest { private static final Storage.BucketListOption BUCKET_LIST_EMPTY_FIELDS = Storage.BucketListOption.fields(); private static final Map BUCKET_LIST_OPTIONS = ImmutableMap.of( - StorageRpc.Option.MAX_RESULTS, BUCKET_LIST_MAX_RESULT.value(), + StorageRpc.Option.MAX_RESULTS, BUCKET_LIST_PAGE_SIZE.value(), StorageRpc.Option.PREFIX, BUCKET_LIST_PREFIX.value()); // Blob list options - private static final Storage.BlobListOption BLOB_LIST_MAX_RESULT = - Storage.BlobListOption.maxResults(42L); + private static final Storage.BlobListOption BLOB_LIST_PAGE_SIZE = + Storage.BlobListOption.pageSize(42L); private static final Storage.BlobListOption BLOB_LIST_PREFIX = Storage.BlobListOption.prefix("prefix"); private static final Storage.BlobListOption BLOB_LIST_FIELDS = @@ -205,7 +205,7 @@ public class StorageImplTest { private static final Storage.BlobListOption BLOB_LIST_EMPTY_FIELDS = Storage.BlobListOption.fields(); private static final Map BLOB_LIST_OPTIONS = ImmutableMap.of( - StorageRpc.Option.MAX_RESULTS, BLOB_LIST_MAX_RESULT.value(), + StorageRpc.Option.MAX_RESULTS, BLOB_LIST_PAGE_SIZE.value(), StorageRpc.Option.PREFIX, BLOB_LIST_PREFIX.value(), StorageRpc.Option.VERSIONS, BLOB_LIST_VERSIONS.value()); @@ -567,7 +567,7 @@ public void testListBucketsWithOptions() { EasyMock.replay(storageRpcMock); initializeService(); ImmutableList bucketList = ImmutableList.of(expectedBucket1, expectedBucket2); - Page page = storage.list(BUCKET_LIST_MAX_RESULT, BUCKET_LIST_PREFIX); + Page page = storage.list(BUCKET_LIST_PAGE_SIZE, BUCKET_LIST_PREFIX); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class)); } @@ -654,7 +654,7 @@ public void testListBlobsWithOptions() { initializeService(); ImmutableList blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page page = - storage.list(BUCKET_NAME1, BLOB_LIST_MAX_RESULT, BLOB_LIST_PREFIX, BLOB_LIST_VERSIONS); + storage.list(BUCKET_NAME1, BLOB_LIST_PAGE_SIZE, BLOB_LIST_PREFIX, BLOB_LIST_VERSIONS); assertEquals(cursor, page.nextPageCursor()); assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class)); } @@ -673,9 +673,9 @@ public void testListBlobsWithSelectedFields() { initializeService(); ImmutableList blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page page = - storage.list(BUCKET_NAME1, BLOB_LIST_MAX_RESULT, BLOB_LIST_PREFIX, BLOB_LIST_FIELDS); - assertEquals(BLOB_LIST_MAX_RESULT.value(), - capturedOptions.getValue().get(BLOB_LIST_MAX_RESULT.rpcOption())); + storage.list(BUCKET_NAME1, BLOB_LIST_PAGE_SIZE, BLOB_LIST_PREFIX, BLOB_LIST_FIELDS); + assertEquals(BLOB_LIST_PAGE_SIZE.value(), + capturedOptions.getValue().get(BLOB_LIST_PAGE_SIZE.rpcOption())); assertEquals(BLOB_LIST_PREFIX.value(), capturedOptions.getValue().get(BLOB_LIST_PREFIX.rpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption()); @@ -704,9 +704,9 @@ public void testListBlobsWithEmptyFields() { initializeService(); ImmutableList blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page page = - storage.list(BUCKET_NAME1, BLOB_LIST_MAX_RESULT, BLOB_LIST_PREFIX, BLOB_LIST_EMPTY_FIELDS); - assertEquals(BLOB_LIST_MAX_RESULT.value(), - capturedOptions.getValue().get(BLOB_LIST_MAX_RESULT.rpcOption())); + storage.list(BUCKET_NAME1, BLOB_LIST_PAGE_SIZE, BLOB_LIST_PREFIX, BLOB_LIST_EMPTY_FIELDS); + assertEquals(BLOB_LIST_PAGE_SIZE.value(), + capturedOptions.getValue().get(BLOB_LIST_PAGE_SIZE.rpcOption())); assertEquals(BLOB_LIST_PREFIX.value(), capturedOptions.getValue().get(BLOB_LIST_PREFIX.rpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_LIST_EMPTY_FIELDS.rpcOption()); From 86e23d55bc9cec8a2bd626d143882a998d1a165a Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 14 Mar 2016 17:59:18 +0100 Subject: [PATCH 23/44] Fix flaky RemoteGcsHelperTest.testForceDeleteTimeout --- .../java/com/google/gcloud/storage/RemoteGcsHelperTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelperTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelperTest.java index 154554a029fe..146922a9dae9 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelperTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelperTest.java @@ -132,7 +132,8 @@ public void testForceDelete() throws InterruptedException, ExecutionException { @Test public void testForceDeleteTimeout() throws InterruptedException, ExecutionException { Storage storageMock = EasyMock.createMock(Storage.class); - EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage).anyTimes(); + EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))) + .andReturn(blobPage).anyTimes(); for (BlobInfo info : blobList) { EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true).anyTimes(); } From 4e0248f951ddc61ae2a5cdb82d1f953f3bba17cb Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 15 Mar 2016 14:33:27 +0100 Subject: [PATCH 24/44] Add better javadoc for signUrl examples --- .../java/com/google/gcloud/storage/Blob.java | 30 +++++++++---------- .../com/google/gcloud/storage/Storage.java | 13 ++++---- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java index c60a703eda91..b4fc892d3df5 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java @@ -456,27 +456,27 @@ public WriteChannel writer(BlobWriteOption... options) { /** * Generates a signed URL for this blob. If you want to allow access for a fixed amount of time to * this blob, you can use this method to generate a URL that is only valid within a certain time - * period. This is particularly useful if you don't want publicly accessible blobs, but don't want - * to require users to explicitly log in. Signing a URL requires a service account - * and its associated private key. If a {@link AuthCredentials.ServiceAccountAuthCredentials} was - * passed to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default - * credentials are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} - * is set, then {@code signUrl} will use that service account and associated key to sign the URL. - * If the credentials passed to {@link StorageOptions} do not expose a private key (this is the - * case for App Engine credentials, Compute Engine credentials and Google Cloud SDK credentials) - * then {@code signUrl} will throw an {@link IllegalArgumentException} unless a service account - * with associated key is passed using the {@code SignUrlOption.serviceAccount()} option. The - * service account and private key passed with {@code SignUrlOption.serviceAccount()} have - * priority over any credentials set with - * {@link StorageOptions.Builder#authCredentials(AuthCredentials)}. + * period. This is particularly useful if you don't want publicly accessible blobs, but also don't + * want to require users to explicitly log in. Signing a URL requires a service account and its + * associated private key. If a {@link AuthCredentials.ServiceAccountAuthCredentials} was passed + * to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials + * are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then + * {@code signUrl} will use that service account and associated key to sign the URL. If the + * credentials passed to {@link StorageOptions} do not expose a private key (this is the case for + * App Engine credentials, Compute Engine credentials and Google Cloud SDK credentials) then + * {@code signUrl} will throw an {@link IllegalArgumentException} unless a service account with + * associated key is passed using the {@code SignUrlOption.serviceAccount()} option. The service + * account and private key passed with {@code SignUrlOption.serviceAccount()} have priority over + * any credentials set with {@link StorageOptions.Builder#authCredentials(AuthCredentials)}. * - *

Example usage of creating a signed URL that is valid for 2 weeks: + *

Example usage of creating a signed URL that is valid for 2 weeks, using the default + * credentials for signing the URL: *

 {@code
    * blob.signUrl(14, TimeUnit.DAYS);
    * }
* *

Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()} - * option: + * option, that will be used for signing the URL: *

 {@code
    * blob.signUrl(14, TimeUnit.DAYS, SignUrlOption.serviceAccount(
    *     AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
index 91f7578d7f89..7a58878469f1 100644
--- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
+++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
@@ -1480,10 +1480,10 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx
    * Generates a signed URL for a blob. If you have a blob that you want to allow access to for a
    * fixed amount of time, you can use this method to generate a URL that is only valid within a
    * certain time period. This is particularly useful if you don't want publicly accessible blobs,
-   * but don't want to require users to explicitly log in. Signing a URL requires a service account
-   * and its associated private key. If a {@link ServiceAccountAuthCredentials} was passed to
-   * {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials are
-   * being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
+   * but also don't want to require users to explicitly log in. Signing a URL requires a service
+   * account and its associated private key. If a {@link ServiceAccountAuthCredentials} was passed
+   * to {@link StorageOptions.Builder#authCredentials(AuthCredentials)} or the default credentials
+   * are being used and the environment variable {@code GOOGLE_APPLICATION_CREDENTIALS} is set, then
    * {@code signUrl} will use that service account and associated key to sign the URL. If the
    * credentials passed to {@link StorageOptions} do not expose a private key (this is the case for
    * App Engine credentials, Compute Engine credentials and Google Cloud SDK credentials) then
@@ -1492,13 +1492,14 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx
    * account and private key passed with {@code SignUrlOption.serviceAccount()} have priority over
    * any credentials set with {@link StorageOptions.Builder#authCredentials(AuthCredentials)}.
    *
-   * 

Example usage of creating a signed URL that is valid for 2 weeks: + *

Example usage of creating a signed URL that is valid for 2 weeks, using the default + * credentials for signing the URL: *

 {@code
    * service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS);
    * }
* *

Example usage of creating a signed URL passing the {@code SignUrlOption.serviceAccount()} - * option: + * option, that will be used for signing the URL: *

 {@code
    * service.signUrl(BlobInfo.builder("bucket", "name").build(), 14, TimeUnit.DAYS,
    *     SignUrlOption.serviceAccount(

From c2c662843263d53d702f88eb045bffa380697293 Mon Sep 17 00:00:00 2001
From: Ajay Kannan 
Date: Tue, 1 Mar 2016 10:02:57 -0800
Subject: [PATCH 25/44] Add get, replace, and test for IAM

---
 .../main/java/com/google/gcloud/Identity.java |  24 +--
 .../java/com/google/gcloud/IdentityTest.java  |  14 +-
 .../google/gcloud/resourcemanager/Policy.java | 102 +++++++++---
 .../gcloud/resourcemanager/Project.java       |  18 +-
 .../resourcemanager/ResourceManager.java      | 157 ++++++++++++++++--
 .../resourcemanager/ResourceManagerImpl.java  |  84 ++++++++--
 .../spi/DefaultResourceManagerRpc.java        |  58 ++++++-
 .../spi/ResourceManagerRpc.java               |  24 +++
 .../testing/LocalResourceManagerHelper.java   | 131 +++++++++++++--
 .../LocalResourceManagerHelperTest.java       |  82 ++++++++-
 .../gcloud/resourcemanager/PolicyTest.java    |  30 +++-
 .../gcloud/resourcemanager/ProjectTest.java   |  26 ++-
 .../ResourceManagerImplTest.java              |  64 +++++++
 .../resourcemanager/SerializationTest.java    |   2 +-
 14 files changed, 718 insertions(+), 98 deletions(-)

diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/Identity.java b/gcloud-java-core/src/main/java/com/google/gcloud/Identity.java
index d1644198f759..687a76ffc42c 100644
--- a/gcloud-java-core/src/main/java/com/google/gcloud/Identity.java
+++ b/gcloud-java-core/src/main/java/com/google/gcloud/Identity.java
@@ -44,7 +44,7 @@ public final class Identity implements Serializable {
   private static final long serialVersionUID = -8181841964597657446L;
 
   private final Type type;
-  private final String id;
+  private final String value;
 
   /**
    * The types of IAM identities.
@@ -82,9 +82,9 @@ public enum Type {
     DOMAIN
   }
 
-  private Identity(Type type, String id) {
+  private Identity(Type type, String value) {
     this.type = type;
-    this.id = id;
+    this.value = value;
   }
 
   public Type type() {
@@ -92,7 +92,7 @@ public Type type() {
   }
 
   /**
-   * Returns the string identifier for this identity. The id corresponds to:
+   * Returns the string identifier for this identity. The value corresponds to:
    * 
    *
  • email address (for identities of type {@code USER}, {@code SERVICE_ACCOUNT}, and * {@code GROUP}) @@ -101,8 +101,8 @@ public Type type() { * {@code ALL_AUTHENTICATED_USERS}) *
*/ - public String id() { - return id; + public String value() { + return value; } /** @@ -163,7 +163,7 @@ public static Identity domain(String domain) { @Override public int hashCode() { - return Objects.hash(id, type); + return Objects.hash(value, type); } @Override @@ -172,7 +172,7 @@ public boolean equals(Object obj) { return false; } Identity other = (Identity) obj; - return Objects.equals(id, other.id()) && Objects.equals(type, other.type()); + return Objects.equals(value, other.value()) && Objects.equals(type, other.type()); } /** @@ -186,13 +186,13 @@ public String strValue() { case ALL_AUTHENTICATED_USERS: return "allAuthenticatedUsers"; case USER: - return "user:" + id; + return "user:" + value; case SERVICE_ACCOUNT: - return "serviceAccount:" + id; + return "serviceAccount:" + value; case GROUP: - return "group:" + id; + return "group:" + value; case DOMAIN: - return "domain:" + id; + return "domain:" + value; default: throw new IllegalStateException("Unexpected identity type: " + type); } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/IdentityTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/IdentityTest.java index 828f1c839431..a42bc9db7abd 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/IdentityTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/IdentityTest.java @@ -34,19 +34,19 @@ public class IdentityTest { @Test public void testAllUsers() { assertEquals(Identity.Type.ALL_USERS, ALL_USERS.type()); - assertNull(ALL_USERS.id()); + assertNull(ALL_USERS.value()); } @Test public void testAllAuthenticatedUsers() { assertEquals(Identity.Type.ALL_AUTHENTICATED_USERS, ALL_AUTH_USERS.type()); - assertNull(ALL_AUTH_USERS.id()); + assertNull(ALL_AUTH_USERS.value()); } @Test public void testUser() { assertEquals(Identity.Type.USER, USER.type()); - assertEquals("abc@gmail.com", USER.id()); + assertEquals("abc@gmail.com", USER.value()); } @Test(expected = NullPointerException.class) @@ -57,7 +57,7 @@ public void testUserNullEmail() { @Test public void testServiceAccount() { assertEquals(Identity.Type.SERVICE_ACCOUNT, SERVICE_ACCOUNT.type()); - assertEquals("service-account@gmail.com", SERVICE_ACCOUNT.id()); + assertEquals("service-account@gmail.com", SERVICE_ACCOUNT.value()); } @Test(expected = NullPointerException.class) @@ -68,7 +68,7 @@ public void testServiceAccountNullEmail() { @Test public void testGroup() { assertEquals(Identity.Type.GROUP, GROUP.type()); - assertEquals("group@gmail.com", GROUP.id()); + assertEquals("group@gmail.com", GROUP.value()); } @Test(expected = NullPointerException.class) @@ -79,7 +79,7 @@ public void testGroupNullEmail() { @Test public void testDomain() { assertEquals(Identity.Type.DOMAIN, DOMAIN.type()); - assertEquals("google.com", DOMAIN.id()); + assertEquals("google.com", DOMAIN.value()); } @Test(expected = NullPointerException.class) @@ -100,6 +100,6 @@ public void testIdentityToAndFromPb() { private void compareIdentities(Identity expected, Identity actual) { assertEquals(expected, actual); assertEquals(expected.type(), actual.type()); - assertEquals(expected.id(), actual.id()); + assertEquals(expected.value(), actual.value()); } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java index 0d7118dcbbd7..46330e19fa59 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java @@ -17,18 +17,19 @@ package com.google.gcloud.resourcemanager; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.CaseFormat; import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.gcloud.IamPolicy; import com.google.gcloud.Identity; +import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; /** @@ -48,40 +49,101 @@ public class Policy extends IamPolicy { /** * Represents legacy roles in an IAM Policy. */ - public enum Role { + public static class Role implements Serializable { /** - * Permissions for read-only actions that preserve state. + * The recognized roles in a Project's IAM policy. */ - VIEWER("roles/viewer"), + public enum Type { + + /** + * Permissions for read-only actions that preserve state. + */ + VIEWER, + + /** + * All viewer permissions and permissions for actions that modify state. + */ + EDITOR, + + /** + * All editor permissions and permissions for the following actions: + *
    + *
  • Manage access control for a resource. + *
  • Set up billing (for a project). + *
+ */ + OWNER + } + + private static final long serialVersionUID = 2421978909244287488L; + + private final String value; + private final Type type; + + private Role(String value, Type type) { + this.value = value; + this.type = type; + } + + String value() { + return value; + } /** - * All viewer permissions and permissions for actions that modify state. + * Returns the type of role (editor, owner, or viewer). Returns {@code null} if the role type + * is unrecognized. */ - EDITOR("roles/editor"), + public Type type() { + return type; + } /** - * All editor permissions and permissions for the following actions: - *
    - *
  • Manage access control for a resource. - *
  • Set up billing (for a project). - *
+ * Returns a {@code Role} of type {@link Type#VIEWER VIEWER}. */ - OWNER("roles/owner"); + public static Role viewer() { + return new Role("roles/viewer", Type.VIEWER); + } - private String strValue; + /** + * Returns a {@code Role} of type {@link Type#EDITOR EDITOR}. + */ + public static Role editor() { + return new Role("roles/editor", Type.EDITOR); + } - private Role(String strValue) { - this.strValue = strValue; + /** + * Returns a {@code Role} of type {@link Type#OWNER OWNER}. + */ + public static Role owner() { + return new Role("roles/owner", Type.OWNER); } - String strValue() { - return strValue; + static Role rawRole(String roleStr) { + return new Role(roleStr, null); } static Role fromStr(String roleStr) { - return Role.valueOf(CaseFormat.LOWER_CAMEL.to( - CaseFormat.UPPER_UNDERSCORE, roleStr.substring("roles/".length()))); + try { + Type type = Type.valueOf(roleStr.split("/")[1].toUpperCase()); + return new Role(roleStr, type); + } catch (Exception ex) { + return new Role(roleStr, null); + } + } + + @Override + public final int hashCode() { + return Objects.hash(value, type); + } + + @Override + public final boolean equals(Object obj) { + if (!(obj instanceof Role)) { + return false; + } + Role other = (Role) obj; + return Objects.equals(value, other.value()) && Objects.equals(type, other.type()); } } @@ -124,7 +186,7 @@ com.google.api.services.cloudresourcemanager.model.Policy toPb() { for (Map.Entry> binding : bindings().entrySet()) { com.google.api.services.cloudresourcemanager.model.Binding bindingPb = new com.google.api.services.cloudresourcemanager.model.Binding(); - bindingPb.setRole(binding.getKey().strValue()); + bindingPb.setRole(binding.getKey().value()); bindingPb.setMembers( Lists.transform( new ArrayList<>(binding.getValue()), diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java index 4d12a31274c0..46b142c5aa53 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java @@ -157,10 +157,10 @@ public Project reload() { * completes, the project is not retrievable by the {@link ResourceManager#get} and * {@link ResourceManager#list} methods. The caller must have modify permissions for this project. * - * @see Cloud - * Resource Manager delete * @throws ResourceManagerException upon failure + * @see Cloud + * Resource Manager delete */ public void delete() { resourceManager.delete(projectId()); @@ -174,10 +174,10 @@ public void delete() { * state of {@link ProjectInfo.State#DELETE_IN_PROGRESS}, the project cannot be restored. The * caller must have modify permissions for this project. * - * @see Cloud - * Resource Manager undelete * @throws ResourceManagerException upon failure (including when the project can't be restored) + * @see Cloud + * Resource Manager undelete */ public void undelete() { resourceManager.undelete(projectId()); @@ -188,11 +188,11 @@ public void undelete() { * *

The caller must have modify permissions for this project. * - * @see Cloud - * Resource Manager update * @return the Project representing the new project metadata * @throws ResourceManagerException upon failure + * @see Cloud + * Resource Manager update */ public Project replace() { return resourceManager.replace(this); diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index a463937f875c..f14d47f2a676 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -18,10 +18,12 @@ import com.google.common.base.Joiner; import com.google.common.collect.Sets; +import com.google.gcloud.IamPolicy; import com.google.gcloud.Page; import com.google.gcloud.Service; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; +import java.util.List; import java.util.Set; /** @@ -167,6 +169,38 @@ public static ProjectListOption fields(ProjectField... fields) { } } + /** + * The permissions associated with a Google Cloud project. These values can be used when calling + * {@link #testPermissions}. + * + * @see + * Project-level roles + */ + enum Permission { + DELETE("delete"), + GET("get"), + GET_POLICY("getIamPolicy"), + REPLACE("update"), + REPLACE_POLICY("setIamPolicy"), + UNDELETE("undelete"); + + private static final String PREFIX = "resourcemanager.projects."; + + private final String value; + + Permission(String suffix) { + this.value = PREFIX + suffix; + } + + /** + * Returns the string representation of the permission. + */ + public String value() { + return value; + } + } + /** * Creates a new project. * @@ -174,13 +208,13 @@ public static ProjectListOption fields(ProjectField... fields) { * grant permission to others to read or update the project. Several APIs are activated * automatically for the project, including Google Cloud Storage. * - * @see Cloud - * Resource Manager create * @return Project object representing the new project's metadata. The returned object will * include the following read-only fields supplied by the server: project number, lifecycle * state, and creation time. * @throws ResourceManagerException upon failure + * @see Cloud + * Resource Manager create */ Project create(ProjectInfo project); @@ -201,10 +235,10 @@ public static ProjectListOption fields(ProjectField... fields) { * completes, the project is not retrievable by the {@link ResourceManager#get} and * {@link ResourceManager#list} methods. The caller must have modify permissions for this project. * - * @see Cloud - * Resource Manager delete * @throws ResourceManagerException upon failure + * @see Cloud + * Resource Manager delete */ void delete(String projectId); @@ -214,10 +248,9 @@ public static ProjectListOption fields(ProjectField... fields) { *

Returns {@code null} if the project is not found or if the user doesn't have read * permissions for the project. * - * @see Cloud - * Resource Manager get * @throws ResourceManagerException upon failure + * @see + * Cloud Resource Manager get */ Project get(String projectId, ProjectGetOption... options); @@ -228,11 +261,11 @@ public static ProjectListOption fields(ProjectField... fields) { * at the end of the list. Use {@link ProjectListOption} to filter this list, set page size, and * set page tokens. * - * @see Cloud - * Resource Manager list * @return {@code Page}, a page of projects * @throws ResourceManagerException upon failure + * @see Cloud + * Resource Manager list */ Page list(ProjectListOption... options); @@ -241,11 +274,11 @@ public static ProjectListOption fields(ProjectField... fields) { * *

The caller must have modify permissions for this project. * - * @see Cloud - * Resource Manager update * @return the Project representing the new project metadata * @throws ResourceManagerException upon failure + * @see Cloud + * Resource Manager update */ Project replace(ProjectInfo newProject); @@ -257,10 +290,98 @@ public static ProjectListOption fields(ProjectField... fields) { * state of {@link ProjectInfo.State#DELETE_IN_PROGRESS}, the project cannot be restored. The * caller must have modify permissions for this project. * - * @see Cloud - * Resource Manager undelete * @throws ResourceManagerException upon failure + * @see Cloud + * Resource Manager undelete */ void undelete(String projectId); + + /** + * Returns the IAM access control policy for the specified project. Returns {@code null} if the + * resource does not exist or if you do not have adequate permission to view the project or get + * the policy. + * + * @throws ResourceManagerException upon failure + * @see + * Resource Manager getIamPolicy + */ + Policy getPolicy(String projectId); + + /** + * Sets the IAM access control policy for the specified project. Replaces any existing policy. The + * following constraints apply: + *

    + *
  • Projects currently support only user:{emailid} and serviceAccount:{emailid} + * members in a binding of a policy. + *
  • To be added as an owner, a user must be invited via Cloud Platform console and must accept + * the invitation. + *
  • Members cannot be added to more than one role in the same policy. + *
  • There must be at least one owner who has accepted the Terms of Service (ToS) agreement in + * the policy. An attempt to set a policy that removes the last ToS-accepted owner from the + * policy will fail. + *
  • Calling this method requires enabling the App Engine Admin API. + *
+ * Note: Removing service accounts from policies or changing their roles can render services + * completely inoperable. It is important to understand how the service account is being used + * before removing or updating its roles. + * + *

It is recommended that you use the read-modify-write pattern. This pattern entails reading + * the project's current policy, updating it locally, and then sending the modified policy for + * writing. Cloud IAM solves the problem of conflicting processes simultaneously attempting to + * modify a policy by using the {@link IamPolicy#etag etag} property. This property is used to + * verify whether the policy has changed since the last request. When you make a request to Cloud + * IAM with an etag value, Cloud IAM compares the etag value in the request with the existing etag + * value associated with the policy. It writes the policy only if the etag values match. If the + * etags don't match, a {@code ResourceManagerException} is thrown, denoting that the server + * aborted update. If an etag is not provided, the policy is overwritten blindly. + * + *

An example of using the read-write-modify pattern is as follows: + *

 {@code
+   * Policy currentPolicy = resourceManager.getPolicy("my-project-id");
+   * Policy modifiedPolicy =
+   *     current.toBuilder().removeIdentity(Role.VIEWER, Identity.user("user@gmail.com"));
+   * Policy newPolicy = resourceManager.replacePolicy("my-project-id", modified);
+   * }
+   * 
+ * + * @throws ResourceManagerException upon failure + * @see + * Resource Manager setIamPolicy + */ + Policy replacePolicy(String projectId, Policy newPolicy); + + /** + * Returns the permissions that a caller has on the specified project. You typically don't call + * this method if you're using Google Cloud Platform directly to manage permissions. This method + * is intended for integration with your proprietary software, such as a customized graphical user + * interface. For example, the Cloud Platform Console tests IAM permissions internally to + * determine which UI should be available to the logged-in user. + * + * @return A list of booleans representing whether the caller has the permissions specified (in + * the order of the given permissions) + * @throws ResourceManagerException upon failure + * @see + * Resource Manager testIamPermissions + */ + List testPermissions(String projectId, List permissions); + + /** + * Returns the permissions that a caller has on the specified project. You typically don't call + * this method if you're using Google Cloud Platform directly to manage permissions. This method + * is intended for integration with your proprietary software, such as a customized graphical user + * interface. For example, the Cloud Platform Console tests IAM permissions internally to + * determine which UI should be available to the logged-in user. + * + * @return A list of booleans representing whether the caller has the permissions specified (in + * the order of the given permissions) + * @throws ResourceManagerException upon failure + * @see + * Resource Manager testIamPermissions + */ + List testPermissions(String projectId, Permission first, Permission... others); } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java index fb699dcb06f0..d9911b911f0b 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.gcloud.BaseService; import com.google.gcloud.Page; @@ -32,6 +33,7 @@ import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Tuple; +import java.util.List; import java.util.Map; import java.util.concurrent.Callable; @@ -55,8 +57,8 @@ public com.google.api.services.cloudresourcemanager.model.Project call() { return resourceManagerRpc.create(project.toPb()); } }, options().retryParams(), EXCEPTION_HANDLER)); - } catch (RetryHelperException e) { - throw ResourceManagerException.translateAndThrow(e); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); } } @@ -70,8 +72,8 @@ public Void call() { return null; } }, options().retryParams(), EXCEPTION_HANDLER); - } catch (RetryHelperException e) { - throw ResourceManagerException.translateAndThrow(e); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); } } @@ -87,8 +89,8 @@ public com.google.api.services.cloudresourcemanager.model.Project call() { } }, options().retryParams(), EXCEPTION_HANDLER); return answer == null ? null : Project.fromPb(this, answer); - } catch (RetryHelperException e) { - throw ResourceManagerException.translateAndThrow(e); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); } } @@ -146,8 +148,8 @@ public Project apply( }); return new PageImpl<>( new ProjectPageFetcher(serviceOptions, cursor, optionsMap), cursor, projects); - } catch (RetryHelperException e) { - throw ResourceManagerException.translateAndThrow(e); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); } } @@ -161,8 +163,8 @@ public com.google.api.services.cloudresourcemanager.model.Project call() { return resourceManagerRpc.replace(newProject.toPb()); } }, options().retryParams(), EXCEPTION_HANDLER)); - } catch (RetryHelperException e) { - throw ResourceManagerException.translateAndThrow(e); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); } } @@ -176,11 +178,69 @@ public Void call() { return null; } }, options().retryParams(), EXCEPTION_HANDLER); - } catch (RetryHelperException e) { - throw ResourceManagerException.translateAndThrow(e); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); } } + @Override + public Policy getPolicy(final String projectId) { + try { + com.google.api.services.cloudresourcemanager.model.Policy answer = + runWithRetries( + new Callable() { + @Override + public com.google.api.services.cloudresourcemanager.model.Policy call() { + return resourceManagerRpc.getPolicy(projectId); + } + }, options().retryParams(), EXCEPTION_HANDLER); + return answer == null ? null : Policy.fromPb(answer); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); + } + } + + @Override + public Policy replacePolicy(final String projectId, final Policy newPolicy) { + try { + return Policy.fromPb(runWithRetries( + new Callable() { + @Override + public com.google.api.services.cloudresourcemanager.model.Policy call() { + return resourceManagerRpc.replacePolicy(projectId, newPolicy.toPb()); + } + }, options().retryParams(), EXCEPTION_HANDLER)); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); + } + } + + @Override + public List testPermissions(final String projectId, final List permissions) { + try { + return runWithRetries( + new Callable>() { + @Override + public List call() { + return resourceManagerRpc.testPermissions(projectId, + Lists.transform(permissions, new Function() { + @Override + public String apply(Permission permission) { + return permission.value(); + } + })); + } + }, options().retryParams(), EXCEPTION_HANDLER); + } catch (RetryHelperException ex) { + throw ResourceManagerException.translateAndThrow(ex); + } + } + + @Override + public List testPermissions(String projectId, Permission first, Permission... others) { + return testPermissions(projectId, Lists.asList(first, others)); + } + private Map optionMap(Option... options) { Map temp = Maps.newEnumMap(ResourceManagerRpc.Option.class); for (Option option : options) { diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java index 2ef0d8c65ff2..9f92ff545874 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/DefaultResourceManagerRpc.java @@ -1,5 +1,6 @@ package com.google.gcloud.resourcemanager.spi; +import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Option.FIELDS; import static com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Option.FILTER; import static com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Option.PAGE_SIZE; @@ -11,13 +12,22 @@ import com.google.api.client.http.HttpTransport; import com.google.api.client.json.jackson.JacksonFactory; import com.google.api.services.cloudresourcemanager.Cloudresourcemanager; +import com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest; import com.google.api.services.cloudresourcemanager.model.ListProjectsResponse; +import com.google.api.services.cloudresourcemanager.model.Policy; import com.google.api.services.cloudresourcemanager.model.Project; +import com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest; +import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsRequest; +import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.gcloud.resourcemanager.ResourceManagerException; import com.google.gcloud.resourcemanager.ResourceManagerOptions; import java.io.IOException; +import java.util.List; import java.util.Map; +import java.util.Set; public class DefaultResourceManagerRpc implements ResourceManagerRpc { @@ -107,5 +117,51 @@ public Project replace(Project project) { throw translate(ex); } } -} + @Override + public Policy getPolicy(String projectId) throws ResourceManagerException { + try { + return resourceManager.projects() + .getIamPolicy(projectId, new GetIamPolicyRequest()) + .execute(); + } catch (IOException ex) { + ResourceManagerException translated = translate(ex); + if (translated.code() == HTTP_FORBIDDEN) { + // Service returns permission denied if policy doesn't exist. + return null; + } else { + throw translated; + } + } + } + + @Override + public Policy replacePolicy(String projectId, Policy newPolicy) throws ResourceManagerException { + try { + return resourceManager.projects() + .setIamPolicy(projectId, new SetIamPolicyRequest().setPolicy(newPolicy)).execute(); + } catch (IOException ex) { + throw translate(ex); + } + } + + @Override + public List testPermissions(String projectId, List permissions) + throws ResourceManagerException { + try { + TestIamPermissionsResponse response = resourceManager.projects() + .testIamPermissions( + projectId, new TestIamPermissionsRequest().setPermissions(permissions)) + .execute(); + Set permissionsOwned = + ImmutableSet.copyOf(firstNonNull(response.getPermissions(), ImmutableList.of())); + ImmutableList.Builder answer = ImmutableList.builder(); + for (String p : permissions) { + answer.add(permissionsOwned.contains(p)); + } + return answer.build(); + } catch (IOException ex) { + throw translate(ex); + } + } +} diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpc.java index 54531edd5ed5..d6ec068a92a3 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/spi/ResourceManagerRpc.java @@ -16,9 +16,11 @@ package com.google.gcloud.resourcemanager.spi; +import com.google.api.services.cloudresourcemanager.model.Policy; import com.google.api.services.cloudresourcemanager.model.Project; import com.google.gcloud.resourcemanager.ResourceManagerException; +import java.util.List; import java.util.Map; public interface ResourceManagerRpc { @@ -121,5 +123,27 @@ public Y y() { */ Project replace(Project project); + /** + * Returns the IAM policy associated with a project. + * + * @throws ResourceManagerException upon failure + */ + Policy getPolicy(String projectId); + + /** + * Replaces the IAM policy associated with the given project. + * + * @throws ResourceManagerException upon failure + */ + Policy replacePolicy(String projectId, Policy newPolicy); + + /** + * Tests whether the caller has the given permissions. Returns a list of booleans corresponding to + * whether or not the user has the permission in the same position of input list. + * + * @throws ResourceManagerException upon failure + */ + List testPermissions(String projectId, List permissions); + // TODO(ajaykannan): implement "Organization" functionality when available (issue #319) } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java index cda2dd1e00ea..8ddca18b6261 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java @@ -5,7 +5,12 @@ import static java.net.HttpURLConnection.HTTP_OK; import com.google.api.client.json.JsonFactory; +import com.google.api.services.cloudresourcemanager.model.Binding; +import com.google.api.services.cloudresourcemanager.model.Policy; import com.google.api.services.cloudresourcemanager.model.Project; +import com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest; +import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsRequest; +import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse; import com.google.common.base.Joiner; import com.google.common.base.Objects; import com.google.common.collect.ImmutableList; @@ -13,6 +18,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteStreams; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.resourcemanager.ResourceManager.Permission; import com.google.gcloud.resourcemanager.ResourceManagerOptions; import com.sun.net.httpserver.Headers; @@ -30,11 +36,14 @@ import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; +import java.util.UUID; import java.util.concurrent.ConcurrentSkipListMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -46,7 +55,25 @@ * Utility to create a local Resource Manager mock for testing. * *

The mock runs in a separate thread, listening for HTTP requests on the local machine at an - * ephemeral port. + * ephemeral port. While this mock attempts to simulate the Cloud Resource Manager, there are some + * divergences in behavior. The following is a non-exhaustive list of some of those behavioral + * differences: + * + *

    + *
  • This mock assumes you have adequate permissions for any action. Related to this, + * testIamPermissions always indicates that the caller has all permissions listed in the + * request. + *
  • IAM policies are set to an empty policy with version 0 (only legacy roles supported) upon + * project creation. The actual service will not have an empty list of bindings and may also + * set your version to 1. + *
  • There is no input validation for the policy provided when replacing a policy. + *
  • In this mock, projects never move from the DELETE_REQUESTED lifecycle state to + * DELETE_IN_PROGRESS without an explicit call to the utility method + * {@link #changeLifecycleState}. Similarly, a project is never completely removed without an + * explicit call to the utility method {@link #removeProject}. + *
  • The messages in the error responses given by this mock do not necessarily match the messages + * given by the actual service. + *
*/ @SuppressWarnings("restriction") public class LocalResourceManagerHelper { @@ -62,8 +89,12 @@ public class LocalResourceManagerHelper { private static final Pattern LIST_FIELDS_PATTERN = Pattern.compile("(.*?)projects\\((.*?)\\)(.*?)"); private static final String[] NO_FIELDS = {}; + private static final Set PERMISSIONS = new HashSet<>(); static { + for (Permission permission : Permission.values()) { + PERMISSIONS.add(permission.value()); + } try { BASE_CONTEXT = new URI(CONTEXT); } catch (URISyntaxException e) { @@ -78,6 +109,7 @@ public class LocalResourceManagerHelper { private final HttpServer server; private final ConcurrentSkipListMap projects = new ConcurrentSkipListMap<>(); + private final Map policies = new HashMap<>(); private final int port; private static class Response { @@ -99,6 +131,7 @@ String body() { } private enum Error { + ABORTED(409, "global", "aborted", "ABORTED"), ALREADY_EXISTS(409, "global", "alreadyExists", "ALREADY_EXISTS"), PERMISSION_DENIED(403, "global", "forbidden", "PERMISSION_DENIED"), FAILED_PRECONDITION(400, "global", "failedPrecondition", "FAILED_PRECONDITION"), @@ -150,13 +183,7 @@ public void handle(HttpExchange exchange) { try { switch (requestMethod) { case "POST": - if (path.endsWith(":undelete")) { - response = undelete(projectIdFromUri(path)); - } else { - String requestBody = - decodeContent(exchange.getRequestHeaders(), exchange.getRequestBody()); - response = create(jsonFactory.fromString(requestBody, Project.class)); - } + response = handlePost(exchange, path); break; case "DELETE": response = delete(projectIdFromUri(path)); @@ -187,6 +214,30 @@ public void handle(HttpExchange exchange) { } } + private Response handlePost(HttpExchange exchange, String path) throws IOException { + String requestBody = decodeContent(exchange.getRequestHeaders(), exchange.getRequestBody()); + if (!path.contains(":")) { + return create(jsonFactory.fromString(requestBody, Project.class)); + } else { + switch (path.split(":", 2)[1]) { + case "undelete": + return undelete(projectIdFromUri(path)); + case "getIamPolicy": + return getPolicy(projectIdFromUri(path)); + case "setIamPolicy": + return replacePolicy(projectIdFromUri(path), + jsonFactory.fromString(requestBody, SetIamPolicyRequest.class).getPolicy()); + case "testIamPermissions": + return testPermissions(projectIdFromUri(path), + jsonFactory.fromString(requestBody, TestIamPermissionsRequest.class) + .getPermissions()); + default: + return Error.BAD_REQUEST.response( + "The server could not understand the following request URI: POST " + path); + } + } + } + private static void writeResponse(HttpExchange exchange, Response response) { exchange.getResponseHeaders().set("Content-type", "application/json; charset=UTF-8"); OutputStream outputStream = exchange.getResponseBody(); @@ -259,7 +310,7 @@ private static Map parseListOptions(String query) throws IOExcep options.put("pageToken", argEntry[1]); break; case "pageSize": - int pageSize = Integer.valueOf(argEntry[1]); + int pageSize = Integer.parseInt(argEntry[1]); if (pageSize < 1) { throw new IOException("Page size must be greater than 0."); } @@ -317,7 +368,7 @@ private static boolean isValidIdOrLabel(String value, int minLength, int maxLeng return value.length() >= minLength && value.length() <= maxLength; } - Response create(Project project) { + synchronized Response create(Project project) { String customErrorMessage = checkForProjectErrors(project); if (customErrorMessage != null) { return Error.INVALID_ARGUMENT.response(customErrorMessage); @@ -329,6 +380,11 @@ Response create(Project project) { return Error.ALREADY_EXISTS.response( "A project with the same project ID (" + project.getProjectId() + ") already exists."); } + Policy emptyPolicy = new Policy() + .setBindings(Collections.emptyList()) + .setEtag(UUID.randomUUID().toString()) + .setVersion(0); + policies.put(project.getProjectId(), emptyPolicy); try { String createdProjectStr = jsonFactory.toString(project); return new Response(HTTP_OK, createdProjectStr); @@ -540,6 +596,58 @@ synchronized Response undelete(String projectId) { return response; } + synchronized Response getPolicy(String projectId) { + Policy policy = policies.get(projectId); + if (policy == null) { + return Error.PERMISSION_DENIED.response("Project " + projectId + " not found."); + } + try { + return new Response(HTTP_OK, jsonFactory.toString(policy)); + } catch (IOException e) { + return Error.INTERNAL_ERROR.response( + "Error when serializing the IAM policy for " + projectId); + } + } + + synchronized Response replacePolicy(String projectId, Policy policy) { + Policy originalPolicy = policies.get(projectId); + if (originalPolicy == null) { + return Error.PERMISSION_DENIED.response("Error when replacing the policy for " + projectId + + " because the project was not found."); + } + String etag = policy.getEtag(); + if (etag != null && !originalPolicy.getEtag().equals(etag)) { + return Error.ABORTED.response("Policy etag mismatch when replacing the policy for project " + + projectId + ", please retry the read."); + } + policy.setEtag(UUID.randomUUID().toString()); + policy.setVersion(originalPolicy.getVersion()); + policies.put(projectId, policy); + try { + return new Response(HTTP_OK, jsonFactory.toString(policy)); + } catch (IOException e) { + return Error.INTERNAL_ERROR.response( + "Error when serializing the policy for project " + projectId); + } + } + + synchronized Response testPermissions(String projectId, List permissions) { + if (!projects.containsKey(projectId)) { + return Error.PERMISSION_DENIED.response("Project " + projectId + " not found."); + } + for (String p : permissions) { + if (!PERMISSIONS.contains(p)) { + return Error.INVALID_ARGUMENT.response("Invalid permission: " + p); + } + } + try { + return new Response(HTTP_OK, + jsonFactory.toString(new TestIamPermissionsResponse().setPermissions(permissions))); + } catch (IOException e) { + return Error.INTERNAL_ERROR.response("Error when serializing permissions " + permissions); + } + } + private LocalResourceManagerHelper() { try { server = HttpServer.create(new InetSocketAddress(0), 0); @@ -611,6 +719,7 @@ public synchronized boolean changeLifecycleState(String projectId, String lifecy public synchronized boolean removeProject(String projectId) { // Because this method is synchronized, any code that relies on non-atomic read/write operations // should not fail if that code is also synchronized. - return projects.remove(checkNotNull(projectId)) != null; + policies.remove(checkNotNull(projectId)); + return projects.remove(projectId) != null; } } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java index c9b2970a4efa..829094816664 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java @@ -2,11 +2,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.api.services.cloudresourcemanager.model.Binding; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gcloud.resourcemanager.spi.DefaultResourceManagerRpc; import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc; @@ -18,8 +21,10 @@ import org.junit.BeforeClass; import org.junit.Test; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; public class LocalResourceManagerHelperTest { @@ -45,7 +50,12 @@ public class LocalResourceManagerHelperTest { .setLabels(ImmutableMap.of("k1", "v1", "k2", "v2")); private static final com.google.api.services.cloudresourcemanager.model.Project PROJECT_WITH_PARENT = - copyFrom(COMPLETE_PROJECT).setProjectId("project-with-parent-id").setParent(PARENT); + copyFrom(COMPLETE_PROJECT).setProjectId("project-with-parent-id").setParent(PARENT); + private static final List BINDINGS = ImmutableList.of( + new Binding().setRole("roles/owner").setMembers(ImmutableList.of("user:me@gmail.com")), + new Binding().setRole("roles/viewer").setMembers(ImmutableList.of("group:group@gmail.com"))); + private static final com.google.api.services.cloudresourcemanager.model.Policy POLICY = + new com.google.api.services.cloudresourcemanager.model.Policy().setBindings(BINDINGS); @BeforeClass public static void beforeClass() { @@ -92,6 +102,13 @@ public void testCreate() { assertNull(returnedProject.getParent()); assertNotNull(returnedProject.getProjectNumber()); assertNotNull(returnedProject.getCreateTime()); + com.google.api.services.cloudresourcemanager.model.Policy policy = + rpc.getPolicy(PARTIAL_PROJECT.getProjectId()); + assertEquals(Collections.emptyList(), policy.getBindings()); + assertNotNull(policy.getEtag()); + assertEquals(0, policy.getVersion().intValue()); + rpc.replacePolicy(PARTIAL_PROJECT.getProjectId(), POLICY); + assertEquals(POLICY.getBindings(), rpc.getPolicy(PARTIAL_PROJECT.getProjectId()).getBindings()); try { rpc.create(PARTIAL_PROJECT); fail("Should fail, project already exists."); @@ -99,6 +116,8 @@ public void testCreate() { assertEquals(409, e.code()); assertTrue(e.getMessage().startsWith("A project with the same project ID") && e.getMessage().endsWith("already exists.")); + assertEquals( + POLICY.getBindings(), rpc.getPolicy(PARTIAL_PROJECT.getProjectId()).getBindings()); } returnedProject = rpc.create(PROJECT_WITH_PARENT); compareReadWriteFields(PROJECT_WITH_PARENT, returnedProject); @@ -609,6 +628,65 @@ public void testUndeleteWhenDeleteInProgress() { } } + @Test + public void testGetPolicy() { + assertNull(rpc.getPolicy("nonexistent-project")); + rpc.create(PARTIAL_PROJECT); + com.google.api.services.cloudresourcemanager.model.Policy policy = + rpc.getPolicy(PARTIAL_PROJECT.getProjectId()); + assertEquals(Collections.emptyList(), policy.getBindings()); + assertNotNull(policy.getEtag()); + } + + @Test + public void testReplacePolicy() { + try { + rpc.replacePolicy("nonexistent-project", POLICY); + fail("Project doesn't exist."); + } catch (ResourceManagerException e) { + assertEquals(403, e.code()); + assertTrue(e.getMessage().contains("project was not found")); + } + rpc.create(PARTIAL_PROJECT); + com.google.api.services.cloudresourcemanager.model.Policy invalidPolicy = + new com.google.api.services.cloudresourcemanager.model.Policy().setEtag("wrong-etag"); + try { + rpc.replacePolicy(PARTIAL_PROJECT.getProjectId(), invalidPolicy); + fail("Invalid etag."); + } catch (ResourceManagerException e) { + assertEquals(409, e.code()); + assertTrue(e.getMessage().startsWith("Policy etag mismatch")); + } + String originalEtag = rpc.getPolicy(PARTIAL_PROJECT.getProjectId()).getEtag(); + com.google.api.services.cloudresourcemanager.model.Policy newPolicy = + rpc.replacePolicy(PARTIAL_PROJECT.getProjectId(), POLICY); + assertEquals(POLICY.getBindings(), newPolicy.getBindings()); + assertNotNull(newPolicy.getEtag()); + assertNotEquals(originalEtag, newPolicy.getEtag()); + } + + @Test + public void testTestPermissions() { + List permissions = ImmutableList.of("resourcemanager.projects.get"); + try { + rpc.testPermissions("nonexistent-project", permissions); + fail("Nonexistent project."); + } catch (ResourceManagerException e) { + assertEquals(403, e.code()); + assertEquals("Project nonexistent-project not found.", e.getMessage()); + } + rpc.create(PARTIAL_PROJECT); + try { + rpc.testPermissions(PARTIAL_PROJECT.getProjectId(), ImmutableList.of("get")); + fail("Invalid permission."); + } catch (ResourceManagerException e) { + assertEquals(400, e.code()); + assertEquals("Invalid permission: get", e.getMessage()); + } + assertEquals(ImmutableList.of(true), + rpc.testPermissions(PARTIAL_PROJECT.getProjectId(), permissions)); + } + @Test public void testChangeLifecycleStatus() { assertFalse(RESOURCE_MANAGER_HELPER.changeLifecycleState( @@ -632,8 +710,10 @@ public void testChangeLifecycleStatus() { public void testRemoveProject() { assertFalse(RESOURCE_MANAGER_HELPER.removeProject(COMPLETE_PROJECT.getProjectId())); rpc.create(COMPLETE_PROJECT); + assertNotNull(rpc.getPolicy(COMPLETE_PROJECT.getProjectId())); assertTrue(RESOURCE_MANAGER_HELPER.removeProject(COMPLETE_PROJECT.getProjectId())); assertNull(rpc.get(COMPLETE_PROJECT.getProjectId(), EMPTY_RPC_OPTIONS)); + assertNull(rpc.getPolicy(COMPLETE_PROJECT.getProjectId())); } private void compareReadWriteFields( diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java index 05d1b85bdbed..e6d0105838b7 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java @@ -17,9 +17,13 @@ package com.google.gcloud.resourcemanager; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import com.google.common.collect.ImmutableSet; import com.google.gcloud.Identity; +import com.google.gcloud.resourcemanager.Policy.Role; +import com.google.gcloud.resourcemanager.Policy.Role.Type; import org.junit.Test; @@ -33,8 +37,10 @@ public class PolicyTest { private static final Identity GROUP = Identity.group("group@gmail.com"); private static final Identity DOMAIN = Identity.domain("google.com"); private static final Policy SIMPLE_POLICY = Policy.builder() - .addBinding(Policy.Role.VIEWER, ImmutableSet.of(USER, SERVICE_ACCOUNT, ALL_USERS)) - .addBinding(Policy.Role.EDITOR, ImmutableSet.of(ALL_AUTH_USERS, GROUP, DOMAIN)) + .addBinding(Role.owner(), ImmutableSet.of(USER)) + .addBinding(Role.viewer(), ImmutableSet.of(ALL_USERS)) + .addBinding(Role.editor(), ImmutableSet.of(ALL_AUTH_USERS, DOMAIN)) + .addBinding(Role.rawRole("some-role"), ImmutableSet.of(SERVICE_ACCOUNT, GROUP)) .build(); private static final Policy FULL_POLICY = new Policy.Builder(SIMPLE_POLICY.bindings(), "etag", 1).build(); @@ -50,4 +56,24 @@ public void testPolicyToAndFromPb() { assertEquals(FULL_POLICY, Policy.fromPb(FULL_POLICY.toPb())); assertEquals(SIMPLE_POLICY, Policy.fromPb(SIMPLE_POLICY.toPb())); } + + @Test + public void testRoleType() { + assertEquals(Type.OWNER, Role.owner().type()); + assertEquals(Type.EDITOR, Role.editor().type()); + assertEquals(Type.VIEWER, Role.viewer().type()); + assertNull(Role.rawRole("raw-role").type()); + } + + @Test + public void testEquals() { + Policy copy = Policy.builder() + .addBinding(Role.owner(), ImmutableSet.of(USER)) + .addBinding(Role.viewer(), ImmutableSet.of(ALL_USERS)) + .addBinding(Role.editor(), ImmutableSet.of(ALL_AUTH_USERS, DOMAIN)) + .addBinding(Role.rawRole("some-role"), ImmutableSet.of(SERVICE_ACCOUNT, GROUP)) + .build(); + assertEquals(SIMPLE_POLICY, copy); + assertNotEquals(SIMPLE_POLICY, FULL_POLICY); + } } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java index 4e239acc45ef..882ec77197f3 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertNull; import com.google.common.collect.ImmutableMap; +import com.google.gcloud.resourcemanager.ProjectInfo.ResourceId; import org.junit.After; import org.junit.Before; @@ -84,12 +85,12 @@ public void testToBuilder() { @Test public void testBuilder() { - initializeExpectedProject(4); - expect(resourceManager.options()).andReturn(mockOptions).times(4); + expect(resourceManager.options()).andReturn(mockOptions).times(7); replay(resourceManager); Project.Builder builder = - new Project.Builder(new Project(resourceManager, new ProjectInfo.BuilderImpl(PROJECT_ID))); - Project project = builder.name(NAME) + new Project.Builder(new Project(resourceManager, new ProjectInfo.BuilderImpl("wrong-id"))); + Project project = builder.projectId(PROJECT_ID) + .name(NAME) .labels(LABELS) .projectNumber(PROJECT_NUMBER) .createTimeMillis(CREATE_TIME_MILLIS) @@ -102,6 +103,23 @@ public void testBuilder() { assertEquals(CREATE_TIME_MILLIS, project.createTimeMillis()); assertEquals(STATE, project.state()); assertEquals(resourceManager.options(), project.resourceManager().options()); + assertNull(project.parent()); + ResourceId parent = new ResourceId("id", "type"); + project = project.toBuilder() + .clearLabels() + .addLabel("k3", "v3") + .addLabel("k4", "v4") + .removeLabel("k4") + .parent(parent) + .build(); + assertEquals(PROJECT_ID, project.projectId()); + assertEquals(NAME, project.name()); + assertEquals(ImmutableMap.of("k3", "v3"), project.labels()); + assertEquals(PROJECT_NUMBER, project.projectNumber()); + assertEquals(CREATE_TIME_MILLIS, project.createTimeMillis()); + assertEquals(STATE, project.state()); + assertEquals(resourceManager.options(), project.resourceManager().options()); + assertEquals(parent, project.parent()); } @Test diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java index 5b172d6a070e..a69880c5d064 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java @@ -18,15 +18,20 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.gcloud.Identity; import com.google.gcloud.Page; +import com.google.gcloud.resourcemanager.Policy.Role; import com.google.gcloud.resourcemanager.ProjectInfo.ResourceId; +import com.google.gcloud.resourcemanager.ResourceManager.Permission; import com.google.gcloud.resourcemanager.ResourceManager.ProjectField; import com.google.gcloud.resourcemanager.ResourceManager.ProjectGetOption; import com.google.gcloud.resourcemanager.ResourceManager.ProjectListOption; @@ -43,6 +48,7 @@ import org.junit.rules.ExpectedException; import java.util.Iterator; +import java.util.List; import java.util.Map; public class ResourceManagerImplTest { @@ -65,6 +71,10 @@ public class ResourceManagerImplTest { .parent(PARENT) .build(); private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); + private static final Policy POLICY = Policy.builder() + .addBinding(Role.owner(), Identity.user("me@gmail.com")) + .addBinding(Role.editor(), Identity.serviceAccount("serviceaccount@gmail.com")) + .build(); @Rule public ExpectedException thrown = ExpectedException.none(); @@ -320,6 +330,60 @@ public void testUndelete() { } } + @Test + public void testGetPolicy() { + assertNull(RESOURCE_MANAGER.getPolicy(COMPLETE_PROJECT.projectId())); + RESOURCE_MANAGER.create(COMPLETE_PROJECT); + RESOURCE_MANAGER.replacePolicy(COMPLETE_PROJECT.projectId(), POLICY); + Policy retrieved = RESOURCE_MANAGER.getPolicy(COMPLETE_PROJECT.projectId()); + assertEquals(POLICY.bindings(), retrieved.bindings()); + assertNotNull(retrieved.etag()); + assertEquals(0, retrieved.version().intValue()); + } + + @Test + public void testReplacePolicy() { + try { + RESOURCE_MANAGER.replacePolicy("nonexistent-project", POLICY); + fail("Project doesn't exist."); + } catch (ResourceManagerException e) { + assertEquals(403, e.code()); + assertTrue(e.getMessage().endsWith("project was not found.")); + } + RESOURCE_MANAGER.create(PARTIAL_PROJECT); + Policy oldPolicy = RESOURCE_MANAGER.getPolicy(PARTIAL_PROJECT.projectId()); + RESOURCE_MANAGER.replacePolicy(PARTIAL_PROJECT.projectId(), POLICY); + try { + RESOURCE_MANAGER.replacePolicy(PARTIAL_PROJECT.projectId(), oldPolicy); + fail("Policy with an invalid etag didn't cause error."); + } catch (ResourceManagerException e) { + assertEquals(409, e.code()); + assertTrue(e.getMessage().contains("Policy etag mismatch")); + } + String originalEtag = RESOURCE_MANAGER.getPolicy(PARTIAL_PROJECT.projectId()).etag(); + Policy newPolicy = RESOURCE_MANAGER.replacePolicy(PARTIAL_PROJECT.projectId(), POLICY); + assertEquals(POLICY.bindings(), newPolicy.bindings()); + assertNotNull(newPolicy.etag()); + assertNotEquals(originalEtag, newPolicy.etag()); + } + + @Test + public void testTestPermissions() { + List permissions = ImmutableList.of(Permission.GET); + try { + RESOURCE_MANAGER.testPermissions("nonexistent-project", permissions); + fail("Nonexistent project"); + } catch (ResourceManagerException e) { + assertEquals(403, e.code()); + assertEquals("Project nonexistent-project not found.", e.getMessage()); + } + RESOURCE_MANAGER.create(PARTIAL_PROJECT); + assertEquals(ImmutableList.of(true), + RESOURCE_MANAGER.testPermissions(PARTIAL_PROJECT.projectId(), permissions)); + assertEquals(ImmutableList.of(true, true), RESOURCE_MANAGER.testPermissions( + PARTIAL_PROJECT.projectId(), Permission.DELETE, Permission.GET)); + } + @Test public void testRetryableException() { ResourceManagerRpcFactory rpcFactoryMock = EasyMock.createMock(ResourceManagerRpcFactory.class); diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 35b72ae1713f..f71f5d7989d6 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -56,7 +56,7 @@ public class SerializationTest { private static final ResourceManager.ProjectListOption PROJECT_LIST_OPTION = ResourceManager.ProjectListOption.filter("name:*"); private static final Policy POLICY = Policy.builder() - .addBinding(Policy.Role.VIEWER, ImmutableSet.of(Identity.user("abc@gmail.com"))) + .addBinding(Policy.Role.viewer(), ImmutableSet.of(Identity.user("abc@gmail.com"))) .build(); @Test From 8ae8a1b160bbbd1e18bedf2a35d014449397ec3e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 16 Mar 2016 09:35:09 +0100 Subject: [PATCH 26/44] Create base class for serialization tests --- gcloud-java-bigquery/pom.xml | 7 ++ .../gcloud/bigquery/SerializationTest.java | 47 ++-------- .../google/gcloud/BaseSerializationTest.java | 64 +++++++++++++ .../com/google/gcloud/SerializationTest.java | 33 +++++++ gcloud-java-datastore/pom.xml | 7 ++ .../gcloud/datastore/SerializationTest.java | 90 +++---------------- gcloud-java-resourcemanager/pom.xml | 7 ++ .../resourcemanager/SerializationTest.java | 50 ++--------- gcloud-java-storage/pom.xml | 7 ++ .../gcloud/storage/SerializationTest.java | 47 ++-------- pom.xml | 7 ++ 11 files changed, 163 insertions(+), 203 deletions(-) create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index 9a2137cb987d..5c79f150c722 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -39,6 +39,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 254c8954bf30..f64946e062d7 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -17,11 +17,11 @@ package com.google.gcloud.bigquery; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; import com.google.gcloud.WriteChannel; @@ -29,17 +29,13 @@ import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final Acl DOMAIN_ACCESS = Acl.of(new Acl.Domain("domain"), Acl.Role.WRITER); @@ -231,27 +227,18 @@ public class SerializationTest { private static final Table TABLE = new Table(BIGQUERY, new TableInfo.BuilderImpl(TABLE_INFO)); private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO)); - @Test - public void testServiceOptions() throws Exception { + @Override + public Serializable[] serializableObjects() { BigQueryOptions options = BigQueryOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) .build(); - BigQueryOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - - options = options.toBuilder() + BigQueryOptions otherOptions = options.toBuilder() .projectId("p2") .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testModelAndRequests() throws Exception { - Serializable[] objects = {DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, + return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION, EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO, EXTERNAL_TABLE_INFO, INLINE_FUNCTION, URI_FUNCTION, JOB_STATISTICS, EXTRACT_STATISTICS, @@ -262,14 +249,7 @@ public void testModelAndRequests() throws Exception { BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(), BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(), - BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB}; - for (Serializable obj : objects) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } + BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB, options, otherOptions}; } @Test @@ -288,17 +268,4 @@ public void testWriteChannelState() throws IOException, ClassNotFoundException { assertEquals(state.hashCode(), deserializedState.hashCode()); assertEquals(state.toString(), deserializedState.toString()); } - - @SuppressWarnings("unchecked") - private T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - return (T) input.readObject(); - } - } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java new file mode 100644 index 000000000000..533dfcde6ad8 --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +/** + * Base class for serialization tests. To use this class in your tests override the + * {@code serializableObjects()} method to return all objects that must be serializable. + */ +public abstract class BaseSerializationTest { + + public abstract Serializable[] serializableObjects(); + + @Test + public void testSerializableObjects() throws Exception { + for (Serializable obj : serializableObjects()) { + Object copy = serializeAndDeserialize(obj); + assertEquals(obj, obj); + assertEquals(obj, copy); + assertEquals(obj.hashCode(), copy.hashCode()); + assertEquals(obj.toString(), copy.toString()); + assertNotSame(obj, copy); + assertEquals(copy, copy); + } + } + + @SuppressWarnings("unchecked") + public T serializeAndDeserialize(T obj) + throws IOException, ClassNotFoundException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { + output.writeObject(obj); + } + try (ObjectInputStream input = + new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { + return (T) input.readObject(); + } + } +} diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java new file mode 100644 index 000000000000..aec9158e1ee0 --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2016 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; + +import com.google.common.collect.ImmutableList; + +import java.io.Serializable; + +public class SerializationTest extends BaseSerializationTest { + + private static final PageImpl PAGE = + new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); + private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance(); + + @Override + public Serializable[] serializableObjects() { + return new Serializable[]{PAGE, RETRY_PARAMS}; + } +} diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index 977b6db22b14..f3b46e22b3c8 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -33,6 +33,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 3976be2cc383..7a6f065d6ca6 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -17,28 +17,17 @@ package com.google.gcloud.datastore; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; import com.google.api.services.datastore.DatastoreV1; -import com.google.common.collect.ImmutableMultimap; -import com.google.common.collect.Multimap; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.Projection; import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final IncompleteKey INCOMPLETE_KEY1 = IncompleteKey.builder("ds", "k").ancestors(PathElement.of("p", 1)).build(); @@ -114,82 +103,23 @@ public class SerializationTest { .build(); private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); - @SuppressWarnings("rawtypes") - private static final Multimap TYPE_TO_VALUES = - ImmutableMultimap.builder() - .put(ValueType.NULL, NULL_VALUE) - .put(ValueType.KEY, KEY_VALUE) - .put(ValueType.STRING, STRING_VALUE) - .putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, - EMBEDDED_ENTITY_VALUE3) - .put(ValueType.LIST, LIST_VALUE) - .put(ValueType.LONG, LONG_VALUE) - .put(ValueType.DOUBLE, DOUBLE_VALUE) - .put(ValueType.BOOLEAN, BOOLEAN_VALUE) - .put(ValueType.DATE_TIME, DATE_AND_TIME_VALUE) - .put(ValueType.BLOB, BLOB_VALUE) - .put(ValueType.RAW_VALUE, RAW_VALUE) - .build(); - - @Test - public void testServiceOptions() throws Exception { + @Override + public java.io.Serializable[] serializableObjects() { DatastoreOptions options = DatastoreOptions.builder() .authCredentials(AuthCredentials.createForAppEngine()) .normalizeDataset(false) .projectId("ds1") .build(); - DatastoreOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - - options = options.toBuilder() + DatastoreOptions otherOptions = options.toBuilder() .namespace("ns1") .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .force(true) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testValues() throws Exception { - for (ValueType valueType : ValueType.values()) { - for (Value value : TYPE_TO_VALUES.get(valueType)) { - Value copy = serializeAndDeserialize(value); - assertEquals(value, value); - assertEquals(value, copy); - assertNotSame(value, copy); - assertEquals(copy, copy); - assertEquals(value.get(), copy.get()); - } - } - } - - @Test - public void testTypes() throws Exception { - Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, - ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, - QUERY1, QUERY2, QUERY3}; - for (Serializable obj : types) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } - } - - private T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - @SuppressWarnings("unchecked") - T result = (T) input.readObject(); - return result; - } + return new java.io.Serializable[]{KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, + ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, + GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1, + EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE, + BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions}; } } diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index c10691d3b07d..c0c48af48f1e 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -33,6 +33,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index f71f5d7989d6..2dcd3f9d3e7b 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -16,26 +16,17 @@ package com.google.gcloud.resourcemanager; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; - import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.gcloud.Identity; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.RetryParams; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Collections; -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final ResourceManager RESOURCE_MANAGER = ResourceManagerOptions.defaultInstance().service(); @@ -59,41 +50,14 @@ public class SerializationTest { .addBinding(Policy.Role.viewer(), ImmutableSet.of(Identity.user("abc@gmail.com"))) .build(); - @Test - public void testServiceOptions() throws Exception { + @Override + public Serializable[] serializableObjects() { ResourceManagerOptions options = ResourceManagerOptions.builder().build(); - ResourceManagerOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - options = options.toBuilder() + ResourceManagerOptions otherOptions = options.toBuilder() .projectId("some-unnecessary-project-ID") .retryParams(RetryParams.defaultInstance()) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testModelAndRequests() throws Exception { - Serializable[] objects = {PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, - PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY}; - for (Serializable obj : objects) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } - } - - @SuppressWarnings("unchecked") - private T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - return (T) input.readObject(); - } + return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, + PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; } } diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index d5f0f6d98660..16427d50de3a 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -37,6 +37,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index efa56d9e39b2..ee3d7c946312 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -17,10 +17,10 @@ package com.google.gcloud.storage; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.ReadChannel; import com.google.gcloud.RestorableState; @@ -31,16 +31,12 @@ import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Collections; import java.util.Map; -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final Storage STORAGE = StorageOptions.builder().projectId("p").build().service(); private static final Acl.Domain ACL_DOMAIN = new Acl.Domain("domain"); @@ -77,37 +73,21 @@ public class SerializationTest { Storage.BucketTargetOption.metagenerationNotMatch(); private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); - @Test - public void testServiceOptions() throws Exception { + @Override + public Serializable[] serializableObjects() { StorageOptions options = StorageOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) .build(); - StorageOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - - options = options.toBuilder() + StorageOptions otherOptions = options.toBuilder() .projectId("p2") .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testModelAndRequests() throws Exception { - Serializable[] objects = {ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, + return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, BLOB_INFO, BLOB, BUCKET_INFO, BUCKET, ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT, BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, - BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS}; - for (Serializable obj : objects) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } + BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions}; } @Test @@ -142,17 +122,4 @@ public void testWriteChannelState() throws IOException, ClassNotFoundException { assertEquals(state.hashCode(), deserializedState.hashCode()); assertEquals(state.toString(), deserializedState.toString()); } - - @SuppressWarnings("unchecked") - private T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - return (T) input.readObject(); - } - } } diff --git a/pom.xml b/pom.xml index b7766ac0e5c6..1abfb094ffb8 100644 --- a/pom.xml +++ b/pom.xml @@ -215,6 +215,13 @@ + + + + test-jar + + + maven-compiler-plugin From 967c5c5a20f39ce232e415b371e7b3a038e453fa Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 17 Mar 2016 09:54:13 +0100 Subject: [PATCH 27/44] Add tests for restorable classes. Fix serialization issues --- .../gcloud/bigquery/SerializationTest.java | 17 +------ .../com/google/gcloud/AuthCredentials.java | 10 ++++ .../com/google/gcloud/ExceptionHandler.java | 22 +++++++++ .../google/gcloud/BaseSerializationTest.java | 19 +++++++- .../com/google/gcloud/SerializationTest.java | 46 ++++++++++++++++++- .../gcloud/datastore/SerializationTest.java | 2 - .../resourcemanager/SerializationTest.java | 4 +- .../gcloud/storage/SerializationTest.java | 29 ++---------- 8 files changed, 101 insertions(+), 48 deletions(-) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index f64946e062d7..087d263b0fa4 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -16,15 +16,10 @@ package com.google.gcloud.bigquery; -import static org.junit.Assert.assertEquals; - import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; -import com.google.gcloud.RestorableState; -import com.google.gcloud.RetryParams; -import com.google.gcloud.WriteChannel; import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; import org.junit.Test; @@ -235,7 +230,6 @@ public Serializable[] serializableObjects() { .build(); BigQueryOptions otherOptions = options.toBuilder() .projectId("p2") - .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, @@ -254,18 +248,11 @@ public Serializable[] serializableObjects() { @Test public void testWriteChannelState() throws IOException, ClassNotFoundException { - BigQueryOptions options = BigQueryOptions.builder() - .projectId("p2") - .retryParams(RetryParams.defaultInstance()) - .build(); + BigQueryOptions options = BigQueryOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes upon failure @SuppressWarnings("resource") TableDataWriteChannel writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION, "upload-id"); - RestorableState state = writer.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); + assertRestorable(writer); } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index 6f9e09ca04bc..38265a26be2e 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -276,6 +276,16 @@ private static class NoAuthCredentialsState public AuthCredentials restore() { return INSTANCE; } + + @Override + public int hashCode() { + return getClass().getName().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof NoAuthCredentialsState; + } } @Override diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java index 39d4c4e75a1a..0dbcbeb65378 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java @@ -26,6 +26,7 @@ import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; @@ -259,6 +260,27 @@ boolean shouldRetry(Exception ex) { return retryResult == Interceptor.RetryResult.RETRY; } + @Override + public int hashCode() { + return Objects.hash(interceptors, retriableExceptions, nonRetriableExceptions, retryInfo); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof ExceptionHandler)) { + return false; + } + ExceptionHandler other = (ExceptionHandler) obj; + return Objects.equals(interceptors, other.interceptors) + && Objects.equals(retriableExceptions, other.retriableExceptions) + && Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions) + && Objects.equals(retryInfo, other.retryInfo); + + } + /** * Returns an instance which retry any checked exception and abort on any runtime exception. */ diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java index 533dfcde6ad8..a8136bfbf0cc 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java @@ -34,6 +34,9 @@ */ public abstract class BaseSerializationTest { + /** + * Returns all objects for which correct serialization must be tested. + */ public abstract Serializable[] serializableObjects(); @Test @@ -50,8 +53,7 @@ public void testSerializableObjects() throws Exception { } @SuppressWarnings("unchecked") - public T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { + public T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { output.writeObject(obj); @@ -61,4 +63,17 @@ public T serializeAndDeserialize(T obj) return (T) input.readObject(); } } + + /** + * Checks whether the state of a restorable object can correctly be captured, serialized and + * restored. + */ + public void assertRestorable(Restorable restorable) throws IOException, + ClassNotFoundException { + RestorableState state = restorable.capture(); + RestorableState deserializedState = serializeAndDeserialize(state); + assertEquals(state, deserializedState); + assertEquals(state.hashCode(), deserializedState.hashCode()); + assertEquals(state.toString(), deserializedState.toString()); + } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index aec9158e1ee0..46f1f73563f7 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -18,16 +18,60 @@ import com.google.common.collect.ImmutableList; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.Serializable; public class SerializationTest extends BaseSerializationTest { + private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance(); + private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance(); + private static final String JSON_KEY = "{\n" + + " \"private_key_id\": \"somekeyid\",\n" + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" + + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" + + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" + + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" + + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" + + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" + + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" + + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" + + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" + + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" + + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" + + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" + + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" + + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" + + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" + + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" + + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" + + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" + + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" + + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" + + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + + " \"type\": \"service_account\"\n" + + "}"; @Override public Serializable[] serializableObjects() { - return new Serializable[]{PAGE, RETRY_PARAMS}; + return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS}; + } + + @Test + public void testAuthCredentialState() throws IOException, ClassNotFoundException { + AuthCredentials credentials = AuthCredentials.createApplicationDefaults(); + assertRestorable(credentials); + credentials = AuthCredentials.createForAppEngine(); + assertRestorable(credentials); + credentials = AuthCredentials.noAuth(); + assertRestorable(credentials); + credentials = AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes())); + assertRestorable(credentials); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 7a6f065d6ca6..5cbf63b9624d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -21,7 +21,6 @@ import com.google.api.services.datastore.DatastoreV1; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; -import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.Projection; @@ -112,7 +111,6 @@ public java.io.Serializable[] serializableObjects() { .build(); DatastoreOptions otherOptions = options.toBuilder() .namespace("ns1") - .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .force(true) .build(); diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 2dcd3f9d3e7b..786865e0f872 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -18,10 +18,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import com.google.gcloud.Identity; import com.google.gcloud.BaseSerializationTest; +import com.google.gcloud.Identity; import com.google.gcloud.PageImpl; -import com.google.gcloud.RetryParams; import java.io.Serializable; import java.util.Collections; @@ -55,7 +54,6 @@ public Serializable[] serializableObjects() { ResourceManagerOptions options = ResourceManagerOptions.builder().build(); ResourceManagerOptions otherOptions = options.toBuilder() .projectId("some-unnecessary-project-ID") - .retryParams(RetryParams.defaultInstance()) .build(); return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index ee3d7c946312..392636ccd100 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -16,16 +16,11 @@ package com.google.gcloud.storage; -import static org.junit.Assert.assertEquals; - import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.ReadChannel; -import com.google.gcloud.RestorableState; -import com.google.gcloud.RetryParams; -import com.google.gcloud.WriteChannel; import com.google.gcloud.storage.Acl.Project.ProjectRole; import com.google.gcloud.storage.spi.StorageRpc; @@ -81,7 +76,6 @@ public Serializable[] serializableObjects() { .build(); StorageOptions otherOptions = options.toBuilder() .projectId("p2") - .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, @@ -92,34 +86,19 @@ public Serializable[] serializableObjects() { @Test public void testReadChannelState() throws IOException, ClassNotFoundException { - StorageOptions options = StorageOptions.builder() - .projectId("p2") - .retryParams(RetryParams.defaultInstance()) - .build(); + StorageOptions options = StorageOptions.builder().projectId("p2").build(); ReadChannel reader = new BlobReadChannel(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS); - RestorableState state = reader.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); - reader.close(); + assertRestorable(reader); } @Test public void testWriteChannelState() throws IOException, ClassNotFoundException { - StorageOptions options = StorageOptions.builder() - .projectId("p2") - .retryParams(RetryParams.defaultInstance()) - .build(); + StorageOptions options = StorageOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes to GCS upon failure @SuppressWarnings("resource") BlobWriteChannel writer = new BlobWriteChannel(options, BlobInfo.builder(BlobId.of("b", "n")).build(), "upload-id"); - RestorableState state = writer.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); + assertRestorable(writer); } } From 034432a4915439f8323cc898e38c47f5cd4f1788 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 17 Mar 2016 17:28:55 +0100 Subject: [PATCH 28/44] Remove serialization test for ApplicationDefaultAuthCredentials --- .../src/test/java/com/google/gcloud/SerializationTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index 46f1f73563f7..a9526a42d7d7 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -65,9 +65,7 @@ public Serializable[] serializableObjects() { @Test public void testAuthCredentialState() throws IOException, ClassNotFoundException { - AuthCredentials credentials = AuthCredentials.createApplicationDefaults(); - assertRestorable(credentials); - credentials = AuthCredentials.createForAppEngine(); + AuthCredentials credentials = AuthCredentials.createForAppEngine(); assertRestorable(credentials); credentials = AuthCredentials.noAuth(); assertRestorable(credentials); From ce597930debaa6ab8ffc16af25547e1bfa7283a6 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 17 Mar 2016 18:59:46 +0100 Subject: [PATCH 29/44] Add restorableObjects method to BaseSerializationTest --- .../gcloud/bigquery/SerializationTest.java | 12 +++--- .../com/google/gcloud/ExceptionHandler.java | 1 - .../google/gcloud/BaseSerializationTest.java | 39 +++++++++++-------- .../com/google/gcloud/SerializationTest.java | 21 +++++----- .../gcloud/datastore/SerializationTest.java | 8 +++- .../resourcemanager/SerializationTest.java | 8 +++- .../gcloud/storage/SerializationTest.java | 18 +++------ 7 files changed, 57 insertions(+), 50 deletions(-) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 087d263b0fa4..74644216c109 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -20,11 +20,9 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; +import com.google.gcloud.Restorable; import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; -import org.junit.Test; - -import java.io.IOException; import java.io.Serializable; import java.nio.charset.StandardCharsets; import java.util.List; @@ -223,7 +221,7 @@ public class SerializationTest extends BaseSerializationTest { private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO)); @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { BigQueryOptions options = BigQueryOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) @@ -246,13 +244,13 @@ public Serializable[] serializableObjects() { BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB, options, otherOptions}; } - @Test - public void testWriteChannelState() throws IOException, ClassNotFoundException { + @Override + protected Restorable[] restorableObjects() { BigQueryOptions options = BigQueryOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes upon failure @SuppressWarnings("resource") TableDataWriteChannel writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION, "upload-id"); - assertRestorable(writer); + return new Restorable[]{writer}; } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java index 0dbcbeb65378..0b3c923d1eb9 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java @@ -278,7 +278,6 @@ public boolean equals(Object obj) { && Objects.equals(retriableExceptions, other.retriableExceptions) && Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions) && Objects.equals(retryInfo, other.retryInfo); - } /** diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java index a8136bfbf0cc..e9ab3d47984b 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java @@ -16,6 +16,7 @@ package com.google.gcloud; +import static com.google.common.base.MoreObjects.firstNonNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; @@ -30,18 +31,26 @@ /** * Base class for serialization tests. To use this class in your tests override the - * {@code serializableObjects()} method to return all objects that must be serializable. + * {@code serializableObjects()} method to return all objects that must be serializable. Also + * override {@code restorableObjects()} method to return all restorable objects whose state must be + * tested for proper serialization. Both methods can return {@code null} if no such object needs to + * be tested. */ public abstract class BaseSerializationTest { /** * Returns all objects for which correct serialization must be tested. */ - public abstract Serializable[] serializableObjects(); + protected abstract Serializable[] serializableObjects(); + + /** + * Returns all restorable objects whose state must be tested for proper serialization. + */ + protected abstract Restorable[] restorableObjects(); @Test public void testSerializableObjects() throws Exception { - for (Serializable obj : serializableObjects()) { + for (Serializable obj : firstNonNull(serializableObjects(), new Serializable[0])) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj); assertEquals(obj, copy); @@ -52,6 +61,17 @@ public void testSerializableObjects() throws Exception { } } + @Test + public void testRestorableObjects() throws Exception { + for (Restorable restorable : firstNonNull(restorableObjects(), new Restorable[0])) { + RestorableState state = restorable.capture(); + RestorableState deserializedState = serializeAndDeserialize(state); + assertEquals(state, deserializedState); + assertEquals(state.hashCode(), deserializedState.hashCode()); + assertEquals(state.toString(), deserializedState.toString()); + } + } + @SuppressWarnings("unchecked") public T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); @@ -63,17 +83,4 @@ public T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundExc return (T) input.readObject(); } } - - /** - * Checks whether the state of a restorable object can correctly be captured, serialized and - * restored. - */ - public void assertRestorable(Restorable restorable) throws IOException, - ClassNotFoundException { - RestorableState state = restorable.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); - } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index a9526a42d7d7..c4bcfcc13b1c 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -18,8 +18,6 @@ import com.google.common.collect.ImmutableList; -import org.junit.Test; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; @@ -59,17 +57,18 @@ public class SerializationTest extends BaseSerializationTest { + "}"; @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS}; } - @Test - public void testAuthCredentialState() throws IOException, ClassNotFoundException { - AuthCredentials credentials = AuthCredentials.createForAppEngine(); - assertRestorable(credentials); - credentials = AuthCredentials.noAuth(); - assertRestorable(credentials); - credentials = AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes())); - assertRestorable(credentials); + @Override + protected Restorable[] restorableObjects() { + try { + return new Restorable[]{AuthCredentials.createForAppEngine(), AuthCredentials.noAuth(), + AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes()))}; + } catch (IOException ex) { + // never reached + throw new RuntimeException(ex); + } } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 5cbf63b9624d..3ea74ef7c4d6 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -21,6 +21,7 @@ import com.google.api.services.datastore.DatastoreV1; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; +import com.google.gcloud.Restorable; import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.Projection; @@ -103,7 +104,7 @@ public class SerializationTest extends BaseSerializationTest { private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); @Override - public java.io.Serializable[] serializableObjects() { + protected java.io.Serializable[] serializableObjects() { DatastoreOptions options = DatastoreOptions.builder() .authCredentials(AuthCredentials.createForAppEngine()) .normalizeDataset(false) @@ -120,4 +121,9 @@ public java.io.Serializable[] serializableObjects() { EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE, BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions}; } + + @Override + protected Restorable[] restorableObjects() { + return null; + } } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 786865e0f872..5bad065003e0 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -21,6 +21,7 @@ import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.Identity; import com.google.gcloud.PageImpl; +import com.google.gcloud.Restorable; import java.io.Serializable; import java.util.Collections; @@ -50,7 +51,7 @@ public class SerializationTest extends BaseSerializationTest { .build(); @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { ResourceManagerOptions options = ResourceManagerOptions.builder().build(); ResourceManagerOptions otherOptions = options.toBuilder() .projectId("some-unnecessary-project-ID") @@ -58,4 +59,9 @@ public Serializable[] serializableObjects() { return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; } + + @Override + protected Restorable[] restorableObjects() { + return null; + } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index 392636ccd100..6aae7b1a0cb2 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -21,12 +21,10 @@ import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.ReadChannel; +import com.google.gcloud.Restorable; import com.google.gcloud.storage.Acl.Project.ProjectRole; import com.google.gcloud.storage.spi.StorageRpc; -import org.junit.Test; - -import java.io.IOException; import java.io.Serializable; import java.util.Collections; import java.util.Map; @@ -69,7 +67,7 @@ public class SerializationTest extends BaseSerializationTest { private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { StorageOptions options = StorageOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) @@ -84,21 +82,15 @@ public Serializable[] serializableObjects() { BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions}; } - @Test - public void testReadChannelState() throws IOException, ClassNotFoundException { + @Override + protected Restorable[] restorableObjects() { StorageOptions options = StorageOptions.builder().projectId("p2").build(); ReadChannel reader = new BlobReadChannel(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS); - assertRestorable(reader); - } - - @Test - public void testWriteChannelState() throws IOException, ClassNotFoundException { - StorageOptions options = StorageOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes to GCS upon failure @SuppressWarnings("resource") BlobWriteChannel writer = new BlobWriteChannel(options, BlobInfo.builder(BlobId.of("b", "n")).build(), "upload-id"); - assertRestorable(writer); + return new Restorable[]{reader, writer}; } } From 0a113cd7431722d0c10d849a2760a41eb2ce4d87 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 14:25:25 +0100 Subject: [PATCH 30/44] Remove default contentType from Storage's compose --- .../java/com/google/gcloud/storage/spi/DefaultStorageRpc.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java index aa6085e161ed..3f465e0ab20e 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java @@ -319,9 +319,6 @@ private Storage.Objects.Delete deleteRequest(StorageObject blob, Map public StorageObject compose(Iterable sources, StorageObject target, Map targetOptions) { ComposeRequest request = new ComposeRequest(); - if (target.getContentType() == null) { - target.setContentType("application/octet-stream"); - } request.setDestination(target); List sourceObjects = new ArrayList<>(); for (StorageObject source : sources) { From 8840b33a9cfbb2ab780c846e12131716fda8e0ab Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 14:56:46 +0100 Subject: [PATCH 31/44] Remove default contentType from Bucket's create --- .../com/google/gcloud/storage/Bucket.java | 50 +++++++++++++++---- .../com/google/gcloud/storage/Storage.java | 2 - .../com/google/gcloud/storage/BucketTest.java | 12 ++--- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java index 5df305ff371c..e44bd60d785c 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java @@ -22,7 +22,6 @@ import static com.google.gcloud.storage.Bucket.BucketSourceOption.toSourceOptions; import com.google.common.base.Function; -import com.google.common.base.MoreObjects; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.gcloud.Page; @@ -633,15 +632,13 @@ public List get(String blobName1, String blobName2, String... blobNames) { * * @param blob a blob name * @param content the blob content - * @param contentType the blob content type. If {@code null} then - * {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used. + * @param contentType the blob content type * @param options options for blob creation * @return a complete blob information * @throws StorageException upon failure */ public Blob create(String blob, byte[] content, String contentType, BlobTargetOption... options) { - BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)) - .contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build(); + BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build(); StorageRpc.Tuple target = BlobTargetOption.toTargetOptions(blobInfo, options); return storage.create(target.x(), content, target.y()); @@ -654,16 +651,51 @@ public Blob create(String blob, byte[] content, String contentType, BlobTargetOp * * @param blob a blob name * @param content the blob content as a stream - * @param contentType the blob content type. If {@code null} then - * {@value com.google.gcloud.storage.Storage#DEFAULT_CONTENT_TYPE} is used. + * @param contentType the blob content type * @param options options for blob creation * @return a complete blob information * @throws StorageException upon failure */ public Blob create(String blob, InputStream content, String contentType, BlobWriteOption... options) { - BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)) - .contentType(MoreObjects.firstNonNull(contentType, Storage.DEFAULT_CONTENT_TYPE)).build(); + BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).contentType(contentType).build(); + StorageRpc.Tuple write = + BlobWriteOption.toWriteOptions(blobInfo, options); + return storage.create(write.x(), content, write.y()); + } + + /** + * Creates a new blob in this bucket. Direct upload is used to upload {@code content}. + * For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)} + * is recommended as it uses resumable upload. MD5 and CRC32C hashes of {@code content} are + * computed and used for validating transferred data. + * + * @param blob a blob name + * @param content the blob content + * @param options options for blob creation + * @return a complete blob information + * @throws StorageException upon failure + */ + public Blob create(String blob, byte[] content, BlobTargetOption... options) { + BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build(); + StorageRpc.Tuple target = + BlobTargetOption.toTargetOptions(blobInfo, options); + return storage.create(target.x(), content, target.y()); + } + + /** + * Creates a new blob in this bucket. Direct upload is used to upload {@code content}. + * For large content, {@link Blob#writer(com.google.gcloud.storage.Storage.BlobWriteOption...)} + * is recommended as it uses resumable upload. + * + * @param blob a blob name + * @param content the blob content as a stream + * @param options options for blob creation + * @return a complete blob information + * @throws StorageException upon failure + */ + public Blob create(String blob, InputStream content, BlobWriteOption... options) { + BlobInfo blobInfo = BlobInfo.builder(BlobId.of(name(), blob)).build(); StorageRpc.Tuple write = BlobWriteOption.toWriteOptions(blobInfo, options); return storage.create(write.x(), content, write.y()); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index c30111e50835..35fc6117cbc2 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -53,8 +53,6 @@ */ public interface Storage extends Service { - String DEFAULT_CONTENT_TYPE = "application/octet-stream"; - enum PredefinedAcl { AUTHENTICATED_READ("authenticatedRead"), ALL_AUTHENTICATED_USERS("allAuthenticatedUsers"), diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java index 236411e0c2d8..53056c39c0dc 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java @@ -293,16 +293,16 @@ public void testCreate() throws Exception { } @Test - public void testCreateNullContentType() throws Exception { + public void testCreateNoContentType() throws Exception { initializeExpectedBucket(5); - BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build(); + BlobInfo info = BlobInfo.builder("b", "n").build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; expect(storage.options()).andReturn(mockOptions); expect(storage.create(info, content)).andReturn(expectedBlob); replay(storage); initializeBucket(); - Blob blob = bucket.create("n", content, null); + Blob blob = bucket.create("n", content); assertEquals(expectedBlob, blob); } @@ -388,9 +388,9 @@ public void testCreateFromStream() throws Exception { } @Test - public void testCreateFromStreamNullContentType() throws Exception { + public void testCreateFromStreamNoContentType() throws Exception { initializeExpectedBucket(5); - BlobInfo info = BlobInfo.builder("b", "n").contentType(Storage.DEFAULT_CONTENT_TYPE).build(); + BlobInfo info = BlobInfo.builder("b", "n").build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; InputStream streamContent = new ByteArrayInputStream(content); @@ -398,7 +398,7 @@ public void testCreateFromStreamNullContentType() throws Exception { expect(storage.create(info, streamContent)).andReturn(expectedBlob); replay(storage); initializeBucket(); - Blob blob = bucket.create("n", streamContent, null); + Blob blob = bucket.create("n", streamContent); assertEquals(expectedBlob, blob); } From 5c4e2886f8257470932341191b699e1d621c890e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 16:01:58 +0100 Subject: [PATCH 32/44] Make NoAuthCredentials constructor private --- .../src/main/java/com/google/gcloud/AuthCredentials.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index 38265a26be2e..27cafc181505 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -288,6 +288,8 @@ public boolean equals(Object obj) { } } + private NoAuthCredentials() {} + @Override public GoogleCredentials credentials() { return null; From 19e0c6e9736bc13a26d6a8f811322ee97e477200 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 16:03:46 +0100 Subject: [PATCH 33/44] Add IamPolicy to SerializationTest --- .../com/google/gcloud/SerializationTest.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index c4bcfcc13b1c..194479ac365c 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -24,11 +24,34 @@ public class SerializationTest extends BaseSerializationTest { + private static class SomeIamPolicy extends IamPolicy { + + private static final long serialVersionUID = 271243551016958285L; + + private static class Builder extends IamPolicy.Builder { + + @Override + public SomeIamPolicy build() { + return new SomeIamPolicy(this); + } + } + + protected SomeIamPolicy(Builder builder) { + super(builder); + } + + @Override + public Builder toBuilder() { + return new Builder(); + } + } + private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance(); private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance(); + private static final SomeIamPolicy SOME_IAM_POLICY = new SomeIamPolicy.Builder().build(); private static final String JSON_KEY = "{\n" + " \"private_key_id\": \"somekeyid\",\n" + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" @@ -58,7 +81,7 @@ public class SerializationTest extends BaseSerializationTest { @Override protected Serializable[] serializableObjects() { - return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS}; + return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS, SOME_IAM_POLICY}; } @Override From 83ddb08d0e6aae40707ab8b9d071fa62854e8769 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 16:08:37 +0100 Subject: [PATCH 34/44] Add equals and hashCode to exceptions. Add exceptions to serialization tests --- .../gcloud/bigquery/BigQueryException.java | 18 ++++++++ .../gcloud/bigquery/SerializationTest.java | 4 +- .../google/gcloud/BaseServiceException.java | 45 ++++++++++++++----- .../com/google/gcloud/SerializationTest.java | 5 ++- .../gcloud/datastore/SerializationTest.java | 5 ++- .../resourcemanager/SerializationTest.java | 5 ++- .../gcloud/storage/SerializationTest.java | 4 +- 7 files changed, 71 insertions(+), 15 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java index a157afd25db2..e78734a2899e 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java @@ -22,6 +22,7 @@ import com.google.gcloud.RetryHelper.RetryInterruptedException; import java.io.IOException; +import java.util.Objects; import java.util.Set; /** @@ -73,6 +74,23 @@ protected Set retryableErrors() { return RETRYABLE_ERRORS; } + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof BigQueryException)) { + return false; + } + BigQueryException other = (BigQueryException) obj; + return super.equals(other) && Objects.equals(error, other.error); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), error); + } + /** * Translate RetryHelperException to the BigQueryException that caused the error. This method will * always throw an exception. diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 74644216c109..111df074ffa2 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -219,6 +219,8 @@ public class SerializationTest extends BaseSerializationTest { new Dataset(BIGQUERY, new DatasetInfo.BuilderImpl(DATASET_INFO)); private static final Table TABLE = new Table(BIGQUERY, new TableInfo.BuilderImpl(TABLE_INFO)); private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO)); + private static final BigQueryException BIG_QUERY_EXCEPTION = + new BigQueryException(42, "message", BIGQUERY_ERROR); @Override protected Serializable[] serializableObjects() { @@ -237,7 +239,7 @@ protected Serializable[] serializableObjects() { LOAD_STATISTICS, QUERY_STATISTICS, BIGQUERY_ERROR, JOB_STATUS, JOB_ID, COPY_JOB_CONFIGURATION, EXTRACT_JOB_CONFIGURATION, LOAD_CONFIGURATION, LOAD_JOB_CONFIGURATION, QUERY_JOB_CONFIGURATION, JOB_INFO, INSERT_ALL_REQUEST, - INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, + INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, BIG_QUERY_EXCEPTION, BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(), BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(), diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java index 365243904436..4e0d03e0073a 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java @@ -32,6 +32,16 @@ */ public class BaseServiceException extends RuntimeException { + private static final long serialVersionUID = 759921776378760835L; + public static final int UNKNOWN_CODE = 0; + + private final int code; + private final boolean retryable; + private final String reason; + private final boolean idempotent; + private final String location; + private final String debugInfo; + protected static final class Error implements Serializable { private static final long serialVersionUID = -4019600198652965721L; @@ -79,16 +89,6 @@ public int hashCode() { } } - private static final long serialVersionUID = 759921776378760835L; - public static final int UNKNOWN_CODE = 0; - - private final int code; - private final boolean retryable; - private final String reason; - private final boolean idempotent; - private final String location; - private final String debugInfo; - public BaseServiceException(IOException exception, boolean idempotent) { super(message(exception), exception); int code = UNKNOWN_CODE; @@ -198,6 +198,31 @@ protected String debugInfo() { return debugInfo; } + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof BaseServiceException)) { + return false; + } + BaseServiceException other = (BaseServiceException) obj; + return Objects.equals(getCause(), other.getCause()) + && Objects.equals(getMessage(), other.getMessage()) + && code == other.code + && retryable == other.retryable + && Objects.equals(reason, other.reason) + && idempotent == other.idempotent + && Objects.equals(location, other.location) + && Objects.equals(debugInfo, other.debugInfo); + } + + @Override + public int hashCode() { + return Objects.hash(getCause(), getMessage(), code, retryable, reason, idempotent, location, + debugInfo); + } + protected static String reason(GoogleJsonError error) { if (error.getErrors() != null && !error.getErrors().isEmpty()) { return error.getErrors().get(0).getReason(); diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index 194479ac365c..3255a17333aa 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -46,6 +46,8 @@ public Builder toBuilder() { } } + private static final BaseServiceException BASE_SERVICE_EXCEPTION = + new BaseServiceException(42, "message", "reason", true); private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance(); private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = @@ -81,7 +83,8 @@ public Builder toBuilder() { @Override protected Serializable[] serializableObjects() { - return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS, SOME_IAM_POLICY}; + return new Serializable[]{BASE_SERVICE_EXCEPTION, EXCEPTION_HANDLER, IDENTITY, PAGE, + RETRY_PARAMS, SOME_IAM_POLICY}; } @Override diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 3ea74ef7c4d6..b9e78800ffab 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -102,6 +102,8 @@ public class SerializationTest extends BaseSerializationTest { .addValue(new NullValue()) .build(); private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); + private static final DatastoreException DATASTORE_EXCEPTION = + new DatastoreException(42, "message", "reason"); @Override protected java.io.Serializable[] serializableObjects() { @@ -119,7 +121,8 @@ protected java.io.Serializable[] serializableObjects() { ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE, - BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions}; + BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, DATASTORE_EXCEPTION, options, + otherOptions}; } @Override diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 5bad065003e0..c6e907da16a0 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -49,6 +49,8 @@ public class SerializationTest extends BaseSerializationTest { private static final Policy POLICY = Policy.builder() .addBinding(Policy.Role.viewer(), ImmutableSet.of(Identity.user("abc@gmail.com"))) .build(); + private static final ResourceManagerException RESOURCE_MANAGER_EXCEPTION = + new ResourceManagerException(42, "message"); @Override protected Serializable[] serializableObjects() { @@ -57,7 +59,8 @@ protected Serializable[] serializableObjects() { .projectId("some-unnecessary-project-ID") .build(); return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, - PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; + PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, RESOURCE_MANAGER_EXCEPTION, options, + otherOptions}; } @Override diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index 6aae7b1a0cb2..613cb81c3549 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -52,6 +52,7 @@ public class SerializationTest extends BaseSerializationTest { Collections.>emptyList()); private static final PageImpl PAGE_RESULT = new PageImpl<>(null, "c", Collections.singletonList(BLOB)); + private static final StorageException STORAGE_EXCEPTION = new StorageException(42, "message"); private static final Storage.BlobListOption BLOB_LIST_OPTIONS = Storage.BlobListOption.pageSize(100); private static final Storage.BlobSourceOption BLOB_SOURCE_OPTIONS = @@ -79,7 +80,8 @@ protected Serializable[] serializableObjects() { return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, BLOB_INFO, BLOB, BUCKET_INFO, BUCKET, ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT, BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, - BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions}; + BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, STORAGE_EXCEPTION, + options, otherOptions}; } @Override From 22636e276005b809e7bef8cc869a38cba786b162 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 17:00:47 +0100 Subject: [PATCH 35/44] Remove checks for contentType in CopyRequest - update CopyRequest to hold both targetId and targetInfo - avoid sending storage object in StorageRpc.rewrite when only targetId is set - refactor CopyWriter and state - add integration tests --- .../com/google/gcloud/storage/CopyWriter.java | 57 +++++--- .../com/google/gcloud/storage/Storage.java | 80 ++++++------ .../google/gcloud/storage/StorageImpl.java | 15 ++- .../gcloud/storage/spi/DefaultStorageRpc.java | 4 +- .../google/gcloud/storage/spi/StorageRpc.java | 19 ++- .../com/google/gcloud/storage/BlobTest.java | 12 +- .../gcloud/storage/CopyRequestTest.java | 53 +++----- .../google/gcloud/storage/CopyWriterTest.java | 122 +++++++++++++++--- .../gcloud/storage/StorageImplTest.java | 14 +- .../gcloud/storage/it/ITStorageTest.java | 51 ++++++++ 10 files changed, 288 insertions(+), 139 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java index 62b39e005369..38d9c0d5e952 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java @@ -65,11 +65,11 @@ public class CopyWriter implements Restorable { * * @throws StorageException upon failure */ - public BlobInfo result() { + public Blob result() { while (!isDone()) { copyChunk(); } - return BlobInfo.fromPb(rewriteResponse.result); + return Blob.fromPb(serviceOptions.service(), rewriteResponse.result); } /** @@ -120,8 +120,12 @@ public RestorableState capture() { serviceOptions, BlobId.fromPb(rewriteResponse.rewriteRequest.source), rewriteResponse.rewriteRequest.sourceOptions, - BlobInfo.fromPb(rewriteResponse.rewriteRequest.target), + BlobId.of(rewriteResponse.rewriteRequest.targetBucket, + rewriteResponse.rewriteRequest.targetName), rewriteResponse.rewriteRequest.targetOptions) + .targetInfo(rewriteResponse.rewriteRequest.targetObject != null + ? BlobInfo.fromPb(rewriteResponse.rewriteRequest.targetObject) : null) + .result(rewriteResponse.result != null ? BlobInfo.fromPb(rewriteResponse.result) : null) .blobSize(blobSize()) .isDone(isDone()) .megabytesCopiedPerChunk(rewriteResponse.rewriteRequest.megabytesRewrittenPerCall) @@ -132,12 +136,13 @@ public RestorableState capture() { static class StateImpl implements RestorableState, Serializable { - private static final long serialVersionUID = 8279287678903181701L; + private static final long serialVersionUID = 1693964441435822700L; private final StorageOptions serviceOptions; private final BlobId source; private final Map sourceOptions; - private final BlobInfo target; + private final BlobId targetId; + private final BlobInfo targetInfo; private final Map targetOptions; private final BlobInfo result; private final long blobSize; @@ -150,7 +155,8 @@ static class StateImpl implements RestorableState, Serializable { this.serviceOptions = builder.serviceOptions; this.source = builder.source; this.sourceOptions = builder.sourceOptions; - this.target = builder.target; + this.targetId = builder.targetId; + this.targetInfo = builder.targetInfo; this.targetOptions = builder.targetOptions; this.result = builder.result; this.blobSize = builder.blobSize; @@ -165,8 +171,9 @@ static class Builder { private final StorageOptions serviceOptions; private final BlobId source; private final Map sourceOptions; - private final BlobInfo target; + private final BlobId targetId; private final Map targetOptions; + private BlobInfo targetInfo; private BlobInfo result; private long blobSize; private boolean isDone; @@ -176,14 +183,19 @@ static class Builder { private Builder(StorageOptions options, BlobId source, Map sourceOptions, - BlobInfo target, Map targetOptions) { + BlobId targetId, Map targetOptions) { this.serviceOptions = options; this.source = source; this.sourceOptions = sourceOptions; - this.target = target; + this.targetId = targetId; this.targetOptions = targetOptions; } + Builder targetInfo(BlobInfo targetInfo) { + this.targetInfo = targetInfo; + return this; + } + Builder result(BlobInfo result) { this.result = result; return this; @@ -220,15 +232,16 @@ RestorableState build() { } static Builder builder(StorageOptions options, BlobId source, - Map sourceOptions, BlobInfo target, + Map sourceOptions, BlobId targetId, Map targetOptions) { - return new Builder(options, source, sourceOptions, target, targetOptions); + return new Builder(options, source, sourceOptions, targetId, targetOptions); } @Override public CopyWriter restore() { - RewriteRequest rewriteRequest = new RewriteRequest( - source.toPb(), sourceOptions, target.toPb(), targetOptions, megabytesCopiedPerChunk); + RewriteRequest rewriteRequest = new RewriteRequest(source.toPb(), sourceOptions, + targetId.bucket(), targetId.name(), targetInfo != null ? targetInfo.toPb() : null, + targetOptions, megabytesCopiedPerChunk); RewriteResponse rewriteResponse = new RewriteResponse(rewriteRequest, result != null ? result.toPb() : null, blobSize, isDone, rewriteToken, totalBytesCopied); @@ -237,8 +250,9 @@ public CopyWriter restore() { @Override public int hashCode() { - return Objects.hash(serviceOptions, source, sourceOptions, target, targetOptions, result, - blobSize, isDone, megabytesCopiedPerChunk, rewriteToken, totalBytesCopied); + return Objects.hash(serviceOptions, source, sourceOptions, targetId, targetInfo, + targetOptions, result, blobSize, isDone, megabytesCopiedPerChunk, rewriteToken, + totalBytesCopied); } @Override @@ -253,7 +267,8 @@ public boolean equals(Object obj) { return Objects.equals(this.serviceOptions, other.serviceOptions) && Objects.equals(this.source, other.source) && Objects.equals(this.sourceOptions, other.sourceOptions) - && Objects.equals(this.target, other.target) + && Objects.equals(this.targetId, other.targetId) + && Objects.equals(this.targetInfo, other.targetInfo) && Objects.equals(this.targetOptions, other.targetOptions) && Objects.equals(this.result, other.result) && Objects.equals(this.rewriteToken, other.rewriteToken) @@ -267,10 +282,14 @@ public boolean equals(Object obj) { public String toString() { return MoreObjects.toStringHelper(this) .add("source", source) - .add("target", target) - .add("isDone", isDone) - .add("totalBytesRewritten", totalBytesCopied) + .add("targetId", targetId) + .add("targetInfo", targetInfo) + .add("result", result) .add("blobSize", blobSize) + .add("isDone", isDone) + .add("rewriteToken", rewriteToken) + .add("totalBytesCopied", totalBytesCopied) + .add("megabytesCopiedPerChunk", megabytesCopiedPerChunk) .toString(); } } diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 35fc6117cbc2..8204f0105e7e 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -962,7 +962,8 @@ class CopyRequest implements Serializable { private final BlobId source; private final List sourceOptions; - private final BlobInfo target; + private final BlobId targetId; + private final BlobInfo targetInfo; private final List targetOptions; private final Long megabytesCopiedPerChunk; @@ -971,7 +972,8 @@ public static class Builder { private final Set sourceOptions = new LinkedHashSet<>(); private final Set targetOptions = new LinkedHashSet<>(); private BlobId source; - private BlobInfo target; + private BlobId targetId; + private BlobInfo targetInfo; private Long megabytesCopiedPerChunk; /** @@ -1019,39 +1021,37 @@ public Builder sourceOptions(Iterable options) { * * @return the builder */ - public Builder target(BlobId target) { - this.target = BlobInfo.builder(target).build(); + public Builder target(BlobId targetId) { + this.targetId = targetId; return this; } /** * Sets the copy target and target options. {@code target} parameter is used to override - * source blob information (e.g. {@code contentType}, {@code contentLanguage}). {@code - * target.contentType} is a required field. + * source blob information (e.g. {@code contentType}, {@code contentLanguage}). Target blob + * information is set exactly to {@code target}, no information is inherited from the source + * blob. If not set, target blob information is inherited from the source blob. * * @return the builder - * @throws IllegalArgumentException if {@code target.contentType} is {@code null} */ - public Builder target(BlobInfo target, BlobTargetOption... options) - throws IllegalArgumentException { - checkContentType(target); - this.target = target; + public Builder target(BlobInfo targetInfo, BlobTargetOption... options) { + this.targetId = targetInfo.blobId(); + this.targetInfo = targetInfo; Collections.addAll(targetOptions, options); return this; } /** * Sets the copy target and target options. {@code target} parameter is used to override - * source blob information (e.g. {@code contentType}, {@code contentLanguage}). {@code - * target.contentType} is a required field. + * source blob information (e.g. {@code contentType}, {@code contentLanguage}). Target blob + * information is set exactly to {@code target}, no information is inherited from the source + * blob. If not set, target blob information is inherited from the source blob. * * @return the builder - * @throws IllegalArgumentException if {@code target.contentType} is {@code null} */ - public Builder target(BlobInfo target, Iterable options) - throws IllegalArgumentException { - checkContentType(target); - this.target = target; + public Builder target(BlobInfo targetInfo, Iterable options) { + this.targetId = targetInfo.blobId(); + this.targetInfo = targetInfo; Iterables.addAll(targetOptions, options); return this; } @@ -1072,8 +1072,6 @@ public Builder megabytesCopiedPerChunk(Long megabytesCopiedPerChunk) { * Creates a {@code CopyRequest} object. */ public CopyRequest build() { - checkNotNull(source); - checkNotNull(target); return new CopyRequest(this); } } @@ -1081,7 +1079,8 @@ public CopyRequest build() { private CopyRequest(Builder builder) { source = checkNotNull(builder.source); sourceOptions = ImmutableList.copyOf(builder.sourceOptions); - target = checkNotNull(builder.target); + targetId = checkNotNull(builder.targetId); + targetInfo = builder.targetInfo; targetOptions = ImmutableList.copyOf(builder.targetOptions); megabytesCopiedPerChunk = builder.megabytesCopiedPerChunk; } @@ -1101,10 +1100,20 @@ public List sourceOptions() { } /** - * Returns the {@link BlobInfo} for the target blob. + * Returns the {@link BlobId} for the target blob. */ - public BlobInfo target() { - return target; + public BlobId targetId() { + return targetId; + } + + /** + * Returns the {@link BlobInfo} for the target blob. If set, this value is used to replace + * source blob information (e.g. {@code contentType}, {@code contentLanguage}). Target blob + * information is set exactly to this value, no information is inherited from the source blob. + * If not set, target blob information is inherited from the source blob. + */ + public BlobInfo targetInfo() { + return targetInfo; } /** @@ -1125,34 +1134,27 @@ public Long megabytesCopiedPerChunk() { /** * Creates a copy request. {@code target} parameter is used to override source blob information - * (e.g. {@code contentType}, {@code contentLanguage}). {@code target.contentType} is a required - * field. + * (e.g. {@code contentType}, {@code contentLanguage}). * * @param sourceBucket name of the bucket containing the source blob * @param sourceBlob name of the source blob * @param target a {@code BlobInfo} object for the target blob * @return a copy request - * @throws IllegalArgumentException if {@code target.contentType} is {@code null} */ - public static CopyRequest of(String sourceBucket, String sourceBlob, BlobInfo target) - throws IllegalArgumentException { - checkContentType(target); + public static CopyRequest of(String sourceBucket, String sourceBlob, BlobInfo target) { return builder().source(sourceBucket, sourceBlob).target(target).build(); } /** - * Creates a copy request. {@code target} parameter is used to override source blob information - * (e.g. {@code contentType}, {@code contentLanguage}). {@code target.contentType} is a required - * field. + * Creates a copy request. {@code target} parameter is used to replace source blob information + * (e.g. {@code contentType}, {@code contentLanguage}). Target blob information is set exactly + * to {@code target}, no information is inherited from the source blob. * * @param sourceBlobId a {@code BlobId} object for the source blob * @param target a {@code BlobInfo} object for the target blob * @return a copy request - * @throws IllegalArgumentException if {@code target.contentType} is {@code null} */ - public static CopyRequest of(BlobId sourceBlobId, BlobInfo target) - throws IllegalArgumentException { - checkContentType(target); + public static CopyRequest of(BlobId sourceBlobId, BlobInfo target) { return builder().source(sourceBlobId).target(target).build(); } @@ -1214,10 +1216,6 @@ public static CopyRequest of(BlobId sourceBlobId, BlobId targetBlobId) { public static Builder builder() { return new Builder(); } - - private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentException { - checkArgument(blobInfo.contentType() != null, "Blob content type can not be null"); - } } /** diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index d58c9e43aea9..30c0046b802c 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -413,15 +413,20 @@ public CopyWriter copy(final CopyRequest copyRequest) { final StorageObject source = copyRequest.source().toPb(); final Map sourceOptions = optionMap(copyRequest.source().generation(), null, copyRequest.sourceOptions(), true); - final StorageObject target = copyRequest.target().toPb(); - final Map targetOptions = optionMap(copyRequest.target().generation(), - copyRequest.target().metageneration(), copyRequest.targetOptions()); + final BlobId targetId = copyRequest.targetId(); + final StorageObject targetObject = + copyRequest.targetInfo() != null ? copyRequest.targetInfo().toPb() : null; + final Map targetOptions = optionMap( + copyRequest.targetInfo() != null ? copyRequest.targetInfo().generation() : null, + copyRequest.targetInfo() != null ? copyRequest.targetInfo().metageneration() : null, + copyRequest.targetOptions()); try { RewriteResponse rewriteResponse = runWithRetries(new Callable() { @Override public RewriteResponse call() { - return storageRpc.openRewrite(new StorageRpc.RewriteRequest(source, sourceOptions, target, - targetOptions, copyRequest.megabytesCopiedPerChunk())); + return storageRpc.openRewrite(new StorageRpc.RewriteRequest(source, sourceOptions, + targetId.bucket(), targetId.name(), targetObject, targetOptions, + copyRequest.megabytesCopiedPerChunk())); } }, options().retryParams(), EXCEPTION_HANDLER); return new CopyWriter(options(), rewriteResponse); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java index 3f465e0ab20e..7f13212556aa 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java @@ -580,8 +580,8 @@ private RewriteResponse rewrite(RewriteRequest req, String token) { Long maxBytesRewrittenPerCall = req.megabytesRewrittenPerCall != null ? req.megabytesRewrittenPerCall * MEGABYTE : null; com.google.api.services.storage.model.RewriteResponse rewriteResponse = storage.objects() - .rewrite(req.source.getBucket(), req.source.getName(), req.target.getBucket(), - req.target.getName(), req.target.getContentType() != null ? req.target : null) + .rewrite(req.source.getBucket(), req.source.getName(), req.targetBucket, + req.targetName, req.targetObject) .setSourceGeneration(req.source.getGeneration()) .setRewriteToken(token) .setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java index d239a475a6dd..7256368e189f 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java @@ -138,16 +138,20 @@ class RewriteRequest { public final StorageObject source; public final Map sourceOptions; - public final StorageObject target; + public final String targetBucket; + public final String targetName; + public final StorageObject targetObject; public final Map targetOptions; public final Long megabytesRewrittenPerCall; public RewriteRequest(StorageObject source, Map sourceOptions, - StorageObject target, Map targetOptions, - Long megabytesRewrittenPerCall) { + String targetBucket, String targetName, StorageObject targetObject, + Map targetOptions, Long megabytesRewrittenPerCall) { this.source = source; this.sourceOptions = sourceOptions; - this.target = target; + this.targetBucket = targetBucket; + this.targetName = targetName; + this.targetObject = targetObject; this.targetOptions = targetOptions; this.megabytesRewrittenPerCall = megabytesRewrittenPerCall; } @@ -163,14 +167,17 @@ public boolean equals(Object obj) { final RewriteRequest other = (RewriteRequest) obj; return Objects.equals(this.source, other.source) && Objects.equals(this.sourceOptions, other.sourceOptions) - && Objects.equals(this.target, other.target) + && Objects.equals(this.targetBucket, other.targetBucket) + && Objects.equals(this.targetName, other.targetName) + && Objects.equals(this.targetObject, other.targetObject) && Objects.equals(this.targetOptions, other.targetOptions) && Objects.equals(this.megabytesRewrittenPerCall, other.megabytesRewrittenPerCall); } @Override public int hashCode() { - return Objects.hash(source, sourceOptions, target, targetOptions, megabytesRewrittenPerCall); + return Objects.hash(source, sourceOptions, targetBucket, targetName, targetObject, + targetOptions, megabytesRewrittenPerCall); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java index 5a6173c08199..2d4920816c38 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java @@ -222,7 +222,6 @@ public void testDelete() throws Exception { @Test public void testCopyToBucket() throws Exception { initializeExpectedBlob(2); - BlobInfo target = BlobInfo.builder(BlobId.of("bt", "n")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture capturedCopyRequest = Capture.newInstance(); expect(storage.options()).andReturn(mockOptions); @@ -232,7 +231,8 @@ public void testCopyToBucket() throws Exception { CopyWriter returnedCopyWriter = blob.copyTo("bt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().source(), blob.blobId()); - assertEquals(capturedCopyRequest.getValue().target(), target); + assertEquals(capturedCopyRequest.getValue().targetId(), BlobId.of("bt", "n")); + assertNull(capturedCopyRequest.getValue().targetInfo()); assertTrue(capturedCopyRequest.getValue().sourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().targetOptions().isEmpty()); } @@ -240,7 +240,6 @@ public void testCopyToBucket() throws Exception { @Test public void testCopyTo() throws Exception { initializeExpectedBlob(2); - BlobInfo target = BlobInfo.builder(BlobId.of("bt", "nt")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture capturedCopyRequest = Capture.newInstance(); expect(storage.options()).andReturn(mockOptions); @@ -250,7 +249,8 @@ public void testCopyTo() throws Exception { CopyWriter returnedCopyWriter = blob.copyTo("bt", "nt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().source(), blob.blobId()); - assertEquals(capturedCopyRequest.getValue().target(), target); + assertEquals(capturedCopyRequest.getValue().targetId(), BlobId.of("bt", "nt")); + assertNull(capturedCopyRequest.getValue().targetInfo()); assertTrue(capturedCopyRequest.getValue().sourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().targetOptions().isEmpty()); } @@ -260,7 +260,6 @@ public void testCopyToBlobId() throws Exception { initializeExpectedBlob(2); BlobId targetId = BlobId.of("bt", "nt"); CopyWriter copyWriter = createMock(CopyWriter.class); - BlobInfo target = BlobInfo.builder(targetId).build(); Capture capturedCopyRequest = Capture.newInstance(); expect(storage.options()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); @@ -269,7 +268,8 @@ public void testCopyToBlobId() throws Exception { CopyWriter returnedCopyWriter = blob.copyTo(targetId); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().source(), blob.blobId()); - assertEquals(capturedCopyRequest.getValue().target(), target); + assertEquals(capturedCopyRequest.getValue().targetId(), targetId); + assertNull(capturedCopyRequest.getValue().targetInfo()); assertTrue(capturedCopyRequest.getValue().sourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().targetOptions().isEmpty()); } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java index b7e8d14e53a1..5700ea804cd6 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java @@ -18,6 +18,7 @@ import static com.google.gcloud.storage.Storage.PredefinedAcl.PUBLIC_READ; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import com.google.common.collect.ImmutableList; import com.google.gcloud.storage.Storage.BlobSourceOption; @@ -52,7 +53,8 @@ public void testCopyRequest() { assertEquals(SOURCE_BLOB_ID, copyRequest1.source()); assertEquals(1, copyRequest1.sourceOptions().size()); assertEquals(BlobSourceOption.generationMatch(1), copyRequest1.sourceOptions().get(0)); - assertEquals(TARGET_BLOB_INFO, copyRequest1.target()); + assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest1.targetId()); + assertEquals(TARGET_BLOB_INFO, copyRequest1.targetInfo()); assertEquals(1, copyRequest1.targetOptions().size()); assertEquals(BlobTargetOption.predefinedAcl(PUBLIC_READ), copyRequest1.targetOptions().get(0)); @@ -61,14 +63,16 @@ public void testCopyRequest() { .target(TARGET_BLOB_ID) .build(); assertEquals(SOURCE_BLOB_ID, copyRequest2.source()); - assertEquals(BlobInfo.builder(TARGET_BLOB_ID).build(), copyRequest2.target()); + assertEquals(TARGET_BLOB_ID, copyRequest2.targetId()); + assertNull(copyRequest2.targetInfo()); Storage.CopyRequest copyRequest3 = Storage.CopyRequest.builder() .source(SOURCE_BLOB_ID) .target(TARGET_BLOB_INFO, ImmutableList.of(BlobTargetOption.predefinedAcl(PUBLIC_READ))) .build(); assertEquals(SOURCE_BLOB_ID, copyRequest3.source()); - assertEquals(TARGET_BLOB_INFO, copyRequest3.target()); + assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest3.targetId()); + assertEquals(TARGET_BLOB_INFO, copyRequest3.targetInfo()); assertEquals(ImmutableList.of(BlobTargetOption.predefinedAcl(PUBLIC_READ)), copyRequest3.targetOptions()); } @@ -77,53 +81,34 @@ public void testCopyRequest() { public void testCopyRequestOf() { Storage.CopyRequest copyRequest1 = Storage.CopyRequest.of(SOURCE_BLOB_ID, TARGET_BLOB_INFO); assertEquals(SOURCE_BLOB_ID, copyRequest1.source()); - assertEquals(TARGET_BLOB_INFO, copyRequest1.target()); + assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest1.targetId()); + assertEquals(TARGET_BLOB_INFO, copyRequest1.targetInfo()); Storage.CopyRequest copyRequest2 = Storage.CopyRequest.of(SOURCE_BLOB_ID, TARGET_BLOB_NAME); assertEquals(SOURCE_BLOB_ID, copyRequest2.source()); - assertEquals(BlobInfo.builder(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME).build(), - copyRequest2.target()); + assertEquals(BlobId.of(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME), copyRequest2.targetId()); + assertNull(copyRequest2.targetInfo()); Storage.CopyRequest copyRequest3 = Storage.CopyRequest.of(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME, TARGET_BLOB_INFO); assertEquals(SOURCE_BLOB_ID, copyRequest3.source()); - assertEquals(TARGET_BLOB_INFO, copyRequest3.target()); + assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest3.targetId()); + assertEquals(TARGET_BLOB_INFO, copyRequest3.targetInfo()); Storage.CopyRequest copyRequest4 = Storage.CopyRequest.of(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME, TARGET_BLOB_NAME); assertEquals(SOURCE_BLOB_ID, copyRequest4.source()); - assertEquals(BlobInfo.builder(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME).build(), - copyRequest4.target()); + assertEquals(BlobId.of(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME), copyRequest4.targetId()); + assertNull(copyRequest4.targetInfo()); Storage.CopyRequest copyRequest5 = Storage.CopyRequest.of(SOURCE_BLOB_ID, TARGET_BLOB_ID); assertEquals(SOURCE_BLOB_ID, copyRequest5.source()); - assertEquals(BlobInfo.builder(TARGET_BLOB_ID).build(), copyRequest5.target()); + assertEquals(TARGET_BLOB_ID, copyRequest5.targetId()); + assertNull(copyRequest5.targetInfo()); Storage.CopyRequest copyRequest6 = Storage.CopyRequest.of(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME, TARGET_BLOB_ID); assertEquals(SOURCE_BLOB_ID, copyRequest6.source()); - assertEquals(BlobInfo.builder(TARGET_BLOB_ID).build(), copyRequest6.target()); - } - - @Test - public void testCopyRequestFail() { - thrown.expect(IllegalArgumentException.class); - Storage.CopyRequest.builder() - .source(SOURCE_BLOB_ID) - .target(BlobInfo.builder(TARGET_BLOB_ID).build()) - .build(); - } - - @Test - public void testCopyRequestOfBlobInfoFail() { - thrown.expect(IllegalArgumentException.class); - Storage.CopyRequest.of(SOURCE_BLOB_ID, BlobInfo.builder(TARGET_BLOB_ID).build()); - } - - @Test - public void testCopyRequestOfStringFail() { - thrown.expect(IllegalArgumentException.class); - Storage.CopyRequest.of( - SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME, BlobInfo.builder(TARGET_BLOB_ID).build()); - } + assertEquals(TARGET_BLOB_ID, copyRequest6.targetId()); + assertNull(copyRequest6.targetInfo()); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java index ad4a04c34127..75b863075770 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java @@ -48,20 +48,29 @@ public class CopyWriterTest { private static final BlobId BLOB_ID = BlobId.of(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME); private static final BlobInfo BLOB_INFO = BlobInfo.builder(DESTINATION_BUCKET_NAME, DESTINATION_BLOB_NAME).build(); - private static final BlobInfo RESULT = + private static final BlobInfo RESULT_INFO = BlobInfo.builder(DESTINATION_BUCKET_NAME, DESTINATION_BLOB_NAME).contentType("type").build(); private static final Map EMPTY_OPTIONS = ImmutableMap.of(); - private static final RewriteRequest REQUEST = new StorageRpc.RewriteRequest(BLOB_ID.toPb(), - EMPTY_OPTIONS, BLOB_INFO.toPb(), EMPTY_OPTIONS, null); - private static final RewriteResponse RESPONSE = new StorageRpc.RewriteResponse(REQUEST, - null, 42L, false, "token", 21L); - private static final RewriteResponse RESPONSE_DONE = new StorageRpc.RewriteResponse(REQUEST, - RESULT.toPb(), 42L, true, "token", 42L); + private static final RewriteRequest REQUEST_WITH_OBJECT = + new StorageRpc.RewriteRequest(BLOB_ID.toPb(), EMPTY_OPTIONS, DESTINATION_BUCKET_NAME, + DESTINATION_BLOB_NAME, BLOB_INFO.toPb(), EMPTY_OPTIONS, null); + private static final RewriteRequest REQUEST_WITHOUT_OBJECT = + new StorageRpc.RewriteRequest(BLOB_ID.toPb(), EMPTY_OPTIONS, DESTINATION_BUCKET_NAME, + DESTINATION_BLOB_NAME, null, EMPTY_OPTIONS, null); + private static final RewriteResponse RESPONSE_WITH_OBJECT = new RewriteResponse( + REQUEST_WITH_OBJECT, null, 42L, false, "token", 21L); + private static final RewriteResponse RESPONSE_WITHOUT_OBJECT = new RewriteResponse( + REQUEST_WITHOUT_OBJECT, null, 42L, false, "token", 21L); + private static final RewriteResponse RESPONSE_WITH_OBJECT_DONE = + new RewriteResponse(REQUEST_WITH_OBJECT, RESULT_INFO.toPb(), 42L, true, "token", 42L); + private static final RewriteResponse RESPONSE_WITHOUT_OBJECT_DONE = + new RewriteResponse(REQUEST_WITHOUT_OBJECT, RESULT_INFO.toPb(), 42L, true, "token", 42L); private StorageOptions options; private StorageRpcFactory rpcFactoryMock; private StorageRpc storageRpcMock; private CopyWriter copyWriter; + private Blob result; @Before public void setUp() { @@ -75,6 +84,7 @@ public void setUp() { .serviceRpcFactory(rpcFactoryMock) .retryParams(RetryParams.noRetries()) .build(); + result = new Blob(options.service(), new BlobInfo.BuilderImpl(RESULT_INFO)); } @After @@ -83,41 +93,111 @@ public void tearDown() throws Exception { } @Test - public void testRewrite() { - EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE)).andReturn(RESPONSE_DONE); + public void testRewriteWithObject() { + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITH_OBJECT)) + .andReturn(RESPONSE_WITH_OBJECT_DONE); EasyMock.replay(storageRpcMock); - copyWriter = new CopyWriter(options, RESPONSE); - assertEquals(RESULT, copyWriter.result()); + copyWriter = new CopyWriter(options, RESPONSE_WITH_OBJECT); + assertEquals(result, copyWriter.result()); assertTrue(copyWriter.isDone()); assertEquals(42L, copyWriter.totalBytesCopied()); assertEquals(42L, copyWriter.blobSize()); } @Test - public void testRewriteMultipleRequests() { - EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE)).andReturn(RESPONSE); - EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE)).andReturn(RESPONSE_DONE); + public void testRewriteWithoutObject() { + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITHOUT_OBJECT)) + .andReturn(RESPONSE_WITHOUT_OBJECT_DONE); EasyMock.replay(storageRpcMock); - copyWriter = new CopyWriter(options, RESPONSE); - assertEquals(RESULT, copyWriter.result()); + copyWriter = new CopyWriter(options, RESPONSE_WITHOUT_OBJECT); + assertEquals(result, copyWriter.result()); assertTrue(copyWriter.isDone()); assertEquals(42L, copyWriter.totalBytesCopied()); assertEquals(42L, copyWriter.blobSize()); } @Test - public void testSaveAndRestore() { - EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE)).andReturn(RESPONSE); - EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE)).andReturn(RESPONSE_DONE); + public void testRewriteWithObjectMultipleRequests() { + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITH_OBJECT)) + .andReturn(RESPONSE_WITH_OBJECT); + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITH_OBJECT)) + .andReturn(RESPONSE_WITH_OBJECT_DONE); EasyMock.replay(storageRpcMock); - copyWriter = new CopyWriter(options, RESPONSE); + copyWriter = new CopyWriter(options, RESPONSE_WITH_OBJECT); + assertEquals(result, copyWriter.result()); + assertTrue(copyWriter.isDone()); + assertEquals(42L, copyWriter.totalBytesCopied()); + assertEquals(42L, copyWriter.blobSize()); + } + + @Test + public void testRewriteWithoutObjectMultipleRequests() { + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITHOUT_OBJECT)) + .andReturn(RESPONSE_WITHOUT_OBJECT); + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITHOUT_OBJECT)) + .andReturn(RESPONSE_WITHOUT_OBJECT_DONE); + EasyMock.replay(storageRpcMock); + copyWriter = new CopyWriter(options, RESPONSE_WITHOUT_OBJECT); + assertEquals(result, copyWriter.result()); + assertTrue(copyWriter.isDone()); + assertEquals(42L, copyWriter.totalBytesCopied()); + assertEquals(42L, copyWriter.blobSize()); + } + + @Test + public void testSaveAndRestoreWithObject() { + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITH_OBJECT)) + .andReturn(RESPONSE_WITH_OBJECT); + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITH_OBJECT)) + .andReturn(RESPONSE_WITH_OBJECT_DONE); + EasyMock.replay(storageRpcMock); + copyWriter = new CopyWriter(options, RESPONSE_WITH_OBJECT); + copyWriter.copyChunk(); + assertTrue(!copyWriter.isDone()); + assertEquals(21L, copyWriter.totalBytesCopied()); + assertEquals(42L, copyWriter.blobSize()); + RestorableState rewriterState = copyWriter.capture(); + CopyWriter restoredRewriter = rewriterState.restore(); + assertEquals(result, restoredRewriter.result()); + assertTrue(restoredRewriter.isDone()); + assertEquals(42L, restoredRewriter.totalBytesCopied()); + assertEquals(42L, restoredRewriter.blobSize()); + } + + @Test + public void testSaveAndRestoreWithoutObject() { + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITHOUT_OBJECT)) + .andReturn(RESPONSE_WITHOUT_OBJECT); + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITHOUT_OBJECT)) + .andReturn(RESPONSE_WITHOUT_OBJECT_DONE); + EasyMock.replay(storageRpcMock); + copyWriter = new CopyWriter(options, RESPONSE_WITHOUT_OBJECT); copyWriter.copyChunk(); assertTrue(!copyWriter.isDone()); assertEquals(21L, copyWriter.totalBytesCopied()); assertEquals(42L, copyWriter.blobSize()); RestorableState rewriterState = copyWriter.capture(); CopyWriter restoredRewriter = rewriterState.restore(); - assertEquals(RESULT, restoredRewriter.result()); + assertEquals(result, restoredRewriter.result()); + assertTrue(restoredRewriter.isDone()); + assertEquals(42L, restoredRewriter.totalBytesCopied()); + assertEquals(42L, restoredRewriter.blobSize()); + } + + @Test + public void testSaveAndRestoreWithResult() { + EasyMock.expect(storageRpcMock.continueRewrite(RESPONSE_WITH_OBJECT)) + .andReturn(RESPONSE_WITH_OBJECT_DONE); + EasyMock.replay(storageRpcMock); + copyWriter = new CopyWriter(options, RESPONSE_WITH_OBJECT); + copyWriter.copyChunk(); + assertEquals(result, copyWriter.result()); + assertTrue(copyWriter.isDone()); + assertEquals(42L, copyWriter.totalBytesCopied()); + assertEquals(42L, copyWriter.blobSize()); + RestorableState rewriterState = copyWriter.capture(); + CopyWriter restoredRewriter = rewriterState.restore(); + assertEquals(result, restoredRewriter.result()); assertTrue(restoredRewriter.isDone()); assertEquals(42L, restoredRewriter.totalBytesCopied()); assertEquals(42L, restoredRewriter.blobSize()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java index 38b4bb58e77f..db1166598237 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java @@ -866,7 +866,8 @@ public void testComposeWithOptions() { public void testCopy() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.blobId(), BLOB_INFO2.blobId()); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - EMPTY_RPC_OPTIONS, request.target().toPb(), EMPTY_RPC_OPTIONS, null); + EMPTY_RPC_OPTIONS, BLOB_INFO2.blobId().bucket(), BLOB_INFO2.blobId().name(), null, + EMPTY_RPC_OPTIONS, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); @@ -886,7 +887,8 @@ public void testCopyWithOptions() { .target(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - BLOB_SOURCE_OPTIONS_COPY, request.target().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); + BLOB_SOURCE_OPTIONS_COPY, BLOB_INFO1.blobId().bucket(), BLOB_INFO1.blobId().name(), + request.targetInfo().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); @@ -906,7 +908,8 @@ public void testCopyWithOptionsFromBlobId() { .target(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - BLOB_SOURCE_OPTIONS_COPY, request.target().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); + BLOB_SOURCE_OPTIONS_COPY, BLOB_INFO1.blobId().bucket(), BLOB_INFO1.blobId().name(), + request.targetInfo().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); @@ -922,7 +925,8 @@ public void testCopyWithOptionsFromBlobId() { public void testCopyMultipleRequests() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.blobId(), BLOB_INFO2.blobId()); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - EMPTY_RPC_OPTIONS, request.target().toPb(), EMPTY_RPC_OPTIONS, null); + EMPTY_RPC_OPTIONS, BLOB_INFO2.blobId().bucket(), BLOB_INFO2.blobId().name(), null, + EMPTY_RPC_OPTIONS, null); StorageRpc.RewriteResponse rpcResponse1 = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); StorageRpc.RewriteResponse rpcResponse2 = new StorageRpc.RewriteResponse(rpcRequest, @@ -935,7 +939,7 @@ public void testCopyMultipleRequests() { assertEquals(42L, writer.blobSize()); assertEquals(21L, writer.totalBytesCopied()); assertTrue(!writer.isDone()); - assertEquals(BLOB_INFO1, writer.result()); + assertEquals(expectedBlob1, writer.result()); assertTrue(writer.isDone()); assertEquals(42L, writer.totalBytesCopied()); assertEquals(42L, writer.blobSize()); diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java index 563a621c48fb..13d768442c34 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java @@ -599,6 +599,37 @@ public void testComposeBlob() { assertNotNull(remoteTargetBlob); assertEquals(targetBlob.name(), remoteTargetBlob.name()); assertEquals(targetBlob.bucket(), remoteTargetBlob.bucket()); + assertNull(remoteTargetBlob.contentType()); + byte[] readBytes = storage.readAllBytes(BUCKET, targetBlobName); + byte[] composedBytes = Arrays.copyOf(BLOB_BYTE_CONTENT, BLOB_BYTE_CONTENT.length * 2); + System.arraycopy(BLOB_BYTE_CONTENT, 0, composedBytes, BLOB_BYTE_CONTENT.length, + BLOB_BYTE_CONTENT.length); + assertArrayEquals(composedBytes, readBytes); + assertTrue(remoteSourceBlob1.delete()); + assertTrue(remoteSourceBlob2.delete()); + assertTrue(remoteTargetBlob.delete()); + } + + @Test + public void testComposeBlobWithContentType() { + String sourceBlobName1 = "test-compose-blob-with-content-type-source-1"; + String sourceBlobName2 = "test-compose-blob-with-content-type-source-2"; + BlobInfo sourceBlob1 = BlobInfo.builder(BUCKET, sourceBlobName1).build(); + BlobInfo sourceBlob2 = BlobInfo.builder(BUCKET, sourceBlobName2).build(); + Blob remoteSourceBlob1 = storage.create(sourceBlob1, BLOB_BYTE_CONTENT); + Blob remoteSourceBlob2 = storage.create(sourceBlob2, BLOB_BYTE_CONTENT); + assertNotNull(remoteSourceBlob1); + assertNotNull(remoteSourceBlob2); + String targetBlobName = "test-compose-blob-with-content-type-target"; + BlobInfo targetBlob = + BlobInfo.builder(BUCKET, targetBlobName).contentType(CONTENT_TYPE).build(); + Storage.ComposeRequest req = + Storage.ComposeRequest.of(ImmutableList.of(sourceBlobName1, sourceBlobName2), targetBlob); + Blob remoteTargetBlob = storage.compose(req); + assertNotNull(remoteTargetBlob); + assertEquals(targetBlob.name(), remoteTargetBlob.name()); + assertEquals(targetBlob.bucket(), remoteTargetBlob.bucket()); + assertEquals(CONTENT_TYPE, remoteTargetBlob.contentType()); byte[] readBytes = storage.readAllBytes(BUCKET, targetBlobName); byte[] composedBytes = Arrays.copyOf(BLOB_BYTE_CONTENT, BLOB_BYTE_CONTENT.length * 2); System.arraycopy(BLOB_BYTE_CONTENT, 0, composedBytes, BLOB_BYTE_CONTENT.length, @@ -682,6 +713,26 @@ public void testCopyBlobUpdateMetadata() { assertTrue(storage.delete(BUCKET, targetBlobName)); } + @Test + public void testCopyBlobNoContentType() { + String sourceBlobName = "test-copy-blob-no-content-type-source"; + BlobId source = BlobId.of(BUCKET, sourceBlobName); + Blob remoteSourceBlob = storage.create(BlobInfo.builder(source).build(), BLOB_BYTE_CONTENT); + assertNotNull(remoteSourceBlob); + String targetBlobName = "test-copy-blob-no-content-type-target"; + ImmutableMap metadata = ImmutableMap.of("k", "v"); + BlobInfo target = BlobInfo.builder(BUCKET, targetBlobName).metadata(metadata).build(); + Storage.CopyRequest req = Storage.CopyRequest.of(source, target); + CopyWriter copyWriter = storage.copy(req); + assertEquals(BUCKET, copyWriter.result().bucket()); + assertEquals(targetBlobName, copyWriter.result().name()); + assertNull(copyWriter.result().contentType()); + assertEquals(metadata, copyWriter.result().metadata()); + assertTrue(copyWriter.isDone()); + assertTrue(remoteSourceBlob.delete()); + assertTrue(storage.delete(BUCKET, targetBlobName)); + } + @Test public void testCopyBlobFail() { String sourceBlobName = "test-copy-blob-source-fail"; From 3e5db7a87013d677d179d6d484448fcae3cbdbc6 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Sun, 20 Mar 2016 21:05:16 +0100 Subject: [PATCH 36/44] Refactor CopyRequest: remove targetId, add boolean overrideInfo --- .../com/google/gcloud/storage/CopyWriter.java | 47 ++++++++--------- .../com/google/gcloud/storage/Storage.java | 50 ++++++++++--------- .../google/gcloud/storage/StorageImpl.java | 12 ++--- .../gcloud/storage/spi/DefaultStorageRpc.java | 4 +- .../google/gcloud/storage/spi/StorageRpc.java | 23 ++++----- .../com/google/gcloud/storage/BlobTest.java | 15 +++--- .../gcloud/storage/CopyRequestTest.java | 42 +++++++++------- .../google/gcloud/storage/CopyWriterTest.java | 8 +-- .../gcloud/storage/StorageImplTest.java | 12 ++--- 9 files changed, 102 insertions(+), 111 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java index 38d9c0d5e952..26c728942a28 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java @@ -120,11 +120,9 @@ public RestorableState capture() { serviceOptions, BlobId.fromPb(rewriteResponse.rewriteRequest.source), rewriteResponse.rewriteRequest.sourceOptions, - BlobId.of(rewriteResponse.rewriteRequest.targetBucket, - rewriteResponse.rewriteRequest.targetName), + rewriteResponse.rewriteRequest.overrideInfo, + BlobInfo.fromPb(rewriteResponse.rewriteRequest.target), rewriteResponse.rewriteRequest.targetOptions) - .targetInfo(rewriteResponse.rewriteRequest.targetObject != null - ? BlobInfo.fromPb(rewriteResponse.rewriteRequest.targetObject) : null) .result(rewriteResponse.result != null ? BlobInfo.fromPb(rewriteResponse.result) : null) .blobSize(blobSize()) .isDone(isDone()) @@ -141,8 +139,8 @@ static class StateImpl implements RestorableState, Serializable { private final StorageOptions serviceOptions; private final BlobId source; private final Map sourceOptions; - private final BlobId targetId; - private final BlobInfo targetInfo; + private final boolean overrideInfo; + private final BlobInfo target; private final Map targetOptions; private final BlobInfo result; private final long blobSize; @@ -155,8 +153,8 @@ static class StateImpl implements RestorableState, Serializable { this.serviceOptions = builder.serviceOptions; this.source = builder.source; this.sourceOptions = builder.sourceOptions; - this.targetId = builder.targetId; - this.targetInfo = builder.targetInfo; + this.overrideInfo = builder.overrideInfo; + this.target = builder.target; this.targetOptions = builder.targetOptions; this.result = builder.result; this.blobSize = builder.blobSize; @@ -171,9 +169,9 @@ static class Builder { private final StorageOptions serviceOptions; private final BlobId source; private final Map sourceOptions; - private final BlobId targetId; + private final boolean overrideInfo; + private BlobInfo target; private final Map targetOptions; - private BlobInfo targetInfo; private BlobInfo result; private long blobSize; private boolean isDone; @@ -182,20 +180,16 @@ static class Builder { private Long megabytesCopiedPerChunk; private Builder(StorageOptions options, BlobId source, - Map sourceOptions, - BlobId targetId, Map targetOptions) { + Map sourceOptions, boolean overrideInfo, BlobInfo target, + Map targetOptions) { this.serviceOptions = options; this.source = source; this.sourceOptions = sourceOptions; - this.targetId = targetId; + this.overrideInfo = overrideInfo; + this.target = target; this.targetOptions = targetOptions; } - Builder targetInfo(BlobInfo targetInfo) { - this.targetInfo = targetInfo; - return this; - } - Builder result(BlobInfo result) { this.result = result; return this; @@ -232,16 +226,15 @@ RestorableState build() { } static Builder builder(StorageOptions options, BlobId source, - Map sourceOptions, BlobId targetId, + Map sourceOptions, boolean overrideInfo, BlobInfo target, Map targetOptions) { - return new Builder(options, source, sourceOptions, targetId, targetOptions); + return new Builder(options, source, sourceOptions, overrideInfo, target, targetOptions); } @Override public CopyWriter restore() { RewriteRequest rewriteRequest = new RewriteRequest(source.toPb(), sourceOptions, - targetId.bucket(), targetId.name(), targetInfo != null ? targetInfo.toPb() : null, - targetOptions, megabytesCopiedPerChunk); + overrideInfo, target.toPb(), targetOptions, megabytesCopiedPerChunk); RewriteResponse rewriteResponse = new RewriteResponse(rewriteRequest, result != null ? result.toPb() : null, blobSize, isDone, rewriteToken, totalBytesCopied); @@ -250,7 +243,7 @@ public CopyWriter restore() { @Override public int hashCode() { - return Objects.hash(serviceOptions, source, sourceOptions, targetId, targetInfo, + return Objects.hash(serviceOptions, source, sourceOptions, overrideInfo, target, targetOptions, result, blobSize, isDone, megabytesCopiedPerChunk, rewriteToken, totalBytesCopied); } @@ -267,8 +260,8 @@ public boolean equals(Object obj) { return Objects.equals(this.serviceOptions, other.serviceOptions) && Objects.equals(this.source, other.source) && Objects.equals(this.sourceOptions, other.sourceOptions) - && Objects.equals(this.targetId, other.targetId) - && Objects.equals(this.targetInfo, other.targetInfo) + && Objects.equals(this.overrideInfo, other.overrideInfo) + && Objects.equals(this.target, other.target) && Objects.equals(this.targetOptions, other.targetOptions) && Objects.equals(this.result, other.result) && Objects.equals(this.rewriteToken, other.rewriteToken) @@ -282,8 +275,8 @@ public boolean equals(Object obj) { public String toString() { return MoreObjects.toStringHelper(this) .add("source", source) - .add("targetId", targetId) - .add("targetInfo", targetInfo) + .add("overrideInfo", overrideInfo) + .add("target", target) .add("result", result) .add("blobSize", blobSize) .add("isDone", isDone) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index 8204f0105e7e..b30d46431de0 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -962,8 +962,8 @@ class CopyRequest implements Serializable { private final BlobId source; private final List sourceOptions; - private final BlobId targetId; - private final BlobInfo targetInfo; + private final boolean overrideInfo; + private final BlobInfo target; private final List targetOptions; private final Long megabytesCopiedPerChunk; @@ -972,8 +972,8 @@ public static class Builder { private final Set sourceOptions = new LinkedHashSet<>(); private final Set targetOptions = new LinkedHashSet<>(); private BlobId source; - private BlobId targetId; - private BlobInfo targetInfo; + private boolean overrideInfo; + private BlobInfo target; private Long megabytesCopiedPerChunk; /** @@ -1022,7 +1022,8 @@ public Builder sourceOptions(Iterable options) { * @return the builder */ public Builder target(BlobId targetId) { - this.targetId = targetId; + this.overrideInfo = false; + this.target = BlobInfo.builder(targetId).build(); return this; } @@ -1030,13 +1031,13 @@ public Builder target(BlobId targetId) { * Sets the copy target and target options. {@code target} parameter is used to override * source blob information (e.g. {@code contentType}, {@code contentLanguage}). Target blob * information is set exactly to {@code target}, no information is inherited from the source - * blob. If not set, target blob information is inherited from the source blob. + * blob. * * @return the builder */ - public Builder target(BlobInfo targetInfo, BlobTargetOption... options) { - this.targetId = targetInfo.blobId(); - this.targetInfo = targetInfo; + public Builder target(BlobInfo target, BlobTargetOption... options) { + this.overrideInfo = true; + this.target = checkNotNull(target); Collections.addAll(targetOptions, options); return this; } @@ -1045,13 +1046,13 @@ public Builder target(BlobInfo targetInfo, BlobTargetOption... options) { * Sets the copy target and target options. {@code target} parameter is used to override * source blob information (e.g. {@code contentType}, {@code contentLanguage}). Target blob * information is set exactly to {@code target}, no information is inherited from the source - * blob. If not set, target blob information is inherited from the source blob. + * blob. * * @return the builder */ - public Builder target(BlobInfo targetInfo, Iterable options) { - this.targetId = targetInfo.blobId(); - this.targetInfo = targetInfo; + public Builder target(BlobInfo target, Iterable options) { + this.overrideInfo = true; + this.target = checkNotNull(target); Iterables.addAll(targetOptions, options); return this; } @@ -1079,8 +1080,8 @@ public CopyRequest build() { private CopyRequest(Builder builder) { source = checkNotNull(builder.source); sourceOptions = ImmutableList.copyOf(builder.sourceOptions); - targetId = checkNotNull(builder.targetId); - targetInfo = builder.targetInfo; + overrideInfo = builder.overrideInfo; + target = checkNotNull(builder.target); targetOptions = ImmutableList.copyOf(builder.targetOptions); megabytesCopiedPerChunk = builder.megabytesCopiedPerChunk; } @@ -1100,20 +1101,21 @@ public List sourceOptions() { } /** - * Returns the {@link BlobId} for the target blob. + * Returns the {@link BlobInfo} for the target blob. */ - public BlobId targetId() { - return targetId; + public BlobInfo target() { + return target; } /** - * Returns the {@link BlobInfo} for the target blob. If set, this value is used to replace - * source blob information (e.g. {@code contentType}, {@code contentLanguage}). Target blob - * information is set exactly to this value, no information is inherited from the source blob. - * If not set, target blob information is inherited from the source blob. + * Returns whether to override the target blob information with {@link #target()}. + * If {@code true}, the value of {@link #target()} is used to replace source blob information + * (e.g. {@code contentType}, {@code contentLanguage}). Target blob information is set exactly + * to this value, no information is inherited from the source blob. If {@code false}, target + * blob information is inherited from the source blob. */ - public BlobInfo targetInfo() { - return targetInfo; + public boolean overrideInfo() { + return overrideInfo; } /** diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index 30c0046b802c..cf709ba5e293 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -413,19 +413,15 @@ public CopyWriter copy(final CopyRequest copyRequest) { final StorageObject source = copyRequest.source().toPb(); final Map sourceOptions = optionMap(copyRequest.source().generation(), null, copyRequest.sourceOptions(), true); - final BlobId targetId = copyRequest.targetId(); - final StorageObject targetObject = - copyRequest.targetInfo() != null ? copyRequest.targetInfo().toPb() : null; - final Map targetOptions = optionMap( - copyRequest.targetInfo() != null ? copyRequest.targetInfo().generation() : null, - copyRequest.targetInfo() != null ? copyRequest.targetInfo().metageneration() : null, - copyRequest.targetOptions()); + final StorageObject targetObject = copyRequest.target().toPb(); + final Map targetOptions = optionMap(copyRequest.target().generation(), + copyRequest.target().metageneration(), copyRequest.targetOptions()); try { RewriteResponse rewriteResponse = runWithRetries(new Callable() { @Override public RewriteResponse call() { return storageRpc.openRewrite(new StorageRpc.RewriteRequest(source, sourceOptions, - targetId.bucket(), targetId.name(), targetObject, targetOptions, + copyRequest.overrideInfo(), targetObject, targetOptions, copyRequest.megabytesCopiedPerChunk())); } }, options().retryParams(), EXCEPTION_HANDLER); diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java index 7f13212556aa..8d06832534e2 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/DefaultStorageRpc.java @@ -580,8 +580,8 @@ private RewriteResponse rewrite(RewriteRequest req, String token) { Long maxBytesRewrittenPerCall = req.megabytesRewrittenPerCall != null ? req.megabytesRewrittenPerCall * MEGABYTE : null; com.google.api.services.storage.model.RewriteResponse rewriteResponse = storage.objects() - .rewrite(req.source.getBucket(), req.source.getName(), req.targetBucket, - req.targetName, req.targetObject) + .rewrite(req.source.getBucket(), req.source.getName(), req.target.getBucket(), + req.target.getName(), req.overrideInfo ? req.target : null) .setSourceGeneration(req.source.getGeneration()) .setRewriteToken(token) .setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java index 7256368e189f..74f8171de87f 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/spi/StorageRpc.java @@ -138,20 +138,18 @@ class RewriteRequest { public final StorageObject source; public final Map sourceOptions; - public final String targetBucket; - public final String targetName; - public final StorageObject targetObject; + public final boolean overrideInfo; + public final StorageObject target; public final Map targetOptions; public final Long megabytesRewrittenPerCall; public RewriteRequest(StorageObject source, Map sourceOptions, - String targetBucket, String targetName, StorageObject targetObject, - Map targetOptions, Long megabytesRewrittenPerCall) { + boolean overrideInfo, StorageObject target, Map targetOptions, + Long megabytesRewrittenPerCall) { this.source = source; this.sourceOptions = sourceOptions; - this.targetBucket = targetBucket; - this.targetName = targetName; - this.targetObject = targetObject; + this.overrideInfo = overrideInfo; + this.target = target; this.targetOptions = targetOptions; this.megabytesRewrittenPerCall = megabytesRewrittenPerCall; } @@ -167,17 +165,16 @@ public boolean equals(Object obj) { final RewriteRequest other = (RewriteRequest) obj; return Objects.equals(this.source, other.source) && Objects.equals(this.sourceOptions, other.sourceOptions) - && Objects.equals(this.targetBucket, other.targetBucket) - && Objects.equals(this.targetName, other.targetName) - && Objects.equals(this.targetObject, other.targetObject) + && Objects.equals(this.overrideInfo, other.overrideInfo) + && Objects.equals(this.target, other.target) && Objects.equals(this.targetOptions, other.targetOptions) && Objects.equals(this.megabytesRewrittenPerCall, other.megabytesRewrittenPerCall); } @Override public int hashCode() { - return Objects.hash(source, sourceOptions, targetBucket, targetName, targetObject, - targetOptions, megabytesRewrittenPerCall); + return Objects.hash(source, sourceOptions, overrideInfo, target, targetOptions, + megabytesRewrittenPerCall); } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java index 2d4920816c38..d6c97ca9ca03 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java @@ -222,6 +222,7 @@ public void testDelete() throws Exception { @Test public void testCopyToBucket() throws Exception { initializeExpectedBlob(2); + BlobInfo target = BlobInfo.builder(BlobId.of("bt", "n")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture capturedCopyRequest = Capture.newInstance(); expect(storage.options()).andReturn(mockOptions); @@ -231,8 +232,8 @@ public void testCopyToBucket() throws Exception { CopyWriter returnedCopyWriter = blob.copyTo("bt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().source(), blob.blobId()); - assertEquals(capturedCopyRequest.getValue().targetId(), BlobId.of("bt", "n")); - assertNull(capturedCopyRequest.getValue().targetInfo()); + assertEquals(capturedCopyRequest.getValue().target(), target); + assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().sourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().targetOptions().isEmpty()); } @@ -240,6 +241,7 @@ public void testCopyToBucket() throws Exception { @Test public void testCopyTo() throws Exception { initializeExpectedBlob(2); + BlobInfo target = BlobInfo.builder(BlobId.of("bt", "nt")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture capturedCopyRequest = Capture.newInstance(); expect(storage.options()).andReturn(mockOptions); @@ -249,8 +251,8 @@ public void testCopyTo() throws Exception { CopyWriter returnedCopyWriter = blob.copyTo("bt", "nt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().source(), blob.blobId()); - assertEquals(capturedCopyRequest.getValue().targetId(), BlobId.of("bt", "nt")); - assertNull(capturedCopyRequest.getValue().targetInfo()); + assertEquals(capturedCopyRequest.getValue().target(), target); + assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().sourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().targetOptions().isEmpty()); } @@ -258,6 +260,7 @@ public void testCopyTo() throws Exception { @Test public void testCopyToBlobId() throws Exception { initializeExpectedBlob(2); + BlobInfo target = BlobInfo.builder(BlobId.of("bt", "nt")).build(); BlobId targetId = BlobId.of("bt", "nt"); CopyWriter copyWriter = createMock(CopyWriter.class); Capture capturedCopyRequest = Capture.newInstance(); @@ -268,8 +271,8 @@ public void testCopyToBlobId() throws Exception { CopyWriter returnedCopyWriter = blob.copyTo(targetId); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().source(), blob.blobId()); - assertEquals(capturedCopyRequest.getValue().targetId(), targetId); - assertNull(capturedCopyRequest.getValue().targetInfo()); + assertEquals(capturedCopyRequest.getValue().target(), target); + assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().sourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().targetOptions().isEmpty()); } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java index 5700ea804cd6..9f8edfb84162 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyRequestTest.java @@ -18,7 +18,8 @@ import static com.google.gcloud.storage.Storage.PredefinedAcl.PUBLIC_READ; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; import com.google.gcloud.storage.Storage.BlobSourceOption; @@ -53,8 +54,8 @@ public void testCopyRequest() { assertEquals(SOURCE_BLOB_ID, copyRequest1.source()); assertEquals(1, copyRequest1.sourceOptions().size()); assertEquals(BlobSourceOption.generationMatch(1), copyRequest1.sourceOptions().get(0)); - assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest1.targetId()); - assertEquals(TARGET_BLOB_INFO, copyRequest1.targetInfo()); + assertEquals(TARGET_BLOB_INFO, copyRequest1.target()); + assertTrue(copyRequest1.overrideInfo()); assertEquals(1, copyRequest1.targetOptions().size()); assertEquals(BlobTargetOption.predefinedAcl(PUBLIC_READ), copyRequest1.targetOptions().get(0)); @@ -63,16 +64,16 @@ public void testCopyRequest() { .target(TARGET_BLOB_ID) .build(); assertEquals(SOURCE_BLOB_ID, copyRequest2.source()); - assertEquals(TARGET_BLOB_ID, copyRequest2.targetId()); - assertNull(copyRequest2.targetInfo()); + assertEquals(BlobInfo.builder(TARGET_BLOB_ID).build(), copyRequest2.target()); + assertFalse(copyRequest2.overrideInfo()); Storage.CopyRequest copyRequest3 = Storage.CopyRequest.builder() .source(SOURCE_BLOB_ID) .target(TARGET_BLOB_INFO, ImmutableList.of(BlobTargetOption.predefinedAcl(PUBLIC_READ))) .build(); assertEquals(SOURCE_BLOB_ID, copyRequest3.source()); - assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest3.targetId()); - assertEquals(TARGET_BLOB_INFO, copyRequest3.targetInfo()); + assertEquals(TARGET_BLOB_INFO, copyRequest3.target()); + assertTrue(copyRequest3.overrideInfo()); assertEquals(ImmutableList.of(BlobTargetOption.predefinedAcl(PUBLIC_READ)), copyRequest3.targetOptions()); } @@ -81,34 +82,37 @@ public void testCopyRequest() { public void testCopyRequestOf() { Storage.CopyRequest copyRequest1 = Storage.CopyRequest.of(SOURCE_BLOB_ID, TARGET_BLOB_INFO); assertEquals(SOURCE_BLOB_ID, copyRequest1.source()); - assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest1.targetId()); - assertEquals(TARGET_BLOB_INFO, copyRequest1.targetInfo()); + assertEquals(TARGET_BLOB_INFO, copyRequest1.target()); + assertTrue(copyRequest1.overrideInfo()); Storage.CopyRequest copyRequest2 = Storage.CopyRequest.of(SOURCE_BLOB_ID, TARGET_BLOB_NAME); assertEquals(SOURCE_BLOB_ID, copyRequest2.source()); - assertEquals(BlobId.of(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME), copyRequest2.targetId()); - assertNull(copyRequest2.targetInfo()); + assertEquals(BlobInfo.builder(BlobId.of(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME)).build(), + copyRequest2.target()); + assertFalse(copyRequest2.overrideInfo()); Storage.CopyRequest copyRequest3 = Storage.CopyRequest.of(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME, TARGET_BLOB_INFO); assertEquals(SOURCE_BLOB_ID, copyRequest3.source()); - assertEquals(TARGET_BLOB_INFO.blobId(), copyRequest3.targetId()); - assertEquals(TARGET_BLOB_INFO, copyRequest3.targetInfo()); + assertEquals(TARGET_BLOB_INFO, copyRequest3.target()); + assertTrue(copyRequest3.overrideInfo()); Storage.CopyRequest copyRequest4 = Storage.CopyRequest.of(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME, TARGET_BLOB_NAME); assertEquals(SOURCE_BLOB_ID, copyRequest4.source()); - assertEquals(BlobId.of(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME), copyRequest4.targetId()); - assertNull(copyRequest4.targetInfo()); + assertEquals(BlobInfo.builder(BlobId.of(SOURCE_BUCKET_NAME, TARGET_BLOB_NAME)).build(), + copyRequest4.target()); + assertFalse(copyRequest4.overrideInfo()); Storage.CopyRequest copyRequest5 = Storage.CopyRequest.of(SOURCE_BLOB_ID, TARGET_BLOB_ID); assertEquals(SOURCE_BLOB_ID, copyRequest5.source()); - assertEquals(TARGET_BLOB_ID, copyRequest5.targetId()); - assertNull(copyRequest5.targetInfo()); + assertEquals(BlobInfo.builder(TARGET_BLOB_ID).build(), copyRequest5.target()); + assertFalse(copyRequest5.overrideInfo()); Storage.CopyRequest copyRequest6 = Storage.CopyRequest.of(SOURCE_BUCKET_NAME, SOURCE_BLOB_NAME, TARGET_BLOB_ID); assertEquals(SOURCE_BLOB_ID, copyRequest6.source()); - assertEquals(TARGET_BLOB_ID, copyRequest6.targetId()); - assertNull(copyRequest6.targetInfo()); } + assertEquals(BlobInfo.builder(TARGET_BLOB_ID).build(), copyRequest6.target()); + assertFalse(copyRequest6.overrideInfo()); + } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java index 75b863075770..8ccb81688b65 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java @@ -52,11 +52,11 @@ public class CopyWriterTest { BlobInfo.builder(DESTINATION_BUCKET_NAME, DESTINATION_BLOB_NAME).contentType("type").build(); private static final Map EMPTY_OPTIONS = ImmutableMap.of(); private static final RewriteRequest REQUEST_WITH_OBJECT = - new StorageRpc.RewriteRequest(BLOB_ID.toPb(), EMPTY_OPTIONS, DESTINATION_BUCKET_NAME, - DESTINATION_BLOB_NAME, BLOB_INFO.toPb(), EMPTY_OPTIONS, null); + new StorageRpc.RewriteRequest(BLOB_ID.toPb(), EMPTY_OPTIONS, true, BLOB_INFO.toPb(), + EMPTY_OPTIONS, null); private static final RewriteRequest REQUEST_WITHOUT_OBJECT = - new StorageRpc.RewriteRequest(BLOB_ID.toPb(), EMPTY_OPTIONS, DESTINATION_BUCKET_NAME, - DESTINATION_BLOB_NAME, null, EMPTY_OPTIONS, null); + new StorageRpc.RewriteRequest(BLOB_ID.toPb(), EMPTY_OPTIONS, false, BLOB_INFO.toPb(), + EMPTY_OPTIONS, null); private static final RewriteResponse RESPONSE_WITH_OBJECT = new RewriteResponse( REQUEST_WITH_OBJECT, null, 42L, false, "token", 21L); private static final RewriteResponse RESPONSE_WITHOUT_OBJECT = new RewriteResponse( diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java index db1166598237..3cc99e3bf884 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java @@ -866,8 +866,7 @@ public void testComposeWithOptions() { public void testCopy() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.blobId(), BLOB_INFO2.blobId()); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - EMPTY_RPC_OPTIONS, BLOB_INFO2.blobId().bucket(), BLOB_INFO2.blobId().name(), null, - EMPTY_RPC_OPTIONS, null); + EMPTY_RPC_OPTIONS, false, BLOB_INFO2.toPb(), EMPTY_RPC_OPTIONS, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); @@ -887,8 +886,7 @@ public void testCopyWithOptions() { .target(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - BLOB_SOURCE_OPTIONS_COPY, BLOB_INFO1.blobId().bucket(), BLOB_INFO1.blobId().name(), - request.targetInfo().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); + BLOB_SOURCE_OPTIONS_COPY, true, request.target().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); @@ -908,8 +906,7 @@ public void testCopyWithOptionsFromBlobId() { .target(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - BLOB_SOURCE_OPTIONS_COPY, BLOB_INFO1.blobId().bucket(), BLOB_INFO1.blobId().name(), - request.targetInfo().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); + BLOB_SOURCE_OPTIONS_COPY, true, request.target().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); @@ -925,8 +922,7 @@ public void testCopyWithOptionsFromBlobId() { public void testCopyMultipleRequests() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.blobId(), BLOB_INFO2.blobId()); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.source().toPb(), - EMPTY_RPC_OPTIONS, BLOB_INFO2.blobId().bucket(), BLOB_INFO2.blobId().name(), null, - EMPTY_RPC_OPTIONS, null); + EMPTY_RPC_OPTIONS, false, BLOB_INFO2.toPb(), EMPTY_RPC_OPTIONS, null); StorageRpc.RewriteResponse rpcResponse1 = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); StorageRpc.RewriteResponse rpcResponse2 = new StorageRpc.RewriteResponse(rpcRequest, From 1855ae1a86370e90bc5fae34dff58bf39cb27671 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Sun, 20 Mar 2016 21:05:49 +0100 Subject: [PATCH 37/44] Add better javadoc to Storage.copy and CopyWriter --- .../com/google/gcloud/storage/CopyWriter.java | 8 +++++++- .../java/com/google/gcloud/storage/Storage.java | 17 +++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java index 26c728942a28..2f3850d5dd03 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java @@ -32,7 +32,13 @@ import java.util.concurrent.Callable; /** - * Google Storage blob copy writer. This class holds the result of a copy request. If source and + * Google Storage blob copy writer. A {@code CopyWriter} object allows to copy both blob's data and + * information. To override source blob's information call {@link Storage#copy(Storage.CopyRequest)} + * with a {@code CopyRequest} object where the copy target is set via + * {@link Storage.CopyRequest.Builder#target(BlobInfo, Storage.BlobTargetOption...)} or + * {@link Storage.CopyRequest.Builder#target(BlobInfo, Iterable)}. + * + *

This class holds the result of a copy request. If source and * destination blobs share the same location and storage class the copy is completed in one RPC call * otherwise one or more {@link #copyChunk} calls are necessary to complete the copy. In addition, * {@link CopyWriter#result()} can be used to automatically complete the copy and return information diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index b30d46431de0..33c1d7e7e143 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -1385,12 +1385,17 @@ public static Builder builder() { Blob compose(ComposeRequest composeRequest); /** - * Sends a copy request. Returns a {@link CopyWriter} object for the provided - * {@code CopyRequest}. If source and destination objects share the same location and storage - * class the source blob is copied with one request and {@link CopyWriter#result()} immediately - * returns, regardless of the {@link CopyRequest#megabytesCopiedPerChunk} parameter. - * If source and destination have different location or storage class {@link CopyWriter#result()} - * might issue multiple RPC calls depending on blob's size. + * Sends a copy request. This method copies both blob's data and information. To override source + * blob's information set the copy target via + * {@link CopyRequest.Builder#target(BlobInfo, BlobTargetOption...)} or + * {@link CopyRequest.Builder#target(BlobInfo, Iterable)}. + * + *

This method returns a {@link CopyWriter} object for the provided {@code CopyRequest}. If + * source and destination objects share the same location and storage class the source blob is + * copied with one request and {@link CopyWriter#result()} immediately returns, regardless of the + * {@link CopyRequest#megabytesCopiedPerChunk} parameter. If source and destination have different + * location or storage class {@link CopyWriter#result()} might issue multiple RPC calls depending + * on blob's size. * *

Example usage of copy: *

 {@code BlobInfo blob = service.copy(copyRequest).result();}

From 5dd0292085c00ac79aed381d4192bc33b09e2b97 Mon Sep 17 00:00:00 2001
From: Marco Ziccardi 
Date: Sun, 20 Mar 2016 22:39:16 +0100
Subject: [PATCH 38/44] Rephrase RewriteChannel and Storage.copy javadoc. Add
 final to CopyWriter's StateImpl.target

---
 .../main/java/com/google/gcloud/storage/CopyWriter.java    | 6 +++---
 .../src/main/java/com/google/gcloud/storage/Storage.java   | 7 ++++---
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java
index 2f3850d5dd03..743630b6c4c2 100644
--- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java
+++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/CopyWriter.java
@@ -33,8 +33,8 @@
 
 /**
  * Google Storage blob copy writer. A {@code CopyWriter} object allows to copy both blob's data and
- * information. To override source blob's information call {@link Storage#copy(Storage.CopyRequest)}
- * with a {@code CopyRequest} object where the copy target is set via
+ * information. To override source blob's information supply a {@code BlobInfo} to the
+ * {@code CopyRequest} using either
  * {@link Storage.CopyRequest.Builder#target(BlobInfo, Storage.BlobTargetOption...)} or
  * {@link Storage.CopyRequest.Builder#target(BlobInfo, Iterable)}.
  *
@@ -176,7 +176,7 @@ static class Builder {
       private final BlobId source;
       private final Map sourceOptions;
       private final boolean overrideInfo;
-      private BlobInfo target;
+      private final BlobInfo target;
       private final Map targetOptions;
       private BlobInfo result;
       private long blobSize;
diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
index 33c1d7e7e143..b4fbe45244b0 100644
--- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
+++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java
@@ -1386,9 +1386,10 @@ public static Builder builder() {
 
   /**
    * Sends a copy request. This method copies both blob's data and information. To override source
-   * blob's information set the copy target via
-   * {@link CopyRequest.Builder#target(BlobInfo, BlobTargetOption...)} or
-   * {@link CopyRequest.Builder#target(BlobInfo, Iterable)}.
+   * blob's information supply a {@code BlobInfo} to the
+   * {@code CopyRequest} using either
+   * {@link Storage.CopyRequest.Builder#target(BlobInfo, Storage.BlobTargetOption...)} or
+   * {@link Storage.CopyRequest.Builder#target(BlobInfo, Iterable)}.
    *
    * 

This method returns a {@link CopyWriter} object for the provided {@code CopyRequest}. If * source and destination objects share the same location and storage class the source blob is From 3e9e1a0e8e034248c090e9c9b9918d3a783412be Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Wed, 16 Mar 2016 13:48:52 -0700 Subject: [PATCH 39/44] IAM docs and functional methods for policies --- .../snippets/ModifyPolicy.java | 64 +++++++++++++++++ gcloud-java-resourcemanager/README.md | 43 +++++++++++- .../google/gcloud/resourcemanager/Policy.java | 5 ++ .../gcloud/resourcemanager/Project.java | 68 +++++++++++++++++++ .../gcloud/resourcemanager/ProjectTest.java | 46 +++++++++++++ 5 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java new file mode 100644 index 000000000000..7401f0b88bbc --- /dev/null +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016 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. + */ + +/* + * EDITING INSTRUCTIONS + * This file is referenced in READMEs and javadoc. Any change to this file should be reflected in + * the project's READMEs and package-info.java. + */ + +package com.google.gcloud.examples.resourcemanager.snippets; + +import com.google.gcloud.Identity; +import com.google.gcloud.resourcemanager.Policy; +import com.google.gcloud.resourcemanager.Policy.Role; +import com.google.gcloud.resourcemanager.Project; +import com.google.gcloud.resourcemanager.ResourceManager; +import com.google.gcloud.resourcemanager.ResourceManagerOptions; + +/** + * A snippet for Google Cloud Resource Manager showing how to modify a project's IAM policy. + */ +public class ModifyPolicy { + + public static void main(String... args) { + // Create Resource Manager service object + // By default, credentials are inferred from the runtime environment. + ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service(); + + // Get a project from the server + String projectId = "some-project-id"; // Use an existing project's ID + Project project = resourceManager.get(projectId); + + // Get the project's policy + Policy policy = project.getPolicy(); + + // Add a viewer + Policy.Builder modifiedPolicy = policy.toBuilder(); + Identity newViewer = Identity.user(""); + if (policy.bindings().containsKey(Role.viewer())) { + modifiedPolicy.addIdentity(Role.viewer(), newViewer); + } else { + modifiedPolicy.addBinding(Role.viewer(), newViewer); + } + + // Write policy + Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build()); + + // Print policy + System.out.printf("Updated policy for %s: %n%s%n", projectId, updatedPolicy); + } +} diff --git a/gcloud-java-resourcemanager/README.md b/gcloud-java-resourcemanager/README.md index 94037e27a709..a2539df7adab 100644 --- a/gcloud-java-resourcemanager/README.md +++ b/gcloud-java-resourcemanager/README.md @@ -163,9 +163,46 @@ while (projectIterator.hasNext()) { } ``` +#### Managing IAM Policies +You can edit [Google Cloud IAM](https://cloud.google.com/iam/) (Identity and Access Management) +policies on the project-level using this library as well. We recommend using the read-modify-write +pattern to make policy changes. This entails reading the project's current policy, updating it +locally, and then sending the modified policy for writing, as shown in the snippet below. First, +add these imports: + +```java +import com.google.gcloud.Identity; +import com.google.gcloud.resourcemanager.Policy; +import com.google.gcloud.resourcemanager.Policy.Role; +``` + +Assuming you have completed the steps above to create the `ResourceManager` service object and load +a project from the server, you just need to add the following code: + +```java +// Get the project's policy +Policy policy = project.getPolicy(); + +// Add a viewer +Policy.Builder modifiedPolicy = policy.toBuilder(); +Identity newViewer = Identity.user(""); +if (policy.bindings().containsKey(Role.viewer())) { + modifiedPolicy.addIdentity(Role.viewer(), newViewer); +} else { + modifiedPolicy.addBinding(Role.viewer(), newViewer); +} + +// Write policy +Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build()); +``` + +Note that the policy you pass in to `replacePolicy` overwrites the original policy. For example, if +the original policy has two bindings and you call `replacePolicy` with a new policy containing only +one binding, the two original bindings are lost. + #### Complete source code -We put together all the code shown above into two programs. Both programs assume that you are +We put together all the code shown above into three programs. The programs assume that you are running from your own desktop and used the Google Cloud SDK to authenticate yourself. The first program creates a project if it does not exist. Complete source code can be found at @@ -175,6 +212,10 @@ The second program updates a project if it exists and lists all projects the use view. Complete source code can be found at [UpdateAndListProjects.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/UpdateAndListProjects.java). +The third program modifies the IAM policy associated with a project using the read-modify-write +pattern. Complete source code can be found at +[ModifyPolicy.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java) + Java Versions ------------- diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java index 46330e19fa59..6399b52b3c04 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java @@ -178,6 +178,11 @@ public Builder toBuilder() { return new Builder(bindings(), etag(), version()); } + @Override + public String toString() { + return toPb().toString(); + } + com.google.api.services.cloudresourcemanager.model.Policy toPb() { com.google.api.services.cloudresourcemanager.model.Policy policyPb = new com.google.api.services.cloudresourcemanager.model.Policy(); diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java index 46b142c5aa53..dd252155645b 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java @@ -18,8 +18,11 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.gcloud.resourcemanager.ResourceManager.Permission; + import java.io.IOException; import java.io.ObjectInputStream; +import java.util.List; import java.util.Map; import java.util.Objects; @@ -198,6 +201,71 @@ public Project replace() { return resourceManager.replace(this); } + /** + * Returns the IAM access control policy for the specified project. Returns {@code null} if the + * resource does not exist or if you do not have adequate permission to view the project or get + * the policy. + * + * @throws ResourceManagerException upon failure + * @see + * Resource Manager getIamPolicy + */ + public Policy getPolicy() { + return resourceManager.getPolicy(projectId()); + } + + /** + * Sets the IAM access control policy for the specified project. Replaces any existing policy. + * It is recommended that you use the read-modify-write pattern. See code samples and important + * details of replacing policies in the documentation for {@link ResourceManager#replacePolicy}. + * + * @throws ResourceManagerException upon failure + * @see ResourceManager#replacePolicy + * @see + * Resource Manager setIamPolicy + */ + public Policy replacePolicy(Policy newPolicy) { + return resourceManager.replacePolicy(projectId(), newPolicy); + } + + /** + * Returns the permissions that a caller has on this project. You typically don't call this method + * if you're using Google Cloud Platform directly to manage permissions. This method is intended + * for integration with your proprietary software, such as a customized graphical user interface. + * For example, the Cloud Platform Console tests IAM permissions internally to determine which UI + * should be available to the logged-in user. + * + * @return A list of booleans representing whether the caller has the permissions specified (in + * the order of the given permissions) + * @throws ResourceManagerException upon failure + * @see + * Resource Manager testIamPermissions + */ + List testPermissions(List permissions) { + return resourceManager.testPermissions(projectId(), permissions); + } + + /** + * Returns the permissions that a caller has on this project. You typically don't call this method + * if you're using Google Cloud Platform directly to manage permissions. This method is intended + * for integration with your proprietary software, such as a customized graphical user interface. + * For example, the Cloud Platform Console tests IAM permissions internally to determine which UI + * should be available to the logged-in user. + * + * @return A list of booleans representing whether the caller has the permissions specified (in + * the order of the given permissions) + * @throws ResourceManagerException upon failure + * @see + * Resource Manager testIamPermissions + */ + List testPermissions(Permission first, Permission... others) { + return resourceManager.testPermissions(projectId(), first, others); + } + @Override public Builder toBuilder() { return new Builder(this); diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java index 882ec77197f3..816f541146d5 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java @@ -25,13 +25,19 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.gcloud.Identity; +import com.google.gcloud.resourcemanager.Policy.Role; import com.google.gcloud.resourcemanager.ProjectInfo.ResourceId; +import com.google.gcloud.resourcemanager.ResourceManager.Permission; import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.util.List; import java.util.Map; public class ProjectTest { @@ -48,6 +54,13 @@ public class ProjectTest { .createTimeMillis(CREATE_TIME_MILLIS) .state(STATE) .build(); + private static final Identity USER = Identity.user("abc@gmail.com"); + private static final Identity SERVICE_ACCOUNT = + Identity.serviceAccount("service-account@gmail.com"); + private static final Policy POLICY = Policy.builder() + .addBinding(Role.owner(), ImmutableSet.of(USER)) + .addBinding(Role.editor(), ImmutableSet.of(SERVICE_ACCOUNT)) + .build(); private ResourceManager serviceMockReturnsOptions = createStrictMock(ResourceManager.class); private ResourceManagerOptions mockOptions = createMock(ResourceManagerOptions.class); @@ -205,6 +218,39 @@ public void testReplace() { compareProjectInfos(expectedReplacedProject, actualReplacedProject); } + @Test + public void testGetPolicy() { + expect(resourceManager.options()).andReturn(mockOptions).times(1); + expect(resourceManager.getPolicy(PROJECT_ID)).andReturn(POLICY); + replay(resourceManager); + initializeProject(); + assertEquals(POLICY, project.getPolicy()); + } + + @Test + public void testReplacePolicy() { + expect(resourceManager.options()).andReturn(mockOptions).times(1); + expect(resourceManager.replacePolicy(PROJECT_ID, POLICY)).andReturn(POLICY); + replay(resourceManager); + initializeProject(); + assertEquals(POLICY, project.replacePolicy(POLICY)); + } + + @Test + public void testTestPermissions() { + List response = ImmutableList.of(true, true); + expect(resourceManager.options()).andReturn(mockOptions).times(1); + expect(resourceManager.testPermissions(PROJECT_ID, Permission.GET, Permission.DELETE)) + .andReturn(response); + expect(resourceManager.testPermissions( + PROJECT_ID, ImmutableList.of(Permission.GET, Permission.DELETE))).andReturn(response); + replay(resourceManager); + initializeProject(); + assertEquals(response, project.testPermissions(Permission.GET, Permission.DELETE)); + assertEquals( + response, project.testPermissions(ImmutableList.of(Permission.GET, Permission.DELETE))); + } + private void compareProjects(Project expected, Project value) { assertEquals(expected, value); compareProjectInfos(expected, value); From c05e738b1f17048560b781f4445ca1ccff7f0759 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 21 Mar 2016 09:36:13 -0700 Subject: [PATCH 40/44] Add binding entries as necessary in IamPolicy.Builder --- .../java/com/google/gcloud/IamPolicy.java | 63 +++++-------------- .../java/com/google/gcloud/IamPolicyTest.java | 44 +++++++++---- .../snippets/ModifyPolicy.java | 6 +- .../gcloud/resourcemanager/Project.java | 18 +++--- .../gcloud/resourcemanager/PolicyTest.java | 16 ++--- .../gcloud/resourcemanager/ProjectTest.java | 4 +- .../ResourceManagerImplTest.java | 4 +- .../resourcemanager/SerializationTest.java | 6 +- 8 files changed, 73 insertions(+), 88 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java b/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java index 748eaba2ab4c..3d7c1422e6fa 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java @@ -83,39 +83,8 @@ public final B bindings(Map> bindings) { return self(); } - /** - * Adds a binding to the policy. - * - * @throws IllegalArgumentException if the policy already contains a binding with the same role - * or if the role or any identities are null - */ - public final B addBinding(R role, Set identities) { - verifyBinding(role, identities); - checkArgument(!bindings.containsKey(role), - "The policy already contains a binding with the role " + role.toString() + "."); - bindings.put(role, new HashSet(identities)); - return self(); - } - - /** - * Adds a binding to the policy. - * - * @throws IllegalArgumentException if the policy already contains a binding with the same role - * or if the role or any identities are null - */ - public final B addBinding(R role, Identity first, Identity... others) { - HashSet identities = new HashSet<>(); - identities.add(first); - identities.addAll(Arrays.asList(others)); - return addBinding(role, identities); - } - private void verifyBinding(R role, Collection identities) { checkArgument(role != null, "The role cannot be null."); - verifyIdentities(identities); - } - - private void verifyIdentities(Collection identities) { checkArgument(identities != null, "A role cannot be assigned to a null set of identities."); checkArgument(!identities.contains(null), "Null identities are not permitted."); } @@ -129,33 +98,33 @@ public final B removeBinding(R role) { } /** - * Adds one or more identities to an existing binding. - * - * @throws IllegalArgumentException if the policy doesn't contain a binding with the specified - * role or any identities are null + * Adds one or more identities to the policy under the role specified. Creates a new role + * binding if the binding corresponding to the given role did not previously exist. */ public final B addIdentity(R role, Identity first, Identity... others) { - checkArgument(bindings.containsKey(role), - "The policy doesn't contain the role " + role.toString() + "."); List toAdd = new LinkedList<>(); toAdd.add(first); toAdd.addAll(Arrays.asList(others)); - verifyIdentities(toAdd); - bindings.get(role).addAll(toAdd); + verifyBinding(role, toAdd); + Set identities = bindings.get(role); + if (identities == null) { + identities = new HashSet(); + bindings.put(role, identities); + } + identities.addAll(toAdd); return self(); } /** - * Removes one or more identities from an existing binding. - * - * @throws IllegalArgumentException if the policy doesn't contain a binding with the specified - * role + * Removes one or more identities from an existing binding. Does nothing if the binding + * associated with the provided role doesn't exist. */ public final B removeIdentity(R role, Identity first, Identity... others) { - checkArgument(bindings.containsKey(role), - "The policy doesn't contain the role " + role.toString() + "."); - bindings.get(role).remove(first); - bindings.get(role).removeAll(Arrays.asList(others)); + Set identities = bindings.get(role); + if (identities != null) { + identities.remove(first); + identities.removeAll(Arrays.asList(others)); + } return self(); } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java index db0935c4766d..2f40c3b76001 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java @@ -28,6 +28,7 @@ import org.junit.Test; +import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -46,8 +47,8 @@ public class IamPolicyTest { "editor", ImmutableSet.of(ALL_AUTH_USERS, GROUP, DOMAIN)); private static final PolicyImpl SIMPLE_POLICY = PolicyImpl.builder() - .addBinding("viewer", ImmutableSet.of(USER, SERVICE_ACCOUNT, ALL_USERS)) - .addBinding("editor", ImmutableSet.of(ALL_AUTH_USERS, GROUP, DOMAIN)) + .addIdentity("viewer", USER, SERVICE_ACCOUNT, ALL_USERS) + .addIdentity("editor", ALL_AUTH_USERS, GROUP, DOMAIN) .build(); private static final PolicyImpl FULL_POLICY = new PolicyImpl.Builder(SIMPLE_POLICY.bindings(), "etag", 1).build(); @@ -105,22 +106,43 @@ public void testBuilder() { policy.bindings()); assertNull(policy.etag()); assertNull(policy.version()); - policy = PolicyImpl.builder().addBinding("owner", USER, SERVICE_ACCOUNT).build(); + policy = PolicyImpl.builder() + .removeIdentity("viewer", USER) + .addIdentity("owner", USER, SERVICE_ACCOUNT) + .build(); assertEquals( ImmutableMap.of("owner", ImmutableSet.of(USER, SERVICE_ACCOUNT)), policy.bindings()); assertNull(policy.etag()); assertNull(policy.version()); + } + + @Test + public void testIllegalPolicies() { + try { + PolicyImpl.builder().addIdentity(null, USER); + fail("Null role should cause exception."); + } catch (IllegalArgumentException ex) { + assertEquals("The role cannot be null.", ex.getMessage()); + } + try { + PolicyImpl.builder().addIdentity("viewer", null, USER); + fail("Null identity should cause exception."); + } catch (IllegalArgumentException ex) { + assertEquals("Null identities are not permitted.", ex.getMessage()); + } try { - SIMPLE_POLICY.toBuilder().addBinding("viewer", USER); - fail("Should have failed due to duplicate role."); - } catch (IllegalArgumentException e) { - assertEquals("The policy already contains a binding with the role viewer.", e.getMessage()); + PolicyImpl.builder().bindings(null); + fail("Null bindings map should cause exception."); + } catch (IllegalArgumentException ex) { + assertEquals("The provided map of bindings cannot be null.", ex.getMessage()); } try { - SIMPLE_POLICY.toBuilder().addBinding("editor", ImmutableSet.of(USER)); - fail("Should have failed due to duplicate role."); - } catch (IllegalArgumentException e) { - assertEquals("The policy already contains a binding with the role editor.", e.getMessage()); + Map> bindings = new HashMap<>(); + bindings.put("viewer", null); + PolicyImpl.builder().bindings(bindings); + fail("Null set of identities should cause exception."); + } catch (IllegalArgumentException ex) { + assertEquals("A role cannot be assigned to a null set of identities.", ex.getMessage()); } } diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java index 7401f0b88bbc..478765a9ecf4 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java @@ -49,11 +49,7 @@ public static void main(String... args) { // Add a viewer Policy.Builder modifiedPolicy = policy.toBuilder(); Identity newViewer = Identity.user(""); - if (policy.bindings().containsKey(Role.viewer())) { - modifiedPolicy.addIdentity(Role.viewer(), newViewer); - } else { - modifiedPolicy.addBinding(Role.viewer(), newViewer); - } + modifiedPolicy.addIdentity(Role.viewer(), newViewer); // Write policy Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build()); diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java index dd252155645b..8c6a0eacd44f 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java @@ -202,10 +202,10 @@ public Project replace() { } /** - * Returns the IAM access control policy for the specified project. Returns {@code null} if the - * resource does not exist or if you do not have adequate permission to view the project or get - * the policy. + * Returns the IAM access control policy for this project. Returns {@code null} if the resource + * does not exist or if you do not have adequate permission to view the project or get the policy. * + * @return the IAM policy for the project * @throws ResourceManagerException upon failure * @see @@ -216,12 +216,12 @@ public Policy getPolicy() { } /** - * Sets the IAM access control policy for the specified project. Replaces any existing policy. - * It is recommended that you use the read-modify-write pattern. See code samples and important - * details of replacing policies in the documentation for {@link ResourceManager#replacePolicy}. + * Sets the IAM access control policy for this project. Replaces any existing policy. It is + * recommended that you use the read-modify-write pattern. See code samples and important details + * of replacing policies in the documentation for {@link ResourceManager#replacePolicy}. * + * @return the newly set IAM policy for this project * @throws ResourceManagerException upon failure - * @see ResourceManager#replacePolicy * @see * Resource Manager setIamPolicy @@ -237,7 +237,7 @@ public Policy replacePolicy(Policy newPolicy) { * For example, the Cloud Platform Console tests IAM permissions internally to determine which UI * should be available to the logged-in user. * - * @return A list of booleans representing whether the caller has the permissions specified (in + * @return a list of booleans representing whether the caller has the permissions specified (in * the order of the given permissions) * @throws ResourceManagerException upon failure * @see testPermissions(List permissions) { * For example, the Cloud Platform Console tests IAM permissions internally to determine which UI * should be available to the logged-in user. * - * @return A list of booleans representing whether the caller has the permissions specified (in + * @return a list of booleans representing whether the caller has the permissions specified (in * the order of the given permissions) * @throws ResourceManagerException upon failure * @see EMPTY_RPC_OPTIONS = ImmutableMap.of(); private static final Policy POLICY = Policy.builder() - .addBinding(Role.owner(), Identity.user("me@gmail.com")) - .addBinding(Role.editor(), Identity.serviceAccount("serviceaccount@gmail.com")) + .addIdentity(Role.owner(), Identity.user("me@gmail.com")) + .addIdentity(Role.editor(), Identity.serviceAccount("serviceaccount@gmail.com")) .build(); @Rule diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index c6e907da16a0..1c0b9c68c86d 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -17,7 +17,6 @@ package com.google.gcloud.resourcemanager; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.Identity; import com.google.gcloud.PageImpl; @@ -46,9 +45,8 @@ public class SerializationTest extends BaseSerializationTest { ResourceManager.ProjectGetOption.fields(ResourceManager.ProjectField.NAME); private static final ResourceManager.ProjectListOption PROJECT_LIST_OPTION = ResourceManager.ProjectListOption.filter("name:*"); - private static final Policy POLICY = Policy.builder() - .addBinding(Policy.Role.viewer(), ImmutableSet.of(Identity.user("abc@gmail.com"))) - .build(); + private static final Policy POLICY = + Policy.builder().addIdentity(Policy.Role.viewer(), Identity.user("abc@gmail.com")).build(); private static final ResourceManagerException RESOURCE_MANAGER_EXCEPTION = new ResourceManagerException(42, "message"); From 7bbe67fd3651f9294b6c3bcb3265a3872d2f1383 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 21 Mar 2016 16:29:10 -0700 Subject: [PATCH 41/44] Take care of minor changes --- .../java/com/google/gcloud/IamPolicy.java | 44 +++++++++++-------- .../java/com/google/gcloud/IamPolicyTest.java | 29 +++++++++--- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java b/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java index 3d7c1422e6fa..9cce4b23c864 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/IamPolicy.java @@ -17,17 +17,16 @@ package com.google.gcloud; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.io.Serializable; import java.util.Arrays; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -69,12 +68,16 @@ protected Builder() {} /** * Replaces the builder's map of bindings with the given map of bindings. * - * @throws IllegalArgumentException if the provided map is null or contain any null values + * @throws NullPointerException if the given map is null or contains any null keys or values + * @throws IllegalArgumentException if any identities in the given map are null */ public final B bindings(Map> bindings) { - checkArgument(bindings != null, "The provided map of bindings cannot be null."); + checkNotNull(bindings, "The provided map of bindings cannot be null."); for (Map.Entry> binding : bindings.entrySet()) { - verifyBinding(binding.getKey(), binding.getValue()); + checkNotNull(binding.getKey(), "The role cannot be null."); + Set identities = binding.getValue(); + checkNotNull(identities, "A role cannot be assigned to a null set of identities."); + checkArgument(!identities.contains(null), "Null identities are not permitted."); } this.bindings.clear(); for (Map.Entry> binding : bindings.entrySet()) { @@ -83,30 +86,30 @@ public final B bindings(Map> bindings) { return self(); } - private void verifyBinding(R role, Collection identities) { - checkArgument(role != null, "The role cannot be null."); - checkArgument(identities != null, "A role cannot be assigned to a null set of identities."); - checkArgument(!identities.contains(null), "Null identities are not permitted."); - } - /** - * Removes the binding associated with the specified role. + * Removes the role (and all identities associated with that role) from the policy. */ - public final B removeBinding(R role) { + public final B removeRole(R role) { bindings.remove(role); return self(); } /** - * Adds one or more identities to the policy under the role specified. Creates a new role - * binding if the binding corresponding to the given role did not previously exist. + * Adds one or more identities to the policy under the role specified. + * + * @throws NullPointerException if the role or any of the identities is null. */ public final B addIdentity(R role, Identity first, Identity... others) { - List toAdd = new LinkedList<>(); + String nullIdentityMessage = "Null identities are not permitted."; + checkNotNull(first, nullIdentityMessage); + checkNotNull(others, nullIdentityMessage); + for (Identity identity : others) { + checkNotNull(identity, nullIdentityMessage); + } + Set toAdd = new LinkedHashSet<>(); toAdd.add(first); toAdd.addAll(Arrays.asList(others)); - verifyBinding(role, toAdd); - Set identities = bindings.get(role); + Set identities = bindings.get(checkNotNull(role, "The role cannot be null.")); if (identities == null) { identities = new HashSet(); bindings.put(role, identities); @@ -125,6 +128,9 @@ public final B removeIdentity(R role, Identity first, Identity... others) { identities.remove(first); identities.removeAll(Arrays.asList(others)); } + if (identities != null && identities.isEmpty()) { + bindings.remove(role); + } return self(); } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java index 2f40c3b76001..235c2c2b1c85 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/IamPolicyTest.java @@ -29,6 +29,7 @@ import org.junit.Test; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -94,7 +95,7 @@ public void testBuilder() { assertEquals(editorBinding, policy.bindings()); assertEquals("etag", policy.etag()); assertEquals(1, policy.version().intValue()); - policy = SIMPLE_POLICY.toBuilder().removeBinding("editor").build(); + policy = SIMPLE_POLICY.toBuilder().removeRole("editor").build(); assertEquals(ImmutableMap.of("viewer", BINDINGS.get("viewer")), policy.bindings()); assertNull(policy.etag()); assertNull(policy.version()); @@ -109,6 +110,8 @@ public void testBuilder() { policy = PolicyImpl.builder() .removeIdentity("viewer", USER) .addIdentity("owner", USER, SERVICE_ACCOUNT) + .addIdentity("editor", GROUP) + .removeIdentity("editor", GROUP) .build(); assertEquals( ImmutableMap.of("owner", ImmutableSet.of(USER, SERVICE_ACCOUNT)), policy.bindings()); @@ -121,19 +124,25 @@ public void testIllegalPolicies() { try { PolicyImpl.builder().addIdentity(null, USER); fail("Null role should cause exception."); - } catch (IllegalArgumentException ex) { + } catch (NullPointerException ex) { assertEquals("The role cannot be null.", ex.getMessage()); } try { PolicyImpl.builder().addIdentity("viewer", null, USER); fail("Null identity should cause exception."); - } catch (IllegalArgumentException ex) { + } catch (NullPointerException ex) { + assertEquals("Null identities are not permitted.", ex.getMessage()); + } + try { + PolicyImpl.builder().addIdentity("viewer", USER, (Identity[]) null); + fail("Null identity should cause exception."); + } catch (NullPointerException ex) { assertEquals("Null identities are not permitted.", ex.getMessage()); } try { PolicyImpl.builder().bindings(null); fail("Null bindings map should cause exception."); - } catch (IllegalArgumentException ex) { + } catch (NullPointerException ex) { assertEquals("The provided map of bindings cannot be null.", ex.getMessage()); } try { @@ -141,9 +150,19 @@ public void testIllegalPolicies() { bindings.put("viewer", null); PolicyImpl.builder().bindings(bindings); fail("Null set of identities should cause exception."); - } catch (IllegalArgumentException ex) { + } catch (NullPointerException ex) { assertEquals("A role cannot be assigned to a null set of identities.", ex.getMessage()); } + try { + Map> bindings = new HashMap<>(); + Set identities = new HashSet<>(); + identities.add(null); + bindings.put("viewer", identities); + PolicyImpl.builder().bindings(bindings); + fail("Null identity should cause exception."); + } catch (IllegalArgumentException ex) { + assertEquals("Null identities are not permitted.", ex.getMessage()); + } } @Test From 3a97ae10587c21d34cf495447a3f9c3e9b9093da Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 21 Mar 2016 17:47:54 -0700 Subject: [PATCH 42/44] Make role and permission strings to allow for service-specific values --- .../snippets/ModifyPolicy.java | 4 +- .../google/gcloud/resourcemanager/Policy.java | 123 +++++------------- .../gcloud/resourcemanager/Project.java | 22 +++- .../resourcemanager/ResourceManager.java | 51 +++----- .../resourcemanager/ResourceManagerImpl.java | 15 +-- .../testing/LocalResourceManagerHelper.java | 14 +- .../LocalResourceManagerHelperTest.java | 7 - .../gcloud/resourcemanager/PolicyTest.java | 29 ++--- .../gcloud/resourcemanager/ProjectTest.java | 19 +-- .../ResourceManagerImplTest.java | 23 ++-- .../resourcemanager/SerializationTest.java | 6 +- 11 files changed, 108 insertions(+), 205 deletions(-) diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java index 478765a9ecf4..f97adf5b0916 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/ModifyPolicy.java @@ -24,7 +24,7 @@ import com.google.gcloud.Identity; import com.google.gcloud.resourcemanager.Policy; -import com.google.gcloud.resourcemanager.Policy.Role; +import com.google.gcloud.resourcemanager.Policy.ProjectRole; import com.google.gcloud.resourcemanager.Project; import com.google.gcloud.resourcemanager.ResourceManager; import com.google.gcloud.resourcemanager.ResourceManagerOptions; @@ -49,7 +49,7 @@ public static void main(String... args) { // Add a viewer Policy.Builder modifiedPolicy = policy.toBuilder(); Identity newViewer = Identity.user(""); - modifiedPolicy.addIdentity(Role.viewer(), newViewer); + modifiedPolicy.addIdentity(ProjectRole.VIEWER.value(), newViewer); // Write policy Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build()); diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java index 6399b52b3c04..884f474bed59 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java @@ -23,13 +23,11 @@ import com.google.gcloud.IamPolicy; import com.google.gcloud.Identity; -import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Set; /** @@ -42,120 +40,63 @@ * * @see Policy */ -public class Policy extends IamPolicy { +public class Policy extends IamPolicy { private static final long serialVersionUID = -5573557282693961850L; /** - * Represents legacy roles in an IAM Policy. + * The project-level roles in an IAM policy. This enum is not an exhaustive list of all roles + * you can use in an IAM policy. You can also use service-specific roles (e.g. + * roles/pubsub.editor). See the Supported Cloud Platform Services page for links + * to service-specific roles. + * + * @see Supported Cloud + * Platform Services */ - public static class Role implements Serializable { + public enum ProjectRole { /** - * The recognized roles in a Project's IAM policy. + * Permissions for read-only actions that preserve state. */ - public enum Type { - - /** - * Permissions for read-only actions that preserve state. - */ - VIEWER, - - /** - * All viewer permissions and permissions for actions that modify state. - */ - EDITOR, - - /** - * All editor permissions and permissions for the following actions: - *

    - *
  • Manage access control for a resource. - *
  • Set up billing (for a project). - *
- */ - OWNER - } - - private static final long serialVersionUID = 2421978909244287488L; - - private final String value; - private final Type type; - - private Role(String value, Type type) { - this.value = value; - this.type = type; - } - - String value() { - return value; - } + VIEWER("roles/viewer"), /** - * Returns the type of role (editor, owner, or viewer). Returns {@code null} if the role type - * is unrecognized. + * All viewer permissions and permissions for actions that modify state. */ - public Type type() { - return type; - } + EDITOR("roles/editor"), /** - * Returns a {@code Role} of type {@link Type#VIEWER VIEWER}. + * All editor permissions and permissions for the following actions: + *
    + *
  • Manage access control for a resource. + *
  • Set up billing (for a project). + *
*/ - public static Role viewer() { - return new Role("roles/viewer", Type.VIEWER); - } + OWNER("roles/owner"); - /** - * Returns a {@code Role} of type {@link Type#EDITOR EDITOR}. - */ - public static Role editor() { - return new Role("roles/editor", Type.EDITOR); + String value; + + private ProjectRole(String value) { + this.value = value; } /** - * Returns a {@code Role} of type {@link Type#OWNER OWNER}. + * Returns the string value associated with the role. */ - public static Role owner() { - return new Role("roles/owner", Type.OWNER); - } - - static Role rawRole(String roleStr) { - return new Role(roleStr, null); - } - - static Role fromStr(String roleStr) { - try { - Type type = Type.valueOf(roleStr.split("/")[1].toUpperCase()); - return new Role(roleStr, type); - } catch (Exception ex) { - return new Role(roleStr, null); - } - } - - @Override - public final int hashCode() { - return Objects.hash(value, type); - } - - @Override - public final boolean equals(Object obj) { - if (!(obj instanceof Role)) { - return false; - } - Role other = (Role) obj; - return Objects.equals(value, other.value()) && Objects.equals(type, other.type()); + public String value() { + return value; } } /** * Builder for an IAM Policy. */ - public static class Builder extends IamPolicy.Builder { + public static class Builder extends IamPolicy.Builder { private Builder() {} @VisibleForTesting - Builder(Map> bindings, String etag, Integer version) { + Builder(Map> bindings, String etag, Integer version) { bindings(bindings).etag(etag).version(version); } @@ -188,10 +129,10 @@ com.google.api.services.cloudresourcemanager.model.Policy toPb() { new com.google.api.services.cloudresourcemanager.model.Policy(); List bindingPbList = new LinkedList<>(); - for (Map.Entry> binding : bindings().entrySet()) { + for (Map.Entry> binding : bindings().entrySet()) { com.google.api.services.cloudresourcemanager.model.Binding bindingPb = new com.google.api.services.cloudresourcemanager.model.Binding(); - bindingPb.setRole(binding.getKey().value()); + bindingPb.setRole(binding.getKey()); bindingPb.setMembers( Lists.transform( new ArrayList<>(binding.getValue()), @@ -211,11 +152,11 @@ public String apply(Identity identity) { static Policy fromPb( com.google.api.services.cloudresourcemanager.model.Policy policyPb) { - Map> bindings = new HashMap<>(); + Map> bindings = new HashMap<>(); for (com.google.api.services.cloudresourcemanager.model.Binding bindingPb : policyPb.getBindings()) { bindings.put( - Role.fromStr(bindingPb.getRole()), + bindingPb.getRole(), ImmutableSet.copyOf( Lists.transform( bindingPb.getMembers(), diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java index 8c6a0eacd44f..de5e6c336af4 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java @@ -18,8 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.gcloud.resourcemanager.ResourceManager.Permission; - import java.io.IOException; import java.io.ObjectInputStream; import java.util.List; @@ -235,7 +233,9 @@ public Policy replacePolicy(Policy newPolicy) { * if you're using Google Cloud Platform directly to manage permissions. This method is intended * for integration with your proprietary software, such as a customized graphical user interface. * For example, the Cloud Platform Console tests IAM permissions internally to determine which UI - * should be available to the logged-in user. + * should be available to the logged-in user. Each service that supports IAM lists the possible + * permissions; see the Supported Cloud Platform services page below for links to these + * lists. * * @return a list of booleans representing whether the caller has the permissions specified (in * the order of the given permissions) @@ -243,8 +243,11 @@ public Policy replacePolicy(Policy newPolicy) { * @see * Resource Manager testIamPermissions + * @see Supported Cloud Platform + * Services */ - List testPermissions(List permissions) { + List testPermissions(List permissions) { return resourceManager.testPermissions(projectId(), permissions); } @@ -253,7 +256,9 @@ List testPermissions(List permissions) { * if you're using Google Cloud Platform directly to manage permissions. This method is intended * for integration with your proprietary software, such as a customized graphical user interface. * For example, the Cloud Platform Console tests IAM permissions internally to determine which UI - * should be available to the logged-in user. + * should be available to the logged-in user. Each service that supports IAM lists the possible + * permissions; see the Supported Cloud Platform services page below for links to these + * lists. * * @return a list of booleans representing whether the caller has the permissions specified (in * the order of the given permissions) @@ -261,9 +266,12 @@ List testPermissions(List permissions) { * @see * Resource Manager testIamPermissions + * @see Supported Cloud Platform + * Services */ - List testPermissions(Permission first, Permission... others) { - return resourceManager.testPermissions(projectId(), first, others); + List testPermissions(String firstPermission, String... otherPermissions) { + return resourceManager.testPermissions(projectId(), firstPermission, otherPermissions); } @Override diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index f14d47f2a676..708bd711b477 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -169,38 +169,6 @@ public static ProjectListOption fields(ProjectField... fields) { } } - /** - * The permissions associated with a Google Cloud project. These values can be used when calling - * {@link #testPermissions}. - * - * @see - * Project-level roles - */ - enum Permission { - DELETE("delete"), - GET("get"), - GET_POLICY("getIamPolicy"), - REPLACE("update"), - REPLACE_POLICY("setIamPolicy"), - UNDELETE("undelete"); - - private static final String PREFIX = "resourcemanager.projects."; - - private final String value; - - Permission(String suffix) { - this.value = PREFIX + suffix; - } - - /** - * Returns the string representation of the permission. - */ - public String value() { - return value; - } - } - /** * Creates a new project. * @@ -358,7 +326,9 @@ public String value() { * this method if you're using Google Cloud Platform directly to manage permissions. This method * is intended for integration with your proprietary software, such as a customized graphical user * interface. For example, the Cloud Platform Console tests IAM permissions internally to - * determine which UI should be available to the logged-in user. + * determine which UI should be available to the logged-in user. Each service that supports IAM + * lists the possible permissions; see the Supported Cloud Platform services page below for + * links to these lists. * * @return A list of booleans representing whether the caller has the permissions specified (in * the order of the given permissions) @@ -366,15 +336,20 @@ public String value() { * @see * Resource Manager testIamPermissions + * @see Supported Cloud Platform + * Services */ - List testPermissions(String projectId, List permissions); + List testPermissions(String projectId, List permissions); /** * Returns the permissions that a caller has on the specified project. You typically don't call * this method if you're using Google Cloud Platform directly to manage permissions. This method * is intended for integration with your proprietary software, such as a customized graphical user * interface. For example, the Cloud Platform Console tests IAM permissions internally to - * determine which UI should be available to the logged-in user. + * determine which UI should be available to the logged-in user. Each service that supports IAM + * lists the possible permissions; see the Supported Cloud Platform services page below for + * links to these lists. * * @return A list of booleans representing whether the caller has the permissions specified (in * the order of the given permissions) @@ -382,6 +357,10 @@ public String value() { * @see * Resource Manager testIamPermissions + * @see Supported Cloud Platform + * Services */ - List testPermissions(String projectId, Permission first, Permission... others); + List testPermissions( + String projectId, String firstPermission, String... otherPermissions); } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java index d9911b911f0b..5526918bc1eb 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java @@ -216,19 +216,13 @@ public com.google.api.services.cloudresourcemanager.model.Policy call() { } @Override - public List testPermissions(final String projectId, final List permissions) { + public List testPermissions(final String projectId, final List permissions) { try { return runWithRetries( new Callable>() { @Override public List call() { - return resourceManagerRpc.testPermissions(projectId, - Lists.transform(permissions, new Function() { - @Override - public String apply(Permission permission) { - return permission.value(); - } - })); + return resourceManagerRpc.testPermissions(projectId, permissions); } }, options().retryParams(), EXCEPTION_HANDLER); } catch (RetryHelperException ex) { @@ -237,8 +231,9 @@ public String apply(Permission permission) { } @Override - public List testPermissions(String projectId, Permission first, Permission... others) { - return testPermissions(projectId, Lists.asList(first, others)); + public List testPermissions( + String projectId, String firstPermission, String... otherPermissions) { + return testPermissions(projectId, Lists.asList(firstPermission, otherPermissions)); } private Map optionMap(Option... options) { diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java index 8ddca18b6261..4d466e55a897 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java @@ -18,7 +18,6 @@ import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteStreams; import com.google.gcloud.AuthCredentials; -import com.google.gcloud.resourcemanager.ResourceManager.Permission; import com.google.gcloud.resourcemanager.ResourceManagerOptions; import com.sun.net.httpserver.Headers; @@ -38,7 +37,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; @@ -66,7 +64,8 @@ *
  • IAM policies are set to an empty policy with version 0 (only legacy roles supported) upon * project creation. The actual service will not have an empty list of bindings and may also * set your version to 1. - *
  • There is no input validation for the policy provided when replacing a policy. + *
  • There is no input validation for the policy provided when replacing a policy or calling + * testIamPermissions. *
  • In this mock, projects never move from the DELETE_REQUESTED lifecycle state to * DELETE_IN_PROGRESS without an explicit call to the utility method * {@link #changeLifecycleState}. Similarly, a project is never completely removed without an @@ -89,12 +88,8 @@ public class LocalResourceManagerHelper { private static final Pattern LIST_FIELDS_PATTERN = Pattern.compile("(.*?)projects\\((.*?)\\)(.*?)"); private static final String[] NO_FIELDS = {}; - private static final Set PERMISSIONS = new HashSet<>(); static { - for (Permission permission : Permission.values()) { - PERMISSIONS.add(permission.value()); - } try { BASE_CONTEXT = new URI(CONTEXT); } catch (URISyntaxException e) { @@ -635,11 +630,6 @@ synchronized Response testPermissions(String projectId, List permissions if (!projects.containsKey(projectId)) { return Error.PERMISSION_DENIED.response("Project " + projectId + " not found."); } - for (String p : permissions) { - if (!PERMISSIONS.contains(p)) { - return Error.INVALID_ARGUMENT.response("Invalid permission: " + p); - } - } try { return new Response(HTTP_OK, jsonFactory.toString(new TestIamPermissionsResponse().setPermissions(permissions))); diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java index 829094816664..75df0ef9e3ae 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java @@ -676,13 +676,6 @@ public void testTestPermissions() { assertEquals("Project nonexistent-project not found.", e.getMessage()); } rpc.create(PARTIAL_PROJECT); - try { - rpc.testPermissions(PARTIAL_PROJECT.getProjectId(), ImmutableList.of("get")); - fail("Invalid permission."); - } catch (ResourceManagerException e) { - assertEquals(400, e.code()); - assertEquals("Invalid permission: get", e.getMessage()); - } assertEquals(ImmutableList.of(true), rpc.testPermissions(PARTIAL_PROJECT.getProjectId(), permissions)); } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java index 7315b9f92565..04826dd9540f 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/PolicyTest.java @@ -18,12 +18,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import com.google.common.collect.ImmutableSet; import com.google.gcloud.Identity; -import com.google.gcloud.resourcemanager.Policy.Role; -import com.google.gcloud.resourcemanager.Policy.Role.Type; +import com.google.gcloud.resourcemanager.Policy.ProjectRole; import org.junit.Test; @@ -37,10 +34,10 @@ public class PolicyTest { private static final Identity GROUP = Identity.group("group@gmail.com"); private static final Identity DOMAIN = Identity.domain("google.com"); private static final Policy SIMPLE_POLICY = Policy.builder() - .addIdentity(Role.owner(), USER) - .addIdentity(Role.viewer(), ALL_USERS) - .addIdentity(Role.editor(), ALL_AUTH_USERS, DOMAIN) - .addIdentity(Role.rawRole("some-role"), SERVICE_ACCOUNT, GROUP) + .addIdentity(ProjectRole.OWNER.value(), USER) + .addIdentity(ProjectRole.VIEWER.value(), ALL_USERS) + .addIdentity(ProjectRole.EDITOR.value(), ALL_AUTH_USERS, DOMAIN) + .addIdentity("roles/some-role", SERVICE_ACCOUNT, GROUP) .build(); private static final Policy FULL_POLICY = new Policy.Builder(SIMPLE_POLICY.bindings(), "etag", 1).build(); @@ -57,21 +54,13 @@ public void testPolicyToAndFromPb() { assertEquals(SIMPLE_POLICY, Policy.fromPb(SIMPLE_POLICY.toPb())); } - @Test - public void testRoleType() { - assertEquals(Type.OWNER, Role.owner().type()); - assertEquals(Type.EDITOR, Role.editor().type()); - assertEquals(Type.VIEWER, Role.viewer().type()); - assertNull(Role.rawRole("raw-role").type()); - } - @Test public void testEquals() { Policy copy = Policy.builder() - .addIdentity(Role.owner(), USER) - .addIdentity(Role.viewer(), ALL_USERS) - .addIdentity(Role.editor(), ALL_AUTH_USERS, DOMAIN) - .addIdentity(Role.rawRole("some-role"), SERVICE_ACCOUNT, GROUP) + .addIdentity(ProjectRole.OWNER.value(), USER) + .addIdentity(ProjectRole.VIEWER.value(), ALL_USERS) + .addIdentity(ProjectRole.EDITOR.value(), ALL_AUTH_USERS, DOMAIN) + .addIdentity("roles/some-role", SERVICE_ACCOUNT, GROUP) .build(); assertEquals(SIMPLE_POLICY, copy); assertNotEquals(SIMPLE_POLICY, FULL_POLICY); diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java index 53b37111b705..acce6f84c680 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java @@ -27,11 +27,9 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import com.google.gcloud.Identity; -import com.google.gcloud.resourcemanager.Policy.Role; +import com.google.gcloud.resourcemanager.Policy.ProjectRole; import com.google.gcloud.resourcemanager.ProjectInfo.ResourceId; -import com.google.gcloud.resourcemanager.ResourceManager.Permission; import org.junit.After; import org.junit.Before; @@ -58,8 +56,8 @@ public class ProjectTest { private static final Identity SERVICE_ACCOUNT = Identity.serviceAccount("service-account@gmail.com"); private static final Policy POLICY = Policy.builder() - .addIdentity(Role.owner(), USER) - .addIdentity(Role.editor(), SERVICE_ACCOUNT) + .addIdentity(ProjectRole.OWNER.value(), USER) + .addIdentity(ProjectRole.EDITOR.value(), SERVICE_ACCOUNT) .build(); private ResourceManager serviceMockReturnsOptions = createStrictMock(ResourceManager.class); @@ -239,16 +237,19 @@ public void testReplacePolicy() { @Test public void testTestPermissions() { List response = ImmutableList.of(true, true); + String getPermission = "resourcemanager.projects.get"; + String deletePermission = "resourcemanager.projects.delete"; expect(resourceManager.options()).andReturn(mockOptions).times(1); - expect(resourceManager.testPermissions(PROJECT_ID, Permission.GET, Permission.DELETE)) + expect(resourceManager.testPermissions(PROJECT_ID, getPermission, deletePermission)) .andReturn(response); expect(resourceManager.testPermissions( - PROJECT_ID, ImmutableList.of(Permission.GET, Permission.DELETE))).andReturn(response); + PROJECT_ID, ImmutableList.of(getPermission, deletePermission))) + .andReturn(response); replay(resourceManager); initializeProject(); - assertEquals(response, project.testPermissions(Permission.GET, Permission.DELETE)); + assertEquals(response, project.testPermissions(getPermission, deletePermission)); assertEquals( - response, project.testPermissions(ImmutableList.of(Permission.GET, Permission.DELETE))); + response, project.testPermissions(ImmutableList.of(getPermission, deletePermission))); } private void compareProjects(Project expected, Project value) { diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java index 9cb9bfcba02f..2525039e9c0d 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java @@ -29,9 +29,8 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.Identity; import com.google.gcloud.Page; -import com.google.gcloud.resourcemanager.Policy.Role; +import com.google.gcloud.resourcemanager.Policy.ProjectRole; import com.google.gcloud.resourcemanager.ProjectInfo.ResourceId; -import com.google.gcloud.resourcemanager.ResourceManager.Permission; import com.google.gcloud.resourcemanager.ResourceManager.ProjectField; import com.google.gcloud.resourcemanager.ResourceManager.ProjectGetOption; import com.google.gcloud.resourcemanager.ResourceManager.ProjectListOption; @@ -71,10 +70,12 @@ public class ResourceManagerImplTest { .parent(PARENT) .build(); private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); - private static final Policy POLICY = Policy.builder() - .addIdentity(Role.owner(), Identity.user("me@gmail.com")) - .addIdentity(Role.editor(), Identity.serviceAccount("serviceaccount@gmail.com")) - .build(); + private static final Policy POLICY = + Policy.builder() + .addIdentity(ProjectRole.OWNER.value(), Identity.user("me@gmail.com")) + .addIdentity( + ProjectRole.EDITOR.value(), Identity.serviceAccount("serviceaccount@gmail.com")) + .build(); @Rule public ExpectedException thrown = ExpectedException.none(); @@ -369,7 +370,7 @@ public void testReplacePolicy() { @Test public void testTestPermissions() { - List permissions = ImmutableList.of(Permission.GET); + List permissions = ImmutableList.of("resourcemanager.projects.get"); try { RESOURCE_MANAGER.testPermissions("nonexistent-project", permissions); fail("Nonexistent project"); @@ -380,8 +381,12 @@ public void testTestPermissions() { RESOURCE_MANAGER.create(PARTIAL_PROJECT); assertEquals(ImmutableList.of(true), RESOURCE_MANAGER.testPermissions(PARTIAL_PROJECT.projectId(), permissions)); - assertEquals(ImmutableList.of(true, true), RESOURCE_MANAGER.testPermissions( - PARTIAL_PROJECT.projectId(), Permission.DELETE, Permission.GET)); + assertEquals( + ImmutableList.of(true, true), + RESOURCE_MANAGER.testPermissions( + PARTIAL_PROJECT.projectId(), + "resourcemanager.projects.delete", + "resourcemanager.projects.get")); } @Test diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 1c0b9c68c86d..4bc1bcede195 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -21,6 +21,7 @@ import com.google.gcloud.Identity; import com.google.gcloud.PageImpl; import com.google.gcloud.Restorable; +import com.google.gcloud.resourcemanager.Policy.ProjectRole; import java.io.Serializable; import java.util.Collections; @@ -45,8 +46,9 @@ public class SerializationTest extends BaseSerializationTest { ResourceManager.ProjectGetOption.fields(ResourceManager.ProjectField.NAME); private static final ResourceManager.ProjectListOption PROJECT_LIST_OPTION = ResourceManager.ProjectListOption.filter("name:*"); - private static final Policy POLICY = - Policy.builder().addIdentity(Policy.Role.viewer(), Identity.user("abc@gmail.com")).build(); + private static final Policy POLICY = Policy.builder() + .addIdentity(ProjectRole.VIEWER.value(), Identity.user("abc@gmail.com")) + .build(); private static final ResourceManagerException RESOURCE_MANAGER_EXCEPTION = new ResourceManagerException(42, "message"); From 52bf6c1001716cd5f2145ff3be40631d542cd200 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 21 Mar 2016 19:51:37 -0700 Subject: [PATCH 43/44] remove varargs for testPermissions and other minor fixes --- .../google/gcloud/resourcemanager/Policy.java | 4 ++-- .../gcloud/resourcemanager/Project.java | 23 ------------------- .../resourcemanager/ResourceManager.java | 22 ------------------ .../resourcemanager/ResourceManagerImpl.java | 7 ------ .../gcloud/resourcemanager/ProjectTest.java | 3 --- .../ResourceManagerImplTest.java | 6 ----- 6 files changed, 2 insertions(+), 63 deletions(-) diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java index 884f474bed59..219d74262319 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Policy.java @@ -47,7 +47,7 @@ public class Policy extends IamPolicy { /** * The project-level roles in an IAM policy. This enum is not an exhaustive list of all roles * you can use in an IAM policy. You can also use service-specific roles (e.g. - * roles/pubsub.editor). See the Supported Cloud Platform Services page for links + * "roles/pubsub.editor"). See the Supported Cloud Platform Services page for links * to service-specific roles. * * @see Supported Cloud @@ -74,7 +74,7 @@ public enum ProjectRole { */ OWNER("roles/owner"); - String value; + private final String value; private ProjectRole(String value) { this.value = value; diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java index de5e6c336af4..bf9cf0e01a6d 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java @@ -251,29 +251,6 @@ List testPermissions(List permissions) { return resourceManager.testPermissions(projectId(), permissions); } - /** - * Returns the permissions that a caller has on this project. You typically don't call this method - * if you're using Google Cloud Platform directly to manage permissions. This method is intended - * for integration with your proprietary software, such as a customized graphical user interface. - * For example, the Cloud Platform Console tests IAM permissions internally to determine which UI - * should be available to the logged-in user. Each service that supports IAM lists the possible - * permissions; see the Supported Cloud Platform services page below for links to these - * lists. - * - * @return a list of booleans representing whether the caller has the permissions specified (in - * the order of the given permissions) - * @throws ResourceManagerException upon failure - * @see - * Resource Manager testIamPermissions - * @see Supported Cloud Platform - * Services - */ - List testPermissions(String firstPermission, String... otherPermissions) { - return resourceManager.testPermissions(projectId(), firstPermission, otherPermissions); - } - @Override public Builder toBuilder() { return new Builder(this); diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index 708bd711b477..70eeb9c8eb50 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -341,26 +341,4 @@ public static ProjectListOption fields(ProjectField... fields) { * Services */ List testPermissions(String projectId, List permissions); - - /** - * Returns the permissions that a caller has on the specified project. You typically don't call - * this method if you're using Google Cloud Platform directly to manage permissions. This method - * is intended for integration with your proprietary software, such as a customized graphical user - * interface. For example, the Cloud Platform Console tests IAM permissions internally to - * determine which UI should be available to the logged-in user. Each service that supports IAM - * lists the possible permissions; see the Supported Cloud Platform services page below for - * links to these lists. - * - * @return A list of booleans representing whether the caller has the permissions specified (in - * the order of the given permissions) - * @throws ResourceManagerException upon failure - * @see - * Resource Manager testIamPermissions - * @see Supported Cloud Platform - * Services - */ - List testPermissions( - String projectId, String firstPermission, String... otherPermissions); } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java index 5526918bc1eb..e4663cb74cb9 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java @@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.gcloud.BaseService; import com.google.gcloud.Page; @@ -230,12 +229,6 @@ public List call() { } } - @Override - public List testPermissions( - String projectId, String firstPermission, String... otherPermissions) { - return testPermissions(projectId, Lists.asList(firstPermission, otherPermissions)); - } - private Map optionMap(Option... options) { Map temp = Maps.newEnumMap(ResourceManagerRpc.Option.class); for (Option option : options) { diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java index acce6f84c680..0f4c205dde17 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java @@ -240,14 +240,11 @@ public void testTestPermissions() { String getPermission = "resourcemanager.projects.get"; String deletePermission = "resourcemanager.projects.delete"; expect(resourceManager.options()).andReturn(mockOptions).times(1); - expect(resourceManager.testPermissions(PROJECT_ID, getPermission, deletePermission)) - .andReturn(response); expect(resourceManager.testPermissions( PROJECT_ID, ImmutableList.of(getPermission, deletePermission))) .andReturn(response); replay(resourceManager); initializeProject(); - assertEquals(response, project.testPermissions(getPermission, deletePermission)); assertEquals( response, project.testPermissions(ImmutableList.of(getPermission, deletePermission))); } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java index 2525039e9c0d..7d52901aa372 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java @@ -381,12 +381,6 @@ public void testTestPermissions() { RESOURCE_MANAGER.create(PARTIAL_PROJECT); assertEquals(ImmutableList.of(true), RESOURCE_MANAGER.testPermissions(PARTIAL_PROJECT.projectId(), permissions)); - assertEquals( - ImmutableList.of(true, true), - RESOURCE_MANAGER.testPermissions( - PARTIAL_PROJECT.projectId(), - "resourcemanager.projects.delete", - "resourcemanager.projects.get")); } @Test From b7c6da4702a665b31e74ea6aef8b7dd7c89e0ce5 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 22 Mar 2016 17:13:45 +0100 Subject: [PATCH 44/44] Rename startPageToken to pageToken in Storage and BigQuery --- .../main/java/com/google/gcloud/bigquery/BigQuery.java | 10 +++++----- .../com/google/gcloud/bigquery/BigQueryImplTest.java | 10 +++++----- .../main/java/com/google/gcloud/storage/Storage.java | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index e06c8d86ee5f..14e324a43370 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -180,7 +180,7 @@ public static DatasetListOption pageSize(long pageSize) { /** * Returns an option to specify the page token from which to start listing datasets. */ - public static DatasetListOption startPageToken(String pageToken) { + public static DatasetListOption pageToken(String pageToken) { return new DatasetListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken); } @@ -256,7 +256,7 @@ public static TableListOption pageSize(long pageSize) { /** * Returns an option to specify the page token from which to start listing tables. */ - public static TableListOption startPageToken(String pageToken) { + public static TableListOption pageToken(String pageToken) { return new TableListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken); } } @@ -305,7 +305,7 @@ public static TableDataListOption pageSize(long pageSize) { /** * Returns an option to specify the page token from which to start listing table data. */ - public static TableDataListOption startPageToken(String pageToken) { + public static TableDataListOption pageToken(String pageToken) { return new TableDataListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken); } @@ -362,7 +362,7 @@ public static JobListOption pageSize(long pageSize) { /** * Returns an option to specify the page token from which to start listing jobs. */ - public static JobListOption startPageToken(String pageToken) { + public static JobListOption pageToken(String pageToken) { return new JobListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken); } @@ -428,7 +428,7 @@ public static QueryResultsOption pageSize(long pageSize) { /** * Returns an option to specify the page token from which to start getting query results. */ - public static QueryResultsOption startPageToken(String pageToken) { + public static QueryResultsOption pageToken(String pageToken) { return new QueryResultsOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken); } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java index b398f238386a..a6f512800024 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java @@ -169,7 +169,7 @@ public class BigQueryImplTest { private static final BigQuery.DatasetListOption DATASET_LIST_ALL = BigQuery.DatasetListOption.all(); private static final BigQuery.DatasetListOption DATASET_LIST_PAGE_TOKEN = - BigQuery.DatasetListOption.startPageToken("cursor"); + BigQuery.DatasetListOption.pageToken("cursor"); private static final BigQuery.DatasetListOption DATASET_LIST_PAGE_SIZE = BigQuery.DatasetListOption.pageSize(42L); private static final Map DATASET_LIST_OPTIONS = ImmutableMap.of( @@ -191,7 +191,7 @@ public class BigQueryImplTest { private static final BigQuery.TableListOption TABLE_LIST_PAGE_SIZE = BigQuery.TableListOption.pageSize(42L); private static final BigQuery.TableListOption TABLE_LIST_PAGE_TOKEN = - BigQuery.TableListOption.startPageToken("cursor"); + BigQuery.TableListOption.pageToken("cursor"); private static final Map TABLE_LIST_OPTIONS = ImmutableMap.of( BigQueryRpc.Option.MAX_RESULTS, 42L, BigQueryRpc.Option.PAGE_TOKEN, "cursor"); @@ -200,7 +200,7 @@ public class BigQueryImplTest { private static final BigQuery.TableDataListOption TABLE_DATA_LIST_PAGE_SIZE = BigQuery.TableDataListOption.pageSize(42L); private static final BigQuery.TableDataListOption TABLE_DATA_LIST_PAGE_TOKEN = - BigQuery.TableDataListOption.startPageToken("cursor"); + BigQuery.TableDataListOption.pageToken("cursor"); private static final BigQuery.TableDataListOption TABLE_DATA_LIST_START_INDEX = BigQuery.TableDataListOption.startIndex(0L); private static final Map TABLE_DATA_LIST_OPTIONS = ImmutableMap.of( @@ -220,7 +220,7 @@ public class BigQueryImplTest { private static final BigQuery.JobListOption JOB_LIST_STATE_FILTER = BigQuery.JobListOption.stateFilter(JobStatus.State.DONE, JobStatus.State.PENDING); private static final BigQuery.JobListOption JOB_LIST_PAGE_TOKEN = - BigQuery.JobListOption.startPageToken("cursor"); + BigQuery.JobListOption.pageToken("cursor"); private static final BigQuery.JobListOption JOB_LIST_PAGE_SIZE = BigQuery.JobListOption.pageSize(42L); private static final Map JOB_LIST_OPTIONS = ImmutableMap.of( @@ -235,7 +235,7 @@ public class BigQueryImplTest { private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_INDEX = BigQuery.QueryResultsOption.startIndex(1024L); private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_PAGE_TOKEN = - BigQuery.QueryResultsOption.startPageToken("cursor"); + BigQuery.QueryResultsOption.pageToken("cursor"); private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_PAGE_SIZE = BigQuery.QueryResultsOption.pageSize(0L); private static final Map QUERY_RESULTS_OPTIONS = ImmutableMap.of( diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java index b4fbe45244b0..78f421e94e52 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java @@ -634,7 +634,7 @@ public static BucketListOption pageSize(long pageSize) { /** * Returns an option to specify the page token from which to start listing buckets. */ - public static BucketListOption startPageToken(String pageToken) { + public static BucketListOption pageToken(String pageToken) { return new BucketListOption(StorageRpc.Option.PAGE_TOKEN, pageToken); } @@ -680,7 +680,7 @@ public static BlobListOption pageSize(long pageSize) { /** * Returns an option to specify the page token from which to start listing blobs. */ - public static BlobListOption startPageToken(String pageToken) { + public static BlobListOption pageToken(String pageToken) { return new BlobListOption(StorageRpc.Option.PAGE_TOKEN, pageToken); }