|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat;
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
| 20 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
20 | 21 |
|
| 22 | +import java.util.Optional; |
| 23 | +import java.util.stream.Stream; |
21 | 24 | import org.junit.jupiter.api.Test;
|
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
22 | 28 |
|
23 | 29 | public class ArnTest {
|
24 | 30 |
|
@@ -311,4 +317,78 @@ public void invalidArnWithoutAccountId_ThrowsIllegalArgumentException() {
|
311 | 317 | String arnString = "arn:aws:s3:us-east-1:";
|
312 | 318 | assertThatThrownBy(() -> Arn.fromString(arnString)).hasMessageContaining("Malformed ARN");
|
313 | 319 | }
|
| 320 | + |
| 321 | + private static Stream<Arguments> validArnTestCases() { |
| 322 | + return Stream.of( |
| 323 | + Arguments.of("Basic resource", "arn:aws:s3:us-east-1:12345678910:myresource"), |
| 324 | + Arguments.of("Minimal requirements", "arn:aws:foobar:::myresource"), |
| 325 | + Arguments.of("Qualified resource", "arn:aws:s3:us-east-1:12345678910:myresource:foobar:1"), |
| 326 | + Arguments.of("Minimal resources", "arn:aws:s3:::bucket"), |
| 327 | + Arguments.of("Without region", "arn:aws:iam::123456789012:root"), |
| 328 | + Arguments.of("Resource type and resource", "arn:aws:s3:us-east-1:12345678910:bucket:foobar"), |
| 329 | + Arguments.of("Resource type And resource and qualifier", |
| 330 | + "arn:aws:s3:us-east-1:12345678910:bucket:foobar:1"), |
| 331 | + Arguments.of("Resource type And resource with slash", "arn:aws:s3:us-east-1:12345678910:bucket/foobar"), |
| 332 | + Arguments.of("Resource type and resource and qualifier slash", |
| 333 | + "arn:aws:s3:us-east-1:12345678910:bucket/foobar/1"), |
| 334 | + Arguments.of("Without region", "arn:aws:s3::123456789012:myresource"), |
| 335 | + Arguments.of("Without accountId", "arn:aws:s3:us-east-1::myresource"), |
| 336 | + Arguments.of("Resource with dots", "arn:aws:s3:us-east-1:12345678910:myresource:foobar.1") |
| 337 | + ); |
| 338 | + } |
| 339 | + |
| 340 | + private static Stream<Arguments> invalidArnTestCases() { |
| 341 | + return Stream.of( |
| 342 | + Arguments.of("Without resource", "arn:aws:s3:us-east-1:12345678910:"), |
| 343 | + Arguments.of("Invalid arn", "arn:aws:"), |
| 344 | + Arguments.of("Doesn't start with arn", "fakearn:aws:"), |
| 345 | + Arguments.of("Invalid without partition", "arn:"), |
| 346 | + Arguments.of("Invalid without service", "arn:aws:"), |
| 347 | + Arguments.of("Invalid without region", "arn:aws:s3:"), |
| 348 | + Arguments.of("Invalid without accountId", "arn:aws:s3:us-east-1:"), |
| 349 | + Arguments.of("Null Arn", null) |
| 350 | + ); |
| 351 | + } |
| 352 | + |
| 353 | + private static Stream<Arguments> exceptionThrowingArnTestCases() { |
| 354 | + return Stream.of( |
| 355 | + Arguments.of("Valid without partition", "arn::s3:us-east-1:12345678910:myresource"), |
| 356 | + Arguments.of("Valid without service", "arn:aws::us-east-1:12345678910:myresource") |
| 357 | + ); |
| 358 | + } |
| 359 | + |
| 360 | + @ParameterizedTest(name = "{0}") |
| 361 | + @MethodSource("validArnTestCases") |
| 362 | + public void optionalArnFromString_ValidArns_ReturnsPopulatedOptional(String testName, String arnString) { |
| 363 | + Optional<Arn> optionalArn = Arn.tryFromString(arnString); |
| 364 | + |
| 365 | + assertThat(optionalArn).isPresent(); |
| 366 | + |
| 367 | + Arn expectedArn = Arn.fromString(arnString); |
| 368 | + Arn actualArn = optionalArn.get(); |
| 369 | + |
| 370 | + assertThat(actualArn.partition()).isEqualTo(expectedArn.partition()); |
| 371 | + assertThat(actualArn.service()).isEqualTo(expectedArn.service()); |
| 372 | + assertThat(actualArn.region()).isEqualTo(expectedArn.region()); |
| 373 | + assertThat(actualArn.accountId()).isEqualTo(expectedArn.accountId()); |
| 374 | + assertThat(actualArn.resourceAsString()).isEqualTo(expectedArn.resourceAsString()); |
| 375 | + |
| 376 | + assertThat(actualArn.toString()).isEqualTo(arnString); |
| 377 | + } |
| 378 | + |
| 379 | + @ParameterizedTest(name = "{0}") |
| 380 | + @MethodSource("invalidArnTestCases") |
| 381 | + public void optionalArnFromString_InvalidArns_ReturnsEmptyOptional(String testName, String arnString) { |
| 382 | + Optional<Arn> optionalArn = Arn.tryFromString(arnString); |
| 383 | + assertThat(optionalArn).isEmpty(); |
| 384 | + } |
| 385 | + |
| 386 | + @ParameterizedTest(name = "{0}") |
| 387 | + @MethodSource("exceptionThrowingArnTestCases") |
| 388 | + public void tryFromString_InvalidArns_ShouldThrowExceptions(String testName, String arnString) { |
| 389 | + assertThrows(IllegalArgumentException.class, () -> { |
| 390 | + Arn.tryFromString(arnString); |
| 391 | + }); |
| 392 | + } |
| 393 | + |
314 | 394 | }
|
0 commit comments