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