use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class JsonPatchManagerTest method testSimpleAddPatch.
@Test
void testSimpleAddPatch() throws Exception {
Constant constant = createConstant('A', 1.0d);
assertEquals("ConstantA", constant.getName());
assertEquals(constant.getValue(), 0, 1.0d);
JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/name\", \"value\": \"updated\"}," + "{\"op\": \"add\", \"path\": \"/value\", \"value\": 5.0}" + "]", JsonPatch.class);
assertNotNull(patch);
Constant patchedConstant = jsonPatchManager.apply(patch, constant);
patchedConstant.setUid(CodeGenerator.generateUid());
assertEquals("ConstantA", constant.getName());
assertEquals(constant.getValue(), 0, 1.0d);
assertEquals("updated", patchedConstant.getName());
assertEquals(patchedConstant.getValue(), 0, 5.0d);
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class AbstractCrudController method patchObject.
// --------------------------------------------------------------------------
// PATCH
// --------------------------------------------------------------------------
/**
* Adds support for HTTP Patch using JSON Patch (RFC 6902), updated object
* is run through normal metadata importer and internally looks like a
* normal PUT (after the JSON Patch has been applied).
*
* For now we only support the official mimetype
* "application/json-patch+json" but in future releases we might also want
* to support "application/json" after the old patch behavior has been
* removed.
*/
@ResponseBody
@PatchMapping(path = "/{uid}", consumes = "application/json-patch+json")
public WebMessage patchObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
return notFound(getEntityClass(), pvUid);
}
final T persistedObject = entities.get(0);
if (!aclService.canUpdate(currentUser, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
manager.resetNonOwnerProperties(persistedObject);
prePatchEntity(persistedObject);
final JsonPatch patch = jsonMapper.readValue(request.getInputStream(), JsonPatch.class);
final T patchedObject = jsonPatchManager.apply(patch, persistedObject);
// we don't allow changing IDs
((BaseIdentifiableObject) patchedObject).setId(persistedObject.getId());
// we don't allow changing UIDs
((BaseIdentifiableObject) patchedObject).setUid(persistedObject.getUid());
// Only supports new Sharing format
((BaseIdentifiableObject) patchedObject).clearLegacySharingCollections();
prePatchEntity(persistedObject, patchedObject);
Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
if (!parameterValuesMap.containsKey("importReportMode")) {
parameterValuesMap.put("importReportMode", Collections.singletonList("ERRORS_NOT_OWNER"));
}
MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(patchedObject);
ImportReport importReport = importService.importMetadata(params);
WebMessage webMessage = objectReport(importReport);
if (importReport.getStatus() == Status.OK) {
T entity = manager.get(getEntityClass(), pvUid);
postPatchEntity(entity);
} else {
webMessage.setStatus(Status.ERROR);
}
return webMessage;
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class JsonPatchTest method testJsonPatchDeserializeEmpty.
@Test
void testJsonPatchDeserializeEmpty() throws JsonProcessingException {
JsonPatch patch = jsonMapper.readValue("[]", JsonPatch.class);
assertNotNull(patch);
assertTrue(patch.getOperations().isEmpty());
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class JsonPatchTest method testJsonPatchDeserializeWithOps.
@Test
void testJsonPatchDeserializeWithOps() throws JsonProcessingException {
JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/aaa\", \"value\": \"bbb\"}," + "{\"op\": \"replace\", \"path\": \"/aaa\", \"value\": \"bbb\"}," + "{\"op\": \"remove\", \"path\": \"/aaa\"}" + "]", JsonPatch.class);
assertNotNull(patch);
assertEquals(3, patch.getOperations().size());
}
use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.
the class ReplaceOperationTest method testBasicTextToObject.
@Test
void testBasicTextToObject() throws JsonProcessingException, JsonPatchException {
JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/aaa\", \"value\": {\"a\": 123}}" + "]", 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.isObject());
assertTrue(testNode.has("a"));
assertEquals(123, testNode.get("a").asInt());
}
Aggregations