Skip to content

Commit

Permalink
FM2-617: Use system to differentiate patient identifer types (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
k4pran authored Oct 4, 2023
1 parent f3f3356 commit 5d0b0ec
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface FhirPatientIdentifierSystemDao {

String getUrlByPatientIdentifierType(PatientIdentifierType patientIdentifierType);

PatientIdentifierType getPatientIdentifierTypeByUrl(String url);

@Authorized(PrivilegeConstants.GET_IDENTIFIER_TYPES)
Optional<FhirPatientIdentifierSystem> getFhirPatientIdentifierSystem(@Nonnull PatientIdentifierType identifierType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public String getUrlByPatientIdentifierType(PatientIdentifierType patientIdentif
.setProjection(Projections.property("url")).uniqueResult();
}

@Override
public PatientIdentifierType getPatientIdentifierTypeByUrl(String url) {
return (PatientIdentifierType) sessionFactory.getCurrentSession().createCriteria(FhirPatientIdentifierSystem.class)
.add(eq("url", url)).setProjection(Projections.property("patientIdentifierType")).uniqueResult();
}

@Override
public Optional<FhirPatientIdentifierSystem> getFhirPatientIdentifierSystem(
@Nonnull PatientIdentifierType patientIdentifierType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirPatientService;
import org.openmrs.module.fhir2.api.dao.FhirPatientDao;
import org.openmrs.module.fhir2.api.dao.FhirPatientIdentifierSystemDao;
import org.openmrs.module.fhir2.api.search.SearchQuery;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
import org.openmrs.module.fhir2.api.search.param.OpenmrsPatientSearchParams;
Expand All @@ -52,6 +53,9 @@ public class FhirPatientServiceImpl extends BaseFhirService<Patient, org.openmrs
@Autowired
private FhirPatientDao dao;

@Autowired
private FhirPatientIdentifierSystemDao systemDao;

@Autowired
private SearchQueryInclude<Patient> searchQueryInclude;

Expand All @@ -72,10 +76,12 @@ public Patient getById(@Nonnull Integer id) {
@Override
@Transactional(readOnly = true)
public PatientIdentifierType getPatientIdentifierTypeByIdentifier(Identifier identifier) {
if (identifier.hasSystem()) {
return systemDao.getPatientIdentifierTypeByUrl(identifier.getSystem());
}
if (identifier.getType() == null || StringUtils.isBlank(identifier.getType().getText())) {
return null;
}

return dao.getPatientIdentifierTypeByNameOrUuid(identifier.getType().getText(), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Optional;

Expand All @@ -22,6 +25,7 @@
import org.openmrs.PatientIdentifierType;
import org.openmrs.api.PatientService;
import org.openmrs.module.fhir2.TestFhirSpringConfiguration;
import org.openmrs.module.fhir2.api.FhirPatientIdentifierSystemService;
import org.openmrs.module.fhir2.model.FhirPatientIdentifierSystem;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -40,6 +44,9 @@ public class FhirPatientIdentifierSystemDaoImplTest extends BaseModuleContextSen
@Autowired
PatientService patientService;

@Autowired
FhirPatientIdentifierSystemService systemService;

@Before
public void setup() throws Exception {
dao = new FhirPatientIdentifierSystemDaoImpl();
Expand All @@ -55,6 +62,28 @@ public void shouldReturnUrlByPatientIdentifierType() {
assertThat(url, equalTo("www.example.com"));
}

@Test
public void shouldReturnPatientIdentifierTypeByUrl() {
PatientIdentifierType expectedIdentifierType = patientService.getPatientIdentifierType(1);
String url = systemService.getUrlByPatientIdentifierType(expectedIdentifierType);

PatientIdentifierType actualIdentifierType = dao.getPatientIdentifierTypeByUrl(url);
assertNotNull(actualIdentifierType);
assertEquals(expectedIdentifierType, actualIdentifierType);
}

@Test
public void shouldReturnNullForInvalidUrl() {
PatientIdentifierType actualIdentifierType = dao.getPatientIdentifierTypeByUrl("invalidUrl");
assertNull(actualIdentifierType);
}

@Test
public void shouldReturnNullForNullUrl() {
PatientIdentifierType actualIdentifierType = dao.getPatientIdentifierTypeByUrl(null);
assertNull(actualIdentifierType);
}

@Test
public void shouldReturnNullIfPatientIdentifierTypeNotFound() {
PatientIdentifierType patientIdentifierType = patientService.getPatientIdentifierType(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.openmrs.module.fhir2.api.impl;

import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand All @@ -19,6 +20,10 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anySet;
Expand All @@ -41,20 +46,25 @@
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
import com.github.dnault.xmlpatch.repackaged.joptsimple.internal.Strings;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.HumanName;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Observation;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifierType;
import org.openmrs.PersonAddress;
import org.openmrs.PersonName;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirGlobalPropertyService;
import org.openmrs.module.fhir2.api.dao.FhirPatientDao;
import org.openmrs.module.fhir2.api.dao.FhirPatientIdentifierSystemDao;
import org.openmrs.module.fhir2.api.search.SearchQuery;
import org.openmrs.module.fhir2.api.search.SearchQueryBundleProvider;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
Expand Down Expand Up @@ -114,6 +124,9 @@ public class FhirPatientServiceImplTest {
@Mock
private FhirPatientDao dao;

@Mock
private FhirPatientIdentifierSystemDao systemDao;

@Mock
private FhirGlobalPropertyService globalPropertyService;

Expand All @@ -139,6 +152,7 @@ protected void validateObject(Patient object) {
};

patientService.setDao(dao);
patientService.setSystemDao(systemDao);
patientService.setTranslator(patientTranslator);
patientService.setSearchQuery(searchQuery);
patientService.setSearchQueryInclude(searchQueryInclude);
Expand Down Expand Up @@ -873,6 +887,90 @@ public void getPatientEverything_shouldReturnAllInformationAboutAllPatients() {
assertThat(resultList.size(), greaterThanOrEqualTo(1));
}

@Test
public void getPatientIdentifierTypeByIdentifier_shouldReturnIdentifierTypeWhenPresent() {
String typeName = "some-type";

Identifier identifier = new Identifier();
identifier.setType(new CodeableConcept().setText(typeName));

PatientIdentifierType expectedType = new PatientIdentifierType();
expectedType.setName(typeName);

when(dao.getPatientIdentifierTypeByNameOrUuid(typeName, null)).thenReturn(expectedType);

PatientIdentifierType result = patientService.getPatientIdentifierTypeByIdentifier(identifier);
assertNull(identifier.getSystem());
assertEquals(expectedType, result);
}

@Test
public void getPatientIdentifierTypeByIdentifier_shouldReturnIdentifierTypeWithSystemWhenSystemIsPresent() {
String systemName = "some-system";
String expectedTypeName = "some-type";
String anotherTypeName = "another-type";

Identifier identifierWithSystem = new Identifier();
identifierWithSystem.setSystem(systemName);
identifierWithSystem.setType(new CodeableConcept().setText(expectedTypeName));

PatientIdentifierType expectedType = new PatientIdentifierType();
expectedType.setName(expectedTypeName);

PatientIdentifierType unexpectedType = new PatientIdentifierType();
unexpectedType.setName(anotherTypeName);

when(systemDao.getPatientIdentifierTypeByUrl(systemName)).thenReturn(expectedType);

PatientIdentifierType result = patientService.getPatientIdentifierTypeByIdentifier(identifierWithSystem);
assertNotEquals(expectedType, unexpectedType);
assertEquals(expectedType, result);
}

@Test
public void getPatientIdentifierTypeByIdentifier_shouldReturnNullWhenSystemAndIdentifierTypeNotPresent() {
Identifier identifier = new Identifier();
identifier.setType(null);

PatientIdentifierType result = patientService.getPatientIdentifierTypeByIdentifier(identifier);
assertNull(identifier.getSystem());
assertFalse(identifier.hasType());
assertNull(result);
}

@Test
public void getPatientIdentifierTypeByIdentifier_shouldReturnNullWhenIdentifierTypeIsNull() {
Identifier identifier = new Identifier();
identifier.setType(null);

PatientIdentifierType result = patientService.getPatientIdentifierTypeByIdentifier(identifier);
assertNull(identifier.getSystem());
assertTrue(identifier.getType().isEmpty());
assertNull(result);
}

@Test
public void getPatientIdentifierTypeByIdentifier_shouldReturnNullWhenIdentifierTypeTextIsNull() {
Identifier identifier = new Identifier();
identifier.setType(new CodeableConcept().setText(null));

PatientIdentifierType result = patientService.getPatientIdentifierTypeByIdentifier(identifier);
assertNull(identifier.getSystem());
assertTrue(identifier.getType().isEmpty());
assertNull(result);
}

@Test
public void getPatientIdentifierTypeByIdentifier_shouldReturnNullWhenIdentifierTypeTextEmpty() {
Identifier identifier = new Identifier();
identifier.setType(new CodeableConcept().setText(Strings.EMPTY));

PatientIdentifierType result = patientService.getPatientIdentifierTypeByIdentifier(identifier);
assertNull(identifier.getSystem());
assertTrue(identifier.getType().isEmpty());
assertNull(result);
}

private List<IBaseResource> get(IBundleProvider results) {
return results.getResources(0, 10);
}
Expand Down

0 comments on commit 5d0b0ec

Please sign in to comment.