Search in sources :

Example 1 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project rdbcache by rdbcache.

the class RTQueryApisTest method retrieveVersionInfo.

@Test
public void retrieveVersionInfo() throws Exception {
    try {
        RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rtquery/v1/version-info").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);
        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 : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) 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 2 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project rdbcache by rdbcache.

the class RTQueryApisTest method retrieveLocalCacheData.

@Test
public void retrieveLocalCacheData() throws Exception {
    try {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test-data.json");
        assertNotNull(inputStream);
        String text = null;
        try (final Reader reader = new InputStreamReader(inputStream)) {
            text = CharStreams.toString(reader);
        }
        assertNotNull(text);
        Map<String, Object> testData = Utils.toMap(text);
        assertNotNull(testData);
        Mockito.when(localCache.listAllData()).thenReturn(testData);
        PowerMockito.mockStatic(AppCtx.class);
        BDDMockito.when(AppCtx.getLocalCache()).thenReturn(localCache);
        RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rtquery/v1/cache/data").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) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) 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 ResultActions

use of org.springframework.test.web.servlet.ResultActions in project rdbcache by rdbcache.

the class RTQueryApisTest method retrieveLocalCacheConfig.

@Test
public void retrieveLocalCacheConfig() throws Exception {
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rtquery/v1/cache/config").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);
    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);
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) 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 4 with ResultActions

use of org.springframework.test.web.servlet.ResultActions 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 5 with ResultActions

use of org.springframework.test.web.servlet.ResultActions 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)

Aggregations

ResultActions (org.springframework.test.web.servlet.ResultActions)372 Test (org.junit.Test)343 UserDetails (com.agiletec.aps.system.services.user.UserDetails)142 AbstractControllerIntegrationTest (org.entando.entando.web.AbstractControllerIntegrationTest)71 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)71 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)43 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)39 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)38 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)28 MvcResult (org.springframework.test.web.servlet.MvcResult)27 UUID (java.util.UUID)22 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)22 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)16 Test (org.junit.jupiter.api.Test)15 WebIntegrationBaseTest (org.nextprot.api.web.dbunit.base.mvc.WebIntegrationBaseTest)14 InputStream (java.io.InputStream)12 Map (java.util.Map)11 UUID.randomUUID (java.util.UUID.randomUUID)11 RestListRequest (org.entando.entando.web.common.model.RestListRequest)10 HashMap (java.util.HashMap)9