Skip to content

Commit

Permalink
#221 rewriting static bucket router + test
Browse files Browse the repository at this point in the history
  • Loading branch information
max402 committed Sep 24, 2024
1 parent 9d3b489 commit d7a1178
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ public String resourceKey(AbsoluteLocation resource) {
UnaryOperator<String> trimStartingSlash = str -> str.replaceFirst("^/", "");

String resourcePath = trimStartingSlash.apply(resource.location().getRawPath());
String bucketNameWithRegion = region + "/" + bucketName;
if (bucketName == null || "".equals(bucketName) || !resourcePath.startsWith(bucketNameWithRegion)) {
return resourcePath;
}

return trimStartingSlash.apply(resourcePath.substring(resourcePath.indexOf(bucketNameWithRegion) + bucketNameWithRegion.length()));
if (bucketName != null && !bucketName.isEmpty()) {
if (resourcePath.startsWith(bucketName)) {
return trimStartingSlash.apply(resourcePath.substring(resourcePath.indexOf(bucketName) + bucketName.length()));
}
String bucketNameWithRegion = region + "/" + bucketName;
if (resourcePath.startsWith(bucketNameWithRegion)) {
return trimStartingSlash.apply(resourcePath.substring(resourcePath.indexOf(bucketNameWithRegion) + bucketNameWithRegion.length()));
}
}
return resourcePath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.adorsys.datasafe.storage.impl.s3;

import de.adorsys.datasafe.types.api.resource.AbsoluteLocation;
import de.adorsys.datasafe.types.api.resource.BasePrivateResource;
import de.adorsys.datasafe.types.api.resource.Uri;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
public class StaticBucketRouterTest {

private StaticBucketRouter router;

@BeforeEach
void setup() {
String region = "region";
String bucketName = "bucket";
router = new StaticBucketRouter(region, bucketName);
}

@Test
void resourceKeyTest() {
var root = new AbsoluteLocation<>(BasePrivateResource.forPrivate(new Uri("http://s3-us-west-2.amazonaws.com/bucket/users/myuserid/private/files/bucket/users/otheruser/private/files/somefile.aes")));
log.info(String.valueOf(root));
String resourcePath = router.resourceKey(root);
assertThat(resourcePath).hasToString("users/myuserid/private/files/bucket/users/otheruser/private/files/somefile.aes");
}

@Test
void noBucketInPath() {
var root = new AbsoluteLocation<>(BasePrivateResource.forPrivate(new Uri("http://bucket.s3-us-west-2.amazonaws.com/users/myuserid/private/files/bucket/users/otheruser/private/files/somefile.aes")));
String resourcePath = router.resourceKey(root);
assertThat(resourcePath).hasToString("users/myuserid/private/files/bucket/users/otheruser/private/files/somefile.aes");
}

@Test
void regionAndBucketInPath() {
var root = new AbsoluteLocation<>(BasePrivateResource.forPrivate(new Uri("s3://host/region/bucket/users/myuserid/private/files/bucket/users/otheruser/private/files/somefile.aes")));
String resourcePath = router.resourceKey(root);
assertThat(resourcePath).hasToString("users/myuserid/private/files/bucket/users/otheruser/private/files/somefile.aes");
}
}

0 comments on commit d7a1178

Please sign in to comment.