Skip to content

Commit

Permalink
fix: /tracker/events exports notes with dates instead of timestamps (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
teleivo authored Oct 30, 2024
1 parent 08b7094 commit e20e3f9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ public List<Event> getEvents(
Note note = new Note();
note.setNote(resultSet.getString("psinote_uid"));
note.setValue(resultSet.getString("psinote_value"));
note.setStoredDate(DateUtils.getIso8601NoTz(resultSet.getDate("psinote_storeddate")));
note.setStoredDate(
DateUtils.getIso8601NoTz(resultSet.getTimestamp("psinote_storeddate")));
note.setStoredBy(resultSet.getString("psinote_storedby"));

if (resultSet.getObject("usernote_id") != null) {
Expand All @@ -529,7 +530,7 @@ public List<Event> getEvents(
resultSet.getString("userinfo_surname")));
}

note.setLastUpdated(resultSet.getDate("psinote_lastupdated"));
note.setLastUpdated(resultSet.getTimestamp("psinote_lastupdated"));

event.getNotes().add(note);
notes.add(resultSet.getString("psinote_id"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
Expand Down Expand Up @@ -76,12 +77,14 @@
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.program.ProgramStageInstance;
import org.hisp.dhis.program.ProgramStatus;
import org.hisp.dhis.program.ProgramType;
import org.hisp.dhis.program.UserInfoSnapshot;
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
import org.hisp.dhis.trackedentity.TrackedEntityInstance;
import org.hisp.dhis.trackedentitycomment.TrackedEntityComment;
import org.hisp.dhis.user.User;
import org.hisp.dhis.util.DateUtils;
import org.hisp.dhis.webapi.controller.event.mapper.OrderParam;
import org.hisp.dhis.webapi.controller.event.mapper.OrderParam.SortDirection;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -112,14 +115,14 @@ class EventExporterTest extends TrackerTest {
private Program program;

final Function<EventQueryParams, List<String>> eventsFunction =
(params) ->
params ->
eventService.getEvents(params).getEvents().stream()
.map(Event::getEvent)
.collect(Collectors.toList());

/** EVENT_ID is at position 0 in column headers in events grid */
final Function<EventQueryParams, List<String>> eventsGridFunction =
(params) ->
params ->
eventService.getEventsGrid(params).getRows().stream()
.map(r -> r.get(0).toString())
.collect(Collectors.toList());
Expand Down Expand Up @@ -180,6 +183,7 @@ void shouldReturnEventsWithRelationships() {

@Test
void shouldReturnEventsWithNotes() {
ProgramStageInstance pTzf9KYMk72 = get(ProgramStageInstance.class, "pTzf9KYMk72");
EventQueryParams params = new EventQueryParams();
params.setOrgUnit(orgUnit);
params.setEvents(Set.of("pTzf9KYMk72"));
Expand All @@ -188,13 +192,7 @@ void shouldReturnEventsWithNotes() {
Events events = eventService.getEvents(params);

assertContainsOnly(List.of("pTzf9KYMk72"), eventUids(events));
List<Note> notes = events.getEvents().get(0).getNotes();
assertContainsOnly(
List.of("SGuCABkhpgn", "DRKO4xUVrpr"),
notes.stream().map(Note::getNote).collect(Collectors.toList()));
assertAll(
() -> assertNote(importUser, "comment value", notes.get(0)),
() -> assertNote(importUser, "comment value", notes.get(1)));
assertNotes(pTzf9KYMk72.getComments(), events.getEvents().get(0).getNotes());
}

@ParameterizedTest
Expand Down Expand Up @@ -1226,11 +1224,47 @@ void shouldNotFilterOutEventsWithoutATrackedEntityAttributeWhenAttributeIsOnlyOr
List.of("mHWCacsGYYn", "QesgJkTyTCk", "QS6w44flWAf", "dUE514NMOlo"), trackedEntities);
}

private void assertNote(User expectedLastUpdatedBy, String expectedNote, Note actual) {
assertEquals(expectedNote, actual.getValue());
UserInfoSnapshot lastUpdatedBy = actual.getLastUpdatedBy();
assertEquals(expectedLastUpdatedBy.getUid(), lastUpdatedBy.getUid());
assertEquals(expectedLastUpdatedBy.getUsername(), lastUpdatedBy.getUsername());
private static void assertNotes(List<TrackedEntityComment> expected, List<Note> actual) {
Map<String, TrackedEntityComment> expectedNotes =
expected.stream()
.collect(Collectors.toMap(IdentifiableObject::getUid, Function.identity()));
Map<String, Note> actualNotes =
actual.stream().collect(Collectors.toMap(Note::getNote, Function.identity()));
List<Executable> assertions =
expectedNotes.entrySet().stream()
.map(
entry ->
(Executable)
() -> {
TrackedEntityComment expectedNote = entry.getValue();
Note actualNote = actualNotes.get(entry.getKey());
assertNotNull(
actualNote, "note " + expectedNote.getUid() + " does not exist");
assertAll(
"note assertions " + expectedNote.getUid(),
() ->
assertEquals(
expectedNote.getCommentText(),
actualNote.getValue(),
"commentText"),
() ->
assertEquals(
expectedNote.getCreator(),
actualNote.getStoredBy(),
"creator"),
() ->
assertEquals(
DateUtils.getIso8601NoTz(expectedNote.getCreated()),
actualNote.getStoredDate(),
"created"),
() ->
assertEquals(
expectedNote.getLastUpdated(),
actualNote.getLastUpdated(),
"lastUpdated"));
})
.collect(Collectors.toList());
assertAll("note assertions", assertions);
}

private DataElement dataElement(String uid) {
Expand Down

0 comments on commit e20e3f9

Please sign in to comment.