Skip to content

Commit

Permalink
chore: Use createdAt as secundary sorting param [DHIS2-18089]
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Oct 16, 2024
1 parent 07a3d76 commit 5da5eab
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ public RuleEvent toMappedRuleEvent(ProgramStageInstance psi) {
psi.getProgramStage().getUid(),
RuleEvent.Status.valueOf(psi.getStatus().toString()),
ObjectUtils.defaultIfNull(psi.getExecutionDate(), psi.getDueDate()),
psi.getCreated(),
psi.getDueDate(),
orgUnit,
orgUnitCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ public ProgramStageInstance fromForRuleEngine(TrackerPreheat preheat, Event even
ProgramStageInstance psi = from(preheat, event, null);
// merge data values from DB
psi.getEventDataValues().addAll(getProgramStageInstanceDataValues(preheat, event));
ProgramStageInstance savedEvent = preheat.getEvent(event.getUid());
if (savedEvent != null) {
psi.setCreated(savedEvent.getCreated());
}
return psi;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
import static org.hisp.dhis.tracker.report.TrackerErrorCode.E1308;
import static org.hisp.dhis.tracker.report.TrackerErrorCode.E1310;
import static org.hisp.dhis.utils.Assertions.assertContainsOnly;
import static org.hisp.dhis.utils.Assertions.assertIsEmpty;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle;
import org.hisp.dhis.eventdatavalue.EventDataValue;
Expand All @@ -63,6 +65,7 @@
import org.hisp.dhis.tracker.TrackerTest;
import org.hisp.dhis.tracker.report.TrackerImportReport;
import org.hisp.dhis.util.DateUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -162,6 +165,61 @@ void shouldImportEventAndCorrectlyAssignPreviousEventDataValue(
assertContainsOnly(List.of(previousEventDataValue), eventDataValues);
}

@Test
void
shouldImportEventAndCorrectlyAssignPreviousEventDataValueConsideringCreateAtWhenOccurredAtIsSame()
throws IOException {
String firstEventUid = CodeGenerator.generateUid();
String secondEventUid = CodeGenerator.generateUid();
String thirdEventUid = CodeGenerator.generateUid();
String fourthEventUid = CodeGenerator.generateUid();

// Events are imported separately to have different createdAt
TrackerImportParams firstEvent = getEvent(firstEventUid, "2024-01-11", "FIRST");
trackerImportService.importTracker(firstEvent);

TrackerImportParams fourthEvent = getEvent(fourthEventUid, "2024-01-26", "FOURTH");
trackerImportService.importTracker(fourthEvent);

TrackerImportParams secondEvent = getEvent(secondEventUid, "2024-01-25", "SECOND");
trackerImportService.importTracker(secondEvent);

TrackerImportParams thirdEvent = getEvent(thirdEventUid, "2024-01-25", "THIRD");
trackerImportService.importTracker(thirdEvent);

assignPreviousEventProgramRule();

dbmsManager.clearSession();
dbmsManager.flushSession();

TrackerImportParams trackerImportParams =
TrackerImportParams.builder()
.events(
List.of(
firstEvent.getEvents().get(0),
secondEvent.getEvents().get(0),
thirdEvent.getEvents().get(0),
fourthEvent.getEvents().get(0)))
.importStrategy(TrackerImportStrategy.CREATE_AND_UPDATE)
.build();

TrackerImportReport importReport = trackerImportService.importTracker(trackerImportParams);

List<String> firstEventDataValues = getValueForAssignedDataElement(firstEventUid);
List<String> secondEventDataValues = getValueForAssignedDataElement(secondEventUid);
List<String> thirdEventDataValues = getValueForAssignedDataElement(thirdEventUid);
List<String> fourthEventDataValues = getValueForAssignedDataElement(fourthEventUid);

Assertions.assertAll(
() ->
assertEquals(
E1308, importReport.getValidationReport().getWarnings().get(0).getWarningCode()),
() -> assertIsEmpty(firstEventDataValues),
() -> assertContainsOnly(List.of("FIRST"), secondEventDataValues),
() -> assertContainsOnly(List.of("SECOND"), thirdEventDataValues),
() -> assertContainsOnly(List.of("THIRD"), fourthEventDataValues));
}

@Test
void shouldImportWithWarningWhenDataElementWithSameValueIsAssignedByAssignRule()
throws IOException {
Expand Down Expand Up @@ -224,6 +282,27 @@ void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() th
assertEquals(E1308, importReport.getValidationReport().getWarnings().get(0).getWarningCode());
}

private TrackerImportParams getEvent(String eventUid, String occurredDate, String value)
throws IOException {
TrackerImportParams trackerImportParams =
fromJson("tracker/programrule/event_without_date.json");
trackerImportParams
.getEvents()
.get(0)
.setOccurredAt(DateUtils.instantFromDateAsString(occurredDate));
trackerImportParams.getEvents().get(0).setEvent(eventUid);
trackerImportParams.getEvents().get(0).getDataValues().iterator().next().setValue(value);

return trackerImportParams;
}

private List<String> getValueForAssignedDataElement(String eventUid) {
return manager.get(ProgramStageInstance.class, eventUid).getEventDataValues().stream()
.filter(dv -> dv.getDataElement().equals("DATAEL00002"))
.map(EventDataValue::getValue)
.collect(Collectors.toList());
}

private void assignProgramRule() {
ProgramRule programRule = createProgramRule('F', program, null, "true");
programRuleService.addProgramRule(programRule);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"trackedEntities": [],
"enrollments": [],
"events": [
{
"status": "COMPLETED",
"program": {
"idScheme": "UID",
"identifier": "BFcipDERJnf"
},
"programStage": {
"idScheme": "UID",
"identifier": "NpsdDv6kKSO"
},
"enrollment": "TvctPPhpD8u",
"orgUnit": {
"idScheme": "UID",
"identifier": "h4w96yEMlzO"
},
"relationships": [],
"scheduledAt": "2019-01-25T12:10:38.100",
"storedBy": "admin",
"deleted": false,
"attributeOptionCombo": {
"idScheme": "UID"
},
"attributeCategoryOptions": [],

"dataValues": [
{
"storedBy": "admin",
"providedElsewhere": false,
"dataElement": {
"idScheme": "UID",
"identifier": "DATAEL00001"
}
}
],
"notes": []
}
],
"relationships": []
}
20 changes: 18 additions & 2 deletions dhis-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<!-- *Dependencies* -->

<!-- DHIS2 Rule Engine -->
<dhis2-rule-engine.version>2.1.8.0</dhis2-rule-engine.version>
<dhis2-rule-engine.version>2.1.8.2</dhis2-rule-engine.version>

<!-- HISP Quick and Staxwax -->
<dhis-hisp-quick.version>1.4.4</dhis-hisp-quick.version>
Expand Down Expand Up @@ -429,6 +429,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hisp.dhis.lib.expression</groupId>
<artifactId>expression-parser</artifactId>
<version>1.0.1</version>
</dependency>

<!-- Spring -->
<dependency>
Expand Down Expand Up @@ -1785,9 +1790,20 @@
<enabled>false</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
<id>central</id>
<id>central-mirror</id>
<url>https://maven-central.storage-download.googleapis.com/maven2/</url>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<releases>
<updatePolicy>never</updatePolicy>
Expand Down

0 comments on commit 5da5eab

Please sign in to comment.