de.fischer.thotti.s3.S3BucketCreatorIT.java Source code

Java tutorial

Introduction

Here is the source code for de.fischer.thotti.s3.S3BucketCreatorIT.java

Source

/*
 * Copyright 2011 Oliver B. Fischer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 de.fischer.thotti.s3;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import de.fischer.thotti.awscommon.AWSAccessCredentials;
import de.fischer.thotti.s3.clients.S3BucketCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.lang.reflect.Method;
import java.util.UUID;

import static de.fischer.thotti.awscommon.AWSAccessCredentials.fromResource;

@Test(groups = { "test.integration" })
public class S3BucketCreatorIT extends S3BaseTest {
    private static Logger logger = LoggerFactory.getLogger(S3BucketCreatorIT.class);
    private String bucketName;

    public void testCreateBucket() {
        bucketName = "ut." + UUID.randomUUID().toString().replace("-", ".");

        S3BucketCreator creator = new S3BucketCreator(s3Credentials);

        creator.createBucket(bucketName);

    }

    @AfterMethod
    public void removeBucket() {

        AmazonS3Client client = new AmazonS3Client(
                new MyAWSCredentials(s3Credentials.getAccessKeyId(), s3Credentials.getSecretKey()));

        try {
            client.deleteBucket(bucketName);
        } catch (AmazonServiceException ase) {
            // @todo improve error handing

        } catch (AmazonClientException e) {
            // @todo Improve logging
        }

    }

    static class MyAWSCredentials implements AWSCredentials {
        private String accessKeyId;
        private String secretKey;

        MyAWSCredentials(String accessKeyId, String secretKey) {
            this.accessKeyId = accessKeyId;
            this.secretKey = secretKey;
        }

        public void setAccessKeyId(String key) {
            accessKeyId = key;
        }

        public void setSecretKey(String key) {
            secretKey = key;
        }

        @Override
        public String getAWSAccessKeyId() {
            return accessKeyId;
        }

        @Override
        public String getAWSSecretKey() {
            return secretKey;
        }
    }
}