Arn { public static Arn fromString(String arn) { int arnColonIndex = arn.indexOf(':'); if (arnColonIndex < 0 || !"arn".equals(arn.substring(0, arnColonIndex))) { throw new IllegalArgumentException("Malformed ARN - doesn't start with 'arn:'"); } int partitionColonIndex = arn.indexOf(':', arnColonIndex + 1); if (partitionColonIndex < 0) { throw new IllegalArgumentException("Malformed ARN - no AWS partition specified"); } String partition = arn.substring(arnColonIndex + 1, partitionColonIndex); int serviceColonIndex = arn.indexOf(':', partitionColonIndex + 1); if (serviceColonIndex < 0) { throw new IllegalArgumentException("Malformed ARN - no service specified"); } String service = arn.substring(partitionColonIndex + 1, serviceColonIndex); int regionColonIndex = arn.indexOf(':', serviceColonIndex + 1); if (regionColonIndex < 0) { throw new IllegalArgumentException("Malformed ARN - no AWS region partition specified"); } String region = arn.substring(serviceColonIndex + 1, regionColonIndex); int accountColonIndex = arn.indexOf(':', regionColonIndex + 1); if (accountColonIndex < 0) { throw new IllegalArgumentException("Malformed ARN - no AWS account specified"); } String accountId = arn.substring(regionColonIndex + 1, accountColonIndex); String resource = arn.substring(accountColonIndex + 1); if (resource.isEmpty()) { throw new IllegalArgumentException("Malformed ARN - no resource specified"); } return Arn.builder() .withPartition(partition) .withService(service) .withRegion(region) .withAccountId(accountId) .withResource(resource) .build(); } private  Arn(Builder builder); String getPartition(); String getService(); String getRegion(); String getAccountId(); ArnResource getResource(); String getResourceAsString(); Builder toBuilder(); static Builder builder(); static Arn fromString(String arn); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test(expected = IllegalArgumentException.class) public void arnWithoutPartition_ThrowsIllegalArgumentException() { String arnString = "arn::s3:us-east-1:12345678910:myresource"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void arnWithoutService_ThrowsIllegalArgumentException() { String arnString = "arn:aws::us-east-1:12345678910:myresource"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void arnWithoutResource_ThrowsIllegalArgumentException() { String arnString = "arn:aws:s3:us-east-1:12345678910:"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void invalidArn_ThrowsIllegalArgumentException() { String arnString = "arn:aws:"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void arnDoesntStartWithArn_ThrowsIllegalArgumentException() { String arnString = "fakearn:aws:"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void invalidArnWithoutPartition_ThrowsIllegalArgumentException() { String arnString = "arn:"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void invalidArnWithoutService_ThrowsIllegalArgumentException() { String arnString = "arn:aws:"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void invalidArnWithoutRegion_ThrowsIllegalArgumentException() { String arnString = "arn:aws:s3:"; Arn arn = Arn.fromString(arnString); }
@Test(expected = IllegalArgumentException.class) public void invalidArnWithoutAccountId_ThrowsIllegalArgumentException() { String arnString = "arn:aws:s3:us-east-1:"; Arn arn = Arn.fromString(arnString); }