From 776ee5483b35e9fabadce62431cf98018440aeb7 Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Wed, 2 Mar 2022 14:34:38 -0800 Subject: [PATCH 1/5] fix: Requester pays to check reason and fallback to error message validation --- .../storage/contrib/nio/CloudStorageFileSystemProvider.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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..30bb5281 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,11 @@ 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("Bucket is a requester pays bucket")) { return true; } throw ex; From 5149f012cfadc037e412fcf806c912fa8b1d0bce Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Thu, 3 Mar 2022 09:16:27 -0800 Subject: [PATCH 2/5] Addressing PR comments --- .../storage/contrib/nio/CloudStorageFileSystemProvider.java | 3 +-- 1 file changed, 1 insertion(+), 2 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 30bb5281..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 @@ -970,8 +970,7 @@ public boolean requesterPays(String bucketName) { if (ex.getReason().equals("userProjectMissing")) { return true; // fallback to checking the error code and error message. - } else if (ex.getCode() == 400 - && ex.getMessage().contains("Bucket is a requester pays bucket")) { + } else if (ex.getCode() == 400 && ex.getMessage().contains("requester pays")) { return true; } throw ex; From d4638ce2682c7d42384298c68af63e1e5c77d7bd Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Fri, 4 Mar 2022 11:19:02 -0800 Subject: [PATCH 3/5] Adding Tests back for requester pays --- .../storage/contrib/nio/it/ITGcsNio.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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..d438eefc 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 @@ -176,7 +176,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 +189,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 +197,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 +205,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 +226,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 +247,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(); @@ -288,7 +282,7 @@ private void innerTestCantCopyWithoutUserProject( Assert.assertTrue( description, hex.getMessage() - .contains("Bucket is requester pays bucket but no user project provided")); + .contains("requester pays")); } catch (StorageException ex) { assertIsRequesterPaysException(description, ex); } @@ -314,7 +308,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 +331,24 @@ public void testAutodetectWhenNotRequesterPays() throws IOException { ""); } + @Test + public void testAutoDetectNoUserProject() throws IOException { + CloudStorageFileSystem testBucket = getRequesterPaysBucket(true, ""); + 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")); + 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")); + ioex.getMessage().contains("requester pays")); } // End of tests related to the "requester pays" feature From ea0a6f4a099844691b7a390a6ea172aa72dbc832 Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Fri, 4 Mar 2022 11:51:55 -0800 Subject: [PATCH 4/5] Fix autoDetectNoUserProjectTest --- .../java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d438eefc..aa56b600 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 @@ -333,7 +333,7 @@ public void testAutodetectWhenNotRequesterPays() throws IOException { @Test public void testAutoDetectNoUserProject() throws IOException { - CloudStorageFileSystem testBucket = getRequesterPaysBucket(true, ""); + CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, ""); Assert.assertTrue(testBucket.provider().requesterPays(testBucket.bucket())); } From 8d596fa0658e9f7990dd18b206592a66340275c7 Mon Sep 17 00:00:00 2001 From: Sydney Munro Date: Fri, 4 Mar 2022 11:54:59 -0800 Subject: [PATCH 5/5] Linter changes --- .../cloud/storage/contrib/nio/it/ITGcsNio.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) 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 aa56b600..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; @@ -279,10 +278,7 @@ private void innerTestCantCopyWithoutUserProject( // normal StorageException. } catch (HttpResponseException hex) { Assert.assertEquals(description, hex.getStatusCode(), 400); - Assert.assertTrue( - description, - hex.getMessage() - .contains("requester pays")); + Assert.assertTrue(description, hex.getMessage().contains("requester pays")); } catch (StorageException ex) { assertIsRequesterPaysException(description, ex); } @@ -339,16 +335,12 @@ public void testAutoDetectNoUserProject() throws IOException { private void assertIsRequesterPaysException(String message, StorageException ex) { Assert.assertEquals(message, ex.getCode(), 400); - Assert.assertTrue( - message, - ex.getMessage().contains("requester pays")); + 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("requester pays")); + Assert.assertTrue(message, ioex.getMessage().contains("requester pays")); } // End of tests related to the "requester pays" feature