use of org.hisp.dhis.tracker.domain.TrackedEntity in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHookTest method shouldFailMissingAttributeValue.
@Test
void shouldFailMissingAttributeValue() {
String tea = "tea";
String tet = "tet";
TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute(tea).build())).trackedEntityType(tet).build();
TrackedEntityType trackedEntityType = new TrackedEntityType();
TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
trackedEntityTypeAttribute.setMandatory(true);
TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
trackedEntityAttribute.setUid(tea);
trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
when(validationContext.getTrackedEntityType(tet)).thenReturn(trackedEntityType);
when(validationContext.getTrackedEntityAttribute(tea)).thenReturn(trackedEntityAttribute);
ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
assertTrue(reporter.hasErrors());
assertEquals(1, reporter.getReportList().size());
assertEquals(1, reporter.getReportList().stream().filter(e -> e.getErrorCode() == TrackerErrorCode.E1076).count());
}
use of org.hisp.dhis.tracker.domain.TrackedEntity in project dhis2-core by dhis2.
the class AssignValueImplementer method addOrOverwriteAttribute.
private void addOrOverwriteAttribute(EnrollmentActionRule actionRule, TrackerBundle bundle) {
Enrollment enrollment = bundle.getEnrollment(actionRule.getEnrollment()).get();
Optional<TrackedEntity> trackedEntity = bundle.getTrackedEntity(enrollment.getTrackedEntity());
List<Attribute> attributes;
if (trackedEntity.isPresent()) {
attributes = trackedEntity.get().getAttributes();
Optional<Attribute> optionalAttribute = attributes.stream().filter(at -> at.getAttribute().equals(actionRule.getField())).findAny();
if (optionalAttribute.isPresent()) {
optionalAttribute.get().setValue(actionRule.getData());
return;
}
}
attributes = enrollment.getAttributes();
Optional<Attribute> optionalAttribute = attributes.stream().filter(at -> at.getAttribute().equals(actionRule.getField())).findAny();
if (optionalAttribute.isPresent()) {
optionalAttribute.get().setValue(actionRule.getData());
} else {
attributes.add(createAttribute(actionRule.getField(), actionRule.getData()));
}
}
use of org.hisp.dhis.tracker.domain.TrackedEntity in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHook method validateAttributes.
protected void validateAttributes(ValidationErrorReporter reporter, TrackedEntity trackedEntity, TrackedEntityInstance tei, OrganisationUnit orgUnit, TrackedEntityType trackedEntityType) {
checkNotNull(trackedEntity, TrackerImporterAssertErrors.TRACKED_ENTITY_CANT_BE_NULL);
checkNotNull(trackedEntityType, TrackerImporterAssertErrors.TRACKED_ENTITY_TYPE_CANT_BE_NULL);
Map<String, TrackedEntityAttributeValue> valueMap = new HashMap<>();
if (tei != null) {
valueMap = tei.getTrackedEntityAttributeValues().stream().collect(Collectors.toMap(v -> v.getAttribute().getUid(), v -> v));
}
for (Attribute attribute : trackedEntity.getAttributes()) {
TrackedEntityAttribute tea = reporter.getValidationContext().getTrackedEntityAttribute(attribute.getAttribute());
if (tea == null) {
reporter.addError(trackedEntity, E1006, attribute.getAttribute());
continue;
}
if (attribute.getValue() == null) {
Optional<TrackedEntityTypeAttribute> optionalTea = Optional.of(trackedEntityType).map(tet -> tet.getTrackedEntityTypeAttributes().stream()).flatMap(tetAtts -> tetAtts.filter(teaAtt -> teaAtt.getTrackedEntityAttribute().getUid().equals(attribute.getAttribute()) && teaAtt.isMandatory() != null && teaAtt.isMandatory()).findFirst());
if (optionalTea.isPresent())
reporter.addError(trackedEntity, E1076, TrackedEntityAttribute.class.getSimpleName(), attribute.getAttribute());
continue;
}
validateAttributeValue(reporter, trackedEntity, tea, attribute.getValue());
validateAttrValueType(reporter, trackedEntity, attribute, tea);
validateOptionSet(reporter, trackedEntity, tea, attribute.getValue());
validateAttributeUniqueness(reporter, trackedEntity, attribute.getValue(), tea, tei, orgUnit);
validateFileNotAlreadyAssigned(reporter, trackedEntity, attribute, valueMap);
}
}
use of org.hisp.dhis.tracker.domain.TrackedEntity in project dhis2-core by dhis2.
the class TrackerBundleTest method testBasicSetup1.
@Test
void testBasicSetup1() {
TrackerBundle trackerBundle = TrackerBundle.builder().atomicMode(AtomicMode.ALL).validationMode(ValidationMode.SKIP).trackedEntities(Collections.singletonList(new TrackedEntity())).enrollments(Collections.singletonList(new Enrollment())).events(Collections.singletonList(new Event())).build();
assertEquals(AtomicMode.ALL, trackerBundle.getAtomicMode());
assertSame(trackerBundle.getValidationMode(), ValidationMode.SKIP);
assertFalse(trackerBundle.getTrackedEntities().isEmpty());
assertFalse(trackerBundle.getEnrollments().isEmpty());
assertFalse(trackerBundle.getEvents().isEmpty());
}
use of org.hisp.dhis.tracker.domain.TrackedEntity in project dhis2-core by dhis2.
the class ClassBasedSupplierTest method setUp.
@BeforeEach
public void setUp() {
classBasedSupplier = new ClassBasedSupplier(identifierCollector, strategiesMap);
classBasedSupplier.setApplicationContext(applicationContext);
TrackerPreheat trackerPreheat = new TrackerPreheat();
when(identifierCollector.collect(trackerImportParams, trackerPreheat.getDefaults())).thenReturn(new HashMap<Class<?>, Set<String>>() {
{
put(TrackedEntity.class, new HashSet<>(Collections.singletonList("trackedEntity")));
}
});
}
Aggregations