Skip to content

Commit

Permalink
Fixes test cases to properly set up and clean up 馃毀
Browse files Browse the repository at this point in the history
Test cases now assume an empty state in the beginning, as enforced by the new methods introduced in commit ce1e173.

#233
  • Loading branch information
jakobvogel committed Sep 18, 2023
1 parent ce1e173 commit b9a1792
Showing 1 changed file with 57 additions and 66 deletions.
123 changes: 57 additions & 66 deletions src/test/java/BaseAWSSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
given:
def bucketName = "does-not-exist"
def client = getClient()
when:
if (client.doesBucketExist(bucketName)) {
client.deleteBucket(bucketName)
}
then:
expect:
!client.doesBucketExist(bucketName)
!client.doesBucketExistV2(bucketName)
}
Expand All @@ -88,28 +84,24 @@ abstract class BaseAWSSpec extends BaseSpecification {
def bucketName = DEFAULT_BUCKET_NAME
def client = getClient()
when:
if (client.doesBucketExist(bucketName)) {
client.deleteBucket(bucketName)
}
client.createBucket(bucketName)
then:
client.doesBucketExist(bucketName)
client.doesBucketExistV2(bucketName)
cleanup:
client.deleteBucket(bucketName)
}

def "DELETE of non-existing bucket as expected"() {
given:
def bucketName = "does-not-exist"
def client = getClient()
when:
if (client.doesBucketExist(bucketName)) {
client.deleteBucket(bucketName)
}
and:
client.deleteBucket(bucketName)
then:
AmazonS3Exception e = thrown()
e.statusCode == 404
and:
!client.doesBucketExist(bucketName)
!client.doesBucketExistV2(bucketName)
}
Expand All @@ -119,11 +111,12 @@ abstract class BaseAWSSpec extends BaseSpecification {
def bucketName = DEFAULT_BUCKET_NAME
def client = getClient()
when:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.deleteBucket(bucketName)
client.createBucket(bucketName)
and:
client.doesBucketExist(bucketName)
then:
client.deleteBucket(bucketName)
and:
!client.doesBucketExist(bucketName)
}

Expand All @@ -133,9 +126,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key = DEFAULT_KEY
def client = getClient()
when:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.createBucket(bucketName)
and:
File file = File.createTempFile("test", "")
file.delete()
Expand All @@ -151,8 +142,9 @@ abstract class BaseAWSSpec extends BaseSpecification {
tm.download(bucketName, key, download).waitForCompletion()
then:
Files.toString(file, StandardCharsets.UTF_8) == Files.toString(download, StandardCharsets.UTF_8)
and:
cleanup:
client.deleteObject(bucketName, key)
client.deleteBucket(bucketName)
}

def "PUT and then GET work as expected"() {
Expand All @@ -161,9 +153,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key = DEFAULT_KEY
def client = getClient()
when:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, key, "Test")
def content = new String(
Expand All @@ -178,8 +168,9 @@ abstract class BaseAWSSpec extends BaseSpecification {
content == "Test"
and:
downloadedData == "Test"
and:
cleanup:
client.deleteObject(bucketName, key)
client.deleteBucket(bucketName)
}

def "PUT and then LIST work as expected"() {
Expand All @@ -189,9 +180,6 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key2 = DEFAULT_KEY + "/Zwei"
def client = getClient()
when:
if (client.doesBucketExist(bucketName)) {
client.deleteBucket(bucketName)
}
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, key1, "Eins")
Expand All @@ -202,6 +190,10 @@ abstract class BaseAWSSpec extends BaseSpecification {
summaries.size() == 2
summaries.get(0).getKey() == key1
summaries.get(1).getKey() == key2
cleanup:
client.deleteObject(bucketName, key1)
client.deleteObject(bucketName, key2)
client.deleteBucket(bucketName)
}

// reported in https://github.com/scireum/s3ninja/issues/180
Expand All @@ -213,9 +205,6 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key3 = "a/key/with a different/prefix/Drei"
def client = getClient()
when:
if (client.doesBucketExist(bucketName)) {
client.deleteBucket(bucketName)
}
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, key1, "Eins")
Expand All @@ -227,6 +216,11 @@ abstract class BaseAWSSpec extends BaseSpecification {
summaries.size() == 2
summaries.get(0).getKey() == key1
summaries.get(1).getKey() == key2
cleanup:
client.deleteObject(bucketName, key1)
client.deleteObject(bucketName, key2)
client.deleteObject(bucketName, key3)
client.deleteBucket(bucketName)
}

def "PUT and then DELETE work as expected"() {
Expand All @@ -235,34 +229,32 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key = DEFAULT_KEY
def client = getClient()
when:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, key, "Test")
client.deleteBucket(bucketName)
client.deleteObject(bucketName, key)
client.getObject(bucketName, key)
then:
AmazonS3Exception e = thrown()
e.statusCode == 404
cleanup:
client.deleteBucket(bucketName)
}

def "MultipartUpload and then GET work as expected"() {
given:
def bucketName = DEFAULT_BUCKET_NAME
def key = DEFAULT_KEY
def client = getClient()
when:
and:
def transfer = TransferManagerBuilder.standard().
withS3Client(client).
withMultipartUploadThreshold(1).
withMinimumUploadPartSize(1).build()
def meta = new ObjectMetadata()
def message = "Test".getBytes(StandardCharsets.UTF_8)
and:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
when:
client.createBucket(bucketName)
and:
meta.setContentLength(message.length)
meta.addUserMetadata("userdata", "test123")
Expand All @@ -275,8 +267,9 @@ abstract class BaseAWSSpec extends BaseSpecification {
then:
content == "Test"
userdata == "test123"
and:
cleanup:
client.deleteObject(bucketName, key)
client.deleteBucket(bucketName)
}

def "MultipartUpload and then DELETE work as expected"() {
Expand All @@ -292,18 +285,18 @@ abstract class BaseAWSSpec extends BaseSpecification {
def meta = new ObjectMetadata()
def message = "Test".getBytes(StandardCharsets.UTF_8)
and:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.createBucket(bucketName)
and:
meta.setContentLength(message.length)
def upload = transfer.upload(bucketName, key, new ByteArrayInputStream(message), meta)
upload.waitForUploadResult()
client.deleteBucket(bucketName)
client.deleteObject(bucketName, key)
client.getObject(bucketName, key)
then:
AmazonS3Exception e = thrown()
e.statusCode == 404
cleanup:
client.deleteBucket(bucketName)
}

def "PUT on presigned URL without signed chunks works as expected"() {
Expand All @@ -312,9 +305,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key = DEFAULT_KEY
def client = getClient()
when:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.createBucket(bucketName)
and:
def content = "NotSigned"
and:
Expand All @@ -336,8 +327,9 @@ abstract class BaseAWSSpec extends BaseSpecification {
String downloadedData = new String(ByteStreams.toByteArray(c.getInputStream()), StandardCharsets.UTF_8)
then:
downloadedData == content
and:
cleanup:
client.deleteObject(bucketName, key)
client.deleteBucket(bucketName)
}

// reported in https://github.com/scireum/s3ninja/issues/153
Expand All @@ -347,9 +339,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key = DEFAULT_KEY
def client = getClient()
when:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, key, "Test")
def content = new String(
Expand All @@ -368,8 +358,9 @@ abstract class BaseAWSSpec extends BaseSpecification {
content == "Test"
and:
downloadedData == "Test"
and:
cleanup:
client.deleteObject(bucketName, key)
client.deleteBucket(bucketName)
}

// reported in https://github.com/scireum/s3ninja/issues/181
Expand All @@ -381,6 +372,8 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key3 = DEFAULT_KEY + "/Drei"
def client = getClient()
when:
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, key1, "Eins")
putObjectWithContent(bucketName, key2, "Zwei")
putObjectWithContent(bucketName, key3, "Drei")
Expand All @@ -393,8 +386,9 @@ abstract class BaseAWSSpec extends BaseSpecification {
def listing = client.listObjects(bucketName)
listing.getObjectSummaries().size() == 1
listing.getObjectSummaries().get(0).getKey() == key3
and:
cleanup:
client.deleteObject(bucketName, key3)
client.deleteBucket(bucketName)
}

// reported in https://github.com/scireum/s3ninja/issues/214
Expand All @@ -406,6 +400,8 @@ abstract class BaseAWSSpec extends BaseSpecification {
def key3 = DEFAULT_KEY + "/Drei"
def client = getClient()
when:
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, key1, "Eins")
putObjectWithContent(bucketName, key2, "Zwei")
putObjectWithContent(bucketName, key3, "Drei")
Expand All @@ -415,10 +411,11 @@ abstract class BaseAWSSpec extends BaseSpecification {
result.getObjectSummaries().size() == 2
result.getObjectSummaries().get(0).getKey() == key1
result.getObjectSummaries().get(1).getKey() == key2
and:
cleanup:
client.deleteObject(bucketName, key1)
client.deleteObject(bucketName, key2)
client.deleteObject(bucketName, key3)
client.deleteBucket(bucketName)
}

// reported in https://github.com/scireum/s3ninja/issues/209
Expand All @@ -438,7 +435,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
connection.getResponseCode() == 200
connection.getContentLengthLong() == content.getBytes(StandardCharsets.UTF_8).length
connection.disconnect()
and:
cleanup:
client.deleteObject(bucketName, key)
client.deleteBucket(bucketName)
}
Expand All @@ -452,9 +449,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
def content = "I am pointless text content, but I deserve to exist twice and will thus be copied!"
def client = getClient()
when:
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName)
}
client.createBucket(bucketName)
and:
putObjectWithContent(bucketName, keyFrom, content)
and:
Expand All @@ -466,7 +461,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
String downloadedData = new String(ByteStreams.toByteArray(c.getInputStream()), StandardCharsets.UTF_8)
then:
downloadedData == content
and:
cleanup:
client.deleteObject(bucketName, keyFrom)
client.deleteObject(bucketName, keyTo)
client.deleteBucket(bucketName)
Expand All @@ -481,13 +476,9 @@ abstract class BaseAWSSpec extends BaseSpecification {
def content = "I am pointless text content, but I deserve to exist twice and will thus be copied!"
def client = getClient()
when:
if (!client.doesBucketExist(bucketNameFrom)) {
client.createBucket(bucketNameFrom)
}
client.createBucket(bucketNameFrom)
and:
if (!client.doesBucketExist(bucketNameTo)) {
client.createBucket(bucketNameTo)
}
client.createBucket(bucketNameTo)
and:
putObjectWithContent(bucketNameFrom, key, content)
and:
Expand All @@ -499,7 +490,7 @@ abstract class BaseAWSSpec extends BaseSpecification {
String downloadedData = new String(ByteStreams.toByteArray(c.getInputStream()), StandardCharsets.UTF_8)
then:
downloadedData == content
and:
cleanup:
client.deleteObject(bucketNameFrom, key)
client.deleteBucket(bucketNameFrom)
client.deleteObject(bucketNameTo, key)
Expand Down

0 comments on commit b9a1792

Please sign in to comment.