use of org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest in project entando-core by entando.
the class DataObjectModelControllerTest method should_validate_put_path_mismatch.
@Test
public void should_validate_put_path_mismatch() throws ApsSystemException, Exception {
UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
String accessToken = mockOAuthInterceptor(user);
ObjectMapper mapper = new ObjectMapper();
DataObjectModelRequest group = new DataObjectModelRequest();
group.setModelId("2l");
group.setDescr("Description");
group.setType("AAA");
group.setModel("<p>Test</p>");
String payload = mapper.writeValueAsString(group);
this.controller.setDataObjectModelValidator(new DataObjectModelValidator());
ResultActions result = mockMvc.perform(put("/dataModels/{dataModelId}", "67").content(payload).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
result.andExpect(status().isBadRequest());
}
use of org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest 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.dataobjectmodel.model.DataObjectModelRequest in project entando-core by entando.
the class DataModelDtoToRequestConverter method convert.
@Override
public DataObjectModelRequest convert(DataModelDto source) {
DataObjectModelRequest request = new DataObjectModelRequest();
request.setModelId(source.getModelId());
request.setModel(source.getModel());
request.setDescr(source.getDescr());
request.setType(source.getType());
request.setStylesheet(source.getStylesheet());
return request;
}
use of org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest in project entando-core by entando.
the class DataObjectModelController method patchDataObjectModel.
@RestAccessControl(permission = Permission.SUPERUSER)
@RequestMapping(value = "/{dataModelId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_VALUE, consumes = "application/json-patch+json")
public ResponseEntity<SimpleRestResponse<DataModelDto>> patchDataObjectModel(@PathVariable Long dataModelId, @RequestBody JsonNode jsonPatch, BindingResult bindingResult) throws JsonProcessingException {
logger.debug("Patching data object model -> {}", dataModelId);
this.getDataObjectModelValidator().validateDataObjectModelJsonPatch(jsonPatch, bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationGenericException(bindingResult);
}
DataModelDto patchedDataModelDto = this.getDataObjectModelService().getPatchedDataObjectModel(dataModelId, jsonPatch);
DataObjectModelRequest dataObjectModelRequest = this.dataModelDtoToRequestConverter.convert(patchedDataModelDto);
return this.updateDataObjectModel(Long.toString(dataModelId), dataObjectModelRequest, bindingResult);
}
use of org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest in project entando-core by entando.
the class DataObjectModelValidator method validate.
@Override
public void validate(Object target, Errors errors) {
DataObjectModelRequest request = (DataObjectModelRequest) target;
String modelId = request.getModelId();
Long dataModelLong = this.checkValidModelId(modelId, errors);
if (null != dataModelLong && null != this.dataObjectModelManager.getDataObjectModel(dataModelLong)) {
errors.reject(ERRCODE_DATAOBJECTMODEL_ALREADY_EXISTS, new String[] { String.valueOf(modelId) }, "dataObjectModel.exists");
}
}
Aggregations