|
27 | 27 | import static org.mockito.Mockito.mock; |
28 | 28 |
|
29 | 29 | import com.google.api.core.ApiClock; |
| 30 | +import com.google.api.services.storage.model.StorageObject; |
30 | 31 | import com.google.cloud.Identity; |
31 | 32 | import com.google.cloud.Policy; |
32 | 33 | import com.google.cloud.ReadChannel; |
|
38 | 39 | import com.google.common.collect.ImmutableList; |
39 | 40 | import com.google.common.collect.ImmutableMap; |
40 | 41 | import com.google.common.io.BaseEncoding; |
| 42 | +import java.io.ByteArrayInputStream; |
41 | 43 | import java.io.IOException; |
42 | 44 | import java.nio.ByteBuffer; |
43 | 45 | import java.security.Key; |
@@ -105,6 +107,11 @@ public class StorageImplMockitoTest { |
105 | 107 | private static final BlobInfo BLOB_INFO2 = BlobInfo.newBuilder(BUCKET_NAME1, BLOB_NAME2).build(); |
106 | 108 | private static final BlobInfo BLOB_INFO3 = BlobInfo.newBuilder(BUCKET_NAME1, BLOB_NAME3).build(); |
107 | 109 |
|
| 110 | + private static final BlobInfo BLOB_INFO_WITH_HASHES = |
| 111 | + BLOB_INFO1.toBuilder().setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); |
| 112 | + private static final BlobInfo BLOB_INFO_WITHOUT_HASHES = |
| 113 | + BLOB_INFO1.toBuilder().setMd5(null).setCrc32c(null).build(); |
| 114 | + |
108 | 115 | // Empty StorageRpc options |
109 | 116 | private static final Map<StorageRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of(); |
110 | 117 |
|
@@ -644,6 +651,316 @@ public void testGetBlobFailure() { |
644 | 651 | } |
645 | 652 | } |
646 | 653 |
|
| 654 | + private void verifyCreateBlobCapturedStream(ArgumentCaptor<ByteArrayInputStream> capturedStream) |
| 655 | + throws IOException { |
| 656 | + ByteArrayInputStream byteStream = capturedStream.getValue(); |
| 657 | + byte[] streamBytes = new byte[BLOB_CONTENT.length]; |
| 658 | + assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes)); |
| 659 | + assertArrayEquals(BLOB_CONTENT, streamBytes); |
| 660 | + assertEquals(-1, byteStream.read(streamBytes)); |
| 661 | + } |
| 662 | + |
| 663 | + @Test |
| 664 | + public void testCreateBlob() throws IOException { |
| 665 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 666 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 667 | + doReturn(BLOB_INFO1.toPb()) |
| 668 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 669 | + .when(storageRpcMock) |
| 670 | + .create( |
| 671 | + Mockito.eq(BLOB_INFO_WITH_HASHES.toPb()), |
| 672 | + capturedStream.capture(), |
| 673 | + Mockito.eq(EMPTY_RPC_OPTIONS)); |
| 674 | + initializeService(); |
| 675 | + |
| 676 | + Blob blob = storage.create(BLOB_INFO1, BLOB_CONTENT); |
| 677 | + |
| 678 | + assertEquals(expectedBlob1, blob); |
| 679 | + verifyCreateBlobCapturedStream(capturedStream); |
| 680 | + } |
| 681 | + |
| 682 | + @Test |
| 683 | + public void testCreateBlobWithSubArrayFromByteArray() throws IOException { |
| 684 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 685 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 686 | + doReturn(BLOB_INFO1.toPb()) |
| 687 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 688 | + .when(storageRpcMock) |
| 689 | + .create( |
| 690 | + Mockito.eq( |
| 691 | + BLOB_INFO1 |
| 692 | + .toBuilder() |
| 693 | + .setMd5(SUB_CONTENT_MD5) |
| 694 | + .setCrc32c(SUB_CONTENT_CRC32C) |
| 695 | + .build() |
| 696 | + .toPb()), |
| 697 | + capturedStream.capture(), |
| 698 | + Mockito.eq(EMPTY_RPC_OPTIONS)); |
| 699 | + initializeService(); |
| 700 | + |
| 701 | + Blob blob = storage.create(BLOB_INFO1, BLOB_CONTENT, 1, 2); |
| 702 | + |
| 703 | + assertEquals(expectedBlob1, blob); |
| 704 | + ByteArrayInputStream byteStream = capturedStream.getValue(); |
| 705 | + byte[] streamBytes = new byte[BLOB_SUB_CONTENT.length]; |
| 706 | + assertEquals(BLOB_SUB_CONTENT.length, byteStream.read(streamBytes)); |
| 707 | + assertArrayEquals(BLOB_SUB_CONTENT, streamBytes); |
| 708 | + assertEquals(-1, byteStream.read(streamBytes)); |
| 709 | + } |
| 710 | + |
| 711 | + @Test |
| 712 | + public void testCreateBlobRetry() throws IOException { |
| 713 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 714 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 715 | + |
| 716 | + StorageObject storageObject = BLOB_INFO_WITH_HASHES.toPb(); |
| 717 | + |
| 718 | + doThrow(new StorageException(500, "internalError")) |
| 719 | + .doReturn(BLOB_INFO1.toPb()) |
| 720 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 721 | + .when(storageRpcMock) |
| 722 | + .create(Mockito.eq(storageObject), capturedStream.capture(), Mockito.eq(EMPTY_RPC_OPTIONS)); |
| 723 | + |
| 724 | + storage = |
| 725 | + options |
| 726 | + .toBuilder() |
| 727 | + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) |
| 728 | + .build() |
| 729 | + .getService(); |
| 730 | + initializeServiceDependentObjects(); |
| 731 | + |
| 732 | + Blob blob = storage.create(BLOB_INFO1, BLOB_CONTENT); |
| 733 | + |
| 734 | + assertEquals(expectedBlob1, blob); |
| 735 | + |
| 736 | + byte[] streamBytes = new byte[BLOB_CONTENT.length]; |
| 737 | + for (ByteArrayInputStream byteStream : capturedStream.getAllValues()) { |
| 738 | + assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes)); |
| 739 | + assertArrayEquals(BLOB_CONTENT, streamBytes); |
| 740 | + assertEquals(-1, byteStream.read(streamBytes)); |
| 741 | + } |
| 742 | + } |
| 743 | + |
| 744 | + @Test |
| 745 | + public void testCreateEmptyBlob() throws IOException { |
| 746 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 747 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 748 | + |
| 749 | + doReturn(BLOB_INFO1.toPb()) |
| 750 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 751 | + .when(storageRpcMock) |
| 752 | + .create( |
| 753 | + Mockito.eq( |
| 754 | + BLOB_INFO1 |
| 755 | + .toBuilder() |
| 756 | + .setMd5("1B2M2Y8AsgTpgAmY7PhCfg==") |
| 757 | + .setCrc32c("AAAAAA==") |
| 758 | + .build() |
| 759 | + .toPb()), |
| 760 | + capturedStream.capture(), |
| 761 | + Mockito.eq(EMPTY_RPC_OPTIONS)); |
| 762 | + initializeService(); |
| 763 | + |
| 764 | + Blob blob = storage.create(BLOB_INFO1); |
| 765 | + assertEquals(expectedBlob1, blob); |
| 766 | + ByteArrayInputStream byteStream = capturedStream.getValue(); |
| 767 | + byte[] streamBytes = new byte[BLOB_CONTENT.length]; |
| 768 | + assertEquals(-1, byteStream.read(streamBytes)); |
| 769 | + } |
| 770 | + |
| 771 | + @Test |
| 772 | + public void testCreateBlobWithOptions() throws IOException { |
| 773 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 774 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 775 | + |
| 776 | + doReturn(BLOB_INFO1.toPb()) |
| 777 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 778 | + .when(storageRpcMock) |
| 779 | + .create( |
| 780 | + Mockito.eq(BLOB_INFO_WITH_HASHES.toPb()), |
| 781 | + capturedStream.capture(), |
| 782 | + Mockito.eq(BLOB_TARGET_OPTIONS_CREATE)); |
| 783 | + initializeService(); |
| 784 | + |
| 785 | + Blob blob = |
| 786 | + storage.create( |
| 787 | + BLOB_INFO1, |
| 788 | + BLOB_CONTENT, |
| 789 | + BLOB_TARGET_METAGENERATION, |
| 790 | + BLOB_TARGET_NOT_EXIST, |
| 791 | + BLOB_TARGET_PREDEFINED_ACL); |
| 792 | + assertEquals(expectedBlob1, blob); |
| 793 | + verifyCreateBlobCapturedStream(capturedStream); |
| 794 | + } |
| 795 | + |
| 796 | + @Test |
| 797 | + public void testCreateBlobWithDisabledGzipContent() throws IOException { |
| 798 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 799 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 800 | + |
| 801 | + doReturn(BLOB_INFO1.toPb()) |
| 802 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 803 | + .when(storageRpcMock) |
| 804 | + .create( |
| 805 | + Mockito.eq(BLOB_INFO_WITH_HASHES.toPb()), |
| 806 | + capturedStream.capture(), |
| 807 | + Mockito.eq(BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT)); |
| 808 | + initializeService(); |
| 809 | + |
| 810 | + Blob blob = storage.create(BLOB_INFO1, BLOB_CONTENT, BLOB_TARGET_DISABLE_GZIP_CONTENT); |
| 811 | + assertEquals(expectedBlob1, blob); |
| 812 | + verifyCreateBlobCapturedStream(capturedStream); |
| 813 | + } |
| 814 | + |
| 815 | + @Test |
| 816 | + public void testCreateBlobWithEncryptionKey() throws IOException { |
| 817 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 818 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 819 | + |
| 820 | + doReturn(BLOB_INFO1.toPb()) |
| 821 | + .doReturn(BLOB_INFO1.toPb()) |
| 822 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 823 | + .when(storageRpcMock) |
| 824 | + .create( |
| 825 | + Mockito.eq(BLOB_INFO_WITH_HASHES.toPb()), |
| 826 | + capturedStream.capture(), |
| 827 | + Mockito.eq(ENCRYPTION_KEY_OPTIONS)); |
| 828 | + initializeService(); |
| 829 | + |
| 830 | + Blob blob = |
| 831 | + storage.create(BLOB_INFO1, BLOB_CONTENT, Storage.BlobTargetOption.encryptionKey(KEY)); |
| 832 | + assertEquals(expectedBlob1, blob); |
| 833 | + verifyCreateBlobCapturedStream(capturedStream); |
| 834 | + blob = |
| 835 | + storage.create( |
| 836 | + BLOB_INFO1, BLOB_CONTENT, Storage.BlobTargetOption.encryptionKey(BASE64_KEY)); |
| 837 | + assertEquals(expectedBlob1, blob); |
| 838 | + verifyCreateBlobCapturedStream(capturedStream); |
| 839 | + } |
| 840 | + |
| 841 | + @Test |
| 842 | + public void testCreateBlobWithKmsKeyName() throws IOException { |
| 843 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 844 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 845 | + |
| 846 | + doReturn(BLOB_INFO1.toPb()) |
| 847 | + .doReturn(BLOB_INFO1.toPb()) |
| 848 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 849 | + .when(storageRpcMock) |
| 850 | + .create( |
| 851 | + Mockito.eq(BLOB_INFO_WITH_HASHES.toPb()), |
| 852 | + capturedStream.capture(), |
| 853 | + Mockito.eq(KMS_KEY_NAME_OPTIONS)); |
| 854 | + initializeService(); |
| 855 | + |
| 856 | + Blob blob = |
| 857 | + storage.create(BLOB_INFO1, BLOB_CONTENT, Storage.BlobTargetOption.kmsKeyName(KMS_KEY_NAME)); |
| 858 | + assertEquals(expectedBlob1, blob); |
| 859 | + verifyCreateBlobCapturedStream(capturedStream); |
| 860 | + blob = |
| 861 | + storage.create(BLOB_INFO1, BLOB_CONTENT, Storage.BlobTargetOption.kmsKeyName(KMS_KEY_NAME)); |
| 862 | + assertEquals(expectedBlob1, blob); |
| 863 | + verifyCreateBlobCapturedStream(capturedStream); |
| 864 | + } |
| 865 | + |
| 866 | + @Test |
| 867 | + @SuppressWarnings({"unchecked", "deprecation"}) |
| 868 | + public void testCreateBlobFromStream() throws IOException { |
| 869 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 870 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 871 | + |
| 872 | + ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); |
| 873 | + |
| 874 | + doReturn(BLOB_INFO1.toPb()) |
| 875 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 876 | + .when(storageRpcMock) |
| 877 | + .create( |
| 878 | + Mockito.eq(BLOB_INFO_WITHOUT_HASHES.toPb()), |
| 879 | + capturedStream.capture(), |
| 880 | + Mockito.eq(EMPTY_RPC_OPTIONS)); |
| 881 | + initializeService(); |
| 882 | + |
| 883 | + Blob blob = storage.create(BLOB_INFO_WITH_HASHES, fileStream); |
| 884 | + |
| 885 | + assertEquals(expectedBlob1, blob); |
| 886 | + verifyCreateBlobCapturedStream(capturedStream); |
| 887 | + } |
| 888 | + |
| 889 | + @Test |
| 890 | + @SuppressWarnings({"unchecked", "deprecation"}) |
| 891 | + public void testCreateBlobFromStreamDisableGzipContent() throws IOException { |
| 892 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 893 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 894 | + |
| 895 | + ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); |
| 896 | + doReturn(BLOB_INFO1.toPb()) |
| 897 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 898 | + .when(storageRpcMock) |
| 899 | + .create( |
| 900 | + Mockito.eq(BLOB_INFO_WITHOUT_HASHES.toPb()), |
| 901 | + capturedStream.capture(), |
| 902 | + Mockito.eq(BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT)); |
| 903 | + initializeService(); |
| 904 | + |
| 905 | + Blob blob = |
| 906 | + storage.create( |
| 907 | + BLOB_INFO_WITH_HASHES, fileStream, Storage.BlobWriteOption.disableGzipContent()); |
| 908 | + |
| 909 | + assertEquals(expectedBlob1, blob); |
| 910 | + verifyCreateBlobCapturedStream(capturedStream); |
| 911 | + } |
| 912 | + |
| 913 | + @Test |
| 914 | + @SuppressWarnings({"unchecked", "deprecation"}) |
| 915 | + public void testCreateBlobFromStreamWithEncryptionKey() throws IOException { |
| 916 | + ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); |
| 917 | + |
| 918 | + doReturn(BLOB_INFO1.toPb()) |
| 919 | + .doReturn(BLOB_INFO1.toPb()) |
| 920 | + .doThrow(UNEXPECTED_CALL_EXCEPTION) |
| 921 | + .when(storageRpcMock) |
| 922 | + .create(BLOB_INFO_WITHOUT_HASHES.toPb(), fileStream, ENCRYPTION_KEY_OPTIONS); |
| 923 | + initializeService(); |
| 924 | + Blob blob = |
| 925 | + storage.create( |
| 926 | + BLOB_INFO_WITH_HASHES, fileStream, Storage.BlobWriteOption.encryptionKey(BASE64_KEY)); |
| 927 | + assertEquals(expectedBlob1, blob); |
| 928 | + blob = |
| 929 | + storage.create( |
| 930 | + BLOB_INFO_WITH_HASHES, fileStream, Storage.BlobWriteOption.encryptionKey(BASE64_KEY)); |
| 931 | + assertEquals(expectedBlob1, blob); |
| 932 | + } |
| 933 | + |
| 934 | + @Test |
| 935 | + @SuppressWarnings({"unchecked", "deprecation"}) |
| 936 | + public void testCreateBlobFromStreamRetryableException() throws IOException { |
| 937 | + ArgumentCaptor<ByteArrayInputStream> capturedStream = |
| 938 | + ArgumentCaptor.forClass(ByteArrayInputStream.class); |
| 939 | + |
| 940 | + ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); |
| 941 | + |
| 942 | + Exception internalErrorException = new StorageException(500, "internalError"); |
| 943 | + doThrow(internalErrorException) |
| 944 | + .when(storageRpcMock) |
| 945 | + .create(BLOB_INFO_WITHOUT_HASHES.toPb(), fileStream, EMPTY_RPC_OPTIONS); |
| 946 | + |
| 947 | + storage = |
| 948 | + options |
| 949 | + .toBuilder() |
| 950 | + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) |
| 951 | + .build() |
| 952 | + .getService(); |
| 953 | + |
| 954 | + // Even though this exception is retryable, storage.create(BlobInfo, InputStream) |
| 955 | + // shouldn't retry. |
| 956 | + try { |
| 957 | + storage.create(BLOB_INFO_WITH_HASHES, fileStream); |
| 958 | + fail(); |
| 959 | + } catch (StorageException ex) { |
| 960 | + assertSame(internalErrorException, ex); |
| 961 | + } |
| 962 | + } |
| 963 | + |
647 | 964 | private void verifyChannelRead(ReadChannel channel, byte[] bytes) throws IOException { |
648 | 965 | assertNotNull(channel); |
649 | 966 | assertTrue(channel.isOpen()); |
@@ -737,15 +1054,12 @@ public void testReaderFailure() throws IOException { |
737 | 1054 |
|
738 | 1055 | @Test |
739 | 1056 | public void testWriter() { |
740 | | - BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder(); |
741 | | - BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); |
742 | | - BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build(); |
743 | 1057 | doReturn("upload-id") |
744 | 1058 | .doThrow(UNEXPECTED_CALL_EXCEPTION) |
745 | 1059 | .when(storageRpcMock) |
746 | | - .open(infoWithoutHashes.toPb(), EMPTY_RPC_OPTIONS); |
| 1060 | + .open(BLOB_INFO_WITHOUT_HASHES.toPb(), EMPTY_RPC_OPTIONS); |
747 | 1061 | initializeService(); |
748 | | - WriteChannel channel = storage.writer(infoWithHashes); |
| 1062 | + WriteChannel channel = storage.writer(BLOB_INFO_WITH_HASHES); |
749 | 1063 | assertNotNull(channel); |
750 | 1064 | assertTrue(channel.isOpen()); |
751 | 1065 | } |
@@ -804,13 +1118,12 @@ public void testWriterWithKmsKeyName() { |
804 | 1118 |
|
805 | 1119 | @Test |
806 | 1120 | public void testWriterFailure() { |
807 | | - BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder(); |
808 | | - BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); |
809 | | - BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build(); |
810 | | - doThrow(STORAGE_FAILURE).when(storageRpcMock).open(infoWithoutHashes.toPb(), EMPTY_RPC_OPTIONS); |
| 1121 | + doThrow(STORAGE_FAILURE) |
| 1122 | + .when(storageRpcMock) |
| 1123 | + .open(BLOB_INFO_WITHOUT_HASHES.toPb(), EMPTY_RPC_OPTIONS); |
811 | 1124 | initializeService(); |
812 | 1125 | try { |
813 | | - storage.writer(infoWithHashes); |
| 1126 | + storage.writer(BLOB_INFO_WITH_HASHES); |
814 | 1127 | fail(); |
815 | 1128 | } catch (StorageException e) { |
816 | 1129 | assertSame(STORAGE_FAILURE, e.getCause()); |
|
0 commit comments