|
| 1 | +/* |
| 2 | + * Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 11 | + * OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | + * License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazonaws.services.s3; |
| 17 | + |
| 18 | +import android.content.Context; |
| 19 | +import android.support.test.InstrumentationRegistry; |
| 20 | + |
| 21 | +import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener; |
| 22 | +import com.amazonaws.mobileconnectors.s3.transferutility.TransferNetworkLossHandler; |
| 23 | +import com.amazonaws.mobileconnectors.s3.transferutility.TransferState; |
| 24 | +import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility; |
| 25 | +import com.amazonaws.services.s3.model.GetObjectTaggingRequest; |
| 26 | +import com.amazonaws.services.s3.model.ObjectMetadata; |
| 27 | +import com.amazonaws.services.s3.model.Tag; |
| 28 | + |
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | +import java.io.RandomAccessFile; |
| 35 | +import java.util.Date; |
| 36 | +import java.util.List; |
| 37 | +import java.util.concurrent.CountDownLatch; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +import static org.hamcrest.CoreMatchers.hasItems; |
| 41 | +import static org.hamcrest.CoreMatchers.is; |
| 42 | +import static org.junit.Assert.assertThat; |
| 43 | +import static org.junit.Assert.fail; |
| 44 | + |
| 45 | +public class TransferUtilityIntegrationTest extends S3IntegrationTestBase { |
| 46 | + |
| 47 | + /** The bucket created and used by these tests */ |
| 48 | + private static final String bucketName = "amazon-transfer-util-integ-test-" + new Date().getTime(); |
| 49 | + |
| 50 | + /** Instrumentation test context */ |
| 51 | + private static Context context = InstrumentationRegistry.getContext(); |
| 52 | + |
| 53 | + /** Countdown latch for testing */ |
| 54 | + private static CountDownLatch latch; |
| 55 | + |
| 56 | + /** The transfer utility used to upload to S3 */ |
| 57 | + private static TransferUtility util; |
| 58 | + |
| 59 | + /** The file containing the test data uploaded to S3 */ |
| 60 | + private static File file = null; |
| 61 | + |
| 62 | + /** The metadata for the object. */ |
| 63 | + private ObjectMetadata metadata; |
| 64 | + |
| 65 | + /** Simple transfer listener to confirm upload completion before bucket deletion */ |
| 66 | + private static final TransferListener listener = new TransferListener() { |
| 67 | + |
| 68 | + public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { |
| 69 | + } |
| 70 | + |
| 71 | + public void onStateChanged(int id, TransferState state) { |
| 72 | + if (state.equals(TransferState.COMPLETED)) { latch.countDown(); } |
| 73 | + } |
| 74 | + |
| 75 | + public void onError(int id, Exception ex) { |
| 76 | + fail(ex.getMessage()); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates and initializes all the test resources needed for these tests. |
| 82 | + */ |
| 83 | + @BeforeClass |
| 84 | + public static void setUpBeforeClass() throws Exception { |
| 85 | + setUp(); |
| 86 | + TransferNetworkLossHandler.getInstance(context); |
| 87 | + util = TransferUtility.builder() |
| 88 | + .context(context) |
| 89 | + .s3Client(s3) |
| 90 | + .build(); |
| 91 | + |
| 92 | + try { |
| 93 | + s3.createBucket(bucketName); |
| 94 | + waitForBucketCreation(bucketName); |
| 95 | + } catch (final Exception e) { |
| 96 | + System.out.println("Error in creating the bucket. " |
| 97 | + + "Please manually create the bucket " + bucketName); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @AfterClass |
| 102 | + public static void tearDown() { |
| 103 | + try { |
| 104 | + deleteBucketAndAllContents(bucketName); |
| 105 | + } catch (final Exception e) { |
| 106 | + System.out.println("Error in deleting the bucket. " |
| 107 | + + "Please manually delete the bucket " + bucketName); |
| 108 | + e.printStackTrace(); |
| 109 | + } |
| 110 | + |
| 111 | + if (file != null) { |
| 112 | + file.delete(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testSingleUploadTagging() throws Exception { |
| 118 | + // Object metadata to add |
| 119 | + metadata = new ObjectMetadata(); |
| 120 | + metadata.addUserMetadata("x-amz-tagging", "key1=value1&key2=value2"); |
| 121 | + metadata.addUserMetadata("key3", "value3"); // Should not get added to tags |
| 122 | + |
| 123 | + latch = new CountDownLatch(1); |
| 124 | + // Small (1KB) file upload |
| 125 | + file = getRandomTempFile("small", 1000L); |
| 126 | + util.upload(bucketName, file.getName(), file, metadata) |
| 127 | + .setTransferListener(listener); |
| 128 | + latch.await(300, TimeUnit.SECONDS); |
| 129 | + |
| 130 | + List<Tag> tags = s3.getObjectTagging(new GetObjectTaggingRequest( |
| 131 | + bucketName, |
| 132 | + file.getName() |
| 133 | + )).getTagSet(); |
| 134 | + assertThat(tags.size(), is(2)); |
| 135 | + assertThat(tags, hasItems( |
| 136 | + new Tag("key1", "value1"), |
| 137 | + new Tag("key2", "value2") |
| 138 | + )); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testMultiUploadTagging() throws Exception { |
| 143 | + // Object metadata to add |
| 144 | + metadata = new ObjectMetadata(); |
| 145 | + metadata.addUserMetadata("x-amz-tagging", "key1=value1&key2=value2"); |
| 146 | + metadata.addUserMetadata("key3", "value3"); // Should not get added to tags |
| 147 | + |
| 148 | + latch = new CountDownLatch(1); |
| 149 | + // Large (5MB) file upload |
| 150 | + long size = 5 * 1024 * 1024 + 1; |
| 151 | + file = getRandomSparseFile("large", size); |
| 152 | + util.upload(bucketName, file.getName(), file, metadata) |
| 153 | + .setTransferListener(listener); |
| 154 | + latch.await(300, TimeUnit.SECONDS); |
| 155 | + |
| 156 | + List<Tag> tags = s3.getObjectTagging(new GetObjectTaggingRequest( |
| 157 | + bucketName, |
| 158 | + file.getName() |
| 159 | + )).getTagSet(); |
| 160 | + assertThat(tags.size(), is(2)); |
| 161 | + assertThat(tags, hasItems( |
| 162 | + new Tag("key1", "value1"), |
| 163 | + new Tag("key2", "value2") |
| 164 | + )); |
| 165 | + } |
| 166 | + |
| 167 | + /* |
| 168 | + * Helper method to create a sparse file (Faster than writing a file with random bytes with getRandomTempFile) |
| 169 | + * Use for testing large uploads |
| 170 | + */ |
| 171 | + private File getRandomSparseFile(String filename, long contentLength) throws Exception { |
| 172 | + // Use the same storage path as getRandomTempFile() |
| 173 | + String pathPrefix = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + "-"; |
| 174 | + File tempFile = new File(pathPrefix + filename); |
| 175 | + RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); // Create a new random access file with read/write access |
| 176 | + raf.setLength(contentLength); |
| 177 | + return tempFile; |
| 178 | + } |
| 179 | +} |
0 commit comments