Search in sources :

Example 6 with DescribeCacheClustersResult

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

the class ElastiCacheFactoryBeanTest method getObject_availableClusterWithLogicalName_returnsConfigurationMemcachedClientWithPhysicalName.

@Test
void getObject_availableClusterWithLogicalName_returnsConfigurationMemcachedClientWithPhysicalName() throws Exception {
    // Arrange
    AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class);
    DescribeCacheClustersRequest testCache = new DescribeCacheClustersRequest().withCacheClusterId("testCache");
    testCache.setShowCacheNodeInfo(true);
    when(amazonElastiCache.describeCacheClusters(testCache)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(45678)).withCacheClusterStatus("available").withEngine("memcached")));
    ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class);
    when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("testCache");
    ElastiCacheFactoryBean elastiCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "test", resourceIdResolver, Collections.<CacheFactory>singletonList(new TestCacheFactory("test", "localhost", 45678)));
    // Act
    elastiCacheFactoryBean.afterPropertiesSet();
    Cache cache = elastiCacheFactoryBean.getObject();
    // Assert
    assertThat(cache).isNotNull();
}
Also used : Endpoint(com.amazonaws.services.elasticache.model.Endpoint) ResourceIdResolver(io.awspring.cloud.core.env.ResourceIdResolver) 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) Cache(org.springframework.cache.Cache) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) Test(org.junit.jupiter.api.Test)

Example 7 with DescribeCacheClustersResult

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

the class ElastiCacheFactoryBeanTest method getObject_availableCluster_returnsConfiguredMemcachedClient.

@Test
void getObject_availableCluster_returnsConfiguredMemcachedClient() throws Exception {
    // Arrange
    AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class);
    DescribeCacheClustersRequest testCache = new DescribeCacheClustersRequest().withCacheClusterId("testCache");
    testCache.setShowCacheNodeInfo(true);
    when(amazonElastiCache.describeCacheClusters(testCache)).thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(45678)).withCacheClusterStatus("available").withEngine("memcached")));
    ElastiCacheFactoryBean elasticCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "testCache", Collections.singletonList(new TestCacheFactory("testCache", "localhost", 45678)));
    // Act
    elasticCacheFactoryBean.afterPropertiesSet();
    Cache cache = elasticCacheFactoryBean.getObject();
    // Assert
    assertThat(cache).isNotNull();
}
Also used : 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) Cache(org.springframework.cache.Cache) AmazonElastiCache(com.amazonaws.services.elasticache.AmazonElastiCache) Test(org.junit.jupiter.api.Test)

Example 8 with DescribeCacheClustersResult

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

the class ElastiCacheFactoryBean method createInstance.

@Override
protected Cache createInstance() throws Exception {
    DescribeCacheClustersRequest describeCacheClustersRequest = new DescribeCacheClustersRequest().withCacheClusterId(getCacheClusterName());
    describeCacheClustersRequest.setShowCacheNodeInfo(true);
    DescribeCacheClustersResult describeCacheClustersResult = this.amazonElastiCache.describeCacheClusters(describeCacheClustersRequest);
    CacheCluster cacheCluster = describeCacheClustersResult.getCacheClusters().get(0);
    if (!"available".equals(cacheCluster.getCacheClusterStatus())) {
        LOGGER.warn("Cache cluster is not available now. Connection may fail during cache access. Current status is {}", cacheCluster.getCacheClusterStatus());
    }
    Endpoint configurationEndpoint = getEndpointForCache(cacheCluster);
    for (CacheFactory cacheFactory : this.cacheFactories) {
        if (cacheFactory.isSupportingCacheArchitecture(cacheCluster.getEngine())) {
            return cacheFactory.createCache(this.cacheClusterId, configurationEndpoint.getAddress(), configurationEndpoint.getPort());
        }
    }
    throw new IllegalArgumentException("No CacheFactory configured for engine: " + cacheCluster.getEngine());
}
Also used : Endpoint(com.amazonaws.services.elasticache.model.Endpoint) DescribeCacheClustersResult(com.amazonaws.services.elasticache.model.DescribeCacheClustersResult) DescribeCacheClustersRequest(com.amazonaws.services.elasticache.model.DescribeCacheClustersRequest) CacheCluster(com.amazonaws.services.elasticache.model.CacheCluster)

Example 9 with DescribeCacheClustersResult

use of com.amazonaws.services.elasticache.model.DescribeCacheClustersResult 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

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