Skip to content

Commit ebd7e3f

Browse files
committed
Add more unit tests
These were written by Junie, but needed tweaking and refactoring.
1 parent 2c1f81d commit ebd7e3f

File tree

1 file changed

+159
-1
lines changed

1 file changed

+159
-1
lines changed

server/src/test/kotlin/com/adobe/testing/s3mock/BucketControllerTest.kt

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import com.adobe.testing.s3mock.dto.LifecycleRuleFilter
3232
import com.adobe.testing.s3mock.dto.ListAllMyBucketsResult
3333
import com.adobe.testing.s3mock.dto.ListBucketResult
3434
import com.adobe.testing.s3mock.dto.ListBucketResultV2
35+
import com.adobe.testing.s3mock.dto.ListVersionsResult
36+
import com.adobe.testing.s3mock.dto.VersioningConfiguration
3537
import com.adobe.testing.s3mock.dto.LocationConstraint
3638
import com.adobe.testing.s3mock.dto.LocationInfo
3739
import com.adobe.testing.s3mock.dto.LocationType.AVAILABILITY_ZONE
@@ -60,6 +62,7 @@ import org.mockito.ArgumentMatchers
6062
import org.mockito.ArgumentMatchers.any
6163
import org.mockito.kotlin.doThrow
6264
import org.mockito.kotlin.verify
65+
import org.mockito.kotlin.eq
6366
import org.mockito.kotlin.whenever
6467
import org.springframework.beans.factory.annotation.Autowired
6568
import org.springframework.boot.test.context.SpringBootTest
@@ -754,6 +757,161 @@ internal class BucketControllerTest : BaseControllerTest() {
754757
assertThat(response.body).isEqualTo(MAPPER.writeValueAsString(configuration))
755758
}
756759

760+
@Test
761+
@Throws(Exception::class)
762+
fun testDeleteBucketLifecycleConfiguration_NoContent() {
763+
givenBucket()
764+
765+
val headers = HttpHeaders().apply {
766+
this.accept = listOf(MediaType.APPLICATION_XML)
767+
this.contentType = MediaType.APPLICATION_XML
768+
}
769+
val uri = UriComponentsBuilder
770+
.fromUriString("/test-bucket")
771+
.queryParam(AwsHttpParameters.LIFECYCLE, "ignored")
772+
.build()
773+
.toString()
774+
775+
val response = restTemplate.exchange(
776+
uri,
777+
HttpMethod.DELETE,
778+
HttpEntity<Any>(headers),
779+
String::class.java
780+
)
781+
assertThat(response.statusCode).isEqualTo(HttpStatus.NO_CONTENT)
782+
verify(bucketService).deleteBucketLifecycleConfiguration(TEST_BUCKET_NAME)
783+
}
784+
785+
@Test
786+
@Throws(Exception::class)
787+
fun testGetBucketLocation_Ok() {
788+
givenBucket()
789+
790+
val headers = HttpHeaders().apply {
791+
this.accept = listOf(MediaType.APPLICATION_XML)
792+
this.contentType = MediaType.APPLICATION_XML
793+
}
794+
val uri = UriComponentsBuilder
795+
.fromUriString("/test-bucket")
796+
.queryParam(AwsHttpParameters.LOCATION, "ignored")
797+
.build()
798+
.toString()
799+
800+
val response = restTemplate.exchange(
801+
uri,
802+
HttpMethod.GET,
803+
HttpEntity<Any>(headers),
804+
String::class.java
805+
)
806+
assertThat(response.statusCode).isEqualTo(HttpStatus.OK)
807+
assertThat(response.body).isEqualTo(MAPPER.writeValueAsString(LocationConstraint("us-west-2")))
808+
}
809+
810+
@Test
811+
@Throws(Exception::class)
812+
fun testGetBucketVersioningConfiguration_Ok() {
813+
givenBucket()
814+
val expected = VersioningConfiguration(VersioningConfiguration.MFADelete.DISABLED, VersioningConfiguration.Status.ENABLED, null)
815+
816+
whenever(bucketService.getVersioningConfiguration(TEST_BUCKET_NAME)).thenReturn(expected)
817+
818+
val headers = HttpHeaders().apply {
819+
this.accept = listOf(MediaType.APPLICATION_XML)
820+
this.contentType = MediaType.APPLICATION_XML
821+
}
822+
val uri = UriComponentsBuilder
823+
.fromUriString("/test-bucket")
824+
.queryParam(AwsHttpParameters.VERSIONING, "ignored")
825+
.build()
826+
.toString()
827+
val response = restTemplate.exchange(
828+
uri,
829+
HttpMethod.GET,
830+
HttpEntity<Any>(headers),
831+
String::class.java
832+
)
833+
assertThat(response.statusCode).isEqualTo(HttpStatus.OK)
834+
assertThat(response.body).isEqualTo(MAPPER.writeValueAsString(expected))
835+
}
836+
837+
@Test
838+
@Throws(Exception::class)
839+
fun testPutBucketVersioningConfiguration_Ok() {
840+
givenBucket()
841+
val configuration = VersioningConfiguration(VersioningConfiguration.MFADelete.DISABLED, VersioningConfiguration.Status.SUSPENDED, null)
842+
843+
val headers = HttpHeaders().apply {
844+
this.accept = listOf(MediaType.APPLICATION_XML)
845+
this.contentType = MediaType.APPLICATION_XML
846+
}
847+
val uri = UriComponentsBuilder
848+
.fromUriString("/test-bucket")
849+
.queryParam(AwsHttpParameters.VERSIONING, "ignored")
850+
.build()
851+
.toString()
852+
val response = restTemplate.exchange(
853+
uri,
854+
HttpMethod.PUT,
855+
HttpEntity(MAPPER.writeValueAsString(configuration), headers),
856+
String::class.java
857+
)
858+
assertThat(response.statusCode).isEqualTo(HttpStatus.OK)
859+
verify(bucketService).setVersioningConfiguration(TEST_BUCKET_NAME, configuration)
860+
}
861+
862+
@Test
863+
@Throws(Exception::class)
864+
fun testListObjectVersions_Ok() {
865+
givenBucket()
866+
867+
val expected = ListVersionsResult(
868+
emptyList(),
869+
emptyList(),
870+
"",
871+
"",
872+
false,
873+
"",
874+
MAX_KEYS_DEFAULT,
875+
TEST_BUCKET_NAME,
876+
"",
877+
"",
878+
"",
879+
emptyList(),
880+
""
881+
)
882+
883+
whenever(
884+
bucketService.listVersions(
885+
eq(TEST_BUCKET_NAME),
886+
any(),
887+
any(),
888+
any(),
889+
eq(MAX_KEYS_DEFAULT),
890+
any(),
891+
any()
892+
)
893+
).thenReturn(expected)
894+
895+
val headers = HttpHeaders().apply {
896+
this.accept = listOf(MediaType.APPLICATION_XML)
897+
this.contentType = MediaType.APPLICATION_XML
898+
}
899+
val uri = UriComponentsBuilder
900+
.fromUriString("/test-bucket")
901+
.queryParam(AwsHttpParameters.VERSIONS, "ignored")
902+
.build()
903+
.toString()
904+
905+
val response = restTemplate.exchange(
906+
uri,
907+
HttpMethod.GET,
908+
HttpEntity<Any>(headers),
909+
String::class.java
910+
)
911+
assertThat(response.statusCode).isEqualTo(HttpStatus.OK)
912+
assertThat(response.body).isEqualTo(MAPPER.writeValueAsString(expected))
913+
}
914+
757915

758916
private fun givenBuckets(count: Int = 0,
759917
prefix: String? = null,
@@ -838,7 +996,7 @@ internal class BucketControllerTest : BaseControllerTest() {
838996
private val TEST_OWNER = Owner("s3-mock-file-store", "123")
839997
private const val TEST_BUCKET_NAME = "test-bucket"
840998
private val CREATION_DATE = Instant.now().toString()
841-
private const val BUCKET_REGION = "us-east-1"
999+
private const val BUCKET_REGION = "us-west-2"
8421000
private val BUCKET_PATH = Paths.get("/tmp/foo/1")
8431001
private const val MAX_BUCKETS_DEFAULT = 1000
8441002
private const val MAX_KEYS_DEFAULT = 1000

0 commit comments

Comments
 (0)