Repository: googleapis/java-storage
Pull request #188 (2020-03-16 19:29:05)

fix: rely on google core for SSLException's
Fixes: #187 



codecov[bot]: # [Codecov](https://codecov.io/gh/googleapis/java-storage/pull/188?src=pr&el=h1) Report
> Merging [#188](https://codecov.io/gh/googleapis/java-storage/pull/188?src=pr&el=desc) into [master](https://codecov.io/gh/googleapis/java-storage/commit/37958253821fe9d32270d34d736a68a3a3915629?src=pr&el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/googleapis/java-storage/pull/188/graphs/tree.svg?width=650&token=ssm139YOdp&height=150&src=pr)](https://codecov.io/gh/googleapis/java-storage/pull/188?src=pr&el=tree)

```diff
@@             Coverage Diff              @@
##             master     #188      +/-   ##
============================================
- Coverage     63.62%   63.58%   -0.04%     
  Complexity      546      546              
============================================
  Files            30       30              
  Lines          4778     4776       -2     
  Branches        429      428       -1     
============================================
- Hits           3040     3037       -3     
- Misses         1577     1579       +2     
+ Partials        161      160       -1
```


| [Impacted Files](https://codecov.io/gh/googleapis/java-storage/pull/188?src=pr&el=tree) | Coverage Δ | Complexity Δ | |
|---|---|---|---|
| [...ava/com/google/cloud/storage/StorageException.java](https://codecov.io/gh/googleapis/java-storage/pull/188/diff?src=pr&el=tree#diff-Z29vZ2xlLWNsb3VkLXN0b3JhZ2Uvc3JjL21haW4vamF2YS9jb20vZ29vZ2xlL2Nsb3VkL3N0b3JhZ2UvU3RvcmFnZUV4Y2VwdGlvbi5qYXZh) | `100% <ø> (+7.69%)` | `9 <0> (ø)` | :arrow_down: |
| [...gle/cloud/storage/testing/RemoteStorageHelper.java](https://codecov.io/gh/googleapis/java-storage/pull/188/diff?src=pr&el=tree#diff-Z29vZ2xlLWNsb3VkLXN0b3JhZ2Uvc3JjL21haW4vamF2YS9jb20vZ29vZ2xlL2Nsb3VkL3N0b3JhZ2UvdGVzdGluZy9SZW1vdGVTdG9yYWdlSGVscGVyLmphdmE=) | `61.98% <0%> (-2.48%)` | `9% <0%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/googleapis/java-storage/pull/188?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/googleapis/java-storage/pull/188?src=pr&el=footer). Last update [3795825...ed3c416](https://codecov.io/gh/googleapis/java-storage/pull/188?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java
index 1f740194..f4b8d0c4 100644
--- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java
+++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java
@@ -35,7 +35,6 @@ import java.util.Set;
 public final class StorageException extends BaseHttpServiceException {
   private static final String INTERNAL_ERROR = "internalError";
   private static final String CONNECTION_CLOSED_PREMATURELY = "connectionClosedPrematurely";
-  private static final String CONNECTION_RESET = "connectionReset";
 
   // see: https://cloud.google.com/storage/docs/resumable-uploads-xml#practices
   private static final Set<Error> RETRYABLE_ERRORS =
@@ -47,8 +46,7 @@ public final class StorageException extends BaseHttpServiceException {
           new Error(429, null),
           new Error(408, null),
           new Error(null, INTERNAL_ERROR),
-          new Error(null, CONNECTION_CLOSED_PREMATURELY),
-          new Error(null, CONNECTION_RESET));
+          new Error(null, CONNECTION_CLOSED_PREMATURELY));
 
   private static final long serialVersionUID = -4168430271327813063L;
 
@@ -86,7 +84,7 @@ public final class StorageException extends BaseHttpServiceException {
   /**
    * Translate IOException to a StorageException representing the cause of the error. This method
    * defaults to idempotent always being {@code true}. Additionally, this method translates
-   * transient issues Connection Closed Prematurely and Connection Reset as retryable errors.
+   * transient issues Connection Closed Prematurely as a retryable error.
    *
    * @returns {@code StorageException}
    */
@@ -94,8 +92,6 @@ public final class StorageException extends BaseHttpServiceException {
     if (exception.getMessage().contains("Connection closed prematurely")) {
       return new StorageException(
           0, exception.getMessage(), CONNECTION_CLOSED_PREMATURELY, exception);
-    } else if (exception.getMessage().contains("Connection reset")) {
-      return new StorageException(0, exception.getMessage(), CONNECTION_RESET, exception);
     } else {
       // default
       return new StorageException(exception);
diff --git a/pom.xml b/pom.xml
index fe80a8a9..cd36c389 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,7 +63,7 @@
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <github.global.server>github</github.global.server>
     <site.installationModule>google-cloud-storage-parent</site.installationModule>
-    <google.core.version>1.93.2</google.core.version>
+    <google.core.version>1.93.3</google.core.version>
     <google.api-common.version>1.8.1</google.api-common.version>
     <junit.version>4.13</junit.version>
     <threeten.version>1.4.1</threeten.version>


Test patch:
diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java
index 299ac039..eb18e4a7 100644
--- a/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java
+++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java
@@ -75,7 +75,9 @@ public class RemoteStorageHelper {
           @Override
           public void run() {
             Page<Bucket> buckets =
-                storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX));
+                storage.list(
+                    Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX),
+                    Storage.BucketListOption.userProject(storage.getOptions().getProjectId()));
             for (Bucket bucket : buckets.iterateAll()) {
               if (bucket.getCreateTime() < olderThan) {
                 try {
@@ -88,10 +90,9 @@ public class RemoteStorageHelper {
                           .iterateAll()) {
                     if (blob.getEventBasedHold() == true || blob.getTemporaryHold() == true) {
                       storage.update(
-                          blob.toBuilder()
-                              .setTemporaryHold(false)
-                              .setEventBasedHold(false)
-                              .build());
+                          blob.toBuilder().setTemporaryHold(false).setEventBasedHold(false).build(),
+                          Storage.BlobTargetOption.userProject(
+                              storage.getOptions().getProjectId()));
                     }
                   }
                   forceDelete(storage, bucket.getName());
diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageExceptionTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageExceptionTest.java
index e065f401..0051cac4 100644
--- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageExceptionTest.java
+++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageExceptionTest.java
@@ -141,11 +141,23 @@ public class StorageExceptionTest {
   public void testTranslateConnectionReset() {
     StorageException exception =
         StorageException.translate(
-            new IOException(
+            new SSLException(
                 "Connection has been shutdown: "
                     + new SSLException(new SocketException("Connection reset"))));
     assertEquals(0, exception.getCode());
-    assertEquals("connectionReset", exception.getReason());
+    assertTrue(exception.isRetryable());
+  }
+
+  @Test
+  public void testTranslateConnectionShutdown() {
+    StorageException exception =
+        StorageException.translate(
+            new SSLException(
+                "Connection has been shutdown: "
+                    + new SSLException(new SocketException("Socket closed"))));
+    String test = exception.getMessage();
+
+    assertEquals(0, exception.getCode());
     assertTrue(exception.isRetryable());
   }
 
diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java
index 31dc630f..dd47e744 100644
--- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java
+++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java
@@ -126,6 +126,7 @@ import javax.crypto.spec.SecretKeySpec;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 import org.junit.AfterClass;
+import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -192,6 +193,23 @@ public class ITStorageTest {
     prepareKmsKeys();
   }
 
+  @Before
+  public void beforeEach() {
+    Bucket remoteBucket =
+        storage.get(
+            BUCKET,
+            Storage.BucketGetOption.fields(BucketField.ID, BucketField.BILLING),
+            Storage.BucketGetOption.userProject(storage.getOptions().getProjectId()));
+    // Disable requester pays in case a test fails to clean up.
+    if (remoteBucket.requesterPays() != null && remoteBucket.requesterPays() == true) {
+      remoteBucket
+          .toBuilder()
+          .setRequesterPays(false)
+          .build()
+          .update(Storage.BucketTargetOption.userProject(storage.getOptions().getProjectId()));
+    }
+  }
+
   @AfterClass
   public static void afterClass() throws ExecutionException, InterruptedException {
     if (storage != null) {
@@ -867,8 +885,9 @@ public class ITStorageTest {
     assertNotNull(storage.create(blob1));
 
     // Test listing a Requester Pays bucket.
-    Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
-    assertNull(remoteBucket.requesterPays());
+    Bucket remoteBucket =
+        storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID, BucketField.BILLING));
+    assertFalse(remoteBucket.requesterPays());
     remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
     Bucket updatedBucket = storage.update(remoteBucket);
     assertTrue(updatedBucket.requesterPays());
@@ -2138,7 +2157,8 @@ public class ITStorageTest {
 
   private void testBucketAclRequesterPays(boolean requesterPays) {
     if (requesterPays) {
-      Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
+      Bucket remoteBucket =
+          storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID, BucketField.BILLING));
       assertNull(remoteBucket.requesterPays());
       remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
       Bucket updatedBucket = storage.update(remoteBucket);
@@ -2164,6 +2184,18 @@ public class ITStorageTest {
     assertTrue(acls.contains(updatedAcl));
     assertTrue(storage.deleteAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
     assertNull(storage.getAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
+    if (requesterPays) {
+      Bucket remoteBucket =
+          storage.get(
+              BUCKET,
+              Storage.BucketGetOption.fields(BucketField.ID, BucketField.BILLING),
+              Storage.BucketGetOption.userProject(projectId));
+      assertTrue(remoteBucket.requesterPays());
+      remoteBucket = remoteBucket.toBuilder().setRequesterPays(false).build();
+      Bucket updatedBucket =
+          storage.update(remoteBucket, Storage.BucketTargetOption.userProject(projectId));
+      assertFalse(updatedBucket.requesterPays());
+    }
   }
 
   @Test
@@ -2347,8 +2379,9 @@ public class ITStorageTest {
 
   @Test
   public void testBucketPolicyV1RequesterPays() {
-    Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
-    assertNull(remoteBucket.requesterPays());
+    Bucket remoteBucket =
+        storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID, BucketField.BILLING));
+    assertFalse(remoteBucket.requesterPays());
     remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
     Bucket updatedBucket = storage.update(remoteBucket);
     assertTrue(updatedBucket.requesterPays());
@@ -2407,6 +2440,9 @@ public class ITStorageTest {
             BUCKET,
             ImmutableList.of("storage.buckets.getIamPolicy", "storage.buckets.setIamPolicy"),
             bucketOptions));
+    remoteBucket = remoteBucket.toBuilder().setRequesterPays(false).build();
+    updatedBucket = storage.update(remoteBucket, Storage.BucketTargetOption.userProject(projectId));
+    assertFalse(updatedBucket.requesterPays());
   }
 
   @Test
@@ -2624,7 +2660,8 @@ public class ITStorageTest {
 
   @Test
   public void testUpdateBucketLabel() {
-    Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
+    Bucket remoteBucket =
+        storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID, BucketField.BILLING));
     assertNull(remoteBucket.getLabels());
     remoteBucket = remoteBucket.toBuilder().setLabels(BUCKET_LABELS).build();
     Bucket updatedBucket = storage.update(remoteBucket);
@@ -2635,8 +2672,9 @@ public class ITStorageTest {
 
   @Test
   public void testUpdateBucketRequesterPays() {
-    Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
-    assertNull(remoteBucket.requesterPays());
+    Bucket remoteBucket =
+        storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID, BucketField.BILLING));
+    assertFalse(remoteBucket.requesterPays());
     remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
     Bucket updatedBucket = storage.update(remoteBucket);
     assertTrue(updatedBucket.requesterPays());
@@ -2646,8 +2684,12 @@ public class ITStorageTest {
     String blobName = "test-create-empty-blob-requester-pays";
     Blob remoteBlob = updatedBucket.create(blobName, BLOB_BYTE_CONTENT, option);
     assertNotNull(remoteBlob);
-    byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
+    byte[] readBytes =
+        storage.readAllBytes(BUCKET, blobName, Storage.BlobSourceOption.userProject(projectId));
     assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
+    remoteBucket = remoteBucket.toBuilder().setRequesterPays(false).build();
+    updatedBucket = storage.update(remoteBucket, Storage.BucketTargetOption.userProject(projectId));
+    assertFalse(updatedBucket.requesterPays());
   }
 
   @Test
@@ -2786,6 +2828,12 @@ public class ITStorageTest {
       assertTrue(remoteBucket.retentionPolicyIsLocked());
       assertNotNull(remoteBucket.getRetentionEffectiveTime());
     } finally {
+      if (requesterPays) {
+        bucketInfo = bucketInfo.toBuilder().setRequesterPays(false).build();
+        Bucket updateBucket =
+            storage.update(bucketInfo, Storage.BucketTargetOption.userProject(projectId));
+        assertFalse(updateBucket.requesterPays());
+      }
       RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
     }
   }