/*
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package software.amazon.awssdk.services.s3;

import static org.assertj.core.api.Assertions.assertThat;
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;

import java.util.StringJoiner;
import org.junit.BeforeClass;
import org.junit.Test;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3control.S3ControlClient;
import software.amazon.awssdk.services.s3control.model.S3ControlException;
import software.amazon.awssdk.services.sts.StsClient;

public class AccessPointsIntegrationTest extends S3IntegrationTestBase {

    private static final String BUCKET = temporaryBucketName(AclIntegrationTest.class);

    private static final String AP_NAME = "java-sdk";

    private static final String KEY = "some-key";

    private static S3ControlClient s3control;

    private static StsClient sts;

    private static String accountId;

    @BeforeClass
    public static void setupFixture() {
        createBucket(BUCKET);

        s3control = S3ControlClient.builder()
                                   .region(Region.US_WEST_2)
                                   .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
                                   .build();

        sts = StsClient.builder()
                       .region(Region.US_WEST_2)
                       .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
                       .build();

        accountId = sts.getCallerIdentity().account();

        try {
            s3control.createAccessPoint(r -> r.accountId(accountId)
                                              .bucket(BUCKET)
                                              .name(AP_NAME));
        } catch (S3ControlException e) {
            // Do nothing as the access point is already created
        }
    }

    @Test
    public void transfer_Succeeds_UsingAccessPoint() {
        StringJoiner apArn = new StringJoiner(":");
        apArn.add("arn").add("aws").add("s3").add("us-west-2").add(accountId).add("accesspoint").add(AP_NAME);

        s3.putObject(PutObjectRequest.builder()
                                     .bucket(apArn.toString())
                                     .key(KEY)
                                     .build(), RequestBody.fromString("helloworld"));

        String objectContent = s3.getObjectAsBytes(GetObjectRequest.builder()
                                                                   .bucket(apArn.toString())
                                                                   .key(KEY)
                                                                   .build()).asUtf8String();

        assertThat(objectContent).isEqualTo("helloworld");
    }

    @Test
    public void transfer_Succeeds_UsingAccessPoint_CrossRegion() {
        S3Client s3DifferentRegion =
            s3ClientBuilder().region(Region.US_EAST_1).serviceConfiguration(c -> c.useArnRegionEnabled(true)).build();

        StringJoiner apArn = new StringJoiner(":");
        apArn.add("arn").add("aws").add("s3").add("us-west-2").add(accountId).add("accesspoint").add(AP_NAME);

        s3DifferentRegion.putObject(PutObjectRequest.builder()
                                                    .bucket(apArn.toString())
                                                    .key(KEY)
                                                    .build(), RequestBody.fromString("helloworld"));

        String objectContent = s3DifferentRegion.getObjectAsBytes(GetObjectRequest.builder()
                                                                                  .bucket(apArn.toString())
                                                                                  .key(KEY)
                                                                                  .build()).asUtf8String();

        assertThat(objectContent).isEqualTo("helloworld");
    }
}

@@ -803,16 +803,45 @@
 Joiner;%0A
+import org.junit.AfterClass;%0A
 import o
@@ -1186,83 +1186,8 @@
 nt;%0A
-import software.amazon.awssdk.services.s3control.model.S3ControlException;%0A
 impo
@@ -1374,17 +1374,26 @@
 tName(Ac
-l
+cessPoints
 Integrat
@@ -1460,17 +1460,47 @@
 java-sdk
-%22
+-%22 + System.currentTimeMillis()
 ;%0A%0A    p
@@ -2238,27 +2238,8 @@
 ();%0A
-%0A        try %7B%0A    
     
@@ -2332,36 +2332,32 @@
                 
-    
 .bucket(BUCKET)%0A
@@ -2356,20 +2356,16 @@
 BUCKET)%0A
-    
         
@@ -2423,119 +2423,186 @@
     
+%7D%0A%0A
     
-%7D catch (S3ControlException e) %7B%0A            // Do nothing as the a
+@AfterClass%0A    public static void tearDown() %7B%0A        deleteBucketAndAllContents(BUCKET);%0A        s3control.deleteA
 ccess
- p
+P
 oint
- is already created%0A        %7D
+(b -%3E b.accountId(accountId).name(AP_NAME));
 %0A   

Fix s3 access point integ tests - creating a new AP for every request and deleting resources afterwards