Search in sources :

Example 21 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class ProgramInstanceCheckTest method failOnNoProgramInstanceByActiveProgramAndTei.

@Test
void failOnNoProgramInstanceByActiveProgramAndTei() {
    // Data preparation
    // 
    // Program Instance
    // 
    when(workContext.getProgramInstanceMap()).thenReturn(new HashMap<>());
    // 
    // Tracked Entity Instance
    // 
    TrackedEntityInstance tei = createTrackedEntityInstance(createOrganisationUnit('A'));
    when(workContext.getTrackedEntityInstance(event.getUid())).thenReturn(Optional.of(tei));
    event.setProgram(program.getUid());
    // 
    // Method under test
    // 
    ImportSummary summary = rule.check(new ImmutableEvent(event), workContext);
    assertHasError(summary, event, "Tracked entity instance: " + tei.getUid() + " is not enrolled in program: " + program.getUid());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) DhisConvenienceTest.createTrackedEntityInstance(org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

Example 22 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class ProgramInstanceRepeatableStageCheckTest method failOnNonRepeatableStageAndExistingEvents.

@Test
void failOnNonRepeatableStageAndExistingEvents() {
    // Data preparation
    Program program = createProgram('P');
    TrackedEntityInstance tei = createTrackedEntityInstance('A', createOrganisationUnit('A'));
    event.setProgramStage(CodeGenerator.generateUid());
    event.setProgram(program.getUid());
    event.setTrackedEntityInstance(tei.getUid());
    ProgramStage programStage = createProgramStage('A', program);
    programStage.setRepeatable(false);
    when(workContext.getProgramStage(programStageIdScheme, event.getProgramStage())).thenReturn(programStage);
    Map<String, Program> programMap = new HashMap<>();
    programMap.put(program.getUid(), program);
    Map<String, ProgramInstance> programInstanceMap = new HashMap<>();
    ProgramInstance programInstance = new ProgramInstance();
    programInstanceMap.put(event.getUid(), programInstance);
    Pair<TrackedEntityInstance, Boolean> teiPair = Pair.of(tei, true);
    Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap = new HashMap<>();
    teiMap.put(event.getUid(), teiPair);
    when(workContext.getTrackedEntityInstanceMap()).thenReturn(teiMap);
    when(workContext.getProgramsMap()).thenReturn(programMap);
    when(workContext.getProgramInstanceMap()).thenReturn(programInstanceMap);
    when(workContext.getServiceDelegator()).thenReturn(serviceDelegator);
    when(serviceDelegator.getJdbcTemplate()).thenReturn(jdbcTemplate);
    when(jdbcTemplate.queryForObject(anyString(), eq(Boolean.class), eq(programInstance.getId()), eq(programStage.getId()), eq(tei.getId()))).thenReturn(true);
    // Method under test
    ImportSummary summary = rule.check(new ImmutableEvent(event), workContext);
    assertHasError(summary, event, "Program stage is not repeatable and an event already exists");
}
Also used : Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) DhisConvenienceTest.createTrackedEntityInstance(org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) DhisConvenienceTest.createProgramStage(org.hisp.dhis.DhisConvenienceTest.createProgramStage) ProgramStage(org.hisp.dhis.program.ProgramStage) Pair(org.apache.commons.lang3.tuple.Pair) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest) Test(org.junit.jupiter.api.Test)

Example 23 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class FilteringOutUndeclaredDataElementsProcessorTest method testNotLinkedDataElementsAreRemovedFromEvent.

@Test
void testNotLinkedDataElementsAreRemovedFromEvent() {
    Event event = new Event();
    event.setProgramStage(PROGRAMSTAGE);
    HashSet<DataValue> dataValues = Sets.newHashSet(new DataValue(DATA_ELEMENT_1, "whatever"), new DataValue(DATA_ELEMENT_2, "another value"));
    event.setDataValues(dataValues);
    WorkContext ctx = WorkContext.builder().importOptions(ImportOptions.getDefaultImportOptions()).programsMap(getProgramMap()).eventDataValueMap(new EventDataValueAggregator().aggregateDataValues(ImmutableList.of(event), Collections.emptyMap(), ImportOptions.getDefaultImportOptions())).build();
    preProcessor.process(event, ctx);
    Set<String> allowedDataValues = ctx.getProgramStage(ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme(), PROGRAMSTAGE).getDataElements().stream().map(BaseIdentifiableObject::getUid).collect(Collectors.toSet());
    Set<String> filteredEventDataValues = ctx.getEventDataValueMap().values().stream().flatMap(Collection::stream).map(EventDataValue::getDataElement).collect(Collectors.toSet());
    assertTrue(allowedDataValues.containsAll(filteredEventDataValues));
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) EventDataValueAggregator(org.hisp.dhis.dxf2.events.importer.context.EventDataValueAggregator) Event(org.hisp.dhis.dxf2.events.event.Event) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 24 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class EventBaseCheckTest method verifyErrorOnInvalidLastUpdatedAtClientDate.

@Test
void verifyErrorOnInvalidLastUpdatedAtClientDate() {
    event.setEvent(event.getUid());
    event.setLastUpdatedAtClient("111-12-122");
    ImportSummary importSummary = rule.check(new ImmutableEvent(event), workContext);
    assertHasError(importSummary, event, null);
    assertHasConflict(importSummary, event, "Invalid event last updated at client date: " + event.getLastUpdatedAtClient());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

Example 25 with WorkContext

use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.

the class EventBaseCheckTest method verifyNoErrorOnNonCompletedProgramInstance.

@Test
void verifyNoErrorOnNonCompletedProgramInstance() {
    event.setEvent(event.getUid());
    Map<String, ProgramInstance> programInstanceMap = new HashMap<>();
    ProgramInstance programInstance = new ProgramInstance();
    programInstanceMap.put(event.getUid(), programInstance);
    when(workContext.getProgramInstanceMap()).thenReturn(programInstanceMap);
    ImportSummary importSummary = rule.check(new ImmutableEvent(event), workContext);
    assertNoError(importSummary);
}
Also used : HashMap(java.util.HashMap) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)47 ImmutableEvent (org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)35 Test (org.junit.jupiter.api.Test)35 BaseValidationTest (org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)26 Program (org.hisp.dhis.program.Program)23 HashMap (java.util.HashMap)20 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)16 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)14 ProgramInstance (org.hisp.dhis.program.ProgramInstance)14 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)13 Event (org.hisp.dhis.dxf2.events.event.Event)12 ProgramStage (org.hisp.dhis.program.ProgramStage)11 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)9 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)9 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)9 Set (java.util.Set)7 DhisConvenienceTest.createTrackedEntityInstance (org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance)7 IdScheme (org.hisp.dhis.common.IdScheme)6 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)6 User (org.hisp.dhis.user.User)5