Search in sources :

Example 1 with CacheOps

use of doitincloud.rdbcache.services.CacheOps 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);
    Map<String, Object> tablesMap = new LinkedHashMap<>();
    tablesMap.put("user_table", "data");
    tablesMap.put("tb1", "data");
    Mockito.when(dbaseOps.getTablesMap(any(Context.class))).thenReturn(tablesMap);
    BDDMockito.when(AppCtx.getDbaseOps()).thenReturn(dbaseOps);
    CacheOps cacheOps = new CacheOps();
    cacheOps.init();
    cacheOps.handleEvent(null);
    BDDMockito.when(AppCtx.getCacheOps()).thenReturn(cacheOps);
}
Also used : Context(doitincloud.rdbcache.supports.Context) MockServletContext(org.springframework.mock.web.MockServletContext) DbaseOps(doitincloud.rdbcache.services.DbaseOps) SimpleKeyInfoRepo(doitincloud.rdbcache.repositories.SimpleKeyInfoRepo) CacheOps(doitincloud.rdbcache.services.CacheOps) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) LinkedHashMap(java.util.LinkedHashMap) Before(org.junit.Before)

Example 2 with CacheOps

use of doitincloud.rdbcache.services.CacheOps 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);
    CacheOps cacheOps = new CacheOps();
    cacheOps.init();
    cacheOps.handleEvent(null);
    AppCtx.setCacheOps(cacheOps);
    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 : AnyKey(doitincloud.rdbcache.supports.AnyKey) CacheOps(doitincloud.rdbcache.services.CacheOps) KeyInfo(doitincloud.rdbcache.models.KeyInfo) Test(org.junit.Test)

Example 3 with CacheOps

use of doitincloud.rdbcache.services.CacheOps 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(cacheOps.listAllKeyInfos()).thenReturn(testKeys);
        // PowerMockito.mockStatic(AppCtx.class);
        // BDDMockito.when(AppCtx.getCacheOps()).thenReturn(cacheOps);
        // try different way
        // 
        CacheOps cache = new CacheOps();
        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.setCacheOps(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) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) CacheOps(doitincloud.rdbcache.services.CacheOps) 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)

Aggregations

CacheOps (doitincloud.rdbcache.services.CacheOps)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 Test (org.junit.Test)2 KeyInfo (doitincloud.rdbcache.models.KeyInfo)1 SimpleKeyInfoRepo (doitincloud.rdbcache.repositories.SimpleKeyInfoRepo)1 DbaseOps (doitincloud.rdbcache.services.DbaseOps)1 AnyKey (doitincloud.rdbcache.supports.AnyKey)1 Context (doitincloud.rdbcache.supports.Context)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)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