use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.
the class AmazonRdsDataSourceFactoryBeanTest method destroyInstance_shutdownInitiated_destroysDynamicDataSource.
@Test
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);
}
use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.
the class AmazonRdsDataSourceFactoryBeanTest method afterPropertiesSet_noUserNameSet_createsInstanceWithUserNameFromMetaData.
@Test
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();
assertThat(datasource).isNotNull();
verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"));
}
use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.
the class AmazonRdsDataSourceFactoryBeanTest method afterPropertiesSet_noInstanceFound_reportsIllegalStateException.
@Test
void afterPropertiesSet_noInstanceFound_reportsIllegalStateException() throws Exception {
// Arrange
AmazonRDS amazonRDS = mock(AmazonRDS.class);
when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenThrow(new DBInstanceNotFoundException("foo"));
AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "foo");
// Assert
assertThatThrownBy(amazonRdsDataSourceFactoryBean::afterPropertiesSet).isInstanceOf(IllegalStateException.class).hasMessageContaining("No database instance with id:'test'");
}
use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.
the class AmazonRdsDataSourceUserTagsFactoryBeanTest method getObject_instanceWithTagsAndNoResourceIdResolverAndDefaultRegion_mapWithTagsReturned.
@Test
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
assertThat(userTagMap.get("key1")).isEqualTo("value1");
assertThat(userTagMap.get("key2")).isEqualTo("value2");
}
use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.
the class AmazonRdsReadReplicaAwareDataSourceFactoryBeanTest method afterPropertiesSet_instanceWithReadReplica_createsDataSourceRouter.
@Test
void afterPropertiesSet_instanceWithReadReplica_createsDataSourceRouter() 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)).withReadReplicaDBInstanceIdentifiers("read1", "read2")));
when(amazonRDS.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))));
when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("read2"))).thenReturn(new DescribeDBInstancesResult().withDBInstances(new DBInstance().withDBInstanceStatus("available").withDBName("read2").withDBInstanceIdentifier("read2").withEngine("mysql").withMasterUsername("admin").withEndpoint(new Endpoint().withAddress("localhost").withPort(3306))));
DataSource createdDataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(createdDataSource);
when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "read1", "admin", "secret"))).thenReturn(createdDataSource);
when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "read2", "admin", "secret"))).thenReturn(createdDataSource);
when(createdDataSource.getConnection()).thenReturn(connection);
AmazonRdsReadReplicaAwareDataSourceFactoryBean factoryBean = new AmazonRdsReadReplicaAwareDataSourceFactoryBean(amazonRDS, "test", "secret");
factoryBean.setDataSourceFactory(dataSourceFactory);
// Act
factoryBean.afterPropertiesSet();
// Assert
DataSource datasource = factoryBean.getObject();
assertThat(datasource).isNotNull();
assertThat(datasource instanceof LazyConnectionDataSourceProxy).isTrue();
ReadOnlyRoutingDataSource source = (ReadOnlyRoutingDataSource) ((LazyConnectionDataSourceProxy) datasource).getTargetDataSource();
assertThat(source.getDataSources().size()).isEqualTo(3);
}
Aggregations