Skip to content

Commit

Permalink
Adds template based actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud DEMARCQ authored and Arnaud DEMARCQ committed Jun 18, 2024
1 parent 9e769cd commit cc17cff
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 9 deletions.
7 changes: 7 additions & 0 deletions rtsl_util/CommonUtils/Dhis2CucumberTestTool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
<classifier>v40.2.2</classifier>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.33</version>
</dependency>

<!--
<dependency>
<groupId>org.hisp.dhis.integration.sdk</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
package org.rtsl.dhis2.cucumber.definitions;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.Version;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.Given;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.hisp.dhis.Dhis2;
import org.hisp.dhis.api.model.v40_2_2.AttributeInfo;
import org.hisp.dhis.api.model.v40_2_2.Body;
import org.hisp.dhis.api.model.v40_2_2.EnrollmentInfo;
import org.hisp.dhis.api.model.v40_2_2.OrganisationUnit;
import org.hisp.dhis.api.model.v40_2_2.Program;
import org.hisp.dhis.api.model.v40_2_2.TrackedEntityInfo;
import org.hisp.dhis.api.model.v40_2_2.TrackerImportReport;
import org.hisp.dhis.integration.sdk.api.Dhis2Client;
import org.hisp.dhis.model.OrgUnit;
import org.hisp.dhis.model.TrackedEntityAttribute;
import org.hisp.dhis.response.object.ObjectResponse;
import org.rtsl.dhis2.cucumber.TestUniqueId;
import org.slf4j.Logger;
Expand All @@ -19,6 +47,8 @@ public class Dhis2StepDefinitions {

private static final Logger LOGGER = LoggerFactory.getLogger(Dhis2StepDefinitions.class);

HttpClientConnectionManager httpClientManager;

@Inject
@Named("testUniqueId")
private TestUniqueId testUniqueId;
Expand All @@ -27,17 +57,31 @@ public class Dhis2StepDefinitions {
@Named("dhisClient")
private Dhis2 dhsi2Client;

@Inject
@Named("dhisSdkClient")
private Dhis2Client dhisSdkClient;

@Inject
@Named("testAtomicInt")
private AtomicInteger testCounter;

String currentFaciliyId = null;

public String getCurrentFaciliyId() {
return currentFaciliyId;
}

@Given("I have {int} cukes in my belly")
public void i_have_n_cukes_in_my_belly(Integer cukes) {
testCounter.addAndGet(cukes);
LOGGER.info("TEST: <{}> <{}> <{}>", cukes, testCounter.hashCode(), this);
}
private Scenario scenario;

@Before
public void setup(Scenario scenario) {
this.scenario = scenario;
}

@Given("I create a new Facility")
public void i_create_a_new_facility() {
Expand All @@ -50,15 +94,79 @@ public void i_create_a_new_facility() {
newFacility.setId(newFacilityId);
this.currentFaciliyId = newFacilityId;
LOGGER.info("created Facility: <{}> <{}>", newFacility.getId(), newFacility.getName());

scenario.log("Created new facility with Id:" + newFacility.getId() + " and Name:" + newFacility.getName());
}

@Given("I register that Facility for program {string}")
public void i_register_that_facility(String programName) throws Exception {

Configuration cfg = new Configuration(Configuration.VERSION_2_3_33);
cfg.setClassForTemplateLoading(this.getClass(), "/templates/");
Template registerTemplate = cfg.getTemplate("register_facility_to_program.tpl.json");
Map<String, Object> templateContext = Map.of(
"data", this,
"programName", programName);
StringWriter sw = new StringWriter();
registerTemplate.process(templateContext, sw);
String body = sw.toString();
LOGGER.info(body);

try (CloseableHttpClient httpClient = HttpClients.createDefault();) {
HttpPut request = new HttpPut("https://dhis2-sandbox2.simple.org/api/programs/pMIglSEqPGS?mergeMode=MERGE&importStrategy=CREATE_AND_UPDATE");
request.setEntity(new StringEntity(body));
request.setHeader("Accept", "application/json");
request.setHeader("Content-Type", "application/json");
request.setHeader("Authorization", "Basic YWRtaW46ZGlzdHJpY3Q=");
try (CloseableHttpResponse response = httpClient.execute(request)) {

}

}
}

@Given("I create a new Patient for this Facility with the following characteristics")
public void i_create_a_new_patient_for_this_facility_with_the_following_characteristics(List<Map<String, String>> dataTable) {
public void i_create_a_new_patient_for_this_facility_with_the_following_characteristics(Map<String, String> dataTable) {

LOGGER.info("Creating patient for data: <{}> ", dataTable);
LOGGER.info("{}", new Date().toString());

// Creating a new TEI
TrackedEntityInfo newTEI = new TrackedEntityInfo();
newTEI.setOrgUnit("KBILynmuWsE"); // todo: use current facility
newTEI.setTrackedEntityType("MCPQUTHX1Ze"); // Person

// Creating new Enrollment
EnrollmentInfo newEnrollment = new EnrollmentInfo();
newEnrollment.setOrgUnit("KBILynmuWsE"); // todo: use current facility
newEnrollment.setProgram("pMIglSEqPGS"); // Hypertension & Diabetes
newEnrollment.setEnrolledAt(new Date());
newEnrollment.setOccurredAt(new Date());
newEnrollment.setAttributes(getAttributes(dataTable));
// TODO

newTEI.setEnrollments(Arrays.asList(newEnrollment));

TrackerImportReport trackerImportReport = dhisSdkClient.post("tracker")
.withResource(new Body().withTrackedEntities(Arrays.asList(newTEI
)))
.withParameter("async", "false")
.transfer()
.returnAs(TrackerImportReport.class);
/**/

LOGGER.info("{}", trackerImportReport.getBundleReport().get().toString());
//dhisSdkClient.
// TODO
}

private List<AttributeInfo> getAttributes(Map<String, String> data) {
List<AttributeInfo> returnList = new ArrayList<>();
for (String currentKey : data.keySet()) {
String value = data.get(currentKey);
AttributeInfo currentAttribute = new AttributeInfo().withAttribute(currentKey).withValue(value);
returnList.add(currentAttribute);
}
return returnList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@

<bean id="dhisClient" class="org.hisp.dhis.Dhis2">
<constructor-arg ref="dhis2Config" />
</bean>
</bean>


<bean id="dhis2SdkClientBuilder" class="org.hisp.dhis.integration.sdk.Dhis2ClientBuilder" factory-method="newClient">
<constructor-arg value="${dhis2.api.url}/api" />
<constructor-arg value="${dhis2.api.username}" />
<constructor-arg value="${dhis2.api.password}" />
</bean>


<bean id="dhisSdkClient" factory-bean="dhis2SdkClientBuilder" factory-method="build" />


<!-- freemarker templates
<bean id="templateFolderUrl" class="java.lang.ClassLoader" factory-method="getSystemResource" >
<constructor-arg value="templates" />
</bean>
<bean id="templateFolderUri" factory-bean="templateFolderUrl" factory-method="toURI" />
<bean id="templateFolder" class="java.io.File" >
<constructor-arg ref="templateFolderUri" />
</bean>
<bean id="freemarkerConfig" class="freemarker.template.Configuration" scope="singleton">
<property name="directoryForTemplateLoading" ref="templateFolder"/>
</bean>
-->






</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Hypertension & Diabetes",
"shortName": "ncd_htn",
"programType": "WITH_REGISTRATION",
"organisationUnits": [
{
"id": "${data.currentFaciliyId}"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</encoder>
</appender>

<root level="debug">
<root level="trace">
<appender-ref ref="unitTestLogs"/>
</root>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ Feature: Demo Dashboard Auditing

Scenario: Load Test Patients and check results
Given I create a new Facility
Given I register that Facility for program "YQj5qpbzQxh"

#
# Patient 1 - Dead patient
#
Given I create a new Patient for this Facility with the following characteristics
| Field | Value |
| Name | Joe Bloggs |
| Registration_Date | 2024-01-16 |
| Has_hypertension | 'YES' |
| sB1IHYu2xQT | Test |
| YJGACwhN0St | true |
| jCRIT4GMMOS | YES |
| NI0QRzJvQ0k | 32 |
| Ot616hCy9j7 | KOLARA |
| ENRjVGxVL6l | TEST |
| v4DnYfXn9Mu | YES |
Given That patient visited for Hypertension on "2024-01-16" with Blood Pressure reading 145:92
Given That patient visited for Hypertension on "2024-01-16" with Blood Pressure reading 145:92
Given That patient visited for Hypertension on "2024-02-02" with Blood Pressure reading 140:87
Expand Down

0 comments on commit cc17cff

Please sign in to comment.