use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class RelationshipsValidationHookTest method verifyValidationFailsOnInvalidFromTrackedEntityType.
@Test
void verifyValidationFailsOnInvalidFromTrackedEntityType() {
RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, PROGRAM_INSTANCE);
String trackedEntityUid = CodeGenerator.generateUid();
TrackedEntityType constraintTrackedEntityType = new TrackedEntityType();
constraintTrackedEntityType.setUid(CodeGenerator.generateUid());
relType.getFromConstraint().setTrackedEntityType(constraintTrackedEntityType);
Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity(trackedEntityUid).build()).to(RelationshipItem.builder().enrollment(CodeGenerator.generateUid()).build()).relationshipType(relType.getUid()).build();
when(preheat.getAll(RelationshipType.class)).thenReturn(Collections.singletonList(relType));
List<TrackedEntity> trackedEntities = new ArrayList<>();
TrackedEntity trackedEntity = new TrackedEntity();
trackedEntity.setTrackedEntity(trackedEntityUid);
trackedEntity.setTrackedEntityType(CodeGenerator.generateUid());
trackedEntities.add(trackedEntity);
when(bundle.getTrackedEntities()).thenReturn(trackedEntities);
reporter = new ValidationErrorReporter(ctx);
validationHook.validateRelationship(reporter, relationship);
hasTrackerError(reporter, E4014, RELATIONSHIP, relationship.getUid());
assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Type `from` constraint requires a Tracked Entity having type `" + constraintTrackedEntityType.getUid() + "` but `" + trackedEntity.getTrackedEntityType() + "` was found."));
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class RelationshipsValidationHookTest method verifyValidationFailsOnInvalidToTrackedEntityType.
@Test
void verifyValidationFailsOnInvalidToTrackedEntityType() {
RelationshipType relType = createRelTypeConstraint(PROGRAM_INSTANCE, TRACKED_ENTITY_INSTANCE);
String trackedEntityUid = CodeGenerator.generateUid();
TrackedEntityType constraintTrackedEntityType = new TrackedEntityType();
constraintTrackedEntityType.setUid(CodeGenerator.generateUid());
relType.getToConstraint().setTrackedEntityType(constraintTrackedEntityType);
Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().enrollment(CodeGenerator.generateUid()).build()).to(RelationshipItem.builder().trackedEntity(trackedEntityUid).build()).relationshipType(relType.getUid()).build();
when(preheat.getAll(RelationshipType.class)).thenReturn(Collections.singletonList(relType));
TrackedEntityType teiTrackedEntityType = new TrackedEntityType();
teiTrackedEntityType.setUid(CodeGenerator.generateUid());
TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
trackedEntityInstance.setUid(trackedEntityUid);
trackedEntityInstance.setTrackedEntityType(teiTrackedEntityType);
when(ctx.getTrackedEntityInstance(trackedEntityUid)).thenReturn(trackedEntityInstance);
reporter = new ValidationErrorReporter(ctx);
validationHook.validateRelationship(reporter, relationship);
hasTrackerError(reporter, E4014, RELATIONSHIP, relationship.getUid());
assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Type `to` constraint requires a Tracked Entity having type `" + constraintTrackedEntityType.getUid() + "` but `" + teiTrackedEntityType.getUid() + "` was found."));
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class RelationshipsValidationHookTest method verifyValidationFailsOnInvalidToConstraint.
@Test
void verifyValidationFailsOnInvalidToConstraint() {
RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, TRACKED_ENTITY_INSTANCE);
Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity(CodeGenerator.generateUid()).build()).to(RelationshipItem.builder().enrollment(CodeGenerator.generateUid()).build()).relationshipType(relType.getUid()).build();
when(preheat.getAll(RelationshipType.class)).thenReturn(Collections.singletonList(relType));
reporter = new ValidationErrorReporter(ctx);
validationHook.validateRelationship(reporter, relationship);
hasTrackerError(reporter, E4010, RELATIONSHIP, relationship.getUid());
assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Type `to` constraint requires a trackedEntity but a enrollment was found."));
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHookTest method shouldFailOptionSetNotValid.
@Test
void shouldFailOptionSetNotValid() {
TrackedEntityAttribute trackedEntityAttribute = getTrackedEntityAttributeWithOptionSet();
when(validationContext.getTrackedEntityAttribute(anyString())).thenReturn(trackedEntityAttribute);
when(validationContext.getTrackedEntityType(anyString())).thenReturn(new TrackedEntityType());
TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute("uid").value("wrongCode").build())).trackedEntityType("trackedEntityType").build();
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.E1125).count());
}
use of org.hisp.dhis.tracker.report.ValidationErrorReporter in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHookTest method shouldFailValidationWhenValueIsNullAndAttributeIsMandatory.
@Test
void shouldFailValidationWhenValueIsNullAndAttributeIsMandatory() {
TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute("trackedEntityAttribute").build())).trackedEntityType("trackedEntityType").build();
TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
trackedEntityAttribute.setUid("trackedEntityAttribute");
trackedEntityAttribute.setValueType(ValueType.TEXT);
trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
trackedEntityTypeAttribute.setMandatory(true);
TrackedEntityType trackedEntityType = new TrackedEntityType();
trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
when(validationContext.getTrackedEntityAttribute("trackedEntityAttribute")).thenReturn(trackedEntityAttribute);
when(validationContext.getTrackedEntityType(anyString())).thenReturn(trackedEntityType);
ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
assertTrue(reporter.hasErrors());
assertEquals(1, reporter.getReportList().size());
assertEquals(TrackerErrorCode.E1076, reporter.getReportList().get(0).getErrorCode());
}
Aggregations