Skip to content

Commit

Permalink
fix: use projectId from CloudStorageConfig (#429)
Browse files Browse the repository at this point in the history
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 ☕️
  • Loading branch information
frankyn committed Feb 12, 2021
1 parent 27bee2f commit b6ec240
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 9 deletions.
Expand Up @@ -103,15 +103,24 @@ boolean seemsLikeADirectoryAndUsePseudoDirectories(Storage storage) {
if (!prefix.endsWith("/")) {
prefix += "/";
}
Page<Blob> 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<Blob> 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) {
Expand Down
@@ -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<Blob> 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<Blob> 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"));
}
}

0 comments on commit b6ec240

Please sign in to comment.