Search in sources :

Example 6 with AmazonRDS

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

the class DatabaseInstanceStatusRetryPolicyTest method canRetry_withResourceIdResolver_returnsTrue.

@Test
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
    assertThat(policy.canRetry(retryContext)).isTrue();
    policy.close(retryContext);
}
Also used : TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) ResourceIdResolver(io.awspring.cloud.core.env.ResourceIdResolver) DBInstance(com.amazonaws.services.rds.model.DBInstance) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) RetryContext(org.springframework.retry.RetryContext) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) RetryContextSupport(org.springframework.retry.context.RetryContextSupport) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 7 with AmazonRDS

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

the class DatabaseInstanceStatusRetryPolicyTest method canRetry_retryNotPossibleDueToNoDatabase_returnsFalse.

@Test
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
    assertThat(policy.canRetry(retryContext)).isFalse();
    policy.close(retryContext);
}
Also used : DBInstanceNotFoundException(com.amazonaws.services.rds.model.DBInstanceNotFoundException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) RetryContext(org.springframework.retry.RetryContext) RetryContextSupport(org.springframework.retry.context.RetryContextSupport) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 8 with AmazonRDS

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

the class DatabaseInstanceStatusRetryPolicyTest method canRetry_multipleDatabasesFoundForInstanceIdentifier_reportsException.

@Test
void canRetry_multipleDatabasesFoundForInstanceIdentifier_reportsException() throws Exception {
    // Arrange
    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
    assertThatThrownBy(() -> policy.canRetry(retryContext)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Multiple databases found for same identifier");
}
Also used : TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) DBInstance(com.amazonaws.services.rds.model.DBInstance) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) RetryContext(org.springframework.retry.RetryContext) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) RetryContextSupport(org.springframework.retry.context.RetryContextSupport) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) Test(org.junit.jupiter.api.Test)

Example 9 with AmazonRDS

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

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_fullConfiguration_createsBeanDefinitionWithoutReadReplicas.

@Test
void parseInternal_fullConfiguration_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() + "-fullConfiguration.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").withEndpoint(new Endpoint().withAddress("localhost").withPort(3306))));
    BeanDefinition definition = beanFactory.getBeanDefinition("test");
    assertThat(definition.getConstructorArgumentValues().getArgumentValue(1, String.class).getValue()).isEqualTo("test");
    assertThat(definition.getConstructorArgumentValues().getArgumentValue(2, String.class).getValue()).isEqualTo("password");
    assertThat(definition.getPropertyValues().getPropertyValue("username").getValue()).isEqualTo("myUser");
    assertThat(definition.getPropertyValues().getPropertyValue("databaseName").getValue()).isEqualTo("fooDb");
    DataSource dataSource = beanFactory.getBean(DataSource.class);
    // Assert
    assertThat(dataSource).isNotNull();
    assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
}
Also used : 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) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ClassPathResource(org.springframework.core.io.ClassPathResource) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) DataSource(javax.sql.DataSource) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Endpoint(com.amazonaws.services.rds.model.Endpoint) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) Test(org.junit.jupiter.api.Test)

Example 10 with AmazonRDS

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

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_customRegionConfigured_amazonRdsClientWithCustomRegionConfigured.

@Test
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
    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)

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