-
Notifications
You must be signed in to change notification settings - Fork 912
Better arn parsing #6163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RanVaknin
wants to merge
3
commits into
master
Choose a base branch
from
rvaknin/better-arn-parsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Better arn parsing #6163
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,14 @@ | |
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class ArnTest { | ||
|
||
|
@@ -311,4 +317,78 @@ public void invalidArnWithoutAccountId_ThrowsIllegalArgumentException() { | |
String arnString = "arn:aws:s3:us-east-1:"; | ||
assertThatThrownBy(() -> Arn.fromString(arnString)).hasMessageContaining("Malformed ARN"); | ||
} | ||
|
||
private static Stream<Arguments> validArnTestCases() { | ||
return Stream.of( | ||
Arguments.of("Basic resource", "arn:aws:s3:us-east-1:12345678910:myresource"), | ||
Arguments.of("Minimal requirements", "arn:aws:foobar:::myresource"), | ||
Arguments.of("Qualified resource", "arn:aws:s3:us-east-1:12345678910:myresource:foobar:1"), | ||
Arguments.of("Minimal resources", "arn:aws:s3:::bucket"), | ||
Arguments.of("Without region", "arn:aws:iam::123456789012:root"), | ||
Arguments.of("Resource type and resource", "arn:aws:s3:us-east-1:12345678910:bucket:foobar"), | ||
Arguments.of("Resource type And resource and qualifier", | ||
"arn:aws:s3:us-east-1:12345678910:bucket:foobar:1"), | ||
Arguments.of("Resource type And resource with slash", "arn:aws:s3:us-east-1:12345678910:bucket/foobar"), | ||
Arguments.of("Resource type and resource and qualifier slash", | ||
"arn:aws:s3:us-east-1:12345678910:bucket/foobar/1"), | ||
Arguments.of("Without region", "arn:aws:s3::123456789012:myresource"), | ||
Arguments.of("Without accountId", "arn:aws:s3:us-east-1::myresource"), | ||
Arguments.of("Resource with dots", "arn:aws:s3:us-east-1:12345678910:myresource:foobar.1") | ||
); | ||
} | ||
|
||
private static Stream<Arguments> invalidArnTestCases() { | ||
return Stream.of( | ||
Arguments.of("Without resource", "arn:aws:s3:us-east-1:12345678910:"), | ||
Arguments.of("Invalid arn", "arn:aws:"), | ||
Arguments.of("Doesn't start with arn", "fakearn:aws:"), | ||
Arguments.of("Invalid without partition", "arn:"), | ||
Arguments.of("Invalid without service", "arn:aws:"), | ||
Arguments.of("Invalid without region", "arn:aws:s3:"), | ||
Arguments.of("Invalid without accountId", "arn:aws:s3:us-east-1:"), | ||
Arguments.of("Null Arn", null) | ||
); | ||
} | ||
|
||
private static Stream<Arguments> exceptionThrowingArnTestCases() { | ||
return Stream.of( | ||
Arguments.of("Valid without partition", "arn::s3:us-east-1:12345678910:myresource"), | ||
Arguments.of("Valid without service", "arn:aws::us-east-1:12345678910:myresource") | ||
); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("validArnTestCases") | ||
public void optionalArnFromString_ValidArns_ReturnsPopulatedOptional(String testName, String arnString) { | ||
Optional<Arn> optionalArn = Arn.tryFromString(arnString); | ||
|
||
assertThat(optionalArn).isPresent(); | ||
|
||
Arn expectedArn = Arn.fromString(arnString); | ||
Arn actualArn = optionalArn.get(); | ||
|
||
assertThat(actualArn.partition()).isEqualTo(expectedArn.partition()); | ||
assertThat(actualArn.service()).isEqualTo(expectedArn.service()); | ||
assertThat(actualArn.region()).isEqualTo(expectedArn.region()); | ||
assertThat(actualArn.accountId()).isEqualTo(expectedArn.accountId()); | ||
assertThat(actualArn.resourceAsString()).isEqualTo(expectedArn.resourceAsString()); | ||
|
||
assertThat(actualArn.toString()).isEqualTo(arnString); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("invalidArnTestCases") | ||
public void optionalArnFromString_InvalidArns_ReturnsEmptyOptional(String testName, String arnString) { | ||
Optional<Arn> optionalArn = Arn.tryFromString(arnString); | ||
assertThat(optionalArn).isEmpty(); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("exceptionThrowingArnTestCases") | ||
public void tryFromString_InvalidArns_ShouldThrowExceptions(String testName, String arnString) { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
Arn.tryFromString(arnString); | ||
}); | ||
} | ||
Comment on lines
+388
to
+392
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I thought it would no longer throw exception, did I miss anything? |
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It contains a lot of duplicate code. Can we extract common code?
One idea is to create a helper method that takes a boolean for throwing behavior
private static Optional<Arn> parseArn(String arn, boolean throwOnError) {...}