Search in sources :

Example 16 with JsonPatch

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

the class AddOperationTest method testAddAppendArray.

@Test
void testAddAppendArray() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/arr/-\", \"value\": 1}," + "{\"op\": \"add\", \"path\": \"/arr/-\", \"value\": 2}," + "{\"op\": \"add\", \"path\": \"/arr/-\", \"value\": 3}" + "]", JsonPatch.class);
    assertNotNull(patch);
    ObjectNode root = jsonMapper.createObjectNode();
    ArrayNode arrayNode = jsonMapper.createArrayNode();
    root.set("arr", arrayNode);
    root = (ObjectNode) patch.apply(root);
    assertTrue(root.has("arr"));
    assertEquals(3, arrayNode.size());
    assertEquals(1, arrayNode.get(0).asInt());
    assertEquals(2, arrayNode.get(1).asInt());
    assertEquals(3, arrayNode.get(2).asInt());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Example 17 with JsonPatch

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

the class AddOperationTest method testAddSimpleStringPropertyPath.

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

Example 18 with JsonPatch

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

the class AddOperationTest method testAddSimpleNumberPropertyPath.

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

Example 19 with JsonPatch

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

the class AddOperationTest method testAddModifyArray.

@Test
void testAddModifyArray() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/arr/0\", \"value\": 1}" + "]", JsonPatch.class);
    assertNotNull(patch);
    ObjectNode root = jsonMapper.createObjectNode();
    root.set("arr", jsonMapper.createArrayNode());
    root = (ObjectNode) patch.apply(root);
    JsonNode arrayNode = root.get("arr");
    assertNotNull(arrayNode);
    assertEquals(1, arrayNode.get(0).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)

Example 20 with JsonPatch

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

the class JsonPatchManagerTest method testAddAndReplaceSharingUser.

@Test
void testAddAndReplaceSharingUser() 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 replacedPatch = jsonMapper.readValue("[" + "{\"op\": \"replace\", \"path\": \"/sharing/users\", \"value\": " + "{" + "\"" + userA.getUid() + "\": { \"access\":\"r-------\",\"id\": \"" + userA.getUid() + "\" }" + "}" + "}" + "]", JsonPatch.class);
    DataElement replacePatchedDE = jsonPatchManager.apply(replacedPatch, patchedDE);
    assertEquals(1, replacePatchedDE.getSharing().getUsers().size());
    assertEquals("r-------", replacePatchedDE.getSharing().getUsers().get(userA.getUid()).getAccess());
}
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)

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