use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHookTest method shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory.
@Test
void shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory() {
TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
trackedEntityAttribute.setUid("trackedEntityAttribute");
trackedEntityAttribute.setValueType(ValueType.TEXT);
trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
TrackedEntityType trackedEntityType = new TrackedEntityType();
trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
when(validationContext.getTrackedEntityAttribute("trackedEntityAttribute")).thenReturn(trackedEntityAttribute);
when(validationContext.getTrackedEntityType(anyString())).thenReturn(trackedEntityType);
TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute("trackedEntityAttribute").build())).trackedEntityType("trackedEntityType").build();
ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
assertFalse(reporter.hasErrors());
assertEquals(0, reporter.getReportList().size());
}
use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHookTest method shouldFailValidationMandatoryFields.
@Test
void shouldFailValidationMandatoryFields() {
String tet = "tet";
TrackedEntityType trackedEntityType = new TrackedEntityType();
TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
trackedEntityTypeAttribute.setMandatory(true);
TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
trackedEntityAttribute.setUid("c");
trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
when(validationContext.getTrackedEntityType(tet)).thenReturn(trackedEntityType);
TrackedEntity trackedEntity = TrackedEntity.builder().trackedEntityType(tet).attributes(Arrays.asList(Attribute.builder().attribute("a").value("value").build(), Attribute.builder().attribute("b").value("value").build())).build();
TrackedEntityAttribute contextAttribute = new TrackedEntityAttribute();
contextAttribute.setUid("uid");
contextAttribute.setValueType(ValueType.TEXT);
when(validationContext.getTrackedEntityAttribute(anyString())).thenReturn(contextAttribute);
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.E1090).count());
}
Aggregations