Search in sources :

Example 56 with TrackedEntity

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

the class TrackedEntityAttributeValidationHookTest method shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory.

@Test
void shouldPassValidationWhenValueIsNullAndAttributeIsNotMandatory() {
    TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
    TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
    trackedEntityAttribute.setUid("trackedEntityAttribute");
    trackedEntityAttribute.setValueType(ValueType.TEXT);
    trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
    TrackedEntityType trackedEntityType = new TrackedEntityType();
    trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
    when(validationContext.getTrackedEntityAttribute("trackedEntityAttribute")).thenReturn(trackedEntityAttribute);
    when(validationContext.getTrackedEntityType(anyString())).thenReturn(trackedEntityType);
    TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute("trackedEntityAttribute").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) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 57 with TrackedEntity

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

the class TrackedEntityAttributeValidationHookTest method shouldFailDataValueIsValid.

@Test
void shouldFailDataValueIsValid() {
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    when(trackedEntityAttribute.getValueType()).thenReturn(ValueType.NUMBER);
    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.E1085.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)

Example 58 with TrackedEntity

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

the class TrackedEntityAttributeValidationHookTest method shouldFailValidationMandatoryFields.

@Test
void shouldFailValidationMandatoryFields() {
    String tet = "tet";
    TrackedEntityType trackedEntityType = new TrackedEntityType();
    TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
    trackedEntityTypeAttribute.setMandatory(true);
    TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
    trackedEntityAttribute.setUid("c");
    trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
    trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
    when(validationContext.getTrackedEntityType(tet)).thenReturn(trackedEntityType);
    TrackedEntity trackedEntity = TrackedEntity.builder().trackedEntityType(tet).attributes(Arrays.asList(Attribute.builder().attribute("a").value("value").build(), Attribute.builder().attribute("b").value("value").build())).build();
    TrackedEntityAttribute contextAttribute = new TrackedEntityAttribute();
    contextAttribute.setUid("uid");
    contextAttribute.setValueType(ValueType.TEXT);
    when(validationContext.getTrackedEntityAttribute(anyString())).thenReturn(contextAttribute);
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
    assertTrue(reporter.hasErrors());
    assertEquals(1, reporter.getReportList().size());
    assertEquals(1, reporter.getReportList().stream().filter(e -> e.getErrorCode() == TrackerErrorCode.E1090).count());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 59 with TrackedEntity

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

the class TrackedEntityAttributeValidationHookTest method shouldFailValueTooLong.

@Test
void shouldFailValueTooLong() {
    ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
    when(trackedEntityAttribute.getValueType()).thenReturn(ValueType.TEXT);
    StringBuilder sbString = new StringBuilder();
    for (int i = 0; i < Constant.MAX_ATTR_VALUE_LENGTH + 1; i++) {
        sbString.append("a");
    }
    TrackedEntity te = TrackedEntity.builder().trackedEntity(CodeGenerator.generateUid()).build();
    trackedEntityAttributeValidationHook.validateAttributeValue(reporter, te, trackedEntityAttribute, sbString.toString());
    assertTrue(reporter.hasErrors());
    assertEquals(1, reporter.getReportList().size());
    assertTrue(reporter.hasErrorReport(err -> TrackerErrorCode.E1077.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)

Example 60 with TrackedEntity

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

the class PreCheckExistenceValidationHookTest method verifyTrackedEntityValidationFailsWhenIsCreateAndTEIIsAlreadyPresent.

@Test
void verifyTrackedEntityValidationFailsWhenIsCreateAndTEIIsAlreadyPresent() {
    // given
    TrackedEntity trackedEntity = TrackedEntity.builder().trackedEntity(TEI_UID).build();
    // when
    when(ctx.getStrategy(trackedEntity)).thenReturn(TrackerImportStrategy.CREATE);
    ValidationErrorReporter reporter = new ValidationErrorReporter(ctx);
    validationHook.validateTrackedEntity(reporter, trackedEntity);
    // then
    hasTrackerError(reporter, E1002, 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)

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