Search in sources :

Example 1 with TrackerErrorReport

use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.

the class PreCheckDataRelationsValidationHookTest method verifyValidationFailsWhenLinkedTrackedEntityIsNotFound.

@Test
void verifyValidationFailsWhenLinkedTrackedEntityIsNotFound() {
    RelationshipType relType = createRelTypeConstraint(TRACKED_ENTITY_INSTANCE, TRACKED_ENTITY_INSTANCE);
    Relationship relationship = Relationship.builder().relationship(CodeGenerator.generateUid()).from(RelationshipItem.builder().trackedEntity("validTrackedEntity").build()).to(RelationshipItem.builder().trackedEntity("anotherValidTrackedEntity").build()).relationshipType(relType.getUid()).build();
    hook.validateRelationship(reporter, relationship);
    assertTrue(reporter.hasErrors());
    assertTrue(reporter.hasErrorReport(r -> r.getErrorCode() == TrackerErrorCode.E4012));
    assertThat(reporter.getReportList().stream().map(TrackerErrorReport::getErrorMessage).collect(Collectors.toList()), hasItem("Could not find `trackedEntity`: `validTrackedEntity`, linked to Relationship."));
    assertThat(reporter.getReportList().stream().map(TrackerErrorReport::getErrorMessage).collect(Collectors.toList()), hasItem("Could not find `trackedEntity`: `anotherValidTrackedEntity`, linked to Relationship."));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CategoryService(org.hisp.dhis.category.CategoryService) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CategoryOption(org.hisp.dhis.category.CategoryOption) TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) RelationshipConstraint(org.hisp.dhis.relationship.RelationshipConstraint) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ReferenceTrackerEntity(org.hisp.dhis.tracker.preheat.ReferenceTrackerEntity) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Category(org.hisp.dhis.category.Category) RelationshipItem(org.hisp.dhis.tracker.domain.RelationshipItem) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgramType(org.hisp.dhis.program.ProgramType) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DataDimensionType(org.hisp.dhis.common.DataDimensionType) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Relationship(org.hisp.dhis.tracker.domain.Relationship) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackerIdentifierParams(org.hisp.dhis.tracker.TrackerIdentifierParams) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) RelationshipType(org.hisp.dhis.relationship.RelationshipType) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TRACKED_ENTITY_INSTANCE(org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE) RelationshipEntity(org.hisp.dhis.relationship.RelationshipEntity) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Event(org.hisp.dhis.tracker.domain.Event) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ProgramStage(org.hisp.dhis.program.ProgramStage) Mockito.verify(org.mockito.Mockito.verify) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Matchers.hasItem(org.hamcrest.Matchers.hasItem) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) TrackerPreheat(org.hisp.dhis.tracker.preheat.TrackerPreheat) Relationship(org.hisp.dhis.tracker.domain.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 2 with TrackerErrorReport

use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.

the class PreCheckSecurityOwnershipValidationHook method checkOrgUnitInCaptureScope.

private void checkOrgUnitInCaptureScope(ValidationErrorReporter reporter, TrackerDto dto, OrganisationUnit orgUnit) {
    TrackerBundle bundle = reporter.getValidationContext().getBundle();
    User user = bundle.getUser();
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(orgUnit, ORGANISATION_UNIT_CANT_BE_NULL);
    if (!organisationUnitService.isInUserHierarchyCached(user, orgUnit)) {
        TrackerErrorReport error = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1000).addArg(user).addArg(orgUnit).build(bundle);
        reporter.addError(error);
    }
}
Also used : User(org.hisp.dhis.user.User) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Example 3 with TrackerErrorReport

use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.

the class PreCheckSecurityOwnershipValidationHook method validateTrackedEntity.

@Override
public void validateTrackedEntity(ValidationErrorReporter reporter, TrackedEntity trackedEntity) {
    TrackerImportValidationContext context = reporter.getValidationContext();
    TrackerImportStrategy strategy = context.getStrategy(trackedEntity);
    TrackerBundle bundle = context.getBundle();
    User user = bundle.getUser();
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(trackedEntity, TRACKED_ENTITY_CANT_BE_NULL);
    checkNotNull(trackedEntity.getOrgUnit(), ORGANISATION_UNIT_CANT_BE_NULL);
    // scope has to be checked
    if (strategy.isCreate() || strategy.isDelete()) {
        checkOrgUnitInCaptureScope(reporter, trackedEntity, context.getOrganisationUnit(trackedEntity.getOrgUnit()));
    } else // if its to update trackedEntity, search scope has to be checked
    {
        checkOrgUnitInSearchScope(reporter, trackedEntity, context.getOrganisationUnit(trackedEntity.getOrgUnit()));
    }
    if (strategy.isDelete()) {
        TrackedEntityInstance tei = context.getTrackedEntityInstance(trackedEntity.getTrackedEntity());
        if (tei.getProgramInstances().stream().anyMatch(pi -> !pi.isDeleted()) && !user.isAuthorized(Authorities.F_TEI_CASCADE_DELETE.getAuthority())) {
            TrackerErrorReport error = TrackerErrorReport.builder().uid(trackedEntity.getUid()).trackerType(TrackerType.TRACKED_ENTITY).errorCode(E1100).addArg(user).addArg(tei).build(bundle);
            reporter.addError(error);
        }
    }
    TrackedEntityType trackedEntityType = context.getTrackedEntityType(trackedEntity.getTrackedEntityType());
    checkTeiTypeWriteAccess(reporter, trackedEntity.getUid(), trackedEntityType);
}
Also used : EVENT_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.EVENT_CANT_BE_NULL) Authorities(org.hisp.dhis.security.Authorities) CategoryOption(org.hisp.dhis.category.CategoryOption) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) E1100(org.hisp.dhis.tracker.report.TrackerErrorCode.E1100) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Relationship(org.hisp.dhis.tracker.domain.Relationship) E1083(org.hisp.dhis.tracker.report.TrackerErrorCode.E1083) Program(org.hisp.dhis.program.Program) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) PROGRAM_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.PROGRAM_CANT_BE_NULL) TRACKED_ENTITY_TYPE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.TRACKED_ENTITY_TYPE_CANT_BE_NULL) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) TRACKED_ENTITY_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.TRACKED_ENTITY_CANT_BE_NULL) USER_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.USER_CANT_BE_NULL) ProgramInstance(org.hisp.dhis.program.ProgramInstance) PROGRAM_STAGE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.PROGRAM_STAGE_CANT_BE_NULL) User(org.hisp.dhis.user.User) ORGANISATION_UNIT_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.ORGANISATION_UNIT_CANT_BE_NULL) PROGRAM_INSTANCE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.PROGRAM_INSTANCE_CANT_BE_NULL) Event(org.hisp.dhis.tracker.domain.Event) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) NonNull(lombok.NonNull) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ENROLLMENT_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.ENROLLMENT_CANT_BE_NULL) EventStatus(org.hisp.dhis.event.EventStatus) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) E1103(org.hisp.dhis.tracker.report.TrackerErrorCode.E1103) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) AclService(org.hisp.dhis.security.acl.AclService) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackerDto(org.hisp.dhis.tracker.domain.TrackerDto) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) TrackerImportStrategy(org.hisp.dhis.tracker.TrackerImportStrategy) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 4 with TrackerErrorReport

use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.

the class PreCheckSecurityOwnershipValidationHook method checkProgramWriteAccess.

private void checkProgramWriteAccess(ValidationErrorReporter reporter, TrackerDto dto, User user, Program program) {
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(program, PROGRAM_CANT_BE_NULL);
    if (!aclService.canDataWrite(user, program)) {
        TrackerErrorReport error = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1091).addArg(user).addArg(program).build(reporter.getValidationContext().getBundle());
        reporter.addError(error);
    }
}
Also used : TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Example 5 with TrackerErrorReport

use of org.hisp.dhis.tracker.report.TrackerErrorReport in project dhis2-core by dhis2.

the class PreCheckSecurityOwnershipValidationHook method checkTeiTypeWriteAccess.

private void checkTeiTypeWriteAccess(ValidationErrorReporter reporter, String teUid, TrackedEntityType trackedEntityType) {
    TrackerBundle bundle = reporter.getValidationContext().getBundle();
    User user = bundle.getUser();
    checkNotNull(user, USER_CANT_BE_NULL);
    checkNotNull(trackedEntityType, TRACKED_ENTITY_TYPE_CANT_BE_NULL);
    if (!aclService.canDataWrite(user, trackedEntityType)) {
        TrackerErrorReport error = TrackerErrorReport.builder().uid(teUid).trackerType(TrackerType.TRACKED_ENTITY).errorCode(TrackerErrorCode.E1001).addArg(user).addArg(trackedEntityType).build(bundle);
        reporter.addError(error);
    }
}
Also used : User(org.hisp.dhis.user.User) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Aggregations

TrackerErrorReport (org.hisp.dhis.tracker.report.TrackerErrorReport)20 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)9 User (org.hisp.dhis.user.User)7 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)5 TrackerImportValidationContext (org.hisp.dhis.tracker.validation.TrackerImportValidationContext)5 Relationship (org.hisp.dhis.tracker.domain.Relationship)4 CategoryOption (org.hisp.dhis.category.CategoryOption)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 Program (org.hisp.dhis.program.Program)3 ProgramStage (org.hisp.dhis.program.ProgramStage)3 TrackerImportStrategy (org.hisp.dhis.tracker.TrackerImportStrategy)3 Event (org.hisp.dhis.tracker.domain.Event)3 TrackerErrorCode (org.hisp.dhis.tracker.report.TrackerErrorCode)3 Test (org.junit.jupiter.api.Test)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Collectors (java.util.stream.Collectors)2 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)2 EventStatus (org.hisp.dhis.event.EventStatus)2 FeatureType (org.hisp.dhis.organisationunit.FeatureType)2 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2