Search in sources :

Example 1 with BulkPatchParameters

use of org.hisp.dhis.jsonpatch.BulkPatchParameters in project dhis2-core by dhis2.

the class BulkPatchManagerTest method testApplyPatchInvalidClassName.

@Test
void testApplyPatchInvalidClassName() throws IOException {
    final BulkJsonPatch bulkJsonPatch = loadPatch("bulk_sharing_patch_invalid_class_name.json", BulkJsonPatch.class);
    BulkPatchParameters patchParameters = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = patchManager.applyPatch(bulkJsonPatch, patchParameters);
    assertEquals(0, patchedObjects.size());
    assertEquals(1, patchParameters.getErrorReportsCount());
    assertEquals(1, patchParameters.getErrorReportsCount(ErrorCode.E6002));
}
Also used : BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 2 with BulkPatchParameters

use of org.hisp.dhis.jsonpatch.BulkPatchParameters in project dhis2-core by dhis2.

the class BulkPatchManagerTest method testApplyPatchInvalidUid.

@Test
void testApplyPatchInvalidUid() throws IOException {
    final BulkJsonPatch bulkJsonPatch = loadPatch("bulk_sharing_patch_invalid_uid.json", BulkJsonPatch.class);
    BulkPatchParameters patchParameters = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = patchManager.applyPatch(bulkJsonPatch, patchParameters);
    assertEquals(1, patchedObjects.size());
    assertEquals(1, patchParameters.getErrorReportsCount());
    assertEquals(1, patchParameters.getErrorReportsCount(ErrorCode.E4014));
    assertTrue(aclService.canRead(userA, patchedObjects.get(0)));
    assertFalse(aclService.canRead(userC, patchedObjects.get(0)));
}
Also used : BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 3 with BulkPatchParameters

use of org.hisp.dhis.jsonpatch.BulkPatchParameters in project dhis2-core by dhis2.

the class AbstractCrudController method bulkSharing.

@ResponseBody
@PatchMapping(path = "/sharing", consumes = "application/json-patch+json", produces = APPLICATION_JSON_VALUE)
public WebMessage bulkSharing(@RequestParam(required = false, defaultValue = "false") boolean atomic, HttpServletRequest request) throws Exception {
    final BulkJsonPatch bulkJsonPatch = jsonMapper.readValue(request.getInputStream(), BulkJsonPatch.class);
    BulkPatchParameters patchParams = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = bulkPatchManager.applyPatch(bulkJsonPatch, patchParams);
    if (patchedObjects.isEmpty() || (atomic && patchParams.hasErrorReports())) {
        ImportReport importReport = new ImportReport();
        importReport.addTypeReports(patchParams.getTypeReports());
        importReport.setStatus(Status.ERROR);
        return importReport(importReport);
    }
    Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
    MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
    params.setUser(currentUserService.getCurrentUser()).setImportStrategy(ImportStrategy.UPDATE).addObjects(patchedObjects);
    ImportReport importReport = importService.importMetadata(params);
    if (patchParams.hasErrorReports()) {
        importReport.addTypeReports(patchParams.getTypeReports());
        importReport.setStatus(importReport.getStatus() == Status.OK ? Status.WARNING : importReport.getStatus());
    }
    return importReport(importReport);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with BulkPatchParameters

use of org.hisp.dhis.jsonpatch.BulkPatchParameters in project dhis2-core by dhis2.

the class BulkPatchManagerTest method testApplyPatchInvalidPath.

@Test
void testApplyPatchInvalidPath() throws IOException {
    final BulkJsonPatch bulkJsonPatch = loadPatch("bulk_sharing_patch_invalid_path.json", BulkJsonPatch.class);
    BulkPatchParameters patchParameters = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = patchManager.applyPatch(bulkJsonPatch, patchParameters);
    assertEquals(0, patchedObjects.size());
    assertEquals(1, patchParameters.getErrorReportsCount());
    assertEquals(1, patchParameters.getErrorReportsCount(ErrorCode.E4032));
}
Also used : BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with BulkPatchParameters

use of org.hisp.dhis.jsonpatch.BulkPatchParameters in project dhis2-core by dhis2.

the class BulkPatchManagerTest method testApplyPatchOk.

@Test
void testApplyPatchOk() throws IOException {
    final BulkJsonPatch bulkJsonPatch = loadPatch("bulk_sharing_patch.json", BulkJsonPatch.class);
    BulkPatchParameters patchParameters = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = patchManager.applyPatch(bulkJsonPatch, patchParameters);
    assertEquals(2, patchedObjects.size());
    assertTrue(aclService.canRead(userA, patchedObjects.get(0)));
    assertTrue(aclService.canRead(userA, patchedObjects.get(1)));
    assertFalse(aclService.canRead(userC, patchedObjects.get(0)));
}
Also used : BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)8 BulkPatchParameters (org.hisp.dhis.jsonpatch.BulkPatchParameters)8 DhisSpringTest (org.hisp.dhis.DhisSpringTest)6 BulkJsonPatch (org.hisp.dhis.jsonpatch.BulkJsonPatch)6 Test (org.junit.jupiter.api.Test)6 List (java.util.List)2 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)2 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)2 BulkJsonPatches (org.hisp.dhis.jsonpatch.BulkJsonPatches)2 PatchMapping (org.springframework.web.bind.annotation.PatchMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 Collections.singletonList (java.util.Collections.singletonList)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1