Search in sources :

Example 1 with DataObjectModelRequest

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());
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) DataObjectModelValidator(org.entando.entando.web.dataobjectmodel.validator.DataObjectModelValidator) ResultActions(org.springframework.test.web.servlet.ResultActions) DataObjectModelRequest(org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Example 2 with DataObjectModelRequest

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);
        }
    }
}
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 3 with DataObjectModelRequest

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;
}
Also used : DataObjectModelRequest(org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest)

Example 4 with DataObjectModelRequest

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);
}
Also used : DataModelDto(org.entando.entando.aps.system.services.dataobjectmodel.model.DataModelDto) DataObjectModelRequest(org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with DataObjectModelRequest

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");
    }
}
Also used : DataObjectModelRequest(org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest)

Aggregations

DataObjectModelRequest (org.entando.entando.web.dataobjectmodel.model.DataObjectModelRequest)5 UserDetails (com.agiletec.aps.system.services.user.UserDetails)2 Test (org.junit.Test)2 ResultActions (org.springframework.test.web.servlet.ResultActions)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DataObjectModel (org.entando.entando.aps.system.services.dataobjectmodel.DataObjectModel)1 DataModelDto (org.entando.entando.aps.system.services.dataobjectmodel.model.DataModelDto)1 AbstractControllerIntegrationTest (org.entando.entando.web.AbstractControllerIntegrationTest)1 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)1 JsonPatchBuilder (org.entando.entando.web.JsonPatchBuilder)1 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)1 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)1 DataObjectModelValidator (org.entando.entando.web.dataobjectmodel.validator.DataObjectModelValidator)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1