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