|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage.contrib.nio; |
| 18 | + |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.times; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import com.google.api.gax.paging.Page; |
| 25 | +import com.google.cloud.storage.Blob; |
| 26 | +import com.google.cloud.storage.BlobId; |
| 27 | +import com.google.cloud.storage.Storage; |
| 28 | +import com.google.cloud.storage.StorageOptions; |
| 29 | +import com.google.cloud.testing.junit4.MultipleAttemptsRule; |
| 30 | +import com.google.common.collect.Lists; |
| 31 | +import java.nio.file.Files; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Rule; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.junit.runners.JUnit4; |
| 37 | + |
| 38 | +/** Unit tests for {@code Files.isDirectory()}. */ |
| 39 | +@RunWith(JUnit4.class) |
| 40 | +public class CloudStorageIsDirectoryTest { |
| 41 | + @Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); |
| 42 | + |
| 43 | + private StorageOptions mockOptions; |
| 44 | + private Storage mockStorage; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void before() { |
| 48 | + mockOptions = mock(StorageOptions.class); |
| 49 | + mockStorage = mock(Storage.class); |
| 50 | + when(mockOptions.getService()).thenReturn(mockStorage); |
| 51 | + CloudStorageFileSystemProvider.setStorageOptions(mockOptions); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testIsDirectoryNoUserProject() { |
| 56 | + CloudStorageFileSystem fs = |
| 57 | + CloudStorageFileSystem.forBucket("bucket", CloudStorageConfiguration.DEFAULT); |
| 58 | + when(mockStorage.get(BlobId.of("bucket", "test", null))) |
| 59 | + .thenThrow(new IllegalArgumentException()); |
| 60 | + Page<Blob> pages = mock(Page.class); |
| 61 | + Blob blob = mock(Blob.class); |
| 62 | + when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt")); |
| 63 | + when(pages.getValues()).thenReturn(Lists.newArrayList(blob)); |
| 64 | + when(mockStorage.list( |
| 65 | + "bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1))) |
| 66 | + .thenReturn(pages); |
| 67 | + |
| 68 | + Files.isDirectory(fs.getPath("test")); |
| 69 | + verify(mockStorage, times(1)) |
| 70 | + .list("bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testIsDirectoryWithUserProject() { |
| 75 | + CloudStorageFileSystem fs = |
| 76 | + CloudStorageFileSystem.forBucket( |
| 77 | + "bucket", CloudStorageConfiguration.builder().userProject("project-id").build()); |
| 78 | + when(mockStorage.get(BlobId.of("bucket", "test", null))) |
| 79 | + .thenThrow(new IllegalArgumentException()); |
| 80 | + Page<Blob> pages = mock(Page.class); |
| 81 | + Blob blob = mock(Blob.class); |
| 82 | + when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt")); |
| 83 | + when(pages.getValues()).thenReturn(Lists.newArrayList(blob)); |
| 84 | + when(mockStorage.list( |
| 85 | + "bucket", |
| 86 | + Storage.BlobListOption.prefix("test/"), |
| 87 | + Storage.BlobListOption.pageSize(1), |
| 88 | + Storage.BlobListOption.userProject("project-id"))) |
| 89 | + .thenReturn(pages); |
| 90 | + Files.isDirectory(fs.getPath("test")); |
| 91 | + verify(mockStorage, times(1)) |
| 92 | + .list( |
| 93 | + "bucket", |
| 94 | + Storage.BlobListOption.prefix("test/"), |
| 95 | + Storage.BlobListOption.pageSize(1), |
| 96 | + Storage.BlobListOption.userProject("project-id")); |
| 97 | + } |
| 98 | +} |
0 commit comments