|
16 | 16 |
|
17 | 17 | package com.google.cloud.storage.contrib.nio; |
18 | 18 |
|
| 19 | +import static com.google.cloud.storage.Acl.Role.OWNER; |
19 | 20 | import static com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket; |
20 | 21 | import static com.google.common.truth.Truth.assertThat; |
21 | 22 | import static java.nio.charset.StandardCharsets.UTF_8; |
|
26 | 27 | import static java.nio.file.StandardOpenOption.CREATE_NEW; |
27 | 28 | import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; |
28 | 29 | import static java.nio.file.StandardOpenOption.WRITE; |
| 30 | +import static org.junit.Assert.assertEquals; |
29 | 31 | import static org.junit.Assert.assertTrue; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.spy; |
| 34 | +import static org.mockito.Mockito.when; |
30 | 35 |
|
| 36 | +import com.google.cloud.storage.Acl; |
| 37 | +import com.google.cloud.storage.Acl.User; |
31 | 38 | import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper; |
32 | 39 | import com.google.cloud.testing.junit4.MultipleAttemptsRule; |
| 40 | +import com.google.common.base.Optional; |
33 | 41 | import com.google.common.collect.ImmutableList; |
34 | 42 | import com.google.common.testing.NullPointerTester; |
35 | 43 | import java.io.IOException; |
|
49 | 57 | import java.nio.file.OpenOption; |
50 | 58 | import java.nio.file.Path; |
51 | 59 | import java.nio.file.Paths; |
| 60 | +import java.nio.file.attribute.BasicFileAttributes; |
| 61 | +import java.nio.file.attribute.FileTime; |
52 | 62 | import java.util.ArrayList; |
53 | 63 | import java.util.HashMap; |
54 | 64 | import java.util.List; |
55 | 65 | import java.util.Map; |
| 66 | +import java.util.TreeMap; |
56 | 67 | import org.junit.After; |
57 | 68 | import org.junit.Assert; |
58 | 69 | import org.junit.Before; |
@@ -817,6 +828,96 @@ public void getUserAgentStartsWithCorrectToken() { |
817 | 828 | .startsWith("gcloud-java-nio/"); |
818 | 829 | } |
819 | 830 |
|
| 831 | + @Test |
| 832 | + public void testReadAttributes() throws IOException { |
| 833 | + CloudStorageFileSystem fileSystem = forBucket("dummy"); |
| 834 | + CloudStorageFileSystemProvider fileSystemProvider = spy(fileSystem.provider()); |
| 835 | + |
| 836 | + BasicFileAttributes attributesBasic = mock(BasicFileAttributes.class); |
| 837 | + // BasicFileAttributes |
| 838 | + when(attributesBasic.creationTime()).thenReturn(FileTime.fromMillis(1L)); |
| 839 | + when(attributesBasic.lastModifiedTime()).thenReturn(FileTime.fromMillis(2L)); |
| 840 | + when(attributesBasic.lastAccessTime()).thenReturn(FileTime.fromMillis(3L)); |
| 841 | + when(attributesBasic.isRegularFile()).thenReturn(true); |
| 842 | + when(attributesBasic.isDirectory()).thenReturn(true); |
| 843 | + when(attributesBasic.isSymbolicLink()).thenReturn(true); |
| 844 | + when(attributesBasic.isOther()).thenReturn(true); |
| 845 | + when(attributesBasic.size()).thenReturn(42L); |
| 846 | + |
| 847 | + CloudStorageFileAttributes attributesGcs = mock(CloudStorageFileAttributes.class); |
| 848 | + // BasicFileAttributes |
| 849 | + when(attributesGcs.creationTime()).thenReturn(FileTime.fromMillis(1L)); |
| 850 | + when(attributesGcs.lastModifiedTime()).thenReturn(FileTime.fromMillis(2L)); |
| 851 | + when(attributesGcs.lastAccessTime()).thenReturn(FileTime.fromMillis(3L)); |
| 852 | + when(attributesGcs.isRegularFile()).thenReturn(true); |
| 853 | + when(attributesGcs.isDirectory()).thenReturn(true); |
| 854 | + when(attributesGcs.isSymbolicLink()).thenReturn(true); |
| 855 | + when(attributesGcs.isOther()).thenReturn(true); |
| 856 | + when(attributesGcs.size()).thenReturn(42L); |
| 857 | + |
| 858 | + List<Acl> acls = ImmutableList.of(Acl.newBuilder(new User("Foo"), OWNER).build()); |
| 859 | + |
| 860 | + // CloudStorageFileAttributes |
| 861 | + when(attributesGcs.etag()).thenReturn(Optional.of("TheEtag")); |
| 862 | + when(attributesGcs.mimeType()).thenReturn(Optional.of("TheMimeType")); |
| 863 | + when(attributesGcs.acl()).thenReturn(Optional.of(acls)); |
| 864 | + when(attributesGcs.cacheControl()).thenReturn(Optional.of("TheCacheControl")); |
| 865 | + when(attributesGcs.contentEncoding()).thenReturn(Optional.of("TheContentEncoding")); |
| 866 | + when(attributesGcs.contentDisposition()).thenReturn(Optional.of("TheContentDisposition")); |
| 867 | + when(attributesGcs.userMetadata()).thenReturn(new TreeMap<>()); |
| 868 | + |
| 869 | + CloudStoragePath path1 = CloudStoragePath.getPath(fileSystem, "/"); |
| 870 | + when(fileSystemProvider.readAttributes(path1, BasicFileAttributes.class)) |
| 871 | + .thenReturn(attributesBasic); |
| 872 | + when(fileSystemProvider.readAttributes(path1, CloudStorageFileAttributes.class)) |
| 873 | + .thenReturn(attributesGcs); |
| 874 | + |
| 875 | + Map<String, Object> expectedBasic = new TreeMap<>(); |
| 876 | + // BasicFileAttributes |
| 877 | + expectedBasic.put("creationTime", FileTime.fromMillis(1L)); |
| 878 | + expectedBasic.put("lastModifiedTime", FileTime.fromMillis(2L)); |
| 879 | + expectedBasic.put("lastAccessTime", FileTime.fromMillis(3L)); |
| 880 | + expectedBasic.put("isRegularFile", true); |
| 881 | + expectedBasic.put("isDirectory", true); |
| 882 | + expectedBasic.put("isSymbolicLink", true); |
| 883 | + expectedBasic.put("isOther", true); |
| 884 | + expectedBasic.put("size", 42L); |
| 885 | + |
| 886 | + assertEquals(expectedBasic, fileSystemProvider.readAttributes(path1, "basic:*")); |
| 887 | + |
| 888 | + Map<String, Object> expectedGcs = new TreeMap<>(expectedBasic); |
| 889 | + // CloudStorageFileAttributes |
| 890 | + expectedGcs.put("etag", Optional.of("TheEtag")); |
| 891 | + expectedGcs.put("mimeType", Optional.of("TheMimeType")); |
| 892 | + expectedGcs.put("acl", Optional.of(acls)); |
| 893 | + expectedGcs.put("cacheControl", Optional.of("TheCacheControl")); |
| 894 | + expectedGcs.put("contentEncoding", Optional.of("TheContentEncoding")); |
| 895 | + expectedGcs.put("contentDisposition", Optional.of("TheContentDisposition")); |
| 896 | + expectedGcs.put("userMetadata", new TreeMap<>()); |
| 897 | + |
| 898 | + assertEquals(expectedGcs, fileSystemProvider.readAttributes(path1, "gcs:*")); |
| 899 | + |
| 900 | + Map<String, Object> expectedSpecific = new TreeMap<>(); |
| 901 | + expectedSpecific.put("lastModifiedTime", FileTime.fromMillis(2L)); |
| 902 | + expectedSpecific.put("isSymbolicLink", true); |
| 903 | + expectedSpecific.put("isOther", true); |
| 904 | + |
| 905 | + // Asking for attributes that should NOT be known because we ask for basic view ! |
| 906 | + assertEquals( |
| 907 | + expectedSpecific, |
| 908 | + fileSystemProvider.readAttributes( |
| 909 | + path1, "basic:lastModifiedTime,isSymbolicLink,isOther,etag,cacheControl")); |
| 910 | + |
| 911 | + // Add the attributes that are only known in gcs view |
| 912 | + expectedSpecific.put("etag", Optional.of("TheEtag")); |
| 913 | + expectedSpecific.put("cacheControl", Optional.of("TheCacheControl")); |
| 914 | + |
| 915 | + assertEquals( |
| 916 | + expectedSpecific, |
| 917 | + fileSystemProvider.readAttributes( |
| 918 | + path1, "gcs:lastModifiedTime,isSymbolicLink,isOther,etag,cacheControl")); |
| 919 | + } |
| 920 | + |
820 | 921 | private static CloudStorageConfiguration permitEmptyPathComponents(boolean value) { |
821 | 922 | return CloudStorageConfiguration.builder().permitEmptyPathComponents(value).build(); |
822 | 923 | } |
|
0 commit comments