### Question:
AwsIamService { public InstanceProfile getInstanceProfile(AmazonIdentityManagement iam, String instanceProfileArn, ValidationResultBuilder validationResultBuilder) { InstanceProfile instanceProfile = null; if (instanceProfileArn != null && instanceProfileArn.contains("/")) { String instanceProfileName = instanceProfileArn.split("/", 2)[1]; GetInstanceProfileRequest instanceProfileRequest = new GetInstanceProfileRequest() .withInstanceProfileName(instanceProfileName); try { instanceProfile = iam.getInstanceProfile(instanceProfileRequest).getInstanceProfile(); } catch (NoSuchEntityException | ServiceFailureException e) { String msg = String.format("Instance profile (%s) doesn't exist.", instanceProfileArn); LOGGER.error(msg, e); validationResultBuilder.error(msg); } } return instanceProfile; }  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 invalidInstanceProfileArn() { assertThat(awsIamService.getInstanceProfile(iam, null, null)).isNull(); assertThat(awsIamService.getInstanceProfile(iam, "", null)).isNull(); assertThat(awsIamService.getInstanceProfile(iam, "abc", null)).isNull(); }
@Test public void missingInstanceProfile() { when(iam.getInstanceProfile(any(GetInstanceProfileRequest.class))).thenThrow(NoSuchEntityException.class); String instanceProfileArn = "account/missingInstanceProfile"; ValidationResultBuilder validationRequestBuilder = new ValidationResultBuilder(); InstanceProfile instanceProfile = awsIamService.getInstanceProfile(iam, instanceProfileArn, validationRequestBuilder); assertThat(instanceProfile).isNull(); ValidationResult validationResult = validationRequestBuilder.build(); assertThat(validationResult.hasError()).isTrue(); assertThat(validationResult.getErrors()).isEqualTo( Collections.singletonList(String.format("Instance profile (%s) doesn't exist.", instanceProfileArn))); }
@Test public void instanceProfileServiceFailureException() { when(iam.getInstanceProfile(any(GetInstanceProfileRequest.class))).thenThrow(ServiceFailureException.class); String instanceProfileArn = "account/potentialInstanceProfile"; ValidationResultBuilder validationRequestBuilder = new ValidationResultBuilder(); InstanceProfile instanceProfile = awsIamService.getInstanceProfile(iam, instanceProfileArn, validationRequestBuilder); assertThat(instanceProfile).isNull(); ValidationResult validationResult = validationRequestBuilder.build(); assertThat(validationResult.hasError()).isTrue(); assertThat(validationResult.getErrors()).isEqualTo( Collections.singletonList(String.format("Instance profile (%s) doesn't exist.", instanceProfileArn))); }
@Test public void validInstanceProfile() { String instanceProfileArn = "account/validInstanceProfile"; InstanceProfile expectedInstanceProfile = new InstanceProfile().withArn(instanceProfileArn); GetInstanceProfileResult getInstanceProfileResult = mock(GetInstanceProfileResult.class); when(getInstanceProfileResult.getInstanceProfile()).thenReturn(expectedInstanceProfile); when(iam.getInstanceProfile(any(GetInstanceProfileRequest.class))).thenReturn(getInstanceProfileResult); ValidationResultBuilder validationRequestBuilder = new ValidationResultBuilder(); InstanceProfile instanceProfile = awsIamService.getInstanceProfile(iam, instanceProfileArn, validationRequestBuilder); assertThat(instanceProfile.getArn()).isEqualTo(instanceProfileArn); assertThat(validationRequestBuilder.build().hasError()).isFalse(); }