Search in sources :

Example 11 with AmazonRDS

use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_custRegionProviderConf_amazRdsClientWithCustomRegionConfThatIsReturnedFromRegionProvider.

@Test
void parseInternal_custRegionProviderConf_amazRdsClientWithCustomRegionConfThatIsReturnedFromRegionProvider() 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
    assertThat(ReflectionTestUtils.getField(amazonRDS, "endpoint").toString()).isEqualTo("https://rds.eu-west-1.amazonaws.com");
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 12 with AmazonRDS

use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_minimalConfiguration_createsBeanDefinitionWithoutReadReplicas.

@Test
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
    assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DBInstance(com.amazonaws.services.rds.model.DBInstance) Endpoint(com.amazonaws.services.rds.model.Endpoint) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 13 with AmazonRDS

use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_userTagsDefined_createsUserTagBeanDefinition.

@Test
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
    assertThat(dsTags.get("key1")).isEqualTo("value2");
}
Also used : GetUserResult(com.amazonaws.services.identitymanagement.model.GetUserResult) User(com.amazonaws.services.identitymanagement.model.User) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) ClassPathResource(org.springframework.core.io.ClassPathResource) Date(java.util.Date) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) Tag(com.amazonaws.services.rds.model.Tag) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) Test(org.junit.jupiter.api.Test)

Example 14 with AmazonRDS

use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_customRdsInstance_createsRdsBeanAndUserTagsWithCustomRdsInstance.

@Test
void parseInternal_customRdsInstance_createsRdsBeanAndUserTagsWithCustomRdsInstance() throws Exception {
    // Arrange
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-customRdsInstance.xml", getClass()));
    AmazonRDS clientMock = beanFactory.getBean("amazonRds", AmazonRDS.class);
    when(clientMock.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")));
    AmazonIdentityManagement amazonIdentityManagement = beanFactory.getBean("myIdentityService", AmazonIdentityManagement.class);
    when(amazonIdentityManagement.getUser()).thenReturn(new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date())));
    when(clientMock.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);
    DataSource dataSource = beanFactory.getBean(DataSource.class);
    // Assert
    assertThat(dsTags.get("key1")).isEqualTo("value2");
    assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
}
Also used : GetUserResult(com.amazonaws.services.identitymanagement.model.GetUserResult) User(com.amazonaws.services.identitymanagement.model.User) DBInstance(com.amazonaws.services.rds.model.DBInstance) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) ClassPathResource(org.springframework.core.io.ClassPathResource) Date(java.util.Date) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) DataSource(javax.sql.DataSource) Endpoint(com.amazonaws.services.rds.model.Endpoint) ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) Tag(com.amazonaws.services.rds.model.Tag) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) Test(org.junit.jupiter.api.Test)

Example 15 with AmazonRDS

use of com.amazonaws.services.rds.AmazonRDS in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_noCredentialsDefined_returnsClientWithDefaultCredentialsProvider.

@Test
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
    assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DBInstance(com.amazonaws.services.rds.model.DBInstance) Endpoint(com.amazonaws.services.rds.model.Endpoint) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) DataSource(javax.sql.DataSource) 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