Search in sources :

Example 6 with BulkPatchParameters

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

the class BulkPatchManagerTest method testApplyPatchesOk.

@Test
void testApplyPatchesOk() throws IOException {
    final BulkJsonPatches bulkJsonPatch = loadPatch("bulk_sharing_patches.json", BulkJsonPatches.class);
    BulkPatchParameters patchParameters = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = patchManager.applyPatches(bulkJsonPatch, patchParameters);
    assertEquals(3, patchedObjects.size());
    assertFalse(aclService.canRead(userA, dataSetA));
    assertFalse(aclService.canRead(userB, dataSetA));
    IdentifiableObject patchedDataElementA = patchedObjects.stream().filter(de -> de.getUid().equals(dataElementA.getUid())).findFirst().get();
    IdentifiableObject patchedDataElementB = patchedObjects.stream().filter(de -> de.getUid().equals(dataElementB.getUid())).findFirst().get();
    IdentifiableObject patchedDataSetA = patchedObjects.stream().filter(de -> de.getUid().equals(dataSetA.getUid())).findFirst().get();
    assertTrue(aclService.canRead(userA, patchedDataElementA));
    assertTrue(aclService.canRead(userB, patchedDataElementB));
    assertTrue(aclService.canRead(userA, patchedDataSetA));
    assertTrue(aclService.canRead(userB, patchedDataSetA));
    assertFalse(aclService.canRead(userC, patchedDataElementA));
    assertFalse(aclService.canRead(userC, patchedDataSetA));
}
Also used : BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) BulkJsonPatches(org.hisp.dhis.jsonpatch.BulkJsonPatches) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 7 with BulkPatchParameters

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

the class BulkPatchManagerTest method testApplyPatchNotShareableSchema.

@Test
void testApplyPatchNotShareableSchema() throws IOException {
    final BulkJsonPatch bulkJsonPatch = loadPatch("bulk_sharing_patch_not_shareable.json", BulkJsonPatch.class);
    BulkPatchParameters patchParameters = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = patchManager.applyPatch(bulkJsonPatch, patchParameters);
    assertEquals(0, patchedObjects.size());
    assertEquals(2, patchParameters.getErrorReportsCount());
    assertEquals(1, patchParameters.getErrorReportsCount(ErrorCode.E3019));
    assertEquals(1, patchParameters.getErrorReportsCount(ErrorCode.E4014));
}
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 8 with BulkPatchParameters

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

the class MetadataImportExportController method bulkSharing.

@ResponseBody
@PatchMapping(value = "sharing", consumes = "application/json-patch+json", produces = APPLICATION_JSON_VALUE)
public WebMessage bulkSharing(@RequestParam(required = false, defaultValue = "false") boolean atomic, HttpServletRequest request) throws IOException {
    final BulkJsonPatches bulkJsonPatches = jsonMapper.readValue(request.getInputStream(), BulkJsonPatches.class);
    BulkPatchParameters patchParams = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = bulkPatchManager.applyPatches(bulkJsonPatches, 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 importParams = metadataImportService.getParamsFromMap(parameterValuesMap);
    importParams.setUser(currentUserService.getCurrentUser()).setImportStrategy(ImportStrategy.UPDATE).setAtomicMode(atomic ? AtomicMode.ALL : AtomicMode.NONE).addObjects(patchedObjects);
    ImportReport importReport = metadataImportService.importMetadata(importParams);
    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) BulkJsonPatches(org.hisp.dhis.jsonpatch.BulkJsonPatches) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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