From f72cc524c39304364de8f267ab7b77208f5c0c88 Mon Sep 17 00:00:00 2001 From: Enrico Colasante Date: Thu, 5 Sep 2024 08:36:01 +0200 Subject: [PATCH] fix: Load events to fix program rule variables calculation [DHIS2-17951] (#18508) * fix: Load events to fix program rule variables calculation [DHIS2-17951] * Fix tests * Fix tests * Fix tests * Fix review comments --- .../tracker/export/event/JdbcEventStore.java | 9 ++ .../DefaultProgramRuleService.java | 29 +++- .../event/EventChangeLogServiceTest.java | 18 +-- ...TrackerNotificationHandlerServiceTest.java | 2 + .../TrackerProgramRuleBundleServiceTest.java | 1 + .../ProgramRuleAssignActionTest.java | 68 +++++++++- .../ProgramRuleEngineDescriptionTest.java | 15 +-- .../EnrollmentImportValidationTest.java | 7 + .../programrule/event_with_data_value.json | 46 +++++++ .../three_events_with_different_dates.json | 124 ++++++++++++++++++ .../tracker/tracker_basic_metadata.json | 6 +- .../EventsExportControllerPostgresTest.java | 1 + 12 files changed, 297 insertions(+), 29 deletions(-) rename dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/{programrule/engine => tracker/imports/programrule}/ProgramRuleEngineDescriptionTest.java (97%) create mode 100644 dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/event_with_data_value.json create mode 100644 dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/three_events_with_different_dates.json diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java index 512eceb4de81..d95b22213557 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/JdbcEventStore.java @@ -157,10 +157,12 @@ class JdbcEventStore implements EventStore { private static final String COLUMN_EVENT_UID = "ev_uid"; private static final String COLUMN_PROGRAM_UID = "p_uid"; private static final String COLUMN_PROGRAM_STAGE_UID = "ps_uid"; + private static final String COLUMN_PROGRAM_STAGE_NAME = "ps_name"; private static final String COLUMN_ENROLLMENT_UID = "en_uid"; private static final String COLUMN_ENROLLMENT_STATUS = "en_status"; private static final String COLUMN_ENROLLMENT_DATE = "en_enrollmentdate"; private static final String COLUMN_ORG_UNIT_UID = "orgunit_uid"; + private static final String COLUMN_ORG_UNIT_CODE = "orgunit_code"; private static final String COLUMN_TRACKEDENTITY_UID = "te_uid"; private static final String COLUMN_EVENT_OCCURRED_DATE = "ev_occurreddate"; private static final String COLUMN_ENROLLMENT_FOLLOWUP = "en_followup"; @@ -302,8 +304,10 @@ private List fetchEvents(EventQueryParams queryParams, PageParams pagePar enrollment.setTrackedEntity(te); OrganisationUnit ou = new OrganisationUnit(); ou.setUid(resultSet.getString(COLUMN_ORG_UNIT_UID)); + ou.setCode(resultSet.getString(COLUMN_ORG_UNIT_CODE)); ProgramStage ps = new ProgramStage(); ps.setUid(resultSet.getString("ps_identifier")); + ps.setName(resultSet.getString(COLUMN_PROGRAM_STAGE_NAME)); event.setDeleted(resultSet.getBoolean(COLUMN_EVENT_DELETED)); enrollment.setStatus( @@ -756,10 +760,15 @@ private String getEventSelectQuery( .append(", ") .append("ou.uid as ") .append(COLUMN_ORG_UNIT_UID) + .append(", ") + .append("ou.code as ") + .append(COLUMN_ORG_UNIT_CODE) .append(", p.uid as ") .append(COLUMN_PROGRAM_UID) .append(", ps.uid as ") .append(COLUMN_PROGRAM_STAGE_UID) + .append(", ps.name as ") + .append(COLUMN_PROGRAM_STAGE_NAME) .append(", ") .append("ev.eventid as ") .append(COLUMN_EVENT_ID) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/programrule/DefaultProgramRuleService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/programrule/DefaultProgramRuleService.java index f6f9a13d6df0..4fb55928b10b 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/programrule/DefaultProgramRuleService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/programrule/DefaultProgramRuleService.java @@ -27,6 +27,8 @@ */ package org.hisp.dhis.tracker.imports.programrule; +import static org.hisp.dhis.common.OrganisationUnitSelectionMode.ACCESSIBLE; + import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -36,11 +38,17 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import lombok.RequiredArgsConstructor; +import org.hisp.dhis.feedback.BadRequestException; +import org.hisp.dhis.feedback.ForbiddenException; +import org.hisp.dhis.feedback.NotFoundException; import org.hisp.dhis.program.Enrollment; import org.hisp.dhis.program.Event; import org.hisp.dhis.program.Program; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; +import org.hisp.dhis.tracker.export.event.EventOperationParams; +import org.hisp.dhis.tracker.export.event.EventParams; +import org.hisp.dhis.tracker.export.event.EventService; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; import org.hisp.dhis.tracker.imports.converter.RuleEngineConverterService; import org.hisp.dhis.tracker.imports.converter.TrackerConverterService; @@ -59,6 +67,8 @@ class DefaultProgramRuleService implements ProgramRuleService { private final ProgramRuleEngine programRuleEngine; + private final EventService eventService; + private final RuleEngineConverterService< org.hisp.dhis.tracker.imports.domain.Enrollment, Enrollment> enrollmentTrackerConverterService; @@ -202,10 +212,21 @@ private List getAttributes( // if they are present in both places private Set getEventsFromEnrollment( String enrollmentUid, TrackerBundle bundle, TrackerPreheat preheat) { - Stream events = - preheat.getEvents().values().stream() - .filter(e -> e.getEnrollment().getUid().equals(enrollmentUid)) - .filter(e -> bundle.findEventByUid(e.getUid()).isEmpty()); + Stream events; + try { + events = + eventService + .getEvents( + EventOperationParams.builder() + .eventParams(EventParams.TRUE) + .orgUnitMode(ACCESSIBLE) + .enrollments(Set.of(enrollmentUid)) + .build()) + .stream() + .filter(e -> bundle.findEventByUid(e.getUid()).isEmpty()); + } catch (BadRequestException | ForbiddenException | NotFoundException e) { + throw new RuntimeException(e); + } Stream bundleEvents = bundle.getEvents().stream() diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventChangeLogServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventChangeLogServiceTest.java index b1002b3df1c8..1d333c4421c4 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventChangeLogServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventChangeLogServiceTest.java @@ -51,6 +51,7 @@ import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.user.User; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -85,6 +86,12 @@ void setUp() throws IOException { importParams, fromJson("tracker/event_and_enrollment.json"))); } + @BeforeEach + void setUpUser() { + importUser = userService.getUser("tTgjgobT1oS"); + injectSecurityContextUser(importUser); + } + @Test void shouldFailWhenEventDoesNotExist() { assertThrows( @@ -142,7 +149,6 @@ void shouldFailWhenProgramWithoutRegistrationAndNoAccessToEventOrgUnit() { @Test void shouldReturnChangeLogsWhenDataValueIsCreated() throws NotFoundException, ForbiddenException { - testAsUser("M5zQapPyTZI"); Event event = getEvent("QRYjLTiJTrA"); String dataElementUid = event.getEventDataValues().iterator().next().getDataElement(); @@ -157,8 +163,6 @@ void shouldReturnChangeLogsWhenDataValueIsCreated() throws NotFoundException, Fo @Test void shouldReturnChangeLogsWhenDataValueIsDeleted() throws NotFoundException, IOException, ForbiddenException { - testAsUser("M5zQapPyTZI"); - Event event = getEvent("QRYjLTiJTrA"); String dataElementUid = event.getEventDataValues().iterator().next().getDataElement(); @@ -179,8 +183,6 @@ void shouldReturnChangeLogsWhenDataValueIsDeleted() @Test void shouldNotUpdateChangeLogsWhenDataValueIsDeletedTwiceInARow() throws NotFoundException, IOException, ForbiddenException { - testAsUser("M5zQapPyTZI"); - Event event = getEvent("QRYjLTiJTrA"); String dataElementUid = event.getEventDataValues().iterator().next().getDataElement(); @@ -203,8 +205,6 @@ void shouldNotUpdateChangeLogsWhenDataValueIsDeletedTwiceInARow() @Test void shouldReturnChangeLogsWhenDataValueIsUpdated() throws NotFoundException, IOException, ForbiddenException { - testAsUser("M5zQapPyTZI"); - Event event = getEvent("QRYjLTiJTrA"); String dataElementUid = event.getEventDataValues().iterator().next().getDataElement(); @@ -225,8 +225,6 @@ void shouldReturnChangeLogsWhenDataValueIsUpdated() @Test void shouldReturnChangeLogsWhenDataValueIsUpdatedTwiceInARow() throws NotFoundException, IOException, ForbiddenException { - testAsUser("M5zQapPyTZI"); - Event event = getEvent("QRYjLTiJTrA"); String dataElementUid = event.getEventDataValues().iterator().next().getDataElement(); @@ -250,8 +248,6 @@ void shouldReturnChangeLogsWhenDataValueIsUpdatedTwiceInARow() @Test void shouldReturnChangeLogsWhenDataValueIsCreatedUpdatedAndDeleted() throws IOException, NotFoundException, ForbiddenException { - testAsUser("M5zQapPyTZI"); - Event event = getEvent("QRYjLTiJTrA"); String dataElementUid = event.getEventDataValues().iterator().next().getDataElement(); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerNotificationHandlerServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerNotificationHandlerServiceTest.java index a15c53b018cc..fe3f7a960a2d 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerNotificationHandlerServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerNotificationHandlerServiceTest.java @@ -119,6 +119,8 @@ void setUp() { user.getGroups().add(userGroup); manager.update(user); + injectSecurityContextUser(user); + templateForEnrollment = createProgramNotification( "enrollment", diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java index 0481f10ad915..1f4e0fa4f003 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java @@ -88,6 +88,7 @@ void setUp() throws IOException { @Test void testRunRuleEngineForEventOnBundleCreate() throws IOException { + injectSecurityContextUser(userService.getUser("tTgjgobT1oS")); TrackerObjects trackerObjects = fromJson("tracker/event_events_and_enrollment.json"); assertEquals(8, trackerObjects.getEvents().size()); TrackerBundle trackerBundle = diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java index 2fba7e1e40cc..4fc65a34a942 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java @@ -28,6 +28,7 @@ package org.hisp.dhis.tracker.imports.programrule; import static org.hisp.dhis.programrule.ProgramRuleActionType.ASSIGN; +import static org.hisp.dhis.test.utils.Assertions.assertContainsOnly; import static org.hisp.dhis.tracker.Assertions.assertHasOnlyErrors; import static org.hisp.dhis.tracker.Assertions.assertHasOnlyWarnings; import static org.hisp.dhis.tracker.imports.validation.ValidationCode.E1307; @@ -35,9 +36,12 @@ import static org.hisp.dhis.tracker.imports.validation.ValidationCode.E1310; import java.io.IOException; +import java.util.List; import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle; +import org.hisp.dhis.eventdatavalue.EventDataValue; import org.hisp.dhis.preheat.PreheatIdentifier; +import org.hisp.dhis.program.Event; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.programrule.ProgramRule; @@ -47,6 +51,7 @@ import org.hisp.dhis.programrule.ProgramRuleService; import org.hisp.dhis.programrule.ProgramRuleVariable; import org.hisp.dhis.programrule.ProgramRuleVariableService; +import org.hisp.dhis.programrule.ProgramRuleVariableSourceType; import org.hisp.dhis.setting.SettingKey; import org.hisp.dhis.setting.SystemSettingManager; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; @@ -57,8 +62,11 @@ import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.user.User; +import org.hisp.dhis.util.DateUtils; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.springframework.beans.factory.annotation.Autowired; class ProgramRuleAssignActionTest extends TrackerTest { @@ -76,6 +84,8 @@ class ProgramRuleAssignActionTest extends TrackerTest { private DataElement dataElement1; + private DataElement dataElement2; + private TrackedEntityAttribute attribute1; @BeforeAll @@ -87,8 +97,7 @@ void setUp() throws IOException { program = bundle.getPreheat().get(PreheatIdentifier.UID, Program.class, "BFcipDERJnf"); dataElement1 = bundle.getPreheat().get(PreheatIdentifier.UID, DataElement.class, "DATAEL00001"); - DataElement dataElement2 = - bundle.getPreheat().get(PreheatIdentifier.UID, DataElement.class, "DATAEL00002"); + dataElement2 = bundle.getPreheat().get(PreheatIdentifier.UID, DataElement.class, "DATAEL00002"); attribute1 = bundle.getPreheat().get(PreheatIdentifier.UID, TrackedEntityAttribute.class, "dIVt4l5vIOa"); TrackedEntityAttribute attribute2 = @@ -100,7 +109,12 @@ void setUp() throws IOException { programRuleVariableService.addProgramRuleVariable(programRuleVariable); programRuleVariableService.addProgramRuleVariable(programRuleVariableAttribute); - assignProgramRule(); + ProgramRuleVariable programRuleVariablePreviousEvent = + createProgramRuleVariableWithDataElement('C', program, dataElement1); + programRuleVariablePreviousEvent.setSourceType( + ProgramRuleVariableSourceType.DATAELEMENT_PREVIOUS_EVENT); + programRuleVariableService.addProgramRuleVariable(programRuleVariablePreviousEvent); + trackerImportService.importTracker( new TrackerImportParams(), fromJson("tracker/programrule/te_enrollment_completed_event.json")); @@ -109,6 +123,7 @@ void setUp() throws IOException { @Test void shouldNotImportWithWarningWhenAttributeWithSameValueIsAssignedByAssignRule() throws IOException { + assignProgramRule(); TrackerImportParams params = new TrackerImportParams(); TrackerObjects trackerObjects = fromJson("tracker/programrule/te_enrollment_update_attribute_same_value.json"); @@ -119,9 +134,43 @@ void shouldNotImportWithWarningWhenAttributeWithSameValueIsAssignedByAssignRule( assertHasOnlyWarnings(importReport, E1310); } + @ParameterizedTest + @CsvSource({"2024-02-10,THIRD", "2024-01-28,SECOND", "2024-01-19,FIRST"}) + void shouldImportEventAndCorrectlyAssignPreviousEventDataValue( + String eventOccurredDate, String previousEventDataValue) throws IOException { + TrackerImportParams params = new TrackerImportParams(); + TrackerObjects trackerObjects = + fromJson("tracker/programrule/three_events_with_different_dates.json"); + params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + + trackerImportService.importTracker(params, trackerObjects); + + assignPreviousEventProgramRule(); + + trackerObjects = fromJson("tracker/programrule/event_with_data_value.json"); + + trackerObjects + .getEvents() + .get(0) + .setOccurredAt(DateUtils.instantFromDateAsString(eventOccurredDate)); + + ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + assertHasOnlyWarnings(importReport, E1308); + + Event event = manager.get(Event.class, "D9PbzJY8bZZ"); + + List eventDataValues = + event.getEventDataValues().stream() + .filter(dv -> dv.getDataElement().equals("DATAEL00002")) + .map(EventDataValue::getValue) + .toList(); + assertContainsOnly(List.of(previousEventDataValue), eventDataValues); + } + @Test void shouldImportWithWarningWhenDataElementWithSameValueIsAssignedByAssignRule() throws IOException { + assignProgramRule(); TrackerImportParams params = new TrackerImportParams(); TrackerObjects trackerObjects = fromJson("tracker/programrule/event_update_datavalue_same_value.json"); @@ -134,6 +183,7 @@ void shouldImportWithWarningWhenDataElementWithSameValueIsAssignedByAssignRule() @Test void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() throws IOException { + assignProgramRule(); TrackerImportParams params = new TrackerImportParams(); TrackerObjects trackerObjects = fromJson("tracker/programrule/event_update_datavalue_different_value.json"); @@ -148,6 +198,7 @@ void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() th void shouldImportWithWarningWhenDataElementWithDifferentValueIsAssignedByAssignRuleAndOverwriteKeyIsTrue() throws IOException { + assignProgramRule(); systemSettingManager.saveSystemSetting(SettingKey.RULE_ENGINE_ASSIGN_OVERWRITE, true); TrackerImportParams params = new TrackerImportParams(); TrackerObjects trackerObjects = @@ -163,6 +214,7 @@ void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() th void shouldImportWithWarningWhenDataElementWithDifferentAndEmptyValueIsAssignedByAssignRuleAndOverwriteKeyIsTrue() throws IOException { + assignProgramRule(); systemSettingManager.saveSystemSetting(SettingKey.RULE_ENGINE_ASSIGN_OVERWRITE, true); TrackerImportParams params = new TrackerImportParams(); TrackerObjects trackerObjects = @@ -188,6 +240,16 @@ private void assignProgramRule() { programRuleService.updateProgramRule(programRule); } + private void assignPreviousEventProgramRule() { + ProgramRule programRule = createProgramRule('G', program, null, "true"); + programRuleService.addProgramRule(programRule); + ProgramRuleAction programRuleAction = + createProgramRuleAction(programRule, ASSIGN, dataElement2, "#{ProgramRuleVariableC}"); + programRuleActionService.addProgramRuleAction(programRuleAction); + programRule.getProgramRuleActions().add(programRuleAction); + programRuleService.updateProgramRule(programRule); + } + private ProgramRule createProgramRule( char uniqueCharacter, Program program, ProgramStage programStage, String condition) { ProgramRule programRule = createProgramRule(uniqueCharacter, program); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/programrule/engine/ProgramRuleEngineDescriptionTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleEngineDescriptionTest.java similarity index 97% rename from dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/programrule/engine/ProgramRuleEngineDescriptionTest.java rename to dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleEngineDescriptionTest.java index 925c29d3760e..1f31e2b4a7dd 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/programrule/engine/ProgramRuleEngineDescriptionTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleEngineDescriptionTest.java @@ -25,7 +25,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.hisp.dhis.programrule.engine; +package org.hisp.dhis.tracker.imports.programrule; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -49,23 +49,18 @@ import org.hisp.dhis.programrule.ProgramRuleVariableSourceType; import org.hisp.dhis.rules.models.RuleEngineValidationException; import org.hisp.dhis.rules.models.RuleValidationResult; -import org.hisp.dhis.test.integration.PostgresIntegrationTestBase; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; +import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.imports.programrule.engine.ProgramRuleEngine; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.TestInstance.Lifecycle; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.transaction.annotation.Transactional; /** * @author Zubair Asghar */ -@TestInstance(Lifecycle.PER_CLASS) -@Transactional -class ProgramRuleEngineDescriptionTest extends PostgresIntegrationTestBase { +class ProgramRuleEngineDescriptionTest extends TrackerTest { private static final UID PROGRAM_UID = UID.of("BFcipDERJnf"); private String conditionTextAtt = @@ -149,7 +144,7 @@ class ProgramRuleEngineDescriptionTest extends PostgresIntegrationTestBase { @Autowired private ProgramRuleService programRuleService; - @BeforeEach + @BeforeAll void setUp() { constantPI = createConstant('P', 3.14); constantArea = createConstant('A', 22.1); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java index b4c78fd2b9d5..0312414c72e1 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java @@ -49,6 +49,7 @@ import org.hisp.dhis.tracker.imports.report.ValidationReport; import org.hisp.dhis.user.User; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -77,6 +78,12 @@ void setUp() throws IOException { manager.flush(); } + @BeforeEach + void setUpUser() { + importUser = userService.getUser("tTgjgobT1oS"); + injectSecurityContextUser(importUser); + } + @Test void testEnrollmentValidationOkAll() throws IOException { TrackerImportParams params = TrackerImportParams.builder().userId(importUser.getUid()).build(); diff --git a/dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/event_with_data_value.json b/dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/event_with_data_value.json new file mode 100644 index 000000000000..0a936632d9dc --- /dev/null +++ b/dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/event_with_data_value.json @@ -0,0 +1,46 @@ +{ + "trackedEntities": [], + "enrollments": [], + "events": [ + { + "event": "D9PbzJY8bZZ", + "status": "COMPLETED", + "program": { + "idScheme": "UID", + "identifier": "BFcipDERJnf" + }, + "programStage": { + "idScheme": "UID", + "identifier": "NpsdDv6kKSO" + }, + "enrollment": "TvctPPhpD8u", + "orgUnit": { + "idScheme": "UID", + "identifier": "h4w96yEMlzO" + }, + "relationships": [], + "occurredAt": "2024-02-01T00:00:00.000", + "scheduledAt": "2019-01-28T12:10:38.100", + "storedBy": "admin", + "deleted": false, + "attributeOptionCombo": { + "idScheme": "UID" + }, + "attributeCategoryOptions": [], + + "dataValues": [ + { + "storedBy": "admin", + "providedElsewhere": false, + "dataElement": { + "idScheme": "UID", + "identifier": "DATAEL00001" + }, + "value": "LAST" + } + ], + "notes": [] + } + ], + "relationships": [] +} \ No newline at end of file diff --git a/dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/three_events_with_different_dates.json b/dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/three_events_with_different_dates.json new file mode 100644 index 000000000000..a889a716896d --- /dev/null +++ b/dhis-2/dhis-test-integration/src/test/resources/tracker/programrule/three_events_with_different_dates.json @@ -0,0 +1,124 @@ +{ + "trackedEntities": [], + "enrollments": [], + "events": [ + { + "event": "D9PbzJY8bJO", + "status": "COMPLETED", + "program": { + "idScheme": "UID", + "identifier": "BFcipDERJnf" + }, + "programStage": { + "idScheme": "UID", + "identifier": "NpsdDv6kKSO" + }, + "enrollment": "TvctPPhpD8u", + "orgUnit": { + "idScheme": "UID", + "identifier": "h4w96yEMlzO" + }, + "relationships": [], + "occurredAt": "2024-01-11T00:00:00.000", + "scheduledAt": "2019-01-28T12:10:38.100", + "storedBy": "admin", + "deleted": false, + "attributeOptionCombo": { + "idScheme": "UID" + }, + "attributeCategoryOptions": [], + + "dataValues": [ + { + "storedBy": "admin", + "providedElsewhere": false, + "dataElement": { + "idScheme": "UID", + "identifier": "DATAEL00001" + }, + "value": "FIRST" + } + ], + "notes": [] + }, + { + "event": "D9PbzJY8bFF", + "status": "COMPLETED", + "program": { + "idScheme": "UID", + "identifier": "BFcipDERJnf" + }, + "programStage": { + "idScheme": "UID", + "identifier": "NpsdDv6kKSO" + }, + "enrollment": "TvctPPhpD8u", + "orgUnit": { + "idScheme": "UID", + "identifier": "h4w96yEMlzO" + }, + "relationships": [], + "occurredAt": "2024-01-25T00:00:00.000", + "scheduledAt": "2019-01-28T12:10:38.100", + "storedBy": "admin", + "deleted": false, + "attributeOptionCombo": { + "idScheme": "UID" + }, + "attributeCategoryOptions": [], + + "dataValues": [ + { + "storedBy": "admin", + "providedElsewhere": false, + "dataElement": { + "idScheme": "UID", + "identifier": "DATAEL00001" + }, + "value": "SECOND" + } + ], + "notes": [] + }, + { + "event": "D9PbzJY8bXX", + "status": "COMPLETED", + "program": { + "idScheme": "UID", + "identifier": "BFcipDERJnf" + }, + "programStage": { + "idScheme": "UID", + "identifier": "NpsdDv6kKSO" + }, + "enrollment": "TvctPPhpD8u", + "orgUnit": { + "idScheme": "UID", + "identifier": "h4w96yEMlzO" + }, + "relationships": [], + "occurredAt": "2024-01-30T00:00:00.000", + "scheduledAt": "2019-01-28T12:10:38.100", + "storedBy": "admin", + "deleted": false, + "attributeOptionCombo": { + "idScheme": "UID" + }, + "attributeCategoryOptions": [], + + "dataValues": [ + { + "storedBy": "admin", + "providedElsewhere": false, + "dataElement": { + "idScheme": "UID", + "identifier": "DATAEL00001" + }, + "value": "THIRD" + } + ], + "notes": [] + } + ], + "relationships": [] +} \ No newline at end of file diff --git a/dhis-2/dhis-test-integration/src/test/resources/tracker/tracker_basic_metadata.json b/dhis-2/dhis-test-integration/src/test/resources/tracker/tracker_basic_metadata.json index 0c5df5b57e01..66db4ea628af 100644 --- a/dhis-2/dhis-test-integration/src/test/resources/tracker/tracker_basic_metadata.json +++ b/dhis-2/dhis-test-integration/src/test/resources/tracker/tracker_basic_metadata.json @@ -1191,7 +1191,11 @@ "id": "QfUVllTs6cS" } ], - "teiSearchOrganisationUnits": [], + "teiSearchOrganisationUnits": [ + { + "id": "QfUVllTs6cS" + } + ], "created": "2019-03-25T13:58:35.560", "code": "netroms", "organisationUnits": [], diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/event/EventsExportControllerPostgresTest.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/event/EventsExportControllerPostgresTest.java index 715342e7121e..2b002157b395 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/event/EventsExportControllerPostgresTest.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/event/EventsExportControllerPostgresTest.java @@ -106,6 +106,7 @@ void setUp() { manager.save(orgUnit); user = createAndAddUser("username", orgUnit, ALL.name()); + injectSecurityContextUser(user); trackedEntityType = trackedEntityTypeAccessible();