From b6ec240876c66262be3ea99782f8abaec4372c96 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 11 Feb 2021 17:22:02 -0800 Subject: [PATCH] fix: use projectId from CloudStorageConfig (#429) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) Fixes #352 ☕️ --- .../storage/contrib/nio/CloudStoragePath.java | 27 +++-- .../nio/CloudStorageIsDirectoryTest.java | 98 +++++++++++++++++++ 2 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageIsDirectoryTest.java diff --git a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java index 32083da8..69c7f4ae 100644 --- a/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java +++ b/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStoragePath.java @@ -103,15 +103,24 @@ boolean seemsLikeADirectoryAndUsePseudoDirectories(Storage storage) { if (!prefix.endsWith("/")) { prefix += "/"; } - Page list = - storage.list( - this.bucket(), - Storage.BlobListOption.prefix(prefix), - // we only look at the first result, so no need for a bigger page. - Storage.BlobListOption.pageSize(1), - fileSystem.provider().getProject() == null - ? null - : Storage.BlobListOption.userProject(fileSystem.provider().getProject())); + String userProject = fileSystem.config().userProject(); + Page list = null; + if (userProject != null) { + list = + storage.list( + this.bucket(), + Storage.BlobListOption.prefix(prefix), + // we only look at the first result, so no need for a bigger page. + Storage.BlobListOption.pageSize(1), + Storage.BlobListOption.userProject(userProject)); + } else { + list = + storage.list( + this.bucket(), + Storage.BlobListOption.prefix(prefix), + // we only look at the first result, so no need for a bigger page. + Storage.BlobListOption.pageSize(1)); + } for (Blob b : list.getValues()) { // if this blob starts with our prefix and then a slash, then prefix is indeed a folder! if (b.getBlobId() == null) { diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageIsDirectoryTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageIsDirectoryTest.java new file mode 100644 index 00000000..ebd201d8 --- /dev/null +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageIsDirectoryTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2021 Google LLC + * + * 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.cloud.storage.contrib.nio; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.testing.junit4.MultipleAttemptsRule; +import com.google.common.collect.Lists; +import java.nio.file.Files; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@code Files.isDirectory()}. */ +@RunWith(JUnit4.class) +public class CloudStorageIsDirectoryTest { + @Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); + + private StorageOptions mockOptions; + private Storage mockStorage; + + @Before + public void before() { + mockOptions = mock(StorageOptions.class); + mockStorage = mock(Storage.class); + when(mockOptions.getService()).thenReturn(mockStorage); + CloudStorageFileSystemProvider.setStorageOptions(mockOptions); + } + + @Test + public void testIsDirectoryNoUserProject() { + CloudStorageFileSystem fs = + CloudStorageFileSystem.forBucket("bucket", CloudStorageConfiguration.DEFAULT); + when(mockStorage.get(BlobId.of("bucket", "test", null))) + .thenThrow(new IllegalArgumentException()); + Page pages = mock(Page.class); + Blob blob = mock(Blob.class); + when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt")); + when(pages.getValues()).thenReturn(Lists.newArrayList(blob)); + when(mockStorage.list( + "bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1))) + .thenReturn(pages); + + Files.isDirectory(fs.getPath("test")); + verify(mockStorage, times(1)) + .list("bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1)); + } + + @Test + public void testIsDirectoryWithUserProject() { + CloudStorageFileSystem fs = + CloudStorageFileSystem.forBucket( + "bucket", CloudStorageConfiguration.builder().userProject("project-id").build()); + when(mockStorage.get(BlobId.of("bucket", "test", null))) + .thenThrow(new IllegalArgumentException()); + Page pages = mock(Page.class); + Blob blob = mock(Blob.class); + when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt")); + when(pages.getValues()).thenReturn(Lists.newArrayList(blob)); + when(mockStorage.list( + "bucket", + Storage.BlobListOption.prefix("test/"), + Storage.BlobListOption.pageSize(1), + Storage.BlobListOption.userProject("project-id"))) + .thenReturn(pages); + Files.isDirectory(fs.getPath("test")); + verify(mockStorage, times(1)) + .list( + "bucket", + Storage.BlobListOption.prefix("test/"), + Storage.BlobListOption.pageSize(1), + Storage.BlobListOption.userProject("project-id")); + } +}