Search in sources :

Example 21 with KeyInfo

use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class MockRedis method mockKeyInfoRedisTemplate.

public static KeyInfoRedisTemplate mockKeyInfoRedisTemplate() {
    KeyInfoRedisTemplate template = mock(KeyInfoRedisTemplate.class, Mockito.RETURNS_DEEP_STUBS);
    HashOperations keyInfoOps = mock(HashOperations.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(template.opsForHash()).thenReturn(keyInfoOps);
    // mock HashOperations get
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String subKey = (String) args[1];
        LOGGER.trace("KeyInfoRedisTemplate HashOperations get " + key + " " + subKey);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        Map<String, Object> subMap = (Map<String, Object>) map.get(subKey);
        return Utils.toPojo(subMap, KeyInfo.class);
    }).when(keyInfoOps).get(anyString(), anyString());
    // mock HashOperations put
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String subKey = (String) args[1];
        LOGGER.trace("KeyInfoRedisTemplate HashOperations put " + key + " " + subKey);
        KeyInfo keyInfo = (KeyInfo) args[2];
        Map<String, Object> subMap = Utils.toMap(keyInfo);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            map = new LinkedHashMap<>();
            data.put(key, map);
        }
        map.put(subKey, subMap);
        return null;
    }).when(keyInfoOps).put(anyString(), anyString(), any(KeyInfo.class));
    // mock HashOperations putAll
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        Map<String, Object> subMaps = (Map<String, Object>) args[1];
        LOGGER.trace("KeyInfoRedisTemplate HashOperations putAll " + key + " " + subMaps.keySet());
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            map = new LinkedHashMap<>();
            data.put(key, map);
        }
        for (Map.Entry<String, Object> entry : subMaps.entrySet()) {
            String subKey = entry.getKey();
            KeyInfo keyInfo = (KeyInfo) entry.getValue();
            map.put(subKey, Utils.toMap(keyInfo));
        }
        return null;
    }).when(keyInfoOps).putAll(anyString(), anyMap());
    // mock HashOperations multiGet
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        List<String> keys = (List<String>) args[1];
        LOGGER.trace("KeyInfoRedisTemplate HashOperations multiGet " + key);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        List<KeyInfo> resultList = new ArrayList<>();
        if (map == null) {
            return resultList;
        }
        for (String subKey : keys) {
            Map<String, Object> subMap = (Map<String, Object>) map.get(subKey);
            if (subMap != null) {
                KeyInfo keyInfo = Utils.toPojo(subMap, KeyInfo.class);
                resultList.add(keyInfo);
            } else {
                resultList.add(null);
            }
        }
        return resultList;
    }).when(keyInfoOps).multiGet(anyString(), anyList());
    // mock HashOperations delete single
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        String subKey = (String) args[1];
        LOGGER.trace("KeyInfoRedisTemplate HashOperations delete " + key + " " + subKey);
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        map.remove(subKey);
        return null;
    }).when(keyInfoOps).delete(anyString(), anyString());
    // mock HashOperations delete multiple
    // 
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String key = (String) args[0];
        Map<String, Object> map = (Map<String, Object>) data.get(key);
        if (map == null) {
            return null;
        }
        List<String> keys = (List<String>) args[1];
        LOGGER.trace("KeyInfoRedisTemplate HashOperations delete " + key + " " + keys);
        for (String subKey : keys) {
            map.remove(subKey);
        }
        return null;
    }).when(keyInfoOps).delete(anyString(), anyList());
    return template;
}
Also used : KeyInfoRedisTemplate(com.rdbcache.configs.KeyInfoRedisTemplate) KeyInfo(com.rdbcache.models.KeyInfo) HashOperations(org.springframework.data.redis.core.HashOperations) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 22 with KeyInfo

use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class AnyKeyTest method getAny.

@Test
public void getAny() {
    AnyKey anyKey;
    KeyInfo keyInfo;
    anyKey = new AnyKey();
    keyInfo = anyKey.getKeyInfo();
    assertNull(keyInfo);
    keyInfo = new KeyInfo();
    keyInfo.setExpire("100");
    keyInfo.setTable("table");
    anyKey = new AnyKey(keyInfo);
    KeyInfo keyInfo2 = anyKey.getKeyInfo();
    assertNotNull(keyInfo2);
    assertTrue(keyInfo == keyInfo2);
    keyInfo2 = anyKey.get(0);
    assertNotNull(keyInfo2);
    assertTrue(keyInfo == keyInfo2);
    LocalCache localCache = new LocalCache();
    localCache.init();
    localCache.handleEvent(null);
    AppCtx.setLocalCache(localCache);
    try {
        for (int i = 0; i < 10; i++) {
            keyInfo = anyKey.getAny(i);
            assertNotNull(keyInfo);
            if (i == 0)
                assertFalse(keyInfo.getIsNew());
            else
                assertTrue(keyInfo.getIsNew());
            assertEquals(i + 1, anyKey.size());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : LocalCache(com.rdbcache.services.LocalCache) KeyInfo(com.rdbcache.models.KeyInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with KeyInfo

use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class AnyKeyTest method setKey.

@Test
public void setKey() {
    AnyKey anyKey;
    KeyInfo keyInfo;
    anyKey = new AnyKey();
    keyInfo = new KeyInfo();
    keyInfo.setExpire("100");
    keyInfo.setTable("table");
    anyKey.setKeyInfo(keyInfo);
    assertEquals(1, anyKey.size());
    assertTrue(keyInfo == anyKey.get(0));
    anyKey = new AnyKey(new KeyInfo());
    anyKey.setKeyInfo(keyInfo);
    assertEquals(1, anyKey.size());
    assertTrue(keyInfo == anyKey.get(0));
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with KeyInfo

use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class RequestTest method process1.

@Test
public void process1() {
    try {
        String key = "*";
        Optional<String> tableOpt = Optional.of("tb1");
        Optional<String> expireOpt = Optional.of("30");
        String queryString = "id=1";
        HttpServletRequest request = getRequest("get", key, null, tableOpt, expireOpt, queryString);
        Context context = new Context();
        AnyKey anyKey = Request.process(context, request, new KvPairs(key), tableOpt, expireOpt);
        assertEquals(1, anyKey.size());
        KeyInfo keyInfo = anyKey.getKeyInfo();
        assertTrue(keyInfo.getIsNew());
        assertEquals("KeyInfo(true, tb1, 30, {id: {=: [1]}})", keyInfo.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) KeyInfo(com.rdbcache.models.KeyInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with KeyInfo

use of com.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class RequestTest method process2.

@Test
public void process2() {
    try {
        String key = "01a089f3ab704c1aaecdbe13777538e0";
        HttpServletRequest request = getRequest("get", key, null, null, null, null);
        Context context = new Context();
        AnyKey anyKey = Request.process(context, request, new KvPairs(key), null, null);
        assertEquals(1, anyKey.size());
        KeyInfo keyInfo = anyKey.getKeyInfo();
        assertFalse(keyInfo.getIsNew());
        // System.out.println(keyInfo.toString());
        assertEquals("KeyInfo(false, user_table, 30, id = ?, [12466])", keyInfo.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getCause().getMessage());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) KeyInfo(com.rdbcache.models.KeyInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

KeyInfo (com.rdbcache.models.KeyInfo)44 KvPair (com.rdbcache.models.KvPair)23 Test (org.junit.Test)13 ServerErrorException (com.rdbcache.exceptions.ServerErrorException)9 StopWatch (com.rdbcache.models.StopWatch)9 AnyKey (com.rdbcache.helpers.AnyKey)7 Context (com.rdbcache.helpers.Context)7 KvPairs (com.rdbcache.helpers.KvPairs)7 BadRequestException (com.rdbcache.exceptions.BadRequestException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 SQLException (java.sql.SQLException)4 QueryInfo (com.rdbcache.queries.QueryInfo)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 KeyInfoRedisTemplate (com.rdbcache.configs.KeyInfoRedisTemplate)1 NotFoundException (com.rdbcache.exceptions.NotFoundException)1 DbaseOps (com.rdbcache.services.DbaseOps)1 LocalCache (com.rdbcache.services.LocalCache)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1