Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IT for createEncounter with grouped obs #514

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EncounterResourceImpl extends BaseResource implements EncounterResource {

private static final String OPENMRS_V19 = "1.9";

@Autowired
Expand Down Expand Up @@ -59,7 +61,7 @@ public EncounterListResult queryForAllEncountersByPatientId(Config config, Strin
public Encounter getEncounterById(Config config, String uuid) {
String responseJson = getJson(config, "/encounter/{uuid}?v=full", uuid);

return checkVersionAndSetEncounter(config, responseJson);
return checkVersionAndSetEncounter(config, responseJson);
}

@Override
Expand Down Expand Up @@ -108,7 +110,7 @@ private Encounter checkVersionAndSetEncounter(Config config, String responseJson
if (OPENMRS_V19.equals(config.getOpenMrsVersion())) {
createdEncounter = buildGsonWithAdaptersDeserialize().fromJson(responseJson, Encounter.class);
} else {
createdEncounter = (Encounter) JsonUtils.readJson(responseJson, Encounter.class);
createdEncounter = (Encounter) JsonUtils.readJsonWithAdapters(responseJson, Encounter.class, createValueAdapter());
}
return createdEncounter;
}
Expand All @@ -117,4 +119,11 @@ private JsonArray prepareAllEncountersList(String json) {
JsonObject obj = new JsonParser().parse(json).getAsJsonObject();
return obj.getAsJsonArray("results");
}

private Map<Type, Object> createValueAdapter() {
Map<Type, Object> valueAdapter = new HashMap<>();
valueAdapter.put(Observation.ObservationValue.class, new Observation.ObservationValueDeserializer());

return valueAdapter;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.motechproject.openmrs.it;

import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
Expand All @@ -11,9 +13,12 @@
import org.motechproject.mds.query.QueryParams;
import org.motechproject.mds.util.Order;
import org.motechproject.openmrs.domain.Attribute;
import org.motechproject.openmrs.domain.Concept;
import org.motechproject.openmrs.domain.ConceptName;
import org.motechproject.openmrs.domain.Encounter;
import org.motechproject.openmrs.domain.EncounterType;
import org.motechproject.openmrs.domain.Location;
import org.motechproject.openmrs.domain.Observation;
import org.motechproject.openmrs.domain.Patient;
import org.motechproject.openmrs.domain.Person;
import org.motechproject.openmrs.domain.Provider;
Expand Down Expand Up @@ -65,7 +70,9 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.motechproject.openmrs.tasks.OpenMRSActionProxyService.DEFAULT_LOCATION_NAME;
import static org.motechproject.openmrs.util.TestConstants.DEFAULT_CONFIG_NAME;

Expand Down Expand Up @@ -219,7 +226,7 @@ public void testOpenMRSEncounterDataSourceAndCreateEncounterAction() throws Inte
// Give Tasks some time to process
assertTrue(waitForTaskExecution(taskID));

checkIfEncounterWasCreatedProperly();
checkIfEncounterWasCreatedProperly(false);
}

@Test
Expand All @@ -235,7 +242,7 @@ public void testOpenMRSProviderDataSourceAndCreateEncounterAction() throws Inter
}

@Test
public void testCreateEncounterPostActionParameter() throws InterruptedException {
public void testCreateEncounterPostActionParameter() throws Exception {
Task task = prepareCreateEncounterPostActionParameterTask();

getTriggerHandler().registerHandlerFor(task.getTrigger().getEffectiveListenerSubject());
Expand Down Expand Up @@ -274,6 +281,18 @@ public void testCreatePatientPostActionParameter() throws InterruptedException {
assertEquals(firstPatientUuid, address.getAddress1());
}

@Test
public void testCreateEncounterActionWithGroupedObservations() throws Exception {
Long taskID = prepareCreateEncounterWithObsTask();

activateTrigger();

// Give Tasks some time to process
assertTrue(waitForTaskExecution(taskID));

checkIfEncounterWasCreatedProperly(true);
}

private Long createPatientTestTask() {
TaskTriggerInformation triggerInformation = new TaskTriggerInformation("CREATE SettingsRecord", "data-services", MDS_CHANNEL_NAME,
VERSION, TRIGGER_SUBJECT, TRIGGER_SUBJECT);
Expand Down Expand Up @@ -388,12 +407,12 @@ private void createEncounterTestData() throws ParseException, ConceptNameAlready
createdEncounter = encounterService.createEncounter(DEFAULT_CONFIG_NAME, encounter);
}

private Task prepareCreateEncounterPostActionParameterTask() {
private Task prepareCreateEncounterPostActionParameterTask() throws Exception {
TaskTriggerInformation triggerInformation = new TaskTriggerInformation("CREATE SettingsRecord", "data-services", MDS_CHANNEL_NAME,
VERSION, TRIGGER_SUBJECT, TRIGGER_SUBJECT);

ArrayList<TaskActionInformation> taskActions = new ArrayList();
taskActions.add(prepareCreateEncounterActionInformation());
taskActions.add(prepareCreateEncounterActionInformation(false));
taskActions.add(prepareCreatePatientActionInformation("{{pa.0.uuid}}", "Jacob Lee", false));

Task task = new Task("OpenMRSEncounterPostActionParameterTestTask", triggerInformation, taskActions, new TaskConfig(), true, true);
Expand All @@ -402,6 +421,19 @@ private Task prepareCreateEncounterPostActionParameterTask() {
return task;
}

private Long prepareCreateEncounterWithObsTask() throws Exception {
TaskTriggerInformation triggerInformation = new TaskTriggerInformation("CREATE SettingsRecord", "data-services", MDS_CHANNEL_NAME,
VERSION, TRIGGER_SUBJECT, TRIGGER_SUBJECT);

ArrayList<TaskActionInformation> taskActions = new ArrayList();
taskActions.add(prepareCreateEncounterActionInformation(true));

Task task = new Task("OpenMRSEncounterWithObsTestTask", triggerInformation, taskActions, new TaskConfig(), true, true);
getTaskService().save(task);

return task.getId();
}

private Task prepareCreatePatientPostActionParameterTask(){
TaskTriggerInformation triggerInformation = new TaskTriggerInformation("CREATE SettingsRecord", "data-services", MDS_CHANNEL_NAME,
VERSION, TRIGGER_SUBJECT, TRIGGER_SUBJECT);
Expand Down Expand Up @@ -451,7 +483,7 @@ private TaskActionInformation prepareCreatePatientActionInformation(String addre
return actionInformation;
}

private TaskActionInformation prepareCreateEncounterActionInformation(){
private TaskActionInformation prepareCreateEncounterActionInformation(boolean withObs) throws ConceptNameAlreadyInUseException {
TaskActionInformation actionInformation = new TaskActionInformation("Create Encounter [" + DEFAULT_CONFIG_NAME + "]", OPENMRS_CHANNEL_NAME,
OPENMRS_CHANNEL_NAME, VERSION, TEST_INTERFACE, "createEncounter");
actionInformation.setSubject(String.format("createEncounter.%s", DEFAULT_CONFIG_NAME));
Expand All @@ -460,9 +492,15 @@ private TaskActionInformation prepareCreateEncounterActionInformation(){
values.put(Keys.PROVIDER_UUID, createdProvider.getUuid());
values.put(Keys.PATIENT_UUID, createdPatient.getUuid());
values.put(Keys.ENCOUNTER_TYPE, createdEncounterType.getUuid());
values.put(Keys.ENCOUNTER_DATE, new DateTime("2015-01-16T00:00:00Z").toString());
values.put(Keys.ENCOUNTER_DATE, encounterDateTime.toString());
values.put(Keys.LOCATION_NAME, DEFAULT_LOCATION_NAME);
values.put(Keys.CONFIG_NAME, DEFAULT_CONFIG_NAME);

if (withObs) {
String obs = Joiner.on('\n').withKeyValueSeparator(":").join(prepareObservations());
values.put(Keys.OBSERVATION, obs);
}

actionInformation.setValues(values);

return actionInformation;
Expand Down Expand Up @@ -614,7 +652,7 @@ private void checkIfPatientWasCreatedProperly() {
assertEquals(createdPatient.getPerson().getDisplay(), actualName.getGivenName());
}

private void checkIfEncounterWasCreatedProperly() {
private void checkIfEncounterWasCreatedProperly(boolean withObs) {
List<Encounter> encounterList = encounterService.getEncountersByEncounterType(DEFAULT_CONFIG_NAME, MOTECH_ID, createdEncounterType.getUuid());

assertEquals(2, encounterList.size());
Expand All @@ -623,8 +661,13 @@ private void checkIfEncounterWasCreatedProperly() {
if (!Objects.equals(encounter.getUuid(), createdEncounter.getUuid())) {
assertEquals(createdEncounter.getPatient().getUuid(), encounter.getPatient().getUuid());
assertEquals(createdEncounter.getEncounterType().getUuid(), encounter.getEncounterType().getUuid());
assertEquals(createdEncounter.getVisit().getUuid(), encounter.getVisit().getUuid());
assertEquals(createdEncounter.getEncounterDatetime(), encounter.getEncounterDatetime());

if (withObs) {
checkEncounterObs(encounter.getObs());
} else {
assertEquals(createdEncounter.getVisit().getUuid(), encounter.getVisit().getUuid());
}
}
}
}
Expand All @@ -639,6 +682,78 @@ private void checkIfProviderWasCreatedProperly() {
assertEquals(createdProvider.getPerson().getUuid(), address.getAddress3());
}

private Map<String, String> prepareObservations() throws ConceptNameAlreadyInUseException {
Map<String, String> observations = new HashMap<>();

String parent = createConcept("Concept1");
String child1 = createConcept("Concept2");
String child1Child1 = createConcept("Concept3");
String child1Child2 = createConcept("Concept4");
String child2 = createConcept("Concept5");
String child3 = createConcept("Concept6");

observations.put(parent + "/" + child1 + "/" + child1Child1, "Yes");
observations.put(parent + "/" + child1 + "/" + child1Child2, "No");
observations.put(parent + "/" + child2, "2nd");
observations.put(parent + "/" + child3, "Maybe");

return observations;
}

private String createConcept(String name) throws ConceptNameAlreadyInUseException {
Concept concept = new Concept();
ConceptName conceptName = new ConceptName(name);

concept.setNames(Collections.singletonList(conceptName));
concept.setDatatype(new Concept.DataType("TEXT"));
concept.setConceptClass(new Concept.ConceptClass("Test"));

return conceptService.createConcept(DEFAULT_CONFIG_NAME, concept).getUuid();
}

private void checkEncounterObs(List<Observation> observations) {
assertEquals(1L, observations.size());

Observation observation = observations.get(0);

assertNull(observation.getValue());
isConceptEquals(observation, "Concept1");

List<Observation> groupMembers = observation.getGroupMembers();
assertEquals(3L, groupMembers.size());

for (Observation obs : groupMembers) {
if (isConceptEquals(obs, "Concept2")) {
assertNull(obs.getValue());

List<Observation> childGroupMembers = obs.getGroupMembers();
assertEquals(2L, childGroupMembers.size());

for (Observation childObs : childGroupMembers) {
if (isConceptEquals(obs, "Concept3")) {
assertEquals("Yes", childObs.getValue().getDisplay());
} else if (isConceptEquals(obs, "Concept4")) {
assertEquals("No", childObs.getValue().getDisplay());
} else {
fail();
}
}
} else if (isConceptEquals(obs, "Concept5")) {
assertEquals("2nd", obs.getValue().getDisplay());
assertEquals(0L, obs.getGroupMembers().size());
} else if (isConceptEquals(obs, "Concept6")) {
assertEquals("Maybe", obs.getValue().getDisplay());
assertEquals(0L, obs.getGroupMembers().size());
} else {
fail();
}
}
}

private boolean isConceptEquals(Observation obs, String conceptName) {
return StringUtils.equals(obs.getConcept().getDisplay(), conceptName);
}

private void activateTrigger() {
settingsDataService.create(new SettingsRecord());
}
Expand Down