### Question:
AwsIamService { public Role getRole(AmazonIdentityManagement iam, String roleArn, ValidationResultBuilder validationResultBuilder) { Role role = null; if (roleArn != null && roleArn.contains("/")) { String roleName = roleArn.split("/", 2)[1]; GetRoleRequest roleRequest = new GetRoleRequest().withRoleName(roleName); try { role = iam.getRole(roleRequest).getRole(); } catch (NoSuchEntityException | ServiceFailureException e) { String msg = String.format("Role (%s) doesn't exist.", roleArn); LOGGER.debug(msg, e); validationResultBuilder.error(msg); } } return role; }  InstanceProfile getInstanceProfile(AmazonIdentityManagement iam, String instanceProfileArn,
            ValidationResultBuilder validationResultBuilder); Set<Role> getValidRoles(AmazonIdentityManagement iam, Set<String> roleArns,
            ValidationResultBuilder validationResultBuilder); Role getRole(AmazonIdentityManagement iam, String roleArn,
            ValidationResultBuilder validationResultBuilder); Policy getAssumeRolePolicy(Role role); Policy getPolicy(String policyFileName, Map<String, String> replacements); SortedSet<String> getStatementActions(Statement statement); SortedSet<String> getStatementResources(Statement statement); List<EvaluationResult> simulatePrincipalPolicy(AmazonIdentityManagement iam,
            String policySourceArn, Collection<String> actionNames, Collection<String> resourceArns); List<EvaluationResult> validateRolePolicies(AmazonIdentityManagement iam, Role role,
            Collection<Policy> policies);  }### Answer:
@Test public void invalidRoleArn() { assertThat(awsIamService.getRole(iam, null, null)).isNull(); assertThat(awsIamService.getRole(iam, "", null)).isNull(); assertThat(awsIamService.getRole(iam, "abc", null)).isNull(); }
@Test public void missingRole() { when(iam.getRole(any(GetRoleRequest.class))).thenThrow(NoSuchEntityException.class); String roleArn = "account/missingRole"; ValidationResultBuilder validationRequestBuilder = new ValidationResultBuilder(); Role role = awsIamService.getRole(iam, roleArn, validationRequestBuilder); assertThat(role).isNull(); ValidationResult validationResult = validationRequestBuilder.build(); assertThat(validationResult.hasError()).isTrue(); assertThat(validationResult.getErrors()).isEqualTo( Collections.singletonList(String.format("Role (%s) doesn't exist.", roleArn))); }
@Test public void roleServiceFailureException() { when(iam.getRole(any(GetRoleRequest.class))).thenThrow(ServiceFailureException.class); String roleArn = "account/potentialRole"; ValidationResultBuilder validationRequestBuilder = new ValidationResultBuilder(); Role role = awsIamService.getRole(iam, roleArn, validationRequestBuilder); assertThat(role).isNull(); ValidationResult validationResult = validationRequestBuilder.build(); assertThat(validationResult.hasError()).isTrue(); assertThat(validationResult.getErrors()).isEqualTo( Collections.singletonList(String.format("Role (%s) doesn't exist.", roleArn))); }
@Test public void validRole() { String roleArn = "account/validRole"; Role expectedRole = new Role().withArn(roleArn); GetRoleResult getRoleResult = mock(GetRoleResult.class); when(getRoleResult.getRole()).thenReturn(expectedRole); when(iam.getRole(any(GetRoleRequest.class))).thenReturn(getRoleResult); ValidationResultBuilder validationRequestBuilder = new ValidationResultBuilder(); Role role = awsIamService.getRole(iam, roleArn, validationRequestBuilder); assertThat(role.getArn()).isEqualTo(roleArn); assertThat(validationRequestBuilder.build().hasError()).isFalse(); }