package org.redisson.jcache;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.cache.Cache;
import javax.cache.Caching;
import javax.cache.configuration.FactoryBuilder;
import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.event.CacheEntryEvent;
import javax.cache.event.CacheEntryExpiredListener;
import javax.cache.event.CacheEntryListenerException;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;

import org.junit.Assert;
import org.junit.Test;
import org.redisson.BaseTest;
import org.redisson.api.RMap;

public class JCacheTest extends BaseTest {

    @Test
    public void testMapPutGet() throws InterruptedException, IllegalArgumentException, URISyntaxException {
        MutableConfiguration<String, String> config = new MutableConfiguration<>();
        config.setStoreByValue(true);
        
        URI configUri = getClass().getResource("redisson-jcache.json").toURI();
        Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null)
                .createCache("test", config);

        long startTime = System.nanoTime();
        cache.get("123");
        long spentTime = System.nanoTime() - startTime;
        System.out.println("get spentTime: " + spentTime);
    }

    @Test
    public void testMapPutGet2() throws InterruptedException, IllegalArgumentException, URISyntaxException {
        RMap<String, String> cache = redisson.getMap("test");
        
        long startTime = System.nanoTime();
        cache.put("123", "90");
        long spentTime = System.nanoTime() - startTime;
        System.out.println("put spentTime: " + spentTime);
        
        startTime = System.nanoTime();
        cache.get("123");
        spentTime = System.nanoTime() - startTime;
        System.out.println("get spentTime: " + spentTime);
    }
    
//    @Test
    public void testExpiration() throws InterruptedException, IllegalArgumentException, URISyntaxException {
        MutableConfiguration<String, String> config = new MutableConfiguration<>();
        config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)));
        config.setStoreByValue(true);
        
        URI configUri = getClass().getResource("redisson-jcache.json").toURI();
        Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null)
                .createCache("test", config);

        CountDownLatch latch = new CountDownLatch(1);
        
        String key = "123";
        ExpiredListener clientListener = new ExpiredListener(latch, key, "90");
        MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = 
                new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(clientListener), null, true, true);
        cache.registerCacheEntryListener(listenerConfiguration);

        cache.put(key, "90");
        Assert.assertNotNull(cache.get(key));
        
        latch.await();
        
        Assert.assertNull(cache.get(key));
    }
    
    public static class ExpiredListener implements CacheEntryExpiredListener<String, String>, Serializable {

        private Object key;
        private Object value;
        private CountDownLatch latch;
        
        public ExpiredListener(CountDownLatch latch, Object key, Object value) {
            super();
            this.latch = latch;
            this.key = key;
            this.value = value;
        }



        @Override
        public void onExpired(Iterable<CacheEntryEvent<? extends String, ? extends String>> events)
                throws CacheEntryListenerException {
            CacheEntryEvent<? extends String, ? extends String> entry = events.iterator().next();
            
            assertThat(entry.getKey()).isEqualTo(key);
            assertThat(entry.getValue()).isEqualTo(value);
            latch.countDown();
        }

        
    }
    
}

@@ -1549,586 +1549,8 @@
 est%0A
-    public void testMapPutGet2() throws InterruptedException, IllegalArgumentException, URISyntaxException %7B%0A        RMap%3CString, String%3E cache = redisson.getMap(%22test%22);%0A        %0A        long startTime = System.nanoTime();%0A        cache.put(%22123%22, %2290%22);%0A        long spentTime = System.nanoTime() - startTime;%0A        System.out.println(%22put spentTime: %22 + spentTime);%0A        %0A        startTime = System.nanoTime();%0A        cache.get(%22123%22);%0A        spentTime = System.nanoTime() - startTime;%0A        System.out.println(%22get spentTime: %22 + spentTime);%0A    %7D%0A    %0A//    @Test%0A
     

test fixed