Search in sources :

Example 1 with LocalCache

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);
}
Also used : MockServletContext(org.springframework.mock.web.MockServletContext) DbaseOps(com.rdbcache.services.DbaseOps) SimpleKeyInfoRepo(com.rdbcache.repositories.SimpleKeyInfoRepo) InputStreamReader(java.io.InputStreamReader) LocalCache(com.rdbcache.services.LocalCache) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Before(org.junit.Before)

Example 2 with 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());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LocalCache(com.rdbcache.services.LocalCache) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) MvcResult(org.springframework.test.web.servlet.MvcResult) ResultActions(org.springframework.test.web.servlet.ResultActions) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with LocalCache

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());
    }
}
Also used : LocalCache(com.rdbcache.services.LocalCache) KeyInfo(com.rdbcache.models.KeyInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LocalCache (com.rdbcache.services.LocalCache)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 KeyInfo (com.rdbcache.models.KeyInfo)1 SimpleKeyInfoRepo (com.rdbcache.repositories.SimpleKeyInfoRepo)1 DbaseOps (com.rdbcache.services.DbaseOps)1 Map (java.util.Map)1 Before (org.junit.Before)1 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)1 ResultActions (org.springframework.test.web.servlet.ResultActions)1