Search in sources :

Example 11 with Relationship

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

the class RelationshipsValidationHookTest method verifyValidationFailsOnFromWithMultipleDataset.

@Test
void verifyValidationFailsOnFromWithMultipleDataset() {
    String relationshipUid = "nBx6auGDUHG";
    Relationship relationship = Relationship.builder().relationship(relationshipUid).relationshipType(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity(CodeGenerator.generateUid()).enrollment(CodeGenerator.generateUid()).build()).to(RelationshipItem.builder().trackedEntity(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);
    assertTrue(reporter.hasErrors());
    assertThat(reporter.getReportList().get(0).getErrorCode(), is(TrackerErrorCode.E4001));
    assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Item `from` 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 12 with Relationship

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

the class RelationshipsValidationHookTest method verifyValidationFailsOnInvalidRelationshipType.

@Test
void verifyValidationFailsOnInvalidRelationshipType() {
    Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).relationshipType("do-not-exist").from(RelationshipItem.builder().trackedEntity(CodeGenerator.generateUid()).build()).to(RelationshipItem.builder().trackedEntity(CodeGenerator.generateUid()).build()).build();
    reporter = new ValidationErrorReporter(ctx);
    validationHook.validateRelationship(reporter, relationship);
    hasTrackerError(reporter, E4009, 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)

Example 13 with Relationship

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

the class RelationshipsValidationHookTest method verifyValidationFailsOnFromWithNoDataset.

@Test
void verifyValidationFailsOnFromWithNoDataset() {
    String relationshipUid = "nBx6auGDUHG";
    Relationship relationship = Relationship.builder().relationship(relationshipUid).relationshipType(CodeGenerator.generateUid()).from(RelationshipItem.builder().build()).to(RelationshipItem.builder().trackedEntity(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, E4013, RELATIONSHIP, relationship.getUid());
    assertThat(reporter.getReportList().get(0).getErrorMessage(), is("Relationship Type `from` constraint is missing trackedEntity."));
}
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 14 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship 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."));
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ArrayList(java.util.ArrayList) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 15 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship 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."));
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) 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