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;
}
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());
}
}
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));
}
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());
}
}
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());
}
}
Aggregations