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());
}
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());
}
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));
}
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());
}
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());
}
Aggregations