Search in sources :

Example 11 with AmazonElastiCache

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

the class CacheBeanDefinitionParserTest method parseInternal_mixedCacheConfig_returnsBothCaches.

@Test
void parseInternal_mixedCacheConfig_returnsBothCaches() 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 cacheBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class);
    cacheBuilder.setFactoryMethod("mock");
    cacheBuilder.addConstructorArgValue(AmazonElastiCache.class);
    beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), cacheBuilder.getBeanDefinition());
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
    reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-mixedCacheConfig.xml", getClass()));
    AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class);
    // Replay invocation that will be called
    DescribeCacheClustersRequest memcached1 = new DescribeCacheClustersRequest().withCacheClusterId("memcached");
    memcached1.setShowCacheNodeInfo(true);
    when(client.describeCacheClusters(memcached1)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withCacheClusterId("memcached").withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))).withCacheClusterStatus("available").withEngine("memcached")));
    Cache cache = beanFactory.getBean("memc", Cache.class);
    when(cache.getName()).thenReturn("memc");
    // Act
    CacheManager cacheManager = beanFactory.getBean(CacheManager.class);
    Cache memc = cacheManager.getCache("memc");
    Cache memcached = cacheManager.getCache("memcached");
    // Assert
    assertThat(cacheManager).isNotNull();
    assertThat(memcached).isNotNull();
    memc.put("foo", "bar");
    memc.evict("foo");
    memcached.put("foo", "bar");
    memcached.evict("foo");
}
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)

Aggregations

AmazonElastiCache (com.amazonaws.services.elasticache.AmazonElastiCache)11 Test (org.junit.jupiter.api.Test)11 CacheCluster (com.amazonaws.services.elasticache.model.CacheCluster)8 DescribeCacheClustersRequest (com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest)8 DescribeCacheClustersResult (com.amazonaws.services.elasticache.model.DescribeCacheClustersResult)8 Endpoint (com.amazonaws.services.elasticache.model.Endpoint)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 ClientConfiguration (com.amazonaws.ClientConfiguration)3 ResourceIdResolver (io.awspring.cloud.core.env.ResourceIdResolver)2 CacheNode (com.amazonaws.services.elasticache.model.CacheNode)1