From d31aeaa670e68690b4f2b35cf3ab46e4dedff3c3 Mon Sep 17 00:00:00 2001 From: Arne Franken Date: Wed, 29 May 2024 23:58:01 +0200 Subject: [PATCH] Version-compatible CopySource --- .../adobe/testing/s3mock/dto/CopySource.java | 22 +++++++++---------- .../testing/s3mock/dto/CopySourceTest.kt | 14 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/com/adobe/testing/s3mock/dto/CopySource.java b/server/src/main/java/com/adobe/testing/s3mock/dto/CopySource.java index 2e2cca4b2..31623fe01 100644 --- a/server/src/main/java/com/adobe/testing/s3mock/dto/CopySource.java +++ b/server/src/main/java/com/adobe/testing/s3mock/dto/CopySource.java @@ -39,17 +39,17 @@ public record CopySource( * @throws IllegalArgumentException If {@code copySource} could not be parsed. * @throws NullPointerException If {@code copySource} is null. */ - public CopySource(String copySource) { - //inefficient duplicate parsing of incoming String, call to default constructor must be the - //first statement... - this(extractBucketAndKeyArray( - SdkHttpUtils.urlDecode(copySource) - )[0], - extractBucketAndKeyArray( - SdkHttpUtils.urlDecode(copySource) - )[1], - null //TODO: support versionId - ); + public static CopySource from(String copySource) { + var bucketAndKey = extractBucketAndKeyArray(SdkHttpUtils.urlDecode(copySource)); + var bucket = requireNonNull(bucketAndKey[0]); + var key = requireNonNull(bucketAndKey[1]); + String versionId = null; + if (key.contains("?versionId=")) { + String[] keyAndVersionId = key.split("\\?versionId="); + key = keyAndVersionId[0]; + versionId = keyAndVersionId[1]; + } + return new CopySource(bucket, key, versionId); } /** diff --git a/server/src/test/kotlin/com/adobe/testing/s3mock/dto/CopySourceTest.kt b/server/src/test/kotlin/com/adobe/testing/s3mock/dto/CopySourceTest.kt index 630a38384..e853ae5b6 100644 --- a/server/src/test/kotlin/com/adobe/testing/s3mock/dto/CopySourceTest.kt +++ b/server/src/test/kotlin/com/adobe/testing/s3mock/dto/CopySourceTest.kt @@ -27,7 +27,7 @@ import java.util.UUID internal class CopySourceTest { @Test fun fromPrefixedCopySourceString() { - val copySource = CopySource("/$VALID_COPY_SOURCE") + val copySource = CopySource.from("/$VALID_COPY_SOURCE") assertThat(copySource.bucket).isEqualTo(BUCKET) assertThat(copySource.key).isEqualTo(KEY) @@ -35,7 +35,7 @@ internal class CopySourceTest { @Test fun fromCopySourceString() { - val copySource = CopySource(VALID_COPY_SOURCE) + val copySource = CopySource.from(VALID_COPY_SOURCE) assertThat(copySource.bucket).isEqualTo(BUCKET) assertThat(copySource.key).isEqualTo(KEY) @@ -44,7 +44,7 @@ internal class CopySourceTest { @Test fun invalidCopySource() { assertThatThrownBy { - CopySource(UUID.randomUUID().toString()) + CopySource.from(UUID.randomUUID().toString()) } .isInstanceOf(IllegalArgumentException::class.java) } @@ -52,15 +52,14 @@ internal class CopySourceTest { @Test fun nullCopySource() { assertThatThrownBy { - CopySource(null) + CopySource.from(null) } .isInstanceOf(NullPointerException::class.java) } @Test - @Disabled fun fromCopySourceWithVersion() { - val copySource = CopySource(COPY_SOURCE_WITH_VERSION) + val copySource = CopySource.from(COPY_SOURCE_WITH_VERSION) assertThat(copySource.bucket).isEqualTo(BUCKET) assertThat(copySource.key).isEqualTo(KEY) @@ -69,7 +68,8 @@ internal class CopySourceTest { companion object { private val BUCKET = UUID.randomUUID().toString() private val KEY = UUID.randomUUID().toString() + private const val VERSION = "123" private val VALID_COPY_SOURCE = "$BUCKET/$KEY" - private val COPY_SOURCE_WITH_VERSION = "$VALID_COPY_SOURCE?versionId=123" + private val COPY_SOURCE_WITH_VERSION = "$VALID_COPY_SOURCE?versionId=$VERSION" } }