Skip to content

Commit

Permalink
AWS S3 tests should delete all buckets they create #3167
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Oct 7, 2021
1 parent a17b490 commit 77da31b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,45 @@ public Response read(@PathParam("key") String key) throws Exception {
return Response.noContent().build();
}

@Path("s3/bucket/{bucketName}/object/{key}")
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public Response read(@PathParam("bucketName") String bucketName, @PathParam("key") String key) throws Exception {
producerTemplate.sendBodyAndHeader(
componentUri(bucketName, AWS2S3Operations.deleteObject),
null,
AWS2S3Constants.KEY,
key);
return Response.noContent().build();
}

@Path("s3/object-keys")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<String> objectKey(@QueryParam("bucket") String bucket) throws Exception {
if (bucket == null) {
bucket = bucketName;
}

public List<String> objectKey() throws Exception {
final List<S3Object> objects = (List<S3Object>) producerTemplate.requestBody(
componentUri(bucket, AWS2S3Operations.listObjects) + "&autoCreateBucket=true",
componentUri(AWS2S3Operations.listObjects),
null,
List.class);
return objects.stream().map(S3Object::key).collect(Collectors.toList());
}

/**
* Do not forget to delete every bucket created through this endpoint after the test.
*
* @param newBucketName
* @return
*/
@Path("s3/autoCreateBucket/{newBucketName}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response autoCreateBucket(@PathParam("newBucketName") String newBucketName) {
producerTemplate.sendBody(
componentUri(newBucketName, AWS2S3Operations.listObjects) + "&autoCreateBucket=true",
null);
return Response.noContent().build();
}

@Path("s3/upload/{key}")
@POST
@Consumes(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.quarkus.component.aws2;

import java.util.Locale;
import java.util.UUID;
import java.util.stream.Stream;

Expand Down Expand Up @@ -150,40 +151,64 @@ public void upload() throws Exception {
}

@Test
public void copyObject() throws Exception {
public void copyObjectDeleteBucket() throws Exception {
final String oid1 = UUID.randomUUID().toString();
final String oid2 = UUID.randomUUID().toString();
final String blobContent = "Hello " + oid1;
final String bucket = "mycamel" + UUID.randomUUID().toString();
final String destinationBucket = "camel-quarkus-copy-object-"
+ RandomStringUtils.randomAlphanumeric(32).toLowerCase(Locale.ROOT);

// Create
// Create an object to copy
RestAssured.given()
.contentType(ContentType.TEXT)
.body(blobContent)
.post("/aws2/s3/object/" + oid1)
.then()
.statusCode(201);

// Check the dest bucket does not contain oid2
final String[] objects = getAllObjects(bucket);
Assertions.assertTrue(Stream.of(objects).noneMatch(key -> key.equals(oid2)));
try {
autoCreateBucket(destinationBucket);

// Copy
RestAssured.given()
.contentType(ContentType.URLENC)
.formParam("dest_key", oid2)
.formParam("dest_bucket", bucket)
.post("/aws2/s3/copy/" + oid1)
.then()
.statusCode(204);
// Make sure the bucket was created
String[] buckets = getAllBuckets();
Assertions.assertTrue(Stream.of(buckets).anyMatch(key -> key.equals(destinationBucket)));

// Verify the object
RestAssured.given()
.contentType(ContentType.TEXT)
.get("/aws2/s3/object/" + oid2 + "?bucket=" + bucket)
.then()
.statusCode(200)
.body(is(blobContent));
// Copy
RestAssured.given()
.contentType(ContentType.URLENC)
.formParam("dest_key", oid2)
.formParam("dest_bucket", destinationBucket)
.post("/aws2/s3/copy/" + oid1)
.then()
.statusCode(204);

// Verify the object
RestAssured.given()
.contentType(ContentType.TEXT)
.get("/aws2/s3/object/" + oid2 + "?bucket=" + destinationBucket)
.then()
.statusCode(200)
.body(is(blobContent));

} finally {
// Delete the object before deleting the bucket
try {
RestAssured.delete("/aws2/s3/bucket/" + destinationBucket + "/object/" + oid2)
.then()
.statusCode(204);
} catch (Exception ignored) {
}

// Delete the bucket
RestAssured.delete("/aws2/s3/bucket/" + destinationBucket)
.then()
.statusCode(204);

// Make sure destinationBucket was really deleted
final String[] buckets = getAllBuckets();
Assertions.assertTrue(Stream.of(buckets).noneMatch(key -> key.equals(destinationBucket)));

}

}

Expand All @@ -194,24 +219,6 @@ void listBuckets() throws Exception {
Assertions.assertTrue(Stream.of(buckets).anyMatch(key -> key.startsWith("camel-quarkus")));
}

@Test
void deleteBucket() throws Exception {
final String bucket = "mycamel-delete" + UUID.randomUUID().toString();

String[] objects = getAllObjects(bucket);
Assertions.assertTrue(objects.length == 0);

String[] buckets = getAllBuckets();
Assertions.assertTrue(Stream.of(buckets).anyMatch(key -> key.equals(bucket)));

RestAssured.delete("/aws2/s3/bucket/" + bucket)
.then()
.statusCode(204);

buckets = getAllBuckets();
Assertions.assertTrue(Stream.of(buckets).noneMatch(key -> key.equals(bucket)));
}

@Test
public void downloadLink() throws Exception {
final String oid = UUID.randomUUID().toString();
Expand Down Expand Up @@ -256,16 +263,16 @@ private void createObject(String oid, String blobContent) {
.statusCode(201);
}

private String[] getAllObjects(String bucket) {
final String[] objects = RestAssured.given()
.param("bucket", bucket)
.get("/aws2/s3/object-keys")
/**
* Do not forget to delete every bucket you create!
*
* @param newBucketName
*/
private void autoCreateBucket(String newBucketName) {
RestAssured.given()
.get("/aws2/s3/autoCreateBucket/" + newBucketName)
.then()
.statusCode(200)
.extract()
.body().as(String[].class);

return objects;
.statusCode(204);
}

private String[] getAllBuckets() {
Expand Down

0 comments on commit 77da31b

Please sign in to comment.