@Test public void afterPropertiesSet_noInstanceFound_reportsIllegalStateException() throws Exception { //Arrange this.expectedException.expect(IllegalStateException.class); this.expectedException.expectMessage("No database instance with id:'test'"); AmazonRDS amazonRDS = mock(AmazonRDS.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenThrow(new DBInstanceNotFoundException("foo")); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "foo"); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert }
@Bean public AmazonRDS amazonRDS() { AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); return client; }
@Test public void parseInternal_readReplicaSupportEnabled_configuresReadReplicaEnabledFactoryBean() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonRDS.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-readReplicaEnabled.xml", getClass())); //Act BeanDefinition beanDefinition = beanFactory.getBeanDefinition("test"); //Assert assertEquals(AmazonRdsReadReplicaAwareDataSourceFactoryBean.class.getName(), beanDefinition.getBeanClassName()); }
@Test public void parseInternal_customRegionConfigured_amazonRdsClientWithCustomRegionConfigured() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-customRegion.xml", getClass())); //Act AmazonRDS amazonRDS = beanFactory.getBean(AmazonRDS.class); //Assert //have to use reflection utils assertEquals("https://rds.eu-west-1.amazonaws.com", ReflectionTestUtils.getField(amazonRDS, "endpoint").toString()); }
@Test public void parseInternal_customRegionProviderConfigured_amazonRdsClientWithCustomRegionConfiguredThatIsReturnedFromRegionProvider() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-customRegionProvider.xml", getClass())); //Act AmazonRDS amazonRDS = beanFactory.getBean(AmazonRDS.class); //Assert //have to use reflection utils assertEquals("https://rds.eu-west-1.amazonaws.com", ReflectionTestUtils.getField(amazonRDS, "endpoint").toString()); }
@Test public void canRetry_retryPossibleDueToAvailableDatabase_returnsTrue() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "test"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenReturn(new DescribeDBInstancesResult().withDBInstances(new DBInstance().withDBInstanceStatus("available"))); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert assertTrue(policy.canRetry(retryContext)); policy.close(retryContext); }
@Test public void canRetry_withResourceIdResolver_returnsTrue() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "foo"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenReturn(new DescribeDBInstancesResult().withDBInstances(new DBInstance().withDBInstanceStatus("available"))); when(resourceIdResolver.resolveToPhysicalResourceId("foo")).thenReturn("test"); policy.setResourceIdResolver(resourceIdResolver); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert assertTrue(policy.canRetry(retryContext)); policy.close(retryContext); }
@Test public void canRetry_retryNotPossibleDueToNoDatabase_returnsFalse() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "test"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenThrow(new DBInstanceNotFoundException("test")); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert assertFalse(policy.canRetry(retryContext)); policy.close(retryContext); }
@Test public void canRetry_multipleDatabasesFoundForInstanceIdentifier_reportsException() throws Exception { //Arrange this.expectedException.expect(IllegalStateException.class); this.expectedException.expectMessage("Multiple databases found for same identifier"); AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "test"); DescribeDBInstancesResult describeDBInstancesResult = new DescribeDBInstancesResult().withDBInstances(new DBInstance(), new DBInstance()); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenReturn(describeDBInstancesResult); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert policy.canRetry(retryContext); }
@Before public void beforeEachTestRuns() { AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain(); AmazonEC2 ec2Client = EnvironmentSetupForTests.createEC2Client(); AmazonElasticLoadBalancing awsElbClient = EnvironmentSetupForTests.createELBClient(); AmazonCloudFormation cfnClient = EnvironmentSetupForTests.createCFNClient(); AmazonRDS awsRdsClient = EnvironmentSetupForTests.createRDSClient(); CloudClient cloudClient = new CloudClient(ec2Client, new DefaultAwsRegionProviderChain()); LoadBalancerClient elbClient = new LoadBalancerClient(awsElbClient); VpcRepository vpcRepository = new VpcRepository(cloudClient); CloudFormationClient cloudFormationClient = new CloudFormationClient(cfnClient); cloudRepository = new CloudRepository(cloudClient); ResourceRepository cfnRepository = new CfnRepository(cloudFormationClient, cloudRepository, "CfnAssist"); elbRepository = new ELBRepository(elbClient, vpcRepository, cfnRepository); rdsClient = new RDSClient(awsRdsClient); }
/** * @inheritDoc */ @Override public void createSnapshot( String snapshotName, String instanceName, Identity identity ) { AmazonRDS rds = ActivityUtils.createClient( AmazonRDSClient.class, identity ); try { rds.describeDBSnapshots( new DescribeDBSnapshotsRequest().withDBSnapshotIdentifier( snapshotName ) ); return; } catch ( DBSnapshotNotFoundException e ) { } rds.createDBSnapshot( new CreateDBSnapshotRequest( snapshotName, instanceName ) ); }
/** * @inheritDoc */ @Override public DatabaseInstance describeInstance( String instanceName, Identity identity ) { AmazonRDS rds = ActivityUtils.createClient( AmazonRDSClient.class, identity ); DescribeDBInstancesResult results = rds.describeDBInstances( new DescribeDBInstancesRequest().withDBInstanceIdentifier( instanceName ) ); if ( results.getDBInstances().isEmpty() ) { return null; } DBInstance result = results.getDBInstances().get( 0 ); return toDatabaseInstance( result ); }
/** * @inheritDoc */ @Override public String getSnapshotStatus( String snapshotName, Identity identity ) { AmazonRDS rds = ActivityUtils.createClient( AmazonRDSClient.class, identity ); try { DescribeDBSnapshotsResult result = rds.describeDBSnapshots( new DescribeDBSnapshotsRequest().withDBSnapshotIdentifier( snapshotName ) ); return result.getDBSnapshots().get( 0 ).getStatus(); } catch ( DBSnapshotNotFoundException e ) { return null; } }
public static Set<String> getReplicaInstances( AmazonRDS rdsClient ) { DescribeDBClustersResult dbClusterResults = rdsClient.describeDBClusters(); Set<String> replicas = new HashSet<>(); for ( DBCluster cluster : dbClusterResults.getDBClusters() ) { for ( DBClusterMember member : cluster.getDBClusterMembers() ) { if ( !member.getIsClusterWriter() ) { replicas.add( member.getDBInstanceIdentifier() ); } } } return replicas; }
/** * Collect data for RDS. * * @param stats * current statistics object. * @param account * currently used credentials object. * @param region * currently used aws region. */ public static void scanRDS(AwsStats stats, AwsAccount account, Regions region) { LOG.debug("Scan for RDS in region " + region.getName() + " in account " + account.getAccountId()); try { AmazonRDS rds = new AmazonRDSClient(account.getCredentials()); rds.setRegion(Region.getRegion(region)); List<DBInstance> list = rds.describeDBInstances().getDBInstances(); int totalItems = list.size(); for (DBInstance dbInstance : list) { AwsResource res = new AwsResource(dbInstance.getDBName(), account.getAccountId(), AwsResourceType.RDS, region); res.addInfo("DBInstanceIdentifier", dbInstance.getDBInstanceIdentifier()); stats.add(res); } LOG.info(totalItems + " RDS instances in region " + region.getName() + " in account " + account.getAccountId()); } catch (AmazonServiceException ase) { LOG.error("Exception of RDS: " + ase.getMessage()); } }
@Test public void getObject_instanceWithTagsConfiguredWithCustomResourceResolverAndCustomRegion_mapWithTagsReturned() throws Exception { //Arrange AmazonRDS amazonRds = mock(AmazonRDS.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); AmazonIdentityManagement amazonIdentityManagement = mock(AmazonIdentityManagement.class); AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean(amazonRds, "test", amazonIdentityManagement); factoryBean.setResourceIdResolver(resourceIdResolver); factoryBean.setRegion(Region.getRegion(Regions.EU_WEST_1)); when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("stack-test"); when(amazonIdentityManagement.getUser()).thenReturn(new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(amazonRds.listTagsForResource(new ListTagsForResourceRequest().withResourceName("arn:aws:rds:eu-west-1:1234567890:db:stack-test"))).thenReturn(new ListTagsForResourceResult().withTagList( new Tag().withKey("key1").withValue("value1"), new Tag().withKey("key2").withValue("value2") )); //Act factoryBean.afterPropertiesSet(); Map<String, String> userTagMap = factoryBean.getObject(); //Assert assertEquals("value1", userTagMap.get("key1")); assertEquals("value2", userTagMap.get("key2")); }
@Test public void getObject_instanceWithOutTags_emptyMapReturned() throws Exception { //Arrange AmazonRDS amazonRds = mock(AmazonRDS.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); AmazonIdentityManagement amazonIdentityManagement = mock(AmazonIdentityManagement.class); AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean(amazonRds, "test", amazonIdentityManagement); factoryBean.setResourceIdResolver(resourceIdResolver); factoryBean.setResourceIdResolver(resourceIdResolver); factoryBean.setRegion(Region.getRegion(Regions.EU_WEST_1)); when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("stack-test"); when(amazonIdentityManagement.getUser()).thenReturn(new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(amazonRds.listTagsForResource(new ListTagsForResourceRequest().withResourceName("arn:aws:rds:eu-west-1:1234567890:db:stack-test"))).thenReturn(new ListTagsForResourceResult()); //Act factoryBean.afterPropertiesSet(); Map<String, String> userTagMap = factoryBean.getObject(); //Assert assertTrue(userTagMap.isEmpty()); }
@Test public void getObject_instanceWithTagsAndNoResourceIdResolverAndDefaultRegion_mapWithTagsReturned() throws Exception { //Arrange AmazonRDS amazonRds = mock(AmazonRDS.class); AmazonIdentityManagement amazonIdentityManagement = mock(AmazonIdentityManagement.class); AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean(amazonRds, "test", amazonIdentityManagement); when(amazonIdentityManagement.getUser()).thenReturn(new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(amazonRds.listTagsForResource(new ListTagsForResourceRequest().withResourceName("arn:aws:rds:us-west-2:1234567890:db:test"))).thenReturn(new ListTagsForResourceResult().withTagList( new Tag().withKey("key1").withValue("value1"), new Tag().withKey("key2").withValue("value2") )); //Act factoryBean.afterPropertiesSet(); Map<String, String> userTagMap = factoryBean.getObject(); //Assert assertEquals("value1", userTagMap.get("key1")); assertEquals("value2", userTagMap.get("key2")); }
@Test public void afterPropertiesSet_instanceWithoutReadReplica_createsNoDataSourceRouter() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); AmazonRdsReadReplicaAwareDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsReadReplicaAwareDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(mock(DataSource.class)); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert DataSource datasource = amazonRdsDataSourceFactoryBean.getObject(); assertNotNull(datasource); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
@Test public void newInstance_withResourceIdResolver_createsInstanceWithResolvedName() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); DataSource dataSource = mock(DataSource.class); when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("bar"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("bar"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("bar"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(dataSource); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); amazonRdsDataSourceFactoryBean.setResourceIdResolver(resourceIdResolver); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert assertNotNull(amazonRdsDataSourceFactoryBean.getObject()); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
@Test public void afterPropertiesSet_noUserNameSet_createsInstanceWithUserNameFromMetaData() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); DataSource dataSource = mock(DataSource.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(dataSource); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert DataSource datasource = amazonRdsDataSourceFactoryBean.getObject(); assertNotNull(datasource); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
@Test public void destroyInstance_shutdownInitiated_destroysDynamicDataSource() throws Exception { AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); DataSource dataSource = mock(DataSource.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(dataSource); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); amazonRdsDataSourceFactoryBean.afterPropertiesSet(); amazonRdsDataSourceFactoryBean.getObject(); amazonRdsDataSourceFactoryBean.destroy(); verify(dataSourceFactory, times(1)).closeDataSource(dataSource); }
@Test public void afterPropertiesSet_customUserNameSet_createsInstanceWithCustomUserNameAndIgnoresMetaDataUserName() throws Exception { AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setUsername("superAdmin"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); amazonRdsDataSourceFactoryBean.afterPropertiesSet(); amazonRdsDataSourceFactoryBean.getObject(); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "superAdmin", "secret")); }
@Bean public AmazonRDS amazonRDS() { AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("read1"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("read1"). withDBInstanceIdentifier("read1"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); return client; }
@Test public void parseInternal_customRegionConfigured_createsAmazonRdsClientWithCustomRegionConfigured() throws Exception { //Arrange ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-customRegion.xml", getClass()); //Act AmazonRDS amazonRDS = classPathXmlApplicationContext.getBean(AmazonRDS.class); //Assert assertEquals("https://rds.eu-west-1.amazonaws.com", ReflectionTestUtils.getField(amazonRDS, "endpoint").toString()); }
@Test public void parseInternal_customRegionProviderConfigured_createAmazonRdsClientWithCustomRegionConfigured() throws Exception { //Arrange ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-customRegionProvider.xml", getClass()); //Act AmazonRDS amazonRDS = classPathXmlApplicationContext.getBean(AmazonRDS.class); //Assert assertEquals("https://rds.eu-west-1.amazonaws.com", ReflectionTestUtils.getField(amazonRDS, "endpoint").toString()); }
@Test public void parseInternal_minimalConfiguration_createsBeanDefinitionWithoutReadReplicas() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonRDS.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-minimal.xml", getClass())); AmazonRDS client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), AmazonRDS.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); //Act DataSource dataSource = beanFactory.getBean(DataSource.class); //Assert assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); }
@Test public void parseInternal_noCredentialsDefined_returnsClientWithDefaultCredentialsProvider() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonRDS.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-noCredentials.xml", getClass())); AmazonRDS client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), AmazonRDS.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); //Act DataSource dataSource = beanFactory.getBean(DataSource.class); //Assert assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); }
@Test public void parseInternal_userTagsDefined_createsUserTagBeanDefinition() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonRDS.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); BeanDefinitionBuilder identityBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); identityBuilder.setFactoryMethod("mock"); identityBuilder.addConstructorArgValue(AmazonIdentityManagement.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonIdentityManagement.class.getName()), identityBuilder.getBeanDefinition()); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-userTags.xml", getClass())); AmazonRDS client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), AmazonRDS.class); AmazonIdentityManagement amazonIdentityManagement = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonIdentityManagement.class.getName()), AmazonIdentityManagement.class); when(amazonIdentityManagement.getUser()).thenReturn(new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(client.listTagsForResource(new ListTagsForResourceRequest().withResourceName("arn:aws:rds:us-west-2:1234567890:db:test"))).thenReturn(new ListTagsForResourceResult().withTagList( new Tag().withKey("key1").withValue("value2") )); //Act Map<?, ?> dsTags = beanFactory.getBean("dsTags", Map.class); //Assert assertEquals("value2", dsTags.get("key1")); }
@Test public void canRetry_noExceptionRegistered_returnsTrue() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "test"); RetryContext retryContext = new RetryContextSupport(null); //Act policy.open(retryContext); //Assert assertTrue(policy.canRetry(retryContext)); }
@Bean public AmazonRDS amazonRDS() { AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("anotherOne"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("anotherOne"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); return client; }
/** * @inheritDoc */ @Override public void deleteSnapshot( String snapshotName, Identity identity ) { AmazonRDS rds = ActivityUtils.createClient( AmazonRDSClient.class, identity ); try { rds.deleteDBSnapshot( new DeleteDBSnapshotRequest( snapshotName ) ); } catch ( DBSnapshotNotFoundException e ) { } }
/** * @inheritDoc */ @Override public void terminateInstance( String instanceName, Identity identity ) { AmazonRDS rds = ActivityUtils.createClient( AmazonRDSClient.class, identity ); try { rds.deleteDBInstance( new DeleteDBInstanceRequest( instanceName ).withSkipFinalSnapshot( true ) ); } catch ( DBInstanceNotFoundException e ) { } }
@Bean @Scope(WebApplicationContext.SCOPE_APPLICATION) public AmazonRDS rdsClient(final AWSCredentialsProvider creds, final Region region) { return region.createClient(AmazonRDSClient.class, creds, null); }
public AmazonRdsDataSourceUserTagsFactoryBean(AmazonRDS amazonRds, String dbInstanceIdentifier, AmazonIdentityManagement identityManagement) { this.amazonRds = amazonRds; this.dbInstanceIdentifier = dbInstanceIdentifier; this.identityManagement = identityManagement; }