From 9f30db3ccca16121fc5260334d603f8d272af2d9 Mon Sep 17 00:00:00 2001 From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com> Date: Fri, 4 Mar 2022 12:07:04 -0800 Subject: [PATCH] fix: Requester pays to check reason and fallback to error message validation (#841) * fix: Requester pays to check reason and fallback to error message validation --- .../nio/CloudStorageFileSystemProvider.java | 5 +++- .../storage/contrib/nio/it/ITGcsNio.java | 27 +++++++------------ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java index f6a344be..1ad0e08b 100644 --- a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java +++ b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java @@ -967,7 +967,10 @@ public boolean requesterPays(String bucketName) { Boolean isRP = storage.get(bucketName).requesterPays(); return isRP != null && isRP.booleanValue(); } catch (StorageException ex) { - if (ex.getCode() == 400 && ex.getMessage().contains("Bucket is requester pays")) { + if (ex.getReason().equals("userProjectMissing")) { + return true; + // fallback to checking the error code and error message. + } else if (ex.getCode() == 400 && ex.getMessage().contains("requester pays")) { return true; } throw ex; diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java index 871c3f94..55a341d4 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java @@ -75,7 +75,6 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -176,7 +175,6 @@ private static void fillRequesterPaysFile(Storage storage, String fname, int siz // Start of tests related to the "requester pays" feature @Test - @Ignore("TODO: https://github.com/googleapis/java-storage-nio/issues/824") public void testFileExistsRequesterPaysNoUserProject() throws IOException { CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, ""); Path path = testBucket.getPath(SML_FILE); @@ -190,7 +188,6 @@ public void testFileExistsRequesterPaysNoUserProject() throws IOException { } @Test - @Ignore("TODO: https://github.com/googleapis/java-storage-nio/issues/824") public void testFileExistsRequesterPays() throws IOException { CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, project); Path path = testBucket.getPath(SML_FILE); @@ -199,7 +196,6 @@ public void testFileExistsRequesterPays() throws IOException { } @Test - @Ignore("TODO: https://github.com/googleapis/java-storage-nio/issues/824") public void testFileExistsRequesterPaysWithAutodetect() throws IOException { CloudStorageFileSystem testBucket = getRequesterPaysBucket(true, project); Path path = testBucket.getPath(SML_FILE); @@ -208,7 +204,6 @@ public void testFileExistsRequesterPaysWithAutodetect() throws IOException { } @Test - @Ignore("TODO: https://github.com/googleapis/java-storage-nio/issues/824") public void testCantCreateWithoutUserProject() throws IOException { CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, ""); Path path = testBucket.getPath(TMP_FILE); @@ -230,7 +225,6 @@ public void testCanCreateWithUserProject() throws IOException { } @Test - @Ignore("TODO: https://github.com/googleapis/java-storage-nio/issues/824") public void testCantReadWithoutUserProject() throws IOException { CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, ""); Path path = testBucket.getPath(SML_FILE); @@ -252,7 +246,6 @@ public void testCanReadWithUserProject() throws IOException { } @Test - @Ignore("TODO: https://github.com/googleapis/java-storage-nio/issues/824") public void testCantCopyWithoutUserProject() throws IOException { CloudStorageFileSystem testRPBucket = getRequesterPaysBucket(false, ""); CloudStorageFileSystem testBucket = getTestBucket(); @@ -285,10 +278,7 @@ private void innerTestCantCopyWithoutUserProject( // normal StorageException. } catch (HttpResponseException hex) { Assert.assertEquals(description, hex.getStatusCode(), 400); - Assert.assertTrue( - description, - hex.getMessage() - .contains("Bucket is requester pays bucket but no user project provided")); + Assert.assertTrue(description, hex.getMessage().contains("requester pays")); } catch (StorageException ex) { assertIsRequesterPaysException(description, ex); } @@ -314,7 +304,6 @@ public void testCanCopyWithUserProject() throws IOException { } @Test - @Ignore("TODO: https://github.com/googleapis/java-storage-nio/issues/824") public void testAutodetectWhenRequesterPays() throws IOException { CloudStorageFileSystem testRPBucket = getRequesterPaysBucket(true, project); Assert.assertEquals( @@ -338,18 +327,20 @@ public void testAutodetectWhenNotRequesterPays() throws IOException { ""); } + @Test + public void testAutoDetectNoUserProject() throws IOException { + CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, ""); + Assert.assertTrue(testBucket.provider().requesterPays(testBucket.bucket())); + } + private void assertIsRequesterPaysException(String message, StorageException ex) { Assert.assertEquals(message, ex.getCode(), 400); - Assert.assertTrue( - message, - ex.getMessage().contains("Bucket is requester pays bucket but no user project provided")); + Assert.assertTrue(message, ex.getMessage().contains("requester pays")); } private void assertIsRequesterPaysException(String message, IOException ioex) { Assert.assertTrue(message, ioex.getMessage().startsWith("400")); - Assert.assertTrue( - message, - ioex.getMessage().contains("Bucket is requester pays bucket but no user project provided")); + Assert.assertTrue(message, ioex.getMessage().contains("requester pays")); } // End of tests related to the "requester pays" feature