Search in sources :

Example 31 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class EventDataValueTest method testTrackedEntityProgramAttributeValueUpdate.

@Test
void testTrackedEntityProgramAttributeValueUpdate() throws IOException {
    TrackerImportParams trackerImportParams = fromJson("tracker/event_with_data_values.json");
    TrackerImportReport trackerImportReport = trackerImportService.importTracker(trackerImportParams);
    assertEquals(TrackerStatus.OK, trackerImportReport.getStatus());
    List<ProgramStageInstance> events = manager.getAll(ProgramStageInstance.class);
    assertEquals(1, events.size());
    ProgramStageInstance psi = events.get(0);
    Set<EventDataValue> eventDataValues = psi.getEventDataValues();
    assertEquals(4, eventDataValues.size());
    // update
    trackerImportParams = fromJson("tracker/event_with_updated_data_values.json");
    // make sure that the uid property is populated as well - otherwise
    // update will
    // not work
    trackerImportParams.getEvents().get(0).setEvent(trackerImportParams.getEvents().get(0).getEvent());
    trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE);
    trackerImportReport = trackerImportService.importTracker(trackerImportParams);
    assertEquals(TrackerStatus.OK, trackerImportReport.getStatus());
    List<ProgramStageInstance> updatedEvents = manager.getAll(ProgramStageInstance.class);
    assertEquals(1, updatedEvents.size());
    ProgramStageInstance updatedPsi = programStageInstanceService.getProgramStageInstance(updatedEvents.get(0).getUid());
    assertEquals(3, updatedPsi.getEventDataValues().size());
    List<String> values = updatedPsi.getEventDataValues().stream().map(EventDataValue::getValue).collect(Collectors.toList());
    assertThat(values, hasItem("First"));
    assertThat(values, hasItem("Second"));
    assertThat(values, hasItem("Fourth updated"));
}
Also used : TrackerImportParams(org.hisp.dhis.tracker.TrackerImportParams) TrackerImportReport(org.hisp.dhis.tracker.report.TrackerImportReport) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) TrackerTest(org.hisp.dhis.tracker.TrackerTest)

Example 32 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class EventTrackerConverterServiceTest method testToEvent.

@Test
void testToEvent() {
    EventDataValue eventDataValue = new EventDataValue();
    eventDataValue.setAutoFields();
    eventDataValue.setCreated(today);
    eventDataValue.setValue("sample-value");
    eventDataValue.setDataElement(dataElement.getUid());
    eventDataValue.setStoredBy(user.getUsername());
    eventDataValue.setCreatedByUserInfo(UserInfoSnapshot.from(user));
    eventDataValue.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user));
    psi.getEventDataValues().add(eventDataValue);
    Event event = converter.to(psi);
    assertEquals(event.getEnrollment(), PROGRAM_INSTANCE_UID);
    assertEquals(event.getStoredBy(), user.getUsername());
    event.getDataValues().forEach(e -> {
        assertEquals(DateUtils.fromInstant(e.getCreatedAt()), psi.getCreated());
        assertEquals(e.getLastUpdatedBy(), psi.getLastUpdatedByUserInfo().getUsername());
        assertEquals(e.getLastUpdatedBy(), psi.getCreatedByUserInfo().getUsername());
    });
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 33 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class EventTrackerConverterServiceTest method testToProgramStageInstance.

@Test
void testToProgramStageInstance() {
    setUpMocks();
    DataElement dataElement = new DataElement();
    dataElement.setUid(CodeGenerator.generateUid());
    when(preheat.get(DataElement.class, dataElement.getUid())).thenReturn(dataElement);
    DataValue dataValue = new DataValue();
    dataValue.setValue("value");
    dataValue.setCreatedBy(USERNAME);
    dataValue.setLastUpdatedBy(USERNAME);
    dataValue.setCreatedAt(Instant.now());
    dataValue.setStoredBy(USERNAME);
    dataValue.setUpdatedAt(Instant.now());
    dataValue.setDataElement(dataElement.getUid());
    Event event = event(dataValue);
    ProgramStageInstance programStageInstance = converter.from(preheat, event);
    assertNotNull(programStageInstance);
    assertNotNull(programStageInstance.getProgramStage());
    assertNotNull(programStageInstance.getProgramStage().getProgram());
    assertNotNull(programStageInstance.getOrganisationUnit());
    assertEquals(PROGRAM_UID, programStageInstance.getProgramStage().getProgram().getUid());
    assertEquals(PROGRAM_STAGE_UID, programStageInstance.getProgramStage().getUid());
    assertEquals(ORGANISATION_UNIT_UID, programStageInstance.getOrganisationUnit().getUid());
    assertEquals(ORGANISATION_UNIT_UID, programStageInstance.getOrganisationUnit().getUid());
    Set<EventDataValue> eventDataValues = programStageInstance.getEventDataValues();
    eventDataValues.forEach(e -> {
        assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername());
        assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername());
    });
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Event(org.hisp.dhis.tracker.domain.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 34 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class EventTrackerConverterServiceTest method fromForRuleEngineGivenExistingEventUpdatesValueOfExistingDataValueOnIdSchemeCode.

@Test
void fromForRuleEngineGivenExistingEventUpdatesValueOfExistingDataValueOnIdSchemeCode() {
    // NTI supports multiple idSchemes. Event.dataElement can thus be any of
    // the supported ones
    // UID, CODE, ATTRIBUTE, NAME
    // merging existing & new data values on events needs to respect the
    // user configured idScheme
    setUpMocks();
    DataElement dataElement = new DataElement();
    dataElement.setUid(CodeGenerator.generateUid());
    dataElement.setCode("DE_424050");
    when(preheat.get(DataElement.class, dataElement.getCode())).thenReturn(dataElement);
    ProgramStageInstance existingPsi = programStageInstance();
    existingPsi.setEventDataValues(Set.of(eventDataValue(dataElement.getUid(), "658")));
    // dataElement is of idScheme CODE if the NTI dataElementIdScheme is set
    // to CODE
    DataValue updatedValue = dataValue(dataElement.getCode(), "900");
    Event event = event(existingPsi.getUid(), updatedValue);
    when(preheat.getEvent(TrackerIdScheme.UID, event.getEvent())).thenReturn(existingPsi);
    ProgramStageInstance programStageInstance = converter.fromForRuleEngine(preheat, event);
    assertEquals(1, programStageInstance.getEventDataValues().size());
    EventDataValue expect1 = new EventDataValue();
    expect1.setDataElement(dataElement.getUid());
    expect1.setValue(updatedValue.getValue());
    assertContainsOnly(programStageInstance.getEventDataValues(), expect1);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataValue(org.hisp.dhis.tracker.domain.DataValue) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Event(org.hisp.dhis.tracker.domain.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Example 35 with EventDataValue

use of org.hisp.dhis.eventdatavalue.EventDataValue in project dhis2-core by dhis2.

the class JsonEventDataValueSetBinaryType method convertObjectToJson.

/**
 * Serializes an object to JSON.
 *
 * @param object the object to convert.
 * @return JSON content.
 */
@SuppressWarnings("unchecked")
@Override
protected String convertObjectToJson(Object object) {
    try {
        Set<EventDataValue> eventDataValues = object == null ? Collections.emptySet() : (Set<EventDataValue>) object;
        Map<String, EventDataValue> tempMap = new HashMap<>();
        for (EventDataValue eventDataValue : eventDataValues) {
            tempMap.put(eventDataValue.getDataElement(), eventDataValue);
        }
        return writer.writeValueAsString(tempMap);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue)

Aggregations

EventDataValue (org.hisp.dhis.eventdatavalue.EventDataValue)35 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)21 DataElement (org.hisp.dhis.dataelement.DataElement)14 Date (java.util.Date)10 HashSet (java.util.HashSet)10 Test (org.junit.jupiter.api.Test)9 DataValue (org.hisp.dhis.tracker.domain.DataValue)8 HashMap (java.util.HashMap)7 Event (org.hisp.dhis.tracker.domain.Event)7 User (org.hisp.dhis.user.User)7 ArrayList (java.util.ArrayList)6 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)6 Set (java.util.Set)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)4 EventStatus (org.hisp.dhis.event.EventStatus)4 Program (org.hisp.dhis.program.Program)4