Skip to content

Commit

Permalink
Make LabOrderHandler testable and add a test
Browse files Browse the repository at this point in the history
Thanks @piotr
  • Loading branch information
ibacher committed Jun 12, 2024
1 parent cd5efbc commit 3c95cf0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import lombok.Setter;
import org.hl7.fhir.r4.model.BooleanType;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.DecimalType;
Expand All @@ -30,13 +31,13 @@
@Component
public class LabOrderHandler {

@Autowired
@Setter(onMethod_ = {@Autowired})
private LabOnFhirConfig config;

@Autowired
@Setter(onMethod_ = {@Autowired})
private FhirTaskService taskService;

@Autowired
@Setter(onMethod_ = {@Autowired})
private FhirObservationService observationService;

public Task createOrder(Order order) throws OrderCreationException {
Expand Down Expand Up @@ -68,7 +69,7 @@ public Task createOrder(Order order) throws OrderCreationException {
CodeableConcept concept = (CodeableConcept) observation.getValue();
input.setValue(new StringType().setValue(concept.getCodingFirstRep().getDisplay()));
} else if (observation.getValue() instanceof Quantity) {
Double quantity = ((Quantity) observation.getValue()).getValue().doubleValue();
double quantity = ((Quantity) observation.getValue()).getValue().doubleValue();
DecimalType decimal = new DecimalType();
decimal.setValue(quantity);
input.setValue(decimal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
import lombok.Setter;
import org.hl7.fhir.r4.model.Location;
import org.hl7.fhir.r4.model.Organization;
import org.hl7.fhir.instance.model.api.IBaseResource;
Expand Down Expand Up @@ -45,7 +46,8 @@ public abstract class LabCreationListener implements EventListener {

private static final Logger log = LoggerFactory.getLogger(EncounterCreationListener.class);

private DaemonToken daemonToken;
@Setter
private DaemonToken daemonToken;

@Autowired
@Qualifier("labOrderFhirClient")
Expand All @@ -69,16 +71,8 @@ public abstract class LabCreationListener implements EventListener {

@Autowired
private LocationService locationService;

public DaemonToken getDaemonToken() {
return daemonToken;
}

public void setDaemonToken(DaemonToken daemonToken) {
this.daemonToken = daemonToken;
}

@Override
@Override
public void onMessage(Message message) {
log.trace("Received message {}", message);

Expand Down Expand Up @@ -125,7 +119,7 @@ public Bundle createLabBundle(Task task) {

Bundle.BundleEntryComponent component = transactionBundle.addEntry();
if (resource instanceof Location || resource instanceof Organization) {
org.openmrs.Location openmrsLocation = locationService.getLocationByUuid(location.getId());
org.openmrs.Location openmrsLocation = locationService.getLocationByUuid(resource.getId());
if (openmrsLocation != null) {
LocationAttributeType mflLocationAttributeType = locationService.getLocationAttributeTypeByUuid(MFL_LOCATION_ATTRIBUTE_TYPE_UUID);
Collection<LocationAttribute> locationAttributeTypes = openmrsLocation.getActiveAttributes();
Expand All @@ -135,12 +129,13 @@ public Bundle createLabBundle(Task task) {

Identifier mflIdentifier = new Identifier();
mflIdentifier.setSystem(MFL_LOCATION_IDENTIFIER_URI);
mflIdentifier.setValue(mflCode);
mflIdentifier.setValue(mflCode);

if (resource instanceof Organization) {
Organization organization = (Organization) resource;
organization.addIdentifier(mflIdentifier);
component.setResource(organization);
} else if (resource instanceof Location) {
} else {
Location location = (Location) resource;
location.addIdentifier(mflIdentifier);
component.setResource(location);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,104 @@
package org.openmrs.module.labonfhir.api;

import org.hl7.fhir.r4.model.Task;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.Location;
import org.openmrs.Obs;
import org.openmrs.Order;
import org.openmrs.Patient;
import org.openmrs.api.EncounterService;
import org.openmrs.api.LocationService;
import org.openmrs.api.ObsService;
import org.openmrs.api.OrderService;
import org.openmrs.api.PatientService;
import org.openmrs.module.fhir2.api.FhirObservationService;
import org.openmrs.module.fhir2.api.FhirTaskService;
import org.openmrs.module.labonfhir.LabOnFhirConfig;
import org.openmrs.module.labonfhir.api.fhir.OrderCreationException;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

public class LabOrderHandlerTest {
@Mock
private OrderService orderService;
@Mock
private EncounterService encounterService;
@Mock
private LocationService locationService;
@Mock
private PatientService patientService;
@Mock
private ObsService obsService;
@Mock
private LabOnFhirConfig labOnFhirConfig;
@Mock
private FhirObservationService fhirObservationService;
@Mock
private FhirTaskService fhirTaskService;

@Before
public void setUp() throws Exception {

MockitoAnnotations.initMocks(this);
}

@Test
public void createOrder_shouldCreateTaskAndERferenceOrder() {
public void createOrder_shouldCreateTask() throws OrderCreationException {
// Create a mock Patient object
Patient patient = new Patient();
patient.setUuid("patientUuid");

// Create a mock Encounter object
Encounter encounter = new Encounter();
encounter.setUuid("encounterUuid");
encounter.setLocation(new Location());
encounter.getLocation().setUuid("locationUuid");
encounter.setPatient(patient);

// Create a mock Order object
Order order = new Order();
order.setUuid("orderUuid");
order.setPatient(patient);

// Create a mock Obs object
Obs obs = new Obs();
obs.setUuid("obsUuid");
obs.setPerson(patient);
obs.setConcept(new Concept());
obs.getConcept().setUuid("conceptUuid");

obs.setEncounter(encounter);
encounter.getObs().add(obs);
encounter.addOrder(order);

// Mock the necessary dependencies
when(orderService.getOrderByUuid("orderUuid")).thenReturn(order);
when(encounterService.getEncounterByUuid("encounterUuid")).thenReturn(encounter);
when(locationService.getLocationByUuid("locationUuid")).thenReturn(new Location());
when(patientService.getPatientByUuid("patientUuid")).thenReturn(new Patient());
when(obsService.getObsByUuid("obsUuid")).thenReturn(new Obs());
when(fhirTaskService.create(any())).then((Answer<Task>) invocation -> (Task) invocation.getArguments()[0]);

// Call the createOrder method
LabOrderHandler labOrderHandler = new LabOrderHandler();
labOrderHandler.setConfig(labOnFhirConfig);
labOrderHandler.setObservationService(fhirObservationService);
labOrderHandler.setTaskService(fhirTaskService);

Task task = labOrderHandler.createOrder(order);

// Assert that the task is not null
assertThat(task, notNullValue());
assertThat(task.hasRequester(), is(true));
assertThat(task.getRequester().getIdentifier().getIdElement(), is("locationUuid"));
}
}

0 comments on commit 3c95cf0

Please sign in to comment.