Search in sources :

Example 21 with JsonPatch

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

the class JsonPatchManagerTest method testCollectionAddPatchNoPersist.

@Test
void testCollectionAddPatchNoPersist() throws Exception {
    DataElementGroup dataElementGroup = createDataElementGroup('A');
    assertEquals("DataElementGroupA", dataElementGroup.getName());
    DataElement dataElementA = createDataElement('A');
    DataElement dataElementB = createDataElement('B');
    dataElementGroup.getMembers().add(dataElementA);
    dataElementGroup.getMembers().add(dataElementB);
    assertEquals(2, dataElementGroup.getMembers().size());
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/name\", \"value\": \"updated\"}," + "{\"op\": \"add\", \"path\": \"/dataElements/-\", \"value\": {\"id\": \"my-uid\"}}" + "]", JsonPatch.class);
    assertNotNull(patch);
    DataElementGroup patchedDataElementGroup = jsonPatchManager.apply(patch, dataElementGroup);
    assertEquals("DataElementGroupA", dataElementGroup.getName());
    assertEquals(2, dataElementGroup.getMembers().size());
    assertEquals("updated", patchedDataElementGroup.getName());
    assertEquals(3, patchedDataElementGroup.getMembers().size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Example 22 with JsonPatch

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

the class JsonPatchManagerTest method testAddAndRemoveSharingUser.

@Test
void testAddAndRemoveSharingUser() throws JsonProcessingException, JsonPatchException {
    User userA = createUser('A');
    manager.save(userA);
    DataElement dataElementA = createDataElement('A');
    manager.save(dataElementA);
    assertEquals(0, dataElementA.getSharing().getUsers().size());
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/sharing/users\", \"value\": " + "{" + "\"" + userA.getUid() + "\": { \"access\":\"rw------\",\"id\": \"" + userA.getUid() + "\" }" + "}" + "}" + "]", JsonPatch.class);
    assertNotNull(patch);
    DataElement patchedDE = jsonPatchManager.apply(patch, dataElementA);
    assertEquals(1, patchedDE.getSharing().getUsers().size());
    assertEquals("rw------", patchedDE.getSharing().getUsers().get(userA.getUid()).getAccess());
    JsonPatch removePatch = jsonMapper.readValue("[" + "{\"op\": \"remove\", \"path\": \"/sharing/users/" + userA.getUid() + "\" } ]", JsonPatch.class);
    DataElement removedPatchedDE = jsonPatchManager.apply(removePatch, patchedDE);
    assertEquals(0, removedPatchedDE.getSharing().getUsers().size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Example 23 with JsonPatch

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

the class DefaultMetadataWorkflowService method acceptUpdate.

private ImportReport acceptUpdate(MetadataProposal proposal, ObjectBundleMode mode) {
    JsonPatch patch = mapJsonChangeToObject(proposal.getChange(), JsonPatch.class);
    Class<? extends IdentifiableObject> objType = proposal.getTarget().getType();
    if (patch == null) {
        return createJsonErrorReport(proposal);
    }
    IdentifiableObject patched = null;
    try {
        patched = patchManager.apply(patch, objectManager.get(objType, proposal.getTargetId()));
    } catch (JsonPatchException ex) {
        log.error("Failed to apply proposed object update: " + proposal.getChange(), ex);
        return createJsonErrorReport(proposal);
    }
    return importService.importMetadata(createImportParams(mode, ImportStrategy.UPDATE, patched));
}
Also used : JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 24 with JsonPatch

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

the class ReplaceOperationTest method testBasicTextToArray.

@Test
void testBasicTextToArray() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/aaa\", \"value\": [1, 2, 3, 4, 5]}" + "]", 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.isArray());
    assertEquals(5, testNode.size());
}
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)

Example 25 with JsonPatch

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

the class RemoveOperationTest method testRemoveProperty.

@Test
void testRemoveProperty() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"remove\", \"path\": \"/aaa\"}" + "]", JsonPatch.class);
    assertNotNull(patch);
    ObjectNode root = jsonMapper.createObjectNode();
    root.set("aaa", TextNode.valueOf("bbb"));
    assertTrue(root.has("aaa"));
    root = (ObjectNode) patch.apply(root);
    assertFalse(root.has("aaa"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) 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