Search in sources :

Example 16 with ObjectBundleValidationReport

use of org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testDeleteRequiresValidReferencesAUTO.

@Test
public void testDeleteRequiresValidReferencesAUTO() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate6.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
    params.setPreheatIdentifier(PreheatIdentifier.AUTO);
    params.setImportStrategy(ImportStrategy.DELETE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.getTypeReportMap(DataElement.class).getObjectReports().isEmpty());
    assertEquals(3, validate.getErrorReportsByCode(DataElement.class, ErrorCode.E5001).size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 17 with ObjectBundleValidationReport

use of org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testCreateMetadataWithSuperuserRoleInjected.

@Test
public void testCreateMetadataWithSuperuserRoleInjected() throws IOException {
    createUserAndInjectSecurityContext(true);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_superuser_bug.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE_AND_UPDATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.isEmpty());
    assertEquals(1, validate.getErrorReportsByCode(UserAuthorityGroup.class, ErrorCode.E5003).size());
}
Also used : ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 18 with ObjectBundleValidationReport

use of org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testUpdateMetadataWithValidationRules.

@Test
public void testUpdateMetadataWithValidationRules() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_with_vr.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertTrue(validate.getErrorReports().isEmpty());
    objectBundleService.commit(bundle);
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_with_vr_update.json").getInputStream(), RenderFormat.JSON);
    params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setObjects(metadata);
    bundle = objectBundleService.create(params);
    validate = objectBundleValidationService.validate(bundle);
    assertTrue(validate.getErrorReports().isEmpty());
    objectBundleService.commit(bundle);
    List<DataSet> dataSets = manager.getAll(DataSet.class);
    List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
    List<DataElement> dataElements = manager.getAll(DataElement.class);
    List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
    List<User> users = manager.getAll(User.class);
    List<ValidationRule> validationRules = manager.getAll(ValidationRule.class);
    assertFalse(dataSets.isEmpty());
    assertFalse(organisationUnits.isEmpty());
    assertFalse(dataElements.isEmpty());
    assertFalse(users.isEmpty());
    assertFalse(userRoles.isEmpty());
    assertEquals(2, validationRules.size());
    ValidationRule validationRule1 = manager.get(ValidationRule.class, "ztzsVjSIWg7");
    assertNotNull(validationRule1.getLeftSide());
    assertNotNull(validationRule1.getRightSide());
    ValidationRule validationRule2 = manager.get(ValidationRule.class, "TGvH4Hiyduc");
    assertNotNull(validationRule2.getLeftSide());
    assertNotNull(validationRule2.getRightSide());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) DataSet(org.hisp.dhis.dataset.DataSet) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ValidationRule(org.hisp.dhis.validation.ValidationRule) DataElement(org.hisp.dhis.dataelement.DataElement) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) List(java.util.List) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 19 with ObjectBundleValidationReport

use of org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport in project dhis2-core by dhis2.

the class DefaultMetadataImportService method importMetadata.

@Override
public ImportReport importMetadata(MetadataImportParams params) {
    Timer timer = new SystemTimer().start();
    ImportReport importReport = new ImportReport();
    importReport.setImportParams(params);
    importReport.setStatus(Status.OK);
    if (params.getUser() == null) {
        params.setUser(currentUserService.getCurrentUser());
    }
    String message = "(" + params.getUsername() + ") Import:Start";
    log.info(message);
    if (params.hasTaskId()) {
        notifier.notify(params.getTaskId(), message);
    }
    ObjectBundleParams bundleParams = params.toObjectBundleParams();
    ObjectBundle bundle = objectBundleService.create(bundleParams);
    prepareBundle(bundle);
    ObjectBundleValidationReport validationReport = objectBundleValidationService.validate(bundle);
    importReport.addTypeReports(validationReport.getTypeReportMap());
    if (!(!validationReport.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode())) {
        Timer commitTimer = new SystemTimer().start();
        ObjectBundleCommitReport commitReport = objectBundleService.commit(bundle);
        importReport.addTypeReports(commitReport.getTypeReportMap());
        if (!importReport.getErrorReports().isEmpty()) {
            importReport.setStatus(Status.WARNING);
        }
        log.info("(" + bundle.getUsername() + ") Import:Commit took " + commitTimer.toString());
    } else {
        importReport.getStats().ignored();
        importReport.getTypeReports().forEach(tr -> tr.getStats().ignored());
        importReport.setStatus(Status.ERROR);
    }
    message = "(" + bundle.getUsername() + ") Import:Done took " + timer.toString();
    log.info(message);
    if (bundle.hasTaskId()) {
        notifier.notify(bundle.getTaskId(), NotificationLevel.INFO, message, true).addTaskSummary(bundle.getTaskId(), importReport);
    }
    if (ObjectBundleMode.VALIDATE == params.getImportMode()) {
        return importReport;
    }
    Lists.newArrayList(importReport.getTypeReportMap().keySet()).forEach(typeReportKey -> {
        if (importReport.getTypeReportMap().get(typeReportKey).getStats().getTotal() == 0) {
            importReport.getTypeReportMap().remove(typeReportKey);
            return;
        }
        TypeReport typeReport = importReport.getTypeReportMap().get(typeReportKey);
        if (ImportReportMode.ERRORS == params.getImportReportMode()) {
            Lists.newArrayList(typeReport.getObjectReportMap().keySet()).forEach(objectReportKey -> {
                if (typeReport.getObjectReportMap().get(objectReportKey).getErrorReportsByCode().isEmpty()) {
                    typeReport.getObjectReportMap().remove(objectReportKey);
                }
            });
        }
        if (ImportReportMode.DEBUG != params.getImportReportMode()) {
            typeReport.getObjectReports().forEach(objectReport -> objectReport.setDisplayName(null));
        }
    });
    return importReport;
}
Also used : ObjectBundleParams(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams) ObjectBundleCommitReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) Timer(org.hisp.dhis.commons.timer.Timer) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) TypeReport(org.hisp.dhis.feedback.TypeReport) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer)

Example 20 with ObjectBundleValidationReport

use of org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport in project dhis2-core by dhis2.

the class DefaultObjectBundleValidationService method validate.

@Override
public ObjectBundleValidationReport validate(ObjectBundle bundle) {
    Timer timer = new SystemTimer().start();
    ObjectBundleValidationReport validation = new ObjectBundleValidationReport();
    if ((bundle.getUser() == null || bundle.getUser().isSuper()) && bundle.isSkipValidation()) {
        log.warn("Skipping validation for metadata import by user '" + bundle.getUsername() + "'. Not recommended.");
        return validation;
    }
    List<Class<? extends IdentifiableObject>> klasses = getSortedClasses(bundle);
    for (Class<? extends IdentifiableObject> klass : klasses) {
        TypeReport typeReport = new TypeReport(klass);
        List<IdentifiableObject> nonPersistedObjects = bundle.getObjects(klass, false);
        List<IdentifiableObject> persistedObjects = bundle.getObjects(klass, true);
        List<IdentifiableObject> allObjects = bundle.getObjectMap().get(klass);
        handleDefaults(nonPersistedObjects);
        handleDefaults(persistedObjects);
        typeReport.merge(checkDuplicateIds(klass, persistedObjects, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
        if (bundle.getImportMode().isCreateAndUpdate()) {
            typeReport.merge(runValidationHooks(klass, nonPersistedObjects, bundle));
            typeReport.merge(runValidationHooks(klass, persistedObjects, bundle));
            typeReport.merge(validateSecurity(klass, nonPersistedObjects, bundle, ImportStrategy.CREATE));
            typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.UPDATE));
            typeReport.merge(validateBySchemas(klass, nonPersistedObjects, bundle));
            typeReport.merge(validateBySchemas(klass, persistedObjects, bundle));
            typeReport.merge(checkUniqueness(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueness(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
            if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
                typeReport.getStats().incIgnored();
            }
            typeReport.getStats().incCreated(nonPersistedObjects.size());
            typeReport.getStats().incUpdated(persistedObjects.size());
            typeReport.merge(checkReferences);
        } else if (bundle.getImportMode().isCreate()) {
            typeReport.merge(runValidationHooks(klass, nonPersistedObjects, bundle));
            typeReport.merge(validateSecurity(klass, nonPersistedObjects, bundle, ImportStrategy.CREATE));
            typeReport.merge(validateForCreate(klass, persistedObjects, bundle));
            typeReport.merge(validateBySchemas(klass, nonPersistedObjects, bundle));
            typeReport.merge(checkUniqueness(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
            if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
                typeReport.getStats().incIgnored();
            }
            typeReport.getStats().incCreated(nonPersistedObjects.size());
            typeReport.merge(checkReferences);
        } else if (bundle.getImportMode().isUpdate()) {
            typeReport.merge(runValidationHooks(klass, persistedObjects, bundle));
            typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.UPDATE));
            typeReport.merge(validateForUpdate(klass, nonPersistedObjects, bundle));
            typeReport.merge(validateBySchemas(klass, persistedObjects, bundle));
            typeReport.merge(checkUniqueness(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
            if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
                typeReport.getStats().incIgnored();
            }
            typeReport.getStats().incUpdated(persistedObjects.size());
            typeReport.merge(checkReferences);
        } else if (bundle.getImportMode().isDelete()) {
            typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.DELETE));
            typeReport.merge(validateForDelete(klass, nonPersistedObjects, bundle));
            typeReport.getStats().incDeleted(persistedObjects.size());
        }
        validation.addTypeReport(typeReport);
    }
    validateAtomicity(bundle, validation);
    bundle.setObjectBundleStatus(ObjectBundleStatus.VALIDATED);
    log.info("(" + bundle.getUsername() + ") Import:Validation took " + timer.toString());
    return validation;
}
Also used : SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) Timer(org.hisp.dhis.commons.timer.Timer) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) TypeReport(org.hisp.dhis.feedback.TypeReport) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)40 Test (org.junit.Test)38 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)37 List (java.util.List)36 DhisSpringTest (org.hisp.dhis.DhisSpringTest)36 ClassPathResource (org.springframework.core.io.ClassPathResource)36 DataElement (org.hisp.dhis.dataelement.DataElement)21 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)15 User (org.hisp.dhis.user.User)14 DataSet (org.hisp.dhis.dataset.DataSet)11 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)10 ObjectReport (org.hisp.dhis.feedback.ObjectReport)6 Section (org.hisp.dhis.dataset.Section)4 TypeReport (org.hisp.dhis.feedback.TypeReport)4 ValidationRule (org.hisp.dhis.validation.ValidationRule)4 Program (org.hisp.dhis.program.Program)3 ProgramStage (org.hisp.dhis.program.ProgramStage)3 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)3 Chart (org.hisp.dhis.chart.Chart)2 SystemTimer (org.hisp.dhis.commons.timer.SystemTimer)2