Skip to content

Commit

Permalink
fix: Upgrade to correct rule-engine version [DHIS2-15261] (#15347)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Oct 12, 2023
1 parent fc9fe29 commit 9fbbe6e
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (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.tracker.programrule;

import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dxf2.metadata.feedback.ImportReport;
import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle;
import org.hisp.dhis.preheat.PreheatIdentifier;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.programrule.ProgramRule;
import org.hisp.dhis.programrule.ProgramRuleAction;
import org.hisp.dhis.programrule.ProgramRuleActionService;
import org.hisp.dhis.programrule.ProgramRuleActionType;
import org.hisp.dhis.programrule.ProgramRuleService;
import org.hisp.dhis.programrule.ProgramRuleVariable;
import org.hisp.dhis.programrule.ProgramRuleVariableService;
import org.hisp.dhis.setting.SettingKey;
import org.hisp.dhis.setting.SystemSettingManager;
import org.hisp.dhis.tracker.TrackerImportParams;
import org.hisp.dhis.tracker.TrackerImportService;
import org.hisp.dhis.tracker.TrackerImportStrategy;
import org.hisp.dhis.tracker.TrackerTest;
import org.hisp.dhis.tracker.report.TrackerImportReport;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

import static org.hisp.dhis.programrule.ProgramRuleActionType.ASSIGN;
import static org.hisp.dhis.tracker.Assertions.assertHasOnlyErrors;
import static org.hisp.dhis.tracker.Assertions.assertNoErrors;
import static org.hisp.dhis.tracker.report.TrackerErrorCode.E1307;
import static org.hisp.dhis.tracker.report.TrackerErrorCode.E1308;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ProgramRuleAssignActionTest extends TrackerTest {
@Autowired private TrackerImportService trackerImportService;

@Autowired private ProgramRuleService programRuleService;

@Autowired private ProgramRuleActionService programRuleActionService;

@Autowired private ProgramRuleVariableService programRuleVariableService;

@Autowired private SystemSettingManager systemSettingManager;

private Program program;

private DataElement dataElement1;

@Override
public void initTest() throws IOException {
ObjectBundle bundle = setUpMetadata("tracker/simple_metadata.json");
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");
ProgramRuleVariable programRuleVariable =
createProgramRuleVariableWithDataElement('A', program, dataElement2);
programRuleVariableService.addProgramRuleVariable(programRuleVariable);

injectAdminUser();

assignProgramRule();
trackerImportService.importTracker(
fromJson("tracker/programrule/tei_enrollment_completed_event.json"));
}

@Test
void shouldImportWithWarningWhenDataElementWithSameValueIsAssignedByAssignRule()
throws IOException {
TrackerImportParams params =
fromJson("tracker/programrule/event_update_datavalue_same_value.json");
params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE);

TrackerImportReport importReport = trackerImportService.importTracker(params);

assertNoErrors(importReport);
assertEquals(E1308, importReport.getValidationReport().getWarnings().get(0).getWarningCode());
}

@Test
void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() throws IOException {
TrackerImportParams params =
fromJson("tracker/programrule/event_update_datavalue_different_value.json");
params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE);

TrackerImportReport importReport = trackerImportService.importTracker(params);

assertHasOnlyErrors(importReport, E1307);
}

@Test
void
shouldImportWithWarningWhenDataElementWithDifferentValueIsAssignedByAssignRuleAndOverwriteKeyIsTrue()
throws IOException {
systemSettingManager.saveSystemSetting(SettingKey.RULE_ENGINE_ASSIGN_OVERWRITE, true);
TrackerImportParams params =
fromJson("tracker/programrule/event_update_datavalue_different_value.json");
params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE);

TrackerImportReport importReport = trackerImportService.importTracker(params);

assertNoErrors(importReport);
assertEquals(E1308, importReport.getValidationReport().getWarnings().get(0).getWarningCode());
}

@Test
void
shouldImportWithWarningWhenDataElementWithDifferentAndEmptyValueIsAssignedByAssignRuleAndOverwriteKeyIsTrue()
throws IOException {
systemSettingManager.saveSystemSetting(SettingKey.RULE_ENGINE_ASSIGN_OVERWRITE, true);
TrackerImportParams params =
fromJson("tracker/programrule/event_update_datavalue_empty_value.json");
params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE);

TrackerImportReport importReport = trackerImportService.importTracker(params);

assertNoErrors(importReport);
assertEquals(E1308, importReport.getValidationReport().getWarnings().get(0).getWarningCode());
}

private void assignProgramRule() {
ProgramRule programRule = createProgramRule('F', program, null, "true");
programRuleService.addProgramRule(programRule);
ProgramRuleAction programRuleAction =
createProgramRuleAction(programRule, ASSIGN, dataElement1, "#{ProgramRuleVariableA}");
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);
programRule.setUid("ProgramRul" + uniqueCharacter);
programRule.setProgramStage(programStage);
programRule.setCondition(condition);
return programRule;
}

private ProgramRuleAction createProgramRuleAction(
ProgramRule programRule,
ProgramRuleActionType actionType,
DataElement dataElement,
String data) {
ProgramRuleAction programRuleAction = createProgramRuleAction('A', programRule);
programRuleAction.setProgramRuleActionType(actionType);
programRuleAction.setContent("CONTENT");
programRuleAction.setDataElement(dataElement);
programRuleAction.setData(data);

return programRuleAction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"importMode": "COMMIT",
"idSchemes": {
"dataElementIdScheme": {
"idScheme": "UID"
},
"orgUnitIdScheme": {
"idScheme": "UID"
},
"programIdScheme": {
"idScheme": "UID"
},
"programStageIdScheme": {
"idScheme": "UID"
},
"idScheme": {
"idScheme": "UID"
},
"categoryOptionComboIdScheme": {
"idScheme": "UID"
},
"categoryOptionIdScheme": {
"idScheme": "UID"
}
},
"importStrategy": "CREATE",
"atomicMode": "ALL",
"flushMode": "AUTO",
"validationMode": "FULL",
"skipPatternValidation": false,
"skipSideEffects": false,
"skipRuleEngine": false,
"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": "2019-01-28T00:00:00.000",
"scheduledAt": "2019-01-28T12:10:38.100",
"storedBy": "admin",
"followup": false,
"deleted": false,
"attributeOptionCombo": {
"idScheme": "UID"
},
"attributeCategoryOptions": [],

"dataValues": [
{
"createdAt": "2019-01-28T12:10:38.113",
"storedBy": "admin",
"providedElsewhere": false,
"dataElement": {
"idScheme": "UID",
"identifier": "DATAEL00002"
},
"value": null
}
],
"notes": []
}
],
"relationships": [],
"username": "system-process"
}

0 comments on commit 9fbbe6e

Please sign in to comment.