Search in sources :

Example 11 with JsonPatch

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

the class ReplaceOperationTest method testBasicPropertyReplacement.

@Test
void testBasicPropertyReplacement() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/aaa\", \"value\": \"bbb\"}" + "]", 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"));
    assertEquals("bbb", root.get("aaa").asText());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Example 12 with JsonPatch

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

the class RemoveOperationTest method testRemovePropertyArray2ndIndex.

@Test
void testRemovePropertyArray2ndIndex() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"remove\", \"path\": \"/aaa/1\"}" + "]", JsonPatch.class);
    assertNotNull(patch);
    ObjectNode root = jsonMapper.createObjectNode();
    ArrayNode arrayNode = jsonMapper.createArrayNode();
    arrayNode.add(10);
    arrayNode.add(20);
    arrayNode.add(30);
    root.set("aaa", arrayNode);
    assertTrue(root.has("aaa"));
    assertEquals(3, arrayNode.size());
    root = (ObjectNode) patch.apply(root);
    arrayNode = (ArrayNode) root.get("aaa");
    assertNotNull(arrayNode);
    assertEquals(2, arrayNode.size());
    assertEquals(10, arrayNode.get(0).asInt());
    assertEquals(30, arrayNode.get(1).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 13 with JsonPatch

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

the class RemoveOperationTest method testRemoveInvalidKeyShouldThrowException.

@Disabled("for now we will allow 'removal' of invalid path keys")
@Test
void testRemoveInvalidKeyShouldThrowException() throws JsonProcessingException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"remove\", \"path\": \"/aaa\"}" + "]", JsonPatch.class);
    assertNotNull(patch);
    JsonNode root = jsonMapper.createObjectNode();
    assertFalse(root.has("aaa"));
    assertThrows(JsonPatchException.class, () -> patch.apply(root));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 14 with JsonPatch

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

the class RemoveOperationTest method testRemovePropertyArrayLastIndex.

@Test
void testRemovePropertyArrayLastIndex() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"remove\", \"path\": \"/aaa/2\"}" + "]", JsonPatch.class);
    assertNotNull(patch);
    ObjectNode root = jsonMapper.createObjectNode();
    ArrayNode arrayNode = jsonMapper.createArrayNode();
    arrayNode.add(10);
    arrayNode.add(20);
    arrayNode.add(30);
    root.set("aaa", arrayNode);
    assertTrue(root.has("aaa"));
    assertEquals(3, arrayNode.size());
    root = (ObjectNode) patch.apply(root);
    arrayNode = (ArrayNode) root.get("aaa");
    assertNotNull(arrayNode);
    assertEquals(2, arrayNode.size());
    assertEquals(10, arrayNode.get(0).asInt());
    assertEquals(20, arrayNode.get(1).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 15 with JsonPatch

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

the class AddOperationTest method testAddEmptyPath.

@Test
void testAddEmptyPath() throws JsonProcessingException, JsonPatchException {
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"\", \"value\": \"bbb\"}" + "]", JsonPatch.class);
    assertNotNull(patch);
    JsonNode root = jsonMapper.createObjectNode();
    root = patch.apply(root);
    assertEquals("bbb", root.asText());
}
Also used : 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