AwsClientFactory { @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) public AmazonEC2 getEc2Client(AwsParamsDto awsParamsDto) { ClientConfiguration clientConfiguration = awsHelper.getClientConfiguration(awsParamsDto); if (StringUtils.isNotBlank(awsParamsDto.getAwsAccessKeyId())) { return AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider( new BasicSessionCredentials(awsParamsDto.getAwsAccessKeyId(), awsParamsDto.getAwsSecretKey(), awsParamsDto.getSessionToken()))) .withClientConfiguration(clientConfiguration).withRegion(awsParamsDto.getAwsRegionName()).build(); } else { return AmazonEC2ClientBuilder.standard().withClientConfiguration(clientConfiguration).withRegion(awsParamsDto.getAwsRegionName()).build(); } }  @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) AmazonSNS getAmazonSNSClient(AwsParamsDto awsParamsDto); @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) AmazonSQS getAmazonSQSClient(AwsParamsDto awsParamsDto); @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) AmazonEC2 getEc2Client(AwsParamsDto awsParamsDto); @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) AmazonElasticMapReduce getEmrClient(AwsParamsDto awsParamsDto); @Cacheable(DaoSpringModuleConfig.HERD_CACHE_NAME) AmazonSimpleEmailService getSesClient(AwsParamsDto awsParamsDto);  }
@Test public void testGetEc2Client() { assertNotNull(awsClientFactory.getEc2Client( new AwsParamsDto(AWS_ASSUMED_ROLE_ACCESS_KEY, AWS_ASSUMED_ROLE_SECRET_KEY, AWS_ASSUMED_ROLE_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1))); }
@Test public void testGetEc2ClientCacheHitMiss() { AwsParamsDto awsParamsDto = new AwsParamsDto(AWS_ASSUMED_ROLE_ACCESS_KEY, AWS_ASSUMED_ROLE_SECRET_KEY, AWS_ASSUMED_ROLE_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1); AmazonEC2 amazonEC2Client = awsClientFactory.getEc2Client(awsParamsDto); assertEquals(amazonEC2Client, awsClientFactory.getEc2Client( new AwsParamsDto(AWS_ASSUMED_ROLE_ACCESS_KEY, AWS_ASSUMED_ROLE_SECRET_KEY, AWS_ASSUMED_ROLE_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1))); assertNotEquals(amazonEC2Client, awsClientFactory.getEc2Client( new AwsParamsDto(AWS_ASSUMED_ROLE_ACCESS_KEY_2, AWS_ASSUMED_ROLE_SECRET_KEY_2, AWS_ASSUMED_ROLE_SESSION_TOKEN_2, HTTP_PROXY_HOST, HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1))); assertNotEquals(amazonEC2Client, awsClientFactory.getEc2Client( new AwsParamsDto(AWS_ASSUMED_ROLE_ACCESS_KEY, AWS_ASSUMED_ROLE_SECRET_KEY, AWS_ASSUMED_ROLE_SESSION_TOKEN, HTTP_PROXY_HOST_2, HTTP_PROXY_PORT_2, AWS_REGION_NAME_US_EAST_1))); cacheManager.getCache(DaoSpringModuleConfig.HERD_CACHE_NAME).clear(); assertNotEquals(amazonEC2Client, awsClientFactory.getEc2Client(awsParamsDto)); }
@Test public void testGetEc2ClientNoAwsCredentials() { assertNotNull(awsClientFactory.getEc2Client( new AwsParamsDto(NO_AWS_ACCESS_KEY, NO_AWS_SECRET_KEY, NO_SESSION_TOKEN, HTTP_PROXY_HOST, HTTP_PROXY_PORT, AWS_REGION_NAME_US_EAST_1))); }