package org.sagebionetworks.bridge.cache;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.bridge.dynamodb.DynamoStudy;
import org.sagebionetworks.bridge.json.BridgeObjectMapper;
import org.sagebionetworks.bridge.models.studies.Study;
import org.sagebionetworks.bridge.redis.JedisStringOps;

import com.google.common.collect.Maps;

public class CacheProviderTest {

    private CacheProvider cacheProvider;
    
    @Before
    public void before() {
        cacheProvider = new CacheProvider();
        cacheProvider.setStringOps(getSimpleStringOps());
    }
    
    @Test
    public void addAndRemoveViewFromCacheProvider() throws Exception {
        Study study = new DynamoStudy();
        study.setIdentifier("test");
        study.setName("This is a test study");
        String json = BridgeObjectMapper.get().writeValueAsString(study);
        assertTrue(json != null && json.length() > 0);
        
        String cacheKey = study.getIdentifier() + ":Study";
        
        cacheProvider.setString(cacheKey, json);
        
        String cachedString = cacheProvider.getString(cacheKey);
        assertEquals(json, cachedString);
        
        // Remove something that's not the key
        cacheProvider.removeString(cacheKey+"2");
        cachedString = cacheProvider.getString(cacheKey);
        assertEquals(json, cachedString);
        
        cacheProvider.removeString(cacheKey);
        cachedString = cacheProvider.getString(cacheKey);
        assertNull(cachedString);
    }
    
    
    private JedisStringOps getSimpleStringOps() {
        return new JedisStringOps() {
            private Map<String,String> map = Maps.newHashMap();
            public Long expire(final String key, final int seconds) {
                return 1L;
            }
            public String setex(final String key, final int seconds, final String value) {
                map.put(key, value);
                return "OK";
            }
            public Long setnx(final String key, final String value) {
                map.put(key, value);
                return 1L;
            }
            public String get(final String key) {
                return map.get(key);
            }
            public Long delete(final String key) {
                map.remove(key);
                return 1L;
            }
        };   
    }    
}

@@ -81,16 +81,106 @@
 Equals;%0A
+import static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNotNull;%0A
 import s
@@ -329,16 +329,119 @@
 t.Test;%0A
+import org.sagebionetworks.bridge.TestConstants;%0Aimport org.sagebionetworks.bridge.config.Environment;%0A
 import o
@@ -592,21 +592,258 @@
 els.
-studies.Study
+User;%0Aimport org.sagebionetworks.bridge.models.UserSession;%0Aimport org.sagebionetworks.bridge.models.studies.Study;%0Aimport org.sagebionetworks.bridge.models.studies.StudyIdentifier;%0Aimport org.sagebionetworks.bridge.models.studies.StudyIdentifierImpl
 ;%0Aim
@@ -895,16 +895,66 @@
 ringOps;
+%0Aimport org.sagebionetworks.bridge.redis.RedisKey;
 %0A%0Aimport
@@ -1062,20 +1062,16 @@
 ovider;%0A
-    
 %0A    @Be
@@ -1207,28 +1207,865 @@
 s());%0A    %7D%0A
+%0A
     
+@Test%0A    public void testUserSession() throws Exception %7B%0A        User user = new User();%0A        user.setEmail(%22email%22);%0A        user.setId(%22id%22);%0A        final String healthCode = %22some health code%22;%0A        user.setHealthCode(healthCode);%0A        UserSession session = new UserSession();%0A        session.setUser(user);%0A        String key = getClass().getName() + %22userSession%22;%0A        cacheProvider.setUserSession(key, session);%0A        String cached = cacheProvider.getString(RedisKey.SESSION.getRedisKey(key));%0A        assertNotNull(cached);%0A        assertFalse(%22Health code should be encrypted%22, cached.contains(healthCode));%0A        session = cacheProvider.getUserSession(key);%0A        assertNotNull(session);%0A        assertNotNull(session.getUser());%0A        assertEquals(healthCode, session.getUser().getHealthCode());%0A    %7D%0A
 %0A    @Test%0A 

Add test for user session