Search in sources :

Example 51 with TrackedEntity

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

the class PreCheckSecurityOwnershipValidationHookTest method verifyValidationFailsForTrackedEntityWithUserNotInOrgUnitCaptureScopeHierarchy.

@Test
void verifyValidationFailsForTrackedEntityWithUserNotInOrgUnitCaptureScopeHierarchy() {
    TrackedEntity trackedEntity = TrackedEntity.builder().trackedEntity(TEI_ID).orgUnit(ORG_UNIT_ID).trackedEntityType(TEI_TYPE_ID).build();
    when(ctx.getOrganisationUnit(ORG_UNIT_ID)).thenReturn(organisationUnit);
    when(ctx.getTrackedEntityType(TEI_TYPE_ID)).thenReturn(trackedEntityType);
    when(ctx.getStrategy(trackedEntity)).thenReturn(TrackerImportStrategy.CREATE);
    when(ctx.getOrganisationUnit(ORG_UNIT_ID)).thenReturn(organisationUnit);
    when(organisationUnitService.isInUserHierarchyCached(user, organisationUnit)).thenReturn(false);
    when(aclService.canDataWrite(user, trackedEntityType)).thenReturn(true);
    reporter = new ValidationErrorReporter(ctx);
    validatorToTest.validateTrackedEntity(reporter, trackedEntity);
    validatorToTest.validateTrackedEntity(reporter, trackedEntity);
    hasTrackerError(reporter, E1000, TrackerType.TRACKED_ENTITY, trackedEntity.getUid());
}
Also used : TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 52 with TrackedEntity

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

the class PreCheckUpdatableFieldsValidationHookTest method verifyTrackedEntityValidationFailsWhenUpdateTrackedEntityType.

@Test
void verifyTrackedEntityValidationFailsWhenUpdateTrackedEntityType() {
    // given
    TrackedEntity trackedEntity = validTei();
    trackedEntity.setTrackedEntityType("NewTrackedEntityTypeId");
    // when
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    validationHook.validateTrackedEntity(reporter, trackedEntity);
    // then
    hasTrackerError(reporter, E1126, TRACKED_ENTITY, trackedEntity.getUid());
}
Also used : TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 53 with TrackedEntity

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

the class TrackedEntityAttributeValidationHookTest method shouldPassValidation.

@Test
void shouldPassValidation() {
    TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
    trackedEntityAttribute.setUid("uid");
    trackedEntityAttribute.setValueType(ValueType.TEXT);
    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("value").build())).trackedEntityType("trackedEntityType").build();
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
    assertFalse(reporter.hasErrors());
    assertEquals(0, reporter.getReportList().size());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 54 with TrackedEntity

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

the class TrackedEntityAttributeValidationHookTest method shouldFailValidationMissingTea.

@Test
void shouldFailValidationMissingTea() {
    TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Arrays.asList(Attribute.builder().attribute("aaaaa").build(), Attribute.builder().attribute("bbbbb").build())).trackedEntityType("tet").build();
    when(validationContext.getTrackedEntityType(anyString())).thenReturn(new TrackedEntityType());
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
    assertTrue(reporter.hasErrors());
    assertEquals(2, reporter.getReportList().size());
    assertEquals(2, reporter.getReportList().stream().filter(e -> e.getErrorCode() == TrackerErrorCode.E1006).count());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 55 with TrackedEntity

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

the class TrackedEntityAttributeValidationHookTest method shouldFailEncryptionStatus.

@Test
void shouldFailEncryptionStatus() {
    when(trackedEntityAttribute.isConfidentialBool()).thenReturn(true);
    when(trackedEntityAttribute.getValueType()).thenReturn(ValueType.AGE);
    when(dhisConfigurationProvider.getEncryptionStatus()).thenReturn(EncryptionStatus.ENCRYPTION_PASSWORD_TOO_SHORT);
    when(dhisConfigurationProvider.getProperty(any())).thenReturn("property");
    when(trackedEntityAttribute.getValueType()).thenReturn(ValueType.TEXT);
    when(validationContext.getTrackedEntityAttribute(anyString())).thenReturn(trackedEntityAttribute);
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    TrackedEntity te = TrackedEntity.builder().trackedEntity(CodeGenerator.generateUid()).build();
    trackedEntityAttributeValidationHook.validateAttributeValue(reporter, te, trackedEntityAttribute, "value");
    assertTrue(reporter.hasErrors());
    assertEquals(1, reporter.getReportList().size());
    assertTrue(reporter.hasErrorReport(err -> TrackerErrorCode.E1112.equals(err.getErrorCode()) && TrackerType.TRACKED_ENTITY.equals(err.getTrackerType()) && te.getTrackedEntity().equals(err.getUid())));
}
Also used : Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) ValueType(org.hisp.dhis.common.ValueType) Attribute(org.hisp.dhis.tracker.domain.Attribute) Mock(org.mockito.Mock) Constant(org.hisp.dhis.tracker.util.Constant) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EncryptionStatus(org.hisp.dhis.encryption.EncryptionStatus) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Mockito.when(org.mockito.Mockito.when) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) TrackerErrorCode(org.hisp.dhis.tracker.report.TrackerErrorCode) Test(org.junit.jupiter.api.Test) Option(org.hisp.dhis.option.Option) OptionSet(org.hisp.dhis.option.OptionSet) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Aggregations

TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)69 Test (org.junit.jupiter.api.Test)55 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)45 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)19 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)18 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)17 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)17 Event (org.hisp.dhis.tracker.domain.Event)17 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)13 TrackerPreheat (org.hisp.dhis.tracker.preheat.TrackerPreheat)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 List (java.util.List)10 Attribute (org.hisp.dhis.tracker.domain.Attribute)10 Relationship (org.hisp.dhis.tracker.domain.Relationship)10 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)10 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)10 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)10 Mockito.when (org.mockito.Mockito.when)10 Optional (java.util.Optional)9 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)9