Search in sources :

Example 16 with Relationship

use of org.hisp.dhis.tracker.domain.Relationship 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."));
}
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 17 with Relationship

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

the class DefaultTrackerObjectsDeletionService method deleteRelationShips.

@Override
public TrackerTypeReport deleteRelationShips(TrackerBundle bundle) {
    TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.RELATIONSHIP);
    List<Relationship> relationships = bundle.getRelationships();
    for (int idx = 0; idx < relationships.size(); idx++) {
        String uid = relationships.get(idx).getRelationship();
        TrackerObjectReport objectReport = new TrackerObjectReport(TrackerType.RELATIONSHIP, uid, idx);
        org.hisp.dhis.relationship.Relationship relationship = relationshipService.getRelationship(uid);
        relationshipService.deleteRelationship(relationship);
        typeReport.getStats().incDeleted();
        typeReport.addObjectReport(objectReport);
    }
    return typeReport;
}
Also used : TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport)

Example 18 with Relationship

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

the class StrategyPreProcessor method preProcessRelationships.

public void preProcessRelationships(TrackerBundle bundle) {
    for (Relationship relationship : bundle.getRelationships()) {
        TrackerImportStrategy importStrategy = bundle.getImportStrategy();
        org.hisp.dhis.relationship.Relationship existingRelationship = bundle.getPreheat().getRelationship(TrackerIdScheme.UID, relationship);
        if (importStrategy.isCreateAndUpdate()) {
            if (existingRelationship == null) {
                bundle.setStrategy(relationship, TrackerImportStrategy.CREATE);
            } else {
                bundle.setStrategy(relationship, TrackerImportStrategy.UPDATE);
            }
        } else {
            bundle.setStrategy(relationship, importStrategy);
        }
    }
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy)

Example 19 with Relationship

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

the class DuplicateRelationshipsPreProcessorTest method test_on_identical_rels_1_is_removed.

/*
     * Verifies that:
     *
     * - given 2 identical relationships
     *
     * - one is removed
     */
@Test
void test_on_identical_rels_1_is_removed() {
    String relType = REL_TYPE_NONBIDIRECTIONAL_UID;
    String fromTeiUid = CodeGenerator.generateUid();
    String toTeiUid = CodeGenerator.generateUid();
    Relationship relationship1 = Relationship.builder().relationship(CodeGenerator.generateUid()).relationshipType(relType).from(RelationshipItem.builder().trackedEntity(fromTeiUid).build()).to(RelationshipItem.builder().trackedEntity(toTeiUid).build()).build();
    Relationship relationship2 = Relationship.builder().relationship(CodeGenerator.generateUid()).relationshipType(relType).from(RelationshipItem.builder().trackedEntity(fromTeiUid).build()).to(RelationshipItem.builder().trackedEntity(toTeiUid).build()).build();
    TrackerBundle bundle = TrackerBundle.builder().preheat(this.preheat).relationships(Lists.newArrayList(relationship1, relationship2)).build();
    preProcessor.process(bundle);
    assertThat(bundle.getRelationships(), hasSize(1));
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Test(org.junit.jupiter.api.Test)

Example 20 with Relationship

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

the class DuplicateRelationshipsPreProcessorTest method test_on_identical_rels_relType_bi_1_is_removed.

/*
     * Verifies that:
     *
     * - given 2 identical relationships
     *
     * - and relationship type's bidirectional property = true
     *
     * - one is removed
     */
@Test
void test_on_identical_rels_relType_bi_1_is_removed() {
    String relType = REL_TYPE_BIDIRECTIONAL_UID;
    String fromTeiUid = CodeGenerator.generateUid();
    String toTeiUid = CodeGenerator.generateUid();
    Relationship relationship1 = Relationship.builder().relationship(CodeGenerator.generateUid()).relationshipType(relType).bidirectional(true).from(RelationshipItem.builder().trackedEntity(fromTeiUid).build()).to(RelationshipItem.builder().trackedEntity(toTeiUid).build()).build();
    Relationship relationship2 = Relationship.builder().relationship(CodeGenerator.generateUid()).relationshipType(relType).bidirectional(true).from(RelationshipItem.builder().trackedEntity(fromTeiUid).build()).to(RelationshipItem.builder().trackedEntity(toTeiUid).build()).build();
    TrackerBundle bundle = TrackerBundle.builder().preheat(this.preheat).relationships(Lists.newArrayList(relationship1, relationship2)).build();
    preProcessor.process(bundle);
    assertThat(bundle.getRelationships(), hasSize(1));
}
Also used : Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) 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