Search in sources :

Example 1 with KvIdType

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

the class RdbcacheApis method trace_get.

/**
 * trace_get get single item
 *
 * get error messages by trace id
 *
 * @param request HttpServletRequest
 * @param traceId the trace id return by API call
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/trace/{traceId}" }, method = RequestMethod.GET)
public ResponseEntity<?> trace_get(HttpServletRequest request, @PathVariable("traceId") String traceId) {
    if (traceId.equals("*")) {
        throw new BadRequestException("no * allowed as trace id");
    }
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("no query string is allowed");
    }
    Context context = new Context(true);
    KvPairs pairs = new KvPairs();
    Request.process(context, request, pairs);
    LOGGER.trace("pairs(" + pairs.size() + "): " + pairs.printKey());
    KvPair pairKey = new KvPair(traceId, "trace");
    Optional<KvPair> opt = AppCtx.getKvPairRepo().findById(new KvIdType(traceId, "trace"));
    if (opt.isPresent()) {
        pairs.add(opt.get());
    }
    return Response.send(context, pairs);
}
Also used : KvIdType(com.rdbcache.models.KvIdType) KvPair(com.rdbcache.models.KvPair) BadRequestException(com.rdbcache.exceptions.BadRequestException)

Example 2 with KvIdType

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

the class RdbcacheApisTest method delall_get1.

@Test
public void delall_get1() {
    try {
        Map<String, Object> data1 = null, data2 = null;
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/select/user_table?email=mike@example.com").accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            data1 = (Map<String, Object>) map.get("data");
            assertNotNull(data1);
            assertEquals(1, data1.size());
        }
        String key = (String) data1.keySet().toArray()[0];
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/delall/" + key).accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            data1 = (Map<String, Object>) map.get("data");
            assertNull(data1);
        }
        // allow time to synchronize data
        try {
            Thread.sleep(250);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        assertFalse(AppCtx.getLocalCache().containsKey(key));
        Map<String, Object> redis = MockRedis.getData();
        // System.out.println(Utils.toJsonMap(redis));
        assertFalse(redis.containsKey(PropCfg.getHdataPrefix() + "::" + key));
        Map<String, Object> rdchkeys = (Map<String, Object>) redis.get("rdchkeys::keyinfo");
        assertFalse(rdchkeys.containsKey(key));
        Optional<KvPair> optional = AppCtx.getKvPairRepo().findById(new KvIdType(key, "info"));
        assertFalse(optional.isPresent());
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/get/*/user_table?id=1").accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(404, response.getStatus());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("caught an exception");
    }
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) KvPair(com.rdbcache.models.KvPair) MvcResult(org.springframework.test.web.servlet.MvcResult) KvIdType(com.rdbcache.models.KvIdType) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with KvIdType

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

the class RdbcacheApisTest method delkey_post1.

@Test
public void delkey_post1() {
    try {
        Map<String, Object> data1 = null, data2 = null;
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/select/user_table?limit=3").accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            data1 = (Map<String, Object>) map.get("data");
            assertNotNull(data1);
            assertEquals(3, data1.size());
        }
        Set<String> keys = data1.keySet();
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/rdbcache/v1/delkey/").contentType(MediaType.APPLICATION_JSON).content(Utils.toJsonMap(keys)).accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            assertNotNull(map.get("data"));
        }
        // allow time to synchronize data
        try {
            Thread.sleep(500);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        Map<String, Object> redis = MockRedis.getData();
        for (String key : keys) {
            assertFalse(AppCtx.getLocalCache().containsKey(key));
            assertFalse(redis.containsKey(PropCfg.getHdataPrefix() + "::" + key));
            Map<String, Object> rdchkeys = (Map<String, Object>) redis.get("rdchkeys::keyinfo");
            assertFalse(rdchkeys.containsKey(key));
            Optional<KvPair> optional = AppCtx.getKvPairRepo().findById(new KvIdType(key, "info"));
            assertFalse(optional.isPresent());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("caught an exception");
    }
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) KvPair(com.rdbcache.models.KvPair) MvcResult(org.springframework.test.web.servlet.MvcResult) KvIdType(com.rdbcache.models.KvIdType) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 4 with KvIdType

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

the class RdbcacheApisTest method delall_post1.

@Test
public void delall_post1() {
    try {
        Map<String, Object> data1 = null, data2 = null;
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/select/user_table?limit=3").accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            data1 = (Map<String, Object>) map.get("data");
            assertNotNull(data1);
            assertEquals(3, data1.size());
        }
        Set<String> keys = data1.keySet();
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/rdbcache/v1/delall/").contentType(MediaType.APPLICATION_JSON).content(Utils.toJsonMap(keys)).accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            assertNotNull(map.get("data"));
        }
        // allow time to synchronize data
        try {
            Thread.sleep(250);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        Map<String, Object> redis = MockRedis.getData();
        // System.out.println(Utils.toJsonMap(redis));
        int i = 1;
        for (String key : keys) {
            assertFalse(AppCtx.getLocalCache().containsKey(key));
            assertFalse(redis.containsKey(PropCfg.getHdataPrefix() + "::" + key));
            Map<String, Object> rdchkeys = (Map<String, Object>) redis.get("rdchkeys::keyinfo");
            // System.out.println(Utils.toJsonMap(rdchkeys));
            assertFalse(rdchkeys.containsKey(key));
            Optional<KvPair> optional = AppCtx.getKvPairRepo().findById(new KvIdType(key, "info"));
            assertFalse(optional.isPresent());
            {
                RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/get/*/user_table?id=" + i++).accept(MediaType.APPLICATION_JSON);
                ResultActions actions = mockMvc.perform(requestBuilder);
                MvcResult result = actions.andReturn();
                MockHttpServletResponse response = result.getResponse();
                assertEquals(404, response.getStatus());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("caught an exception");
    }
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) KvPair(com.rdbcache.models.KvPair) MvcResult(org.springframework.test.web.servlet.MvcResult) KvIdType(com.rdbcache.models.KvIdType) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 5 with KvIdType

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

the class RdbcacheApisTest method delkey_get1.

@Test
public void delkey_get1() {
    try {
        Map<String, Object> data1 = null, data2 = null;
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/select/user_table?limit=1").accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            data1 = (Map<String, Object>) map.get("data");
            assertNotNull(data1);
            assertEquals(1, data1.size());
        }
        String key = (String) data1.keySet().toArray()[0];
        {
            RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/delkey/" + key).accept(MediaType.APPLICATION_JSON);
            ResultActions actions = mockMvc.perform(requestBuilder);
            MvcResult result = actions.andReturn();
            MockHttpServletResponse response = result.getResponse();
            assertEquals(200, response.getStatus());
            String body = response.getContentAsString();
            // System.out.println(body);
            Map<String, Object> map = Utils.toMap(body);
            data1 = (Map<String, Object>) map.get("data");
            assertNull(data1);
        }
        // allow time to synchronize data
        try {
            Thread.sleep(250);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        assertFalse(AppCtx.getLocalCache().containsKey(key));
        Map<String, Object> redis = MockRedis.getData();
        // System.out.println(Utils.toJsonMap(redis));
        assertFalse(redis.containsKey(PropCfg.getHdataPrefix() + "::" + key));
        Map<String, Object> rdchkeys = (Map<String, Object>) redis.get("rdchkeys::keyinfo");
        assertFalse(rdchkeys.containsKey(key));
        Optional<KvPair> optional = AppCtx.getKvPairRepo().findById(new KvIdType(key, "info"));
        assertFalse(optional.isPresent());
    } catch (Exception e) {
        e.printStackTrace();
        fail("caught an exception");
    }
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) KvPair(com.rdbcache.models.KvPair) MvcResult(org.springframework.test.web.servlet.MvcResult) KvIdType(com.rdbcache.models.KvIdType) ResultActions(org.springframework.test.web.servlet.ResultActions) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Aggregations

KvIdType (com.rdbcache.models.KvIdType)6 KvPair (com.rdbcache.models.KvPair)6 Test (org.junit.Test)4 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 BadRequestException (com.rdbcache.exceptions.BadRequestException)2