Search in sources :

Example 1 with AmazonRDS

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);
}
Also used : DataSourceFactory(io.awspring.cloud.jdbc.datasource.DataSourceFactory) DBInstance(com.amazonaws.services.rds.model.DBInstance) Endpoint(com.amazonaws.services.rds.model.Endpoint) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DataSourceInformation(io.awspring.cloud.jdbc.datasource.DataSourceInformation) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DataSource(javax.sql.DataSource) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 2 with AmazonRDS

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"));
}
Also used : DataSourceFactory(io.awspring.cloud.jdbc.datasource.DataSourceFactory) DBInstance(com.amazonaws.services.rds.model.DBInstance) Endpoint(com.amazonaws.services.rds.model.Endpoint) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DataSourceInformation(io.awspring.cloud.jdbc.datasource.DataSourceInformation) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DataSource(javax.sql.DataSource) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 3 with AmazonRDS

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'");
}
Also used : DBInstanceNotFoundException(com.amazonaws.services.rds.model.DBInstanceNotFoundException) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 4 with AmazonRDS

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");
}
Also used : GetUserResult(com.amazonaws.services.identitymanagement.model.GetUserResult) User(com.amazonaws.services.identitymanagement.model.User) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) Tag(com.amazonaws.services.rds.model.Tag) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 5 with AmazonRDS

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);
}
Also used : DataSourceFactory(io.awspring.cloud.jdbc.datasource.DataSourceFactory) LazyConnectionDataSourceProxy(org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy) DBInstance(com.amazonaws.services.rds.model.DBInstance) Endpoint(com.amazonaws.services.rds.model.Endpoint) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) Connection(java.sql.Connection) ReadOnlyRoutingDataSource(io.awspring.cloud.jdbc.datasource.ReadOnlyRoutingDataSource) DataSourceInformation(io.awspring.cloud.jdbc.datasource.DataSourceInformation) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) DataSource(javax.sql.DataSource) ReadOnlyRoutingDataSource(io.awspring.cloud.jdbc.datasource.ReadOnlyRoutingDataSource) Test(org.junit.jupiter.api.Test)

Aggregations

AmazonRDS (com.amazonaws.services.rds.AmazonRDS)27 Test (org.junit.jupiter.api.Test)24 DBInstance (com.amazonaws.services.rds.model.DBInstance)15 DescribeDBInstancesRequest (com.amazonaws.services.rds.model.DescribeDBInstancesRequest)15 DescribeDBInstancesResult (com.amazonaws.services.rds.model.DescribeDBInstancesResult)14 Endpoint (com.amazonaws.services.rds.model.Endpoint)10 DataSource (javax.sql.DataSource)9 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)7 ClassPathResource (org.springframework.core.io.ClassPathResource)7 DataSourceFactory (io.awspring.cloud.jdbc.datasource.DataSourceFactory)6 DataSourceInformation (io.awspring.cloud.jdbc.datasource.DataSourceInformation)6 AmazonIdentityManagement (com.amazonaws.services.identitymanagement.AmazonIdentityManagement)5 GetUserResult (com.amazonaws.services.identitymanagement.model.GetUserResult)5 User (com.amazonaws.services.identitymanagement.model.User)5 ListTagsForResourceRequest (com.amazonaws.services.rds.model.ListTagsForResourceRequest)5 ListTagsForResourceResult (com.amazonaws.services.rds.model.ListTagsForResourceResult)5 Date (java.util.Date)5 RetryContext (org.springframework.retry.RetryContext)5 RetryContextSupport (org.springframework.retry.context.RetryContextSupport)5