diff --git a/extensions/jets3t/src/main/java/org/jclouds/aws/s3/jets3t/JCloudsS3Service.java b/extensions/jets3t/src/main/java/org/jclouds/aws/s3/jets3t/JCloudsS3Service.java index b9d7a24b72..1630b32b3e 100644 --- a/extensions/jets3t/src/main/java/org/jclouds/aws/s3/jets3t/JCloudsS3Service.java +++ b/extensions/jets3t/src/main/java/org/jclouds/aws/s3/jets3t/JCloudsS3Service.java @@ -27,6 +27,8 @@ import java.util.Calendar; import java.util.Map; import java.util.concurrent.TimeUnit; +import org.jclouds.Utils; +import org.jclouds.aws.s3.S3Connection; import org.jclouds.aws.s3.S3Context; import org.jclouds.aws.s3.S3ContextFactory; import org.jets3t.service.S3ObjectsChunk; @@ -51,8 +53,20 @@ public class JCloudsS3Service extends S3Service { private static final long serialVersionUID = 1L; private final S3Context context; + private final S3Connection connection; + private final long requestTimeoutMilliseconds = 10000; + /** + * Initializes a JClouds context to S3. + * + * @param awsCredentials + * - credentials to access S3 + * @param modules + * - Module that configures a FutureHttpClient, if not specified, + * default is URLFetchServiceClientModule + * @throws S3ServiceException + */ protected JCloudsS3Service(AWSCredentials awsCredentials, Module... modules) throws S3ServiceException { super(awsCredentials); @@ -60,6 +74,7 @@ public class JCloudsS3Service extends S3Service { modules = new Module[] { new org.jclouds.gae.config.URLFetchServiceClientModule() }; context = S3ContextFactory.createS3Context(awsCredentials .getAccessKey(), awsCredentials.getSecretKey(), modules); + connection = context.getConnection(); } @Override @@ -86,24 +101,44 @@ public class JCloudsS3Service extends S3Service { throw new UnsupportedOperationException(); } + /** + * {@inheritDoc} + * + * @see S3Connection#deleteBucket(org.jclouds.aws.s3.domain.S3Bucket) + */ @Override protected void deleteBucketImpl(String bucketName) throws S3ServiceException { try { - context.getConnection().deleteBucket( + connection.deleteBucket( new org.jclouds.aws.s3.domain.S3Bucket(bucketName)).get( requestTimeoutMilliseconds, TimeUnit.MILLISECONDS); } catch (Exception e) { + Utils. rethrowIfRuntimeOrSameType(e); throw new S3ServiceException( "error deleting bucket: " + bucketName, e); } } + /** + * {@inheritDoc} + * + * @see S3Connection#deleteObject(org.jclouds.aws.s3.domain.S3Bucket, + * String) + */ @Override protected void deleteObjectImpl(String bucketName, String objectKey) throws S3ServiceException { - // TODO Unimplemented - throw new UnsupportedOperationException(); + try { + connection.deleteObject( + new org.jclouds.aws.s3.domain.S3Bucket(bucketName), + objectKey).get(requestTimeoutMilliseconds, + TimeUnit.MILLISECONDS); + } catch (Exception e) { + Utils. rethrowIfRuntimeOrSameType(e); + throw new S3ServiceException(String.format( + "error deleting object: %1s:%2s", bucketName, objectKey), e); + } } @Override diff --git a/extensions/jets3t/src/test/java/org/jclouds/aws/s3/jets3t/JCloudsS3ServiceTest.java b/extensions/jets3t/src/test/java/org/jclouds/aws/s3/jets3t/JCloudsS3ServiceTest.java index 4b8c461882..ae6d0e2e25 100644 --- a/extensions/jets3t/src/test/java/org/jclouds/aws/s3/jets3t/JCloudsS3ServiceTest.java +++ b/extensions/jets3t/src/test/java/org/jclouds/aws/s3/jets3t/JCloudsS3ServiceTest.java @@ -23,6 +23,7 @@ */ package org.jclouds.aws.s3.jets3t; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; import java.util.concurrent.ExecutionException; @@ -31,6 +32,7 @@ import java.util.concurrent.TimeoutException; import org.jclouds.aws.s3.S3Context; import org.jclouds.aws.s3.S3IntegrationTest; +import org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule; import org.jets3t.service.S3Service; import org.jets3t.service.S3ServiceException; import org.jets3t.service.model.S3Bucket; @@ -49,6 +51,15 @@ public class JCloudsS3ServiceTest extends S3IntegrationTest { AWSCredentials credentials; S3Service service; + @Override + protected boolean debugEnabled() { + return true; + } + + /** + * overridden only to get access to the amazon credentials used for jets3t + * initialization. + */ @Override protected S3Context createS3Context(String AWSAccessKeyId, String AWSSecretAccessKey) { @@ -56,12 +67,17 @@ public class JCloudsS3ServiceTest extends S3IntegrationTest { return super.createS3Context(AWSAccessKeyId, AWSSecretAccessKey); } + /** + * initialize a new JCloudsS3Service, but passing + * JavaUrlHttpFutureCommandClientModule(), as it is easier to debug in unit + * tests. + * + * @throws S3ServiceException + */ @BeforeMethod public void testJCloudsS3Service() throws S3ServiceException { - service = new JCloudsS3Service( - credentials, - new org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule()); - + service = new JCloudsS3Service(credentials, + new JavaUrlHttpFutureCommandClientModule()); } @Test @@ -82,17 +98,48 @@ public class JCloudsS3ServiceTest extends S3IntegrationTest { @Test public void testDeleteBucketImplString() throws S3ServiceException, InterruptedException, ExecutionException, TimeoutException { - String name = bucketPrefix + ".testDeleteBucketImplString"; - org.jclouds.aws.s3.domain.S3Bucket jcloudsBucket = new org.jclouds.aws.s3.domain.S3Bucket( - name); - client.createBucketIfNotExists(jcloudsBucket).get(10, TimeUnit.SECONDS); - service.deleteBucket(new S3Bucket(name)); + String bucketName = bucketPrefix + ".testDeleteBucketImplString"; + org.jclouds.aws.s3.domain.S3Bucket jcloudsBucket = createBucket(bucketName); + + service.deleteBucket(new S3Bucket(bucketName)); + assert !client.bucketExists(jcloudsBucket).get(10, TimeUnit.SECONDS); } + private org.jclouds.aws.s3.domain.S3Bucket createBucket(String bucketName) + throws InterruptedException, ExecutionException, TimeoutException { + org.jclouds.aws.s3.domain.S3Bucket jcloudsBucket = new org.jclouds.aws.s3.domain.S3Bucket( + bucketName); + client.createBucketIfNotExists(jcloudsBucket).get(10, TimeUnit.SECONDS); + return jcloudsBucket; + } + @Test - public void testDeleteObjectImplStringString() { - fail("Not yet implemented"); + public void testDeleteObjectImplStringString() throws InterruptedException, + ExecutionException, TimeoutException, S3ServiceException { + String bucketName = bucketPrefix + ".testDeleteObjectImplStringString"; + String objectKey = "key"; + String objectValue = "test"; + + org.jclouds.aws.s3.domain.S3Bucket jcloudsBucket = addNewObject( + bucketName, objectKey, objectValue); + + service.deleteObject(bucketName, objectKey); + + assertEquals(client.headObject(jcloudsBucket, objectKey).get(10, + TimeUnit.SECONDS), org.jclouds.aws.s3.domain.S3Object.NOT_FOUND); + } + + private org.jclouds.aws.s3.domain.S3Bucket addNewObject(String name, + String objectKey, String objectValue) throws InterruptedException, + ExecutionException, TimeoutException { + org.jclouds.aws.s3.domain.S3Bucket jcloudsBucket = createBucket(name); + org.jclouds.aws.s3.domain.S3Object jcloudsObject = new org.jclouds.aws.s3.domain.S3Object( + objectKey); + jcloudsObject.setContent(objectValue); + client.addObject(jcloudsBucket, jcloudsObject) + .get(10, TimeUnit.SECONDS); + return jcloudsBucket; } @Test