|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.urlconnection; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.testutils.service.AwsTestBase.CREDENTIALS_PROVIDER_CHAIN; |
| 19 | + |
| 20 | +import org.assertj.core.api.Assertions; |
| 21 | +import org.junit.AfterClass; |
| 22 | +import org.junit.BeforeClass; |
| 23 | +import org.junit.Test; |
| 24 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 25 | +import software.amazon.awssdk.regions.Region; |
| 26 | +import software.amazon.awssdk.services.s3.S3Client; |
| 27 | +import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; |
| 28 | +import software.amazon.awssdk.services.s3.model.CreateBucketRequest; |
| 29 | +import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; |
| 30 | +import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; |
| 31 | +import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; |
| 32 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 33 | + |
| 34 | +public class S3WithUrlHttpClientIntegrationTest { |
| 35 | + |
| 36 | + /** |
| 37 | + * The name of the bucket created, used, and deleted by these tests. |
| 38 | + */ |
| 39 | + private static String BUCKET_NAME = "java-sdk-integ-" + System.currentTimeMillis(); |
| 40 | + |
| 41 | + private static String KEY = "key"; |
| 42 | + |
| 43 | + private static Region REGION = Region.US_WEST_2; |
| 44 | + |
| 45 | + private static S3Client s3; |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates all the test resources for the tests. |
| 49 | + */ |
| 50 | + @BeforeClass |
| 51 | + public static void createResources() throws Exception { |
| 52 | + s3 = S3Client.builder() |
| 53 | + .region(REGION) |
| 54 | + .httpClient(UrlConnectionHttpClient.builder().build()) |
| 55 | + .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN) |
| 56 | + .build(); |
| 57 | + |
| 58 | + createBucket(BUCKET_NAME, REGION); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Releases all resources created in this test. |
| 63 | + */ |
| 64 | + @AfterClass |
| 65 | + public static void tearDown() { |
| 66 | + deleteObject(BUCKET_NAME, KEY); |
| 67 | + deleteBucket(BUCKET_NAME); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void verifyPutObject() { |
| 72 | + Assertions.assertThat(objectCount(BUCKET_NAME)).isEqualTo(0); |
| 73 | + |
| 74 | + // Put Object |
| 75 | + s3.putObject(PutObjectRequest.builder().bucket(BUCKET_NAME).key(KEY).build(), |
| 76 | + RequestBody.fromString("foobar")); |
| 77 | + |
| 78 | + |
| 79 | + Assertions.assertThat(objectCount(BUCKET_NAME)).isEqualTo(1); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + private static void createBucket(String bucket, Region region) { |
| 84 | + s3.createBucket(CreateBucketRequest |
| 85 | + .builder() |
| 86 | + .bucket(bucket) |
| 87 | + .createBucketConfiguration( |
| 88 | + CreateBucketConfiguration.builder() |
| 89 | + .locationConstraint(region.id()) |
| 90 | + .build()) |
| 91 | + .build()); |
| 92 | + } |
| 93 | + |
| 94 | + private static void deleteObject(String bucket, String key) { |
| 95 | + DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(key).build(); |
| 96 | + s3.deleteObject(deleteObjectRequest); |
| 97 | + } |
| 98 | + |
| 99 | + private static void deleteBucket(String bucket) { |
| 100 | + DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build(); |
| 101 | + s3.deleteBucket(deleteBucketRequest); |
| 102 | + } |
| 103 | + |
| 104 | + private int objectCount(String bucket) { |
| 105 | + ListObjectsV2Request listReq = ListObjectsV2Request.builder() |
| 106 | + .bucket(bucket) |
| 107 | + .build(); |
| 108 | + |
| 109 | + return s3.listObjectsV2(listReq).keyCount(); |
| 110 | + } |
| 111 | +} |
0 commit comments