diff --git a/api-2.2/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl_2_2.java b/api-2.2/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl_2_2.java index cb88ea7da..ebbee1de8 100644 --- a/api-2.2/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl_2_2.java +++ b/api-2.2/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl_2_2.java @@ -138,4 +138,9 @@ protected String paramToProp(@Nonnull String param) { return super.paramToProp(param); } + + @Override + protected Condition deproxyResult(Condition result) { + return super.deproxyResult(result); + } } diff --git a/api-2.2/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImpl_2_2Test.java b/api-2.2/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImpl_2_2Test.java index 32109bd18..d62f25fee 100644 --- a/api-2.2/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImpl_2_2Test.java +++ b/api-2.2/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImpl_2_2Test.java @@ -116,9 +116,9 @@ private List get(IBundleProvider results) { } @Test - public void shouldGetConditionByUuid() { + public void get_shouldGetConditionByUuid() { when(dao.get(CONDITION_UUID)).thenReturn(openmrsCondition); - when(conditionTranslator.toFhirResource(openmrsCondition)).thenReturn(fhirCondition); + when(conditionTranslator.toFhirResource(openmrsCondition, null)).thenReturn(fhirCondition); org.hl7.fhir.r4.model.Condition condition = conditionService.get(CONDITION_UUID); @@ -128,7 +128,7 @@ public void shouldGetConditionByUuid() { } @Test - public void shouldThrowExceptionWhenGetMissingUuid() { + public void get_shouldThrowExceptionForMissingUuid() { assertThrows(ResourceNotFoundException.class, () -> conditionService.get(WRONG_CONDITION_UUID)); } @@ -160,7 +160,7 @@ public void update_shouldUpdateExistingCondition() { condition.setId(CONDITION_UUID); when(dao.get(CONDITION_UUID)).thenReturn(openmrsCondition); - when(conditionTranslator.toFhirResource(openmrsCondition)).thenReturn(condition); + when(conditionTranslator.toFhirResource(openmrsCondition, null)).thenReturn(condition); when(dao.createOrUpdate(openmrsCondition)).thenReturn(openmrsCondition); when(conditionTranslator.toOpenmrsType(any(Condition.class), any(org.hl7.fhir.r4.model.Condition.class))) .thenReturn(openmrsCondition); diff --git a/api/pom.xml b/api/pom.xml index 791ea3f98..4845f7c3b 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -48,6 +48,10 @@ javax.servlet javax.servlet-api + + com.github.ben-manes.caffeine + caffeine + org.projectlombok lombok diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/FhirConceptSourceService.java b/api/src/main/java/org/openmrs/module/fhir2/api/FhirConceptSourceService.java index e8f50030f..fa08743a2 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/FhirConceptSourceService.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/FhirConceptSourceService.java @@ -10,10 +10,12 @@ package org.openmrs.module.fhir2.api; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Collection; import java.util.Optional; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.openmrs.module.fhir2.model.FhirConceptSource; public interface FhirConceptSourceService { @@ -22,5 +24,6 @@ public interface FhirConceptSourceService { Optional getFhirConceptSourceByUrl(@Nonnull String url); - Optional getFhirConceptSourceByConceptSourceName(@Nonnull String sourceName); + Optional getFhirConceptSourceByConceptSourceName(@Nonnull String sourceName, + @Nullable FhirCache cache); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java index be594e97f..3aff55261 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirConditionDaoImpl.java @@ -23,7 +23,6 @@ import lombok.AccessLevel; import lombok.Setter; import org.hibernate.Criteria; -import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.openmrs.Obs; import org.openmrs.annotation.Authorized; @@ -32,8 +31,6 @@ import org.openmrs.module.fhir2.api.dao.FhirConditionDao; import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; import org.openmrs.util.PrivilegeConstants; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component @@ -41,10 +38,6 @@ @OpenmrsProfile(openmrsPlatformVersion = "2.0.5 - 2.1.*") public class FhirConditionDaoImpl extends BaseFhirDao implements FhirConditionDao { - @Qualifier("sessionFactory") - @Autowired - private SessionFactory sessionFactory; - @Override @Authorized(PrivilegeConstants.GET_OBS) public Obs get(@Nonnull String uuid) { @@ -131,4 +124,12 @@ protected String paramToProp(@Nonnull String param) { return super.paramToProp(param); } + + @Override + protected Obs deproxyResult(Obs result) { + Obs obs = super.deproxyResult(result); + obs.setConcept(deproxyObject(obs.getConcept())); + obs.setPerson(deproxyObject(obs.getPerson())); + return obs; + } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/impl/BaseFhirService.java b/api/src/main/java/org/openmrs/module/fhir2/api/impl/BaseFhirService.java index 977dbbc79..32135762d 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/impl/BaseFhirService.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/impl/BaseFhirService.java @@ -20,6 +20,8 @@ import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import com.google.common.reflect.TypeToken; +import lombok.AccessLevel; +import lombok.Getter; import org.hl7.fhir.instance.model.api.IAnyResource; import org.openmrs.Auditable; import org.openmrs.OpenmrsObject; @@ -30,14 +32,20 @@ import org.openmrs.module.fhir2.api.dao.FhirDao; import org.openmrs.module.fhir2.api.translators.OpenmrsFhirTranslator; import org.openmrs.module.fhir2.api.translators.UpdatableOpenmrsTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.openmrs.module.fhir2.api.util.FhirUtils; import org.openmrs.validator.ValidateUtil; +import org.springframework.beans.factory.annotation.Autowired; @SuppressWarnings("UnstableApiUsage") public abstract class BaseFhirService implements FhirService { protected final Class resourceClass; + @Autowired + @Getter(AccessLevel.PROTECTED) + private FhirCache fhirCache; + protected BaseFhirService() { // @formatter:off TypeToken resourceTypeToken = new TypeToken(getClass()) {}; @@ -61,13 +69,13 @@ public T get(@Nonnull String uuid) { "Resource of type " + resourceClass.getSimpleName() + " with ID " + uuid + " is gone/deleted"); } - return getTranslator().toFhirResource(openmrsObj); + return getTranslator().toFhirResource(openmrsObj, fhirCache); } @Override public List get(@Nonnull Collection uuids) { OpenmrsFhirTranslator translator = getTranslator(); - return getDao().get(uuids).stream().map(translator::toFhirResource).collect(Collectors.toList()); + return getDao().get(uuids).stream().map(r -> translator.toFhirResource(r, fhirCache)).collect(Collectors.toList()); } @Override @@ -125,7 +133,9 @@ public T update(@Nonnull String uuid, @Nonnull T updatedResource) { validateObject(updatedObject); - return translator.toFhirResource(getDao().createOrUpdate(updatedObject)); + translator.invalidate(updatedObject, fhirCache); + + return translator.toFhirResource(getDao().createOrUpdate(updatedObject), fhirCache); } @Override diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java index 3d35bf631..1e453dc9a 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImpl.java @@ -10,6 +10,7 @@ package org.openmrs.module.fhir2.api.impl; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Collection; import java.util.Optional; @@ -18,6 +19,7 @@ import lombok.Setter; import org.openmrs.module.fhir2.api.FhirConceptSourceService; import org.openmrs.module.fhir2.api.dao.FhirConceptSourceDao; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.openmrs.module.fhir2.model.FhirConceptSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -45,7 +47,18 @@ public Optional getFhirConceptSourceByUrl(@Nonnull String url @Override @Transactional(readOnly = true) - public Optional getFhirConceptSourceByConceptSourceName(@Nonnull String sourceName) { + public Optional getFhirConceptSourceByConceptSourceName(@Nonnull String sourceName, + @Nullable FhirCache cache) { + if (sourceName == null) { + return Optional.empty(); + } + + if (cache != null) { + String cacheKey = "fhir-concept-source-" + sourceName; + return (Optional) cache.get(cacheKey, + k -> dao.getFhirConceptSourceByConceptSourceName(sourceName)); + } + return dao.getFhirConceptSourceByConceptSourceName(sourceName); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryBundleProvider.java b/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryBundleProvider.java index 691b5cf70..b577a5187 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryBundleProvider.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/search/SearchQueryBundleProvider.java @@ -106,7 +106,7 @@ public List getResources(int fromIndex, int toIndex) { List returnedResourceList = dao .getSearchResults(searchParameterMap, matchingResourceUuids.subList(firstResult, lastResult)).stream() - .map(translator::toFhirResource).filter(Objects::nonNull).collect(Collectors.toList()); + .map(obj -> translator.toFhirResource(obj)).filter(Objects::nonNull).collect(Collectors.toList()); Set includedResources = searchQueryInclude.getIncludedResources(returnedResourceList, this.searchParameterMap); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/OrderIdentifierTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/OrderIdentifierTranslator.java index efb0fa288..89cef3aed 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/OrderIdentifierTranslator.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/OrderIdentifierTranslator.java @@ -9,14 +9,9 @@ */ package org.openmrs.module.fhir2.api.translators; -import javax.annotation.Nonnull; - import org.hl7.fhir.r4.model.Identifier; import org.openmrs.Order; -public interface OrderIdentifierTranslator extends OpenmrsFhirTranslator { - - @Override - public Identifier toFhirResource(@Nonnull Order order); +public interface OrderIdentifierTranslator extends ToFhirTranslator { } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/ProvenanceTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/ProvenanceTranslator.java index dc40289bc..27dab42fd 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/ProvenanceTranslator.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/ProvenanceTranslator.java @@ -9,10 +9,16 @@ */ package org.openmrs.module.fhir2.api.translators; +import javax.annotation.Nonnull; + +import java.util.Locale; + import org.hl7.fhir.r4.model.Provenance; +import org.openmrs.OpenmrsObject; +import org.openmrs.module.fhir2.api.util.FhirCache; /** - * Generic interface for a translator between OpenMRS data and FHIR resources + * Generic interface for a translator between OpenMRS data and FHIR provenance resources * * @param OpenMRS data type */ @@ -27,6 +33,26 @@ public interface ProvenanceTranslator { */ Provenance getCreateProvenance(T openMrsObject); + default Provenance getCreateProvenance(T openMrsObject, FhirCache cache) { + if (cache != null) { + String cacheKey = getCacheKey(openMrsObject, "create-provenance-"); + if (cacheKey != null) { + try { + @SuppressWarnings("unchecked") + Provenance cached = (Provenance) cache.get(((OpenmrsObject) openMrsObject).getUuid(), + (k) -> getCreateProvenance(openMrsObject)); + return cached; + } + catch (ClassCastException e) { + // we shouldn't really get here, but... + return getCreateProvenance(openMrsObject); + } + } + } + + return getCreateProvenance(openMrsObject); + } + /** * Maps an OpenMRS Object to a {@link org.hl7.fhir.r4.model.Provenance} resource * @@ -34,4 +60,33 @@ public interface ProvenanceTranslator { * @return the corresponding {@link org.hl7.fhir.r4.model.Provenance} resource */ Provenance getUpdateProvenance(T openMrsObject); + + default Provenance getUpdateProvenance(T openMrsObject, FhirCache cache) { + if (cache != null) { + String cacheKey = getCacheKey(openMrsObject, "update-provenance-"); + if (cacheKey != null) { + try { + @SuppressWarnings("unchecked") + Provenance cached = (Provenance) cache.get(((OpenmrsObject) openMrsObject).getUuid(), + (k) -> getUpdateProvenance(openMrsObject)); + return cached; + } + catch (ClassCastException e) { + // we shouldn't really get here, but... + return getUpdateProvenance(openMrsObject); + } + } + } + + return getUpdateProvenance(openMrsObject); + } + + default String getCacheKey(@Nonnull T data, String prefix) { + if (data instanceof OpenmrsObject) { + return prefix + data.getClass().getSimpleName().toLowerCase(Locale.ROOT) + "-" + + ((OpenmrsObject) data).getUuid(); + } + + return null; + } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/ToFhirTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/ToFhirTranslator.java index 298632107..451144b25 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/ToFhirTranslator.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/ToFhirTranslator.java @@ -10,6 +10,12 @@ package org.openmrs.module.fhir2.api.translators; import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import java.util.Locale; + +import org.openmrs.OpenmrsObject; +import org.openmrs.module.fhir2.api.util.FhirCache; /** * Generic interface for a translator between OpenMRS data and FHIR resources @@ -21,9 +27,52 @@ public interface ToFhirTranslator extends FhirTranslator { /** * Maps an OpenMRS data element to a FHIR resource - * + * * @param data the OpenMRS data element to translate * @return the corresponding FHIR resource */ U toFhirResource(@Nonnull T data); + + /** + * Maps an OpenMRS data element to a FHIR resource + * + * @param data the OpenMRS data element to translate + * @param cache contextual cache + * @return the corresponding FHIR resource + */ + default U toFhirResource(@Nonnull T data, @Nullable FhirCache cache) { + if (cache != null) { + String cacheKey = getCacheKey(data); + if (cacheKey != null) { + try { + @SuppressWarnings("unchecked") + U cached = (U) cache.get(cacheKey, k -> toFhirResource(data)); + return cached; + } + catch (ClassCastException e) { + // we shouldn't really get here, but... + return toFhirResource(data); + } + } + } + + return toFhirResource(data); + } + + default void invalidate(@Nonnull T data, FhirCache fhirCache) { + fhirCache.invalidate(getCacheKey(data)); + } + + default String getCacheKey(@Nonnull T data) { + if (data == null) { + return null; + } + + if (data instanceof OpenmrsObject) { + return this.getClass().getSimpleName().toLowerCase(Locale.ROOT) + "-" + + data.getClass().getSimpleName().toLowerCase(Locale.ROOT) + "-" + ((OpenmrsObject) data).getUuid(); + } + + return null; + } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java index 3004abc8d..5d29a1526 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImpl.java @@ -9,9 +9,8 @@ */ package org.openmrs.module.fhir2.api.translators.impl; -import static org.apache.commons.lang3.Validate.notNull; - import javax.annotation.Nonnull; +import javax.annotation.Nullable; import lombok.AccessLevel; import lombok.Setter; @@ -33,12 +32,13 @@ import org.openmrs.module.fhir2.api.translators.PatientReferenceTranslator; import org.openmrs.module.fhir2.api.translators.PractitionerReferenceTranslator; import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component @Setter(AccessLevel.PACKAGE) -public class AllergyIntoleranceTranslatorImpl extends BaseReferenceHandlingTranslator implements AllergyIntoleranceTranslator { +public class AllergyIntoleranceTranslatorImpl implements AllergyIntoleranceTranslator { @Autowired private PractitionerReferenceTranslator practitionerReferenceTranslator; @@ -66,42 +66,70 @@ public class AllergyIntoleranceTranslatorImpl extends BaseReferenceHandlingTrans @Override public AllergyIntolerance toFhirResource(@Nonnull Allergy omrsAllergy) { - notNull(omrsAllergy, "The Allergy object should not be null"); + if (omrsAllergy == null) { + return null; + } + + return toFhirResourceInternal(omrsAllergy, null); + } + + @Override + public AllergyIntolerance toFhirResource(@Nonnull Allergy omrsAllergy, @Nullable FhirCache cache) { + if (omrsAllergy == null) { + return null; + } + if (cache != null) { + return (AllergyIntolerance) cache.get(getCacheKey(omrsAllergy), k -> toFhirResourceInternal(omrsAllergy, cache)); + } + + return toFhirResourceInternal(omrsAllergy, null); + } + + protected AllergyIntolerance toFhirResourceInternal(@Nonnull Allergy omrsAllergy, @Nullable FhirCache cache) { AllergyIntolerance allergy = new AllergyIntolerance(); allergy.setId(omrsAllergy.getUuid()); if (omrsAllergy.getAllergen() != null) { - allergy.addCategory(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType())); + allergy.addCategory(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType(), cache)); } + allergy.setClinicalStatus(setClinicalStatus(omrsAllergy.getVoided())); allergy.setVerificationStatus(new CodeableConcept().setText("Confirmed").addCoding( new Coding(FhirConstants.ALLERGY_INTOLERANCE_VERIFICATION_STATUS_SYSTEM_URI, "confirmed", "Confirmed"))); - allergy.setPatient(patientReferenceTranslator.toFhirResource(omrsAllergy.getPatient())); - allergy.setRecorder(practitionerReferenceTranslator.toFhirResource(omrsAllergy.getCreator())); + allergy.setPatient(patientReferenceTranslator.toFhirResource(omrsAllergy.getPatient(), cache)); + allergy.setRecorder(practitionerReferenceTranslator.toFhirResource(omrsAllergy.getCreator(), cache)); allergy.setRecordedDate(omrsAllergy.getDateCreated()); allergy.getMeta().setLastUpdated(omrsAllergy.getDateChanged()); allergy.setType(AllergyIntolerance.AllergyIntoleranceType.ALLERGY); allergy.addNote(new Annotation().setText(omrsAllergy.getComment())); allergy.setCriticality( - criticalityTranslator.toFhirResource(severityTranslator.toFhirResource(omrsAllergy.getSeverity()))); - allergy.addReaction(reactionComponentTranslator.toFhirResource(omrsAllergy)); + criticalityTranslator.toFhirResource(severityTranslator.toFhirResource(omrsAllergy.getSeverity(), cache))); + allergy.addReaction(reactionComponentTranslator.toFhirResource(omrsAllergy, cache)); allergy.setCode(allergy.getReactionFirstRep().getSubstance()); - allergy.addContained(provenanceTranslator.getCreateProvenance(omrsAllergy)); - allergy.addContained(provenanceTranslator.getUpdateProvenance(omrsAllergy)); + allergy.addContained(provenanceTranslator.getCreateProvenance(omrsAllergy, cache)); + allergy.addContained(provenanceTranslator.getUpdateProvenance(omrsAllergy, cache)); return allergy; } @Override public Allergy toOpenmrsType(@Nonnull AllergyIntolerance fhirAllergy) { - notNull(fhirAllergy, "The AllergyIntolerance object should not be null"); + if (fhirAllergy == null) { + return null; + } + return toOpenmrsType(new Allergy(), fhirAllergy); } @Override public Allergy toOpenmrsType(@Nonnull Allergy allergy, @Nonnull AllergyIntolerance fhirAllergy) { - notNull(allergy, "The existing Allergy should not be null"); - notNull(fhirAllergy, "The AllergyIntolerance object should not be null"); + if (allergy == null) { + return null; + } + + if (fhirAllergy == null) { + return null; + } if (fhirAllergy.getId() != null) { allergy.setUuid(fhirAllergy.getId()); @@ -115,10 +143,12 @@ public Allergy toOpenmrsType(@Nonnull Allergy allergy, @Nonnull AllergyIntoleran allergy.setAllergen(allergen); } } + if (fhirAllergy.hasCategory() && allergy.getAllergen() != null) { allergy.getAllergen() .setAllergenType(categoryTranslator.toOpenmrsType(fhirAllergy.getCategory().get(0).getValue())); } + allergy.setVoided(isAllergyInactive(fhirAllergy.getClinicalStatus())); allergy.setPatient(patientReferenceTranslator.toOpenmrsType(fhirAllergy.getPatient())); allergy.setCreator(practitionerReferenceTranslator.toOpenmrsType(fhirAllergy.getRecorder())); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslator.java index 0902f4169..e6e687362 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslator.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslator.java @@ -9,12 +9,16 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import lombok.AccessLevel; import lombok.Setter; import org.hl7.fhir.r4.model.Coding; import org.openmrs.Location; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.mappings.EncounterClassMap; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; @Setter(AccessLevel.PACKAGE) @@ -23,7 +27,16 @@ public abstract class BaseEncounterTranslator { @Autowired private EncounterClassMap encounterClassMap; - protected Coding mapLocationToClass(Location location) { + protected Coding mapLocationToClass(@Nonnull Location location, @Nullable FhirCache cache) { + if (location != null && cache != null) { + return (Coding) cache.get("encounter-class-location-" + location.getUuid(), + k -> mapLocationToClassInternal(location)); + } + + return mapLocationToClassInternal(location); + } + + private Coding mapLocationToClassInternal(@Nonnull Location location) { Coding coding = new Coding(); coding.setSystem(FhirConstants.ENCOUNTER_CLASS_VALUE_SET_URI); // The default code for anything that cannot be matched with FHIR codes. @@ -31,6 +44,7 @@ protected Coding mapLocationToClass(Location location) { if (location == null) { return coding; } + String classCode = encounterClassMap.getFhirClass(location.getUuid()); if (classCode != null) { coding.setCode(classCode); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BasePractitionerTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BasePractitionerTranslator.java new file mode 100644 index 000000000..253b1562d --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BasePractitionerTranslator.java @@ -0,0 +1,86 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.api.translators.impl; + +import lombok.AccessLevel; +import lombok.Setter; +import org.hl7.fhir.r4.model.Address; +import org.hl7.fhir.r4.model.HumanName; +import org.hl7.fhir.r4.model.Practitioner; +import org.openmrs.Person; +import org.openmrs.PersonAddress; +import org.openmrs.PersonName; +import org.openmrs.module.fhir2.api.translators.BirthDateTranslator; +import org.openmrs.module.fhir2.api.translators.GenderTranslator; +import org.openmrs.module.fhir2.api.translators.PersonAddressTranslator; +import org.openmrs.module.fhir2.api.translators.PersonNameTranslator; +import org.springframework.beans.factory.annotation.Autowired; + +@Setter(AccessLevel.PACKAGE) +public class BasePractitionerTranslator { + + @Autowired + private PersonNameTranslator nameTranslator; + + @Autowired + private GenderTranslator genderTranslator; + + @Autowired + private BirthDateTranslator birthDateTranslator; + + @Autowired + private PersonAddressTranslator addressTranslator; + + protected void personToPractitioner(Person person, Practitioner practitioner) { + if (person == null) { + return; + } + + if (practitioner == null) { + return; + } + + practitioner.setBirthDateElement(birthDateTranslator.toFhirResource(person)); + + practitioner.setGender(genderTranslator.toFhirResource(person.getGender())); + for (PersonName name : person.getNames()) { + practitioner.addName(nameTranslator.toFhirResource(name)); + } + + for (PersonAddress address : person.getAddresses()) { + practitioner.addAddress(addressTranslator.toFhirResource(address)); + } + } + + protected void practitionerToPerson(Practitioner practitioner, Person person) { + if (practitioner == null) { + return; + } + + if (person == null) { + return; + } + + for (HumanName name : practitioner.getName()) { + person.addName(nameTranslator.toOpenmrsType(name)); + } + + for (Address address : practitioner.getAddress()) { + person.addAddress(addressTranslator.toOpenmrsType(address)); + } + + if (practitioner.hasBirthDateElement()) { + birthDateTranslator.toOpenmrsType(person, practitioner.getBirthDateElement()); + } + + person.setGender(genderTranslator.toOpenmrsType(practitioner.getGender())); + } + +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseProvenanceHandlingTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseProvenanceHandlingTranslator.java index a5b94fe9e..919c11248 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseProvenanceHandlingTranslator.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseProvenanceHandlingTranslator.java @@ -20,11 +20,12 @@ import org.openmrs.User; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.translators.PractitionerReferenceTranslator; +import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; import org.openmrs.module.fhir2.api.util.FhirUtils; import org.springframework.beans.factory.annotation.Autowired; @Setter(AccessLevel.PACKAGE) -public abstract class BaseProvenanceHandlingTranslator { +public abstract class BaseProvenanceHandlingTranslator implements ProvenanceTranslator { private static final String AGENT_TYPE_CODE = "author"; @@ -39,10 +40,12 @@ public abstract class BaseProvenanceHandlingTranslator serviceRequestTasks = results.getResources(START_INDEX, END_INDEX).stream().map(p -> (Task) p) - .collect(Collectors.toList()); - - ServiceRequest.ServiceRequestStatus serviceRequestStatus = ServiceRequest.ServiceRequestStatus.UNKNOWN; - - if (serviceRequestTasks.size() != 1) { - return serviceRequestStatus; - } - - Task serviceRequestTask = serviceRequestTasks.iterator().next(); - - if (serviceRequestTask.hasStatus()) { - switch (serviceRequestTask.getStatus()) { - case ACCEPTED: - case REQUESTED: - serviceRequestStatus = ServiceRequest.ServiceRequestStatus.ACTIVE; - break; - case REJECTED: - serviceRequestStatus = ServiceRequest.ServiceRequestStatus.REVOKED; - break; - case COMPLETED: - serviceRequestStatus = ServiceRequest.ServiceRequestStatus.COMPLETED; - break; - } - } - return serviceRequestStatus; - } - protected Reference determineServiceRequestPerformer(String orderUuid) { IBundleProvider results = taskService.searchForTasks( new ReferenceAndListParam() diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImpl.java index f0d224b83..41b822a98 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImpl.java @@ -10,6 +10,7 @@ package org.openmrs.module.fhir2.api.translators.impl; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import lombok.AccessLevel; import lombok.Setter; @@ -23,6 +24,7 @@ import org.openmrs.module.fhir2.api.FhirConceptService; import org.openmrs.module.fhir2.api.FhirConceptSourceService; import org.openmrs.module.fhir2.api.translators.ConceptTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.openmrs.module.fhir2.model.FhirConceptSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -44,13 +46,34 @@ public CodeableConcept toFhirResource(@Nonnull Concept concept) { return null; } + return toFhirResourceInternal(concept, null); + } + + @Override + public CodeableConcept toFhirResource(@Nonnull Concept concept, @Nullable FhirCache cache) { + if (concept == null) { + return null; + } + + if (cache != null) { + return (CodeableConcept) cache.get(getCacheKey(concept), k -> toFhirResourceInternal(concept, cache)); + } + + return toFhirResourceInternal(concept, null); + } + + protected CodeableConcept toFhirResourceInternal(@Nonnull Concept concept, @Nullable FhirCache cache) { + if (concept == null) { + return null; + } + CodeableConcept codeableConcept = new CodeableConcept(); addConceptCoding(codeableConcept.addCoding(), null, concept.getUuid(), concept); for (ConceptMap mapping : concept.getConceptMappings()) { ConceptReferenceTerm crt = mapping.getConceptReferenceTerm(); - String sourceUrl = conceptSourceToURL(crt.getConceptSource().getName()); + String sourceUrl = conceptSourceToURL(crt.getConceptSource().getName(), cache); if (sourceUrl == null) { continue; } @@ -102,9 +125,9 @@ private void addConceptCoding(Coding coding, String system, String code, Concept coding.setDisplay(display); } - private String conceptSourceToURL(String conceptSourceName) { - return conceptSourceService.getFhirConceptSourceByConceptSourceName(conceptSourceName).map(FhirConceptSource::getUrl) - .orElse(null); + private String conceptSourceToURL(String conceptSourceName, FhirCache cache) { + return conceptSourceService.getFhirConceptSourceByConceptSourceName(conceptSourceName, cache) + .map(FhirConceptSource::getUrl).orElse(null); } private String conceptURLToSource(String url) { diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java index e27a0c889..f2c354cf1 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java @@ -12,14 +12,15 @@ import static org.apache.commons.lang3.Validate.notNull; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Date; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import lombok.AccessLevel; import lombok.Setter; -import org.hibernate.proxy.HibernateProxy; import org.hl7.fhir.r4.model.CodeableConcept; +import org.hl7.fhir.r4.model.Condition; import org.hl7.fhir.r4.model.DateTimeType; import org.openmrs.Concept; import org.openmrs.Obs; @@ -28,13 +29,13 @@ import org.openmrs.User; import org.openmrs.annotation.OpenmrsProfile; import org.openmrs.api.ConceptService; -import org.openmrs.api.db.hibernate.HibernateUtil; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.translators.ConceptTranslator; import org.openmrs.module.fhir2.api.translators.ConditionTranslator; import org.openmrs.module.fhir2.api.translators.PatientReferenceTranslator; import org.openmrs.module.fhir2.api.translators.PractitionerReferenceTranslator; import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -61,27 +62,32 @@ public class ConditionTranslatorImpl implements ConditionTranslator { @Override public org.hl7.fhir.r4.model.Condition toFhirResource(@Nonnull Obs obsCondition) { notNull(obsCondition, "The Openmrs Condition object should not be null"); - + return toFhirResourceInternal(obsCondition, null); + } + + @Override + public Condition toFhirResource(@Nonnull Obs obsCondition, @Nullable FhirCache cache) { + notNull(obsCondition, "The Openmrs Condition object should not be null"); + return toFhirResourceInternal(obsCondition, cache); + } + + protected Condition toFhirResourceInternal(@Nonnull Obs obsCondition, @Nullable FhirCache cache) { org.hl7.fhir.r4.model.Condition fhirCondition = new org.hl7.fhir.r4.model.Condition(); fhirCondition.setId(obsCondition.getUuid()); Person obsPerson = obsCondition.getPerson(); if (obsPerson != null) { - if (obsPerson instanceof HibernateProxy) { - obsPerson = HibernateUtil.getRealObjectFromProxy(obsPerson); - } - if (obsPerson instanceof Patient) { - fhirCondition.setSubject(patientReferenceTranslator.toFhirResource((Patient) obsPerson)); + fhirCondition.setSubject(patientReferenceTranslator.toFhirResource((Patient) obsPerson, cache)); } } if (obsCondition.getValueCoded() != null) { - fhirCondition.setCode(conceptTranslator.toFhirResource(obsCondition.getValueCoded())); + fhirCondition.setCode(conceptTranslator.toFhirResource(obsCondition.getValueCoded(), cache)); } fhirCondition.setOnset(new DateTimeType().setValue(obsCondition.getObsDatetime())); - fhirCondition.setRecorder(practitionerReferenceTranslator.toFhirResource(obsCondition.getCreator())); + fhirCondition.setRecorder(practitionerReferenceTranslator.toFhirResource(obsCondition.getCreator(), cache)); fhirCondition.setRecordedDate(obsCondition.getDateCreated()); fhirCondition.getMeta().setLastUpdated(obsCondition.getDateChanged()); fhirCondition.addContained(provenanceTranslator.getCreateProvenance(obsCondition)); @@ -100,17 +106,20 @@ public Obs toOpenmrsType(@Nonnull org.hl7.fhir.r4.model.Condition condition) { public Obs toOpenmrsType(@Nonnull Obs existingObsCondition, @Nonnull org.hl7.fhir.r4.model.Condition condition) { notNull(existingObsCondition, "The existing Openmrs Obs Condition object should not be null"); notNull(condition, "The Condition object should not be null"); + existingObsCondition.setUuid(condition.getIdElement().getIdPart()); CodeableConcept codeableConcept = condition.getCode(); existingObsCondition.setValueCoded(conceptTranslator.toOpenmrsType(codeableConcept)); existingObsCondition.setPerson(patientReferenceTranslator.toOpenmrsType(condition.getSubject())); Concept problemList = conceptService.getConceptByUuid(FhirConstants.CONDITION_OBSERVATION_CONCEPT_UUID); + if (problemList != null) { existingObsCondition.setConcept(problemList); } else { throw new InternalErrorException( "Concept " + FhirConstants.CONDITION_OBSERVATION_CONCEPT_UUID + " ProblemList Not found"); } + Date onsetTime = condition.getOnsetDateTimeType().getValue(); Date recordTime = condition.getRecordedDateElement().getValue(); if (onsetTime != null) { diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImpl.java index 73858e3fc..4aa43c71c 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImpl.java @@ -10,8 +10,10 @@ package org.openmrs.module.fhir2.api.translators.impl; import static org.apache.commons.lang3.Validate.notNull; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Date; import java.util.stream.Collectors; @@ -28,7 +30,7 @@ import org.openmrs.module.fhir2.api.translators.EncounterReferenceTranslator; import org.openmrs.module.fhir2.api.translators.ObservationReferenceTranslator; import org.openmrs.module.fhir2.api.translators.PatientReferenceTranslator; -import org.openmrs.module.fhir2.api.util.FhirUtils; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.openmrs.module.fhir2.model.FhirDiagnosticReport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -52,7 +54,17 @@ public class DiagnosticReportTranslatorImpl implements DiagnosticReportTranslato @Override public DiagnosticReport toFhirResource(@Nonnull FhirDiagnosticReport fhirDiagnosticReport) { notNull(fhirDiagnosticReport, "The diagnostic report should not be null"); - + return toFhirResourceInternal(fhirDiagnosticReport, null); + } + + @Override + public DiagnosticReport toFhirResource(@Nonnull FhirDiagnosticReport fhirDiagnosticReport, @Nullable FhirCache cache) { + notNull(fhirDiagnosticReport, "The diagnostic report should not be null"); + return toFhirResourceInternal(fhirDiagnosticReport, cache); + } + + protected DiagnosticReport toFhirResourceInternal(@Nonnull FhirDiagnosticReport fhirDiagnosticReport, + @Nullable FhirCache cache) { DiagnosticReport diagnosticReport = new DiagnosticReport(); diagnosticReport.setId(fhirDiagnosticReport.getUuid()); @@ -71,16 +83,17 @@ public DiagnosticReport toFhirResource(@Nonnull FhirDiagnosticReport fhirDiagnos } if (fhirDiagnosticReport.getEncounter() != null) { - diagnosticReport.setEncounter(encounterReferenceTranslator.toFhirResource(fhirDiagnosticReport.getEncounter())); + diagnosticReport + .setEncounter(encounterReferenceTranslator.toFhirResource(fhirDiagnosticReport.getEncounter(), cache)); } if (fhirDiagnosticReport.getSubject() != null) { - diagnosticReport.setSubject(patientReferenceTranslator.toFhirResource(fhirDiagnosticReport.getSubject())); + diagnosticReport.setSubject(patientReferenceTranslator.toFhirResource(fhirDiagnosticReport.getSubject(), cache)); } Concept code = fhirDiagnosticReport.getCode(); if (code != null) { - diagnosticReport.setCode(conceptTranslator.toFhirResource(code)); + diagnosticReport.setCode(conceptTranslator.toFhirResource(code, cache)); } diagnosticReport.addCategory().addCoding().setSystem(FhirConstants.DIAGNOSTIC_REPORT_SERVICE_SYSTEM_URI) @@ -89,7 +102,7 @@ public DiagnosticReport toFhirResource(@Nonnull FhirDiagnosticReport fhirDiagnos diagnosticReport.setIssued(fhirDiagnosticReport.getIssued()); for (Obs obs : fhirDiagnosticReport.getResults()) { - diagnosticReport.addResult(observationReferenceTranslator.toFhirResource(obs)); + diagnosticReport.addResult(observationReferenceTranslator.toFhirResource(obs, cache)); } return diagnosticReport; @@ -127,7 +140,7 @@ public FhirDiagnosticReport toOpenmrsType(@Nonnull FhirDiagnosticReport existing } if (diagnosticReport.hasSubject()) { - FhirUtils.getReferenceType(diagnosticReport.getSubject()).ifPresent(t -> { + getReferenceType(diagnosticReport.getSubject()).ifPresent(t -> { if (FhirConstants.PATIENT.equals(t)) { existingDiagnosticReport .setSubject(patientReferenceTranslator.toOpenmrsType(diagnosticReport.getSubject())); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImpl.java index 9b92cc80b..b6ff06b22 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImpl.java @@ -10,6 +10,7 @@ package org.openmrs.module.fhir2.api.translators.impl; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import lombok.AccessLevel; import lombok.Setter; @@ -19,6 +20,7 @@ import org.openmrs.module.fhir2.api.translators.ConceptTranslator; import org.openmrs.module.fhir2.api.translators.DosageTranslator; import org.openmrs.module.fhir2.api.translators.MedicationRequestTimingTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -34,14 +36,24 @@ public class DosageTranslatorImpl implements DosageTranslator { @Override public Dosage toFhirResource(@Nonnull DrugOrder drugOrder) { + return toFhirResourceInternal(drugOrder, null); + } + + @Override + public Dosage toFhirResource(@Nonnull DrugOrder drugOrder, @Nullable FhirCache cache) { + return toFhirResourceInternal(drugOrder, cache); + } + + protected Dosage toFhirResourceInternal(@Nonnull DrugOrder drugOrder, @Nullable FhirCache cache) { if (drugOrder == null) { return null; } + Dosage dosage = new Dosage(); dosage.setText(drugOrder.getDosingInstructions()); dosage.setAsNeeded(new BooleanType(drugOrder.getAsNeeded())); - dosage.setRoute(conceptTranslator.toFhirResource(drugOrder.getRoute())); - dosage.setTiming(timingTranslator.toFhirResource(drugOrder)); + dosage.setRoute(conceptTranslator.toFhirResource(drugOrder.getRoute(), cache)); + dosage.setTiming(timingTranslator.toFhirResource(drugOrder, cache)); return dosage; } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DurationUnitTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DurationUnitTranslatorImpl.java index 39ac9e5cb..1c4539c3c 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DurationUnitTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/DurationUnitTranslatorImpl.java @@ -32,11 +32,9 @@ public class DurationUnitTranslatorImpl implements DurationUnitTranslator { @Override public Timing.UnitsOfTime toFhirResource(@Nonnull Concept concept) { - - if (concept.getUuid() == null) { + if (concept == null || concept.getUuid() == null) { return null; } - unitsOfTime = durationUnitMap.getDurationUnit(concept.getUuid()); if (unitsOfTime == null) { diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterLocationTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterLocationTranslatorImpl.java index bec37bee6..2eecdc861 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterLocationTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterLocationTranslatorImpl.java @@ -10,6 +10,8 @@ package org.openmrs.module.fhir2.api.translators.impl; import static org.apache.commons.lang3.Validate.notNull; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createLocationReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import javax.annotation.Nonnull; @@ -24,7 +26,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class EncounterLocationTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterLocationTranslator { +public class EncounterLocationTranslatorImpl implements EncounterLocationTranslator { @Autowired private FhirLocationDao locationDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterParticipantTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterParticipantTranslatorImpl.java index f1052457b..f6aa7294b 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterParticipantTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterParticipantTranslatorImpl.java @@ -10,6 +10,8 @@ package org.openmrs.module.fhir2.api.translators.impl; import static org.apache.commons.lang3.Validate.notNull; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createPractitionerReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import javax.annotation.Nonnull; @@ -24,7 +26,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class EncounterParticipantTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterParticipantTranslator { +public class EncounterParticipantTranslatorImpl implements EncounterParticipantTranslator { @Autowired private FhirPractitionerDao practitionerDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImpl.java index 4b9579858..74d202ddb 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createEncounterReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class EncounterReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterReferenceTranslator { +public class EncounterReferenceTranslatorImpl implements EncounterReferenceTranslator { @Autowired private FhirEncounterDao encounterDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImpl.java index 622929cb2..2b7991d66 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImpl.java @@ -9,9 +9,8 @@ */ package org.openmrs.module.fhir2.api.translators.impl; -import static org.apache.commons.lang3.Validate.notNull; - import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Collections; import java.util.LinkedHashSet; @@ -32,6 +31,7 @@ import org.openmrs.module.fhir2.api.translators.EncounterTypeTranslator; import org.openmrs.module.fhir2.api.translators.PatientReferenceTranslator; import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -61,48 +61,75 @@ public class EncounterTranslatorImpl extends BaseEncounterTranslator implements private EncounterPeriodTranslator encounterPeriodTranslator; @Override - public Encounter toFhirResource(@Nonnull org.openmrs.Encounter openMrsEncounter) { - notNull(openMrsEncounter, "The Openmrs Encounter object should not be null"); + public Encounter toFhirResource(@Nonnull org.openmrs.Encounter omrsEncounter) { + if (omrsEncounter == null) { + return null; + } + + return toFhirResourceInternal(omrsEncounter, null); + } + + @Override + public Encounter toFhirResource(@Nonnull org.openmrs.Encounter omrsEncounter, @Nullable FhirCache cache) { + if (omrsEncounter == null) { + return null; + } + + if (cache != null) { + return (Encounter) cache.get(getCacheKey(omrsEncounter), k -> toFhirResourceInternal(omrsEncounter, cache)); + } + return toFhirResourceInternal(omrsEncounter, null); + } + + protected Encounter toFhirResourceInternal(@Nonnull org.openmrs.Encounter omrsEncounter, @Nullable FhirCache cache) { Encounter encounter = new Encounter(); - encounter.setId(openMrsEncounter.getUuid()); + encounter.setId(omrsEncounter.getUuid()); encounter.setStatus(Encounter.EncounterStatus.UNKNOWN); - encounter.setType(encounterTypeTranslator.toFhirResource(openMrsEncounter.getEncounterType())); + encounter.setType(encounterTypeTranslator.toFhirResource(omrsEncounter.getEncounterType(), cache)); - encounter.setSubject(patientReferenceTranslator.toFhirResource(openMrsEncounter.getPatient())); - encounter.setParticipant(openMrsEncounter.getEncounterProviders().stream().map(participantTranslator::toFhirResource) - .collect(Collectors.toList())); + encounter.setSubject(patientReferenceTranslator.toFhirResource(omrsEncounter.getPatient(), cache)); + encounter.setParticipant(omrsEncounter.getEncounterProviders().stream() + .map(p -> participantTranslator.toFhirResource(p, cache)).collect(Collectors.toList())); // add visit as part of encounter - encounter.setPartOf(visitReferenceTranlator.toFhirResource(openMrsEncounter.getVisit())); + encounter.setPartOf(visitReferenceTranlator.toFhirResource(omrsEncounter.getVisit(), cache)); - if (openMrsEncounter.getLocation() != null) { + if (omrsEncounter.getLocation() != null) { encounter.setLocation( - Collections.singletonList(encounterLocationTranslator.toFhirResource(openMrsEncounter.getLocation()))); + Collections.singletonList(encounterLocationTranslator.toFhirResource(omrsEncounter.getLocation(), cache))); } - encounter.setPeriod(encounterPeriodTranslator.toFhirResource(openMrsEncounter)); + encounter.setPeriod(encounterPeriodTranslator.toFhirResource(omrsEncounter, cache)); encounter.getMeta().addTag(FhirConstants.OPENMRS_FHIR_EXT_ENCOUNTER_TAG, "encounter", "Encounter"); - encounter.getMeta().setLastUpdated(openMrsEncounter.getDateChanged()); - encounter.addContained(provenanceTranslator.getCreateProvenance(openMrsEncounter)); - encounter.addContained(provenanceTranslator.getUpdateProvenance(openMrsEncounter)); - encounter.setClass_(mapLocationToClass(openMrsEncounter.getLocation())); + encounter.getMeta().setLastUpdated(omrsEncounter.getDateChanged()); + encounter.addContained(provenanceTranslator.getCreateProvenance(omrsEncounter, cache)); + encounter.addContained(provenanceTranslator.getUpdateProvenance(omrsEncounter, cache)); + encounter.setClass_(mapLocationToClass(omrsEncounter.getLocation(), cache)); return encounter; } @Override public org.openmrs.Encounter toOpenmrsType(@Nonnull Encounter fhirEncounter) { - notNull(fhirEncounter, "The Encounter object should not be null"); + if (fhirEncounter == null) { + return null; + } + return this.toOpenmrsType(new org.openmrs.Encounter(), fhirEncounter); } @Override public org.openmrs.Encounter toOpenmrsType(@Nonnull org.openmrs.Encounter existingEncounter, @Nonnull Encounter encounter) { - notNull(existingEncounter, "The existing Openmrs Encounter object should not be null"); - notNull(encounter, "The Encounter object should not be null"); + if (encounter == null) { + return null; + } + + if (existingEncounter == null) { + existingEncounter = new org.openmrs.Encounter(); + } existingEncounter.setUuid(encounter.getId()); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationReferenceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationReferenceTranslatorImpl.java index 5f00db847..94dbeba68 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationReferenceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationReferenceTranslatorImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createLocationReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class LocationReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements LocationReferenceTranslator { +public class LocationReferenceTranslatorImpl implements LocationReferenceTranslator { @Autowired private FhirLocationDao locationDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java index d8610a7c4..2030b80ef 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImpl.java @@ -10,6 +10,8 @@ package org.openmrs.module.fhir2.api.translators.impl; import static org.apache.commons.lang3.Validate.notNull; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createLocationReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import static org.openmrs.module.fhir2.api.util.FhirUtils.getMetadataTranslation; import javax.annotation.Nonnull; @@ -41,7 +43,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class LocationTranslatorImpl extends BaseReferenceHandlingTranslator implements LocationTranslator { +public class LocationTranslatorImpl implements LocationTranslator { @Autowired private LocationAddressTranslator locationAddressTranslator; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImpl.java index b9e286f87..68db48991 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createMedicationReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class MedicationReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements MedicationReferenceTranslator { +public class MedicationReferenceTranslatorImpl implements MedicationReferenceTranslator { @Autowired private FhirMedicationDao medicationDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTimingTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTimingTranslatorImpl.java index 36aeab526..3c47ea82b 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTimingTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTimingTranslatorImpl.java @@ -32,6 +32,7 @@ public Timing toFhirResource(@Nonnull DrugOrder drugOrder) { if (drugOrder == null) { return null; } + Timing timing = new Timing(); timing.addEvent(drugOrder.getScheduledDate()); timing.setRepeat(timingComponentTranslator.toFhirResource(drugOrder)); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTranslatorImpl.java index 1ddecf97e..76321c0e5 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/MedicationRequestTranslatorImpl.java @@ -10,6 +10,7 @@ package org.openmrs.module.fhir2.api.translators.impl; import static org.apache.commons.lang3.Validate.notNull; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createOrderReference; import javax.annotation.Nonnull; @@ -38,7 +39,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class MedicationRequestTranslatorImpl extends BaseReferenceHandlingTranslator implements MedicationRequestTranslator { +public class MedicationRequestTranslatorImpl implements MedicationRequestTranslator { @Autowired private MedicationRequestStatusTranslator statusTranslator; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationBasedOnReferenceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationBasedOnReferenceTranslatorImpl.java index d6642559c..00f97b886 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationBasedOnReferenceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationBasedOnReferenceTranslatorImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createOrderReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -25,7 +29,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class ObservationBasedOnReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements ObservationBasedOnReferenceTranslator { +public class ObservationBasedOnReferenceTranslatorImpl implements ObservationBasedOnReferenceTranslator { @Autowired private FhirServiceRequestDao serviceRequestDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationInterpretationTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationInterpretationTranslatorImpl.java index 4ef1321fd..ee0f31714 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationInterpretationTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationInterpretationTranslatorImpl.java @@ -30,4 +30,5 @@ public CodeableConcept toFhirResource(@Nonnull Obs obs) { public Obs toOpenmrsType(@Nonnull Obs existingObs, @Nonnull CodeableConcept resource) { return existingObs; } + } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImpl.java index 7ae756dcd..ba29ba7ed 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createObservationReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class ObservationReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements ObservationReferenceTranslator { +public class ObservationReferenceTranslatorImpl implements ObservationReferenceTranslator { @Autowired private FhirObservationDao observationDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationTranslatorImpl.java index 0edbe85c8..6324f48c3 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ObservationTranslatorImpl.java @@ -10,6 +10,7 @@ package org.openmrs.module.fhir2.api.translators.impl; import static org.apache.commons.lang3.Validate.notNull; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createLocationReferenceByUuid; import javax.annotation.Nonnull; @@ -47,7 +48,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class ObservationTranslatorImpl extends BaseReferenceHandlingTranslator implements ObservationTranslator { +public class ObservationTranslatorImpl implements ObservationTranslator { @Autowired private ObservationStatusTranslator observationStatusTranslator; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/OrderIdentifierTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/OrderIdentifierTranslatorImpl.java index f466ea56a..0e2bb5524 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/OrderIdentifierTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/OrderIdentifierTranslatorImpl.java @@ -11,7 +11,6 @@ import javax.annotation.Nonnull; -import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Identifier; @@ -24,7 +23,6 @@ public class OrderIdentifierTranslatorImpl implements OrderIdentifierTranslator @Override public Identifier toFhirResource(@Nonnull Order order) { - Identifier orderIdentifier = new Identifier(); Coding placCoding = new Coding().setSystem("http://terminology.hl7.org/CodeSystem/v2-0203").setCode("PLAC") @@ -37,10 +35,4 @@ public Identifier toFhirResource(@Nonnull Order order) { return orderIdentifier; } - - @Override - public Order toOpenmrsType(Identifier resource) { - throw new InvalidRequestException("Order Identifier cannot be manualy set"); - } - } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientIdentifierTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientIdentifierTranslatorImpl.java index 5488cf644..a635c3d9b 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientIdentifierTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientIdentifierTranslatorImpl.java @@ -9,6 +9,8 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createLocationReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import static org.openmrs.module.fhir2.api.util.FhirUtils.getMetadataTranslation; import javax.annotation.Nonnull; @@ -32,8 +34,7 @@ @Component @Setter(AccessLevel.PACKAGE) -// TODO Create proper "System" value -public class PatientIdentifierTranslatorImpl extends BaseReferenceHandlingTranslator implements PatientIdentifierTranslator { +public class PatientIdentifierTranslatorImpl implements PatientIdentifierTranslator { @Autowired private FhirPatientIdentifierSystemService patientIdentifierSystemService; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImpl.java index deb5ab2af..335a55cce 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createPatientReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class PatientReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements PatientReferenceTranslator { +public class PatientReferenceTranslatorImpl implements PatientReferenceTranslator { @Autowired private FhirPatientDao patientDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java index 1b6210ae1..ef1c6a570 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImpl.java @@ -12,6 +12,7 @@ import static org.apache.commons.lang3.Validate.notNull; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.List; import java.util.Objects; @@ -43,6 +44,7 @@ import org.openmrs.module.fhir2.api.translators.PersonNameTranslator; import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; import org.openmrs.module.fhir2.api.translators.TelecomTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -78,26 +80,49 @@ public class PatientTranslatorImpl implements PatientTranslator { private ProvenanceTranslator provenanceTranslator; @Override - public Patient toFhirResource(@Nonnull org.openmrs.Patient openmrsPatient) { - notNull(openmrsPatient, "The Openmrs Patient object should not be null"); + public Patient toFhirResource(@Nonnull org.openmrs.Patient patient) { + if (patient == null) { + return null; + } + + return toFhirResourceInternal(patient, null); + } + + @Override + public Patient toFhirResource(@Nonnull org.openmrs.Patient patient, @Nullable FhirCache cache) { + if (patient == null) { + return null; + } + + if (cache != null) { + return (Patient) cache.get(getCacheKey(patient), k -> toFhirResourceInternal(patient, cache)); + } + + return toFhirResourceInternal(patient, null); + } + + protected Patient toFhirResourceInternal(@Nonnull org.openmrs.Patient openmrsPatient, @Nullable FhirCache cache) { + if (openmrsPatient == null) { + return null; + } Patient patient = new Patient(); patient.setId(openmrsPatient.getUuid()); patient.setActive(!openmrsPatient.getVoided()); for (PatientIdentifier identifier : openmrsPatient.getActiveIdentifiers()) { - patient.addIdentifier(identifierTranslator.toFhirResource(identifier)); + patient.addIdentifier(identifierTranslator.toFhirResource(identifier, cache)); } for (PersonName name : openmrsPatient.getNames()) { - patient.addName(nameTranslator.toFhirResource(name)); + patient.addName(nameTranslator.toFhirResource(name, cache)); } if (openmrsPatient.getGender() != null) { - patient.setGender(genderTranslator.toFhirResource(openmrsPatient.getGender())); + patient.setGender(genderTranslator.toFhirResource(openmrsPatient.getGender(), cache)); } - patient.setBirthDateElement(birthDateTranslator.toFhirResource(openmrsPatient)); + patient.setBirthDateElement(birthDateTranslator.toFhirResource(openmrsPatient, cache)); if (openmrsPatient.getDead()) { if (openmrsPatient.getDeathDate() != null) { @@ -110,13 +135,13 @@ public Patient toFhirResource(@Nonnull org.openmrs.Patient openmrsPatient) { } for (PersonAddress address : openmrsPatient.getAddresses()) { - patient.addAddress(addressTranslator.toFhirResource(address)); + patient.addAddress(addressTranslator.toFhirResource(address, cache)); } patient.setTelecom(getPatientContactDetails(openmrsPatient)); patient.getMeta().setLastUpdated(openmrsPatient.getDateChanged()); - patient.addContained(provenanceTranslator.getCreateProvenance(openmrsPatient)); - patient.addContained(provenanceTranslator.getUpdateProvenance(openmrsPatient)); + patient.addContained(provenanceTranslator.getCreateProvenance(openmrsPatient, cache)); + patient.addContained(provenanceTranslator.getUpdateProvenance(openmrsPatient, cache)); return patient; } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImpl.java index ee775de80..f45e19b6b 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImpl.java @@ -12,6 +12,7 @@ import static org.apache.commons.lang3.Validate.notNull; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Objects; @@ -33,6 +34,7 @@ import org.openmrs.module.fhir2.api.translators.PersonTranslator; import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; import org.openmrs.module.fhir2.api.translators.TelecomTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -65,7 +67,28 @@ public class PersonTranslatorImpl implements PersonTranslator { private FhirPatientDao patientDao; @Override - public org.hl7.fhir.r4.model.Person toFhirResource(@Nonnull Person openmrsPerson) { + public org.hl7.fhir.r4.model.Person toFhirResource(@Nonnull Person person) { + if (person == null) { + return null; + } + + return toFhirResourceInternal(person, null); + } + + @Override + public org.hl7.fhir.r4.model.Person toFhirResource(@Nonnull Person person, @Nullable FhirCache cache) { + if (person == null) { + return null; + } + + if (cache != null) { + return (org.hl7.fhir.r4.model.Person) cache.get(getCacheKey(person), k -> toFhirResourceInternal(person, cache)); + } + + return toFhirResourceInternal(person, null); + } + + protected org.hl7.fhir.r4.model.Person toFhirResourceInternal(@Nonnull Person openmrsPerson, @Nullable FhirCache cache) { notNull(openmrsPerson, "The Openmrs Person object should not be null"); org.hl7.fhir.r4.model.Person person = new org.hl7.fhir.r4.model.Person(); @@ -73,28 +96,29 @@ public org.hl7.fhir.r4.model.Person toFhirResource(@Nonnull Person openmrsPerson person.setActive(true); for (PersonName name : openmrsPerson.getNames()) { - person.addName(nameTranslator.toFhirResource(name)); + person.addName(nameTranslator.toFhirResource(name, cache)); } if (openmrsPerson.getGender() != null) { - person.setGender(genderTranslator.toFhirResource(openmrsPerson.getGender())); + person.setGender(genderTranslator.toFhirResource(openmrsPerson.getGender(), cache)); } - person.setBirthDateElement(birthDateTranslator.toFhirResource(openmrsPerson)); + person.setBirthDateElement(birthDateTranslator.toFhirResource(openmrsPerson, cache)); for (PersonAddress address : openmrsPerson.getAddresses()) { - person.addAddress(addressTranslator.toFhirResource(address)); + person.addAddress(addressTranslator.toFhirResource(address, cache)); } - person.addTelecom(telecomTranslator.toFhirResource(openmrsPerson)); + person.addTelecom(telecomTranslator.toFhirResource(openmrsPerson, cache)); if (openmrsPerson.getIsPatient()) { person.addLink(new org.hl7.fhir.r4.model.Person.PersonLinkComponent() .setTarget(patientReferenceTranslator.toFhirResource(patientDao.get(openmrsPerson.getUuid())))); } + person.getMeta().setLastUpdated(openmrsPerson.getDateChanged()); - person.addContained(provenanceTranslator.getCreateProvenance(openmrsPerson)); - person.addContained(provenanceTranslator.getUpdateProvenance(openmrsPerson)); + person.addContained(provenanceTranslator.getCreateProvenance(openmrsPerson, cache)); + person.addContained(provenanceTranslator.getUpdateProvenance(openmrsPerson, cache)); return person; } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImpl.java index 1c6919c46..9f657bdb9 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createPractitionerReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class PractitionerReferenceTranslatorProviderImpl extends BaseReferenceHandlingTranslator implements PractitionerReferenceTranslator { +public class PractitionerReferenceTranslatorProviderImpl implements PractitionerReferenceTranslator { @Autowired private FhirPractitionerDao practitionerDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImpl.java index 93ba2719b..9670c2970 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createPractitionerReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class PractitionerReferenceTranslatorUserImpl extends BaseReferenceHandlingTranslator implements PractitionerReferenceTranslator { +public class PractitionerReferenceTranslatorUserImpl implements PractitionerReferenceTranslator { @Autowired private FhirUserDao userDao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorProviderImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorProviderImpl.java index 8e74ac8da..a4ee70971 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorProviderImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorProviderImpl.java @@ -17,24 +17,16 @@ import lombok.AccessLevel; import lombok.Setter; -import org.hl7.fhir.r4.model.Address; import org.hl7.fhir.r4.model.ContactPoint; -import org.hl7.fhir.r4.model.HumanName; import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Practitioner; import org.openmrs.BaseOpenmrsData; import org.openmrs.Person; -import org.openmrs.PersonAddress; -import org.openmrs.PersonName; import org.openmrs.Provider; import org.openmrs.ProviderAttribute; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.FhirGlobalPropertyService; import org.openmrs.module.fhir2.api.dao.FhirPractitionerDao; -import org.openmrs.module.fhir2.api.translators.BirthDateTranslator; -import org.openmrs.module.fhir2.api.translators.GenderTranslator; -import org.openmrs.module.fhir2.api.translators.PersonAddressTranslator; -import org.openmrs.module.fhir2.api.translators.PersonNameTranslator; import org.openmrs.module.fhir2.api.translators.PractitionerTranslator; import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; import org.openmrs.module.fhir2.api.translators.TelecomTranslator; @@ -43,19 +35,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class PractitionerTranslatorProviderImpl implements PractitionerTranslator { - - @Autowired - private PersonNameTranslator nameTranslator; - - @Autowired - private PersonAddressTranslator addressTranslator; - - @Autowired - private GenderTranslator genderTranslator; - - @Autowired - private BirthDateTranslator birthDateTranslator; +public class PractitionerTranslatorProviderImpl extends BasePractitionerTranslator implements PractitionerTranslator { @Autowired private TelecomTranslator telecomTranslator; @@ -69,46 +49,6 @@ public class PractitionerTranslatorProviderImpl implements PractitionerTranslato @Autowired private ProvenanceTranslator provenanceTranslator; - @Override - public Provider toOpenmrsType(@Nonnull Provider existingProvider, @Nonnull Practitioner practitioner) { - if (existingProvider == null) { - return null; - } - - if (practitioner == null) { - return null; - } - - existingProvider.setUuid(practitioner.getId()); - - existingProvider.setIdentifier(practitioner.getIdentifierFirstRep().getValue()); - - if (existingProvider.getPerson() == null) { - existingProvider.setPerson(new Person()); - } - - if (practitioner.hasBirthDateElement()) { - birthDateTranslator.toOpenmrsType(existingProvider.getPerson(), practitioner.getBirthDateElement()); - } - - for (HumanName name : practitioner.getName()) { - existingProvider.getPerson().addName(nameTranslator.toOpenmrsType(name)); - } - - if (practitioner.hasGender()) { - existingProvider.getPerson().setGender(genderTranslator.toOpenmrsType(practitioner.getGender())); - } - - for (Address address : practitioner.getAddress()) { - existingProvider.getPerson().addAddress(addressTranslator.toOpenmrsType(address)); - } - practitioner.getTelecom().stream().map( - contactPoint -> (ProviderAttribute) telecomTranslator.toOpenmrsType(new ProviderAttribute(), contactPoint)) - .filter(Objects::nonNull).forEach(existingProvider::addAttribute); - - return existingProvider; - } - @Override public Practitioner toFhirResource(@Nonnull Provider provider) { if (provider == null) { @@ -126,17 +66,7 @@ public Practitioner toFhirResource(@Nonnull Provider provider) { practitioner.setTelecom(getProviderContactDetails(provider)); if (provider.getPerson() != null) { - practitioner.setBirthDateElement(birthDateTranslator.toFhirResource(provider.getPerson())); - - practitioner.setGender(genderTranslator.toFhirResource(provider.getPerson().getGender())); - - for (PersonName name : provider.getPerson().getNames()) { - practitioner.addName(nameTranslator.toFhirResource(name)); - } - - for (PersonAddress address : provider.getPerson().getAddresses()) { - practitioner.addAddress(addressTranslator.toFhirResource(address)); - } + personToPractitioner(provider.getPerson(), practitioner); } practitioner.getMeta().setLastUpdated(provider.getDateChanged()); @@ -146,13 +76,6 @@ public Practitioner toFhirResource(@Nonnull Provider provider) { return practitioner; } - public List getProviderContactDetails(@Nonnull Provider provider) { - return fhirPractitionerDao - .getActiveAttributesByPractitionerAndAttributeTypeUuid(provider, - globalPropertyService.getGlobalProperty(FhirConstants.PROVIDER_CONTACT_POINT_ATTRIBUTE_TYPE)) - .stream().map(telecomTranslator::toFhirResource).collect(Collectors.toList()); - } - @Override public Provider toOpenmrsType(@Nonnull Practitioner practitioner) { if (practitioner == null) { @@ -162,4 +85,38 @@ public Provider toOpenmrsType(@Nonnull Practitioner practitioner) { return toOpenmrsType(new org.openmrs.Provider(), practitioner); } + @Override + public Provider toOpenmrsType(@Nonnull Provider existingProvider, @Nonnull Practitioner practitioner) { + if (existingProvider == null) { + return null; + } + + if (practitioner == null) { + return null; + } + + existingProvider.setUuid(practitioner.getId()); + + existingProvider.setIdentifier(practitioner.getIdentifierFirstRep().getValue()); + + if (existingProvider.getPerson() == null) { + existingProvider.setPerson(new Person()); + } + + practitionerToPerson(practitioner, existingProvider.getPerson()); + + practitioner.getTelecom().stream().map( + contactPoint -> (ProviderAttribute) telecomTranslator.toOpenmrsType(new ProviderAttribute(), contactPoint)) + .filter(Objects::nonNull).forEach(existingProvider::addAttribute); + + return existingProvider; + } + + protected List getProviderContactDetails(@Nonnull Provider provider) { + return fhirPractitionerDao + .getActiveAttributesByPractitionerAndAttributeTypeUuid(provider, + globalPropertyService.getGlobalProperty(FhirConstants.PROVIDER_CONTACT_POINT_ATTRIBUTE_TYPE)) + .stream().map(telecomTranslator::toFhirResource).collect(Collectors.toList()); + } + } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorUserImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorUserImpl.java index bbfcb7232..cbecf38cb 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorUserImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerTranslatorUserImpl.java @@ -18,33 +18,14 @@ import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Practitioner; import org.openmrs.Person; -import org.openmrs.PersonAddress; -import org.openmrs.PersonName; import org.openmrs.User; import org.openmrs.module.fhir2.FhirConstants; -import org.openmrs.module.fhir2.api.translators.BirthDateTranslator; -import org.openmrs.module.fhir2.api.translators.GenderTranslator; -import org.openmrs.module.fhir2.api.translators.PersonAddressTranslator; -import org.openmrs.module.fhir2.api.translators.PersonNameTranslator; import org.openmrs.module.fhir2.api.translators.PractitionerTranslator; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component @Setter(AccessLevel.PACKAGE) -public class PractitionerTranslatorUserImpl implements PractitionerTranslator { - - @Autowired - private PersonNameTranslator nameTranslator; - - @Autowired - private GenderTranslator genderTranslator; - - @Autowired - private BirthDateTranslator birthDateTranslator; - - @Autowired - private PersonAddressTranslator addressTranslator; +public class PractitionerTranslatorUserImpl extends BasePractitionerTranslator implements PractitionerTranslator { @Override public Practitioner toFhirResource(@Nonnull User user) { @@ -59,17 +40,9 @@ public Practitioner toFhirResource(@Nonnull User user) { practitioner.addIdentifier(userIdentifier); if (user.getPerson() != null) { - practitioner.setBirthDateElement(birthDateTranslator.toFhirResource(user.getPerson())); - - practitioner.setGender(genderTranslator.toFhirResource(user.getPerson().getGender())); - for (PersonName name : user.getPerson().getNames()) { - practitioner.addName(nameTranslator.toFhirResource(name)); - } - - for (PersonAddress address : user.getPerson().getAddresses()) { - practitioner.addAddress(addressTranslator.toFhirResource(address)); - } + personToPractitioner(user.getPerson(), practitioner); } + practitioner.getMeta().setLastUpdated(user.getDateChanged()); return practitioner; @@ -88,15 +61,11 @@ public User toOpenmrsType(@Nonnull User user, @Nonnull Practitioner practitioner user.setUuid(practitioner.getId()); setSystemId(practitioner, user); - if (practitioner.hasBirthDateElement()) { - birthDateTranslator.toOpenmrsType(user.getPerson(), practitioner.getBirthDateElement()); - } - if (user.getPerson() == null) { user.setPerson(new Person()); } - user.getPerson().setGender(genderTranslator.toOpenmrsType(practitioner.getGender())); + practitionerToPerson(practitioner, user.getPerson()); user.setDateChanged(practitioner.getMeta().getLastUpdated()); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ProvenanceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ProvenanceTranslatorImpl.java index 652030a75..629cefb02 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ProvenanceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ProvenanceTranslatorImpl.java @@ -13,11 +13,10 @@ import lombok.Setter; import org.openmrs.Auditable; import org.openmrs.OpenmrsObject; -import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; import org.springframework.stereotype.Component; @Component @Setter(AccessLevel.PACKAGE) -public class ProvenanceTranslatorImpl extends BaseProvenanceHandlingTranslator implements ProvenanceTranslator { +public class ProvenanceTranslatorImpl extends BaseProvenanceHandlingTranslator { } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseReferenceHandlingTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ReferenceHandlingTranslator.java similarity index 81% rename from api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseReferenceHandlingTranslator.java rename to api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ReferenceHandlingTranslator.java index 01f647a5e..0f1c780e7 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/BaseReferenceHandlingTranslator.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ReferenceHandlingTranslator.java @@ -40,41 +40,45 @@ @Setter(AccessLevel.PACKAGE) @Slf4j -public abstract class BaseReferenceHandlingTranslator { +public final class ReferenceHandlingTranslator { - protected Reference createEncounterReference(@Nonnull Encounter encounter) { + private ReferenceHandlingTranslator() { + + } + + public static Reference createEncounterReference(@Nonnull Encounter encounter) { return createEncounterReference((OpenmrsObject) encounter); } - protected Reference createEncounterReference(@Nonnull Visit visit) { + public static Reference createEncounterReference(@Nonnull Visit visit) { return createEncounterReference((OpenmrsObject) visit); } - private Reference createEncounterReference(@Nonnull OpenmrsObject encounter) { + public static Reference createEncounterReference(@Nonnull OpenmrsObject encounter) { return new Reference().setReference(FhirConstants.ENCOUNTER + "/" + encounter.getUuid()) .setType(FhirConstants.ENCOUNTER); } - protected Reference createMedicationReference(@Nonnull Drug drug) { + public static Reference createMedicationReference(@Nonnull Drug drug) { return new Reference().setReference(FhirConstants.MEDICATION + "/" + drug.getUuid()) .setType(FhirConstants.MEDICATION); } - protected Reference createObservationReference(@Nonnull Obs obs) { + public static Reference createObservationReference(@Nonnull Obs obs) { return new Reference().setReference(FhirConstants.OBSERVATION + "/" + obs.getUuid()) .setType(FhirConstants.OBSERVATION); } - protected Reference createLocationReferenceByUuid(@Nonnull String uuid) { + public static Reference createLocationReferenceByUuid(@Nonnull String uuid) { return new Reference().setReference(FhirConstants.LOCATION + "/" + uuid).setType(FhirConstants.LOCATION); } - protected Reference createLocationReference(@Nonnull Location location) { + public static Reference createLocationReference(@Nonnull Location location) { return new Reference().setReference(FhirConstants.LOCATION + "/" + location.getUuid()) .setType(FhirConstants.LOCATION).setDisplay(getMetadataTranslation(location)); } - protected Reference createPatientReference(@Nonnull Patient patient) { + public static Reference createPatientReference(@Nonnull Patient patient) { Reference reference = new Reference().setReference(FhirConstants.PATIENT + "/" + patient.getUuid()) .setType(FhirConstants.PATIENT); @@ -99,7 +103,7 @@ protected Reference createPatientReference(@Nonnull Patient patient) { return reference; } - protected Reference createPractitionerReference(@Nonnull User user) { + public static Reference createPractitionerReference(@Nonnull User user) { Reference reference = new Reference().setReference(FhirConstants.PRACTITIONER + "/" + user.getUuid()) .setType(FhirConstants.PRACTITIONER); @@ -112,7 +116,7 @@ protected Reference createPractitionerReference(@Nonnull User user) { return reference; } - protected Reference createPractitionerReference(@Nonnull Provider provider) { + public static Reference createPractitionerReference(@Nonnull Provider provider) { Reference reference = new Reference().setReference(FhirConstants.PRACTITIONER + "/" + provider.getUuid()) .setType(FhirConstants.PRACTITIONER); @@ -135,7 +139,7 @@ protected Reference createPractitionerReference(@Nonnull Provider provider) { return reference; } - protected Reference createOrderReference(@Nonnull Order order) { + public static Reference createOrderReference(@Nonnull Order order) { if (order == null) { return null; } @@ -152,11 +156,11 @@ protected Reference createOrderReference(@Nonnull Order order) { } } - protected Optional getReferenceType(Reference reference) { + public static Optional getReferenceType(Reference reference) { return FhirUtils.getReferenceType(reference); } - protected Optional getReferenceId(Reference reference) { + public static Optional getReferenceId(Reference reference) { return FhirUtils.referenceToId(reference.getReference()); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ServiceRequestTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ServiceRequestTranslatorImpl.java index 400b96dfa..654bd88af 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ServiceRequestTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ServiceRequestTranslatorImpl.java @@ -10,29 +10,21 @@ package org.openmrs.module.fhir2.api.translators.impl; import static org.apache.commons.lang3.Validate.notNull; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createOrderReference; import javax.annotation.Nonnull; -import java.util.Collection; import java.util.Collections; import java.util.Date; -import java.util.stream.Collectors; -import ca.uhn.fhir.rest.api.server.IBundleProvider; -import ca.uhn.fhir.rest.param.ReferenceAndListParam; -import ca.uhn.fhir.rest.param.ReferenceOrListParam; -import ca.uhn.fhir.rest.param.ReferenceParam; import lombok.AccessLevel; import lombok.Setter; import org.hl7.fhir.r4.model.Period; -import org.hl7.fhir.r4.model.Reference; import org.hl7.fhir.r4.model.ServiceRequest; -import org.hl7.fhir.r4.model.Task; import org.openmrs.Encounter; import org.openmrs.Order; import org.openmrs.Provider; import org.openmrs.TestOrder; -import org.openmrs.module.fhir2.api.FhirTaskService; import org.openmrs.module.fhir2.api.translators.ConceptTranslator; import org.openmrs.module.fhir2.api.translators.EncounterReferenceTranslator; import org.openmrs.module.fhir2.api.translators.OrderIdentifierTranslator; @@ -44,14 +36,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class ServiceRequestTranslatorImpl extends BaseReferenceHandlingTranslator implements ServiceRequestTranslator { - - private static final int START_INDEX = 0; - - private static final int END_INDEX = 10; - - @Autowired - private FhirTaskService taskService; +public class ServiceRequestTranslatorImpl extends BaseServiceRequestTranslator implements ServiceRequestTranslator { @Autowired private ConceptTranslator conceptTranslator; @@ -131,20 +116,4 @@ private ServiceRequest.ServiceRequestStatus determineServiceRequestStatus(TestOr return ServiceRequest.ServiceRequestStatus.ACTIVE; } } - - private Reference determineServiceRequestPerformer(String orderUuid) { - IBundleProvider results = taskService.searchForTasks( - new ReferenceAndListParam() - .addAnd(new ReferenceOrListParam().add(new ReferenceParam("ServiceRequest", null, orderUuid))), - null, null, null, null, null, null); - - Collection serviceRequestTasks = results.getResources(START_INDEX, END_INDEX).stream().map(p -> (Task) p) - .collect(Collectors.toList()); - - if (serviceRequestTasks.size() != 1) { - return null; - } - - return serviceRequestTasks.iterator().next().getOwner(); - } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImpl.java index b5de0c28d..875353d4d 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImpl.java @@ -9,6 +9,10 @@ */ package org.openmrs.module.fhir2.api.translators.impl; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createEncounterReference; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType; + import javax.annotation.Nonnull; import lombok.AccessLevel; @@ -23,7 +27,7 @@ @Component @Setter(AccessLevel.PACKAGE) -public class VisitReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterReferenceTranslator { +public class VisitReferenceTranslatorImpl implements EncounterReferenceTranslator { @Autowired private FhirVisitDao dao; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImpl.java index 384a4b4fa..cf09dbcfa 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImpl.java @@ -12,20 +12,20 @@ import static org.apache.commons.lang3.Validate.notNull; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import lombok.AccessLevel; import lombok.Setter; import org.hl7.fhir.r4.model.Encounter; -import org.openmrs.EncounterType; import org.openmrs.Visit; import org.openmrs.VisitType; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.translators.EncounterLocationTranslator; import org.openmrs.module.fhir2.api.translators.EncounterPeriodTranslator; import org.openmrs.module.fhir2.api.translators.EncounterTranslator; -import org.openmrs.module.fhir2.api.translators.EncounterTypeTranslator; import org.openmrs.module.fhir2.api.translators.PatientReferenceTranslator; import org.openmrs.module.fhir2.api.translators.ProvenanceTranslator; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -42,9 +42,6 @@ public class VisitTranslatorImpl extends BaseEncounterTranslator implements Enco @Autowired private ProvenanceTranslator provenanceTranslator; - @Autowired - private EncounterTypeTranslator encounterTypeTranslator; - @Autowired private VisitTypeTranslatorImpl visitTypeTranslator; @@ -53,8 +50,27 @@ public class VisitTranslatorImpl extends BaseEncounterTranslator implements Enco @Override public Encounter toFhirResource(@Nonnull Visit visit) { - notNull(visit, "The OpenMrs Visit object should not be null"); + if (visit == null) { + return null; + } + + return toFhirResourceInternal(visit, null); + } + + @Override + public Encounter toFhirResource(@Nonnull Visit visit, @Nullable FhirCache cache) { + if (visit == null) { + return null; + } + + if (cache != null) { + return (Encounter) cache.get(getCacheKey(visit), k -> toFhirResourceInternal(visit, cache)); + } + return toFhirResourceInternal(visit, null); + } + + private Encounter toFhirResourceInternal(@Nonnull Visit visit, @Nullable FhirCache cache) { Encounter encounter = new Encounter(); encounter.setId(visit.getUuid()); encounter.setStatus(Encounter.EncounterStatus.UNKNOWN); @@ -65,7 +81,7 @@ public Encounter toFhirResource(@Nonnull Visit visit) { encounterLocationTranslator.toFhirResource(visit.getLocation()); } - encounter.setClass_(mapLocationToClass(visit.getLocation())); + encounter.setClass_(mapLocationToClass(visit.getLocation(), cache)); encounter.setPeriod(visitPeriodTranslator.toFhirResource(visit)); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/util/FhirCache.java b/api/src/main/java/org/openmrs/module/fhir2/api/util/FhirCache.java new file mode 100644 index 000000000..4a7c5acee --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/api/util/FhirCache.java @@ -0,0 +1,58 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.api.util; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import java.util.concurrent.TimeUnit; +import java.util.function.Function; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.springframework.stereotype.Component; + +@Component +public class FhirCache { + + private final Cache fhirResourceCache; + + public FhirCache() { + // @formatter:off + fhirResourceCache = Caffeine.newBuilder().initialCapacity(5_000).maximumSize(100_000) + .expireAfterAccess(2, TimeUnit.MINUTES).build(); + // @formatter:on + } + + @Nullable + public Object get(@Nonnull String s, @Nonnull Function function) { + if (s != null) { + return fhirResourceCache.get(s, function); + } + + return function.apply(null); + } + + public void put(@Nonnull String s, @Nonnull Object o) { + if (s != null) { + fhirResourceCache.put(s, o); + } + } + + public void invalidate(@Nonnull String key) { + if (key != null) { + fhirResourceCache.invalidate(key); + } + } + + public void invalidateAll() { + fhirResourceCache.invalidateAll(); + } +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/util/FhirUtils.java b/api/src/main/java/org/openmrs/module/fhir2/api/util/FhirUtils.java index ff5ff2760..fe2971c01 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/util/FhirUtils.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/util/FhirUtils.java @@ -62,6 +62,10 @@ public enum OpenmrsEncounterType { + "|VerificationResult|VisionPrescription)" + "/(?[A-Za-z0-9\\-.]{1,64})(?:/_history/(?[A-Za-z0-9\\-.]{1,64}))?"); + private FhirUtils() { + + } + public static String newUuid() { return UUID.randomUUID().toString(); } diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirAllergyIntoleranceServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirAllergyIntoleranceServiceImplTest.java index f92279719..84b7c4c71 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirAllergyIntoleranceServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirAllergyIntoleranceServiceImplTest.java @@ -122,9 +122,9 @@ private List get(IBundleProvider results) { } @Test - public void getAllergyIntoleranceByUuid_shouldGetAllergyIntoleranceByUuid() { + public void get_shouldGetAllergyIntoleranceByUuid() { when(allergyIntoleranceDao.get(ALLERGY_UUID)).thenReturn(omrsAllergy); - when(translator.toFhirResource(omrsAllergy)).thenReturn(fhirAllergy); + when(translator.toFhirResource(omrsAllergy, null)).thenReturn(fhirAllergy); AllergyIntolerance result = service.get(ALLERGY_UUID); assertThat(result, notNullValue()); @@ -133,7 +133,7 @@ public void getAllergyIntoleranceByUuid_shouldGetAllergyIntoleranceByUuid() { } @Test - public void getAllergyIntoleranceByUuid_shouldResourceNotFoundWhenCalledWithWrongUuid() { + public void get_shouldResourceNotFoundWhenCalledWithWrongUuid() { assertThrows(ResourceNotFoundException.class, () -> service.get(WRONG_ALLERGY_UUID)); } @@ -494,7 +494,7 @@ public void searchForAllergies_shouldNotAddPatientsToReturnedResultsForEmptyIncl } @Test - public void saveAllergy_shouldSaveNewAllergy() { + public void create_shouldSaveNewAllergy() { Allergy allergy = new Allergy(); allergy.setUuid(ALLERGY_UUID); @@ -506,6 +506,7 @@ public void saveAllergy_shouldSaveNewAllergy() { when(allergyIntoleranceDao.createOrUpdate(allergy)).thenReturn(allergy); AllergyIntolerance result = service.create(allergyIntolerance); + assertThat(result, notNullValue()); assertThat(result.getId(), equalTo(ALLERGY_UUID)); } diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImplTest.java index 710573ef0..999ad902b 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConceptSourceServiceImplTest.java @@ -90,7 +90,7 @@ public void getFhirConceptSourceByConceptSourceName_shouldReturnConceptSourceFor FhirConceptSource source = new FhirConceptSource(); when(dao.getFhirConceptSourceByConceptSourceName("LOINC")).thenReturn(Optional.of(source)); - Optional result = fhirConceptSourceService.getFhirConceptSourceByConceptSourceName("LOINC"); + Optional result = fhirConceptSourceService.getFhirConceptSourceByConceptSourceName("LOINC", null); assertThat(result.isPresent(), is(true)); assertThat(result.get(), equalTo(source)); @@ -100,7 +100,7 @@ public void getFhirConceptSourceByConceptSourceName_shouldReturnConceptSourceFor public void getFhirConceptSourceByConceptSourceName_shouldReturnEmptyWhenNoConceptSourceFound() { when(dao.getFhirConceptSourceByConceptSourceName("LOINC")).thenReturn(Optional.empty()); - Optional result = fhirConceptSourceService.getFhirConceptSourceByConceptSourceName("LOINC"); + Optional result = fhirConceptSourceService.getFhirConceptSourceByConceptSourceName("LOINC", null); assertThat(result.isPresent(), is(false)); } diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImplTest.java index 9cc26141a..66b03ba83 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirConditionServiceImplTest.java @@ -118,9 +118,10 @@ private List get(IBundleProvider results) { @Test public void getObsConditionByUuid_shouldReturnConditionByUuid() { when(dao.get(OBS_UUID)).thenReturn(obsCondition); - when(translator.toFhirResource(obsCondition)).thenReturn(condition); + when(translator.toFhirResource(obsCondition, null)).thenReturn(condition); Condition result = fhirConditionService.get(OBS_UUID); + assertThat(result, notNullValue()); assertThat(result.getId(), equalTo(OBS_UUID)); } @@ -146,7 +147,7 @@ public void create_shouldCreateNewCondition() { @Test public void update_shouldUpdateExistingObsCondition() { when(dao.get(OBS_UUID)).thenReturn(obsCondition); - when(translator.toFhirResource(obsCondition)).thenReturn(condition); + when(translator.toFhirResource(obsCondition, null)).thenReturn(condition); when(dao.createOrUpdate(obsCondition)).thenReturn(obsCondition); when(translator.toOpenmrsType(any(Obs.class), any(org.hl7.fhir.r4.model.Condition.class))).thenReturn(obsCondition); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirDiagnosticReportServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirDiagnosticReportServiceImplTest.java index 4704a2d24..16ac2c39a 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirDiagnosticReportServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirDiagnosticReportServiceImplTest.java @@ -108,7 +108,7 @@ public void getDiagnosticReportByUuid_shouldRetrieveDiagnosticReportByUuid() { diagnosticReport.setId(UUID); when(dao.get(UUID)).thenReturn(fhirDiagnosticReport); - when(translator.toFhirResource(fhirDiagnosticReport)).thenReturn(diagnosticReport); + when(translator.toFhirResource(fhirDiagnosticReport, null)).thenReturn(diagnosticReport); DiagnosticReport result = service.get(UUID); @@ -159,7 +159,7 @@ public void updateDiagnosticReport_shouldUpdateExistingDiagnosticReport() { when(translator.toOpenmrsType(fhirDiagnosticReport, diagnosticReport)).thenReturn(updatedFhirDiagnosticReport); when(dao.createOrUpdate(updatedFhirDiagnosticReport)).thenReturn(updatedFhirDiagnosticReport); when(dao.get(UUID)).thenReturn(fhirDiagnosticReport); - when(translator.toFhirResource(updatedFhirDiagnosticReport)).thenReturn(diagnosticReport); + when(translator.toFhirResource(updatedFhirDiagnosticReport, null)).thenReturn(diagnosticReport); DiagnosticReport result = service.update(UUID, diagnosticReport); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirEncounterServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirEncounterServiceImplTest.java index 7c5570c65..b830a247f 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirEncounterServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirEncounterServiceImplTest.java @@ -134,7 +134,7 @@ private List get(IBundleProvider results) { @Test public void get_shouldGetEncounterByUuid() { when(dao.get(ENCOUNTER_UUID)).thenReturn(openMrsEncounter); - when(encounterTranslator.toFhirResource(openMrsEncounter)).thenReturn(fhirEncounter); + when(encounterTranslator.toFhirResource(openMrsEncounter, null)).thenReturn(fhirEncounter); org.hl7.fhir.r4.model.Encounter fhirEncounter = encounterService.get(ENCOUNTER_UUID); @@ -211,7 +211,7 @@ public void update_shouldUpdateEncounter() { when(encounterTranslator.toOpenmrsType(openMrsEncounter, fhirEncounter)).thenReturn(openMrsEncounter); when(dao.createOrUpdate(openMrsEncounter)).thenReturn(openMrsEncounter); when(dao.get(ENCOUNTER_UUID)).thenReturn(openMrsEncounter); - when(encounterTranslator.toFhirResource(openMrsEncounter)).thenReturn(fhirEncounter); + when(encounterTranslator.toFhirResource(openMrsEncounter, null)).thenReturn(fhirEncounter); org.hl7.fhir.r4.model.Encounter encounter = encounterService.update(ENCOUNTER_UUID, fhirEncounter); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirGroupServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirGroupServiceImplTest.java index 79d7818ca..13fdb0d82 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirGroupServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirGroupServiceImplTest.java @@ -107,9 +107,9 @@ private List get(IBundleProvider results) { } @Test - public void getGroupByUuid_shouldGetGroupByUuid() { + public void get_shouldGetGroupByUuid() { when(dao.get(COHORT_UUID)).thenReturn(cohort); - when(translator.toFhirResource(cohort)).thenReturn(group); + when(translator.toFhirResource(cohort, null)).thenReturn(group); Group group = groupService.get(COHORT_UUID); assertThat(group, notNullValue()); @@ -123,7 +123,7 @@ public void getGroupByUuid_shouldThrowResourceNotFoundWhenCalledWithUnknownUuid( } @Test - public void shouldSaveNewGroup() { + public void create_shouldSaveNewGroup() { Cohort cohort = new Cohort(); cohort.setUuid(COHORT_UUID); @@ -135,6 +135,7 @@ public void shouldSaveNewGroup() { when(dao.createOrUpdate(cohort)).thenReturn(cohort); Group result = groupService.create(group); + assertThat(result, notNullValue()); assertThat(result.getId(), equalTo(COHORT_UUID)); } @@ -164,7 +165,7 @@ public void updateGroupShouldThrowResourceNotFoundException() { } @Test - public void shouldUpdateGroup() { + public void update_shouldUpdateGroup() { Cohort cohort = new Cohort(); cohort.setUuid(COHORT_UUID); cohort.setVoided(false); @@ -174,17 +175,18 @@ public void shouldUpdateGroup() { group.setActive(false); when(dao.get(COHORT_UUID)).thenReturn(cohort); - when(translator.toFhirResource(cohort)).thenReturn(group); + when(translator.toFhirResource(cohort, null)).thenReturn(group); when(translator.toOpenmrsType(cohort, group)).thenReturn(cohort); when(dao.createOrUpdate(cohort)).thenReturn(cohort); Group result = groupService.update(COHORT_UUID, group); + assertThat(result, notNullValue()); assertThat(result.getActive(), is(false)); } @Test - public void shouldDeleteGroup() { + public void delete_shouldDeleteGroup() { Group group = new Group(); group.setId(COHORT_UUID); @@ -192,6 +194,7 @@ public void shouldDeleteGroup() { when(translator.toFhirResource(cohort)).thenReturn(group); Group result = groupService.delete(COHORT_UUID); + assertThat(result, notNullValue()); assertThat(result.getId(), equalTo(COHORT_UUID)); assertThat(result.getActive(), is(false)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirLocationServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirLocationServiceImplTest.java index 28f1a3c04..49591db38 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirLocationServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirLocationServiceImplTest.java @@ -130,7 +130,7 @@ protected void validateObject(org.openmrs.Location object) { @Test public void getLocationByUuid_shouldGetLocationByUuid() { when(locationDao.get(LOCATION_UUID)).thenReturn(location); - when(locationTranslator.toFhirResource(location)).thenReturn(fhirLocation); + when(locationTranslator.toFhirResource(location, null)).thenReturn(fhirLocation); org.hl7.fhir.r4.model.Location result = fhirLocationService.get(LOCATION_UUID); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationRequestServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationRequestServiceImplTest.java index 12208f738..803921aeb 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationRequestServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationRequestServiceImplTest.java @@ -111,9 +111,9 @@ protected void validateObject(DrugOrder object) { } @Test - public void shouldGetMedicationRequestByUuid() { + public void get_shouldGetMedicationRequestByUuid() { when(dao.get(MEDICATION_REQUEST_UUID)).thenReturn(drugOrder); - when(medicationRequestTranslator.toFhirResource(drugOrder)).thenReturn(medicationRequest); + when(medicationRequestTranslator.toFhirResource(drugOrder, null)).thenReturn(medicationRequest); MedicationRequest result = medicationRequestService.get(MEDICATION_REQUEST_UUID); assertThat(result, notNullValue()); @@ -122,7 +122,7 @@ public void shouldGetMedicationRequestByUuid() { } @Test - public void shouldThrowResourceNotFoundForBadMedicationRequestUuid() { + public void get_shouldThrowResourceNotFoundForBadMedicationRequestUuid() { assertThrows(ResourceNotFoundException.class, () -> medicationRequestService.get(BAD_MEDICATION_REQUEST_UUID)); } diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationServiceImplTest.java index 20828f9d7..571eb790a 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirMedicationServiceImplTest.java @@ -116,18 +116,19 @@ private List get(IBundleProvider results) { } @Test - public void getMedicationByUuid_shouldGetMedicationByUuid() { + public void get_shouldGetMedicationByUuid() { when(medicationDao.get(MEDICATION_UUID)).thenReturn(drug); - when(medicationTranslator.toFhirResource(drug)).thenReturn(medication); + when(medicationTranslator.toFhirResource(drug, null)).thenReturn(medication); Medication medication = fhirMedicationService.get(MEDICATION_UUID); + assertThat(medication, notNullValue()); assertThat(medication.getId(), notNullValue()); assertThat(medication.getId(), equalTo(MEDICATION_UUID)); } @Test - public void getMedicationByUuid_shouldThrowResourceNotFoundWhenCalledWithUnknownUuid() { + public void get_shouldThrowResourceNotFoundWhenCalledWithUnknownUuid() { assertThrows(ResourceNotFoundException.class, () -> fhirMedicationService.get(WRONG_MEDICATION_UUID)); } @@ -307,7 +308,7 @@ public void searchForMedications_shouldNotAddRelatedResourcesToResultListForEmpt } @Test - public void saveMedication_shouldSaveNewMedication() { + public void create_shouldSaveNewMedication() { Drug drug = new Drug(); drug.setUuid(MEDICATION_UUID); @@ -319,6 +320,7 @@ public void saveMedication_shouldSaveNewMedication() { when(medicationDao.createOrUpdate(drug)).thenReturn(drug); Medication result = fhirMedicationService.create(medication); + assertThat(result, notNullValue()); assertThat(result.getId(), equalTo(MEDICATION_UUID)); } @@ -357,11 +359,12 @@ public void updateMedication_shouldUpdateMedication() { medication.setStatus(Medication.MedicationStatus.INACTIVE); when(medicationDao.get(MEDICATION_UUID)).thenReturn(drug); - when(medicationTranslator.toFhirResource(drug)).thenReturn(medication); + when(medicationTranslator.toFhirResource(drug, null)).thenReturn(medication); when(medicationTranslator.toOpenmrsType(drug, medication)).thenReturn(drug); when(medicationDao.createOrUpdate(drug)).thenReturn(drug); Medication result = fhirMedicationService.update(MEDICATION_UUID, medication); + assertThat(result, notNullValue()); assertThat(result.getStatus(), equalTo(Medication.MedicationStatus.INACTIVE)); } @@ -373,6 +376,7 @@ public void deleteMedication_shouldDeleteMedication() { when(medicationTranslator.toFhirResource(drug)).thenReturn(medication); Medication medication = fhirMedicationService.delete(MEDICATION_UUID); + assertThat(medication, notNullValue()); assertThat(medication.getId(), equalTo(MEDICATION_UUID)); assertThat(medication.getStatus(), equalTo(Medication.MedicationStatus.INACTIVE)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirObservationServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirObservationServiceImplTest.java index 789df587a..1dfcb7591 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirObservationServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirObservationServiceImplTest.java @@ -96,13 +96,13 @@ protected void validateObject(Obs object) { } @Test - public void getObservationByUuid_shouldReturnObservationByUuid() { + public void get_shouldReturnObservationByUuid() { Obs obs = new Obs(); obs.setUuid(OBS_UUID); Observation observation = new Observation(); observation.setId(OBS_UUID); when(dao.get(OBS_UUID)).thenReturn(obs); - when(translator.toFhirResource(obs)).thenReturn(observation); + when(translator.toFhirResource(obs, null)).thenReturn(observation); Observation result = fhirObservationService.get(OBS_UUID); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPatientServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPatientServiceImplTest.java index 6e3e5875f..1c64cca51 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPatientServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPatientServiceImplTest.java @@ -164,9 +164,9 @@ protected void validateObject(Patient object) { } @Test - public void getPatientByUuid_shouldRetrievePatientByUuid() { + public void get_shouldRetrievePatientByUuid() { when(dao.get(PATIENT_UUID)).thenReturn(patient); - when(patientTranslator.toFhirResource(patient)).thenReturn(fhirPatient); + when(patientTranslator.toFhirResource(patient, null)).thenReturn(fhirPatient); org.hl7.fhir.r4.model.Patient result = patientService.get(PATIENT_UUID); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPractitionerServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPractitionerServiceImplTest.java index 403641223..7ccee17f2 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPractitionerServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirPractitionerServiceImplTest.java @@ -184,9 +184,10 @@ private List get(IBundleProvider results) { @Test public void shouldRetrievePractitionerByUuidWhoIsProvider() { when(practitionerDao.get(UUID)).thenReturn(provider); - when(practitionerTranslator.toFhirResource(provider)).thenReturn(practitioner); + when(practitionerTranslator.toFhirResource(provider, null)).thenReturn(practitioner); Practitioner result = practitionerService.get(UUID); + assertThat(result, notNullValue()); assertThat(result.getId(), notNullValue()); assertThat(result.getId(), equalTo(UUID)); @@ -195,10 +196,10 @@ public void shouldRetrievePractitionerByUuidWhoIsProvider() { @Test public void shouldRetrievePractitionerByUuidWhoIsUser() { when(practitionerDao.get(UUID2)).thenReturn(null); - when(userService.get(UUID2)).thenReturn(practitioner2); Practitioner result = practitionerService.get(UUID2); + assertThat(result, notNullValue()); assertThat(result.getId(), notNullValue()); assertThat(result.getId(), equalTo(UUID2)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirRelatedPersonServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirRelatedPersonServiceImplTest.java index fca6c599b..8f3d7fdda 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirRelatedPersonServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirRelatedPersonServiceImplTest.java @@ -200,9 +200,10 @@ public void shouldGetRelatedPersonById() { relatedPerson.setId(RELATED_PERSON_UUID); when(dao.get(RELATED_PERSON_UUID)).thenReturn(relationship); - when(translator.toFhirResource(relationship)).thenReturn(relatedPerson); + when(translator.toFhirResource(relationship, null)).thenReturn(relatedPerson); RelatedPerson result = relatedPersonService.get(RELATED_PERSON_UUID); + assertThat(result, notNullValue()); assertThat(result.getId(), notNullValue()); assertThat(result.getId(), equalTo(RELATED_PERSON_UUID)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirServiceRequestServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirServiceRequestServiceImplTest.java index c95a10e6e..3da11f4eb 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirServiceRequestServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirServiceRequestServiceImplTest.java @@ -132,7 +132,7 @@ private List get(IBundleProvider results) { @Test public void shouldRetrieveServiceRequestByUUID() { when(dao.get(SERVICE_REQUEST_UUID)).thenReturn(order); - when(translator.toFhirResource(order)).thenReturn(fhirServiceRequest); + when(translator.toFhirResource(order, null)).thenReturn(fhirServiceRequest); ServiceRequest result = serviceRequestService.get(SERVICE_REQUEST_UUID); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirTaskServiceImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirTaskServiceImplTest.java index ed5baff73..7e111df70 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirTaskServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/impl/FhirTaskServiceImplTest.java @@ -101,7 +101,7 @@ private List get(IBundleProvider results) { } @Test - public void getTask_shouldRetrieveTaskByUuid() { + public void get_shouldRetrieveTaskByUuid() { FhirTask task = new FhirTask(); org.hl7.fhir.r4.model.Task translatedTask = new org.hl7.fhir.r4.model.Task(); @@ -109,7 +109,7 @@ public void getTask_shouldRetrieveTaskByUuid() { translatedTask.setId(TASK_UUID); when(dao.get(TASK_UUID)).thenReturn(task); - when(translator.toFhirResource(task)).thenReturn(translatedTask); + when(translator.toFhirResource(task, null)).thenReturn(translatedTask); org.hl7.fhir.r4.model.Task result = fhirTaskService.get(TASK_UUID); @@ -160,7 +160,7 @@ public void updateTask_shouldUpdateExistingTask() { when(translator.toOpenmrsType(openmrsTask, fhirTask)).thenReturn(updatedOpenmrsTask); when(dao.createOrUpdate(updatedOpenmrsTask)).thenReturn(updatedOpenmrsTask); when(dao.get(TASK_UUID)).thenReturn(openmrsTask); - when(translator.toFhirResource(updatedOpenmrsTask)).thenReturn(fhirTask); + when(translator.toFhirResource(updatedOpenmrsTask, null)).thenReturn(fhirTask); org.hl7.fhir.r4.model.Task result = fhirTaskService.update(TASK_UUID, fhirTask); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java index 7759e950c..bd888f7b4 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/AllergyIntoleranceTranslatorImplTest.java @@ -17,6 +17,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import java.util.Collections; import java.util.Date; @@ -121,9 +122,9 @@ public void setUp() { omrsAllergy.setAllergen(allergen); } - @Test(expected = NullPointerException.class) - public void toFhirResource_shouldThrowExceptionWhenCalledWithANullObject() { - allergyIntoleranceTranslator.toFhirResource(null); + @Test + public void toFhirResource_shouldReturnNullWhenCalledWithANullObject() { + assertThat(allergyIntoleranceTranslator.toFhirResource(null), nullValue()); } @Test @@ -156,9 +157,11 @@ public void toFhirResource_shouldTranslateAllergyIntoleranceCategoryFoodCorrectl Allergen allergen = new Allergen(); allergen.setAllergenType(AllergenType.FOOD); omrsAllergy.setAllergen(allergen); - when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType())) + when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType(), null)) .thenReturn(AllergyIntolerance.AllergyIntoleranceCategory.FOOD); + AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy); + assertThat(allergyIntolerance, notNullValue()); assertThat(allergyIntolerance.getCategory().get(0).getValue(), equalTo(AllergyIntolerance.AllergyIntoleranceCategory.FOOD)); @@ -169,9 +172,11 @@ public void toFhirResource_shouldTranslateAllergyIntoleranceCategoryMedicationCo Allergen allergen = new Allergen(); allergen.setAllergenType(AllergenType.DRUG); omrsAllergy.setAllergen(allergen); - when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType())) + when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType(), null)) .thenReturn(AllergyIntolerance.AllergyIntoleranceCategory.MEDICATION); + AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy); + assertThat(allergyIntolerance, notNullValue()); assertThat(allergyIntolerance.getCategory().get(0).getValue(), equalTo(AllergyIntolerance.AllergyIntoleranceCategory.MEDICATION)); @@ -182,9 +187,11 @@ public void toFhirResource_shouldTranslateAllergyIntoleranceCategoryEnvironmentC Allergen allergen = new Allergen(); allergen.setAllergenType(AllergenType.ENVIRONMENT); omrsAllergy.setAllergen(allergen); - when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType())) + when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType(), null)) .thenReturn(AllergyIntolerance.AllergyIntoleranceCategory.ENVIRONMENT); + AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy); + assertThat(allergyIntolerance, notNullValue()); assertThat(allergyIntolerance.getCategory().get(0).getValue(), equalTo(AllergyIntolerance.AllergyIntoleranceCategory.ENVIRONMENT)); @@ -195,8 +202,10 @@ public void toFhirResource_shouldReturnNullCategoryWhenCalledWithOpenMrsOtherCat Allergen allergen = new Allergen(); allergen.setAllergenType(AllergenType.OTHER); omrsAllergy.setAllergen(allergen); - when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType())).thenReturn(null); + when(categoryTranslator.toFhirResource(omrsAllergy.getAllergen().getAllergenType(), null)).thenReturn(null); + AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy); + assertThat(allergyIntolerance.getCategory().get(0).getValue(), nullValue()); } @@ -227,12 +236,13 @@ public void toFhirResource_shouldTranslateOmrsPatientToFhirPatientReference() { Reference patientReference = new Reference().setReference(FhirTestConstants.PATIENT + "/" + PATIENT_UUID) .setType(FhirTestConstants.PATIENT).setIdentifier(new Identifier().setValue(PATIENT_UUID)); - when(patientReferenceTranslator.toFhirResource(patient)).thenReturn(patientReference); + when(patientReferenceTranslator.toFhirResource(patient, null)).thenReturn(patientReference); + AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy); + assertThat(allergyIntolerance.getPatient(), notNullValue()); assertThat(allergyIntolerance.getPatient().getType(), is(FhirTestConstants.PATIENT)); - assertThat(allergyIntoleranceTranslator.getReferenceId(allergyIntolerance.getPatient()).orElse(null), - equalTo(PATIENT_UUID)); + assertThat(getReferenceId(allergyIntolerance.getPatient()).orElse(null), equalTo(PATIENT_UUID)); } @Test @@ -244,14 +254,13 @@ public void toFhirResource_shouldTranslateOmrsAllergyCreatorToFhirPractitionerRe Reference practionerReference = new Reference().setReference(FhirTestConstants.PRACTITIONER + "/" + CREATOR_UUID) .setType(FhirTestConstants.PRACTITIONER).setIdentifier(new Identifier().setValue(CREATOR_UUID)); - when(practitionerReferenceTranslator.toFhirResource(user)).thenReturn(practionerReference); + when(practitionerReferenceTranslator.toFhirResource(user, null)).thenReturn(practionerReference); AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy); assertThat(allergyIntolerance.getRecorder(), notNullValue()); assertThat(allergyIntolerance.getRecorder().getType(), is(FhirTestConstants.PRACTITIONER)); - assertThat(allergyIntoleranceTranslator.getReferenceId(allergyIntolerance.getRecorder()).orElse(null), - equalTo(CREATOR_UUID)); + assertThat(getReferenceId(allergyIntolerance.getRecorder()).orElse(null), equalTo(CREATOR_UUID)); } @Test @@ -292,7 +301,7 @@ public void toFhirResource_shouldTranslateToHighCriticality() { severeConcept.setUuid(GLOBAL_PROPERTY_SEVERE_VALUE); omrsAllergy.setSeverity(severeConcept); - when(severityTranslator.toFhirResource(severeConcept)) + when(severityTranslator.toFhirResource(severeConcept, null)) .thenReturn(AllergyIntolerance.AllergyIntoleranceSeverity.SEVERE); when(criticalityTranslator.toFhirResource(AllergyIntolerance.AllergyIntoleranceSeverity.SEVERE)) .thenReturn(AllergyIntolerance.AllergyIntoleranceCriticality.HIGH); @@ -307,11 +316,13 @@ public void toFhirResource_shouldTranslateToLowCriticality() { Concept mildConcept = new Concept(); mildConcept.setUuid(GLOBAL_PROPERTY_MILD_VALUE); omrsAllergy.setSeverity(mildConcept); - when(severityTranslator.toFhirResource(mildConcept)).thenReturn(AllergyIntolerance.AllergyIntoleranceSeverity.MILD); + when(severityTranslator.toFhirResource(mildConcept, null)) + .thenReturn(AllergyIntolerance.AllergyIntoleranceSeverity.MILD); when(criticalityTranslator.toFhirResource(AllergyIntolerance.AllergyIntoleranceSeverity.MILD)) .thenReturn(AllergyIntolerance.AllergyIntoleranceCriticality.LOW); AllergyIntolerance allergyIntolerance = allergyIntoleranceTranslator.toFhirResource(omrsAllergy); + assertThat(allergyIntolerance, notNullValue()); assertThat(allergyIntolerance.getCriticality(), equalTo(AllergyIntolerance.AllergyIntoleranceCriticality.LOW)); } @@ -321,7 +332,8 @@ public void toFhirResource_shouldTranslateToNullCriticality() { Concept otherConcept = new Concept(); otherConcept.setUuid(GLOBAL_PROPERTY_OTHER_VALUE); omrsAllergy.setSeverity(otherConcept); - when(severityTranslator.toFhirResource(otherConcept)).thenReturn(AllergyIntolerance.AllergyIntoleranceSeverity.NULL); + when(severityTranslator.toFhirResource(otherConcept, null)) + .thenReturn(AllergyIntolerance.AllergyIntoleranceSeverity.NULL); when(criticalityTranslator.toFhirResource(AllergyIntolerance.AllergyIntoleranceSeverity.NULL)) .thenReturn(AllergyIntolerance.AllergyIntoleranceCriticality.NULL); @@ -338,9 +350,9 @@ public void toFhirResource_shouldTranslateCommentToNote() { assertThat(allergyIntolerance.getNote().get(0).getText(), equalTo("")); } - @Test(expected = NullPointerException.class) - public void toOpenmrsType_shouldThrowExceptionIfAllergyIntoleranceIsNull() { - allergyIntoleranceTranslator.toOpenmrsType(omrsAllergy, null); + @Test + public void toOpenmrsType_shouldReturnNullIfAllergyIntoleranceIsNull() { + assertThat(allergyIntoleranceTranslator.toOpenmrsType(omrsAllergy, null), nullValue()); } @Test @@ -546,20 +558,22 @@ public void shouldAddProvenances() { allergy.setUuid(ALLERGY_UUID); Concept concept = new Concept(); concept.setUuid(CONCEPT_UUID); + Allergen allergen = new Allergen(); allergen.setCodedAllergen(concept); allergen.setNonCodedAllergen("Test Allergen"); allergen.setAllergenType(AllergenType.FOOD); - allergy.setAllergen(allergen); + Provenance provenance = new Provenance(); provenance.setId(new IdType(FhirUtils.newUuid())); provenance.setRecorded(new Date()); - when(provenanceTranslator.getCreateProvenance(allergy)).thenReturn(provenance); - when(provenanceTranslator.getUpdateProvenance(allergy)).thenReturn(provenance); + when(provenanceTranslator.getCreateProvenance(allergy, null)).thenReturn(provenance); + when(provenanceTranslator.getUpdateProvenance(allergy, null)).thenReturn(provenance); AllergyIntolerance result = allergyIntoleranceTranslator.toFhirResource(allergy); List resources = result.getContained(); + assertThat(resources, notNullValue()); assertThat(resources, not(empty())); assertThat(resources.stream().findAny().isPresent(), CoreMatchers.is(true)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslatorTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslatorTest.java index 02bc63a9e..f45fda131 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslatorTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseEncounterTranslatorTest.java @@ -46,7 +46,7 @@ public void shouldMapLocationToClass() { location.setUuid(LOCATION_UUID); when(encounterClassMap.getFhirClass(LOCATION_UUID)).thenReturn("AMB"); - Coding result = baseEncounterTranslator.mapLocationToClass(location); + Coding result = baseEncounterTranslator.mapLocationToClass(location, null); assertThat(result, notNullValue()); assertThat(result, notNullValue()); @@ -56,7 +56,7 @@ public void shouldMapLocationToClass() { @Test public void shouldMapLocationToAMBCodeWhenLocationIsNull() { - Coding result = baseEncounterTranslator.mapLocationToClass(null); + Coding result = baseEncounterTranslator.mapLocationToClass(null, null); assertThat(result, notNullValue()); assertThat(result.getSystem(), is(FhirConstants.ENCOUNTER_CLASS_VALUE_SET_URI)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseReferenceHandlingTranslatorTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseReferenceHandlingTranslatorTest.java index 07aa47057..ce8bb3bf8 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseReferenceHandlingTranslatorTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/BaseReferenceHandlingTranslatorTest.java @@ -93,12 +93,8 @@ public class BaseReferenceHandlingTranslatorTest { private org.openmrs.Encounter encounter; - private BaseReferenceHandlingTranslator referenceHandlingTranslator; - @Before public void setUp() { - referenceHandlingTranslator = new BaseReferenceHandlingTranslator() {}; - patient = new Patient(); patient.setUuid(PATIENT_UUID); @@ -138,61 +134,61 @@ public void setUp() { @Test public void shouldExtractIdFromReference() { - assertThat(referenceHandlingTranslator.getReferenceId(new Reference().setReference(PATIENT_URI)).orElse(null), + assertThat(ReferenceHandlingTranslator.getReferenceId(new Reference().setReference(PATIENT_URI)).orElse(null), equalTo(PATIENT_UUID)); } @Test public void shouldReturnNullIdForNullReference() { - assertThat(referenceHandlingTranslator.getReferenceId(new Reference()).orElse(null), nullValue()); + assertThat(ReferenceHandlingTranslator.getReferenceId(new Reference()).orElse(null), nullValue()); } @Test public void shouldNotExtractIdWhenIdMissingFromReference() { - assertThat(referenceHandlingTranslator.getReferenceId(new Reference().setReference(FhirConstants.PATIENT + "/")) + assertThat(ReferenceHandlingTranslator.getReferenceId(new Reference().setReference(FhirConstants.PATIENT + "/")) .orElse(null), nullValue()); } @Test public void shouldReturnNullIdWhenNoSlashFound() { - assertThat(referenceHandlingTranslator.getReferenceId(new Reference().setReference(PATIENT_UUID)).orElse(null), + assertThat(ReferenceHandlingTranslator.getReferenceId(new Reference().setReference(PATIENT_UUID)).orElse(null), nullValue()); } @Test public void shouldExtractReferenceTypeFromReference() { - assertThat(referenceHandlingTranslator.getReferenceType(new Reference().setReference(PATIENT_URI)).orElse(null), + assertThat(ReferenceHandlingTranslator.getReferenceType(new Reference().setReference(PATIENT_URI)).orElse(null), equalTo(FhirConstants.PATIENT)); } @Test public void shouldReturnNullTypeForNullReference() { - assertThat(referenceHandlingTranslator.getReferenceType(new Reference()).orElse(null), nullValue()); + assertThat(ReferenceHandlingTranslator.getReferenceType(new Reference()).orElse(null), nullValue()); } @Test public void shouldReturnNullTypeWhenTypeMissing() { assertThat( - referenceHandlingTranslator.getReferenceType(new Reference().setReference("/" + PATIENT_UUID)).orElse(null), + ReferenceHandlingTranslator.getReferenceType(new Reference().setReference("/" + PATIENT_UUID)).orElse(null), nullValue()); } @Test public void shouldReturnNullTypeWhenNoSlashFound() { - assertThat(referenceHandlingTranslator.getReferenceType(new Reference().setReference(PATIENT_UUID)).orElse(null), + assertThat(ReferenceHandlingTranslator.getReferenceType(new Reference().setReference(PATIENT_UUID)).orElse(null), nullValue()); } @Test public void shouldUseExplicitType() { - assertThat(referenceHandlingTranslator.getReferenceType(new Reference().setType(FhirConstants.PATIENT)).orElse(null), + assertThat(ReferenceHandlingTranslator.getReferenceType(new Reference().setType(FhirConstants.PATIENT)).orElse(null), equalTo(FhirConstants.PATIENT)); } @Test public void shouldReturnPatientReference() { - Reference reference = referenceHandlingTranslator.createPatientReference(patient); + Reference reference = ReferenceHandlingTranslator.createPatientReference(patient); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(PATIENT_URI)); @@ -201,7 +197,7 @@ public void shouldReturnPatientReference() { @Test public void shouldReturnPatientReferenceWithCorrectDisplayName() { - Reference reference = referenceHandlingTranslator.createPatientReference(patient); + Reference reference = ReferenceHandlingTranslator.createPatientReference(patient); assertThat(reference, notNullValue()); assertThat(reference.getDisplay(), equalTo(NAME_DISPLAY)); @@ -209,7 +205,7 @@ public void shouldReturnPatientReferenceWithCorrectDisplayName() { @Test public void shouldAddPractitionerReference() { - Reference reference = referenceHandlingTranslator.createPractitionerReference(provider); + Reference reference = ReferenceHandlingTranslator.createPractitionerReference(provider); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(PROVIDER_URI)); @@ -226,7 +222,7 @@ public void shouldReturnReferenceWithDisplayForProviderWithPerson() { person.addName(name); provider.setPerson(person); - Reference reference = referenceHandlingTranslator.createPractitionerReference(provider); + Reference reference = ReferenceHandlingTranslator.createPractitionerReference(provider); assertThat(reference, notNullValue()); assertThat(reference.getDisplay(), equalTo(PROVIDER_DISPLAY)); @@ -234,7 +230,7 @@ public void shouldReturnReferenceWithDisplayForProviderWithPerson() { @Test public void shouldReturnNullDisplayForPractitionerWithNullPerson() { - Reference reference = referenceHandlingTranslator.createPractitionerReference(provider); + Reference reference = ReferenceHandlingTranslator.createPractitionerReference(provider); assertThat(reference, notNullValue()); assertThat(reference.getDisplay(), nullValue()); @@ -242,7 +238,7 @@ public void shouldReturnNullDisplayForPractitionerWithNullPerson() { @Test public void shouldAddLocationReference() { - Reference reference = referenceHandlingTranslator.createLocationReference(location); + Reference reference = ReferenceHandlingTranslator.createLocationReference(location); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(LOCATION_URI)); @@ -254,7 +250,7 @@ public void shouldAddLocationReference() { public void shouldReturnNullDisplayWhenLocationNameIsNull() { location.setName(null); - Reference reference = referenceHandlingTranslator.createLocationReference(location); + Reference reference = ReferenceHandlingTranslator.createLocationReference(location); assertThat(reference, notNullValue()); assertThat(reference.getDisplay(), nullValue()); @@ -262,7 +258,7 @@ public void shouldReturnNullDisplayWhenLocationNameIsNull() { @Test public void shouldAddEncounterReference() { - Reference reference = referenceHandlingTranslator.createEncounterReference(encounter); + Reference reference = ReferenceHandlingTranslator.createEncounterReference(encounter); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(ENCOUNTER_URI)); @@ -271,7 +267,7 @@ public void shouldAddEncounterReference() { @Test public void shouldAddPractitionerGivenOpenMrsUserReference() { - Reference reference = referenceHandlingTranslator.createPractitionerReference(user); + Reference reference = ReferenceHandlingTranslator.createPractitionerReference(user); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(PRACTITIONER_REFERENCE)); @@ -284,7 +280,7 @@ public void shouldReturnReferenceWithNullDisplayIfUserPersonNameIsNull() { User user = new User(); user.setUuid(USER_UUID); - Reference reference = referenceHandlingTranslator.createPractitionerReference(user); + Reference reference = ReferenceHandlingTranslator.createPractitionerReference(user); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(PRACTITIONER_REFERENCE)); @@ -296,7 +292,7 @@ public void shouldAddOrderReferenceForTestOrder() { TestOrder order = new TestOrder(); order.setUuid(ORDER_UUID); - Reference reference = referenceHandlingTranslator.createOrderReference(order); + Reference reference = ReferenceHandlingTranslator.createOrderReference(order); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(TEST_ORDER_REFERENCE)); @@ -308,7 +304,7 @@ public void shouldAddOrderReferenceForTestOrderSubclass() { TestOrder order = new TestOrder() {}; order.setUuid(ORDER_UUID); - Reference reference = referenceHandlingTranslator.createOrderReference(order); + Reference reference = ReferenceHandlingTranslator.createOrderReference(order); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(TEST_ORDER_REFERENCE)); @@ -320,7 +316,7 @@ public void shouldAddOrderReferenceForDrugOrder() { DrugOrder order = new DrugOrder(); order.setUuid(ORDER_UUID); - Reference reference = referenceHandlingTranslator.createOrderReference(order); + Reference reference = ReferenceHandlingTranslator.createOrderReference(order); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(DRUG_ORDER_REFERENCE)); @@ -332,7 +328,7 @@ public void shouldAddOrderReferenceForDrugOrderSubclass() { DrugOrder order = new DrugOrder() {}; order.setUuid(ORDER_UUID); - Reference reference = referenceHandlingTranslator.createOrderReference(order); + Reference reference = ReferenceHandlingTranslator.createOrderReference(order); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(DRUG_ORDER_REFERENCE)); @@ -344,7 +340,7 @@ public void shouldReturnNullForRawOrder() { Order order = new Order(); order.setUuid(ORDER_UUID); - Reference reference = referenceHandlingTranslator.createOrderReference(order); + Reference reference = ReferenceHandlingTranslator.createOrderReference(order); assertThat(reference, nullValue()); } @@ -354,14 +350,14 @@ public void shouldReturnNullForUnknownOrderSubclass() { Order order = new Order() {}; order.setUuid(ORDER_UUID); - Reference reference = referenceHandlingTranslator.createOrderReference(order); + Reference reference = ReferenceHandlingTranslator.createOrderReference(order); assertThat(reference, nullValue()); } @Test public void shouldReturnLocationReferenceForUuid() { - Reference reference = referenceHandlingTranslator.createLocationReferenceByUuid(LOCATION_UUID); + Reference reference = ReferenceHandlingTranslator.createLocationReferenceByUuid(LOCATION_UUID); assertThat(reference, notNullValue()); assertThat(reference.getReference(), equalTo(LOCATION_URI)); assertThat(reference.getType(), equalTo(FhirConstants.LOCATION)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImplTest.java index 985db84ec..b9158332b 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConceptTranslatorImplTest.java @@ -108,7 +108,7 @@ public void shouldTranslateLOINCMappingForLOINCMappedConcept() { loincConceptSource.setName("LOINC"); loinc.setConceptSource(loincConceptSource); loinc.setUrl(FhirTestConstants.LOINC_SYSTEM_URL); - when(conceptSourceService.getFhirConceptSourceByConceptSourceName("LOINC")).thenReturn(Optional.of(loinc)); + when(conceptSourceService.getFhirConceptSourceByConceptSourceName("LOINC", null)).thenReturn(Optional.of(loinc)); CodeableConcept result = conceptTranslator.toFhirResource(concept); @@ -138,7 +138,7 @@ public void shouldTranslateCIELMappingForCIELMappedConcept() { cielConceptSource.setName("CIEL"); ciel.setConceptSource(cielConceptSource); ciel.setUrl(FhirTestConstants.CIEL_SYSTEM_URN); - when(conceptSourceService.getFhirConceptSourceByConceptSourceName("CIEL")).thenReturn(Optional.of(ciel)); + when(conceptSourceService.getFhirConceptSourceByConceptSourceName("CIEL", null)).thenReturn(Optional.of(ciel)); CodeableConcept result = conceptTranslator.toFhirResource(concept); assertThat(result, notNullValue()); @@ -159,7 +159,7 @@ public void shouldNotTranslateUnknownMappingCode() { when(conceptReferenceTerm.getConceptSource()).thenReturn(conceptSource); when(conceptSource.getName()).thenReturn("Unknown"); when(concept.getConceptMappings()).thenReturn(conceptMaps); - when(conceptSourceService.getFhirConceptSourceByConceptSourceName("Unknown")).thenReturn(Optional.empty()); + when(conceptSourceService.getFhirConceptSourceByConceptSourceName("Unknown", null)).thenReturn(Optional.empty()); CodeableConcept result = conceptTranslator.toFhirResource(concept); assertThat(result, notNullValue()); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java index afe6d0ed7..5d41c7771 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java @@ -172,8 +172,10 @@ public void shouldTranslateConditionSubjectToOpenMrsType() { @Test public void shouldTranslateConditionPatientToFhirType() { - when(patientReferenceTranslator.toFhirResource(patient)).thenReturn(patientRef); + when(patientReferenceTranslator.toFhirResource(patient, null)).thenReturn(patientRef); + Condition condition = conditionTranslator.toFhirResource(openmrsCondition); + assertThat(condition, notNullValue()); assertThat(condition.getSubject(), notNullValue()); assertThat(condition.getSubject(), equalTo(patientRef)); @@ -183,7 +185,9 @@ public void shouldTranslateConditionPatientToFhirType() { @Test public void shouldTranslateOpenMrsConditionOnsetDateToFhirType() { openmrsCondition.setObsDatetime(new Date()); + org.hl7.fhir.r4.model.Condition condition = conditionTranslator.toFhirResource(openmrsCondition); + assertThat(condition, notNullValue()); assertThat(condition.getOnsetDateTimeType().getValue(), notNullValue()); assertThat(condition.getOnsetDateTimeType().getValue(), DateMatchers.sameDay(new Date())); @@ -192,10 +196,13 @@ public void shouldTranslateOpenMrsConditionOnsetDateToFhirType() { @Test public void shouldTranslateFhirConditionOnsetToOpenMrsOnsetDate() { when(conceptService.getConceptByUuid(FhirConstants.CONDITION_OBSERVATION_CONCEPT_UUID)).thenReturn(concept); + DateTimeType theDateTime = new DateTimeType(); theDateTime.setValue(new Date()); fhirCondition.setOnset(theDateTime); + Obs condition = conditionTranslator.toOpenmrsType(fhirCondition); + assertThat(condition, notNullValue()); assertThat(condition.getObsDatetime(), notNullValue()); assertThat(condition.getObsDatetime(), DateMatchers.sameDay(new Date())); @@ -225,14 +232,17 @@ public void shouldTranslateConditionConceptToFhirType() { Concept concept = new Concept(); concept.setUuid(CONCEPT_UUID); concept.setConceptId(CODE); + CodeableConcept codeableConcept = new CodeableConcept(); Coding coding = new Coding(); coding.setCode(CODE.toString()); coding.setSystem(SYSTEM); codeableConcept.addCoding(coding); openmrsCondition.setValueCoded(concept); - when(conceptTranslator.toFhirResource(concept)).thenReturn(codeableConcept); + when(conceptTranslator.toFhirResource(concept, null)).thenReturn(codeableConcept); + org.hl7.fhir.r4.model.Condition condition = conditionTranslator.toFhirResource(openmrsCondition); + assertThat(condition, notNullValue()); assertThat(condition.getCode(), notNullValue()); assertThat(condition.getCode().getCoding(), not(Collections.emptyList())); @@ -243,7 +253,9 @@ public void shouldTranslateConditionConceptToFhirType() { @Test public void shouldTranslateConditionDateCreatedToRecordedDateFhirType() { openmrsCondition.setDateCreated(new Date()); + org.hl7.fhir.r4.model.Condition condition = conditionTranslator.toFhirResource(openmrsCondition); + assertThat(condition, notNullValue()); assertThat(condition.getRecordedDate(), notNullValue()); assertThat(condition.getRecordedDate(), DateMatchers.sameDay(new Date())); @@ -271,8 +283,10 @@ public void shouldTranslateConditionCreatorToRecorderFhirType() { Reference userRef = new Reference(); userRef.setReference(PRACTITIONER_REFERENCE); openmrsCondition.setCreator(user); - when(creatorReferenceTranslator.toFhirResource(user)).thenReturn(userRef); + when(creatorReferenceTranslator.toFhirResource(user, null)).thenReturn(userRef); + org.hl7.fhir.r4.model.Condition condition = conditionTranslator.toFhirResource(openmrsCondition); + assertThat(condition, notNullValue()); assertThat(condition.getRecorder(), notNullValue()); assertThat(condition.getRecorder().getReference(), equalTo(PRACTITIONER_REFERENCE)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImplTest.java index 3c2fad051..dda4ca770 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DiagnosticReportTranslatorImplTest.java @@ -90,7 +90,7 @@ public void setup() { diagnosticReport.addResult(obsReference); // Mocks for DiagnosticReport.result - when(observationReferenceTranslator.toFhirResource(childObs)).thenReturn(obsReference); + when(observationReferenceTranslator.toFhirResource(childObs, null)).thenReturn(obsReference); when(observationReferenceTranslator.toOpenmrsType(obsReference)).thenReturn(childObs); } @@ -126,7 +126,7 @@ public void toFhirResource_shouldConvertSubject() { Reference subjectReference = new Reference(); fhirDiagnosticReport.setSubject(subject); - when(patientReferenceTranslator.toFhirResource(subject)).thenReturn(subjectReference); + when(patientReferenceTranslator.toFhirResource(subject, null)).thenReturn(subjectReference); DiagnosticReport result = translator.toFhirResource(fhirDiagnosticReport); @@ -139,7 +139,7 @@ public void toFhirResource_shouldConvertEncounter() { Reference encounterReference = new Reference(); fhirDiagnosticReport.setEncounter(encounter); - when(encounterReferenceTranslator.toFhirResource(encounter)).thenReturn(encounterReference); + when(encounterReferenceTranslator.toFhirResource(encounter, null)).thenReturn(encounterReference); DiagnosticReport result = translator.toFhirResource(fhirDiagnosticReport); @@ -152,7 +152,7 @@ public void toFhirResource_shouldConvertCode() { CodeableConcept translatedCode = new CodeableConcept(); fhirDiagnosticReport.setCode(code); - when(conceptTranslator.toFhirResource(code)).thenReturn(translatedCode); + when(conceptTranslator.toFhirResource(code, null)).thenReturn(translatedCode); DiagnosticReport result = translator.toFhirResource(fhirDiagnosticReport); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImplTest.java index f854840aa..f5fce8014 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/DosageTranslatorImplTest.java @@ -82,9 +82,10 @@ public void toFhirResource_shouldTranslateDrugOrderAsNeededToAsNeeded() { CodeableConcept codeableConcept = new CodeableConcept(); codeableConcept.addCoding(new Coding().setCode(concept.getConceptId().toString())); drugOrder.setRoute(concept); - when(conceptTranslator.toFhirResource(concept)).thenReturn(codeableConcept); + when(conceptTranslator.toFhirResource(concept, null)).thenReturn(codeableConcept); Dosage result = dosageTranslator.toFhirResource(drugOrder); + assertThat(result, notNullValue()); assertThat(result.getRoute(), equalTo(codeableConcept)); assertThat(result.getRoute().getCodingFirstRep().getCode(), equalTo("1000")); @@ -112,9 +113,10 @@ public void toFhirResource_shouldSetDosageTiming() { Timing timing = new Timing(); timing.addEvent(new Date()); timing.setRepeat(repeatComponent); - when(timingTranslator.toFhirResource(drugOrder)).thenReturn(timing); + when(timingTranslator.toFhirResource(drugOrder, null)).thenReturn(timing); Dosage result = dosageTranslator.toFhirResource(drugOrder); + assertThat(result, notNullValue()); assertThat(result.getTiming(), notNullValue()); assertThat(result.getTiming().getEvent(), not(empty())); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImplTest.java index 232b11c4b..025622a32 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterReferenceTranslatorImplTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Reference; @@ -51,7 +52,7 @@ public void toFhirResource_shouldConvertEncounterToReference() { assertThat(result, notNullValue()); assertThat(result.getType(), equalTo(FhirConstants.ENCOUNTER)); - assertThat(encounterReferenceTranslator.getReferenceId(result).orElse(null), equalTo(ENCOUNTER_UUID)); + assertThat(getReferenceId(result).orElse(null), equalTo(ENCOUNTER_UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImplTest.java index 777828e47..6ae1743d5 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/EncounterTranslatorImplTest.java @@ -190,9 +190,9 @@ public void toOpenMrsType_shouldTranslateIdToUuid() { assertThat(result.getUuid(), equalTo(ENCOUNTER_UUID)); } - @Test(expected = NullPointerException.class) - public void toFhirResource_shouldThrowExceptionWhenOpenMrsEncounterIsNull() { - encounterTranslator.toFhirResource(null); + @Test + public void toFhirResource_shouldReturnNullWhenOpenMrsEncounterIsNull() { + assertThat(encounterTranslator.toFhirResource(null), nullValue()); } @Test @@ -221,20 +221,25 @@ public void toOpenMrsType_shouldTranslateToPatient() { public void toFhirResource_shouldTranslateToFhirSubjectAsReference() { PatientIdentifier identifier = new PatientIdentifier(); identifier.setIdentifier(PATIENT_IDENTIFIER); + PatientIdentifierType identifierType = new PatientIdentifierType(); identifierType.setName(TEST_IDENTIFIER_TYPE_NAME); identifier.setIdentifierType(identifierType); + PersonName name = new PersonName(); name.setGivenName(GIVEN_NAME); name.setFamilyName(FAMILY_NAME); + Patient patient = new Patient(); patient.setUuid(PATIENT_UUID); patient.addIdentifier(identifier); patient.addName(name); omrsEncounter.setUuid(ENCOUNTER_UUID); omrsEncounter.setPatient(patient); - when(patientReferenceTranslator.toFhirResource(patient)).thenReturn(patientRef); + when(patientReferenceTranslator.toFhirResource(patient, null)).thenReturn(patientRef); + Encounter result = encounterTranslator.toFhirResource(omrsEncounter); + assertThat(result, notNullValue()); assertThat(result.getSubject(), notNullValue()); assertThat(result.getSubject().getReference(), equalTo(PATIENT_URI)); @@ -249,13 +254,16 @@ public void toOpenMrsType_shouldTranslateParticipantToEncounterProvider() { participantComponent.setIndividual(practitionerRef); participantComponents.add(participantComponent); fhirEncounter.setParticipant(participantComponents); + EncounterProvider encounterProvider = new EncounterProvider(); Provider provider = new Provider(); provider.setUuid(PRACTITIONER_UUID); encounterProvider.setProvider(provider); + Patient patient = new Patient(); patient.setUuid(PATIENT_UUID); when(patientReferenceTranslator.toOpenmrsType(patientRef)).thenReturn(patient); + org.openmrs.Encounter result = encounterTranslator.toOpenmrsType(fhirEncounter); assertThat(result.getEncounterProviders(), not(empty())); @@ -270,14 +278,16 @@ public void toFhirResource_shouldTranslateToFhirParticipant() { provider.setUuid(PRACTITIONER_UUID); encounterProvider.setProvider(provider); providerList.add(encounterProvider); + Encounter.EncounterParticipantComponent participantComponent = new Encounter.EncounterParticipantComponent(); Reference practitionerRef = new Reference(); practitionerRef.setReference(PRACTITIONER_URI); participantComponent.setIndividual(practitionerRef); omrsEncounter.setEncounterProviders(providerList); - when(participantTranslator.toFhirResource(encounterProvider)).thenReturn(participantComponent); + when(participantTranslator.toFhirResource(encounterProvider, null)).thenReturn(participantComponent); Encounter result = encounterTranslator.toFhirResource(omrsEncounter); + assertThat(result, notNullValue()); assertThat(result.getParticipant(), notNullValue()); assertThat(result.getParticipant().size(), equalTo(1)); @@ -298,13 +308,16 @@ public void toFhirResource_shouldTranslateLocationToEncounterLocationComponent() @Test public void toFhirResource_shouldTranslateLocationToEncounterLocationWithCorrectReference() { omrsEncounter.setLocation(location); + Encounter.EncounterLocationComponent locationComponent = new Encounter.EncounterLocationComponent(); Reference locationRef = new Reference(); locationRef.setReference(LOCATION_URI); locationRef.setDisplay(TEST_LOCATION_NAME); locationComponent.setLocation(locationRef); - when(encounterLocationTranslator.toFhirResource(location)).thenReturn(locationComponent); + when(encounterLocationTranslator.toFhirResource(location, null)).thenReturn(locationComponent); + Encounter result = encounterTranslator.toFhirResource(omrsEncounter); + assertThat(result, notNullValue()); assertThat(result.getLocation(), notNullValue()); assertThat(result.getLocation().size(), is(1)); @@ -321,11 +334,14 @@ public void toOpenMrsType_shouldTranslateLocationToEncounterLocation() { locationComponent.setLocation(locationRef); locationComponents.add(locationComponent); fhirEncounter.setLocation(locationComponents); + Patient patient = new Patient(); patient.setUuid(PATIENT_UUID); when(patientReferenceTranslator.toOpenmrsType(patientRef)).thenReturn(patient); when(encounterLocationTranslator.toOpenmrsType(locationComponent)).thenReturn(location); + org.openmrs.Encounter result = encounterTranslator.toOpenmrsType(fhirEncounter); + assertThat(result, notNullValue()); assertThat(result.getLocation(), notNullValue()); assertThat(result.getLocation().getName(), equalTo(TEST_LOCATION_NAME)); @@ -397,7 +413,7 @@ public void toFhirResource_shouldTranslateToPartOf() { Reference reference = new Reference(); reference.setReference(VISIT_URI); - when(visitReferenceTranslator.toFhirResource(visit)).thenReturn(reference); + when(visitReferenceTranslator.toFhirResource(visit, null)).thenReturn(reference); Encounter result = encounterTranslator.toFhirResource(encounter); assertThat(result, notNullValue()); @@ -442,11 +458,12 @@ public void toFhirResource_shouldAddProvenances() { Provenance provenance = new Provenance(); provenance.setId(new IdType(FhirUtils.newUuid())); provenance.setRecorded(new Date()); - when(provenanceTranslator.getCreateProvenance(encounter)).thenReturn(provenance); - when(provenanceTranslator.getUpdateProvenance(encounter)).thenReturn(provenance); + when(provenanceTranslator.getCreateProvenance(encounter, null)).thenReturn(provenance); + when(provenanceTranslator.getUpdateProvenance(encounter, null)).thenReturn(provenance); Encounter result = encounterTranslator.toFhirResource(encounter); List resources = result.getContained(); + assertThat(resources, notNullValue()); assertThat(resources, not(empty())); assertThat(resources.stream().findAny().isPresent(), is(true)); @@ -483,7 +500,7 @@ public void toFhirResource_shouldTranslateLocationToEncounterDefaultClass() { public void toFhirResource_shouldTranslateEncounterTypeToEncounterTypeField() { CodeableConcept fhirEncounterType = new CodeableConcept(); fhirEncounterType.addCoding().setSystem(FhirConstants.ENCOUNTER_TYPE_SYSTEM_URI).setCode("1"); - when(encounterTypeTranslator.toFhirResource(ArgumentMatchers.any())) + when(encounterTypeTranslator.toFhirResource(ArgumentMatchers.any(), ArgumentMatchers.any())) .thenReturn(Collections.singletonList(fhirEncounterType)); EncounterType omrsEncounterType = new EncounterType(); @@ -516,7 +533,7 @@ public void toFhirResource_shouldTranslateEncounterDatetimeToPeriod() { period.setStart(encounterDate); - when(encounterPeriodTranslator.toFhirResource(ArgumentMatchers.any())).thenReturn(period); + when(encounterPeriodTranslator.toFhirResource(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(period); Encounter result = encounterTranslator.toFhirResource(omrsEncounter); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImplTest.java index c313b9a1d..a57a5ad17 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/LocationTranslatorImplTest.java @@ -21,6 +21,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import java.math.BigDecimal; import java.util.ArrayList; @@ -328,7 +329,7 @@ public void toFhirResource_shouldTranslateOpenmrsParentLocationToFhirReference() assertThat(locationReference, notNullValue()); assertThat(locationReference.getType(), is(FhirConstants.LOCATION)); assertThat(locationReference.getDisplay(), is(PARENT_LOCATION_NAME)); - assertThat(locationTranslator.getReferenceId(locationReference).orElse(null), equalTo(PARENT_LOCATION_UUID)); + assertThat(getReferenceId(locationReference).orElse(null), equalTo(PARENT_LOCATION_UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImplTest.java index 2c5c7cda7..092334a81 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/MedicationReferenceTranslatorImplTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import org.hl7.fhir.r4.model.Reference; import org.junit.Before; @@ -50,7 +51,7 @@ public void toFhirResource_shouldConvertDrugToReference() { assertThat(result, notNullValue()); assertThat(result.getType(), equalTo(FhirConstants.MEDICATION)); - assertThat(medicationReferenceTranslator.getReferenceId(result).orElse(null), equalTo(MEDICATION_UUID)); + assertThat(getReferenceId(result).orElse(null), equalTo(MEDICATION_UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImplTest.java index 7bbbd04e0..67612162b 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ObservationReferenceTranslatorImplTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Reference; @@ -51,7 +52,7 @@ public void toFhirResource_shouldConvertObsToReference() { assertThat(result, notNullValue()); assertThat(result.getType(), equalTo(FhirConstants.OBSERVATION)); - assertThat(observationReferenceTranslator.getReferenceId(result).orElse(null), equalTo(UUID)); + assertThat(getReferenceId(result).orElse(null), equalTo(UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImplTest.java index 9c6fcc394..20188fc71 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientReferenceTranslatorImplTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Reference; @@ -51,7 +52,7 @@ public void toFhirResource_shouldConvertEncounterToReference() { assertThat(result, notNullValue()); assertThat(result.getType(), equalTo(FhirConstants.PATIENT)); - assertThat(patientReferenceTranslator.getReferenceId(result).orElse(null), equalTo(PATIENT_UUID)); + assertThat(getReferenceId(result).orElse(null), equalTo(PATIENT_UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java index 9d16d41bb..8d8cbfda6 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PatientTranslatorImplTest.java @@ -45,6 +45,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.openmrs.BaseOpenmrsData; @@ -185,8 +186,8 @@ public void shouldTranslateDeadPatientWithDeathDateToPatientWithDeathDate() { public void shouldTranslatePatientIdentifierToFhirIdentifier() { Identifier id = new Identifier(); id.setValue(PATIENT_IDENTIFIER_UUID); - when(identifierTranslator.toFhirResource(argThat(hasProperty("uuid", equalTo(PATIENT_IDENTIFIER_UUID))))) - .thenReturn(id); + when(identifierTranslator.toFhirResource(argThat(hasProperty("uuid", equalTo(PATIENT_IDENTIFIER_UUID))), + ArgumentMatchers.any())).thenReturn(id); org.openmrs.Patient patient = new org.openmrs.Patient(); PatientIdentifier patientIdentifier = new PatientIdentifier(); @@ -194,6 +195,7 @@ public void shouldTranslatePatientIdentifierToFhirIdentifier() { patient.addIdentifier(patientIdentifier); Patient result = patientTranslator.toFhirResource(patient); + assertThat(result.getIdentifier(), not(empty())); assertThat(result.getIdentifier(), hasItem(hasProperty("value", equalTo(PATIENT_IDENTIFIER_UUID)))); } @@ -204,7 +206,7 @@ public void shouldTranslateOpenmrsPatientNameToFhirPatientName() { humanName.addGiven(PATIENT_GIVEN_NAME); humanName.setFamily(PATIENT_FAMILY_NAME); when(nameTranslator.toFhirResource(argThat(allOf(hasProperty("givenName", equalTo(PATIENT_GIVEN_NAME)), - hasProperty("familyName", equalTo(PATIENT_FAMILY_NAME)))))).thenReturn(humanName); + hasProperty("familyName", equalTo(PATIENT_FAMILY_NAME)))), ArgumentMatchers.any())).thenReturn(humanName); org.openmrs.Patient patient = new org.openmrs.Patient(); PersonName name = new PersonName(); @@ -213,6 +215,7 @@ public void shouldTranslateOpenmrsPatientNameToFhirPatientName() { patient.setNames(Sets.newHashSet(name)); Patient result = patientTranslator.toFhirResource(patient); + assertThat(result.getName(), not(empty())); assertThat(result.getName().get(0), notNullValue()); assertThat(result.getName().get(0).getGivenAsSingleString(), equalTo(PATIENT_GIVEN_NAME)); @@ -221,7 +224,8 @@ public void shouldTranslateOpenmrsPatientNameToFhirPatientName() { @Test public void shouldTranslateOpenMRSPatientGenderToFhirGender() { - when(genderTranslator.toFhirResource(argThat(equalTo("M")))).thenReturn(Enumerations.AdministrativeGender.MALE); + when(genderTranslator.toFhirResource(argThat(equalTo("M")), ArgumentMatchers.any())) + .thenReturn(Enumerations.AdministrativeGender.MALE); org.openmrs.Patient patient = new org.openmrs.Patient(); patient.setGender("M"); @@ -235,7 +239,8 @@ public void shouldTranslateOpenMRSAddressToFhirAddress() { Address address = new Address(); address.setId(ADDRESS_UUID); address.setCity(ADDRESS_CITY); - when(addressTranslator.toFhirResource(argThat(hasProperty("uuid", equalTo(ADDRESS_UUID))))).thenReturn(address); + when(addressTranslator.toFhirResource(argThat(hasProperty("uuid", equalTo(ADDRESS_UUID))), ArgumentMatchers.any())) + .thenReturn(address); org.openmrs.Patient patient = new org.openmrs.Patient(); PersonAddress personAddress = new PersonAddress(); @@ -244,6 +249,7 @@ public void shouldTranslateOpenMRSAddressToFhirAddress() { patient.addAddress(personAddress); Patient result = patientTranslator.toFhirResource(patient); + assertThat(result.getAddress(), not(empty())); assertThat(result.getAddress(), hasItem(hasProperty("id", equalTo(ADDRESS_UUID)))); assertThat(result.getAddress(), hasItem(hasProperty("city", equalTo(ADDRESS_CITY)))); @@ -387,11 +393,14 @@ public void shouldTranslateOpenMrsDateChangedToLastUpdatedDate() { public void shouldAddProvenanceResources() { org.openmrs.Patient patient = new org.openmrs.Patient(); patient.setUuid(PATIENT_UUID); + Provenance provenance = new Provenance(); provenance.setId(new IdType(FhirUtils.newUuid())); - when(provenanceTranslator.getCreateProvenance(patient)).thenReturn(provenance); - when(provenanceTranslator.getUpdateProvenance(patient)).thenReturn(provenance); + when(provenanceTranslator.getCreateProvenance(patient, null)).thenReturn(provenance); + when(provenanceTranslator.getUpdateProvenance(patient, null)).thenReturn(provenance); + org.hl7.fhir.r4.model.Patient result = patientTranslator.toFhirResource(patient); + assertThat(result, notNullValue()); assertThat(result.getContained(), not(empty())); assertThat(result.getContained().size(), greaterThanOrEqualTo(2)); @@ -409,10 +418,11 @@ public void shouldNotAddUpdateProvenanceIfDateChangedAndChangedByAreBothNull() { patient.setUuid(PATIENT_UUID); patient.setDateChanged(null); patient.setChangedBy(null); - when(provenanceTranslator.getCreateProvenance(patient)).thenReturn(provenance); - when(provenanceTranslator.getUpdateProvenance(patient)).thenReturn(null); + when(provenanceTranslator.getCreateProvenance(patient, null)).thenReturn(provenance); + when(provenanceTranslator.getUpdateProvenance(patient, null)).thenReturn(null); org.hl7.fhir.r4.model.Patient result = patientTranslator.toFhirResource(patient); + assertThat(result, notNullValue()); assertThat(result.getContained(), not(empty())); assertThat(result.getContained().size(), equalTo(1)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImplTest.java index 013ef9be1..3786d32f1 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PersonTranslatorImplTest.java @@ -40,6 +40,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.openmrs.BaseOpenmrsData; @@ -176,7 +177,8 @@ public void shouldTranslateOpenmrsGenderToFhirGenderType() { personAttribute.setValue(PERSON_ATTRIBUTE_VALUE); personAttribute.setAttributeType(attributeType); - when(genderTranslator.toFhirResource(argThat(equalTo("F")))).thenReturn(Enumerations.AdministrativeGender.FEMALE); + when(genderTranslator.toFhirResource(argThat(equalTo("F")), ArgumentMatchers.any())) + .thenReturn(Enumerations.AdministrativeGender.FEMALE); person.setGender("F"); org.hl7.fhir.r4.model.Person result = personTranslator.toFhirResource(person); @@ -269,7 +271,7 @@ public void shouldTranslateOpenmrsPersonNameToFhirPersonName() { humanName.addGiven(PERSON_GIVEN_NAME); humanName.setFamily(PERSON_FAMILY_NAME); when(nameTranslator.toFhirResource(argThat(allOf(hasProperty("givenName", equalTo(PERSON_GIVEN_NAME)), - hasProperty("familyName", equalTo(PERSON_FAMILY_NAME)))))).thenReturn(humanName); + hasProperty("familyName", equalTo(PERSON_FAMILY_NAME)))), ArgumentMatchers.any())).thenReturn(humanName); Person person = new Person(); PersonName name = new PersonName(); @@ -291,8 +293,8 @@ public void shouldTranslateOpenmrsAddressToFhirAddress() { address.setId(ADDRESS_UUID); address.setCity(ADDRESS_CITY); when(addressTranslator.toFhirResource( - argThat(allOf(hasProperty("uuid", equalTo(ADDRESS_UUID)), hasProperty("cityVillage", equalTo(ADDRESS_CITY)))))) - .thenReturn(address); + argThat(allOf(hasProperty("uuid", equalTo(ADDRESS_UUID)), hasProperty("cityVillage", equalTo(ADDRESS_CITY)))), + ArgumentMatchers.any())).thenReturn(address); Person person = new Person(); PersonAddress personAddress = new PersonAddress(); @@ -480,9 +482,11 @@ public void shouldTranslateOpenMrsDateChangedToLastUpdatedDate() { public void shouldAddProvenanceResources() { Provenance provenance = new Provenance(); provenance.setId(new IdType(FhirUtils.newUuid())); - when(provenanceTranslator.getCreateProvenance(personMock)).thenReturn(provenance); - when(provenanceTranslator.getUpdateProvenance(personMock)).thenReturn(provenance); + when(provenanceTranslator.getCreateProvenance(personMock, null)).thenReturn(provenance); + when(provenanceTranslator.getUpdateProvenance(personMock, null)).thenReturn(provenance); + org.hl7.fhir.r4.model.Person result = personTranslator.toFhirResource(personMock); + assertThat(result, notNullValue()); assertThat(result.getContained(), not(empty())); assertThat(result.getContained().size(), greaterThanOrEqualTo(2)); @@ -497,10 +501,11 @@ public void shouldNotAddUpdateProvenanceIfDateChangedAndChangedByAreBothNull() { provenance.setId(new IdType(FhirUtils.newUuid())); personMock.setDateChanged(null); personMock.setChangedBy(null); - when(provenanceTranslator.getCreateProvenance(personMock)).thenReturn(provenance); - when(provenanceTranslator.getUpdateProvenance(personMock)).thenReturn(null); + when(provenanceTranslator.getCreateProvenance(personMock, null)).thenReturn(provenance); + when(provenanceTranslator.getUpdateProvenance(personMock, null)).thenReturn(null); org.hl7.fhir.r4.model.Person result = personTranslator.toFhirResource(personMock); + assertThat(result, notNullValue()); assertThat(result.getContained(), not(empty())); assertThat(result.getContained().size(), equalTo(1)); diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImplTest.java index 3b30dd4f3..b97484e3a 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorProviderImplTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import org.hamcrest.Matchers; import org.hl7.fhir.r4.model.Practitioner; @@ -54,7 +55,7 @@ public void shouldConvertProviderToFhirPractitionerReference() { Reference result = referenceTranslatorProvider.toFhirResource(provider); assertThat(result, notNullValue()); assertThat(result.getType(), equalTo(FhirConstants.PRACTITIONER)); - assertThat(referenceTranslatorProvider.getReferenceId(result).orElse(null), equalTo(PRACTITIONER_UUID)); + assertThat(getReferenceId(result).orElse(null), equalTo(PRACTITIONER_UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImplTest.java index 522e5e984..563a449a8 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/PractitionerReferenceTranslatorUserImplTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import org.hamcrest.Matchers; import org.hl7.fhir.r4.model.Practitioner; @@ -54,7 +55,7 @@ public void shouldConvertUserToFhirPractitionerReference() { Reference result = practitionerReferenceTranslatorUser.toFhirResource(user); assertThat(result, notNullValue()); assertThat(result.getType(), equalTo(FhirConstants.PRACTITIONER)); - assertThat(practitionerReferenceTranslatorUser.getReferenceId(result).orElse(null), equalTo(USER_UUID)); + assertThat(getReferenceId(result).orElse(null), equalTo(USER_UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImplTest.java index 94c4a613d..3880320c5 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitReferenceTranslatorImplTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.when; +import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId; import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Reference; @@ -51,7 +52,7 @@ public void toFhirResource_shouldConvertVisitToReference() { assertThat(result, notNullValue()); assertThat(result.getType(), equalTo(FhirConstants.ENCOUNTER)); - assertThat(visitReferenceTranslator.getReferenceId(result).orElse(null), equalTo(VISIT_UUID)); + assertThat(getReferenceId(result).orElse(null), equalTo(VISIT_UUID)); } @Test diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImplTest.java index 9caa4986a..cc0219557 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/VisitTranslatorImplTest.java @@ -177,9 +177,9 @@ public void toOpenmrsType_shouldConvertPeriodWithNullValues() { assertThat(result.getStopDatetime(), nullValue()); } - @Test(expected = NullPointerException.class) - public void toFhirResource_shouldThrowExceptionWhenVisitIsNull() { - visitTranslator.toFhirResource(null); + @Test + public void toFhirResource_shouldReturnNullWhenVisitIsNull() { + assertThat(visitTranslator.toFhirResource(null), nullValue()); } @Test(expected = NullPointerException.class) diff --git a/integration-tests/src/test/java/org/openmrs/module/fhir2/BaseFhirIntegrationTest.java b/integration-tests/src/test/java/org/openmrs/module/fhir2/BaseFhirIntegrationTest.java index 35444cdaa..8f61c6f1b 100644 --- a/integration-tests/src/test/java/org/openmrs/module/fhir2/BaseFhirIntegrationTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/fhir2/BaseFhirIntegrationTest.java @@ -38,7 +38,9 @@ import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.instance.model.api.IDomainResource; +import org.junit.After; import org.junit.Before; +import org.openmrs.module.fhir2.api.util.FhirCache; import org.openmrs.module.fhir2.web.servlet.FhirRestServlet; import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; @@ -66,6 +68,9 @@ public abstract class BaseFhirIntegrationTest com.github.ben-manes.caffeine caffeine - 2.8.2 + 2.9.2 junit