Search in sources :

Example 1 with DescribeCacheClustersRequest

use of com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest in project spring-cloud-aws by awspring.

the class CacheBeanDefinitionParserTest method parseInternal_clusterCacheConfiguration_returnsConfiguredClusterCache.

@Test
void parseInternal_clusterCacheConfiguration_returnsConfiguredClusterCache() throws Exception {
    // Arrange
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // Register a mock object which will be used to replay service calls
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class);
    beanDefinitionBuilder.setFactoryMethod("mock");
    beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class);
    beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition());
    // Load xml file
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfig.xml", getClass()));
    AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class);
    // Replay invocation that will be called
    DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached");
    memcached.setShowCacheNodeInfo(true);
    when(client.describeCacheClusters(memcached)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withCacheClusterId("memcached").withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))).withCacheClusterStatus("available").withEngine("memcached")));
    // Act
    CacheManager cacheManager = beanFactory.getBean(CacheManager.class);
    Cache cache = cacheManager.getCache("memcached");
    cache.put("foo", "bar");
    cache.evict("foo");
    // Assert
    assertThat(cacheManager).isNotNull();
    assertThat(cache).isNotNull();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Endpoint(com.amazonaws.services.elasticache.model.Endpoint) DescribeCacheClustersResult(com.amazonaws.services.elasticache.model.DescribeCacheClustersResult) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) DescribeCacheClustersRequest(com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest) CacheManager(org.springframework.cache.CacheManager) CacheCluster(com.amazonaws.services.elasticache.model.CacheCluster) ClassPathResource(org.springframework.core.io.ClassPathResource) Cache(org.springframework.cache.Cache) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) Test(org.junit.jupiter.api.Test)

Example 2 with DescribeCacheClustersRequest

use of com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest in project spring-cloud-aws by awspring.

the class CacheBeanDefinitionParserTest method parseInternal_clusterCacheConfigurationWithCustomElastiCacheClient_returnsConfigurationWithCustomClient.

@Test
void parseInternal_clusterCacheConfigurationWithCustomElastiCacheClient_returnsConfigurationWithCustomClient() throws Exception {
    // Arrange
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // Load xml file
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfigWithCustomElastiCacheClient.xml", getClass()));
    AmazonElastiCache amazonElastiCacheMock = beanFactory.getBean("customClient", AmazonElastiCache.class);
    DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached");
    memcached.setShowCacheNodeInfo(true);
    when(amazonElastiCacheMock.describeCacheClusters(memcached)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withCacheClusterId("memcached").withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))).withCacheClusterStatus("available").withEngine("memcached")));
    // Act
    CacheManager cacheManager = beanFactory.getBean(CacheManager.class);
    Cache cache = cacheManager.getCache("memcached");
    cache.put("foo", "bar");
    cache.evict("foo");
    // Assert
    assertThat(cacheManager).isNotNull();
    assertThat(cache).isNotNull();
}
Also used : Endpoint(com.amazonaws.services.elasticache.model.Endpoint) DescribeCacheClustersResult(com.amazonaws.services.elasticache.model.DescribeCacheClustersResult) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) DescribeCacheClustersRequest(com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest) CacheManager(org.springframework.cache.CacheManager) CacheCluster(com.amazonaws.services.elasticache.model.CacheCluster) ClassPathResource(org.springframework.core.io.ClassPathResource) Cache(org.springframework.cache.Cache) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) Test(org.junit.jupiter.api.Test)

Example 3 with DescribeCacheClustersRequest

use of com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest in project spring-cloud-aws by awspring.

the class CacheBeanDefinitionParserTest method parseInternal_cacheConfigWithExpiration_returnsConfiguredCacheThatRespectExpiration.

@Test
void parseInternal_cacheConfigWithExpiration_returnsConfiguredCacheThatRespectExpiration() throws Exception {
    // Arrange
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // Register a mock object which will be used to replay service calls
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class);
    beanDefinitionBuilder.setFactoryMethod("mock");
    beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class);
    beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition());
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
    reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-cacheConfigWithExpiration.xml", getClass()));
    AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class);
    // Replay invocation that will be called
    DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached");
    memcached.setShowCacheNodeInfo(true);
    when(client.describeCacheClusters(memcached)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withCacheClusterId("memcached").withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))).withCacheClusterStatus("available").withEngine("memcached")));
    // Act
    CacheManager cacheManager = beanFactory.getBean(CacheManager.class);
    Cache cache = cacheManager.getCache("memcached");
    cache.put("foo", "bar");
    Thread.sleep(2000);
    // Assert
    assertThat(cacheManager).isNotNull();
    assertThat(cache).isNotNull();
    assertThat(cache.get("foo")).isNull();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Endpoint(com.amazonaws.services.elasticache.model.Endpoint) DescribeCacheClustersResult(com.amazonaws.services.elasticache.model.DescribeCacheClustersResult) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) DescribeCacheClustersRequest(com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest) CacheManager(org.springframework.cache.CacheManager) CacheCluster(com.amazonaws.services.elasticache.model.CacheCluster) ClassPathResource(org.springframework.core.io.ClassPathResource) Cache(org.springframework.cache.Cache) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) Test(org.junit.jupiter.api.Test)

Example 4 with DescribeCacheClustersRequest

use of com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest in project spring-cloud-aws by awspring.

the class CacheBeanDefinitionParserTest method parseInternal_clusterCacheConfigurationWithLogicalName_returnsConfiguredClusterCacheWithPhysicalName.

@Test
void parseInternal_clusterCacheConfigurationWithLogicalName_returnsConfiguredClusterCacheWithPhysicalName() throws Exception {
    // Arrange
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // Register a mock object which will be used to replay service calls
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class);
    beanDefinitionBuilder.setFactoryMethod("mock");
    beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class);
    beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition());
    BeanDefinitionBuilder resourceIdBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class);
    resourceIdBuilder.setFactoryMethod("mock");
    resourceIdBuilder.addConstructorArgValue(ResourceIdResolver.class);
    beanFactory.registerBeanDefinition(GlobalBeanDefinitionUtils.RESOURCE_ID_RESOLVER_BEAN_NAME, resourceIdBuilder.getBeanDefinition());
    // Load xml file
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfigStackConfigured.xml", getClass()));
    AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class);
    ResourceIdResolver resourceIdResolver = beanFactory.getBean(GlobalBeanDefinitionUtils.RESOURCE_ID_RESOLVER_BEAN_NAME, ResourceIdResolver.class);
    when(resourceIdResolver.resolveToPhysicalResourceId("testMemcached")).thenReturn("memcached");
    // Replay invocation that will be called
    DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached");
    memcached.setShowCacheNodeInfo(true);
    when(client.describeCacheClusters(memcached)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withCacheClusterId("memcached").withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))).withCacheClusterStatus("available").withEngine("memcached")));
    // Act
    CacheManager cacheManager = beanFactory.getBean(CacheManager.class);
    Cache cache = cacheManager.getCache("testMemcached");
    cache.put("foo", "bar");
    cache.evict("foo");
    // Assert
    assertThat(cacheManager).isNotNull();
    assertThat(cache).isNotNull();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ResourceIdResolver(io.awspring.cloud.core.env.ResourceIdResolver) Endpoint(com.amazonaws.services.elasticache.model.Endpoint) DescribeCacheClustersResult(com.amazonaws.services.elasticache.model.DescribeCacheClustersResult) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) DescribeCacheClustersRequest(com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest) CacheManager(org.springframework.cache.CacheManager) CacheCluster(com.amazonaws.services.elasticache.model.CacheCluster) ClassPathResource(org.springframework.core.io.ClassPathResource) Cache(org.springframework.cache.Cache) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) Test(org.junit.jupiter.api.Test)

Example 5 with DescribeCacheClustersRequest

use of com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest in project spring-cloud-aws by awspring.

the class ElastiCacheFactoryBeanTest method getObject_clusterWithRedisEngineConfigured_reportsError.

@Test
void getObject_clusterWithRedisEngineConfigured_reportsError() throws Exception {
    // Arrange
    AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class);
    DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached");
    memcached.setShowCacheNodeInfo(true);
    when(amazonElastiCache.describeCacheClusters(memcached)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withEngine("redis").withCacheNodes(new CacheNode().withEndpoint(new Endpoint().withAddress("localhost").withPort(45678)))));
    ElastiCacheFactoryBean elastiCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "memcached", Collections.singletonList(new TestCacheFactory("testCache", "localhost", 45678)));
    // Assert
    assertThatThrownBy(elastiCacheFactoryBean::afterPropertiesSet).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("engine");
}
Also used : CacheNode(com.amazonaws.services.elasticache.model.CacheNode) Endpoint(com.amazonaws.services.elasticache.model.Endpoint) DescribeCacheClustersResult(com.amazonaws.services.elasticache.model.DescribeCacheClustersResult) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) DescribeCacheClustersRequest(com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest) CacheCluster(com.amazonaws.services.elasticache.model.CacheCluster) Test(org.junit.jupiter.api.Test)

Aggregations

CacheCluster (com.amazonaws.services.elasticache.model.CacheCluster)9 DescribeCacheClustersRequest (com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest)9 DescribeCacheClustersResult (com.amazonaws.services.elasticache.model.DescribeCacheClustersResult)9 Endpoint (com.amazonaws.services.elasticache.model.Endpoint)9 AmazonElastiCache (com.amazonaws.services.elasticache.AmazonElastiCache)8 Test (org.junit.jupiter.api.Test)8 Cache (org.springframework.cache.Cache)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)5 CacheManager (org.springframework.cache.CacheManager)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)4 ResourceIdResolver (io.awspring.cloud.core.env.ResourceIdResolver)2 CacheNode (com.amazonaws.services.elasticache.model.CacheNode)1