use of org.hisp.dhis.preheat.PreheatErrorReport in project dhis2-core by dhis2.
the class ObjectBundleServiceTest method testPreheatValidations.
@Test
public void testPreheatValidations() throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate1.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
params.setImportStrategy(ImportStrategy.CREATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertFalse(validate.isEmpty());
List<ObjectReport> objectReports = validate.getObjectReports(DataElement.class);
assertFalse(objectReports.isEmpty());
for (ObjectReport objectReport : objectReports) {
for (ErrorCode errorCode : objectReport.getErrorCodes()) {
List<ErrorReport> errorReports = objectReport.getErrorReportsByCode().get(errorCode);
assertFalse(errorReports.isEmpty());
for (ErrorReport errorReport : errorReports) {
assertTrue(PreheatErrorReport.class.isInstance(errorReport));
PreheatErrorReport preheatErrorReport = (PreheatErrorReport) errorReport;
assertEquals(PreheatIdentifier.UID, preheatErrorReport.getPreheatIdentifier());
if (DataElementCategoryCombo.class.isInstance(preheatErrorReport.getValue())) {
assertEquals("p0KPaWEg3cf", preheatErrorReport.getObjectReference().getUid());
} else if (User.class.isInstance(preheatErrorReport.getValue())) {
assertEquals("GOLswS44mh8", preheatErrorReport.getObjectReference().getUid());
} else if (OptionSet.class.isInstance(preheatErrorReport.getValue())) {
assertEquals("pQYCiuosBnZ", preheatErrorReport.getObjectReference().getUid());
}
}
}
}
}
use of org.hisp.dhis.preheat.PreheatErrorReport in project dhis2-core by dhis2.
the class ObjectBundleServiceTest method testPreheatValidationsWithCatCombo.
@Test
public void testPreheatValidationsWithCatCombo() throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate1.json").getInputStream(), RenderFormat.JSON);
DataElementCategoryCombo categoryCombo = manager.getByName(DataElementCategoryCombo.class, "default");
categoryCombo.setUid("p0KPaWEg3cf");
manager.update(categoryCombo);
OptionSet optionSet = new OptionSet("OptionSet: pQYCiuosBnZ", ValueType.TEXT);
optionSet.setAutoFields();
optionSet.setUid("pQYCiuosBnZ");
manager.save(optionSet);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
params.setImportStrategy(ImportStrategy.CREATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertFalse(validate.getTypeReportMap().isEmpty());
List<ObjectReport> objectReports = validate.getObjectReports(DataElement.class);
assertFalse(objectReports.isEmpty());
for (ObjectReport objectReport : objectReports) {
for (ErrorCode errorCode : objectReport.getErrorCodes()) {
List<ErrorReport> errorReports = objectReport.getErrorReportsByCode().get(errorCode);
assertFalse(errorReports.isEmpty());
for (ErrorReport errorReport : errorReports) {
assertTrue(PreheatErrorReport.class.isInstance(errorReport));
PreheatErrorReport preheatErrorReport = (PreheatErrorReport) errorReport;
assertEquals(PreheatIdentifier.UID, preheatErrorReport.getPreheatIdentifier());
if (DataElementCategoryCombo.class.isInstance(preheatErrorReport.getValue())) {
assertFalse(true);
} else if (User.class.isInstance(preheatErrorReport.getValue())) {
assertEquals("GOLswS44mh8", preheatErrorReport.getObjectReference().getUid());
} else if (OptionSet.class.isInstance(preheatErrorReport.getValue())) {
assertFalse(true);
}
}
}
}
}
use of org.hisp.dhis.preheat.PreheatErrorReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
if (object == null) {
return preheatErrorReports;
}
Schema schema = schemaService.getDynamicSchema(object.getClass());
schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).forEach(p -> {
if (skipCheck(p.getKlass()) || skipCheck(p.getItemKlass())) {
return;
}
if (!p.isCollection()) {
IdentifiableObject refObject = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
if (!("user".equals(p.getName()) && User.class.isAssignableFrom(p.getKlass()) && skipSharing)) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getName()));
}
}
} else {
Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
for (IdentifiableObject refObject : refObjects) {
if (Preheat.isDefault(refObject))
continue;
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getCollectionName()));
} else {
objects.add(refObject);
}
}
ReflectionUtils.invokeMethod(object, p.getSetterMethod(), objects);
}
});
if (schema.havePersistedProperty("attributeValues")) {
object.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute() != null && preheat.get(identifier, attributeValue.getAttribute()) == null).forEach(attributeValue -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(attributeValue.getAttribute()), identifier.getIdentifiersWithName(object), "attributeValues")));
}
if (schema.havePersistedProperty("userGroupAccesses")) {
object.getUserGroupAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUserGroup() != null && preheat.get(identifier, userGroupAccess.getUserGroup()) == null).forEach(userGroupAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userGroupAccesses.getUserGroup()), identifier.getIdentifiersWithName(object), "userGroupAccesses")));
}
if (schema.havePersistedProperty("userAccesses")) {
object.getUserAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUser() != null && preheat.get(identifier, userGroupAccess.getUser()) == null).forEach(userAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userAccesses.getUser()), identifier.getIdentifiersWithName(object), "userAccesses")));
}
return preheatErrorReports;
}
use of org.hisp.dhis.preheat.PreheatErrorReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkReferences.
private TypeReport checkReferences(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
TypeReport typeReport = new TypeReport(klass);
if (objects.isEmpty()) {
return typeReport;
}
for (int idx = 0; idx < objects.size(); idx++) {
IdentifiableObject object = objects.get(idx);
List<PreheatErrorReport> errorReports = checkReferences(object, preheat, identifier, skipSharing);
if (errorReports.isEmpty())
continue;
ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
objectReport.addErrorReports(errorReports);
typeReport.addObjectReport(objectReport);
}
return typeReport;
}
Aggregations