Search in sources :

Example 1 with ServiceDelegator

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

the class ProgramInstanceRepeatableStageCheckTest method successOnNonRepeatableStageAndExistingEventsOnNewEnrollment.

@Test
void successOnNonRepeatableStageAndExistingEventsOnNewEnrollment() {
    // 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(false);
    // Method under test
    ImportSummary summary = rule.check(new ImmutableEvent(event), workContext);
    assertNoError(summary);
}
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 2 with ServiceDelegator

use of org.hisp.dhis.dxf2.events.importer.ServiceDelegator 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 3 with ServiceDelegator

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

the class WorkContextLoader method load.

@Transactional(readOnly = true)
public WorkContext load(ImportOptions importOptions, List<Event> events) {
    sessionFactory.getCurrentSession().flush();
    ImportOptions localImportOptions = importOptions;
    // API allows a null Import Options
    if (localImportOptions == null) {
        localImportOptions = ImportOptions.getDefaultImportOptions();
    }
    initializeUser(localImportOptions);
    // Make sure all events have the 'uid' field populated
    events = uidGen.assignUidToEvents(events);
    final Map<String, ProgramStageInstance> programStageInstanceMap = programStageInstanceSupplier.get(localImportOptions, events);
    final Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap = trackedEntityInstanceSupplier.get(localImportOptions, events);
    final Map<String, OrganisationUnit> orgUniMap = organisationUnitSupplier.get(localImportOptions, events);
    return WorkContext.builder().importOptions(localImportOptions).programsMap(programSupplier.get(localImportOptions, events)).programStageInstanceMap(programStageInstanceMap).organisationUnitMap(orgUniMap).trackedEntityInstanceMap(teiMap).programInstanceMap(programInstanceSupplier.get(localImportOptions, teiMap, events)).categoryOptionComboMap(categoryOptionComboSupplier.get(localImportOptions, events)).dataElementMap(dataElementSupplier.get(localImportOptions, events)).notesMap(noteSupplier.get(localImportOptions, events)).assignedUserMap(assignedUserSupplier.get(localImportOptions, events)).eventDataValueMap(new EventDataValueAggregator().aggregateDataValues(events, programStageInstanceMap, localImportOptions)).programWithOrgUnitsMap(programOrgUnitSupplier.get(localImportOptions, events, orgUniMap)).serviceDelegator(serviceDelegatorSupplier.get()).build();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Pair(org.apache.commons.lang3.tuple.Pair) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Pair (org.apache.commons.lang3.tuple.Pair)3 HashMap (java.util.HashMap)2 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)2 DhisConvenienceTest.createProgramStage (org.hisp.dhis.DhisConvenienceTest.createProgramStage)2 DhisConvenienceTest.createTrackedEntityInstance (org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance)2 ImmutableEvent (org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)2 BaseValidationTest (org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)2 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)2 Program (org.hisp.dhis.program.Program)2 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 ProgramStage (org.hisp.dhis.program.ProgramStage)2 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)2 Test (org.junit.jupiter.api.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)1 Transactional (org.springframework.transaction.annotation.Transactional)1