|
| 1 | +/* |
| 2 | + * Copyright 2010-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 static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertNotNull; |
| 20 | + |
| 21 | +import com.amazonaws.AmazonClientException; |
| 22 | +import com.amazonaws.regions.Region; |
| 23 | +import com.amazonaws.regions.Regions; |
| 24 | +import com.amazonaws.services.s3.model.AmazonS3Exception; |
| 25 | +import com.amazonaws.services.s3.model.BucketAccelerateConfiguration; |
| 26 | +import com.amazonaws.services.s3.model.BucketAccelerateStatus; |
| 27 | +import com.amazonaws.services.s3.model.GetObjectRequest; |
| 28 | +import com.amazonaws.services.s3.model.S3Object; |
| 29 | + |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.util.Date; |
| 36 | + |
| 37 | +public class AccelerateIntegrationTest extends S3IntegrationTestBase { |
| 38 | + |
| 39 | + /** The name of the bucket these tests will create, test on and delete */ |
| 40 | + private static final String BUCKET = "acceleration-integration-test-" + new Date().getTime(); |
| 41 | + |
| 42 | + /** The key of the object these tests will create, test on and delete */ |
| 43 | + private static final String KEY = "key"; |
| 44 | + private static AmazonS3Client accelerateS3; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setupBucket() throws Exception { |
| 48 | + s3.createBucket(BUCKET); |
| 49 | + S3IntegrationTestBase.waitForBucketCreation(BUCKET); |
| 50 | + accelerateS3 = new AmazonS3Client(credentials, Region.getRegion(Regions.US_WEST_1)); |
| 51 | + accelerateS3.setS3ClientOptions( |
| 52 | + S3ClientOptions.builder().setAccelerateModeEnabled(true).build()); |
| 53 | + } |
| 54 | + |
| 55 | + @After |
| 56 | + public void teardown() throws Exception { |
| 57 | + try { |
| 58 | + if (s3.doesObjectExist(BUCKET, KEY)) { |
| 59 | + s3.deleteObject(BUCKET, KEY); |
| 60 | + } |
| 61 | + } catch (AmazonClientException ace) { |
| 62 | + ace.printStackTrace(); |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + s3.deleteBucket(BUCKET); |
| 67 | + } catch (AmazonClientException ace) { |
| 68 | + ace.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGetAndSetAcceleration() throws Exception { |
| 74 | + s3.setBucketAccelerateConfiguration(BUCKET, |
| 75 | + new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled)); |
| 76 | + Thread.sleep(2500); |
| 77 | + assertEquals(BucketAccelerateStatus.Enabled.toString(), |
| 78 | + s3.getBucketAccelerateConfiguration(BUCKET).getStatus()); |
| 79 | + |
| 80 | + s3.setBucketAccelerateConfiguration(BUCKET, |
| 81 | + new BucketAccelerateConfiguration(BucketAccelerateStatus.Suspended)); |
| 82 | + Thread.sleep(2500); |
| 83 | + assertEquals(BucketAccelerateStatus.Suspended.toString(), |
| 84 | + s3.getBucketAccelerateConfiguration(BUCKET).getStatus()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testSetAccelerationWithAccelerateClient() throws Exception { |
| 89 | + accelerateS3.setBucketAccelerateConfiguration(BUCKET, |
| 90 | + new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled)); |
| 91 | + Thread.sleep(2500); |
| 92 | + assertEquals(BucketAccelerateStatus.Enabled.toString(), |
| 93 | + accelerateS3.getBucketAccelerateConfiguration(BUCKET).getStatus()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test(expected = AmazonS3Exception.class) |
| 97 | + public void testAccessWithAccelerateClient() throws Exception { |
| 98 | + s3.setBucketAccelerateConfiguration(BUCKET, |
| 99 | + new BucketAccelerateConfiguration(BucketAccelerateStatus.Suspended)); |
| 100 | + // it takes time to propagate the configuration |
| 101 | + Thread.sleep(2500); |
| 102 | + |
| 103 | + try { |
| 104 | + accelerateS3.getBucketAccelerateConfiguration(BUCKET); |
| 105 | + } catch (AmazonS3Exception ase) { |
| 106 | + System.out.println(ase.toString()); |
| 107 | + ase.printStackTrace(); |
| 108 | + assertEquals("InvalidRequest", ase.getErrorCode()); |
| 109 | + throw ase; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testPutAndGetObjectWithAccelerateClient() throws Exception { |
| 115 | + accelerateS3.setBucketAccelerateConfiguration(BUCKET, |
| 116 | + new BucketAccelerateConfiguration(BucketAccelerateStatus.Enabled)); |
| 117 | + long contentLength = 128 * 1024; |
| 118 | + File file = getRandomTempFile(KEY, contentLength); |
| 119 | + accelerateS3.putObject(BUCKET, KEY, file); |
| 120 | + GetObjectRequest req = new GetObjectRequest(BUCKET, KEY); |
| 121 | + File output = new File("output"); |
| 122 | + output.deleteOnExit(); |
| 123 | + S3Object obj = accelerateS3.getObject(req); |
| 124 | + assertNotNull(obj); |
| 125 | + assertFileEqualsStream(file, obj.getObjectContent()); |
| 126 | + obj.getObjectContent().close(); |
| 127 | + } |
| 128 | +} |
0 commit comments