@Test
public void testEmrPreCreateClusterStepsImpl() throws Exception
{

    // Create an override for EMR cluster definition.
    EmrClusterDefinition emrClusterDefinitionOverride = new EmrClusterDefinition();
    emrClusterDefinitionOverride.setAccountId(AWS_ACCOUNT_ID_2);
    emrClusterDefinitionOverride.setAdditionalInfo(STRING_VALUE_2);
    // Create an EMR cluster create request.
    EmrClusterCreateRequest emrClusterCreateRequest =
        new EmrClusterCreateRequest(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME, EMR_CLUSTER_NAME, NO_DRY_RUN, emrClusterDefinitionOverride);
    // Create an EMR cluster alternate key DTO.
    EmrClusterAlternateKeyDto emrClusterAlternateKeyDto = new EmrClusterAlternateKeyDto(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME, EMR_CLUSTER_NAME);
    // Create an EMR cluster definition key.
    EmrClusterDefinitionKey emrClusterDefinitionKey = new EmrClusterDefinitionKey(NAMESPACE, EMR_CLUSTER_DEFINITION_NAME);
    // Create a namespace entity.
    NamespaceEntity namespaceEntity = new NamespaceEntity();
    namespaceEntity.setCode(NAMESPACE);
    // Create an EMR cluster definition entity
    EmrClusterDefinitionEntity emrClusterDefinitionEntity = new EmrClusterDefinitionEntity();
    emrClusterDefinitionEntity.setNamespace(namespaceEntity);
    emrClusterDefinitionEntity.setConfiguration(EMR_CLUSTER_CONFIGURATION_XML_1);
    // Create an original EMR cluster definition.
    EmrClusterDefinition originalEmrClusterDefinition = new EmrClusterDefinition();
    originalEmrClusterDefinition.setAdditionalInfo(STRING_VALUE);
    originalEmrClusterDefinition.setAccountId(AWS_ACCOUNT_ID);
    // Create a final (updated) EMR cluster definition.
    EmrClusterDefinition finalEmrClusterDefinition = new EmrClusterDefinition();
    finalEmrClusterDefinition.setServiceIamRole(SERVICE_IAM_ROLE);
    finalEmrClusterDefinition.setEc2NodeIamProfileName(EC2_NODE_IAM_PROFILE_NAME);
    // Create an EMR parameters DTO.
    EmrParamsDto emrParamsDto =
        new EmrParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1,
            S3_TRUSTING_ACCOUNT_STAGING_BUCKET_NAME);
    // Mock the external calls.
    when(emrClusterDefinitionDaoHelper.getEmrClusterDefinitionEntity(emrClusterDefinitionKey)).thenReturn(emrClusterDefinitionEntity);
    when(xmlHelper.unmarshallXmlToObject(EmrClusterDefinition.class, EMR_CLUSTER_CONFIGURATION_XML_1)).thenReturn(originalEmrClusterDefinition);
    when(emrHelper.getEmrParamsDtoByAccountId(AWS_ACCOUNT_ID_2)).thenReturn(emrParamsDto);
    when(xmlHelper.objectToXml(originalEmrClusterDefinition)).thenReturn(EMR_CLUSTER_CONFIGURATION_XML_2);
    when(configurationHelper.getProperty(ConfigurationValue.S3_STAGING_RESOURCE_LOCATION))
        .thenReturn((String) ConfigurationValue.S3_STAGING_RESOURCE_LOCATION.getDefaultValue());
    when(emrHelper.getS3StagingLocation(S3_TRUSTING_ACCOUNT_STAGING_BUCKET_NAME)).thenReturn(S3_STAGING_LOCATION);
    when(xmlHelper.unmarshallXmlToObject(EmrClusterDefinition.class, EMR_CLUSTER_CONFIGURATION_XML_2)).thenReturn(finalEmrClusterDefinition);
    // Call the method under test.
    emrHelperServiceImpl.emrPreCreateClusterSteps(emrClusterAlternateKeyDto, emrClusterCreateRequest);
    // Verify the external calls.
    verify(emrClusterDefinitionDaoHelper).getEmrClusterDefinitionEntity(emrClusterDefinitionKey);
    verify(xmlHelper).unmarshallXmlToObject(EmrClusterDefinition.class, EMR_CLUSTER_CONFIGURATION_XML_1);
    verify(emrHelper).getEmrParamsDtoByAccountId(AWS_ACCOUNT_ID_2);
    verify(xmlHelper).objectToXml(originalEmrClusterDefinition);
    verify(configurationHelper).getProperty(ConfigurationValue.S3_STAGING_RESOURCE_LOCATION);
    verify(emrHelper).getS3StagingLocation(S3_TRUSTING_ACCOUNT_STAGING_BUCKET_NAME);
    verify(xmlHelper).unmarshallXmlToObject(EmrClusterDefinition.class, EMR_CLUSTER_CONFIGURATION_XML_2);
    verify(emrClusterDefinitionHelper).validateEmrClusterDefinitionConfiguration(finalEmrClusterDefinition);
    verify(namespaceIamRoleAuthorizationHelper).checkPermissions(namespaceEntity, SERVICE_IAM_ROLE, EC2_NODE_IAM_PROFILE_NAME);
    verifyNoMoreInteractionsHelper();
}