Search in sources :

Example 61 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse 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 62 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse 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 63 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse 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 64 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project rdbcache by rdbcache.

the class RdbcacheApisTest method get_get2.

@Test
public void get_get2() {
    try {
        RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/rdbcache/v1/get/my-test-hash-key/user_table?id=2").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("key"));
        assertTrue(map.containsKey("data"));
        assertTrue(map.containsKey("trace_id"));
        Map<String, Object> data = (Map<String, Object>) map.get("data");
        assertTrue(data.size() > 0);
        assertEquals("2", data.get("id").toString());
        assertEquals("kevin@example.com", data.get("email").toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail("caught an exception");
    }
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 65 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project books by aidanwhiteley.

the class HttpCookieOAuth2AuthorizationRequestRepositoryTest method testSaveAndLoadCookie.

@Test
public void testSaveAndLoadCookie() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    String TEST_CLIENT_ID = "Hello world";
    String DUMMY_TEXT_NOT_TESTED = "dummy";
    OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode().clientId(TEST_CLIENT_ID).authorizationUri(DUMMY_TEXT_NOT_TESTED).build();
    HttpCookieOAuth2AuthorizationRequestRepository repo = new HttpCookieOAuth2AuthorizationRequestRepository();
    repo.saveAuthorizationRequest(authorizationRequest, request, response);
    // Now get the cookie that should have been added to the response
    Cookie cookie = response.getCookie(HttpCookieOAuth2AuthorizationRequestRepository.COOKIE_NAME);
    request.setCookies(cookie);
    OAuth2AuthorizationRequest retrievedOauth = repo.loadAuthorizationRequest(request);
    String clientId = retrievedOauth.getClientId();
    assertEquals(TEST_CLIENT_ID, clientId);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2338 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1986 Test (org.junit.jupiter.api.Test)1412 lombok.val (lombok.val)946 Test (org.junit.Test)558 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)484 MockServletContext (org.springframework.mock.web.MockServletContext)462 MockRequestContext (org.springframework.webflow.test.MockRequestContext)460 MockFilterChain (org.springframework.mock.web.MockFilterChain)239 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)184 JEEContext (org.pac4j.core.context.JEEContext)159 FilterChain (jakarta.servlet.FilterChain)117 Authentication (org.springframework.security.core.Authentication)116 BeforeEach (org.junit.jupiter.api.BeforeEach)106 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)97 HashMap (java.util.HashMap)84 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)83 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)65 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)64