Search in sources :

Example 6 with ResultActions

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

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

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class LabelControllerIntegrationTest method testGetLabels2.

@Test
public void testGetLabels2() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/labels").param("direction", FieldSearchFilter.DESC_ORDER).param("filter[0].attribute", "value").param("filter[0].value", "gina").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.payload", hasSize(3)));
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Example 9 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class PageConfigurationControllerIntegrationTest method testPageConfiguration.

@Test
public void testPageConfiguration() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/pages/{pageCode}/configuration", "homepage").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.payload.online", is(true)));
    /**
     * The response should have the correct CORS headers and the CORS
     * configuration should reflect the one set in
     * org.entando.entando.aps.servlet.CORSFilter class
     */
    result.andExpect(header().string("Access-Control-Allow-Origin", "*"));
    result.andExpect(header().string("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"));
    result.andExpect(header().string("Access-Control-Allow-Headers", "Content-Type, Authorization"));
    result.andExpect(header().string("Access-Control-Max-Age", "3600"));
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Example 10 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class PageConfigurationControllerIntegrationTest method testGetPageConfigurationOnLineNotFound.

/**
 * Given:
 *  a page only in draft
 * When:
 *  the user request the configuration for status draft
 * Then
 *  the result is ok
 *
 * Given:
 *  a page only in draft
 * When:
 *  the user request the configuration for status published
 * Then
 *  an error with status code 400 is raised
 *
 * @throws Exception
 */
@Test
public void testGetPageConfigurationOnLineNotFound() throws Exception {
    String pageCode = "draft_page_100";
    try {
        Page mockPage = createPage(pageCode);
        this.pageManager.addPage(mockPage);
        IPage onlinePage = this.pageManager.getOnlinePage(pageCode);
        assertThat(onlinePage, is(nullValue()));
        IPage draftPage = this.pageManager.getDraftPage(pageCode);
        assertThat(draftPage, is(not(nullValue())));
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        ResultActions result = mockMvc.perform(get("/pages/{pageCode}/configuration", new Object[] { pageCode }).param("status", IPageService.STATUS_DRAFT).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result = mockMvc.perform(get("/pages/{pageCode}/configuration", new Object[] { pageCode }).param("status", IPageService.STATUS_ONLINE).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isBadRequest());
    } finally {
        this.pageManager.deletePage(pageCode);
    }
}
Also used : IPage(com.agiletec.aps.system.services.page.IPage) UserDetails(com.agiletec.aps.system.services.user.UserDetails) Page(com.agiletec.aps.system.services.page.Page) IPage(com.agiletec.aps.system.services.page.IPage) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Aggregations

ResultActions (org.springframework.test.web.servlet.ResultActions)600 Test (org.junit.Test)525 UserDetails (com.agiletec.aps.system.services.user.UserDetails)297 AbstractControllerIntegrationTest (org.entando.entando.web.AbstractControllerIntegrationTest)221 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)101 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 MvcResult (org.springframework.test.web.servlet.MvcResult)27 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)26 UUID (java.util.UUID)22 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)22 InputStream (java.io.InputStream)18 IEntityTypesConfigurer (com.agiletec.aps.system.common.entity.IEntityTypesConfigurer)17 HashMap (java.util.HashMap)17 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 IPage (com.agiletec.aps.system.services.page.IPage)13 Page (com.agiletec.aps.system.services.page.Page)12