Search in sources :

Example 36 with TrackedEntity

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());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 37 with TrackedEntity

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()));
    }
}
Also used : DataValue(org.hisp.dhis.tracker.domain.DataValue) RuleActionAssign(org.hisp.dhis.rules.models.RuleActionAssign) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DataElement(org.hisp.dhis.dataelement.DataElement) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Lists(com.google.common.collect.Lists) Map(java.util.Map) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) ProgramRuleIssue(org.hisp.dhis.tracker.programrule.ProgramRuleIssue) Event(org.hisp.dhis.tracker.domain.Event) EventActionRule(org.hisp.dhis.tracker.programrule.EventActionRule) EnrollmentActionRule(org.hisp.dhis.tracker.programrule.EnrollmentActionRule) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Set(java.util.Set) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Sets(com.google.common.collect.Sets) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) List(java.util.List) Component(org.springframework.stereotype.Component) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) IssueType(org.hisp.dhis.tracker.programrule.IssueType) SettingKey(org.hisp.dhis.setting.SettingKey) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) RuleActionImplementer(org.hisp.dhis.tracker.programrule.RuleActionImplementer) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment)

Example 38 with TrackedEntity

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);
    }
}
Also used : Attribute(org.hisp.dhis.tracker.domain.Attribute) Constant(org.hisp.dhis.tracker.util.Constant) HashMap(java.util.HashMap) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) E1084(org.hisp.dhis.tracker.report.TrackerErrorCode.E1084) E1085(org.hisp.dhis.tracker.report.TrackerErrorCode.E1085) TRACKED_ENTITY_ATTRIBUTE_VALUE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.TRACKED_ENTITY_ATTRIBUTE_VALUE_CANT_BE_NULL) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Map(java.util.Map) E1090(org.hisp.dhis.tracker.report.TrackerErrorCode.E1090) ATTRIBUTE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.ATTRIBUTE_CANT_BE_NULL) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) FileResource(org.hisp.dhis.fileresource.FileResource) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) E1077(org.hisp.dhis.tracker.report.TrackerErrorCode.E1077) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) E1076(org.hisp.dhis.tracker.report.TrackerErrorCode.E1076) Set(java.util.Set) E1112(org.hisp.dhis.tracker.report.TrackerErrorCode.E1112) TrackedAttributeValidationService(org.hisp.dhis.tracker.validation.service.attribute.TrackedAttributeValidationService) Collectors(java.util.stream.Collectors) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ValidationUtils.dataValueIsValid(org.hisp.dhis.system.util.ValidationUtils.dataValueIsValid) Component(org.springframework.stereotype.Component) E1009(org.hisp.dhis.tracker.report.TrackerErrorCode.E1009) E1006(org.hisp.dhis.tracker.report.TrackerErrorCode.E1006) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute)

Example 39 with TrackedEntity

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());
}
Also used : TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Event(org.hisp.dhis.tracker.domain.Event) Test(org.junit.jupiter.api.Test)

Example 40 with TrackedEntity

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")));
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) HashSet(java.util.HashSet) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)69 Test (org.junit.jupiter.api.Test)55 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)45 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)19 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)18 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)17 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)17 Event (org.hisp.dhis.tracker.domain.Event)17 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)13 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 List (java.util.List)10 Attribute (org.hisp.dhis.tracker.domain.Attribute)10 Relationship (org.hisp.dhis.tracker.domain.Relationship)10 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)10 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)10 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)10 Mockito.when (org.mockito.Mockito.when)10 Optional (java.util.Optional)9 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)9