Skip to content

Commit

Permalink
Version-compatible CopySource
Browse files Browse the repository at this point in the history
  • Loading branch information
afranken committed May 30, 2024
1 parent 31861db commit d31aeaa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
22 changes: 11 additions & 11 deletions server/src/main/java/com/adobe/testing/s3mock/dto/CopySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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)
}

@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)
Expand All @@ -44,23 +44,22 @@ internal class CopySourceTest {
@Test
fun invalidCopySource() {
assertThatThrownBy {
CopySource(UUID.randomUUID().toString())
CopySource.from(UUID.randomUUID().toString())
}
.isInstanceOf(IllegalArgumentException::class.java)
}

@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)
Expand All @@ -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"
}
}

0 comments on commit d31aeaa

Please sign in to comment.