Search in sources :

Example 1 with JsonPatchBuilder

use of org.entando.entando.web.JsonPatchBuilder in project entando-core by entando.

the class DataObjectModelControllerIntegrationTest method testCrud.

@Test
public void testCrud() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    long id = 12345;
    try {
        DataObjectModelRequest request = new DataObjectModelRequest();
        request.setModelId(String.valueOf(id));
        request.setDescr("test_" + id);
        request.setType("ART");
        request.setModel("hello");
        String payload = mapper.writeValueAsString(request);
        // post
        ResultActions result = mockMvc.perform(post("/dataModels").content(payload).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        // get
        result = mockMvc.perform(get("/dataModels/{modelId}", String.valueOf(id)).content(payload).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        // put
        result = mockMvc.perform(put("/dataModels/{modelId}", String.valueOf(id)).content(payload).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        // patch
        payload = new JsonPatchBuilder().withReplace("/descr", "").withReplace("/stylesheet", "body { font-size: 10px; }").getJsonPatchAsString();
        result = mockMvc.perform(patch("/dataModels/{modelId}", String.valueOf(id)).content(payload).contentType(RestMediaTypes.JSON_PATCH_JSON).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload.descr", is("")));
        result.andExpect(jsonPath("$.payload.stylesheet", is("body { font-size: 10px; }")));
        // invalid patch
        payload = new JsonPatchBuilder().withReplace("/modelId", "anything").getJsonPatchAsString();
        result = mockMvc.perform(patch("/dataModels/{modelId}", String.valueOf(id)).content(payload).contentType(RestMediaTypes.JSON_PATCH_JSON).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isBadRequest());
        result.andExpect(jsonPath("$.errors[0]", allOf(hasEntry("code", "1"), hasEntry("message", "The field 'modelId' can not be updated via JSON patch"))));
        // delete
        result = mockMvc.perform(delete("/dataModels/{modelId}", String.valueOf(id)).content(payload).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
    } finally {
        DataObjectModel dm = this.dataObjectModelManager.getDataObjectModel(id);
        if (null != dm) {
            this.dataObjectModelManager.removeDataObjectModel(dm);
        }
    }
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) JsonPatchBuilder(org.entando.entando.web.JsonPatchBuilder) DataObjectModel(org.entando.entando.aps.system.services.dataobjectmodel.DataObjectModel) ResultActions(org.springframework.test.web.servlet.ResultActions) DataObjectModelRequest(org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Example 2 with JsonPatchBuilder

use of org.entando.entando.web.JsonPatchBuilder in project entando-core by entando.

the class PageControllerIntegrationTest method testPatchPage.

@Test
public void testPatchPage() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    String newPageCode = "test_page";
    try {
        ResultActions result = mockMvc.perform(get("/pages/{code}", newPageCode).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isNotFound());
        result.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        result.andExpect(jsonPath("$.errors.size()", is(1)));
        Page newPage = this.createPage(newPageCode, null, this.pageManager.getDraftRoot().getCode());
        newPage.setTitle("it", "Title IT");
        newPage.setTitle("en", "Title EN");
        newPage.setShowable(false);
        newPage.setCharset("ascii");
        newPage.setMimeType("application/json");
        newPage.setExtraGroups(Stream.of("administration", "customers").collect(Collectors.toSet()));
        this.pageManager.addPage(newPage);
        result = mockMvc.perform(get("/pages/{code}", newPageCode).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload.code", is(newPageCode)));
        result.andExpect(jsonPath("$.payload.status", is("unpublished")));
        result.andExpect(jsonPath("$.payload.titles.it", is("Title IT")));
        result.andExpect(jsonPath("$.payload.titles.en", is("Title EN")));
        String payload = new JsonPatchBuilder().withReplace("/displayedInMenu", true).withReplace("/charset", "utf8").withReplace("/contentType", "text/html").withReplace("/titles", ImmutableMap.of("en", "Title English", "it", "Titolo Italiano")).withReplace("/joinGroups", ImmutableList.of("management", "customers")).getJsonPatchAsString();
        result = mockMvc.perform(patch("/pages/{code}", newPageCode).header("Authorization", "Bearer " + accessToken).contentType(RestMediaTypes.JSON_PATCH_JSON).accept(MediaType.APPLICATION_JSON).characterEncoding("UTF-8").content(payload));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload.displayedInMenu", is(true)));
        result.andExpect(jsonPath("$.payload.charset", is("utf8")));
        result.andExpect(jsonPath("$.payload.contentType", is("text/html")));
        result.andExpect(jsonPath("$.payload.joinGroups", hasItems("management", "customers")));
        result.andExpect(jsonPath("$.payload.titles.en", is("Title English")));
    } finally {
        this.pageManager.deletePage(newPageCode);
    }
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) JsonPatchBuilder(org.entando.entando.web.JsonPatchBuilder) IPage(com.agiletec.aps.system.services.page.IPage) Page(com.agiletec.aps.system.services.page.Page) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Example 3 with JsonPatchBuilder

use of org.entando.entando.web.JsonPatchBuilder in project entando-core by entando.

the class RoleControllerIntegrationTest method testCrudRole.

/**
 * Add a role without permissions
 * </p>
 *
 * Update the role with 1 valid permissions
 * </p>
 *
 * Update the role by adding 1 invalid permissions
 * </p>
 *
 * Update the role by adding 1 valid permissions
 * </p>
 *
 * delete the role
 * </p>
 *
 * @throws Exception
 */
@Test
public void testCrudRole() throws Exception {
    String code = "test";
    try {
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        ObjectMapper mapper = new ObjectMapper();
        RoleRequest request = new RoleRequest();
        request.setCode(code);
        request.setName(code);
        String payload = mapper.writeValueAsString(request);
        ResultActions result = mockMvc.perform(post("/roles").contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        // --------------
        request = new RoleRequest();
        request.setCode(code);
        request.setName(code.toUpperCase());
        request.getPermissions().put("editContents", true);
        payload = mapper.writeValueAsString(request);
        result = mockMvc.perform(put("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        // --------------
        request = new RoleRequest();
        request.setCode(code);
        request.setName(code.toUpperCase());
        request.getPermissions().put("editContents", true);
        request.getPermissions().put("WRONG", true);
        payload = mapper.writeValueAsString(request);
        result = mockMvc.perform(put("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isBadRequest());
        // --------------
        request = new RoleRequest();
        request.setCode(code);
        request.setName(code.toUpperCase());
        request.getPermissions().put("editContents", true);
        request.getPermissions().put("manageResources", true);
        request.getPermissions().put("manageCategories", false);
        payload = mapper.writeValueAsString(request);
        result = mockMvc.perform(put("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        // --------------
        payload = new JsonPatchBuilder().withOperation("replace", "/name", "New Test Role").withOperation("replace", "/permissions", ImmutableMap.of("manageCategories", true)).getJsonPatchAsString();
        result = mockMvc.perform(patch("/roles/{code}", code).contentType(RestMediaTypes.JSON_PATCH_JSON).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload.name", is("New Test Role")));
        result.andExpect(jsonPath("$.payload.permissions.manageCategories", is(true)));
        // --------------
        payload = new JsonPatchBuilder().withOperation("replace", "/code", "newcode").getJsonPatchAsString();
        result = mockMvc.perform(patch("/roles/{code}", code).contentType(RestMediaTypes.JSON_PATCH_JSON).content(payload).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isBadRequest());
        result.andExpect(jsonPath("$.errors[0]", allOf(hasEntry("code", "1"), hasEntry("message", "The field 'code' can not be updated via JSON patch"))));
        // --------------
        result = mockMvc.perform(delete("/roles/{code}", code).contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
    } finally {
        Role role = this.roleManager.getRole(code);
        if (null != role) {
            this.roleManager.removeRole(role);
        }
    }
}
Also used : Role(com.agiletec.aps.system.services.role.Role) UserDetails(com.agiletec.aps.system.services.user.UserDetails) JsonPatchBuilder(org.entando.entando.web.JsonPatchBuilder) ResultActions(org.springframework.test.web.servlet.ResultActions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RoleRequest(org.entando.entando.web.role.model.RoleRequest) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Aggregations

UserDetails (com.agiletec.aps.system.services.user.UserDetails)3 AbstractControllerIntegrationTest (org.entando.entando.web.AbstractControllerIntegrationTest)3 JsonPatchBuilder (org.entando.entando.web.JsonPatchBuilder)3 Test (org.junit.Test)3 ResultActions (org.springframework.test.web.servlet.ResultActions)3 IPage (com.agiletec.aps.system.services.page.IPage)1 Page (com.agiletec.aps.system.services.page.Page)1 Role (com.agiletec.aps.system.services.role.Role)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DataObjectModel (org.entando.entando.aps.system.services.dataobjectmodel.DataObjectModel)1 DataObjectModelRequest (org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest)1 RoleRequest (org.entando.entando.web.role.model.RoleRequest)1