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);
}
}
}
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);
}
}
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);
}
}
}
Aggregations