Search in sources :

Example 6 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class JsonPatchManagerTest method testSimpleAddPatch.

@Test
void testSimpleAddPatch() throws Exception {
    Constant constant = createConstant('A', 1.0d);
    assertEquals("ConstantA", constant.getName());
    assertEquals(constant.getValue(), 0, 1.0d);
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/name\", \"value\": \"updated\"}," + "{\"op\": \"add\", \"path\": \"/value\", \"value\": 5.0}" + "]", JsonPatch.class);
    assertNotNull(patch);
    Constant patchedConstant = jsonPatchManager.apply(patch, constant);
    patchedConstant.setUid(CodeGenerator.generateUid());
    assertEquals("ConstantA", constant.getName());
    assertEquals(constant.getValue(), 0, 1.0d);
    assertEquals("updated", patchedConstant.getName());
    assertEquals(patchedConstant.getValue(), 0, 5.0d);
}
Also used : Constant(org.hisp.dhis.constant.Constant) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Example 7 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class AbstractCrudController method patchObject.

// --------------------------------------------------------------------------
// PATCH
// --------------------------------------------------------------------------
/**
 * Adds support for HTTP Patch using JSON Patch (RFC 6902), updated object
 * is run through normal metadata importer and internally looks like a
 * normal PUT (after the JSON Patch has been applied).
 *
 * For now we only support the official mimetype
 * "application/json-patch+json" but in future releases we might also want
 * to support "application/json" after the old patch behavior has been
 * removed.
 */
@ResponseBody
@PatchMapping(path = "/{uid}", consumes = "application/json-patch+json")
public WebMessage patchObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        return notFound(getEntityClass(), pvUid);
    }
    final T persistedObject = entities.get(0);
    if (!aclService.canUpdate(currentUser, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    manager.resetNonOwnerProperties(persistedObject);
    prePatchEntity(persistedObject);
    final JsonPatch patch = jsonMapper.readValue(request.getInputStream(), JsonPatch.class);
    final T patchedObject = jsonPatchManager.apply(patch, persistedObject);
    // we don't allow changing IDs
    ((BaseIdentifiableObject) patchedObject).setId(persistedObject.getId());
    // we don't allow changing UIDs
    ((BaseIdentifiableObject) patchedObject).setUid(persistedObject.getUid());
    // Only supports new Sharing format
    ((BaseIdentifiableObject) patchedObject).clearLegacySharingCollections();
    prePatchEntity(persistedObject, patchedObject);
    Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
    if (!parameterValuesMap.containsKey("importReportMode")) {
        parameterValuesMap.put("importReportMode", Collections.singletonList("ERRORS_NOT_OWNER"));
    }
    MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
    params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(patchedObject);
    ImportReport importReport = importService.importMetadata(params);
    WebMessage webMessage = objectReport(importReport);
    if (importReport.getStatus() == Status.OK) {
        T entity = manager.get(getEntityClass(), pvUid);
        postPatchEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    return webMessage;
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class JsonPatchTest method testJsonPatchDeserializeEmpty.

@Test
void testJsonPatchDeserializeEmpty() throws JsonProcessingException {
    JsonPatch patch = jsonMapper.readValue("[]", JsonPatch.class);
    assertNotNull(patch);
    assertTrue(patch.getOperations().isEmpty());
}
Also used : JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Example 9 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class JsonPatchTest method testJsonPatchDeserializeWithOps.

@Test
void testJsonPatchDeserializeWithOps() throws JsonProcessingException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/aaa\", \"value\": \"bbb\"}," + "{\"op\": \"replace\", \"path\": \"/aaa\", \"value\": \"bbb\"}," + "{\"op\": \"remove\", \"path\": \"/aaa\"}" + "]", JsonPatch.class);
    assertNotNull(patch);
    assertEquals(3, patch.getOperations().size());
}
Also used : JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Example 10 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class ReplaceOperationTest method testBasicTextToObject.

@Test
void testBasicTextToObject() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/aaa\", \"value\": {\"a\": 123}}" + "]", JsonPatch.class);
    assertNotNull(patch);
    ObjectNode root = jsonMapper.createObjectNode();
    root.set("aaa", TextNode.valueOf("aaa"));
    assertTrue(root.has("aaa"));
    assertEquals("aaa", root.get("aaa").asText());
    root = (ObjectNode) patch.apply(root);
    assertTrue(root.has("aaa"));
    JsonNode testNode = root.get("aaa");
    assertTrue(testNode.isObject());
    assertTrue(testNode.has("a"));
    assertEquals(123, testNode.get("a").asInt());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Aggregations

JsonPatch (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch)26 Test (org.junit.jupiter.api.Test)20 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 JsonPatchException (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException)5 List (java.util.List)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 Schema (org.hisp.dhis.schema.Schema)4 SchemaService (org.hisp.dhis.schema.SchemaService)4 Service (org.springframework.stereotype.Service)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 AllArgsConstructor (lombok.AllArgsConstructor)3 IdentifiableObjectManager (org.hisp.dhis.common.IdentifiableObjectManager)3 DataElement (org.hisp.dhis.dataelement.DataElement)3 ErrorCode (org.hisp.dhis.feedback.ErrorCode)3 ErrorReport (org.hisp.dhis.feedback.ErrorReport)3 ObjectReport (org.hisp.dhis.feedback.ObjectReport)3 TypeReport (org.hisp.dhis.feedback.TypeReport)3