use of com.rdbcache.services.LocalCache in project rdbcache by rdbcache.
the class RequestTest method setUp.
@Before
public void setUp() throws Exception {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test-keyinfos.json");
assertNotNull(inputStream);
String text = null;
try (final Reader reader = new InputStreamReader(inputStream)) {
text = CharStreams.toString(reader);
}
assertNotNull(text);
Map<String, Object> map = Utils.toMap(text);
assertNotNull(map);
PowerMockito.mockStatic(AppCtx.class);
keyInfoRepo = new SimpleKeyInfoRepo(map);
BDDMockito.when(AppCtx.getKeyInfoRepo()).thenReturn(keyInfoRepo);
DbaseOps dbaseOps = mock(DbaseOps.class, Mockito.RETURNS_DEEP_STUBS);
assertNotNull(dbaseOps);
Mockito.when(dbaseOps.getTableList(any(Context.class))).thenReturn(Arrays.asList("user_table", "tb1"));
BDDMockito.when(AppCtx.getDbaseOps()).thenReturn(dbaseOps);
LocalCache localCache = new LocalCache();
localCache.init();
localCache.handleEvent(null);
BDDMockito.when(AppCtx.getLocalCache()).thenReturn(localCache);
}
use of com.rdbcache.services.LocalCache in project rdbcache by rdbcache.
the class RTQueryApisTest method retrieveLocalCacheKey.
@Test
public void retrieveLocalCacheKey() throws Exception {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test-keys.json");
assertNotNull(inputStream);
String text = null;
try (final Reader reader = new InputStreamReader(inputStream)) {
text = CharStreams.toString(reader);
}
assertNotNull(text);
Map<String, Object> testKeys = Utils.toMap(text);
assertNotNull(testKeys);
// Mockito.when(localCache.listAllKeyInfos()).thenReturn(testKeys);
// PowerMockito.mockStatic(AppCtx.class);
// BDDMockito.when(AppCtx.getLocalCache()).thenReturn(localCache);
// try different way
//
LocalCache cache = new LocalCache();
cache.init();
cache.handleEvent(null);
// cache.handleApplicationReadyEvent(null);
for (Map.Entry<String, Object> entry : testKeys.entrySet()) {
cache.put(entry.getKey(), (Map<String, Object>) entry.getValue());
}
AppCtx.setLocalCache(cache);
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rtquery/v1/cache/key").accept(MediaType.APPLICATION_JSON);
ResultActions actions = mockMvc.perform(requestBuilder);
MvcResult result = actions.andReturn();
MockHttpServletResponse response = result.getResponse();
assertEquals(response.getStatus(), 200);
String body = response.getContentAsString();
// System.out.println(body);
Map<String, Object> map = Utils.toMap(body);
assertTrue(map.containsKey("timestamp"));
assertTrue(map.containsKey("duration"));
assertTrue(map.containsKey("data"));
assertTrue(map.containsKey("trace_id"));
Map<String, Object> data = (Map<String, Object>) map.get("data");
assertTrue(data.size() > 0);
} catch (Exception e) {
e.printStackTrace();
fail(e.getCause().getMessage());
}
}
use of com.rdbcache.services.LocalCache 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());
}
}
Aggregations