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"));
}
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());
});
}
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());
});
}
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);
}
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);
}
}
Aggregations