Search in sources :

Example 31 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.

the class RelationshipsValidationHookTest method verifyValidationFailsOnToWithMultipleDataset.

@Test
void verifyValidationFailsOnToWithMultipleDataset() {
    String relationshipUid = "nBx6auGDUHG";
    Relationship relationship = Relationship.builder().relationship(relationshipUid).relationshipType(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity(CodeGenerator.generateUid()).build()).to(RelationshipItem.builder().trackedEntity(CodeGenerator.generateUid()).enrollment(CodeGenerator.generateUid()).build()).build();
    RelationshipType relationshipType = new RelationshipType();
    relationshipType.setUid(relationship.getRelationshipType());
    RelationshipConstraint constraint = new RelationshipConstraint();
    constraint.setRelationshipEntity(RelationshipEntity.TRACKED_ENTITY_INSTANCE);
    relationshipType.setFromConstraint(constraint);
    relationshipType.setToConstraint(constraint);
    when(preheat.getAll(RelationshipType.class)).thenReturn(Collections.singletonList(relationshipType));
    reporter = new ValidationErrorReporter(ctx);
    validationHook.validateRelationship(reporter, relationship);
    hasTrackerError(reporter, E4001, RELATIONSHIP, relationship.getUid());
    assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Item `to` for Relationship `nBx6auGDUHG` is invalid: an Item can link only one Tracker entity."));
}
Also used : RelationshipConstraint(org.hisp.dhis.relationship.RelationshipConstraint) Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 32 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.

the class RelationshipsValidationHookTest method verifyValidationFailsWhenParentObjectFailed.

@Test
void verifyValidationFailsWhenParentObjectFailed() {
    RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, TRACKED_ENTITY_INSTANCE);
    when(preheat.getAll(RelationshipType.class)).thenReturn(Collections.singletonList(relType));
    reporter = new ValidationErrorReporter(ctx);
    Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity("validTrackedEntity").build()).to(RelationshipItem.builder().trackedEntity("notValidTrackedEntity").build()).relationshipType(relType.getUid()).build();
    TrackerErrorReport error = TrackerErrorReport.builder().uid(relationship.getTo().getTrackedEntity()).trackerType(TRACKED_ENTITY).errorCode(TrackerErrorCode.E9999).build(bundle);
    reporter.addError(error);
    validationHook.validateRelationship(reporter, relationship);
    hasTrackerError(reporter, TrackerErrorCode.E4011, RELATIONSHIP, relationship.getUid());
    assertThat(reporter.getReportList().stream().map(TrackerErrorReport::getErrorMessage).collect(Collectors.toList()), hasItem("Relationship: `" + relationship.getRelationship() + "` cannot be persisted because trackedEntity notValidTrackedEntity referenced by this relationship is not valid."));
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) Test(org.junit.jupiter.api.Test)

Example 33 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.

the class RelationshipsValidationHookTest method verifyValidationFailsOnInvalidToConstraintOfTypeProgramStage.

@Test
void verifyValidationFailsOnInvalidToConstraintOfTypeProgramStage() {
    RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, PROGRAM_STAGE_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 event but a enrollment was found."));
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 34 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.

the class PreCheckMetaValidationHookTest method verifyRelationshipValidationSuccess.

@Test
void verifyRelationshipValidationSuccess() {
    // given
    Relationship relationship = validRelationship();
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    // when
    when(ctx.getRelationShipType(RELATIONSHIP_TYPE_UID)).thenReturn(new RelationshipType());
    validatorToTest.validateRelationship(reporter, relationship);
    // then
    assertFalse(reporter.hasErrors());
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 35 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship in project dhis2-core by dhis2.

the class PreCheckMetaValidationHookTest method verifyRelationshipValidationFailsWhenRelationshipTypeIsNotPresentInDb.

@Test
void verifyRelationshipValidationFailsWhenRelationshipTypeIsNotPresentInDb() {
    // given
    Relationship relationship = validRelationship();
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    // when
    validatorToTest.validateRelationship(reporter, relationship);
    // then
    hasTrackerError(reporter, E4006, RELATIONSHIP, relationship.getUid());
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Aggregations

Relationship (org.hisp.dhis.tracker.domain.Relationship)38 Test (org.junit.jupiter.api.Test)34 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)23 RelationshipType (org.hisp.dhis.relationship.RelationshipType)14 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)9 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)7 ArrayList (java.util.ArrayList)6 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)6 Event (org.hisp.dhis.tracker.domain.Event)6 RelationshipConstraint (org.hisp.dhis.relationship.RelationshipConstraint)4 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)4 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)3 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)3 Collectors (java.util.stream.Collectors)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 CodeGenerator (org.hisp.dhis.common.CodeGenerator)2 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)2 ReferenceTrackerEntity (org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity)2 TrackerErrorReport (org.hisp.dhis.tracker.report.TrackerErrorReport)2