diff --git a/api/pom.xml b/api/pom.xml
index 33d0cc313..6e7cb22b1 100644
--- a/api/pom.xml
+++ b/api/pom.xml
@@ -17,10 +17,18 @@
ca.uhn.hapi.fhir
hapi-fhir-base
+
+ ca.uhn.hapi.fhir
+ hapi-fhir-structures-dstu3
+
ca.uhn.hapi.fhir
hapi-fhir-structures-r4
+
+ ca.uhn.hapi.fhir
+ hapi-fhir-converter
+
ca.uhn.hapi.fhir
hapi-fhir-server
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirResourceProvider.java
new file mode 100644
index 000000000..c041e20bc
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirResourceProvider.java
@@ -0,0 +1,94 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import ca.uhn.fhir.rest.annotation.History;
+import ca.uhn.fhir.rest.annotation.IdParam;
+import ca.uhn.fhir.rest.annotation.OptionalParam;
+import ca.uhn.fhir.rest.annotation.Read;
+import ca.uhn.fhir.rest.annotation.Search;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.AllergyIntolerance30_40;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Provenance30_40;
+import org.hl7.fhir.dstu3.model.AllergyIntolerance;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Patient;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.hl7.fhir.r4.model.Provenance;
+import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("allergyIntoleranceFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class AllergyIntoleranceFhirResourceProvider implements IResourceProvider {
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return AllergyIntolerance.class;
+ }
+
+ @Autowired
+ private FhirAllergyIntoleranceService allergyIntoleranceService;
+
+ @Read
+ @SuppressWarnings("unused")
+ public AllergyIntolerance getAllergyIntoleranceById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = allergyIntoleranceService.get(id.getIdPart());
+ if (allergyIntolerance == null) {
+ throw new ResourceNotFoundException("Could not find allergyIntolerance with Id " + id.getIdPart());
+ }
+
+ return AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getAllergyIntoleranceHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = allergyIntoleranceService.get(id.getIdPart());
+ if (allergyIntolerance == null) {
+ throw new ResourceNotFoundException("Could not find allergy with Id " + id.getIdPart());
+ }
+
+ return allergyIntolerance.getContained().stream().filter(r -> r instanceof Provenance).map(r -> (Provenance) r)
+ .map(Provenance30_40::convertProvenance).collect(Collectors.toList());
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle searchForAllergies(
+ @OptionalParam(name = AllergyIntolerance.SP_PATIENT, chainWhitelist = { "", Patient.SP_IDENTIFIER,
+ Patient.SP_GIVEN, Patient.SP_FAMILY,
+ Patient.SP_NAME }, targetTypes = Patient.class) ReferenceAndListParam patientReference,
+ @OptionalParam(name = AllergyIntolerance.SP_CATEGORY) TokenAndListParam category,
+ @OptionalParam(name = AllergyIntolerance.SP_CODE) TokenAndListParam allergen,
+ @OptionalParam(name = AllergyIntolerance.SP_SEVERITY) TokenAndListParam severity,
+ @OptionalParam(name = AllergyIntolerance.SP_MANIFESTATION) TokenAndListParam manifestationCode,
+ @OptionalParam(name = AllergyIntolerance.SP_CLINICAL_STATUS) TokenAndListParam clinicalStatus) {
+ return Bundle30_40.convertBundle(FhirProviderUtils.convertSearchResultsToBundle(allergyIntoleranceService
+ .searchForAllergies(patientReference, category, allergen, severity, manifestationCode, clinicalStatus)));
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirResourceProvider.java
new file mode 100644
index 000000000..6ab87bf23
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirResourceProvider.java
@@ -0,0 +1,93 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.MethodOutcome;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.QuantityAndListParam;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Condition30_40;
+import org.hl7.fhir.dstu3.model.*;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirConditionService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("conditionFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class ConditionFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirConditionService conditionService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Condition.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Condition getConditionById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Condition condition = conditionService.getConditionByUuid(id.getIdPart());
+ if (condition == null) {
+ throw new ResourceNotFoundException("Could not find condition with Id " + id.getIdPart());
+ }
+
+ return Condition30_40.convertCondition(condition);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getConditionHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Condition condition = conditionService.getConditionByUuid(id.getIdPart());
+ if (condition == null) {
+ throw new ResourceNotFoundException("Could not find condition with Id " + id.getIdPart());
+ }
+ return Condition30_40.convertCondition(condition).getContained();
+ }
+
+ @Create
+ @SuppressWarnings("unused")
+ public MethodOutcome createCondition(@ResourceParam Condition newCondition) {
+ return FhirProviderUtils.buildCreate(conditionService.saveCondition(Condition30_40.convertCondition(newCondition)));
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle searchConditions(
+ @OptionalParam(name = Condition.SP_PATIENT, chainWhitelist = { "", Patient.SP_IDENTIFIER, Patient.SP_NAME,
+ Patient.SP_GIVEN, Patient.SP_FAMILY }) ReferenceAndListParam patientParam,
+ @OptionalParam(name = Condition.SP_SUBJECT, chainWhitelist = { "", Patient.SP_IDENTIFIER, Patient.SP_NAME,
+ Patient.SP_GIVEN, Patient.SP_FAMILY }) ReferenceAndListParam subjectParam,
+ @OptionalParam(name = Condition.SP_CODE) TokenAndListParam code,
+ @OptionalParam(name = Condition.SP_CLINICAL_STATUS) TokenAndListParam clinicalStatus,
+ @OptionalParam(name = Condition.SP_ONSET_DATE) DateRangeParam onsetDate,
+ @OptionalParam(name = Condition.SP_ONSET_AGE) QuantityAndListParam onsetAge,
+ @OptionalParam(name = Condition.SP_ASSERTED_DATE) DateRangeParam recordedDate, @Sort SortSpec sort) {
+ return Bundle30_40
+ .convertBundle(FhirProviderUtils.convertSearchResultsToBundle(conditionService.searchConditions(patientParam,
+ subjectParam, code, clinicalStatus, onsetDate, onsetAge, recordedDate, sort)));
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProvider.java
new file mode 100644
index 000000000..4072c9379
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProvider.java
@@ -0,0 +1,71 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.MethodOutcome;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.DiagnosticReport30_40;
+import org.hl7.fhir.dstu3.model.DiagnosticReport;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirDiagnosticReportService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("diagnosticReportFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class DiagnosticReportFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirDiagnosticReportService diagnosticReportService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return DiagnosticReport.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public DiagnosticReport getDiagnosticReportById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.DiagnosticReport diagnosticReport = diagnosticReportService.get(id.getIdPart());
+ if (diagnosticReport == null) {
+ throw new ResourceNotFoundException("Could not find diagnosticReport with Id " + id.getIdPart());
+ }
+
+ return DiagnosticReport30_40.convertDiagnosticReport(diagnosticReport);
+ }
+
+ @Create
+ public MethodOutcome createDiagnosticReport(@ResourceParam DiagnosticReport diagnosticReport) {
+ return FhirProviderUtils.buildCreate(
+ diagnosticReportService.create(DiagnosticReport30_40.convertDiagnosticReport(diagnosticReport)));
+ }
+
+ @Update
+ public MethodOutcome updateDiagnosticReport(@IdParam IdType id, @ResourceParam DiagnosticReport diagnosticReport) {
+ String idPart = null;
+
+ if (id != null) {
+ idPart = id.getIdPart();
+ }
+
+ return FhirProviderUtils.buildUpdate(
+ diagnosticReportService.update(idPart, DiagnosticReport30_40.convertDiagnosticReport(diagnosticReport)));
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProvider.java
new file mode 100644
index 000000000..096e4670c
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProvider.java
@@ -0,0 +1,83 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Encounter30_40;
+import org.hl7.fhir.dstu3.model.*;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirEncounterService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("encounterFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class EncounterFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirEncounterService encounterService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Encounter.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Encounter getEncounterById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Encounter encounter = encounterService.get(id.getIdPart());
+ if (encounter == null) {
+ throw new ResourceNotFoundException("Could not find encounter with Id " + id.getIdPart());
+ }
+
+ return Encounter30_40.convertEncounter(encounter);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getEncounterHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Encounter encounter = encounterService.get(id.getIdPart());
+ if (encounter == null) {
+ throw new ResourceNotFoundException("Could not find encounter with Id " + id.getIdPart());
+ }
+ return Encounter30_40.convertEncounter(encounter).getContained();
+ }
+
+ @Search
+ public Bundle searchEncounter(@OptionalParam(name = Encounter.SP_DATE) DateRangeParam date,
+ @OptionalParam(name = Encounter.SP_LOCATION, chainWhitelist = { "", Location.SP_ADDRESS_CITY,
+ Location.SP_ADDRESS_STATE, Location.SP_ADDRESS_COUNTRY,
+ Location.SP_ADDRESS_POSTALCODE }, targetTypes = Location.class) ReferenceAndListParam location,
+ @OptionalParam(name = Encounter.SP_PARTICIPANT, chainWhitelist = { "", Practitioner.SP_IDENTIFIER,
+ Practitioner.SP_GIVEN, Practitioner.SP_FAMILY,
+ Practitioner.SP_NAME }, targetTypes = Practitioner.class) ReferenceAndListParam participantReference,
+ @OptionalParam(name = Encounter.SP_SUBJECT, chainWhitelist = { "", Patient.SP_IDENTIFIER, Patient.SP_GIVEN,
+ Patient.SP_FAMILY,
+ Patient.SP_NAME }, targetTypes = Patient.class) ReferenceAndListParam subjectReference) {
+ return Bundle30_40.convertBundle(FhirProviderUtils.convertSearchResultsToBundle(
+ encounterService.searchForEncounters(date, location, participantReference, subjectReference)));
+
+ }
+
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProvider.java
new file mode 100644
index 000000000..e5e055c6e
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProvider.java
@@ -0,0 +1,53 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import ca.uhn.fhir.rest.annotation.IdParam;
+import ca.uhn.fhir.rest.annotation.Read;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.List30_40;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.ListResource;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.Cohort;
+import org.openmrs.module.fhir2.api.FhirListService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("listFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class ListFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirListService listService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return ListResource.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public ListResource getListById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.ListResource listResource = listService.get(id.getIdPart());
+ if (listResource == null) {
+ throw new ResourceNotFoundException("Could not find list with Id " + id.getIdPart());
+ }
+
+ return List30_40.convertList(listResource);
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProvider.java
new file mode 100644
index 000000000..e9a0df425
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProvider.java
@@ -0,0 +1,83 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Location30_40;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Location;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirLocationService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("locationFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class LocationFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirLocationService locationService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Location.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Location getLocationById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Location location = locationService.get(id.getIdPart());
+ if (location == null) {
+ throw new ResourceNotFoundException("Could not find location with Id " + id.getIdPart());
+ }
+
+ return Location30_40.convertLocation(location);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getLocationHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Location location = locationService.get(id.getIdPart());
+ if (location == null) {
+ throw new ResourceNotFoundException("Could not find location with Id " + id.getIdPart());
+ }
+ return Location30_40.convertLocation(location).getContained();
+ }
+
+ @Search
+ public Bundle searchLocations(@OptionalParam(name = Location.SP_NAME) StringAndListParam name,
+ @OptionalParam(name = Location.SP_ADDRESS_CITY) StringAndListParam city,
+ @OptionalParam(name = Location.SP_ADDRESS_COUNTRY) StringAndListParam country,
+ @OptionalParam(name = Location.SP_ADDRESS_POSTALCODE) StringAndListParam postalCode,
+ @OptionalParam(name = Location.SP_ADDRESS_STATE) StringAndListParam state,
+ @OptionalParam(name = "_tag") TokenAndListParam tag,
+ @OptionalParam(name = Location.SP_PARTOF) ReferenceAndListParam parent, @Sort SortSpec sort) {
+ return Bundle30_40.convertBundle(FhirProviderUtils.convertSearchResultsToBundle(
+ locationService.searchForLocations(name, city, country, postalCode, state, tag, parent, sort)));
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProvider.java
new file mode 100644
index 000000000..9b3645b89
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProvider.java
@@ -0,0 +1,96 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.MethodOutcome;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Medication30_40;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Medication;
+import org.hl7.fhir.dstu3.model.OperationOutcome;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirMedicationService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("medicationFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class MedicationFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirMedicationService medicationService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Medication.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Medication getMedicationById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Medication medication = medicationService.get(id.getIdPart());
+ if (medication == null) {
+ throw new ResourceNotFoundException("Could not find medication with Id " + id.getIdPart());
+ }
+
+ return Medication30_40.convertMedication(medication);
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle searchForMedication(@OptionalParam(name = Medication.SP_CODE) TokenAndListParam code,
+ @OptionalParam(name = Medication.SP_FORM) TokenAndListParam dosageForm,
+ @OptionalParam(name = Medication.SP_STATUS) TokenAndListParam status) {
+ return Bundle30_40.convertBundle(FhirProviderUtils
+ .convertSearchResultsToBundle(medicationService.searchForMedications(code, dosageForm, null, status)));
+ }
+
+ @Create
+ @SuppressWarnings("unused")
+ public MethodOutcome createMedication(@ResourceParam Medication medication) {
+ return FhirProviderUtils.buildCreate(medicationService.create(Medication30_40.convertMedication(medication)));
+ }
+
+ @Update
+ @SuppressWarnings("unused")
+ public MethodOutcome updateMedication(@IdParam IdType id, @ResourceParam Medication medication) {
+ if (id != null) {
+ medication.setId(id.getIdPart());
+ }
+
+ return FhirProviderUtils
+ .buildUpdate(medicationService.update(id.getIdPart(), Medication30_40.convertMedication(medication)));
+ }
+
+ @Delete
+ @SuppressWarnings("unused")
+ public OperationOutcome deleteMedication(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Medication medication = medicationService.delete(id.getIdPart());
+ if (medication == null) {
+ throw new ResourceNotFoundException("Could not find medication to update with id " + id.getIdPart());
+ }
+ OperationOutcome retVal = new OperationOutcome();
+ retVal.setId(id.getIdPart());
+ retVal.getText().setDivAsString("Deleted successfully");
+ return retVal;
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProvider.java
new file mode 100644
index 000000000..811ac25ea
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProvider.java
@@ -0,0 +1,52 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import ca.uhn.fhir.rest.annotation.IdParam;
+import ca.uhn.fhir.rest.annotation.Read;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.MedicationRequest30_40;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.MedicationRequest;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirMedicationRequestService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("medicationRequestFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class MedicationRequestFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirMedicationRequestService medicationRequestService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return MedicationRequest.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public MedicationRequest getMedicationRequestById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.MedicationRequest medicationRequest = medicationRequestService.get(id.getIdPart());
+ if (medicationRequest == null) {
+ throw new ResourceNotFoundException("Could not find medicationRequest with Id " + id.getIdPart());
+ }
+
+ return MedicationRequest30_40.convertMedicationRequest(medicationRequest);
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProvider.java
new file mode 100644
index 000000000..799c7305d
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProvider.java
@@ -0,0 +1,83 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.api.server.IBundleProvider;
+import ca.uhn.fhir.rest.param.*;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Observation30_40;
+import org.hl7.fhir.dstu3.model.*;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirObservationService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("observationFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class ObservationFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirObservationService observationService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Observation.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Observation getObservationById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Observation observation = observationService.get(id.getIdPart());
+ if (observation == null) {
+ throw new ResourceNotFoundException("Could not find observation with Id " + id.getIdPart());
+ }
+
+ return Observation30_40.convertObservation(observation);
+ }
+
+ @History
+ public List getObservationHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Observation observation = observationService.get(id.getIdPart());
+ if (observation == null) {
+ throw new ResourceNotFoundException("Could not find Observation with Id " + id.getIdPart());
+ }
+ return Observation30_40.convertObservation(observation).getContained();
+ }
+
+ @Search
+ public IBundleProvider searchObservations(
+ @OptionalParam(name = Observation.SP_ENCOUNTER) ReferenceAndListParam encounterReference,
+ @OptionalParam(name = Observation.SP_SUBJECT, chainWhitelist = { "", Patient.SP_IDENTIFIER, Patient.SP_GIVEN,
+ Patient.SP_FAMILY,
+ Patient.SP_NAME }, targetTypes = Patient.class) ReferenceAndListParam patientReference,
+ @OptionalParam(name = Observation.SP_RELATED_TYPE, chainWhitelist = { "",
+ Observation.SP_CODE }, targetTypes = Observation.class) ReferenceParam hasMemberReference,
+ @OptionalParam(name = Observation.SP_VALUE_CONCEPT) TokenAndListParam valueConcept,
+ @OptionalParam(name = Observation.SP_VALUE_DATE) DateRangeParam valueDateParam,
+ @OptionalParam(name = Observation.SP_VALUE_QUANTITY) QuantityAndListParam valueQuantityParam,
+ @OptionalParam(name = Observation.SP_VALUE_STRING) StringAndListParam valueStringParam,
+ @OptionalParam(name = Observation.SP_DATE) DateRangeParam date,
+ @OptionalParam(name = Observation.SP_CODE) TokenAndListParam code, @Sort SortSpec sort) {
+ return observationService.searchForObservations(encounterReference, patientReference, hasMemberReference,
+ valueConcept, valueDateParam, valueQuantityParam, valueStringParam, date, code, sort);
+ }
+
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PatientFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PatientFhirResourceProvider.java
new file mode 100644
index 000000000..d979db20a
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PatientFhirResourceProvider.java
@@ -0,0 +1,92 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Patient30_40;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Patient;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirPatientService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("patientFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class PatientFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirPatientService patientService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Patient.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Patient getPatientById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Patient patient = patientService.get(id.getIdPart());
+ if (patient == null) {
+ throw new ResourceNotFoundException("Could not find patient with Id " + id.getIdPart());
+ }
+
+ return Patient30_40.convertPatient(patient);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getPatientHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Patient patient = patientService.get(id.getIdPart());
+ if (patient == null) {
+ throw new ResourceNotFoundException("Could not find patient with Id " + id.getIdPart());
+ }
+
+ return Patient30_40.convertPatient(patient).getContained();
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle searchPatients(@OptionalParam(name = Patient.SP_NAME) StringAndListParam name,
+ @OptionalParam(name = Patient.SP_GIVEN) StringAndListParam given,
+ @OptionalParam(name = Patient.SP_FAMILY) StringAndListParam family,
+ @OptionalParam(name = Patient.SP_IDENTIFIER) TokenAndListParam identifier,
+ @OptionalParam(name = Patient.SP_GENDER) TokenAndListParam gender,
+ @OptionalParam(name = Patient.SP_BIRTHDATE) DateRangeParam birthDate,
+ @OptionalParam(name = Patient.SP_DEATH_DATE) DateRangeParam deathDate,
+ @OptionalParam(name = Patient.SP_DECEASED) TokenAndListParam deceased,
+ @OptionalParam(name = Patient.SP_ADDRESS_CITY) StringAndListParam city,
+ @OptionalParam(name = Patient.SP_ADDRESS_STATE) StringAndListParam state,
+ @OptionalParam(name = Patient.SP_ADDRESS_POSTALCODE) StringAndListParam postalCode,
+ @OptionalParam(name = Patient.SP_ADDRESS_COUNTRY) StringAndListParam country, @Sort SortSpec sort) {
+ return Bundle30_40
+ .convertBundle(FhirProviderUtils.convertSearchResultsToBundle(patientService.searchForPatients(name, given,
+ family, identifier, gender, birthDate, deathDate, deceased, city, state, postalCode, country, sort)));
+ }
+
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProvider.java
new file mode 100644
index 000000000..9496030e4
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProvider.java
@@ -0,0 +1,85 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Person30_40;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Person;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirPersonService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("personFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class PersonFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirPersonService personService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Person.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Person getPersonById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Person person = personService.get(id.getIdPart());
+ if (person == null) {
+ throw new ResourceNotFoundException("Could not find person with Id " + id.getIdPart());
+ }
+
+ return Person30_40.convertPerson(person);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getPersonHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Person person = personService.get(id.getIdPart());
+ if (person == null) {
+ throw new ResourceNotFoundException("Could not find person with Id " + id.getIdPart());
+ }
+ return Person30_40.convertPerson(person).getContained();
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle searchPeople(@OptionalParam(name = Person.SP_NAME) StringAndListParam name,
+ @OptionalParam(name = Person.SP_GENDER) TokenAndListParam gender,
+ @OptionalParam(name = Person.SP_BIRTHDATE) DateRangeParam birthDate,
+ @OptionalParam(name = Person.SP_ADDRESS_CITY) StringAndListParam city,
+ @OptionalParam(name = Person.SP_ADDRESS_STATE) StringAndListParam state,
+ @OptionalParam(name = Person.SP_ADDRESS_POSTALCODE) StringAndListParam postalCode,
+ @OptionalParam(name = Person.SP_ADDRESS_COUNTRY) StringAndListParam country, @Sort SortSpec sort) {
+ return Bundle30_40.convertBundle(FhirProviderUtils.convertSearchResultsToBundle(
+ personService.searchForPeople(name, gender, birthDate, city, state, postalCode, country, sort)));
+ }
+
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProvider.java
new file mode 100644
index 000000000..aa71d0bee
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProvider.java
@@ -0,0 +1,83 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.*;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.convertors.conv30_40.Practitioner30_40;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Practitioner;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirPractitionerService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("practitionerFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class PractitionerFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirPractitionerService practitionerService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Practitioner.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Practitioner getPractitionerById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Practitioner practitioner = practitionerService.getPractitionerByUuid(id.getIdPart());
+ if (practitioner == null) {
+ throw new ResourceNotFoundException("Could not find practitioner with Id " + id.getIdPart());
+ }
+
+ return Practitioner30_40.convertPractitioner(practitioner);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getPractitionerHistoryById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Practitioner practitioner = practitionerService.getPractitionerByUuid(id.getIdPart());
+ if (practitioner == null) {
+ throw new ResourceNotFoundException("Could not find practitioner with Id " + id.getIdPart());
+ }
+ return Practitioner30_40.convertPractitioner(practitioner).getContained();
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle findPractitionersByName(@RequiredParam(name = Practitioner.SP_NAME) @NotNull String name) {
+ return Bundle30_40.convertBundle(
+ FhirProviderUtils.convertSearchResultsToBundle(practitionerService.findPractitionerByName(name)));
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle findPractitionersByIdentifier(
+ @RequiredParam(name = Practitioner.SP_IDENTIFIER) @NotNull String identifier) {
+ return Bundle30_40.convertBundle(
+ FhirProviderUtils.convertSearchResultsToBundle(practitionerService.findPractitionerByIdentifier(identifier)));
+ }
+
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProvider.java
new file mode 100644
index 000000000..69d723306
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProvider.java
@@ -0,0 +1,51 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import ca.uhn.fhir.rest.annotation.IdParam;
+import ca.uhn.fhir.rest.annotation.Read;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.VersionConvertor_30_40;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.ProcedureRequest;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirServiceRequestService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("procedureRequestFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class ProcedureRequestFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirServiceRequestService serviceRequestService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return ProcedureRequest.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public ProcedureRequest getProcedureRequestById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.ServiceRequest serviceRequest = serviceRequestService.get(id.getIdPart());
+ if (serviceRequest == null) {
+ throw new ResourceNotFoundException("Could not find serviceRequest with Id " + id.getIdPart());
+ }
+ return (ProcedureRequest) VersionConvertor_30_40.convertResource(serviceRequest, false);
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProvider.java
new file mode 100644
index 000000000..c46b6fcc4
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProvider.java
@@ -0,0 +1,51 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import ca.uhn.fhir.rest.annotation.IdParam;
+import ca.uhn.fhir.rest.annotation.Read;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.RelatedPerson30_40;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.RelatedPerson;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirRelatedPersonService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("relatedPersonFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class RelatedPersonFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirRelatedPersonService relatedPersonService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return RelatedPerson.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public RelatedPerson getRelatedPersonById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.RelatedPerson relatedPerson = relatedPersonService.get(id.getIdPart());
+ if (relatedPerson == null) {
+ throw new ResourceNotFoundException("Could not find relatedPerson with Id " + id.getIdPart());
+ }
+ return RelatedPerson30_40.convertRelatedPerson(relatedPerson);
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProvider.java
new file mode 100644
index 000000000..96a603d8b
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProvider.java
@@ -0,0 +1,101 @@
+/*
+ * 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.providers.r3;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.annotation.Create;
+import ca.uhn.fhir.rest.annotation.History;
+import ca.uhn.fhir.rest.annotation.IdParam;
+import ca.uhn.fhir.rest.annotation.OptionalParam;
+import ca.uhn.fhir.rest.annotation.Read;
+import ca.uhn.fhir.rest.annotation.ResourceParam;
+import ca.uhn.fhir.rest.annotation.Search;
+import ca.uhn.fhir.rest.annotation.Sort;
+import ca.uhn.fhir.rest.annotation.Update;
+import ca.uhn.fhir.rest.api.MethodOutcome;
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.param.ReferenceParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Setter;
+import org.hl7.fhir.convertors.conv30_40.Bundle30_40;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.dstu3.model.Task;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.api.FhirTaskService;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
+import org.openmrs.module.fhir2.providers.util.TaskVersionConverter;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component("taskFhirR3ResourceProvider")
+@Qualifier("fhirR3Resources")
+@Setter(AccessLevel.PACKAGE)
+public class TaskFhirResourceProvider implements IResourceProvider {
+
+ @Autowired
+ private FhirTaskService fhirTaskService;
+
+ @Override
+ public Class extends IBaseResource> getResourceType() {
+ return Task.class;
+ }
+
+ @Read
+ @SuppressWarnings("unused")
+ public Task getTaskById(@IdParam @NotNull IdType id) {
+ org.hl7.fhir.r4.model.Task task = fhirTaskService.get(id.getIdPart());
+ if (task == null) {
+ throw new ResourceNotFoundException("Could not find task with Id " + id.getIdPart());
+ }
+
+ return TaskVersionConverter.convertTask(task);
+ }
+
+ @History
+ @SuppressWarnings("unused")
+ public List getTaskHistoryById(@IdParam IdType id) {
+ org.hl7.fhir.r4.model.Task task = fhirTaskService.get(id.getIdPart());
+ if (task == null) {
+ throw new ResourceNotFoundException("Could not find Task with Id " + id.getIdPart());
+ }
+
+ return TaskVersionConverter.convertTask(task).getContained();
+ }
+
+ @Create
+ @SuppressWarnings("unused")
+ public MethodOutcome createTask(@ResourceParam Task newTask) {
+ return FhirProviderUtils.buildCreate(fhirTaskService.create(TaskVersionConverter.convertTask(newTask)));
+ }
+
+ @Update
+ @SuppressWarnings("unused")
+ public MethodOutcome updateTask(@IdParam IdType id, @ResourceParam Task task) {
+ return FhirProviderUtils.buildUpdate(fhirTaskService.update(id.getIdPart(), TaskVersionConverter.convertTask(task)));
+ }
+
+ @Search
+ @SuppressWarnings("unused")
+ public Bundle searchTasks(@OptionalParam(name = Task.SP_BASED_ON) ReferenceParam basedOnReference,
+ @OptionalParam(name = Task.SP_OWNER) ReferenceParam ownerReference,
+ @OptionalParam(name = Task.SP_STATUS) TokenAndListParam status, @Sort SortSpec sort) {
+ return Bundle30_40.convertBundle(FhirProviderUtils.convertSearchResultsToBundle(
+ fhirTaskService.searchForTasks(basedOnReference, ownerReference, status, sort)));
+ }
+}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProvider.java
similarity index 91%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProvider.java
index 391dd8256..7fde7c54d 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -34,12 +34,12 @@
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Resource;
import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("allergyIntoleranceFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class AllergyIntoleranceFhirResourceProvider implements IResourceProvider {
@@ -83,14 +83,14 @@ public Bundle searchForAllergies(
@OptionalParam(name = AllergyIntolerance.SP_SEVERITY) TokenAndListParam severity,
@OptionalParam(name = AllergyIntolerance.SP_MANIFESTATION) TokenAndListParam manifestationCode,
@OptionalParam(name = AllergyIntolerance.SP_CLINICAL_STATUS) TokenAndListParam clinicalStatus) {
- return FhirServerUtils.convertSearchResultsToBundle(fhirAllergyIntoleranceService
+ return FhirProviderUtils.convertSearchResultsToBundle(fhirAllergyIntoleranceService
.searchForAllergies(patientReference, category, allergen, severity, manifestationCode, clinicalStatus));
}
@Create
@SuppressWarnings("unused")
public MethodOutcome createAllergy(@ResourceParam AllergyIntolerance allergy) {
- return FhirServerUtils.buildCreate(fhirAllergyIntoleranceService.create(allergy));
+ return FhirProviderUtils.buildCreate(fhirAllergyIntoleranceService.create(allergy));
}
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProvider.java
similarity index 91%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProvider.java
index b7a04a281..0e3ca0efd 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -38,12 +38,12 @@
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Resource;
import org.openmrs.module.fhir2.api.FhirConditionService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("conditionFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class ConditionFhirResourceProvider implements IResourceProvider {
@@ -78,7 +78,7 @@ public List getConditionHistoryById(@IdParam @NotNull IdType id) {
@Create
@SuppressWarnings("unused")
public MethodOutcome createCondition(@ResourceParam Condition newCondition) {
- return FhirServerUtils.buildCreate(conditionService.saveCondition(newCondition));
+ return FhirProviderUtils.buildCreate(conditionService.saveCondition(newCondition));
}
@Search
@@ -93,7 +93,7 @@ public Bundle searchConditions(
@OptionalParam(name = Condition.SP_ONSET_DATE) DateRangeParam onsetDate,
@OptionalParam(name = Condition.SP_ONSET_AGE) QuantityAndListParam onsetAge,
@OptionalParam(name = Condition.SP_RECORDED_DATE) DateRangeParam recordedDate, @Sort SortSpec sort) {
- return FhirServerUtils.convertSearchResultsToBundle(conditionService.searchConditions(patientParam, subjectParam,
+ return FhirProviderUtils.convertSearchResultsToBundle(conditionService.searchConditions(patientParam, subjectParam,
code, clinicalStatus, onsetDate, onsetAge, recordedDate, sort));
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProvider.java
similarity index 90%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProvider.java
index 13d86b75e..8fa677efa 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -35,12 +35,12 @@
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Patient;
import org.openmrs.module.fhir2.api.FhirDiagnosticReportService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("diagnosticReportFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class DiagnosticReportFhirResourceProvider implements IResourceProvider {
@@ -66,7 +66,7 @@ public DiagnosticReport getDiagnosticReportById(@IdParam @NotNull IdType id) {
@Create
public MethodOutcome createDiagnosticReport(@ResourceParam DiagnosticReport diagnosticReport) {
- return FhirServerUtils.buildCreate(service.create(diagnosticReport));
+ return FhirProviderUtils.buildCreate(service.create(diagnosticReport));
}
@Update
@@ -76,8 +76,7 @@ public MethodOutcome updateDiagnosticReport(@IdParam IdType id, @ResourceParam D
if (id != null) {
idPart = id.getIdPart();
}
-
- return FhirServerUtils.buildUpdate(service.update(idPart, diagnosticReport));
+ return FhirProviderUtils.buildUpdate(service.update(idPart, diagnosticReport));
}
@Search
@@ -89,7 +88,7 @@ public Bundle searchForDiagnosticReports(
Patient.SP_NAME }, targetTypes = Patient.class) ReferenceAndListParam patientReference,
@OptionalParam(name = DiagnosticReport.SP_ISSUED) DateRangeParam issueDate,
@OptionalParam(name = DiagnosticReport.SP_CODE) TokenAndListParam code, @Sort SortSpec sort) {
- return FhirServerUtils.convertSearchResultsToBundle(
+ return FhirProviderUtils.convertSearchResultsToBundle(
service.searchForDiagnosticReports(encounterReference, patientReference, issueDate, code, sort));
}
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProvider.java
similarity index 94%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProvider.java
index 899453d1f..f26edb0f4 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -33,12 +33,12 @@
import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.Resource;
import org.openmrs.module.fhir2.api.FhirEncounterService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("encounterFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class EncounterFhirResourceProvider implements IResourceProvider {
@@ -81,7 +81,7 @@ public Bundle searchEncounter(@OptionalParam(name = Encounter.SP_DATE) DateRange
@OptionalParam(name = Encounter.SP_SUBJECT, chainWhitelist = { "", Patient.SP_IDENTIFIER, Patient.SP_GIVEN,
Patient.SP_FAMILY,
Patient.SP_NAME }, targetTypes = Patient.class) ReferenceAndListParam subjectReference) {
- return FhirServerUtils.convertSearchResultsToBundle(
+ return FhirProviderUtils.convertSearchResultsToBundle(
encounterService.searchForEncounters(date, location, participantReference, subjectReference));
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/ListFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProvider.java
similarity index 95%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/ListFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProvider.java
index de45e4050..a099c6274 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/ListFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -26,7 +26,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("listFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class ListFhirResourceProvider implements IResourceProvider {
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProvider.java
similarity index 93%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProvider.java
index 4f15fb83d..6649ad6e1 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -33,12 +33,12 @@
import org.hl7.fhir.r4.model.Location;
import org.hl7.fhir.r4.model.Resource;
import org.openmrs.module.fhir2.api.FhirLocationService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("locationFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class LocationFhirResourceProvider implements IResourceProvider {
@@ -79,7 +79,7 @@ public Bundle searchLocations(@OptionalParam(name = Location.SP_NAME) StringAndL
@OptionalParam(name = Location.SP_ADDRESS_STATE) StringAndListParam state,
@OptionalParam(name = "_tag") TokenAndListParam tag,
@OptionalParam(name = Location.SP_PARTOF) ReferenceAndListParam parent, @Sort SortSpec sort) {
- return FhirServerUtils.convertSearchResultsToBundle(
+ return FhirProviderUtils.convertSearchResultsToBundle(
fhirLocationService.searchForLocations(name, city, country, postalCode, state, tag, parent, sort));
}
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java
similarity index 90%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java
index 1816b9fa5..cb868e942 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -32,12 +32,12 @@
import org.hl7.fhir.r4.model.Medication;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.openmrs.module.fhir2.api.FhirMedicationService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("medicationFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class MedicationFhirResourceProvider implements IResourceProvider {
@@ -65,14 +65,14 @@ public Medication getMedicationByUuid(@IdParam @NotNull IdType id) {
public Bundle searchForMedication(@OptionalParam(name = Medication.SP_CODE) TokenAndListParam code,
@OptionalParam(name = Medication.SP_FORM) TokenAndListParam dosageForm,
@OptionalParam(name = Medication.SP_STATUS) TokenAndListParam status) {
- return FhirServerUtils
+ return FhirProviderUtils
.convertSearchResultsToBundle(fhirMedicationService.searchForMedications(code, dosageForm, null, status));
}
@Create
@SuppressWarnings("unused")
public MethodOutcome createMedication(@ResourceParam Medication medication) {
- return FhirServerUtils.buildCreate(fhirMedicationService.create(medication));
+ return FhirProviderUtils.buildCreate(fhirMedicationService.create(medication));
}
@Update
@@ -84,7 +84,7 @@ public MethodOutcome updateMedication(@IdParam IdType id, @ResourceParam Medicat
medication.setId(id.getIdPart());
- return FhirServerUtils.buildUpdate(fhirMedicationService.update(id.getIdPart(), medication));
+ return FhirProviderUtils.buildUpdate(fhirMedicationService.update(id.getIdPart(), medication));
}
@Delete
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProvider.java
similarity index 94%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProvider.java
index f068ec71a..f3bcf1495 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -25,7 +25,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("medicationRequestFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class MedicationRequestFhirResourceProvider implements IResourceProvider {
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProvider.java
similarity index 97%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProvider.java
index cf6c09a6c..7c14bfe9e 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -42,7 +42,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("observationFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
@SuppressWarnings("unused")
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProvider.java
similarity index 90%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProvider.java
index c744257b4..23723481b 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -33,12 +33,12 @@
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Resource;
import org.openmrs.module.fhir2.api.FhirPatientService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("patientFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class PatientFhirResourceProvider implements IResourceProvider {
@@ -85,7 +85,7 @@ public Bundle searchPatients(@OptionalParam(name = Patient.SP_NAME) StringAndLis
@OptionalParam(name = Patient.SP_ADDRESS_STATE) StringAndListParam state,
@OptionalParam(name = Patient.SP_ADDRESS_POSTALCODE) StringAndListParam postalCode,
@OptionalParam(name = Patient.SP_ADDRESS_COUNTRY) StringAndListParam country, @Sort SortSpec sort) {
- return FhirServerUtils.convertSearchResultsToBundle(patientService.searchForPatients(name, given, family, identifier,
- gender, birthDate, deathDate, deceased, city, state, postalCode, country, sort));
+ return FhirProviderUtils.convertSearchResultsToBundle(patientService.searchForPatients(name, given, family,
+ identifier, gender, birthDate, deathDate, deceased, city, state, postalCode, country, sort));
}
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProvider.java
similarity index 93%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProvider.java
index b4e071a75..e6ad43e39 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -33,12 +33,12 @@
import org.hl7.fhir.r4.model.Person;
import org.hl7.fhir.r4.model.Resource;
import org.openmrs.module.fhir2.api.FhirPersonService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("personFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class PersonFhirResourceProvider implements IResourceProvider {
@@ -80,7 +80,7 @@ public Bundle searchPeople(@OptionalParam(name = Person.SP_NAME) StringAndListPa
@OptionalParam(name = Person.SP_ADDRESS_STATE) StringAndListParam state,
@OptionalParam(name = Person.SP_ADDRESS_POSTALCODE) StringAndListParam postalCode,
@OptionalParam(name = Person.SP_ADDRESS_COUNTRY) StringAndListParam country, @Sort SortSpec sort) {
- return FhirServerUtils.convertSearchResultsToBundle(
+ return FhirProviderUtils.convertSearchResultsToBundle(
fhirPersonService.searchForPeople(name, gender, birthDate, city, state, postalCode, country, sort));
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProvider.java
similarity index 87%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProvider.java
index 6c8b918d1..e588b4099 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -28,12 +28,12 @@
import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.Resource;
import org.openmrs.module.fhir2.api.FhirPractitionerService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("practitionerFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class PractitionerFhirResourceProvider implements IResourceProvider {
@@ -69,14 +69,14 @@ public List getPractitionerHistoryById(@IdParam @NotNull IdType id) {
@Search
@SuppressWarnings("unused")
public Bundle findPractitionersByName(@RequiredParam(name = Practitioner.SP_NAME) @NotNull String name) {
- return FhirServerUtils.convertSearchResultsToBundle(practitionerService.findPractitionerByName(name));
+ return FhirProviderUtils.convertSearchResultsToBundle(practitionerService.findPractitionerByName(name));
}
@Search
@SuppressWarnings("unused")
public Bundle findPractitionersByIdentifier(
@RequiredParam(name = Practitioner.SP_IDENTIFIER) @NotNull String identifier) {
- return FhirServerUtils.convertSearchResultsToBundle(practitionerService.findPractitionerByIdentifier(identifier));
+ return FhirProviderUtils.convertSearchResultsToBundle(practitionerService.findPractitionerByIdentifier(identifier));
}
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProvider.java
similarity index 94%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProvider.java
index ebeb78ca5..804bf2a79 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -25,7 +25,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("relatedPersonFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class RelatedPersonFhirResourceProvider implements IResourceProvider {
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProvider.java
similarity index 94%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProvider.java
index 80e568472..6a6f9efcd 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import javax.validation.constraints.NotNull;
@@ -25,7 +25,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("serviceRequestFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class ServiceRequestFhirResourceProvider implements IResourceProvider {
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProvider.java
similarity index 90%
rename from omod/src/main/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProvider.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProvider.java
index 81086c577..e751afba4 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProvider.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProvider.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import java.util.List;
@@ -34,12 +34,12 @@
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.Task;
import org.openmrs.module.fhir2.api.FhirTaskService;
-import org.openmrs.module.fhir2.util.FhirServerUtils;
+import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-@Component
+@Component("taskFhirR4ResourceProvider")
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class TaskFhirResourceProvider implements IResourceProvider {
@@ -75,13 +75,13 @@ public List getTaskHistoryById(@IdParam IdType id) {
@Create
@SuppressWarnings("unused")
public MethodOutcome createTask(@ResourceParam Task newTask) {
- return FhirServerUtils.buildCreate(service.create(newTask));
+ return FhirProviderUtils.buildCreate(service.create(newTask));
}
@Update
@SuppressWarnings("unused")
public MethodOutcome updateTask(@IdParam IdType id, @ResourceParam Task task) {
- return FhirServerUtils.buildUpdate(service.update(id.getIdPart(), task));
+ return FhirProviderUtils.buildUpdate(service.update(id.getIdPart(), task));
}
@Search
@@ -89,7 +89,7 @@ public MethodOutcome updateTask(@IdParam IdType id, @ResourceParam Task task) {
public Bundle searchTasks(@OptionalParam(name = Task.SP_BASED_ON) ReferenceParam basedOnReference,
@OptionalParam(name = Task.SP_OWNER) ReferenceParam ownerReference,
@OptionalParam(name = Task.SP_STATUS) TokenAndListParam status, @Sort SortSpec sort) {
- return FhirServerUtils
+ return FhirProviderUtils
.convertSearchResultsToBundle(service.searchForTasks(basedOnReference, ownerReference, status, sort));
}
}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/util/FhirServerUtils.java b/api/src/main/java/org/openmrs/module/fhir2/providers/util/FhirProviderUtils.java
similarity index 92%
rename from omod/src/main/java/org/openmrs/module/fhir2/util/FhirServerUtils.java
rename to api/src/main/java/org/openmrs/module/fhir2/providers/util/FhirProviderUtils.java
index 165b7d7e1..12481d71d 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/util/FhirServerUtils.java
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/util/FhirProviderUtils.java
@@ -7,7 +7,7 @@
* 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.util;
+package org.openmrs.module.fhir2.providers.util;
import java.util.Collection;
@@ -19,7 +19,7 @@
import org.hl7.fhir.r4.model.Resource;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public class FhirServerUtils {
+public class FhirProviderUtils {
public static MethodOutcome buildUpdate(DomainResource resource) {
MethodOutcome methodOutcome = new MethodOutcome();
@@ -46,7 +46,7 @@ private static MethodOutcome buildWithResource(MethodOutcome methodOutcome, Doma
}
public static Bundle convertSearchResultsToBundle(Collection resources) {
- Bundle bundle = FhirServerUtils.convertIterableToBundle(resources);
+ Bundle bundle = FhirProviderUtils.convertIterableToBundle(resources);
bundle.setType(Bundle.BundleType.SEARCHSET);
bundle.setTotal(resources.size());
return bundle;
diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/util/TaskVersionConverter.java b/api/src/main/java/org/openmrs/module/fhir2/providers/util/TaskVersionConverter.java
new file mode 100644
index 000000000..9d0136951
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/fhir2/providers/util/TaskVersionConverter.java
@@ -0,0 +1,519 @@
+/*
+ * 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.providers.util;
+
+import org.hl7.fhir.convertors.VersionConvertor_30_40;
+import org.hl7.fhir.dstu3.model.Task;
+import org.hl7.fhir.exceptions.FHIRException;
+
+public class TaskVersionConverter {
+
+ public static org.hl7.fhir.dstu3.model.Task convertTask(org.hl7.fhir.r4.model.Task src) throws FHIRException {
+ org.hl7.fhir.dstu3.model.Task tgt = new org.hl7.fhir.dstu3.model.Task();
+ VersionConvertor_30_40.copyDomainResource(src, tgt);
+ for (org.hl7.fhir.r4.model.Identifier t : src.getIdentifier()) {
+ tgt.addIdentifier(VersionConvertor_30_40.convertIdentifier(t));
+ }
+ //TODO: check
+ if (src.hasInstantiatesUri()) {
+ tgt.setDefinition(VersionConvertor_30_40.convertType(src.getInstantiatesUriElement()));
+ }
+ //TODO: check
+ if (src.hasInstantiatesCanonical()) {
+ tgt.setDefinition(VersionConvertor_30_40.convertType(src.getInstantiatesCanonicalElement()));
+ }
+ if (src.hasBasedOn()) {
+ for (org.hl7.fhir.r4.model.Reference t : src.getBasedOn()) {
+ tgt.addBasedOn(VersionConvertor_30_40.convertReference(t));
+ }
+ }
+ if (src.hasGroupIdentifier()) {
+ tgt.setGroupIdentifier(VersionConvertor_30_40.convertIdentifier(src.getGroupIdentifier()));
+ }
+ if (src.hasPartOf()) {
+ for (org.hl7.fhir.r4.model.Reference t : src.getPartOf()) {
+ tgt.addPartOf(VersionConvertor_30_40.convertReference(t));
+ }
+ }
+ if (src.hasStatus()) {
+ tgt.setStatusElement(convertTaskStatus(src.getStatusElement()));
+ }
+ if (src.hasStatusReason()) {
+ tgt.setStatusReason(VersionConvertor_30_40.convertCodeableConcept(src.getStatusReason()));
+ }
+ if (src.hasBusinessStatus()) {
+ tgt.setBusinessStatus(VersionConvertor_30_40.convertCodeableConcept(src.getBusinessStatus()));
+ }
+ if (src.hasIntent()) {
+ tgt.setIntentElement(convertTaskIntent(src.getIntentElement()));
+ }
+ if (src.hasPriority()) {
+ tgt.setPriorityElement(convertProcedureRequestPriority(src.getPriorityElement()));
+ }
+ if (src.hasCode()) {
+ tgt.setCode(VersionConvertor_30_40.convertCodeableConcept(src.getCode()));
+ }
+ if (src.hasDescription()) {
+ tgt.setDescriptionElement(VersionConvertor_30_40.convertString(src.getDescriptionElement()));
+ }
+ if (src.hasFocus()) {
+ tgt.setFocus(VersionConvertor_30_40.convertReference(src.getFocus()));
+ }
+ if (src.hasFor()) {
+ tgt.setFor(VersionConvertor_30_40.convertReference(src.getFor()));
+ }
+ if (src.hasEncounter()) {
+ tgt.setContext(VersionConvertor_30_40.convertReference(src.getEncounter()));
+ }
+ if (src.hasExecutionPeriod()) {
+ tgt.setExecutionPeriod(VersionConvertor_30_40.convertPeriod(src.getExecutionPeriod()));
+ }
+ if (src.hasAuthoredOn()) {
+ tgt.setAuthoredOnElement(VersionConvertor_30_40.convertDateTime(src.getAuthoredOnElement()));
+ }
+ if (src.hasLastModified()) {
+ tgt.setLastModifiedElement(VersionConvertor_30_40.convertDateTime(src.getLastModifiedElement()));
+ }
+ if (src.hasRequester()) {
+ tgt.getRequester().setAgent(VersionConvertor_30_40.convertReference(src.getRequester()));
+ }
+ if (src.hasPerformerType()) {
+ for (org.hl7.fhir.r4.model.CodeableConcept t : src.getPerformerType())
+ tgt.addPerformerType(VersionConvertor_30_40.convertCodeableConcept(t));
+ }
+ if (src.hasOwner()) {
+ tgt.setOwner(VersionConvertor_30_40.convertReference(src.getOwner()));
+ }
+ if (src.hasReasonCode()) {
+ tgt.setReason(VersionConvertor_30_40.convertCodeableConcept(src.getReasonCode()));
+ }
+ if (src.hasNote()) {
+ for (org.hl7.fhir.r4.model.Annotation t : src.getNote())
+ tgt.addNote(VersionConvertor_30_40.convertAnnotation(t));
+ }
+ if (src.hasRelevantHistory()) {
+ for (org.hl7.fhir.r4.model.Reference t : src.getRelevantHistory())
+ tgt.addRelevantHistory(VersionConvertor_30_40.convertReference(t));
+ }
+ if (src.hasRestriction()) {
+ tgt.setRestriction(convertTaskRestriction(src.getRestriction(), tgt.getRestriction()));
+ }
+ if (src.hasInput()) {
+ convertTaskInput(src, tgt);
+ }
+ if (src.hasOutput()) {
+ convertTaskOutput(src, tgt);
+ }
+
+ return tgt;
+ }
+
+ public static org.hl7.fhir.r4.model.Task convertTask(org.hl7.fhir.dstu3.model.Task src) throws FHIRException {
+ org.hl7.fhir.r4.model.Task tgt = new org.hl7.fhir.r4.model.Task();
+ VersionConvertor_30_40.copyDomainResource(src, tgt);
+
+ for (org.hl7.fhir.dstu3.model.Identifier t : src.getIdentifier()) {
+ tgt.addIdentifier(VersionConvertor_30_40.convertIdentifier(t));
+ }
+ //TODO: check
+ if (src.hasDefinition()) {
+ tgt.setInstantiatesUri(String.valueOf(VersionConvertor_30_40.convertUri(src.getDefinitionUriType())));
+ }
+ //TODO: check
+ if (src.hasDefinition()) {
+ tgt.setInstantiatesCanonical(
+ String.valueOf(VersionConvertor_30_40.convertReference(src.getDefinitionReference())));
+ }
+ if (src.hasBasedOn()) {
+ for (org.hl7.fhir.dstu3.model.Reference t : src.getBasedOn()) {
+ tgt.addBasedOn(VersionConvertor_30_40.convertReference(t));
+ }
+ }
+ if (src.hasGroupIdentifier()) {
+ tgt.setGroupIdentifier(VersionConvertor_30_40.convertIdentifier(src.getGroupIdentifier()));
+ }
+ if (src.hasPartOf()) {
+ for (org.hl7.fhir.dstu3.model.Reference t : src.getPartOf()) {
+ tgt.addPartOf(VersionConvertor_30_40.convertReference(t));
+ }
+ }
+ if (src.hasStatus()) {
+ tgt.setStatusElement(convertTaskStatus(src.getStatusElement()));
+ }
+ if (src.hasStatusReason()) {
+ tgt.setStatusReason(VersionConvertor_30_40.convertCodeableConcept(src.getStatusReason()));
+ }
+ if (src.hasBusinessStatus()) {
+ tgt.setBusinessStatus(VersionConvertor_30_40.convertCodeableConcept(src.getBusinessStatus()));
+ }
+ if (src.hasIntent()) {
+ tgt.setIntentElement(convertTaskIntent(src.getIntentElement()));
+ }
+ if (src.hasPriority()) {
+ tgt.setPriorityElement(convertProcedureRequestPriority(src.getPriorityElement()));
+ }
+ if (src.hasCode()) {
+ tgt.setCode(VersionConvertor_30_40.convertCodeableConcept(src.getCode()));
+ }
+ if (src.hasDescription()) {
+ tgt.setDescriptionElement(VersionConvertor_30_40.convertString(src.getDescriptionElement()));
+ }
+ if (src.hasFocus()) {
+ tgt.setFocus(VersionConvertor_30_40.convertReference(src.getFocus()));
+ }
+ if (src.hasFor()) {
+ tgt.setFor(VersionConvertor_30_40.convertReference(src.getFor()));
+ }
+ if (src.hasContext()) {
+ tgt.setFocus(VersionConvertor_30_40.convertReference(src.getContext()));
+ }
+ if (src.hasExecutionPeriod()) {
+ tgt.setExecutionPeriod(VersionConvertor_30_40.convertPeriod(src.getExecutionPeriod()));
+ }
+ if (src.hasAuthoredOn()) {
+ tgt.setAuthoredOnElement(VersionConvertor_30_40.convertDateTime(src.getAuthoredOnElement()));
+ }
+ if (src.hasLastModified()) {
+ tgt.setLastModifiedElement(VersionConvertor_30_40.convertDateTime(src.getLastModifiedElement()));
+ }
+ if (src.hasRequester()) {
+ tgt.setRequester(VersionConvertor_30_40.convertReference(src.getRequester().getAgent()));
+ }
+ if (src.hasPerformerType()) {
+ for (org.hl7.fhir.dstu3.model.CodeableConcept t : src.getPerformerType())
+ tgt.addPerformerType(VersionConvertor_30_40.convertCodeableConcept(t));
+ }
+ if (src.hasOwner()) {
+ tgt.setOwner(VersionConvertor_30_40.convertReference(src.getOwner()));
+ }
+ if (src.hasReason()) {
+ tgt.setReasonCode(VersionConvertor_30_40.convertCodeableConcept(src.getReason()));
+ }
+ if (src.hasNote()) {
+ for (org.hl7.fhir.dstu3.model.Annotation t : src.getNote())
+ tgt.addNote(VersionConvertor_30_40.convertAnnotation(t));
+ }
+ if (src.hasRelevantHistory()) {
+ for (org.hl7.fhir.dstu3.model.Reference t : src.getRelevantHistory())
+ tgt.addRelevantHistory(VersionConvertor_30_40.convertReference(t));
+ }
+ if (src.hasRestriction()) {
+ tgt.setRestriction(convertTaskRestriction(src.getRestriction(), tgt.getRestriction()));
+ }
+ if (src.hasInput()) {
+ convertTaskInput(src, tgt);
+ }
+ if (src.hasOutput()) {
+ convertTaskOutput(src, tgt);
+ }
+
+ return tgt;
+ }
+
+ public static org.hl7.fhir.r4.model.Enumeration convertTaskStatus(
+ org.hl7.fhir.dstu3.model.Enumeration src) throws FHIRException {
+ if (src == null || src.isEmpty())
+ return null;
+ org.hl7.fhir.r4.model.Enumeration tgt = new org.hl7.fhir.r4.model.Enumeration<>(
+ new org.hl7.fhir.r4.model.Task.TaskStatusEnumFactory());
+ switch (src.getValue()) {
+ case DRAFT:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.DRAFT);
+ break;
+ case REQUESTED:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.REQUESTED);
+ break;
+ case RECEIVED:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.RECEIVED);
+ break;
+ case ACCEPTED:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.ACCEPTED);
+ break;
+ case REJECTED:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.REJECTED);
+ break;
+ case READY:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.READY);
+ break;
+ case CANCELLED:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.CANCELLED);
+ break;
+ case INPROGRESS:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.INPROGRESS);
+ break;
+ case ONHOLD:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.ONHOLD);
+ break;
+ case FAILED:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.FAILED);
+ break;
+ case COMPLETED:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.COMPLETED);
+ break;
+ case ENTEREDINERROR:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.ENTEREDINERROR);
+ break;
+ case NULL:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.NULL);
+ break;
+ default:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskStatus.NULL);
+ break;
+ }
+ return tgt;
+ }
+
+ public static org.hl7.fhir.dstu3.model.Enumeration convertTaskStatus(
+ org.hl7.fhir.r4.model.Enumeration src) throws FHIRException {
+ if (src == null || src.isEmpty())
+ return null;
+ org.hl7.fhir.dstu3.model.Enumeration tgt = new org.hl7.fhir.dstu3.model.Enumeration<>(
+ new Task.TaskStatusEnumFactory());
+ switch (src.getValue()) {
+ case DRAFT:
+ tgt.setValue(Task.TaskStatus.DRAFT);
+ break;
+ case REQUESTED:
+ tgt.setValue(Task.TaskStatus.REQUESTED);
+ break;
+ case RECEIVED:
+ tgt.setValue(Task.TaskStatus.RECEIVED);
+ break;
+ case ACCEPTED:
+ tgt.setValue(Task.TaskStatus.ACCEPTED);
+ break;
+ case REJECTED:
+ tgt.setValue(Task.TaskStatus.REJECTED);
+ break;
+ case READY:
+ tgt.setValue(Task.TaskStatus.READY);
+ break;
+ case CANCELLED:
+ tgt.setValue(Task.TaskStatus.CANCELLED);
+ break;
+ case INPROGRESS:
+ tgt.setValue(Task.TaskStatus.INPROGRESS);
+ break;
+ case ONHOLD:
+ tgt.setValue(Task.TaskStatus.ONHOLD);
+ break;
+ case FAILED:
+ tgt.setValue(Task.TaskStatus.FAILED);
+ break;
+ case COMPLETED:
+ tgt.setValue(Task.TaskStatus.COMPLETED);
+ break;
+ case ENTEREDINERROR:
+ tgt.setValue(Task.TaskStatus.ENTEREDINERROR);
+ break;
+ case NULL:
+ tgt.setValue(Task.TaskStatus.NULL);
+ break;
+ default:
+ tgt.setValue(Task.TaskStatus.NULL);
+ break;
+ }
+ return tgt;
+ }
+
+ public static org.hl7.fhir.r4.model.Enumeration convertTaskIntent(
+ org.hl7.fhir.dstu3.model.Enumeration src) throws FHIRException {
+ if (src == null || src.isEmpty())
+ return null;
+ org.hl7.fhir.r4.model.Enumeration tgt = new org.hl7.fhir.r4.model.Enumeration<>(
+ new org.hl7.fhir.r4.model.Task.TaskIntentEnumFactory());
+ switch (src.getValue()) {
+ case PROPOSAL:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.PROPOSAL);
+ break;
+ case PLAN:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.PLAN);
+ break;
+ case ORDER:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.ORDER);
+ break;
+ case ORIGINALORDER:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.ORIGINALORDER);
+ break;
+ case REFLEXORDER:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.REFLEXORDER);
+ break;
+ case FILLERORDER:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.FILLERORDER);
+ break;
+ case INSTANCEORDER:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.INSTANCEORDER);
+ break;
+ case OPTION:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.OPTION);
+ break;
+ case NULL:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.NULL);
+ break;
+ default:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskIntent.NULL);
+ break;
+ }
+ return tgt;
+ }
+
+ public static org.hl7.fhir.dstu3.model.Enumeration convertTaskIntent(
+ org.hl7.fhir.r4.model.Enumeration src) throws FHIRException {
+ if (src == null || src.isEmpty())
+ return null;
+ org.hl7.fhir.dstu3.model.Enumeration tgt = new org.hl7.fhir.dstu3.model.Enumeration<>(
+ new Task.TaskIntentEnumFactory());
+ switch (src.getValue()) {
+ case PROPOSAL:
+ tgt.setValue(Task.TaskIntent.PROPOSAL);
+ break;
+ case PLAN:
+ tgt.setValue(Task.TaskIntent.PLAN);
+ break;
+ case ORDER:
+ tgt.setValue(Task.TaskIntent.ORDER);
+ break;
+ case ORIGINALORDER:
+ tgt.setValue(Task.TaskIntent.ORIGINALORDER);
+ break;
+ case REFLEXORDER:
+ tgt.setValue(Task.TaskIntent.REFLEXORDER);
+ break;
+ case FILLERORDER:
+ tgt.setValue(Task.TaskIntent.FILLERORDER);
+ break;
+ case INSTANCEORDER:
+ tgt.setValue(Task.TaskIntent.INSTANCEORDER);
+ break;
+ case OPTION:
+ tgt.setValue(Task.TaskIntent.OPTION);
+ break;
+ case NULL:
+ tgt.setValue(Task.TaskIntent.NULL);
+ break;
+ default:
+ tgt.setValue(Task.TaskIntent.NULL);
+ break;
+ }
+ return tgt;
+ }
+
+ public static org.hl7.fhir.r4.model.Enumeration convertProcedureRequestPriority(
+ org.hl7.fhir.dstu3.model.Enumeration src) throws FHIRException {
+ if (src == null || src.isEmpty())
+ return null;
+ org.hl7.fhir.r4.model.Enumeration tgt = new org.hl7.fhir.r4.model.Enumeration<>(
+ new org.hl7.fhir.r4.model.Task.TaskPriorityEnumFactory());
+ switch (src.getValue()) {
+ case ROUTINE:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskPriority.ROUTINE);
+ break;
+ case URGENT:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskPriority.URGENT);
+ break;
+ case ASAP:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskPriority.ASAP);
+ break;
+ case STAT:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskPriority.STAT);
+ break;
+ case NULL:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskPriority.NULL);
+ break;
+ default:
+ tgt.setValue(org.hl7.fhir.r4.model.Task.TaskPriority.NULL);
+ break;
+ }
+ return tgt;
+ }
+
+ public static org.hl7.fhir.dstu3.model.Enumeration convertProcedureRequestPriority(
+ org.hl7.fhir.r4.model.Enumeration src) throws FHIRException {
+ if (src == null || src.isEmpty())
+ return null;
+ org.hl7.fhir.dstu3.model.Enumeration tgt = new org.hl7.fhir.dstu3.model.Enumeration<>(
+ new Task.TaskPriorityEnumFactory());
+ switch (src.getValue()) {
+ case ROUTINE:
+ tgt.setValue(org.hl7.fhir.dstu3.model.Task.TaskPriority.ROUTINE);
+ break;
+ case URGENT:
+ tgt.setValue(org.hl7.fhir.dstu3.model.Task.TaskPriority.URGENT);
+ break;
+ case ASAP:
+ tgt.setValue(org.hl7.fhir.dstu3.model.Task.TaskPriority.ASAP);
+ break;
+ case STAT:
+ tgt.setValue(org.hl7.fhir.dstu3.model.Task.TaskPriority.STAT);
+ break;
+ case NULL:
+ tgt.setValue(org.hl7.fhir.dstu3.model.Task.TaskPriority.NULL);
+ break;
+ default:
+ tgt.setValue(org.hl7.fhir.dstu3.model.Task.TaskPriority.NULL);
+ break;
+ }
+ return tgt;
+ }
+
+ public static Task.TaskRestrictionComponent convertTaskRestriction(
+ org.hl7.fhir.r4.model.Task.TaskRestrictionComponent src,
+ org.hl7.fhir.dstu3.model.Task.TaskRestrictionComponent tgt) {
+
+ if (src.hasRepetitions()) {
+ tgt.setRepetitionsElement(VersionConvertor_30_40.convertPositiveInt(src.getRepetitionsElement()));
+ }
+ if (src.hasPeriod()) {
+ tgt.setPeriod(VersionConvertor_30_40.convertPeriod(src.getPeriod()));
+ }
+ if (src.hasRecipient()) {
+ for (org.hl7.fhir.r4.model.Reference t : src.getRecipient())
+ tgt.addRecipient(VersionConvertor_30_40.convertReference(t));
+ }
+ return tgt;
+ }
+
+ public static org.hl7.fhir.r4.model.Task.TaskRestrictionComponent convertTaskRestriction(
+ org.hl7.fhir.dstu3.model.Task.TaskRestrictionComponent src,
+ org.hl7.fhir.r4.model.Task.TaskRestrictionComponent tgt) {
+
+ if (src.hasRepetitions()) {
+ tgt.setRepetitionsElement(VersionConvertor_30_40.convertPositiveInt(src.getRepetitionsElement()));
+ }
+ if (src.hasPeriod()) {
+ tgt.setPeriod(VersionConvertor_30_40.convertPeriod(src.getPeriod()));
+ }
+ if (src.hasRecipient()) {
+ for (org.hl7.fhir.dstu3.model.Reference t : src.getRecipient())
+ tgt.addRecipient(VersionConvertor_30_40.convertReference(t));
+ }
+ return tgt;
+ }
+
+ public static void convertTaskInput(org.hl7.fhir.r4.model.Task src, org.hl7.fhir.dstu3.model.Task tgt) {
+ src.getInput().forEach(ti -> tgt.addInput().setType(VersionConvertor_30_40.convertCodeableConcept(ti.getType()))
+ .setValue(VersionConvertor_30_40.convertType(ti.getValue())));
+ }
+
+ public static void convertTaskInput(org.hl7.fhir.dstu3.model.Task src, org.hl7.fhir.r4.model.Task tgt) {
+ src.getOutput().forEach(ti -> tgt.addOutput().setType(VersionConvertor_30_40.convertCodeableConcept(ti.getType()))
+ .setValue(VersionConvertor_30_40.convertType(ti.getValue())));
+ }
+
+ public static void convertTaskOutput(org.hl7.fhir.r4.model.Task src, org.hl7.fhir.dstu3.model.Task tgt) {
+ tgt.addInput().setType(VersionConvertor_30_40.convertCodeableConcept(src.getInput().iterator().next().getType()));
+ tgt.addInput().setValue(VersionConvertor_30_40.convertType(src.getInput().iterator().next().getValue()));
+ }
+
+ public static void convertTaskOutput(org.hl7.fhir.dstu3.model.Task src, org.hl7.fhir.r4.model.Task tgt) {
+ tgt.addOutput().setType(VersionConvertor_30_40.convertCodeableConcept(src.getInput().iterator().next().getType()));
+ tgt.addOutput().setValue(VersionConvertor_30_40.convertType(src.getInput().iterator().next().getValue()));
+ }
+}
diff --git a/api/src/main/resources/moduleApplicationContext.xml b/api/src/main/resources/moduleApplicationContext.xml
index a189876c2..25128154c 100644
--- a/api/src/main/resources/moduleApplicationContext.xml
+++ b/api/src/main/resources/moduleApplicationContext.xml
@@ -22,5 +22,6 @@
+
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/BaseFhirProvenanceResourceTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirProvenanceResourceTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/web/servlet/BaseFhirProvenanceResourceTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirProvenanceResourceTest.java
index a6365da86..f03341f5d 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/BaseFhirProvenanceResourceTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirProvenanceResourceTest.java
@@ -7,7 +7,7 @@
* 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.web.servlet;
+package org.openmrs.module.fhir2.providers;
import static org.mockito.Mockito.when;
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/MockIBundleProvider.java b/api/src/test/java/org/openmrs/module/fhir2/providers/MockIBundleProvider.java
new file mode 100644
index 000000000..d14bd079c
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/MockIBundleProvider.java
@@ -0,0 +1,71 @@
+/*
+ * 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.providers;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+
+import ca.uhn.fhir.rest.api.server.IBundleProvider;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.hl7.fhir.instance.model.api.IPrimitiveType;
+import org.hl7.fhir.r4.model.InstantType;
+
+public class MockIBundleProvider implements IBundleProvider {
+
+ private final Date datePublished;
+
+ private List mockResultList;
+
+ private final UUID uuid;
+
+ private Integer count;
+
+ private Integer preferredPageSize;
+
+ public MockIBundleProvider(List mockResultList, Integer preferredPageSize, Integer count) {
+ this.count = count;
+ this.uuid = UUID.randomUUID();
+ this.datePublished = new Date();
+ this.mockResultList = mockResultList;
+ this.preferredPageSize = preferredPageSize;
+ }
+
+ @Override
+ public IPrimitiveType getPublished() {
+ return new InstantType(datePublished);
+ }
+
+ @Nonnull
+ @Override
+ public List getResources(int i, int i1) {
+ return (List) this.mockResultList;
+ }
+
+ @Nullable
+ @Override
+ public String getUuid() {
+ return String.valueOf(this.uuid);
+ }
+
+ @Override
+ public Integer preferredPageSize() {
+ return this.preferredPageSize;
+ }
+
+ @Nullable
+ @Override
+ public Integer size() {
+ return this.count;
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderTest.java
new file mode 100644
index 000000000..60e83874b
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderTest.java
@@ -0,0 +1,257 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.when;
+import static org.mockito.hamcrest.MockitoHamcrest.argThat;
+
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.ReferenceOrListParam;
+import ca.uhn.fhir.rest.param.ReferenceParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import ca.uhn.fhir.rest.param.TokenParam;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hl7.fhir.dstu3.model.AllergyIntolerance;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Patient;
+import org.hl7.fhir.dstu3.model.Provenance;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AllergyIntoleranceFhirR3ResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String ALLERGY_UUID = "1085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ private static final String WRONG_ALLERGY_UUID = "2085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ private static final String CODED_ALLERGEN_UUID = "5085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ private static final String SEVERITY_CONCEPT_UUID = "5088AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ private static final String CODED_REACTION_UUID = "5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ @Mock
+ private FhirAllergyIntoleranceService service;
+
+ private AllergyIntoleranceFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance;
+
+ @Before
+ public void setup() {
+ resourceProvider = new AllergyIntoleranceFhirResourceProvider();
+ resourceProvider.setAllergyIntoleranceService(service);
+ }
+
+ @Before
+ public void initAllergyIntolerance() {
+ allergyIntolerance = new org.hl7.fhir.r4.model.AllergyIntolerance();
+ allergyIntolerance.setId(ALLERGY_UUID);
+ allergyIntolerance.addCategory(org.hl7.fhir.r4.model.AllergyIntolerance.AllergyIntoleranceCategory.FOOD);
+ setProvenanceResources(allergyIntolerance);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(AllergyIntolerance.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(AllergyIntolerance.class.getName()));
+ }
+
+ @Test
+ public void getAllergyIntoleranceByUuid_shouldReturnMatchingAllergy() {
+ when(service.get(ALLERGY_UUID)).thenReturn(allergyIntolerance);
+
+ IdType id = new IdType();
+ id.setValue(ALLERGY_UUID);
+ AllergyIntolerance allergy = resourceProvider.getAllergyIntoleranceById(id);
+ assertThat(allergy, notNullValue());
+ assertThat(allergy.getId(), notNullValue());
+ assertThat(allergy.getId(), equalTo(ALLERGY_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getAllergyIntoleranceByUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_ALLERGY_UUID);
+ AllergyIntolerance result = resourceProvider.getAllergyIntoleranceById(id);
+ assertThat(result, nullValue());
+ }
+
+ @Test
+ public void getAllergyIntoleranceHistory_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(ALLERGY_UUID);
+ when(service.get(ALLERGY_UUID)).thenReturn(allergyIntolerance);
+
+ List resources = resourceProvider.getAllergyIntoleranceHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(), equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getAllergyIntoleranceHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_ALLERGY_UUID);
+ assertThat(resourceProvider.getAllergyIntoleranceHistoryById(idType).isEmpty(), is(true));
+ assertThat(resourceProvider.getAllergyIntoleranceHistoryById(idType).size(), equalTo(0));
+
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByIdentifier() {
+ ReferenceAndListParam patient = new ReferenceAndListParam();
+ patient.addValue(
+ new ReferenceOrListParam().add(new ReferenceParam().setValue("M4001-1").setChain(Patient.SP_IDENTIFIER)));
+
+ when(service.searchForAllergies(argThat(is(patient)), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(patient, null, null, null, null, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByPatientGivenName() {
+ ReferenceAndListParam patient = new ReferenceAndListParam();
+ patient.addValue(new ReferenceOrListParam().add(new ReferenceParam().setValue("John").setChain(Patient.SP_GIVEN)));
+
+ when(service.searchForAllergies(argThat(is(patient)), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(patient, null, null, null, null, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByPatientFamilyName() {
+ ReferenceAndListParam patient = new ReferenceAndListParam();
+ patient.addValue(new ReferenceOrListParam().add(new ReferenceParam().setValue("John").setChain(Patient.SP_FAMILY)));
+
+ when(service.searchForAllergies(argThat(is(patient)), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(patient, null, null, null, null, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByPatientName() {
+ ReferenceAndListParam patient = new ReferenceAndListParam();
+ patient.addValue(
+ new ReferenceOrListParam().add(new ReferenceParam().setValue("John Doe").setChain(Patient.SP_NAME)));
+
+ when(service.searchForAllergies(argThat(is(patient)), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(patient, null, null, null, null, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByCategory() {
+ TokenAndListParam category = new TokenAndListParam();
+ category.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue("food")));
+
+ when(service.searchForAllergies(isNull(), argThat(is(category)), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(null, category, null, null, null, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByAllergen() {
+ TokenAndListParam allergen = new TokenAndListParam();
+ allergen.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue(CODED_ALLERGEN_UUID)));
+
+ when(service.searchForAllergies(isNull(), isNull(), argThat(is(allergen)), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(null, null, allergen, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesBySeverity() {
+ TokenAndListParam severity = new TokenAndListParam();
+ severity.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue(SEVERITY_CONCEPT_UUID)));
+
+ when(service.searchForAllergies(isNull(), isNull(), isNull(), argThat(is(severity)), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(null, null, null, severity, null, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByManifestation() {
+ TokenAndListParam manifestation = new TokenAndListParam();
+ manifestation.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue(CODED_REACTION_UUID)));
+
+ when(service.searchForAllergies(isNull(), isNull(), isNull(), isNull(), argThat(is(manifestation)), isNull()))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(null, null, null, null, manifestation, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByStatus() {
+ TokenAndListParam status = new TokenAndListParam();
+ status.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue("active")));
+
+ when(service.searchForAllergies(isNull(), isNull(), isNull(), isNull(), isNull(), argThat(is(status))))
+ .thenReturn(Collections.singletonList(allergyIntolerance));
+
+ Bundle results = resourceProvider.searchForAllergies(null, null, null, null, null, status);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirIBundleResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirIBundleResourceProviderTest.java
similarity index 95%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirIBundleResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirIBundleResourceProviderTest.java
index 264dcd5b1..ab5b08b98 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirIBundleResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirIBundleResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r3;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -17,9 +17,9 @@
import java.util.UUID;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
+import org.hl7.fhir.dstu3.model.InstantType;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
-import org.hl7.fhir.r4.model.InstantType;
public class BaseFhirIBundleResourceProviderTest implements IBundleProvider {
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3ProvenanceResourceTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3ProvenanceResourceTest.java
new file mode 100644
index 000000000..2fbe1b64f
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3ProvenanceResourceTest.java
@@ -0,0 +1,16 @@
+/*
+ * 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.providers.r3;
+
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
+
+public abstract class BaseFhirR3ProvenanceResourceTest extends BaseFhirProvenanceResourceTest {
+
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirR3ResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirR3ResourceProviderTest.java
new file mode 100644
index 000000000..77449190f
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirR3ResourceProviderTest.java
@@ -0,0 +1,146 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.api.SortSpec;
+import ca.uhn.fhir.rest.param.*;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hamcrest.Matchers;
+import org.hl7.fhir.convertors.conv30_40.Condition30_40;
+import org.hl7.fhir.dstu3.model.*;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirConditionService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ConditionFhirR3ResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String CONDITION_UUID = "23f620c3-2ecb-4d80-aea8-44fa1c5ff978";
+
+ private static final String WRONG_CONDITION_UUID = "ca0dfd38-ee20-41a6-909e-7d84247ca192";
+
+ @Mock
+ private FhirConditionService conditionService;
+
+ private org.hl7.fhir.r4.model.Condition condition;
+
+ private ConditionFhirResourceProvider resourceProvider;
+
+ @Before
+ public void setUp() {
+ resourceProvider = new ConditionFhirResourceProvider();
+ resourceProvider.setConditionService(conditionService);
+ }
+
+ @Before
+ public void initCondition() {
+ condition = new org.hl7.fhir.r4.model.Condition();
+ condition.setId(CONDITION_UUID);
+ setProvenanceResources(condition);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(Condition.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(Condition.class.getName()));
+ }
+
+ @Test
+ public void getConditionByUuid_shouldReturnMatchingEncounter() {
+ when(conditionService.getConditionByUuid(CONDITION_UUID)).thenReturn(condition);
+ IdType id = new IdType();
+ id.setValue(CONDITION_UUID);
+ Condition condition = resourceProvider.getConditionById(id);
+ assertThat(condition, notNullValue());
+ assertThat(condition.getId(), notNullValue());
+ assertThat(condition.getId(), equalTo(CONDITION_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getConditionWithWrongUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_CONDITION_UUID);
+ Condition result = resourceProvider.getConditionById(id);
+ assertThat(result, nullValue());
+ }
+
+ @Test
+ public void getConditionHistory_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(CONDITION_UUID);
+ when(conditionService.getConditionByUuid(CONDITION_UUID)).thenReturn(condition);
+
+ List resources = resourceProvider.getConditionHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(),
+ Matchers.equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getConditionHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_CONDITION_UUID);
+ assertThat(resourceProvider.getConditionHistoryById(idType).isEmpty(), is(true));
+ assertThat(resourceProvider.getConditionHistoryById(idType).size(), Matchers.equalTo(0));
+ }
+
+ // @Test
+ // public void shouldCreateNewCondition() {
+ // when(conditionService.saveCondition(condition)).thenReturn(condition);
+ //
+ // MethodOutcome result = resourceProvider.createCondition(condition);
+ // assertThat(result, notNullValue());
+ // assertThat(result.getCreated(), is(true));
+ // assertThat(result.getResource(), equalTo(condition));
+ // }
+
+ @Test
+ public void searchConditions_shouldReturnConditionReturnedByService() {
+ ReferenceAndListParam patientReference = new ReferenceAndListParam();
+ patientReference.addValue(new ReferenceOrListParam().add(new ReferenceParam(Patient.SP_GIVEN, "patient name")));
+ ReferenceAndListParam subjectReference = new ReferenceAndListParam();
+ subjectReference.addValue(new ReferenceOrListParam().add(new ReferenceParam(Patient.SP_GIVEN, "subject name")));
+ TokenAndListParam codeList = new TokenAndListParam();
+ codeList.addValue(new TokenOrListParam().add(new TokenParam("test code")));
+ TokenAndListParam clinicalList = new TokenAndListParam();
+ clinicalList.addValue(new TokenOrListParam().add(new TokenParam("test clinical")));
+ DateRangeParam onsetDate = new DateRangeParam().setLowerBound("lower date").setUpperBound("upper date");
+ QuantityAndListParam onsetAge = new QuantityAndListParam();
+ onsetAge.addValue(new QuantityOrListParam().add(new QuantityParam(12)));
+ DateRangeParam recordDate = new DateRangeParam().setLowerBound("lower record date")
+ .setUpperBound("upper record date");
+ SortSpec sort = new SortSpec("sort param");
+ when(conditionService.searchConditions(patientReference, subjectReference, codeList, clinicalList, onsetDate,
+ onsetAge, recordDate, sort)).thenReturn(Collections.singletonList(condition));
+
+ Bundle result = resourceProvider.searchConditions(patientReference, subjectReference, codeList, clinicalList,
+ onsetDate, onsetAge, recordDate, sort);
+ assertThat(result, notNullValue());
+ assertThat(result.getTotal(), is(1));
+ assertThat(result.getEntry().get(0).getResource().getIdElement().getIdPart(),
+ equalTo(Condition30_40.convertCondition(condition).getIdElement().getIdPart()));
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProviderTest.java
new file mode 100644
index 000000000..ceddc467e
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProviderTest.java
@@ -0,0 +1,147 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hl7.fhir.dstu3.model.DiagnosticReport;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirDiagnosticReportService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DiagnosticReportFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String UUID = "bdd7e368-3d1a-42a9-9538-395391b64adf";
+
+ private static final String WRONG_UUID = "df34a1c1-f57b-4c33-bee5-e601b56b9d5b";
+
+ @Mock
+ private FhirDiagnosticReportService service;
+
+ @Getter(AccessLevel.PUBLIC)
+ private DiagnosticReportFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.DiagnosticReport diagnosticReport;
+
+ @Before
+ public void setup() {
+ resourceProvider = new DiagnosticReportFhirResourceProvider();
+ resourceProvider.setDiagnosticReportService(service);
+ }
+
+ @Before
+ public void initDiagnosticReport() {
+ diagnosticReport = new org.hl7.fhir.r4.model.DiagnosticReport();
+ diagnosticReport.setId(UUID);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(DiagnosticReport.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(DiagnosticReport.class.getName()));
+ }
+
+ @Test
+ public void getDiagnosticReportById_shouldReturnMatchingDiagnosticReport() {
+ IdType id = new IdType();
+ id.setValue(UUID);
+
+ when(service.get(UUID)).thenReturn(diagnosticReport);
+
+ DiagnosticReport result = resourceProvider.getDiagnosticReportById(id);
+
+ assertThat(result, notNullValue());
+ assertThat(result.isResource(), is(true));
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getDiagnosticReportByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_UUID);
+
+ assertThat(resourceProvider.getDiagnosticReportById(idType).isResource(), is(true));
+ assertThat(resourceProvider.getDiagnosticReportById(idType), nullValue());
+ }
+
+ // @Test
+ // public void createDiagnosticReport_shouldCreateNewDiagnosticReport() {
+ // when(service.saveDiagnosticReport(diagnosticReport)).thenReturn(diagnosticReport);
+ //
+ // MethodOutcome result = resourceProvider
+ // .createDiagnosticReport(VersionConvertor_30_40.convertDiagnosticReport(diagnosticReport));
+ //
+ // assertThat(result, notNullValue());
+ // assertThat(result.getResource(), equalTo(VersionConvertor_30_40.convertDiagnosticReport(diagnosticReport)));
+ // }
+ //
+ // @Test
+ // public void updateDiagnosticReport_shouldUpdateExistingDiagnosticReport() {
+ // DiagnosticReport diagnosticReport = new DiagnosticReport();
+ //
+ // diagnosticReport.setId(WRONG_UUID);
+ // when(service.updateDiagnosticReport(UUID, VersionConvertor_30_40.convertDiagnosticReport(diagnosticReport)))
+ // .thenReturn(VersionConvertor_30_40.convertDiagnosticReport(diagnosticReport));
+ //
+ // MethodOutcome result = resourceProvider.updateDiagnosticReport(new IdType().setValue(UUID), diagnosticReport);
+ //
+ // assertThat(result, notNullValue());
+ // assertThat(result.getResource(), equalTo(diagnosticReport));
+ // }
+ //
+ // @Test(expected = InvalidRequestException.class)
+ // public void updateDiagnosticReport_shouldThrowInvalidRequestForUuidMismatch() {
+ // DiagnosticReport wrongDiagnosticReport = new DiagnosticReport();
+ //
+ // wrongDiagnosticReport.setId(WRONG_UUID);
+ //
+ // when(service.updateDiagnosticReport(WRONG_UUID,
+ // VersionConvertor_30_40.convertDiagnosticReport(wrongDiagnosticReport))).thenThrow(InvalidRequestException.class);
+ //
+ // resourceProvider.updateDiagnosticReport(new IdType().setValue(WRONG_UUID), wrongDiagnosticReport);
+ // }
+ //
+ // @Test(expected = InvalidRequestException.class)
+ // public void updateDiagnosticReport_shouldThrowInvalidRequestForMissingId() {
+ // DiagnosticReport noIdDiagnostiReport = new DiagnosticReport();
+ //
+ // when(service.updateDiagnosticReport(UUID, VersionConvertor_30_40.convertDiagnosticReport(noIdDiagnostiReport)))
+ // .thenThrow(InvalidRequestException.class);
+ //
+ // resourceProvider.updateDiagnosticReport(new IdType().setValue(UUID), noIdDiagnostiReport);
+ // }
+ //
+ // @Test(expected = MethodNotAllowedException.class)
+ // public void updateDiagnosticReport_shouldThrowMethodNotAllowedIfDoesNotExist() {
+ // DiagnosticReport wrongDiagnosticReport = new DiagnosticReport();
+ //
+ // wrongDiagnosticReport.setId(WRONG_UUID);
+ //
+ // when(service.updateDiagnosticReport(WRONG_UUID,
+ // VersionConvertor_30_40.convertDiagnosticReport(wrongDiagnosticReport)))
+ // .thenThrow(MethodNotAllowedException.class);
+ //
+ // resourceProvider.updateDiagnosticReport(new IdType().setValue(WRONG_UUID), wrongDiagnosticReport);
+ // }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderTest.java
new file mode 100644
index 000000000..258a4293f
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.ReferenceOrListParam;
+import ca.uhn.fhir.rest.param.ReferenceParam;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hamcrest.Matchers;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.Encounter;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Patient;
+import org.hl7.fhir.dstu3.model.Provenance;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirEncounterService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class EncounterFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String ENCOUNTER_UUID = "123xx34-623hh34-22hj89-23hjy5";
+
+ private static final String WRONG_ENCOUNTER_UUID = "c3467w-hi4jer83-56hj34-23hjy5";
+
+ private static final String PATIENT_IDENTIFIER = "1003DE";
+
+ @Mock
+ private FhirEncounterService encounterService;
+
+ private EncounterFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.Encounter encounter;
+
+ @Before
+ public void setup() {
+ resourceProvider = new EncounterFhirResourceProvider();
+ resourceProvider.setEncounterService(encounterService);
+ }
+
+ @Before
+ public void initEncounter() {
+ encounter = new org.hl7.fhir.r4.model.Encounter();
+ encounter.setId(ENCOUNTER_UUID);
+ encounter.setStatus(org.hl7.fhir.r4.model.Encounter.EncounterStatus.UNKNOWN);
+ setProvenanceResources(encounter);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(Encounter.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(Encounter.class.getName()));
+ }
+
+ @Test
+ public void getEncounterByUuid_shouldReturnMatchingEncounter() {
+ IdType id = new IdType();
+ id.setValue(ENCOUNTER_UUID);
+ when(encounterService.get(ENCOUNTER_UUID)).thenReturn(encounter);
+ Encounter result = resourceProvider.getEncounterById(id);
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(ENCOUNTER_UUID));
+ assertThat(result.getStatus(), equalTo(Encounter.EncounterStatus.UNKNOWN));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getEncounterWithWrongUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_ENCOUNTER_UUID);
+ Encounter result = resourceProvider.getEncounterById(id);
+ assertThat(result, nullValue());
+ }
+
+ @Test
+ public void searchEncounters_shouldReturnMatchingEncounters() {
+ List encounters = new ArrayList<>();
+ encounters.add(encounter);
+ when(encounterService.searchForEncounters(any(), any(), any(), any())).thenReturn(encounters);
+
+ ReferenceAndListParam subjectreference = new ReferenceAndListParam();
+ subjectreference.addValue(new ReferenceOrListParam().add(new ReferenceParam().setChain(Patient.SP_NAME)));
+
+ Bundle results = resourceProvider.searchEncounter(null, null, null, subjectreference);
+ assertThat(results, notNullValue());
+ assertThat(results.getTotal(), equalTo(1));
+ assertThat(results.getEntry(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().fhirType(), equalTo("Encounter"));
+ assertThat(results.getEntry().get(0).getResource().getId(), equalTo(ENCOUNTER_UUID));
+ }
+
+ @Test
+ public void getEncounterHistory_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(ENCOUNTER_UUID);
+ when(encounterService.get(ENCOUNTER_UUID)).thenReturn(encounter);
+
+ List resources = resourceProvider.getEncounterHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(),
+ Matchers.equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getEncounterHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_ENCOUNTER_UUID);
+ assertThat(resourceProvider.getEncounterHistoryById(idType).isEmpty(), is(true));
+ assertThat(resourceProvider.getEncounterHistoryById(idType).size(), Matchers.equalTo(0));
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProviderTest.java
new file mode 100644
index 000000000..c3736475a
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProviderTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.Date;
+
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.r4.model.Annotation;
+import org.hl7.fhir.r4.model.ListResource;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.Cohort;
+import org.openmrs.module.fhir2.api.FhirListService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ListFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String LIST_UUID = "c0b1f314-1691-11df-97a5-7038c432aab88";
+
+ private static final String UNKNOWN_UUID = "c0b1f314-1691-11df-97a5-7038c432aab99";
+
+ private static final String TITLE = "Covid19 patients";
+
+ private static final String DESCRIPTION = "Covid19 patients";
+
+ @Mock
+ private FhirListService cohortFhirListService;
+
+ private ListFhirResourceProvider listFhirResourceProvider;
+
+ private ListResource list;
+
+ @Before
+ public void setUp() {
+ listFhirResourceProvider = new ListFhirResourceProvider();
+ listFhirResourceProvider.setListService(cohortFhirListService);
+ }
+
+ @Before
+ public void initListResource() {
+ list = new ListResource();
+ list.setId(LIST_UUID);
+ list.setTitle(TITLE);
+ list.setStatus(ListResource.ListStatus.CURRENT);
+ list.setDate(new Date());
+ list.setNote(Collections.singletonList(new Annotation().setText(DESCRIPTION)));
+ list.setMode(ListResource.ListMode.WORKING);
+
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(listFhirResourceProvider.getResourceType(), equalTo(org.hl7.fhir.dstu3.model.ListResource.class));
+ assertThat(listFhirResourceProvider.getResourceType().getName(),
+ equalTo(org.hl7.fhir.dstu3.model.ListResource.class.getName()));
+ }
+
+ @Test
+ public void getListById_shouldReturnMatchingList() {
+ when(cohortFhirListService.get(LIST_UUID)).thenReturn(list);
+ IdType id = new IdType();
+ id.setValue(LIST_UUID);
+ org.hl7.fhir.dstu3.model.ListResource result = listFhirResourceProvider.getListById(id);
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(LIST_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getListWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(UNKNOWN_UUID);
+ org.hl7.fhir.dstu3.model.ListResource result = listFhirResourceProvider.getListById(id);
+ assertThat(result, nullValue());
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProviderTest.java
new file mode 100644
index 000000000..8f37cbc3a
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProviderTest.java
@@ -0,0 +1,261 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.not;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.when;
+import static org.mockito.hamcrest.MockitoHamcrest.argThat;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.StringOrListParam;
+import ca.uhn.fhir.rest.param.StringParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hamcrest.Matchers;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.r4.model.Address;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.Location;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirLocationService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class LocationFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String LOCATION_UUID = "123xx34-623hh34-22hj89-23hjy5";
+
+ private static final String WRONG_LOCATION_UUID = "c3467w-hi4jer83-56hj34-23hjy5";
+
+ private static final String LOCATION_NAME = "chulaimbo";
+
+ private static final String CITY = "kakamega";
+
+ private static final String COUNTRY = "Kenya";
+
+ private static final String STATE = "Pan villa";
+
+ private static final String POSTAL_CODE = "234-30100";
+
+ private static final String LOGIN_LOCATION_TAG_NAME = "login";
+
+ private static final String LOGIN_LOCATION_TAG_DESCRIPTION = "Identify login locations";
+
+ @Mock
+ private FhirLocationService locationService;
+
+ private LocationFhirResourceProvider resourceProvider;
+
+ private Location location;
+
+ @Before
+ public void setup() {
+ resourceProvider = new LocationFhirResourceProvider();
+ resourceProvider.setLocationService(locationService);
+ }
+
+ @Before
+ public void initLocation() {
+ Address address = new Address();
+ address.setCity(CITY);
+ address.setCountry(COUNTRY);
+ address.setState(STATE);
+ address.setPostalCode(POSTAL_CODE);
+
+ location = new Location();
+ location.setId(LOCATION_UUID);
+ location.setName(LOCATION_NAME);
+ location.setAddress(address);
+ location.getMeta().addTag(new Coding(FhirConstants.OPENMRS_FHIR_EXT_LOCATION_TAG, LOGIN_LOCATION_TAG_NAME,
+ LOGIN_LOCATION_TAG_DESCRIPTION));
+ setProvenanceResources(location);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(org.hl7.fhir.dstu3.model.Location.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(org.hl7.fhir.dstu3.model.Location.class.getName()));
+ }
+
+ @Test
+ public void getLocationByUuid_shouldReturnMatchingLocation() {
+ when(locationService.get(LOCATION_UUID)).thenReturn(location);
+ IdType id = new IdType();
+ id.setValue(LOCATION_UUID);
+ org.hl7.fhir.dstu3.model.Location result = resourceProvider.getLocationById(id);
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(LOCATION_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getLocationWithWrongUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_LOCATION_UUID);
+ org.hl7.fhir.dstu3.model.Location result = resourceProvider.getLocationById(id);
+ assertThat(result, nullValue());
+ }
+
+ @Test
+ public void findLocationsByName_shouldReturnMatchingBundleOfLocations() {
+ StringAndListParam nameParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(LOCATION_NAME)));
+ when(locationService.searchForLocations(argThat(Matchers.is(nameParam)), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull())).thenReturn(Collections.singletonList(location));
+
+ Bundle results = resourceProvider.searchLocations(nameParam, null, null, null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void findLocationsByCity_shouldReturnMatchingBundleOfLocations() {
+ StringAndListParam cityParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(CITY)));
+ when(locationService.searchForLocations(isNull(), argThat(Matchers.is(cityParam)), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull())).thenReturn(Collections.singletonList(location));
+
+ Bundle results = resourceProvider.searchLocations(null, cityParam, null, null, null, null, null, null);
+
+ assertThat(results, Matchers.notNullValue());
+ assertThat(results.isResource(), Matchers.is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void findLocationsByCountry_shouldReturnMatchingBundleOfLocations() {
+ StringAndListParam countryParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(COUNTRY)));
+ when(locationService.searchForLocations(isNull(), isNull(), argThat(Matchers.is(countryParam)), isNull(), isNull(),
+ isNull(), isNull(), isNull())).thenReturn(Collections.singletonList(location));
+
+ Bundle results = resourceProvider.searchLocations(null, null, countryParam, null, null, null, null, null);
+
+ assertThat(results, Matchers.notNullValue());
+ assertThat(results.isResource(), Matchers.is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void findLocationsByState_shouldReturnMatchingBundleOfLocations() {
+ StringAndListParam stateParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(STATE)));
+ when(locationService.searchForLocations(isNull(), isNull(), isNull(), isNull(), argThat(Matchers.is(stateParam)),
+ isNull(), isNull(), isNull())).thenReturn(Collections.singletonList(location));
+
+ Bundle results = resourceProvider.searchLocations(null, null, null, null, stateParam, null, null, null);
+
+ assertThat(results, Matchers.notNullValue());
+ assertThat(results.isResource(), Matchers.is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void findLocationsByPostalCode_shouldReturnMatchingBundleOfLocations() {
+ StringAndListParam postalCodeParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(POSTAL_CODE)));
+ when(locationService.searchForLocations(isNull(), isNull(), isNull(), argThat(Matchers.is(postalCodeParam)),
+ isNull(), isNull(), isNull(), isNull())).thenReturn(Collections.singletonList(location));
+
+ Bundle results = resourceProvider.searchLocations(null, null, null, postalCodeParam, null, null, null, null);
+
+ assertThat(results, Matchers.notNullValue());
+ assertThat(results.isResource(), Matchers.is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void findLocationsByTags_shouldReturnLocationsContainingGivenTag() {
+ TokenAndListParam tag = new TokenAndListParam()
+ .addAnd(new TokenOrListParam(FhirConstants.OPENMRS_FHIR_EXT_LOCATION_TAG, LOGIN_LOCATION_TAG_NAME));
+ when(locationService.searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), argThat(Matchers.is(tag)),
+ isNull(), isNull())).thenReturn(Collections.singletonList(location));
+
+ Bundle results = resourceProvider.searchLocations(null, null, null, null, null, tag, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchLocations_shouldReturnMatchingBundleOfLocations() {
+ List locations = new ArrayList<>();
+ locations.add(location);
+ when(locationService.searchForLocations(any(), any(), any(), any(), any(), any(), any(), any()))
+ .thenReturn(locations);
+
+ StringAndListParam location = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(LOCATION_NAME)));
+
+ Bundle resultLocations = resourceProvider.searchLocations(location, null, null, null, null, null, null, null);
+
+ assertThat(resultLocations, notNullValue());
+ assertThat(resultLocations.isResource(), is(true));
+ assertThat(resultLocations.getTotal(), equalTo(1));
+ assertThat(resultLocations.getEntry(), notNullValue());
+ assertThat(resultLocations.getEntry().get(0).getResource().fhirType(), equalTo("Location"));
+ assertThat(resultLocations.getEntry().get(0).getResource().getId(), equalTo(LOCATION_UUID));
+ }
+
+ @Test
+ public void getLocationHistoryById_shouldReturnListOfResource() {
+ IdType id = new IdType();
+ id.setValue(LOCATION_UUID);
+ when(locationService.get(LOCATION_UUID)).thenReturn(location);
+
+ List resources = resourceProvider.getLocationHistoryById(id);
+ assertThat(resources, Matchers.notNullValue());
+ assertThat(resources, not(empty()));
+ assertThat(resources.size(), Matchers.equalTo(2));
+ }
+
+ @Test
+ public void getLocationHistoryById_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(LOCATION_UUID);
+ when(locationService.get(LOCATION_UUID)).thenReturn(location);
+
+ List resources = resourceProvider.getLocationHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), Matchers.is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(),
+ Matchers.equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getLocationHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(LOCATION_UUID);
+ assertThat(resourceProvider.getLocationHistoryById(idType).isEmpty(), Matchers.is(true));
+ assertThat(resourceProvider.getLocationHistoryById(idType).size(), Matchers.equalTo(0));
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProviderTest.java
new file mode 100644
index 000000000..22134e31f
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProviderTest.java
@@ -0,0 +1,193 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.when;
+import static org.mockito.hamcrest.MockitoHamcrest.argThat;
+
+import java.util.Collections;
+
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import ca.uhn.fhir.rest.param.TokenParam;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hamcrest.CoreMatchers;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Medication;
+import org.hl7.fhir.dstu3.model.OperationOutcome;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirMedicationService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MedicationFhirResourceProviderTest {
+
+ private static final String MEDICATION_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ private static final String WRONG_MEDICATION_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ private static final String CODE = "5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ @Mock
+ private FhirMedicationService fhirMedicationService;
+
+ private MedicationFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.Medication medication;
+
+ @Before
+ public void setup() {
+ resourceProvider = new MedicationFhirResourceProvider();
+ resourceProvider.setMedicationService(fhirMedicationService);
+
+ medication = new org.hl7.fhir.r4.model.Medication();
+ medication.setId(MEDICATION_UUID);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(Medication.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(Medication.class.getName()));
+ }
+
+ @Test
+ public void getMedicationByUuid_shouldReturnMatchingMedication() {
+ when(fhirMedicationService.get(MEDICATION_UUID)).thenReturn(medication);
+
+ IdType id = new IdType();
+ id.setValue(MEDICATION_UUID);
+ Medication medication = resourceProvider.getMedicationById(id);
+ assertThat(medication, notNullValue());
+ assertThat(medication.getId(), notNullValue());
+ assertThat(medication.getId(), equalTo(MEDICATION_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getMedicationByUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_MEDICATION_UUID);
+ Medication medication = resourceProvider.getMedicationById(id);
+ assertThat(medication, nullValue());
+ }
+
+ @Test
+ public void searchForMedication_shouldReturnMatchingBundleOfMedicationByCode() {
+ TokenAndListParam code = new TokenAndListParam();
+ code.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue(CODE)));
+
+ when(fhirMedicationService.searchForMedications(argThat(is(code)), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(medication));
+
+ Bundle results = resourceProvider.searchForMedication(code, null, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForMedication_shouldReturnMatchingBundleOfMedicationByDosageForm() {
+ TokenAndListParam dosageFormCode = new TokenAndListParam();
+ dosageFormCode.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue(CODE)));
+
+ when(fhirMedicationService.searchForMedications(isNull(), argThat(is(dosageFormCode)), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(medication));
+
+ Bundle results = resourceProvider.searchForMedication(null, dosageFormCode, null);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForMedication_shouldReturnMatchingBundleOfMedicationByStatus() {
+ TokenAndListParam status = new TokenAndListParam();
+ status.addAnd(new TokenOrListParam().addOr(new TokenParam().setValue("active")));
+
+ when(fhirMedicationService.searchForMedications(isNull(), isNull(), isNull(), argThat(is(status))))
+ .thenReturn(Collections.singletonList(medication));
+
+ Bundle results = resourceProvider.searchForMedication(null, null, status);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ // @Test
+ // public void shouldCreateNewMedication() {
+ // when(fhirMedicationService.create(medication)).thenReturn(medication);
+ //
+ // MethodOutcome result = resourceProvider.createMedication(VersionConvertor_30_40.convertMedication(medication));
+ // assertThat(result, CoreMatchers.notNullValue());
+ // assertThat(result.getCreated(), is(true));
+ // assertThat(result.getResource(), CoreMatchers.equalTo(VersionConvertor_30_40.convertMedication(medication)));
+ // }
+ //
+ // @Test
+ // public void shouldUpdateMedication() {
+ // org.hl7.fhir.r4.model.Medication med = medication;
+ // med.setStatus(org.hl7.fhir.r4.model.Medication.MedicationStatus.INACTIVE);
+ //
+ // when(fhirMedicationService.update(MEDICATION_UUID, medication)).thenReturn(med);
+ //
+ // MethodOutcome result = resourceProvider.updateMedication(new IdType().setValue(MEDICATION_UUID), VersionConvertor_30_40.convertMedication(medication));
+ // assertThat(result, CoreMatchers.notNullValue());
+ // assertThat(result.getResource(), CoreMatchers.equalTo(med));
+ // }
+ //
+ // @Test(expected = InvalidRequestException.class)
+ // public void updateMedicationShouldThrowInvalidRequestForUuidMismatch() {
+ // when(fhirMedicationService.update(WRONG_MEDICATION_UUID, medication)).thenThrow(InvalidRequestException.class);
+ //
+ // resourceProvider.updateMedication(new IdType().setValue(WRONG_MEDICATION_UUID), VersionConvertor_30_40.convertMedication(medication));
+ // }
+ //
+ // @Test(expected = MethodNotAllowedException.class)
+ // public void updateMedicationShouldThrowMethodNotAllowedIfDoesNotExist() {
+ // org.hl7.fhir.r4.model.Medication wrongMedication = new org.hl7.fhir.r4.model.Medication();
+ //
+ // wrongMedication.setId(WRONG_MEDICATION_UUID);
+ //
+ // when(fhirMedicationService.update(WRONG_MEDICATION_UUID, wrongMedication))
+ // .thenThrow(MethodNotAllowedException.class);
+ //
+ // resourceProvider.updateMedication(new IdType().setValue(WRONG_MEDICATION_UUID), VersionConvertor_30_40.convertMedication(wrongMedication));
+ // }
+
+ @Test
+ public void shouldDeleteMedication() {
+ org.hl7.fhir.r4.model.Medication med = medication;
+ med.setStatus(org.hl7.fhir.r4.model.Medication.MedicationStatus.INACTIVE);
+
+ when(fhirMedicationService.delete(MEDICATION_UUID)).thenReturn(med);
+
+ OperationOutcome result = resourceProvider.deleteMedication(new IdType().setValue(MEDICATION_UUID));
+ assertThat(result, CoreMatchers.notNullValue());
+ assertThat(result.getId(), equalTo(MEDICATION_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void deleteMedicationShouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_MEDICATION_UUID);
+ OperationOutcome medication = resourceProvider.deleteMedication(id);
+ assertThat(medication, nullValue());
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProviderTest.java
new file mode 100644
index 000000000..cb65483b2
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProviderTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.when;
+
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.MedicationRequest;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirMedicationRequestService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MedicationRequestFhirResourceProviderTest {
+
+ private static final String MEDICATION_REQUEST_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ private static final String WRONG_MEDICATION_REQUEST_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ @Mock
+ private FhirMedicationRequestService fhirMedicationRequestService;
+
+ private MedicationRequestFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.MedicationRequest medicationRequest;
+
+ @Before
+ public void setup() {
+ resourceProvider = new MedicationRequestFhirResourceProvider();
+ resourceProvider.setMedicationRequestService(fhirMedicationRequestService);
+
+ medicationRequest = new org.hl7.fhir.r4.model.MedicationRequest();
+ medicationRequest.setId(MEDICATION_REQUEST_UUID);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(MedicationRequest.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(MedicationRequest.class.getName()));
+ }
+
+ @Test
+ public void getMedicationRequestByUuid_shouldReturnMatchingMedicationRequest() {
+ when(fhirMedicationRequestService.get(MEDICATION_REQUEST_UUID)).thenReturn(medicationRequest);
+
+ IdType id = new IdType();
+ id.setValue(MEDICATION_REQUEST_UUID);
+ MedicationRequest medicationRequest = resourceProvider.getMedicationRequestById(id);
+ assertThat(medicationRequest, notNullValue());
+ assertThat(medicationRequest.getId(), notNullValue());
+ assertThat(medicationRequest.getId(), equalTo(MEDICATION_REQUEST_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getMedicationRequestByUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_MEDICATION_REQUEST_UUID);
+ MedicationRequest medicationRequest = resourceProvider.getMedicationRequestById(id);
+ assertThat(medicationRequest, nullValue());
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProviderTest.java
new file mode 100644
index 000000000..3510977e3
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProviderTest.java
@@ -0,0 +1,162 @@
+/*
+ * 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.
+ */
+/*
+* 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.api.server.IBundleProvider;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenParam;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hamcrest.Matchers;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Observation;
+import org.hl7.fhir.dstu3.model.Provenance;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirObservationService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ObservationFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String OBSERVATION_UUID = "1223h34-34nj3-34nj34-34nj";
+
+ private static final String WRONG_OBSERVATION_UUID = "hj243h34-cb4vsd-34xxx34-ope4jj";
+
+ @Mock
+ private FhirObservationService observationService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private ObservationFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.Observation observation;
+
+ @Before
+ public void setup() {
+ resourceProvider = new ObservationFhirResourceProvider();
+ resourceProvider.setObservationService(observationService);
+ }
+
+ @Before
+ public void initObservation() {
+ observation = new org.hl7.fhir.r4.model.Observation();
+ observation.setId(OBSERVATION_UUID);
+ observation.setStatus(org.hl7.fhir.r4.model.Observation.ObservationStatus.UNKNOWN);
+ setProvenanceResources(observation);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(Observation.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(Observation.class.getName()));
+ }
+
+ @Test
+ public void getObservationByUuid_shouldReturnMatchingObservation() {
+ when(observationService.get(OBSERVATION_UUID)).thenReturn(observation);
+ IdType id = new IdType();
+ id.setValue(OBSERVATION_UUID);
+
+ Observation result = resourceProvider.getObservationById(id);
+
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(OBSERVATION_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getObservationWithWrongUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_OBSERVATION_UUID);
+
+ resourceProvider.getObservationById(id);
+ }
+
+ @Test
+ public void searchObservations_shouldReturnMatchingObservations() {
+ observation = new org.hl7.fhir.r4.model.Observation();
+ observation.setId(OBSERVATION_UUID);
+
+ when(observationService.searchForObservations(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
+ .thenReturn(new BaseFhirIBundleResourceProviderTest<>(Collections.singletonList(observation), 10, 1));
+
+ TokenAndListParam code = new TokenAndListParam();
+ TokenParam codingToken = new TokenParam();
+ codingToken.setValue("1000");
+ code.addAnd(codingToken);
+
+ IBundleProvider results = resourceProvider.searchObservations(null, null, null, null, null, null, null, null, code,
+ null);
+ assertThat(results, notNullValue());
+ assertThat(results.getResources(1, 5), hasSize(equalTo(1)));
+ assertThat(results.getResources(1, 5).get(0), notNullValue());
+ assertThat(results.getResources(1, 5).get(0).fhirType(), equalTo("Observation"));
+ assertThat(results.getResources(1, 5).get(0).getIdElement().getIdPart(), equalTo(OBSERVATION_UUID));
+ }
+
+ @Test
+ public void getPatientResourceHistory_shouldReturnListOfResource() {
+ IdType id = new IdType();
+ id.setValue(OBSERVATION_UUID);
+ when(observationService.get(OBSERVATION_UUID)).thenReturn(observation);
+
+ List resources = resourceProvider.getObservationHistoryById(id);
+ assertThat(resources, Matchers.notNullValue());
+ assertThat(resources, not(empty()));
+ assertThat(resources.size(), Matchers.equalTo(2));
+ }
+
+ @Test
+ public void getPatientResourceHistory_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(OBSERVATION_UUID);
+ when(observationService.get(OBSERVATION_UUID)).thenReturn(observation);
+
+ List resources = resourceProvider.getObservationHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(),
+ Matchers.equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getPatientHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_OBSERVATION_UUID);
+ assertThat(resourceProvider.getObservationHistoryById(idType).isEmpty(), is(true));
+ assertThat(resourceProvider.getObservationHistoryById(idType).size(), Matchers.equalTo(0));
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirR3ResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirR3ResourceProviderTest.java
new file mode 100644
index 000000000..cfcba7e64
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirR3ResourceProviderTest.java
@@ -0,0 +1,339 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.when;
+import static org.mockito.hamcrest.MockitoHamcrest.argThat;
+
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.StringOrListParam;
+import ca.uhn.fhir.rest.param.StringParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Patient;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.r4.model.*;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirPatientService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PatientFhirR3ResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String PATIENT_UUID = "017312a1-cf56-43ab-ae87-44070b801d1c";
+
+ private static final String WRONG_PATIENT_UUID = "017312a1-cf56-43ab-ae87-44070b801d1c";
+
+ private static final String NAME = "Rick";
+
+ private static final String GIVEN_NAME = "Nihilism";
+
+ private static final String FAMILY_NAME = "Sanchez";
+
+ private static final String GENDER = "male";
+
+ private static final String IDENTIFIER = "M10000";
+
+ private static final String BIRTH_DATE = "1947-04-01";
+
+ private static final String DEATH_DATE = "2019-12-15";
+
+ private static final String CITY = "Seattle";
+
+ private static final String STATE = "Washington";
+
+ private static final String COUNTRY = "Washington";
+
+ private static final String POSTAL_CODE = "98136";
+
+ @Mock
+ private FhirPatientService patientService;
+
+ private PatientFhirResourceProvider patientFhirResourceProvider;
+
+ private org.hl7.fhir.r4.model.Patient patient;
+
+ @Before
+ public void setup() {
+ patientFhirResourceProvider = new PatientFhirResourceProvider();
+ patientFhirResourceProvider.setPatientService(patientService);
+ }
+
+ @Before
+ public void initPatient() {
+ HumanName name = new HumanName();
+ name.addGiven(GIVEN_NAME);
+ name.setFamily(FAMILY_NAME);
+
+ patient = new org.hl7.fhir.r4.model.Patient();
+ patient.setId(PATIENT_UUID);
+ patient.addName(name);
+ patient.setActive(true);
+ patient.setBirthDate(new Date());
+ patient.setGender(Enumerations.AdministrativeGender.MALE);
+ setProvenanceResources(patient);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(patientFhirResourceProvider.getResourceType(), equalTo(Patient.class));
+ assertThat(patientFhirResourceProvider.getResourceType().getName(), equalTo(Patient.class.getName()));
+ }
+
+ @Test
+ public void getPatientById_shouldReturnPatient() {
+ IdType id = new IdType();
+ id.setValue(PATIENT_UUID);
+ when(patientService.get(PATIENT_UUID)).thenReturn(patient);
+
+ Patient result = patientFhirResourceProvider.getPatientById(id);
+
+ assertThat(result.isResource(), is(true));
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(PATIENT_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getPatientByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_PATIENT_UUID);
+ assertThat(patientFhirResourceProvider.getPatientById(idType).isResource(), is(true));
+ assertThat(patientFhirResourceProvider.getPatientById(idType), nullValue());
+ }
+
+ @Test
+ public void searchPatients_shouldReturnMatchingBundleOfPatientsByName() {
+ StringAndListParam nameParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(NAME)));
+ when(patientService.searchForPatients(argThat(is(nameParam)), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(nameParam, null, null, null, null, null, null, null,
+ null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchPatients_shouldReturnMatchingBundleOfPatientsByGivenName() {
+ StringAndListParam givenNameParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(NAME)));
+ when(patientService.searchForPatients(isNull(), argThat(is(givenNameParam)), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, givenNameParam, null, null, null, null, null, null,
+ null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchPatients_shouldReturnMatchingBundleOfPatientsByFamilyName() {
+ StringAndListParam familyNameParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(FAMILY_NAME)));
+ when(patientService.searchForPatients(isNull(), isNull(), argThat(is(familyNameParam)), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, familyNameParam, null, null, null, null,
+ null, null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchPatients_shouldReturnMatchingBundleOfPatientsByIdentifier() {
+ TokenAndListParam identifierParam = new TokenAndListParam().addAnd(new TokenOrListParam().add(IDENTIFIER));
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), argThat(is(identifierParam)), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, identifierParam, null, null, null,
+ null, null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByGender() {
+ TokenAndListParam genderParam = new TokenAndListParam().addAnd(new TokenOrListParam().add(GENDER));
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), argThat(is(genderParam)), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, genderParam, null, null, null,
+ null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByBirthDate() {
+ DateRangeParam birthDateParam = new DateRangeParam().setLowerBound(BIRTH_DATE).setUpperBound(BIRTH_DATE);
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), argThat(is(birthDateParam)),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, null, birthDateParam, null, null,
+ null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByDeathDate() {
+ DateRangeParam deathDateParam = new DateRangeParam().setLowerBound(DEATH_DATE).setUpperBound(DEATH_DATE);
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ argThat(is(deathDateParam)), isNull(), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, null, null, deathDateParam, null,
+ null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByDeceased() {
+ TokenAndListParam deceasedParam = new TokenAndListParam().addAnd(new TokenOrListParam().add("true"));
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ argThat(is(deceasedParam)), isNull(), isNull(), isNull(), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, null, null, null, deceasedParam,
+ null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByCity() {
+ StringAndListParam cityParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(CITY)));
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ argThat(is(cityParam)), isNull(), isNull(), isNull(), isNull())).thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, null, null, null, null,
+ cityParam, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByState() {
+ StringAndListParam stateParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(STATE)));
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), argThat(is(stateParam)), isNull(), isNull(), isNull())).thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, null, null, null, null, null,
+ stateParam, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByPostalCode() {
+ StringAndListParam postalCodeParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(POSTAL_CODE)));
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), argThat(is(postalCodeParam)), isNull(), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, null, null, null, null, null,
+ null, postalCodeParam, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPatients_shouldReturnMatchingBundleOfPatientsByCountry() {
+ StringAndListParam countryParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(COUNTRY)));
+ when(patientService.searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), argThat(is(countryParam)), isNull()))
+ .thenReturn(Collections.singletonList(patient));
+
+ Bundle results = patientFhirResourceProvider.searchPatients(null, null, null, null, null, null, null, null, null,
+ null, null, countryParam, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void getPatientResourceHistory_shouldReturnListOfResource() {
+ IdType id = new IdType();
+ id.setValue(PATIENT_UUID);
+ when(patientService.get(PATIENT_UUID)).thenReturn(patient);
+
+ List resources = patientFhirResourceProvider.getPatientHistoryById(id);
+ assertThat(resources, notNullValue());
+ assertThat(resources, not(empty()));
+ assertThat(resources.size(), equalTo(2));
+ }
+
+ @Test
+ public void getPatientResourceHistory_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(PATIENT_UUID);
+ when(patientService.get(PATIENT_UUID)).thenReturn(patient);
+
+ List resources = patientFhirResourceProvider.getPatientHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(), equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getPatientHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_PATIENT_UUID);
+ assertThat(patientFhirResourceProvider.getPatientHistoryById(idType).isEmpty(), is(true));
+ assertThat(patientFhirResourceProvider.getPatientHistoryById(idType).size(), equalTo(0));
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProviderTest.java
new file mode 100644
index 000000000..da2ce905d
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProviderTest.java
@@ -0,0 +1,250 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.when;
+import static org.mockito.hamcrest.MockitoHamcrest.argThat;
+
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.StringOrListParam;
+import ca.uhn.fhir.rest.param.StringParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Person;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.r4.model.Enumerations;
+import org.hl7.fhir.r4.model.HumanName;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirPersonService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PersonFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String PERSON_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
+
+ private static final String WRONG_PERSON_UUID = "9bf0d1ac-62a8-4440-a5a1-eb1015a7cc65";
+
+ private static final String GENDER = "M";
+
+ private static final String BIRTH_DATE = "1992-03-04";
+
+ private static final String GIVEN_NAME = "Jeanne";
+
+ private static final String FAMILY_NAME = "we";
+
+ private static final String CITY = "Seattle";
+
+ private static final String STATE = "Washington";
+
+ private static final String POSTAL_CODE = "98136";
+
+ private static final String COUNTRY = "Canada";
+
+ @Mock
+ private FhirPersonService fhirPersonService;
+
+ private PersonFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.Person person;
+
+ @Before
+ public void setup() {
+ resourceProvider = new PersonFhirResourceProvider();
+ resourceProvider.setPersonService(fhirPersonService);
+ }
+
+ @Before
+ public void initPerson() {
+ HumanName name = new HumanName();
+ name.addGiven(GIVEN_NAME);
+ name.setFamily(FAMILY_NAME);
+
+ person = new org.hl7.fhir.r4.model.Person();
+ person.setId(PERSON_UUID);
+ person.setGender(Enumerations.AdministrativeGender.MALE);
+ person.addName(name);
+ setProvenanceResources(person);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(Person.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(Person.class.getName()));
+ }
+
+ @Test
+ public void getPersonById_shouldReturnPerson() {
+ IdType id = new IdType();
+ id.setValue(PERSON_UUID);
+ when(fhirPersonService.get(PERSON_UUID)).thenReturn(person);
+
+ Person result = resourceProvider.getPersonById(id);
+ assertThat(result.isResource(), is(true));
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(PERSON_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getPersonByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_PERSON_UUID);
+ assertThat(resourceProvider.getPersonById(idType).isResource(), is(true));
+ assertThat(resourceProvider.getPersonById(idType), nullValue());
+ }
+
+ @Test
+ public void searchPeople_shouldReturnMatchingBundleOfPeopleByName() {
+ StringAndListParam nameParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(GIVEN_NAME)));
+ when(fhirPersonService.searchForPeople(argThat(is(nameParam)), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull())).thenReturn(Collections.singletonList(person));
+
+ Bundle results = resourceProvider.searchPeople(nameParam, null, null, null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPeople_shouldReturnMatchingBundleOfPeopleByGender() {
+ TokenAndListParam genderParam = new TokenAndListParam().addAnd(new TokenOrListParam().add(GENDER));
+ when(fhirPersonService.searchForPeople(isNull(), argThat(is(genderParam)), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull())).thenReturn(Collections.singletonList(person));
+
+ Bundle results = resourceProvider.searchPeople(null, genderParam, null, null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPeople_shouldReturnMatchingBundleOfPeopleByBirthDate() {
+ DateRangeParam birthDateParam = new DateRangeParam().setLowerBound(BIRTH_DATE).setUpperBound(BIRTH_DATE);
+ when(fhirPersonService.searchForPeople(isNull(), isNull(), argThat(is(birthDateParam)), isNull(), isNull(), isNull(),
+ isNull(), isNull())).thenReturn(Collections.singletonList(person));
+
+ Bundle results = resourceProvider.searchPeople(null, null, birthDateParam, null, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPeople_shouldReturnMatchingBundleOfPeopleByCity() {
+ StringAndListParam cityParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(CITY)));
+ when(fhirPersonService.searchForPeople(isNull(), isNull(), isNull(), argThat(is(cityParam)), isNull(), isNull(),
+ isNull(), isNull())).thenReturn(Collections.singletonList(person));
+
+ Bundle results = resourceProvider.searchPeople(null, null, null, cityParam, null, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPeople_shouldReturnMatchingBundleOfPeopleByState() {
+ StringAndListParam stateParam = new StringAndListParam().addAnd(new StringOrListParam().add(new StringParam(STATE)));
+ when(fhirPersonService.searchForPeople(isNull(), isNull(), isNull(), isNull(), argThat(is(stateParam)), isNull(),
+ isNull(), isNull())).thenReturn(Collections.singletonList(person));
+
+ Bundle results = resourceProvider.searchPeople(null, null, null, null, stateParam, null, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPeople_shouldReturnMatchingBundleOfPeopleByPostalCode() {
+ StringAndListParam postalCodeParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(POSTAL_CODE)));
+ when(fhirPersonService.searchForPeople(isNull(), isNull(), isNull(), isNull(), isNull(),
+ argThat(is(postalCodeParam)), isNull(), isNull())).thenReturn(Collections.singletonList(person));
+
+ Bundle results = resourceProvider.searchPeople(null, null, null, null, null, postalCodeParam, null, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void searchForPeople_shouldReturnMatchingBundleOfPeopleByCountry() {
+ StringAndListParam countryParam = new StringAndListParam()
+ .addAnd(new StringOrListParam().add(new StringParam(COUNTRY)));
+ when(fhirPersonService.searchForPeople(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ argThat(is(countryParam)), isNull())).thenReturn(Collections.singletonList(person));
+
+ Bundle results = resourceProvider.searchPeople(null, null, null, null, null, null, countryParam, null);
+
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void getPatientResourceHistory_shouldReturnListOfResource() {
+ IdType id = new IdType();
+ id.setValue(PERSON_UUID);
+ when(fhirPersonService.get(PERSON_UUID)).thenReturn(person);
+
+ List resources = resourceProvider.getPersonHistoryById(id);
+ assertThat(resources, notNullValue());
+ assertThat(resources, not(empty()));
+ assertThat(resources.size(), equalTo(2));
+ }
+
+ @Test
+ public void getPatientResourceHistory_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(PERSON_UUID);
+ when(fhirPersonService.get(PERSON_UUID)).thenReturn(person);
+
+ List resources = resourceProvider.getPersonHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(), equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getPatientHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_PERSON_UUID);
+ assertThat(resourceProvider.getPersonHistoryById(idType).isEmpty(), is(true));
+ assertThat(resourceProvider.getPersonHistoryById(idType).size(), equalTo(0));
+ }
+
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProviderTest.java
new file mode 100644
index 000000000..48d39a9d2
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProviderTest.java
@@ -0,0 +1,185 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.not;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hamcrest.Matchers;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Practitioner;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.r4.model.HumanName;
+import org.hl7.fhir.r4.model.Identifier;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirPractitionerService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PractitionerFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String PRACTITIONER_UUID = "48fb709b-48aa-4902-b681-926df5156e88";
+
+ private static final String WRONG_PRACTITIONER_UUID = "f8bc0122-21db-4e91-a5d3-92ae01cafe92";
+
+ private static final String GIVEN_NAME = "James";
+
+ private static final String FAMILY_NAME = "pope";
+
+ private static final String WRONG_NAME = "wrong name";
+
+ private static final String PRACTITIONER_IDENTIFIER = "nurse";
+
+ private static final String WRONG_PRACTITIONER_IDENTIFIER = "wrong identifier";
+
+ @Mock
+ private FhirPractitionerService practitionerService;
+
+ private PractitionerFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.Practitioner practitioner;
+
+ @Before
+ public void setup() {
+ resourceProvider = new PractitionerFhirResourceProvider();
+ resourceProvider.setPractitionerService(practitionerService);
+ }
+
+ @Before
+ public void initPractitioner() {
+ HumanName name = new HumanName();
+ name.addGiven(GIVEN_NAME);
+ name.setFamily(FAMILY_NAME);
+
+ Identifier theIdentifier = new Identifier();
+ theIdentifier.setValue(PRACTITIONER_IDENTIFIER);
+
+ practitioner = new org.hl7.fhir.r4.model.Practitioner();
+ practitioner.setId(PRACTITIONER_UUID);
+ practitioner.addName(name);
+ practitioner.addIdentifier(theIdentifier);
+ setProvenanceResources(practitioner);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(Practitioner.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(Practitioner.class.getName()));
+ }
+
+ @Test
+ public void getPractitionerById_shouldReturnPractitioner() {
+ IdType id = new IdType();
+ id.setValue(PRACTITIONER_UUID);
+ when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
+
+ Practitioner result = resourceProvider.getPractitionerById(id);
+ assertThat(result.isResource(), is(true));
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(PRACTITIONER_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getPractitionerByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_PRACTITIONER_UUID);
+ assertThat(resourceProvider.getPractitionerById(idType).isResource(), is(true));
+ assertThat(resourceProvider.getPractitionerById(idType), nullValue());
+ }
+
+ @Test
+ public void findPractitionersByName_shouldReturnMatchingBundleOfPractitioners() {
+ when(practitionerService.findPractitionerByName(GIVEN_NAME)).thenReturn(Collections.singletonList(practitioner));
+
+ Bundle results = resourceProvider.findPractitionersByName(GIVEN_NAME);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ assertThat(results.getEntryFirstRep().getResource().getChildByName("name").hasValues(), is(true));
+ }
+
+ @Test
+ public void findPractitionersByWrongName_shouldReturnBundleWithEmptyEntries() {
+ Bundle results = resourceProvider.findPractitionersByName(WRONG_NAME);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry(), is(empty()));
+ }
+
+ @Test
+ public void findPractitionersByIdentifier_shouldReturnMatchingBundleOfPractitioners() {
+ when(practitionerService.findPractitionerByIdentifier(PRACTITIONER_IDENTIFIER))
+ .thenReturn(Collections.singletonList(practitioner));
+
+ Bundle results = resourceProvider.findPractitionersByIdentifier(PRACTITIONER_IDENTIFIER);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry().size(), greaterThanOrEqualTo(1));
+ assertThat(results.getEntryFirstRep().getResource().getChildByName("identifier").hasValues(), is(true));
+ }
+
+ @Test
+ public void findPractitionersByWrongIdentifier_shouldReturnBundleWithEmptyEntries() {
+ Bundle results = resourceProvider.findPractitionersByIdentifier(WRONG_PRACTITIONER_IDENTIFIER);
+ assertThat(results, notNullValue());
+ assertThat(results.isResource(), is(true));
+ assertThat(results.getEntry(), is(empty()));
+ }
+
+ @Test
+ public void getPractitionerHistoryById_shouldReturnListOfResource() {
+ IdType id = new IdType();
+ id.setValue(PRACTITIONER_UUID);
+ when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
+
+ List resources = resourceProvider.getPractitionerHistoryById(id);
+ assertThat(resources, Matchers.notNullValue());
+ assertThat(resources, not(empty()));
+ assertThat(resources.size(), Matchers.equalTo(2));
+ }
+
+ @Test
+ public void getPractitionerHistoryById_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(PRACTITIONER_UUID);
+ when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
+
+ List resources = resourceProvider.getPractitionerHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), Matchers.is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(),
+ Matchers.equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getPractitionerHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_PRACTITIONER_UUID);
+ assertThat(resourceProvider.getPractitionerHistoryById(idType).isEmpty(), Matchers.is(true));
+ assertThat(resourceProvider.getPractitionerHistoryById(idType).size(), Matchers.equalTo(0));
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProviderTest.java
new file mode 100644
index 000000000..2d2ca3055
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProviderTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.ProcedureRequest;
+import org.hl7.fhir.r4.model.ServiceRequest;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirServiceRequestService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ProcedureRequestFhirResourceProviderTest {
+
+ private static final String SERVICE_REQUEST_UUID = "7d13b03b-58c2-43f5-b34d-08750c51aea9";
+
+ private static final String WRONG_SERVICE_REQUEST_UUID = "92b04062-e57d-43aa-8c38-90a1ad70080c";
+
+ @Mock
+ private FhirServiceRequestService serviceRequestService;
+
+ private ProcedureRequestFhirResourceProvider resourceProvider;
+
+ private ServiceRequest serviceRequest;
+
+ @Before
+ public void setup() {
+ resourceProvider = new ProcedureRequestFhirResourceProvider();
+ resourceProvider.setServiceRequestService(serviceRequestService);
+ }
+
+ @Before
+ public void initServiceRequest() {
+ serviceRequest = new ServiceRequest();
+ serviceRequest.setId(SERVICE_REQUEST_UUID);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(ProcedureRequest.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(ProcedureRequest.class.getName()));
+ }
+
+ @Test
+ public void getServiceRequestById_shouldReturnServiceRequest() {
+ IdType id = new IdType();
+ id.setValue(SERVICE_REQUEST_UUID);
+ when(serviceRequestService.get(SERVICE_REQUEST_UUID)).thenReturn(serviceRequest);
+
+ ProcedureRequest result = resourceProvider.getProcedureRequestById(id);
+ assertThat(result.isResource(), is(true));
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(SERVICE_REQUEST_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getServiceRequestByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_SERVICE_REQUEST_UUID);
+ assertThat(resourceProvider.getProcedureRequestById(idType).isResource(), is(true));
+ assertThat(resourceProvider.getProcedureRequestById(idType), nullValue());
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProviderTest.java
new file mode 100644
index 000000000..5b45c82a9
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProviderTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.RelatedPerson;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirRelatedPersonService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RelatedPersonFhirResourceProviderTest {
+
+ private static final String RELATED_PERSON_UUID = "23f620c3-2ecb-4d80-aea8-44fa1c5ff978";
+
+ private static final String WRONG_RELATED_PERSON_UUID = "ca0dfd38-ee20-41a6-909e-7d84247ca192";
+
+ @Mock
+ private FhirRelatedPersonService relatedPersonService;
+
+ private org.hl7.fhir.r4.model.RelatedPerson relatedPerson;
+
+ private RelatedPersonFhirResourceProvider resourceProvider;
+
+ @Before
+ public void setUp() {
+ resourceProvider = new RelatedPersonFhirResourceProvider();
+ resourceProvider.setRelatedPersonService(relatedPersonService);
+ }
+
+ @Before
+ public void initRelatedPerson() {
+ relatedPerson = new org.hl7.fhir.r4.model.RelatedPerson();
+ relatedPerson.setId(RELATED_PERSON_UUID);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(RelatedPerson.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(RelatedPerson.class.getName()));
+ }
+
+ @Test
+ public void getRelatedPersonById_shouldReturnMatchingRelatedPerson() {
+ when(relatedPersonService.get(RELATED_PERSON_UUID)).thenReturn(relatedPerson);
+ IdType id = new IdType();
+ id.setValue(RELATED_PERSON_UUID);
+ RelatedPerson relatedPerson = resourceProvider.getRelatedPersonById(id);
+ assertThat(relatedPerson, notNullValue());
+ assertThat(relatedPerson.getId(), notNullValue());
+ assertThat(relatedPerson.getId(), equalTo(RELATED_PERSON_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getRelatedPersonWithWrongUuid_shouldThrowResourceNotFoundException() {
+ IdType id = new IdType();
+ id.setValue(WRONG_RELATED_PERSON_UUID);
+ RelatedPerson result = resourceProvider.getRelatedPersonById(id);
+ assertThat(result, nullValue());
+ }
+}
diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderTest.java
new file mode 100644
index 000000000..19db8e06a
--- /dev/null
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderTest.java
@@ -0,0 +1,194 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.not;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+
+import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hamcrest.Matchers;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Resource;
+import org.hl7.fhir.dstu3.model.Task;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirTaskService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TaskFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest {
+
+ private static final String TASK_UUID = "bdd7e368-3d1a-42a9-9538-395391b64adf";
+
+ private static final String WRONG_TASK_UUID = "df34a1c1-f57b-4c33-bee5-e601b56b9d5b";
+
+ @Mock
+ private FhirTaskService taskService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private TaskFhirResourceProvider resourceProvider;
+
+ private org.hl7.fhir.r4.model.Task task;
+
+ @Before
+ public void setup() {
+ resourceProvider = new TaskFhirResourceProvider();
+ resourceProvider.setFhirTaskService(taskService);
+ }
+
+ @Before
+ public void initTask() {
+ task = new org.hl7.fhir.r4.model.Task();
+ task.setId(TASK_UUID);
+ setProvenanceResources(task);
+ }
+
+ @Test
+ public void getResourceType_shouldReturnResourceType() {
+ assertThat(resourceProvider.getResourceType(), equalTo(Task.class));
+ assertThat(resourceProvider.getResourceType().getName(), equalTo(Task.class.getName()));
+ }
+
+ @Test
+ public void getTaskById_shouldReturnMatchingTask() {
+ IdType id = new IdType();
+ id.setValue(TASK_UUID);
+ when(taskService.get(TASK_UUID)).thenReturn(task);
+
+ Task result = resourceProvider.getTaskById(id);
+
+ assertThat(result.isResource(), is(true));
+ assertThat(result, notNullValue());
+ assertThat(result.getId(), notNullValue());
+ assertThat(result.getId(), equalTo(TASK_UUID));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getTaskByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_TASK_UUID);
+
+ assertThat(resourceProvider.getTaskById(idType).isResource(), is(true));
+ assertThat(resourceProvider.getTaskById(idType), nullValue());
+ }
+
+ @Test
+ public void getTaskHistoryById_shouldReturnListOfResource() {
+ IdType id = new IdType();
+ id.setValue(TASK_UUID);
+ when(taskService.get(TASK_UUID)).thenReturn(task);
+
+ List resources = resourceProvider.getTaskHistoryById(id);
+ assertThat(resources, Matchers.notNullValue());
+ assertThat(resources, not(empty()));
+ assertThat(resources.size(), Matchers.equalTo(2));
+ }
+
+ @Test
+ public void getTaskHistoryById_shouldReturnProvenanceResources() {
+ IdType id = new IdType();
+ id.setValue(TASK_UUID);
+ when(taskService.get(TASK_UUID)).thenReturn(task);
+
+ List resources = resourceProvider.getTaskHistoryById(id);
+ assertThat(resources, not(empty()));
+ assertThat(resources.stream().findAny().isPresent(), Matchers.is(true));
+ assertThat(resources.stream().findAny().get().getResourceType().name(),
+ Matchers.equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test(expected = ResourceNotFoundException.class)
+ public void getTaskHistoryByWithWrongId_shouldThrowResourceNotFoundException() {
+ IdType idType = new IdType();
+ idType.setValue(WRONG_TASK_UUID);
+ assertThat(resourceProvider.getTaskHistoryById(idType).isEmpty(), Matchers.is(true));
+ assertThat(resourceProvider.getTaskHistoryById(idType).size(), Matchers.equalTo(0));
+ }
+
+ // @Test
+ // public void createTask_shouldCreateNewTask() {
+ // when(taskService.saveTask(task)).thenReturn(task);
+ //
+ // MethodOutcome result = resourceProvider.createTask(TaskVersionConverter.convertTask(task));
+ // assertThat(result.getResource(), equalTo(task));
+ // }
+ //
+ // @Test
+ // public void updateTask_shouldUpdateTask() {
+ // when(taskService.updateTask(TASK_UUID, task)).thenReturn(task);
+ //
+ // IdType uuid = new IdType();
+ // uuid.setValue(TASK_UUID);
+ //
+ // MethodOutcome result = resourceProvider.updateTask(uuid, TaskVersionConverter.convertTask(task));
+ // assertThat(result.getResource(), equalTo(task));
+ // }
+ //
+ // @Test(expected = InvalidRequestException.class)
+ // public void updateTask_shouldThrowInvalidRequestForTaskUuidMismatch() {
+ // when(taskService.updateTask(WRONG_TASK_UUID, task)).thenThrow(InvalidRequestException.class);
+ //
+ // resourceProvider.updateTask(new IdType().setValue(WRONG_TASK_UUID), TaskVersionConverter.convertTask(task));
+ // }
+ //
+ // @Test(expected = InvalidRequestException.class)
+ // public void updateTask_shouldThrowInvalidRequestIfTaskHasNoUuid() {
+ // Task noIdTask = new Task();
+ //
+ // when(taskService.updateTask(TASK_UUID, TaskVersionConverter.convertTask(noIdTask))).thenThrow(InvalidRequestException.class);
+ //
+ // resourceProvider.updateTask(new IdType().setValue(TASK_UUID), noIdTask);
+ // }
+ //
+ // @Test(expected = MethodNotAllowedException.class)
+ // public void updateTask_shouldThrowMethodNotAllowedIfTaskDoesNotExist() {
+ // Task wrongTask = new Task();
+ // wrongTask.setId(WRONG_TASK_UUID);
+ //
+ // when(taskService.updateTask(WRONG_TASK_UUID, TaskVersionConverter.convertTask(wrongTask))).thenThrow(MethodNotAllowedException.class);
+ //
+ // resourceProvider.updateTask(new IdType().setValue(WRONG_TASK_UUID), wrongTask);
+ // }
+
+ //TODO ERROR
+ // @Test
+ // public void searchTasks_shouldReturnMatchingTasks() {
+ // List tasks = new ArrayList<>();
+ // tasks.add(task);
+ //
+ // when(taskService.searchForTasks(any(), any(), any(), any())).thenReturn(tasks);
+ //
+ // TokenOrListParam status = new TokenOrListParam();
+ // TokenParam statusToken = new TokenParam();
+ // statusToken.setValue("ACCEPTED");
+ // status.add(statusToken);
+ //
+ // Bundle results = resourceProvider.searchTasks(null, null, status, null);
+ //
+ // assertThat(results, notNullValue());
+ // assertThat(results.getTotal(), equalTo(1));
+ // assertThat(results.getEntry(), notNullValue());
+ // assertThat(results.getEntry().get(0).getResource().fhirType(), equalTo("Task"));
+ // assertThat(results.getEntry().get(0).getResource().getId(), equalTo(TASK_UUID));
+ // }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderTest.java
index 3683b334e..ea127fafe 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -45,7 +45,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class AllergyIntoleranceFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderTest.java
index 96de7c4d3..0ef86631e 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -47,7 +47,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirConditionService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class ConditionFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProviderTest.java
similarity index 99%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProviderTest.java
index e8afbb841..35808316f 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProviderTest.java
similarity index 97%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProviderTest.java
index 735da18aa..20c3f8ab2 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -39,7 +39,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirEncounterService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class EncounterFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ListFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProviderTest.java
similarity index 96%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ListFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProviderTest.java
index 2d8845198..abe281522 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ListFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -29,7 +29,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.Cohort;
import org.openmrs.module.fhir2.api.FhirListService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class ListFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProviderTest.java
index ec3fba680..11aef3aab 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -47,7 +47,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirLocationService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class LocationFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderTest.java
similarity index 99%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderTest.java
index 4539f3b49..9021e9d6b 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProviderTest.java
index a40d687f4..368703db3 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProviderTest.java
similarity index 95%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProviderTest.java
index e63030a81..8345a2319 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -39,7 +39,8 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirObservationService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.MockIBundleProvider;
@RunWith(MockitoJUnitRunner.class)
public class ObservationFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
@@ -103,7 +104,7 @@ public void searchObservations_shouldReturnMatchingObservations() {
observation.setId(OBSERVATION_UUID);
when(observationService.searchForObservations(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
- .thenReturn(new BaseFhirIBundleResourceProviderTest<>(Collections.singletonList(observation), 10, 1));
+ .thenReturn(new MockIBundleProvider<>(Collections.singletonList(observation), 10, 1));
TokenAndListParam code = new TokenAndListParam();
TokenParam codingToken = new TokenParam();
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderTest.java
similarity index 99%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderTest.java
index a61343a8e..fe88b01f9 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -45,7 +45,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirPatientService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class PatientFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProviderTest.java
index e7bd3016d..162d6737d 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -44,7 +44,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirPersonService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class PersonFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProviderTest.java
index 92b64f914..37a11adcf 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -37,7 +37,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirPractitionerService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class PractitionerFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProviderTest.java
index 615aefcd8..409cd10a5 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProviderTest.java
index 74518e0f5..8159d732c 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProviderTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProviderTest.java
rename to api/src/test/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProviderTest.java
index 6482e3b8f..dac927d4e 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProviderTest.java
+++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProviderTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -43,7 +43,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirTaskService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;
+import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;
@RunWith(MockitoJUnitRunner.class)
public class TaskFhirResourceProviderTest extends BaseFhirProvenanceResourceTest {
diff --git a/omod/pom.xml b/omod/pom.xml
index f50207834..929bdcd42 100644
--- a/omod/pom.xml
+++ b/omod/pom.xml
@@ -18,6 +18,14 @@
${project.parent.artifactId}-api
${project.version}
+
+ ${project.parent.groupId}
+ ${project.parent.artifactId}-api
+ ${project.version}
+ test-jar
+ tests
+ test
+
${project.parent.groupId}
${project.parent.artifactId}-api-2.1
@@ -26,7 +34,7 @@
${project.parent.groupId}
${project.parent.artifactId}-api-2.2
- ${project.version}
+ ${project.parent.version}
org.openmrs.web
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/web/filter/ForwardingFilter.java b/omod/src/main/java/org/openmrs/module/fhir2/web/filter/ForwardingFilter.java
index d37c7f5da..7f51d833c 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/web/filter/ForwardingFilter.java
+++ b/omod/src/main/java/org/openmrs/module/fhir2/web/filter/ForwardingFilter.java
@@ -9,6 +9,9 @@
*/
package org.openmrs.module.fhir2.web.filter;
+import static org.openmrs.module.fhir2.web.servlet.FhirVersionUtils.FhirVersion.R3;
+import static org.openmrs.module.fhir2.web.servlet.FhirVersionUtils.FhirVersion.R4;
+
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -16,28 +19,51 @@
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
+import org.openmrs.module.fhir2.web.servlet.FhirVersionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
public class ForwardingFilter implements Filter {
+ private static final Logger log = LoggerFactory.getLogger(ForwardingFilter.class);
+
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest) req;
- String requestURI = request.getRequestURI();
-
- String contextPath = ((HttpServletRequest) req).getContextPath();
- String prefix = contextPath + "/ws/fhir2";
- if (requestURI.startsWith(prefix)) {
- String newURI = requestURI.replace(prefix, "/ms/fhir2Servlet");
+ if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
+ HttpServletRequest request = (HttpServletRequest) req;
+ String requestURI = request.getRequestURI();
+
+ String contextPath = ((HttpServletRequest) req).getContextPath();
+ String prefix = contextPath + "/ws/fhir2";
+ Enum fhirVersionCase = FhirVersionUtils.getFhirResourceVersion(request);
+ String fhirVersion = String.valueOf(FhirVersionUtils.getFhirResourceVersion(request));
+
+ String replacement;
+ if (R3.equals(fhirVersionCase)) {
+ prefix += "/" + fhirVersion;
+ replacement = "/ms/fhir2R3Servlet";
+ } else if (R4.equals(fhirVersionCase)) {
+ prefix += "/" + fhirVersion;
+ replacement = "/ms/fhir2Servlet";
+ } else {
+ ((HttpServletResponse) res).sendError(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ String newURI = requestURI.replace(prefix, replacement);
req.getRequestDispatcher(newURI).forward(req, res);
- } else {
- chain.doFilter(req, res);
+ return;
}
+
+ ((HttpServletResponse) res).sendError(HttpServletResponse.SC_NOT_FOUND);
}
@Override
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirR3RestServlet.java b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirR3RestServlet.java
new file mode 100644
index 000000000..25f994ca7
--- /dev/null
+++ b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirR3RestServlet.java
@@ -0,0 +1,42 @@
+/*
+ * 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.web.servlet;
+
+import java.util.Collection;
+
+import ca.uhn.fhir.context.FhirContext;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+@Component
+public class FhirR3RestServlet extends FhirRestServlet {
+
+ @Override
+ protected String getRequestPath(String requestFullPath, String servletContextPath, String servletPath) {
+ return requestFullPath.substring(
+ escapedLength(servletContextPath) + escapedLength(servletPath) + escapedLength("/fhir2R3Servlet"));
+ }
+
+ @Override
+ @Autowired
+ @Qualifier("fhirR3")
+ public void setFhirContext(FhirContext theFhirContext) {
+ super.setFhirContext(theFhirContext);
+ }
+
+ @Override
+ @Autowired
+ @Qualifier("fhirR3Resources")
+ public void setResourceProviders(Collection theProviders) {
+ super.setResourceProviders(theProviders);
+ }
+}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java
index 861456790..4f833c1b3 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java
+++ b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java
@@ -29,7 +29,7 @@
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
@Component
-@Setter(AccessLevel.PACKAGE)
+@Setter(AccessLevel.PUBLIC)
public class FhirRestServlet extends RestfulServer {
private static final long serialVersionUID = 1L;
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirVersionUtils.java b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirVersionUtils.java
new file mode 100644
index 000000000..cc2ad16c4
--- /dev/null
+++ b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirVersionUtils.java
@@ -0,0 +1,57 @@
+/*
+ * 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.web.servlet;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class FhirVersionUtils {
+
+ public enum FhirVersion {
+ UNKNOWN,
+ R3,
+ R4
+ }
+
+ public static Enum getFhirResourceVersion(HttpServletRequest request) {
+ String requestURI = request.getRequestURI();
+ String contextPath = request.getContextPath();
+ String prefix = contextPath + "/ws/fhir2";
+ if (requestURI.startsWith(prefix)) {
+ int prefixIdx = requestURI.indexOf(prefix);
+ if (prefixIdx >= 0) {
+ int prefixEnd = prefixIdx + prefix.length();
+ int pathIdx = requestURI.indexOf('/', prefixEnd + 1);
+
+ if (pathIdx >= 0) {
+ String version = requestURI.substring(prefixEnd, pathIdx);
+
+ if (version.isEmpty()) {
+ return FhirVersion.UNKNOWN;
+ }
+
+ if (version.charAt(0) == '/') {
+ version = version.substring(1);
+ }
+
+ switch (version) {
+ case "R3":
+ return FhirVersion.R3;
+ case "R4":
+ return FhirVersion.R4;
+ default:
+ return FhirVersion.UNKNOWN;
+ }
+ }
+ }
+ }
+
+ return FhirVersion.UNKNOWN;
+ }
+}
diff --git a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategy.java b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategy.java
index 827492b80..835accf69 100644
--- a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategy.java
+++ b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategy.java
@@ -9,6 +9,9 @@
*/
package org.openmrs.module.fhir2.web.servlet;
+import static org.openmrs.module.fhir2.web.servlet.FhirVersionUtils.FhirVersion.R3;
+import static org.openmrs.module.fhir2.web.servlet.FhirVersionUtils.FhirVersion.R4;
+
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
@@ -33,6 +36,12 @@ public String determineServerBase(ServletContext context, HttpServletRequest req
String gpPrefix = globalPropertyService.getGlobalProperty(FhirConstants.GLOBAL_PROPERTY_URI_PREFIX);
if (StringUtils.isNotBlank(gpPrefix)) {
+ Enum fhirVersion = FhirVersionUtils.getFhirResourceVersion(request);
+ if (R3.equals(fhirVersion)) {
+ gpPrefix += "/R3/";
+ } else if (R4.equals(fhirVersion)) {
+ gpPrefix += "/R4/";
+ }
return gpPrefix;
}
@@ -55,6 +64,7 @@ public String determineServerBase(ServletContext context, HttpServletRequest req
}
}
- return request.getScheme() + "://" + request.getServerName() + port + "/" + contextPath + "/ws/fhir2/";
+ return request.getScheme() + "://" + request.getServerName() + port + "/" + contextPath + "/ws/fhir2/"
+ + FhirVersionUtils.getFhirResourceVersion(request);
}
}
diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml
index 4a83be71d..c6b7ed4d9 100644
--- a/omod/src/main/resources/config.xml
+++ b/omod/src/main/resources/config.xml
@@ -50,6 +50,11 @@
org.openmrs.module.fhir2.web.servlet.FhirRestServlet
+
+ fhir2R3Servlet
+ org.openmrs.module.fhir2.web.servlet.FhirR3RestServlet
+
+
fhir2AuthenticationFilter
org.openmrs.module.fhir2.web.filter.AuthenticationFilter
@@ -60,6 +65,7 @@
/ws/fhir2/*
/ms/fhir2Servlet
/ms/fhir2Servlet/*
+ /ms/fhir2R3Servlet/*
fhir2ForwardingFilter
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/BaseFhirResourceProviderTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirResourceProviderWebTest.java
similarity index 68%
rename from omod/src/test/java/org/openmrs/module/fhir2/web/servlet/BaseFhirResourceProviderTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirResourceProviderWebTest.java
index 34b5edb82..d4e9b81ff 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/BaseFhirResourceProviderTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/BaseFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.web.servlet;
+package org.openmrs.module.fhir2.providers;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
@@ -20,28 +20,25 @@
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.List;
-import java.util.stream.Collectors;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
-import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.interceptor.LoggingInterceptor;
import lombok.SneakyThrows;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
+import org.hl7.fhir.instance.model.api.IBaseBundle;
+import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
import org.hl7.fhir.instance.model.api.IBaseResource;
-import org.hl7.fhir.r4.model.Bundle;
-import org.hl7.fhir.r4.model.OperationOutcome;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.openmrs.api.APIException;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.impl.FhirGlobalPropertyServiceImpl;
+import org.openmrs.module.fhir2.web.servlet.FhirRestServlet;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -50,65 +47,44 @@
import org.springframework.mock.web.MockServletContext;
import org.springframework.transaction.annotation.Transactional;
-public abstract class BaseFhirResourceProviderTest {
+public abstract class BaseFhirResourceProviderWebTest {
- private static final String SERVLET_NAME = "fhir2Servlet";
+ private ServletConfig servletConfig;
- private static final int FROM_INDEX = 0;
+ private IParser parser;
- private static final int TO_INDEX = 10;
-
- private static ServletConfig servletConfig;
-
- private static IParser parser;
-
- private static LoggingInterceptor interceptor;
+ private LoggingInterceptor interceptor;
private FhirRestServlet servlet;
- public static Matcher isOk() {
- return new IsOkMatcher();
- }
-
- public static Matcher isNotFound() {
- return statusEquals(HttpStatus.NOT_FOUND);
- }
-
- public static Matcher isCreated() {
- return statusEquals(HttpStatus.CREATED);
- }
-
- public static Matcher isBadRequest() {
- return statusEquals(HttpStatus.BAD_REQUEST);
- }
-
- public static Matcher isMethodNotAllowed() {
- return statusEquals(HttpStatus.METHOD_NOT_ALLOWED);
- }
-
- public static Matcher statusEquals(final int status) {
- return new StatusEqualsMatcher(status);
- }
-
- public static Matcher statusEquals(HttpStatus status) {
- return statusEquals(status.value());
- }
+ // This must be implemented by subclasses
+ public abstract T getResourceProvider();
- @BeforeClass
- public static void setupServlet() {
- parser = FhirContext.forR4().newJsonParser();
+ @Before
+ public void setup() throws ServletException {
+ parser = getFhirContext().newJsonParser();
interceptor = new LoggingInterceptor();
interceptor.setLoggerName("org.openmrs.module.fhir2.accessLog");
MockServletContext servletContext = new MockServletContext();
- servletConfig = new MockServletConfig(servletContext, SERVLET_NAME);
+ servletConfig = new MockServletConfig(servletContext, getServletName());
+
+ setupFhirServlet();
}
- @Before
- public void setup() throws Exception {
- servlet = new FhirRestServlet();
- servlet.setFhirContext(FhirContext.forR4());
+ // These are expected to be implemented by version-specific sub-classes
+ public abstract String getServletName();
+
+ public abstract FhirContext getFhirContext();
+
+ public abstract FhirRestServlet getRestfulServer();
+
+ public abstract void describeOperationOutcome(Description mismatchDescription, IBaseOperationOutcome operationOutcome);
+
+ public void setupFhirServlet() throws ServletException {
+ servlet = getRestfulServer();
+ servlet.setFhirContext(getFhirContext());
servlet.setLoggingInterceptor(interceptor);
servlet.setGlobalPropertyService(new FhirGlobalPropertyServiceImpl() {
@@ -124,40 +100,63 @@ public String getGlobalProperty(String property) throws APIException {
return null;
}
});
+
servlet.setResourceProviders(getResourceProvider());
servlet.init(servletConfig);
}
+ public Matcher isOk() {
+ return new IsOkMatcher();
+ }
+
+ public Matcher isNotFound() {
+ return statusEquals(HttpStatus.NOT_FOUND);
+ }
+
+ public Matcher isCreated() {
+ return statusEquals(HttpStatus.CREATED);
+ }
+
+ public Matcher isBadRequest() {
+ return statusEquals(HttpStatus.BAD_REQUEST);
+ }
+
+ public Matcher isMethodNotAllowed() {
+ return statusEquals(HttpStatus.METHOD_NOT_ALLOWED);
+ }
+
+ public Matcher statusEquals(final int status) {
+ return new StatusEqualsMatcher(status);
+ }
+
+ public Matcher statusEquals(HttpStatus status) {
+ return statusEquals(status.value());
+ }
+
public FhirRequestBuilder get(@NotNull String uri) throws MalformedURLException {
- return new FhirRequestBuilder(RequestTypeEnum.GET, "http://localhost:8080/" + SERVLET_NAME + uri);
+ return new FhirRequestBuilder(RequestTypeEnum.GET, "http://localhost:8080/" + getServletName() + uri);
}
public FhirRequestBuilder post(@NotNull String uri) throws MalformedURLException {
- return new FhirRequestBuilder(RequestTypeEnum.POST, "http://localhost:8080/" + SERVLET_NAME + uri);
+ return new FhirRequestBuilder(RequestTypeEnum.POST, "http://localhost:8080/" + getServletName() + uri);
}
public FhirRequestBuilder put(@NotNull String uri) throws MalformedURLException {
- return new FhirRequestBuilder(RequestTypeEnum.PUT, "http://localhost:8080/" + SERVLET_NAME + uri);
+ return new FhirRequestBuilder(RequestTypeEnum.PUT, "http://localhost:8080/" + getServletName() + uri);
}
public FhirRequestBuilder delete(@NotNull String uri) throws MalformedURLException {
- return new FhirRequestBuilder(RequestTypeEnum.DELETE, "http://localhost:8080/" + SERVLET_NAME + uri);
+ return new FhirRequestBuilder(RequestTypeEnum.DELETE, "http://localhost:8080/" + getServletName() + uri);
}
public U readResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
return (U) parser.parseResource(response.getContentAsString());
}
- public Bundle readBundleResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
- return (Bundle) parser.parseResource(response.getContentAsString());
+ public IBaseBundle readBundleResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
+ return (IBaseBundle) parser.parseResource(response.getContentAsString());
}
- public List getResources(IBundleProvider results) {
- return results.getResources(FROM_INDEX, TO_INDEX);
- }
-
- public abstract T getResourceProvider();
-
public static class FhirMediaTypes {
public static final MediaType JSON;
@@ -173,34 +172,29 @@ private FhirMediaTypes() {
}
}
- private static abstract class HttpResponseMatcher extends TypeSafeMatcher {
+ private abstract class HttpResponseMatcher extends TypeSafeMatcher {
@SneakyThrows
@Override
protected void describeMismatchSafely(MockHttpServletResponse item, Description mismatchDescription) {
- FhirContext fhirContext = FhirContext.forR4();
+ FhirContext fhirContext = getFhirContext();
IParser parser = fhirContext.newJsonParser();
- OperationOutcome operationOutcome = null;
+ IBaseOperationOutcome operationOutcome = null;
try {
- operationOutcome = parser.parseResource(OperationOutcome.class, item.getContentAsString());
+ operationOutcome = parser.parseResource(IBaseOperationOutcome.class, item.getContentAsString());
}
catch (DataFormatException ignored) {}
mismatchDescription.appendText("response with status code ").appendValue(item.getStatus());
- if (operationOutcome != null && operationOutcome.hasIssue() && operationOutcome.getIssue().stream()
- .anyMatch(o -> o.getSeverity().ordinal() <= OperationOutcome.IssueSeverity.WARNING.ordinal())) {
- mismatchDescription.appendText(" with message ");
- mismatchDescription.appendValue(operationOutcome.getIssue().stream()
- .filter(o -> o.getSeverity().ordinal() <= OperationOutcome.IssueSeverity.WARNING.ordinal())
- .map(OperationOutcome.OperationOutcomeIssueComponent::getDiagnostics)
- .collect(Collectors.joining(". ")));
+ if (operationOutcome != null) {
+ describeOperationOutcome(mismatchDescription, operationOutcome);
}
}
}
- private static class IsOkMatcher extends HttpResponseMatcher {
+ private class IsOkMatcher extends HttpResponseMatcher {
@Override
protected boolean matchesSafely(MockHttpServletResponse item) {
@@ -214,7 +208,7 @@ public void describeTo(Description description) {
}
}
- private static class StatusEqualsMatcher extends HttpResponseMatcher {
+ private class StatusEqualsMatcher extends HttpResponseMatcher {
private int status;
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderWebTest.java
new file mode 100644
index 000000000..3fa2d99a6
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderWebTest.java
@@ -0,0 +1,401 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.openmrs.module.fhir2.FhirConstants.AUT;
+import static org.openmrs.module.fhir2.FhirConstants.AUTHOR;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.ReferenceOrListParam;
+import ca.uhn.fhir.rest.param.ReferenceParam;
+import ca.uhn.fhir.rest.param.StringOrListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import ca.uhn.fhir.rest.param.TokenParam;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hl7.fhir.convertors.conv30_40.AllergyIntolerance30_40;
+import org.hl7.fhir.dstu3.model.AllergyIntolerance;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AllergyIntoleranceFhirR3ResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String ALLERGY_UUID = "1085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ private static final String WRONG_ALLERGY_UUID = "2085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ @Mock
+ private FhirAllergyIntoleranceService allergyService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private AllergyIntoleranceFhirResourceProvider allergyProvider;
+
+ @Captor
+ private ArgumentCaptor patientCaptor;
+
+ @Captor
+ private ArgumentCaptor stringOrListParamArgumentCaptor;
+
+ @Captor
+ private ArgumentCaptor tokenOrListParamArgumentCaptor;
+
+ @Captor
+ private ArgumentCaptor tokenAndListParamArgumentCaptor;
+
+ AllergyIntolerance allergyIntolerance;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ allergyProvider = new AllergyIntoleranceFhirResourceProvider();
+ allergyProvider.setAllergyIntoleranceService(allergyService);
+ allergyIntolerance = new AllergyIntolerance();
+ allergyIntolerance.setId(ALLERGY_UUID);
+ super.setup();
+ }
+
+ @Override
+ public AllergyIntoleranceFhirResourceProvider getResourceProvider() {
+ return allergyProvider;
+ }
+
+ @Test
+ public void getAllergyIntoleranceByUuid_shouldReturnAllergy() throws Exception {
+ org.hl7.fhir.r4.model.AllergyIntolerance allergy = new org.hl7.fhir.r4.model.AllergyIntolerance();
+ allergy.setId(ALLERGY_UUID);
+ when(allergyService.get(ALLERGY_UUID)).thenReturn(allergy);
+
+ MockHttpServletResponse response = get("/AllergyIntolerance/" + ALLERGY_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(ALLERGY_UUID));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientIdentifier() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.identifier=M4001-1");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo("M4001-1"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientIdentifierWithOr() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.identifier=M4001-1,MK89I");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo("M4001-1"));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientIdentifierWithAnd() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.identifier=M4001-1&patient.identifier=MK89I");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo("M4001-1"));
+ assertThat(patientCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientGivenName() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.given=John");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("given"));
+ assertThat(referenceParam.getValue(), equalTo("John"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientFamilyName() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.family=John");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo("John"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientFamilyNameWithOr() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.family=John,Tim,Him");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo("John"));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(3));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientFamilyNameWithAnd() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.family=John&patient.family=Tim&patient.family=Him");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo("John"));
+ assertThat(patientCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(3));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByPatientName() throws Exception {
+ verifyUri("/AllergyIntolerance?patient.name=John");
+
+ verify(allergyService).searchForAllergies(patientCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("name"));
+ assertThat(referenceParam.getValue(), equalTo("John"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByCategory() throws Exception {
+ verifyUri("/AllergyIntolerance?category=food");
+
+ verify(allergyService).searchForAllergies(isNull(), tokenAndListParamArgumentCaptor.capture(), isNull(), isNull(),
+ isNull(), isNull());
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ equalTo("food"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByAllergenCode() throws Exception {
+ verifyUri("/AllergyIntolerance?code=d1b98543-10ff-4911-83a2-b7f5fafe2751");
+
+ verify(allergyService).searchForAllergies(isNull(), isNull(), tokenAndListParamArgumentCaptor.capture(), isNull(),
+ isNull(), isNull());
+
+ List listParams = tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens();
+ TokenParam tokenParam = listParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenParam.getValue(), equalTo("d1b98543-10ff-4911-83a2-b7f5fafe2751"));
+ }
+
+ public void searchForAllergies_shouldSearchForAllergiesByAllergenCodeAndSystem() throws Exception {
+ verifyUri("/AllergyIntolerance?code=d1b98543-10ff-4911-83a2-b7f5fafe2751");
+
+ verify(allergyService).searchForAllergies(isNull(), isNull(), tokenAndListParamArgumentCaptor.capture(), isNull(),
+ isNull(), isNull());
+ assertThat(tokenOrListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenOrListParamArgumentCaptor.getValue().getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("d1b98543-10ff-4911-83a2-b7f5fafe2751"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesBySeverity() throws Exception {
+ verifyUri("/AllergyIntolerance?severity=severe");
+
+ verify(allergyService).searchForAllergies(isNull(), isNull(), isNull(), tokenAndListParamArgumentCaptor.capture(),
+ isNull(), isNull());
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ equalTo("severe"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByManifestation() throws Exception {
+ verifyUri("/AllergyIntolerance?manifestation=c0b1f314-1691-11df-97a5-7038c432aabd");
+
+ verify(allergyService).searchForAllergies(isNull(), isNull(), isNull(), isNull(),
+ tokenAndListParamArgumentCaptor.capture(), isNull());
+
+ List listParams = tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens();
+ TokenParam tokenParam = listParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenParam.getValue(), equalTo("c0b1f314-1691-11df-97a5-7038c432aabd"));
+ }
+
+ @Test
+ public void searchForAllergies_shouldSearchForAllergiesByStatus() throws Exception {
+ verifyUri("/AllergyIntolerance?clinical-status=active");
+
+ verify(allergyService).searchForAllergies(isNull(), isNull(), isNull(), isNull(), isNull(),
+ tokenAndListParamArgumentCaptor.capture());
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ equalTo("active"));
+ }
+
+ @Test
+ public void getAllergyIntoleranceByUuid_shouldReturn404() throws Exception {
+ when(allergyService.get(WRONG_ALLERGY_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/AllergyIntolerance/" + WRONG_ALLERGY_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void shouldVerifyAllergyIntoleranceHistoryByIdUri() throws Exception {
+ org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = new org.hl7.fhir.r4.model.AllergyIntolerance();
+ allergyIntolerance.setId(ALLERGY_UUID);
+ when(allergyService.get(ALLERGY_UUID)).thenReturn(allergyIntolerance);
+
+ MockHttpServletResponse response = getAllergyIntoleranceHistoryRequest();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(BaseFhirR3ResourceProviderWebTest.FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void shouldGetAllergyIntoleranceHistoryById() throws IOException, ServletException {
+ Provenance provenance = new Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+ org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = new org.hl7.fhir.r4.model.AllergyIntolerance();
+ allergyIntolerance.setId(ALLERGY_UUID);
+ allergyIntolerance.addContained(provenance);
+
+ when(allergyService.get(ALLERGY_UUID)).thenReturn(allergyIntolerance);
+
+ MockHttpServletResponse response = getAllergyIntoleranceHistoryRequest();
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results, notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+
+ }
+
+ @Test
+ public void getAllergyIntoleranceHistoryById_shouldReturnBundleWithEmptyEntriesIfResourceContainedIsEmpty()
+ throws Exception {
+ org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = new org.hl7.fhir.r4.model.AllergyIntolerance();
+ allergyIntolerance.setId(ALLERGY_UUID);
+ allergyIntolerance.setContained(new ArrayList<>());
+ when(allergyService.get(ALLERGY_UUID)).thenReturn(allergyIntolerance);
+
+ MockHttpServletResponse response = getAllergyIntoleranceHistoryRequest();
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getAllergyIntoleranceHistoryById_shouldReturn404IfAllergyIntoleranceIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/AllergyIntolerance/" + WRONG_ALLERGY_UUID + "/_history")
+ .accept(BaseFhirR3ResourceProviderWebTest.FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getAllergyIntoleranceHistoryRequest() throws IOException, ServletException {
+ return get("/AllergyIntolerance/" + ALLERGY_UUID + "/_history")
+ .accept(BaseFhirR3ResourceProviderWebTest.FhirMediaTypes.JSON).go();
+
+ }
+
+ private void verifyUri(String uri) throws Exception {
+ AllergyIntolerance allergy = new AllergyIntolerance();
+ allergy.setId(ALLERGY_UUID);
+ when(allergyService.searchForAllergies(any(), any(), any(), any(), any(), any())).thenReturn(
+ Collections.singletonList(AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance)));
+
+ MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results.getEntry(), notNullValue());
+ assertThat(results.getEntry(), not(empty()));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(ALLERGY_UUID));
+ }
+
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3ResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3ResourceProviderWebTest.java
new file mode 100644
index 000000000..f3bcf9989
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/BaseFhirR3ResourceProviderWebTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.providers.r3;
+
+import java.io.UnsupportedEncodingException;
+import java.util.stream.Collectors;
+
+import ca.uhn.fhir.context.FhirContext;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import org.hamcrest.Description;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.OperationOutcome;
+import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.openmrs.module.fhir2.providers.BaseFhirResourceProviderWebTest;
+import org.openmrs.module.fhir2.web.servlet.FhirR3RestServlet;
+import org.openmrs.module.fhir2.web.servlet.FhirRestServlet;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public abstract class BaseFhirR3ResourceProviderWebTest extends BaseFhirResourceProviderWebTest {
+
+ private static final FhirContext FHIR_CONTEXT = FhirContext.forDstu3();
+
+ @Override
+ public String getServletName() {
+ return "fhir2R3Servlet";
+ }
+
+ @Override
+ public FhirContext getFhirContext() {
+ return FHIR_CONTEXT;
+ }
+
+ @Override
+ public FhirRestServlet getRestfulServer() {
+ return new FhirR3RestServlet();
+ }
+
+ @Override
+ public void describeOperationOutcome(Description mismatchDescription, IBaseOperationOutcome baseOperationOutcome) {
+ if (baseOperationOutcome instanceof OperationOutcome) {
+ OperationOutcome operationOutcome = (OperationOutcome) baseOperationOutcome;
+ if (operationOutcome.hasIssue() && operationOutcome.getIssue().stream()
+ .anyMatch(o -> o.getSeverity().ordinal() <= OperationOutcome.IssueSeverity.WARNING.ordinal())) {
+ mismatchDescription.appendText(" with message ");
+ mismatchDescription.appendValue(operationOutcome.getIssue().stream()
+ .filter(o -> o.getSeverity().ordinal() <= OperationOutcome.IssueSeverity.WARNING.ordinal())
+ .map(OperationOutcome.OperationOutcomeIssueComponent::getDiagnostics)
+ .collect(Collectors.joining(". ")));
+ }
+ }
+ }
+
+ @Override
+ public Bundle readBundleResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
+ return (Bundle) super.readBundleResponse(response);
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirR3ResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirR3ResourceProviderWebTest.java
new file mode 100644
index 000000000..4c58093eb
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ConditionFhirR3ResourceProviderWebTest.java
@@ -0,0 +1,174 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+import static org.openmrs.module.fhir2.FhirConstants.AUT;
+import static org.openmrs.module.fhir2.FhirConstants.AUTHOR;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Date;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.apache.commons.io.IOUtils;
+import org.hl7.fhir.dstu3.model.*;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirConditionService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ConditionFhirR3ResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String CONDITION_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
+
+ private static final String WRONG_CONDITION_UUID = "9bf0d1ac-62a8-4440-a5a1-eb1015a7cc65";
+
+ private static final String JSON_CREATE_CONDITION_PATH = "org/openmrs/module/fhir2/providers/ConditionResourceWebTest_create.json";
+
+ @Mock
+ private FhirConditionService conditionService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private ConditionFhirResourceProvider resourceProvider;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new ConditionFhirResourceProvider();
+ resourceProvider.setConditionService(conditionService);
+ super.setup();
+ }
+
+ @Test
+ public void shouldReturnConditionByUuid() throws Exception {
+ org.hl7.fhir.r4.model.Condition condition = new org.hl7.fhir.r4.model.Condition();
+ condition.setId(CONDITION_UUID);
+ when(conditionService.getConditionByUuid(CONDITION_UUID)).thenReturn(condition);
+
+ MockHttpServletResponse response = get("/Condition/" + CONDITION_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Condition resource = readResponse(response);
+ assertThat(resource.getIdElement().getIdPart(), equalTo(CONDITION_UUID));
+ }
+
+ @Test
+ public void shouldReturn404IfConditionNotFound() throws Exception {
+ when(conditionService.getConditionByUuid(WRONG_CONDITION_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/Condition/" + WRONG_CONDITION_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void shouldVerifyConditionHistoryByIdUri() throws Exception {
+ org.hl7.fhir.r4.model.Condition condition = new org.hl7.fhir.r4.model.Condition();
+ condition.setId(CONDITION_UUID);
+ when(conditionService.getConditionByUuid(CONDITION_UUID)).thenReturn(condition);
+
+ MockHttpServletResponse response = getConditionHistoryRequest();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void shouldGetConditionHistoryById() throws IOException, ServletException {
+ Provenance provenance = new Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+ org.hl7.fhir.r4.model.Condition condition = new org.hl7.fhir.r4.model.Condition();
+ condition.setId(CONDITION_UUID);
+ condition.addContained(provenance);
+
+ when(conditionService.getConditionByUuid(CONDITION_UUID)).thenReturn(condition);
+
+ MockHttpServletResponse response = getConditionHistoryRequest();
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results, notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+
+ }
+
+ @Test
+ public void getConditionHistoryById_shouldReturnBundleWithEmptyEntriesIfConditionContainedIsEmpty() throws Exception {
+ org.hl7.fhir.r4.model.Condition condition = new org.hl7.fhir.r4.model.Condition();
+ condition.setId(CONDITION_UUID);
+ condition.setContained(new ArrayList<>());
+ when(conditionService.getConditionByUuid(CONDITION_UUID)).thenReturn(condition);
+
+ MockHttpServletResponse response = getConditionHistoryRequest();
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getConditionHistoryById_shouldReturn404IfConditionIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/Condition/" + WRONG_CONDITION_UUID + "/_history")
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getConditionHistoryRequest() throws IOException, ServletException {
+ return get("/Condition/" + CONDITION_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+ }
+
+ @Test
+ public void shouldCreateNewConditionGivenValidConditionResource() throws Exception {
+ org.hl7.fhir.r4.model.Condition condition = new org.hl7.fhir.r4.model.Condition();
+ condition.setId(CONDITION_UUID);
+ String conditionJson;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_CREATE_CONDITION_PATH)) {
+ assert is != null;
+ conditionJson = IOUtils.toString(is);
+ }
+
+ when(conditionService.saveCondition(any(org.hl7.fhir.r4.model.Condition.class))).thenReturn(condition);
+
+ MockHttpServletResponse response = post("/Condition").jsonContent(conditionJson).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isCreated());
+ assertThat(response.getStatus(), is(201));
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..0730f3c47
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/DiagnosticReportFhirResourceProviderWebTest.java
@@ -0,0 +1,167 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsStringIgnoringCase;
+import static org.hamcrest.Matchers.equalTo;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import java.io.InputStream;
+
+import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.apache.commons.io.IOUtils;
+import org.hl7.fhir.dstu3.model.DiagnosticReport;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirDiagnosticReportService;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DiagnosticReportFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String DIAGNOSTIC_REPORT_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
+
+ private static final String WRONG_UUID = "9bf0d1ac-62a8-4440-a5a1-eb1015a7cc65";
+
+ private static final String JSON_DIAGNOSTIC_REPORT_PATH = "org/openmrs/module/fhir2/providers/TestDiagnosticReport_CreateUpdate.json";
+
+ private static final String JSON_DIAGNOSTIC_REPORT_NO_ID_PATH = "org/openmrs/module/fhir2/providers/TestDiagnosticReport_CreateUpdate_NoId.json";
+
+ private static final String JSON_DIAGNOSTIC_REPORT_WRONG_UUID_PATH = "org/openmrs/module/fhir2/providers/TestDiagnosticReport_CreateUpdate_WrongId.json";
+
+ @Mock
+ private FhirDiagnosticReportService service;
+
+ @Getter(AccessLevel.PUBLIC)
+ private DiagnosticReportFhirResourceProvider resourceProvider;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new DiagnosticReportFhirResourceProvider();
+ resourceProvider.setDiagnosticReportService(service);
+ super.setup();
+ }
+
+ @Test
+ public void getDiagnosticReportByUuid_shouldReturnDiagnosticReport() throws Exception {
+ org.hl7.fhir.r4.model.DiagnosticReport diagnosticReport = new org.hl7.fhir.r4.model.DiagnosticReport();
+ diagnosticReport.setId(DIAGNOSTIC_REPORT_UUID);
+ when(service.get(DIAGNOSTIC_REPORT_UUID)).thenReturn(diagnosticReport);
+
+ MockHttpServletResponse response = get("/DiagnosticReport/" + DIAGNOSTIC_REPORT_UUID).accept(FhirMediaTypes.JSON)
+ .go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(DIAGNOSTIC_REPORT_UUID));
+ }
+
+ @Test
+ public void getDiagnosticReportByWrongUuid_shouldReturn404() throws Exception {
+ when(service.get(WRONG_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/DiagnosticReport/" + WRONG_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void createDiagnosticReport_shouldCreateNewDiagnosticReport() throws Exception {
+ String jsonDiagnosticReport;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_DIAGNOSTIC_REPORT_PATH)) {
+ jsonDiagnosticReport = IOUtils.toString(is);
+ }
+
+ org.hl7.fhir.r4.model.DiagnosticReport diagnosticReport = new org.hl7.fhir.r4.model.DiagnosticReport();
+ diagnosticReport.setId(DIAGNOSTIC_REPORT_UUID);
+
+ when(service.create(any(org.hl7.fhir.r4.model.DiagnosticReport.class))).thenReturn(diagnosticReport);
+
+ MockHttpServletResponse response = post("/DiagnosticReport").jsonContent(jsonDiagnosticReport)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isCreated());
+ }
+
+ @Test
+ public void updateDiagnosticReport_shouldUpdateExistingDiagnosticReport() throws Exception {
+ String jsonDiagnosticReport;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_DIAGNOSTIC_REPORT_PATH)) {
+ jsonDiagnosticReport = IOUtils.toString(is);
+ }
+
+ org.hl7.fhir.r4.model.DiagnosticReport diagnosticReport = new org.hl7.fhir.r4.model.DiagnosticReport();
+ diagnosticReport.setId(DIAGNOSTIC_REPORT_UUID);
+
+ when(service.update(anyString(), any(org.hl7.fhir.r4.model.DiagnosticReport.class))).thenReturn(diagnosticReport);
+
+ MockHttpServletResponse response = put("/DiagnosticReport/" + DIAGNOSTIC_REPORT_UUID)
+ .jsonContent(jsonDiagnosticReport).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ }
+
+ @Test
+ public void updateDiagnosticReport_shouldErrorForIdMismatch() throws Exception {
+ String jsonDiagnosticReport;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_DIAGNOSTIC_REPORT_PATH)) {
+ jsonDiagnosticReport = IOUtils.toString(is);
+ }
+
+ MockHttpServletResponse response = put("/DiagnosticReport/" + WRONG_UUID).jsonContent(jsonDiagnosticReport)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isBadRequest());
+ assertThat(response.getContentAsString(),
+ containsStringIgnoringCase("body must contain an ID element which matches the request URL"));
+ }
+
+ @Test
+ public void updateDiagnosticReport_shouldErrorForNoId() throws Exception {
+ String jsonDiagnosticReport;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_DIAGNOSTIC_REPORT_NO_ID_PATH)) {
+ jsonDiagnosticReport = IOUtils.toString(is);
+ }
+
+ MockHttpServletResponse response = put("/DiagnosticReport/" + DIAGNOSTIC_REPORT_UUID)
+ .jsonContent(jsonDiagnosticReport).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isBadRequest());
+ assertThat(response.getContentAsString(), containsStringIgnoringCase("body must contain an ID element for update"));
+ }
+
+ @Test
+ public void updateDiagnosticReport_shouldErrorForNonexistentDiagnosticReport() throws Exception {
+ String jsonDiagnosticReport;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_DIAGNOSTIC_REPORT_WRONG_UUID_PATH)) {
+ jsonDiagnosticReport = IOUtils.toString(is);
+ }
+
+ when(service.update(eq(WRONG_UUID), any(org.hl7.fhir.r4.model.DiagnosticReport.class)))
+ .thenThrow(new MethodNotAllowedException("DiagnosticReport " + WRONG_UUID + " does not exist"));
+
+ MockHttpServletResponse response = put("/DiagnosticReport/" + WRONG_UUID).jsonContent(jsonDiagnosticReport)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isMethodNotAllowed());
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..c9801c79a
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/EncounterFhirResourceProviderWebTest.java
@@ -0,0 +1,555 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.openmrs.module.fhir2.FhirConstants.AUT;
+import static org.openmrs.module.fhir2.FhirConstants.AUTHOR;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.DateRangeParam;
+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.Getter;
+import org.apache.commons.lang.time.DateUtils;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.Encounter;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.IdType;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirEncounterService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class EncounterFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String ENCOUNTER_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
+
+ private static final String WRONG_ENCOUNTER_UUID = "9bf0d1ac-62a8-4440-a5a1-eb1015a7cc65";
+
+ private static final String PATIENT_IDENTIFIER = "h43489-h";
+
+ private static final String PATIENT_UUID = "d9bc6c12-6adc-4ca6-8bde-441ec1a1c344";
+
+ private static final String PATIENT_GIVEN_NAME = "Hannibal";
+
+ private static final String PATIENT_FAMILY_NAME = "Sid";
+
+ private static final String ENCOUNTER_ADDRESS_CITY = "Boston";
+
+ private static final String ENCOUNTER_ADDRESS_COUNTRY = "INDIA";
+
+ private static final String ENCOUNTER_ADDRESS_STATE = "MA";
+
+ private static final String ENCOUNTER_POSTALCODE = "248001";
+
+ private static final String PARTICIPANT_GIVEN_NAME = "John";
+
+ private static final String PARTICIPANT_FAMILY_NAME = "Doe";
+
+ private static final String PARTICIPANT_IDENTIFIER = "1000WF";
+
+ @Mock
+ private FhirEncounterService encounterService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private EncounterFhirResourceProvider resourceProvider;
+
+ @Captor
+ private ArgumentCaptor locationCaptor;
+
+ @Captor
+ private ArgumentCaptor participantCaptor;
+
+ @Captor
+ private ArgumentCaptor subjectCaptor;
+
+ @Captor
+ private ArgumentCaptor dateRangeCaptor;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new EncounterFhirResourceProvider();
+ resourceProvider.setEncounterService(encounterService);
+ super.setup();
+ }
+
+ @Test
+ public void getEncounterByUuid_shouldReturnEncounter() throws Exception {
+ org.hl7.fhir.r4.model.Encounter encounter = new org.hl7.fhir.r4.model.Encounter();
+ encounter.setId(ENCOUNTER_UUID);
+ when(encounterService.get(ENCOUNTER_UUID)).thenReturn(encounter);
+
+ MockHttpServletResponse response = get("/Encounter/" + ENCOUNTER_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(ENCOUNTER_UUID));
+ }
+
+ @Test
+ public void getEncounterByWrongUuid_shouldReturn404() throws Exception {
+ when(encounterService.get(WRONG_ENCOUNTER_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/Encounter/" + WRONG_ENCOUNTER_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void shouldGetEncountersBySubjectUuid() throws Exception {
+ verifyUri(String.format("/Encounter?subject:Patient=%s", PATIENT_UUID));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), isNull(), subjectCaptor.capture());
+ assertThat(subjectCaptor.getValue(), notNullValue());
+ assertThat(subjectCaptor.getAllValues().iterator().next().getValuesAsQueryTokens().iterator().next()
+ .getValuesAsQueryTokens().iterator().next().getIdPart(),
+ equalTo(PATIENT_UUID));
+ }
+
+ @Test
+ public void shouldGetEncountersByDate() throws Exception {
+ verifyUri("/Encounter/?date=ge1975-02-02");
+
+ verify(encounterService).searchForEncounters(dateRangeCaptor.capture(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetEncountersByLocationCityVillage() throws Exception {
+ verifyUri(String.format("/Encounter/?location.address-city=%s", ENCOUNTER_ADDRESS_CITY));
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), isNull());
+
+ List orListParams = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("address-city"));
+ assertThat(referenceParam.getValue(), equalTo(ENCOUNTER_ADDRESS_CITY));
+ }
+
+ @Test
+ public void shouldGetEncountersByLocationState() throws Exception {
+ verifyUri(String.format("/Encounter/?location.address-state=%s", ENCOUNTER_ADDRESS_STATE));
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), isNull());
+
+ List orListParams = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("address-state"));
+ assertThat(referenceParam.getValue(), equalTo(ENCOUNTER_ADDRESS_STATE));
+ }
+
+ @Test
+ public void shouldGetEncountersByLocationPostalCode() throws Exception {
+ verifyUri(String.format("/Encounter/?location.address-postalcode=%s", ENCOUNTER_POSTALCODE));
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), isNull());
+
+ List orListParams = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("address-postalcode"));
+ assertThat(referenceParam.getValue(), equalTo(ENCOUNTER_POSTALCODE));
+ }
+
+ @Test
+ public void shouldGetEncountersByLocationCountry() throws Exception {
+ verifyUri(String.format("/Encounter/?location.address-country=%s", ENCOUNTER_ADDRESS_COUNTRY));
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), isNull());
+
+ List orListParams = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("address-country"));
+ assertThat(referenceParam.getValue(), equalTo(ENCOUNTER_ADDRESS_COUNTRY));
+ }
+
+ @Test
+ public void shouldGetEncountersByLocationCountryWithOr() throws Exception {
+ verifyUri(String.format("/Encounter/?location.address-country=%s,%s", ENCOUNTER_ADDRESS_COUNTRY, "USA"));
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), isNull());
+
+ List orListParams = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("address-country"));
+ assertThat(referenceParam.getValue(), equalTo(ENCOUNTER_ADDRESS_COUNTRY));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetEncountersByLocationCountryWithAnd() throws Exception {
+ verifyUri("/Encounter/?location.address-country=INDIA&location.address-country=USA");
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), isNull());
+
+ List orListParams = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("address-country"));
+ assertThat(referenceParam.getValue(), equalTo(ENCOUNTER_ADDRESS_COUNTRY));
+ assertThat(locationCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetEncountersByParticipantGivenName() throws Exception {
+ verifyUri(String.format("/Encounter/?participant:Practitioner.given=%s", PARTICIPANT_GIVEN_NAME));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), participantCaptor.capture(), isNull());
+
+ List orListParams = participantCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(participantCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("given"));
+ assertThat(referenceParam.getValue(), equalTo(PARTICIPANT_GIVEN_NAME));
+ }
+
+ @Test
+ public void shouldGetEncountersByParticipantFamilyName() throws Exception {
+ verifyUri(String.format("/Encounter/?participant:Practitioner.family=%s", PARTICIPANT_FAMILY_NAME));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), participantCaptor.capture(), isNull());
+
+ List orListParams = participantCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(participantCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo(PARTICIPANT_FAMILY_NAME));
+ }
+
+ @Test
+ public void shouldGetEncountersByParticipantFamilyNameWithOr() throws Exception {
+ verifyUri(String.format("/Encounter/?participant:Practitioner.family=%s,%s", PARTICIPANT_FAMILY_NAME, "Vox"));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), participantCaptor.capture(), isNull());
+
+ List orListParams = participantCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(participantCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo(PARTICIPANT_FAMILY_NAME));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetEncountersByParticipantFamilyNameWithAnd() throws Exception {
+ verifyUri(String.format("/Encounter/?participant:Practitioner.family=%s&participant:Practitioner.family=%s",
+ PARTICIPANT_FAMILY_NAME, "Vox"));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), participantCaptor.capture(), isNull());
+
+ List orListParams = participantCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(participantCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo(PARTICIPANT_FAMILY_NAME));
+ assertThat(participantCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetEncountersByParticipantIdentifier() throws Exception {
+ verifyUri(String.format("/Encounter/?participant:Practitioner.identifier=%s,%s", PARTICIPANT_IDENTIFIER,
+ "op87yh-34fd-34egs-56h34-34f7"));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), participantCaptor.capture(), isNull());
+
+ List orListParams = participantCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(participantCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo(PARTICIPANT_IDENTIFIER));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetEncountersBySubjectGivenName() throws Exception {
+ verifyUri(String.format("/Encounter/?subject.given=%s", PATIENT_GIVEN_NAME));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), isNull(), subjectCaptor.capture());
+
+ List orListParams = subjectCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(subjectCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("given"));
+ assertThat(referenceParam.getValue(), equalTo(PATIENT_GIVEN_NAME));
+ }
+
+ @Test
+ public void shouldGetEncountersBySubjectFamilyName() throws Exception {
+ verifyUri(String.format("/Encounter?subject.family=%s", PATIENT_FAMILY_NAME));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), isNull(), subjectCaptor.capture());
+
+ List orListParams = subjectCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(subjectCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo(PATIENT_FAMILY_NAME));
+ }
+
+ @Test
+ public void shouldGetEncountersBySubjectIdentifier() throws Exception {
+ verifyUri(String.format("/Encounter?subject.identifier=%s", PATIENT_IDENTIFIER));
+
+ verify(encounterService).searchForEncounters(isNull(), isNull(), isNull(), subjectCaptor.capture());
+
+ List orListParams = subjectCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(subjectCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo(PATIENT_IDENTIFIER));
+ }
+
+ @Test
+ public void shouldGetEncountersBySubjectGivenNameAndLocationPostalCode() throws Exception {
+ verifyUri("/Encounter?subject.given=Hannibal&location.address-postalcode=248001");
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), subjectCaptor.capture());
+
+ List orListParamsSubject = subjectCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamSubject = orListParamsSubject.get(0).getValuesAsQueryTokens().get(0);
+
+ List orListParamsLocation = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamLocation = orListParamsLocation.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(subjectCaptor.getValue(), notNullValue());
+ assertThat(referenceParamSubject.getChain(), equalTo("given"));
+ assertThat(referenceParamSubject.getValue(), equalTo(PATIENT_GIVEN_NAME));
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParamLocation.getChain(), equalTo("address-postalcode"));
+ assertThat(referenceParamLocation.getValue(), equalTo(ENCOUNTER_POSTALCODE));
+ }
+
+ @Test
+ public void shouldGetEncountersBySubjectGivenNameAndLocationPostalCodeWithOr() throws Exception {
+ verifyUri("/Encounter?subject.given=Hannibal&location.address-postalcode=248001,854796");
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), subjectCaptor.capture());
+
+ List orListParamsSubject = subjectCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamSubject = orListParamsSubject.get(0).getValuesAsQueryTokens().get(0);
+
+ List orListParamsLocation = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamLocation = orListParamsLocation.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(subjectCaptor.getValue(), notNullValue());
+ assertThat(referenceParamSubject.getChain(), equalTo("given"));
+ assertThat(referenceParamSubject.getValue(), equalTo(PATIENT_GIVEN_NAME));
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParamLocation.getChain(), equalTo("address-postalcode"));
+ assertThat(referenceParamLocation.getValue(), equalTo(ENCOUNTER_POSTALCODE));
+ assertThat(orListParamsLocation.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetEncountersBySubjectGivenNameAndLocationPostalCodeWithAnd() throws Exception {
+ verifyUri("/Encounter?subject.given=Hannibal&location.address-postalcode=248001&location.address-postalcode=854796");
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), isNull(), subjectCaptor.capture());
+
+ List orListParamsSubject = subjectCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamSubject = orListParamsSubject.get(0).getValuesAsQueryTokens().get(0);
+
+ List orListParamsLocation = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamLocation = orListParamsLocation.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(subjectCaptor.getValue(), notNullValue());
+ assertThat(referenceParamSubject.getChain(), equalTo("given"));
+ assertThat(referenceParamSubject.getValue(), equalTo(PATIENT_GIVEN_NAME));
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParamLocation.getChain(), equalTo("address-postalcode"));
+ assertThat(referenceParamLocation.getValue(), equalTo(ENCOUNTER_POSTALCODE));
+ assertThat(locationCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetEncountersByParticipantIdentifierAndLocationPostalCode() throws Exception {
+ verifyUri("/Encounter?participant:Practitioner.identifier=1000WF&location.address-postalcode=248001");
+
+ verify(encounterService).searchForEncounters(isNull(), locationCaptor.capture(), participantCaptor.capture(),
+ isNull());
+
+ List orListParamsParticipant = participantCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamParticipant = orListParamsParticipant.get(0).getValuesAsQueryTokens().get(0);
+
+ List orListParamsLocation = locationCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamLocation = orListParamsLocation.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(participantCaptor.getValue(), notNullValue());
+ assertThat(referenceParamParticipant.getChain(), equalTo("identifier"));
+ assertThat(referenceParamParticipant.getValue(), equalTo(PARTICIPANT_IDENTIFIER));
+ assertThat(locationCaptor.getValue(), notNullValue());
+ assertThat(referenceParamLocation.getChain(), equalTo("address-postalcode"));
+ assertThat(referenceParamLocation.getValue(), equalTo(ENCOUNTER_POSTALCODE));
+ }
+
+ @Test
+ public void shouldGetEncountersByParticipantIdentifierAndDate() throws Exception {
+ verifyUri("/Encounter?participant:Practitioner.identifier=1000WF,670WD&date=ge1975-02-02");
+
+ verify(encounterService).searchForEncounters(dateRangeCaptor.capture(), isNull(), participantCaptor.capture(),
+ isNull());
+
+ List orListParamsParticipant = participantCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParamParticipant = orListParamsParticipant.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(participantCaptor.getValue(), notNullValue());
+ assertThat(referenceParamParticipant.getChain(), equalTo("identifier"));
+ assertThat(referenceParamParticipant.getValue(), equalTo(PARTICIPANT_IDENTIFIER));
+ assertThat(orListParamsParticipant.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ private void verifyUri(String uri) throws Exception {
+ org.hl7.fhir.r4.model.Encounter encounter = new org.hl7.fhir.r4.model.Encounter();
+ encounter.setId(ENCOUNTER_UUID);
+ when(encounterService.searchForEncounters(any(), any(), any(), any()))
+ .thenReturn(Collections.singletonList(encounter));
+
+ MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results.getEntry(), notNullValue());
+ assertThat(results.getEntry(), not(empty()));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(ENCOUNTER_UUID));
+ }
+
+ @Test
+ public void shouldVerifyEncounterHistoryByIdUri() throws Exception {
+ org.hl7.fhir.r4.model.Encounter encounter = new org.hl7.fhir.r4.model.Encounter();
+ encounter.setId(ENCOUNTER_UUID);
+ when(encounterService.get(ENCOUNTER_UUID)).thenReturn(encounter);
+
+ MockHttpServletResponse response = getEncounterHistoryRequest();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void shouldGetEncounterHistoryById() throws IOException, ServletException {
+ org.hl7.fhir.r4.model.Provenance provenance = new org.hl7.fhir.r4.model.Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+ org.hl7.fhir.r4.model.Encounter encounter = new org.hl7.fhir.r4.model.Encounter();
+ encounter.setId(ENCOUNTER_UUID);
+ encounter.addContained(provenance);
+
+ when(encounterService.get(ENCOUNTER_UUID)).thenReturn(encounter);
+
+ MockHttpServletResponse response = getEncounterHistoryRequest();
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results, notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+
+ }
+
+ @Test
+ public void getEncounterHistoryById_shouldReturnBundleWithEmptyEntriesIfPractitionerContainedIsEmpty() throws Exception {
+ org.hl7.fhir.r4.model.Encounter encounter = new org.hl7.fhir.r4.model.Encounter();
+ encounter.setId(ENCOUNTER_UUID);
+ encounter.setContained(new ArrayList<>());
+ when(encounterService.get(ENCOUNTER_UUID)).thenReturn(encounter);
+
+ MockHttpServletResponse response = getEncounterHistoryRequest();
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getEncounterHistoryById_shouldReturn404IfEncounterIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/Encounter/" + WRONG_ENCOUNTER_UUID + "/_history")
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getEncounterHistoryRequest() throws IOException, ServletException {
+ return get("/Encounter/" + ENCOUNTER_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+ }
+
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..756f915b3
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ListFhirResourceProviderWebTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hl7.fhir.dstu3.model.ListResource;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.Cohort;
+import org.openmrs.module.fhir2.api.FhirListService;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ListFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String LIST_UUID = "c0b1f314-1691-11df-97a5-7038c432aab88";
+
+ private static final String UNKNOWN_UUID = "c0b1f314-1691-11df-97a5-7038c432aab99";
+
+ @Mock
+ private FhirListService cohortFhirListService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private ListFhirResourceProvider listFhirResourceProvider;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ listFhirResourceProvider = new ListFhirResourceProvider();
+ listFhirResourceProvider.setListService(cohortFhirListService);
+ super.setup();
+ }
+
+ @Override
+ public ListFhirResourceProvider getResourceProvider() {
+ return listFhirResourceProvider;
+ }
+
+ @Test
+ public void getListById_shouldReturnListWithMatchingUuid() throws Exception {
+ org.hl7.fhir.r4.model.ListResource listResource = new org.hl7.fhir.r4.model.ListResource();
+ listResource.setId(LIST_UUID);
+ when(cohortFhirListService.get(LIST_UUID)).thenReturn(listResource);
+
+ MockHttpServletResponse response = get("/List/" + LIST_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ ListResource resource = readResponse(response);
+ assertThat(resource.getIdElement().getIdPart(), equalTo(LIST_UUID));
+ }
+
+ @Test
+ public void shouldReturn404IfListNotFound() throws Exception {
+ when(cohortFhirListService.get(UNKNOWN_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/List/" + UNKNOWN_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..718e1011e
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/LocationFhirResourceProviderWebTest.java
@@ -0,0 +1,392 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.openmrs.module.fhir2.FhirConstants.AUT;
+import static org.openmrs.module.fhir2.FhirConstants.AUTHOR;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hamcrest.CoreMatchers;
+import org.hamcrest.Matchers;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.Location;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.IdType;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirLocationService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class LocationFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String LOCATION_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ private static final String WRONG_LOCATION_UUID = "c0938432-1691-11df-97a5-7038c432aabd";
+
+ private static final String LOCATION_NAME = "Ngeria";
+
+ private static final String CITY = "Test City";
+
+ private static final String COUNTRY = "Kenya";
+
+ private static final String STATE = "Pan villa";
+
+ private static final String POSTAL_CODE = "234-30100";
+
+ private static final String SYSTEM = "https://fhir.openmrs.org/ext/location-tag";
+
+ private static final String LOGIN_LOCATION_TAG_NAME = "login";
+
+ private static final String LOGIN_LOCATION_TAG_DESCRIPTION = "Identify login locations";
+
+ private static final String PARENT_LOCATION_NAME = "Test parent location";
+
+ private static final String PARENT_LOCATION_ID = "c0938432-1691-11df-97a5-7038c432aabe";
+
+ private static final String PARENT_LOCATION_CITY = "Test parent city";
+
+ private static final String PARENT_LOCATION_STATE = "Test parent state";
+
+ private static final String PARENT_LOCATION_COUNTRY = "Test parent country";
+
+ private static final String PARENT_LOCATION_POSTAL_CODE = "Test parent postal code";
+
+ @Mock
+ private FhirLocationService locationService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private LocationFhirResourceProvider locationProvider;
+
+ @Captor
+ ArgumentCaptor tagCaptor;
+
+ @Captor
+ ArgumentCaptor stringAndListParamCaptor;
+
+ @Captor
+ ArgumentCaptor referenceAndListParamCaptor;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ locationProvider = new LocationFhirResourceProvider();
+ locationProvider.setLocationService(locationService);
+ super.setup();
+ }
+
+ @Override
+ public LocationFhirResourceProvider getResourceProvider() {
+ return locationProvider;
+ }
+
+ @Test
+ public void getLocationById_shouldReturnLocationWithMatchingUuid() throws Exception {
+ org.hl7.fhir.r4.model.Location location = new org.hl7.fhir.r4.model.Location();
+ location.setId(LOCATION_UUID);
+ when(locationService.get(LOCATION_UUID)).thenReturn(location);
+
+ MockHttpServletResponse response = get("/Location/" + LOCATION_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Location resource = readResponse(response);
+ assertThat(resource.getIdElement().getIdPart(), equalTo(LOCATION_UUID));
+ }
+
+ @Test
+ public void findLocationByName_shouldReturnBundleOfLocationsWithMatchingName() throws Exception {
+ verifyURI(String.format("/Location?name=%s", LOCATION_NAME));
+
+ verify(locationService).searchForLocations(stringAndListParamCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull());
+ assertThat(stringAndListParamCaptor.getValue(), notNullValue());
+ assertThat(
+ stringAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ CoreMatchers.equalTo(LOCATION_NAME));
+ }
+
+ @Test
+ public void findLocationsByCity_shouldReturnBundleOfLocationsWithMatchingCity() throws Exception {
+ verifyURI(String.format("/Location?address-city=%s", CITY));
+
+ verify(locationService).searchForLocations(isNull(), stringAndListParamCaptor.capture(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull());
+ assertThat(stringAndListParamCaptor.getValue(), notNullValue());
+ assertThat(
+ stringAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ CoreMatchers.equalTo(CITY));
+ }
+
+ @Test
+ public void findLocationsByCountry_shouldReturnBundleOfLocationsWithMatchingCountry() throws Exception {
+ verifyURI(String.format("/Location?address-country=%s", COUNTRY));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), stringAndListParamCaptor.capture(), isNull(),
+ isNull(), isNull(), isNull(), isNull());
+ assertThat(stringAndListParamCaptor.getValue(), notNullValue());
+ assertThat(
+ stringAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ CoreMatchers.equalTo(COUNTRY));
+ }
+
+ @Test
+ public void findLocationsByPostalCode_shouldReturnBundleOfLocationsWithMatchingAddressCode() throws Exception {
+ verifyURI(String.format("/Location?address-postalcode=%s", POSTAL_CODE));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), stringAndListParamCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull());
+ assertThat(stringAndListParamCaptor.getValue(), notNullValue());
+ assertThat(
+ stringAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ CoreMatchers.equalTo(POSTAL_CODE));
+ }
+
+ @Test
+ public void findLocationsByState_shouldReturnBundleOfLocationsWithMatchingAddressState() throws Exception {
+ verifyURI(String.format("/Location?address-state=%s", STATE));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(),
+ stringAndListParamCaptor.capture(), isNull(), isNull(), isNull());
+ assertThat(stringAndListParamCaptor.getValue(), notNullValue());
+ assertThat(
+ stringAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ CoreMatchers.equalTo(STATE));
+ }
+
+ @Test
+ public void findLocationsByTag_shouldReturnBundleOfLocationsWithMatchingTag() throws Exception {
+ org.hl7.fhir.r4.model.Location location = new org.hl7.fhir.r4.model.Location();
+ location.getMeta().setTag(Collections.singletonList(new Coding(FhirConstants.OPENMRS_FHIR_EXT_LOCATION_TAG,
+ LOGIN_LOCATION_TAG_NAME, LOGIN_LOCATION_TAG_DESCRIPTION)));
+
+ when(locationService.searchForLocations(any(), any(), any(), any(), any(), any(TokenAndListParam.class), any(),
+ any())).thenReturn(Collections.singletonList(location));
+
+ MockHttpServletResponse response = get("/Location?_tag=" + LOGIN_LOCATION_TAG_NAME).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readBundleResponse(response).getEntry().size(), greaterThanOrEqualTo(1));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), tagCaptor.capture(),
+ isNull(), isNull());
+ assertThat(tagCaptor.getValue(), notNullValue());
+ assertThat(tagCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ CoreMatchers.equalTo(LOGIN_LOCATION_TAG_NAME));
+ }
+
+ @Test
+ public void findLocationsByParent_shouldReturnBundleOfLocationsWithMatchingParentId() throws Exception {
+ verifyURI(String.format("/Location?partof=%s", PARENT_LOCATION_ID));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ referenceAndListParamCaptor.capture(), isNull());
+ assertThat(referenceAndListParamCaptor.getValue(), notNullValue());
+ assertThat(referenceAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ equalTo(PARENT_LOCATION_ID));
+ }
+
+ @Test
+ public void findLocationsByParent_shouldReturnBundleOfLocationsWithMatchingParentName() throws Exception {
+ verifyURI(String.format("/Location?partof.name=%s", PARENT_LOCATION_NAME));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ referenceAndListParamCaptor.capture(), isNull());
+ assertThat(referenceAndListParamCaptor.getValue(), notNullValue());
+ assertThat(referenceAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ CoreMatchers.equalTo(PARENT_LOCATION_NAME));
+ }
+
+ @Test
+ public void findLocationsByParent_shouldReturnBundleOfLocationsWithMatchingParentCity() throws Exception {
+ verifyURI(String.format("/Location?partof.address-city=%s", PARENT_LOCATION_CITY));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ referenceAndListParamCaptor.capture(), isNull());
+ assertThat(referenceAndListParamCaptor.getValue(), notNullValue());
+ assertThat(referenceAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ CoreMatchers.equalTo(PARENT_LOCATION_CITY));
+ }
+
+ @Test
+ public void findLocationsByParent_shouldReturnBundleOfLocationsWithMatchingParentCountry() throws Exception {
+ verifyURI(String.format("/Location?partof.address-country=%s", PARENT_LOCATION_COUNTRY));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ referenceAndListParamCaptor.capture(), isNull());
+ assertThat(referenceAndListParamCaptor.getValue(), notNullValue());
+ assertThat(referenceAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ CoreMatchers.equalTo(PARENT_LOCATION_COUNTRY));
+ }
+
+ @Test
+ public void findLocationsByParent_shouldReturnBundleOfLocationsWithMatchingParentPostalCode() throws Exception {
+ verifyURI(String.format("/Location?partof.address-postalcode=%s", PARENT_LOCATION_POSTAL_CODE));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ referenceAndListParamCaptor.capture(), isNull());
+ assertThat(referenceAndListParamCaptor.getValue(), notNullValue());
+ assertThat(referenceAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ CoreMatchers.equalTo(PARENT_LOCATION_POSTAL_CODE));
+ }
+
+ @Test
+ public void findLocationsByParent_shouldReturnBundleOfLocationsWithMatchingParentState() throws Exception {
+ verifyURI(String.format("/Location?partof.address-state=%s", PARENT_LOCATION_STATE));
+
+ verify(locationService).searchForLocations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ referenceAndListParamCaptor.capture(), isNull());
+ assertThat(referenceAndListParamCaptor.getValue(), notNullValue());
+ assertThat(referenceAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ CoreMatchers.equalTo(PARENT_LOCATION_STATE));
+ }
+
+ @Test
+ public void shouldGetLocationByComplexQuery() throws Exception {
+ verifyURI(String.format("/Location?name=%s&partof.address-city=%s", LOCATION_NAME, PARENT_LOCATION_CITY));
+
+ verify(locationService).searchForLocations(stringAndListParamCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), referenceAndListParamCaptor.capture(), isNull());
+
+ assertThat(stringAndListParamCaptor.getValue(), notNullValue());
+ assertThat(
+ stringAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(LOCATION_NAME));
+
+ assertThat(referenceAndListParamCaptor.getValue(), notNullValue());
+ assertThat(referenceAndListParamCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ equalTo(PARENT_LOCATION_CITY));
+ }
+
+ @Test
+ public void shouldReturn404IfLocationNotFound() throws Exception {
+ when(locationService.get(WRONG_LOCATION_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/Location/" + WRONG_LOCATION_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void shouldVerifyGetLocationHistoryByIdUri() throws Exception {
+ org.hl7.fhir.r4.model.Location location = new org.hl7.fhir.r4.model.Location();
+ location.setId(LOCATION_UUID);
+ when(locationService.get(LOCATION_UUID)).thenReturn(location);
+
+ MockHttpServletResponse response = getLocationHistoryByIdRequest();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void shouldGetLocationHistoryById() throws IOException, ServletException {
+ Provenance provenance = new Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+ org.hl7.fhir.r4.model.Location location = new org.hl7.fhir.r4.model.Location();
+ location.setId(LOCATION_UUID);
+ location.addContained(provenance);
+
+ when(locationService.get(LOCATION_UUID)).thenReturn(location);
+
+ MockHttpServletResponse response = getLocationHistoryByIdRequest();
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results, Matchers.notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), Matchers.notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+
+ }
+
+ @Test
+ public void getLocationHistoryById_shouldReturnBundleWithEmptyEntriesIfPractitionerContainedIsEmpty() throws Exception {
+ org.hl7.fhir.r4.model.Location location = new org.hl7.fhir.r4.model.Location();
+ location.setId(LOCATION_UUID);
+ location.setContained(new ArrayList<>());
+ when(locationService.get(LOCATION_UUID)).thenReturn(location);
+
+ MockHttpServletResponse response = getLocationHistoryByIdRequest();
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getLocationHistoryById_shouldReturn404IfPractitionerIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/Location/" + WRONG_LOCATION_UUID + "/_history").accept(FhirMediaTypes.JSON)
+ .go();
+
+ assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getLocationHistoryByIdRequest() throws IOException, ServletException {
+ return get("/Location/" + LOCATION_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+ }
+
+ private void verifyURI(String uri) throws Exception {
+ org.hl7.fhir.r4.model.Location location = new org.hl7.fhir.r4.model.Location();
+ location.setId(LOCATION_UUID);
+ when(locationService.searchForLocations(any(), any(), any(), any(), any(), any(), any(), any()))
+ .thenReturn(Collections.singleton(location));
+
+ MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readBundleResponse(response).getEntry().size(), greaterThanOrEqualTo(1));
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..53cfdab81
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationFhirResourceProviderWebTest.java
@@ -0,0 +1,271 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsStringIgnoringCase;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import ca.uhn.fhir.rest.param.TokenParam;
+import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException;
+import lombok.Getter;
+import org.apache.commons.io.IOUtils;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.Medication;
+import org.hl7.fhir.r4.model.OperationOutcome;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirMedicationService;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MedicationFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String MEDICATION_UUID = "a2749656-1bc0-4d22-9c11-1d60026b672b";
+
+ private static final String WRONG_MEDICATION_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ private static final String CODE = "5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+ private static final String JSON_CREATE_MEDICATION_PATH = "org/openmrs/module/fhir2/providers/MedicationResourceWebTest_create.json";
+
+ private static final String JSON_UPDATE_MEDICATION_PATH = "org/openmrs/module/fhir2/providers/MedicationResourceWebTest_update.json";
+
+ private static final String JSON_UPDATE_WITHOUTID_MEDICATION_PATH = "org/openmrs/module/fhir2/providers/MedicationResourceWebTest_UdateWithoutId.json";
+
+ private static final String JSON_UPDATE_WITHWRONGID_MEDICATION_PATH = "org/openmrs/module/fhir2/providers/MedicationResourceWebTest_UdateWithWrongId.json";
+
+ @Mock
+ private FhirMedicationService fhirMedicationService;
+
+ @Getter
+ private MedicationFhirResourceProvider resourceProvider;
+
+ @Captor
+ private ArgumentCaptor tokenOrListParamArgumentCaptor;
+
+ @Captor
+ private ArgumentCaptor tokenAndListParamArgumentCaptor;
+
+ private org.hl7.fhir.r4.model.Medication medication;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new MedicationFhirResourceProvider();
+ resourceProvider.setMedicationService(fhirMedicationService);
+ medication = new org.hl7.fhir.r4.model.Medication();
+ medication.setId(MEDICATION_UUID);
+ super.setup();
+ }
+
+ @Test
+ public void getMedicationByUuid_shouldReturnMedication() throws Exception {
+ org.hl7.fhir.r4.model.Medication medication = new org.hl7.fhir.r4.model.Medication();
+ medication.setId(MEDICATION_UUID);
+ when(fhirMedicationService.get(MEDICATION_UUID)).thenReturn(medication);
+
+ MockHttpServletResponse response = get("/Medication/" + MEDICATION_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(MEDICATION_UUID));
+ }
+
+ @Test
+ public void searchForMedications_shouldSearchForMedicationsByCode() throws Exception {
+ verifyUri("/Medication?code=5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+
+ verify(fhirMedicationService).searchForMedications(tokenAndListParamArgumentCaptor.capture(), isNull(), isNull(),
+ isNull());
+
+ List listParams = tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens();
+ TokenParam tokenParam = listParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenParam.getValue(), equalTo(CODE));
+ }
+
+ @Test
+ public void searchForMedications_shouldSearchForMedicationsByDosageForm() throws Exception {
+ verifyUri("/Medication?form=5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+
+ verify(fhirMedicationService).searchForMedications(isNull(), tokenAndListParamArgumentCaptor.capture(), isNull(),
+ isNull());
+
+ List listParams = tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens();
+ TokenParam tokenParam = listParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenParam.getValue(), equalTo(CODE));
+ }
+
+ @Test
+ public void searchForMedications_shouldSearchForMedicationsByStatus() throws Exception {
+ verifyUri("/Medication?status=active");
+
+ verify(fhirMedicationService).searchForMedications(isNull(), isNull(), isNull(),
+ tokenAndListParamArgumentCaptor.capture());
+ assertThat(tokenAndListParamArgumentCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListParamArgumentCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0)
+ .getValue(),
+ equalTo("active"));
+ }
+
+ @Test
+ public void getMedicationByUuid_shouldReturn404() throws Exception {
+ when(fhirMedicationService.get(WRONG_MEDICATION_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/Medication/" + WRONG_MEDICATION_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ private void verifyUri(String uri) throws Exception {
+ when(fhirMedicationService.searchForMedications(any(), any(), any(), any()))
+ .thenReturn(Collections.singletonList(medication));
+
+ MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results.getEntry(), notNullValue());
+ assertThat(results.getEntry(), not(empty()));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(MEDICATION_UUID));
+ }
+
+ @Test
+ public void shouldCreateNewMedication() throws Exception {
+ org.hl7.fhir.r4.model.Medication medication = new org.hl7.fhir.r4.model.Medication();
+ medication.setId(MEDICATION_UUID);
+ String medicationJson;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_CREATE_MEDICATION_PATH)) {
+ assert is != null;
+ medicationJson = IOUtils.toString(is);
+ }
+
+ when(fhirMedicationService.create(any(org.hl7.fhir.r4.model.Medication.class))).thenReturn(medication);
+
+ MockHttpServletResponse response = post("/Medication").jsonContent(medicationJson).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isCreated());
+ assertThat(response.getStatus(), is(201));
+ }
+
+ @Test
+ public void shouldUpdateMedication() throws Exception {
+ org.hl7.fhir.r4.model.Medication medication = new org.hl7.fhir.r4.model.Medication();
+ medication.setId(MEDICATION_UUID);
+ medication.setStatus(org.hl7.fhir.r4.model.Medication.MedicationStatus.INACTIVE);
+ String medicationJson;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_MEDICATION_PATH)) {
+ assert is != null;
+ medicationJson = IOUtils.toString(is);
+ }
+
+ when(fhirMedicationService.update(any(String.class), any(org.hl7.fhir.r4.model.Medication.class)))
+ .thenReturn(medication);
+
+ MockHttpServletResponse response = put("/Medication/" + MEDICATION_UUID).jsonContent(medicationJson)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ }
+
+ @Test
+ public void updateMedicationShouldErrorForIdMismatch() throws Exception {
+ String medicationJson;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_MEDICATION_PATH)) {
+ medicationJson = IOUtils.toString(is);
+ }
+
+ MockHttpServletResponse response = put("/Medication/" + WRONG_MEDICATION_UUID).jsonContent(medicationJson)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isBadRequest());
+ assertThat(response.getContentAsString(),
+ containsStringIgnoringCase("body must contain an ID element which matches the request URL"));
+ }
+
+ @Test
+ public void updateMedicationShouldErrorForNoId() throws Exception {
+ String medicationJson;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_WITHOUTID_MEDICATION_PATH)) {
+ medicationJson = IOUtils.toString(is);
+ }
+
+ MockHttpServletResponse response = put("/Medication/" + MEDICATION_UUID).jsonContent(medicationJson)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isBadRequest());
+ assertThat(response.getContentAsString(), containsStringIgnoringCase("body must contain an ID element for update"));
+ }
+
+ @Test
+ public void updateMedicationShouldErrorForNonexistentMedication() throws Exception {
+ String medicationJson;
+ try (InputStream is = this.getClass().getClassLoader()
+ .getResourceAsStream(JSON_UPDATE_WITHWRONGID_MEDICATION_PATH)) {
+ medicationJson = IOUtils.toString(is);
+ }
+
+ when(fhirMedicationService.update(eq(WRONG_MEDICATION_UUID), any(org.hl7.fhir.r4.model.Medication.class)))
+ .thenThrow(new MethodNotAllowedException("Medication " + WRONG_MEDICATION_UUID + " does not exist"));
+
+ MockHttpServletResponse response = put("/Medication/" + WRONG_MEDICATION_UUID).jsonContent(medicationJson)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isMethodNotAllowed());
+ }
+
+ @Test
+ public void shouldDeleteMedication() throws Exception {
+ OperationOutcome retVal = new OperationOutcome();
+ retVal.setId(MEDICATION_UUID);
+ retVal.getText().setDivAsString("Deleted successfully");
+
+ org.hl7.fhir.r4.model.Medication medication = new org.hl7.fhir.r4.model.Medication();
+ medication.setId(MEDICATION_UUID);
+ medication.setStatus(org.hl7.fhir.r4.model.Medication.MedicationStatus.INACTIVE);
+
+ when(fhirMedicationService.delete(any(String.class))).thenReturn(medication);
+
+ MockHttpServletResponse response = delete("/Medication/" + MEDICATION_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..f29e13809
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/MedicationRequestFhirResourceProviderWebTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hl7.fhir.dstu3.model.MedicationRequest;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirMedicationRequestService;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MedicationRequestFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String MEDICATION_REQUEST_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ private static final String WRONG_MEDICATION_REQUEST_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
+
+ @Mock
+ private FhirMedicationRequestService fhirMedicationRequestService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private MedicationRequestFhirResourceProvider resourceProvider;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new MedicationRequestFhirResourceProvider();
+ resourceProvider.setMedicationRequestService(fhirMedicationRequestService);
+ super.setup();
+ }
+
+ @Test
+ public void getMedicationRequestByUuid_shouldReturnMedication() throws Exception {
+ org.hl7.fhir.r4.model.MedicationRequest medicationRequest = new org.hl7.fhir.r4.model.MedicationRequest();
+ medicationRequest.setId(MEDICATION_REQUEST_UUID);
+ when(fhirMedicationRequestService.get(MEDICATION_REQUEST_UUID)).thenReturn(medicationRequest);
+
+ MockHttpServletResponse response = get("/MedicationRequest/" + MEDICATION_REQUEST_UUID).accept(FhirMediaTypes.JSON)
+ .go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(MEDICATION_REQUEST_UUID));
+ }
+
+ @Test
+ public void getMedicationRequestByUuid_shouldReturn404() throws Exception {
+ when(fhirMedicationRequestService.get(WRONG_MEDICATION_REQUEST_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/MedicationRequest/" + WRONG_MEDICATION_REQUEST_UUID)
+ .accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProviderWebTest.java
similarity index 97%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProviderWebTest.java
index 8aa54ef21..f6960882d 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ObservationFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ObservationFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r3;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -20,6 +20,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import javax.servlet.ServletException;
+
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
@@ -38,8 +40,8 @@
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.lang.time.DateUtils;
-import org.hl7.fhir.r4.model.Bundle;
-import org.hl7.fhir.r4.model.Observation;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.Observation;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -48,11 +50,11 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirObservationService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
+import org.openmrs.module.fhir2.providers.MockIBundleProvider;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class ObservationFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class ObservationFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
private static final String OBS_UUID = "39fb7f47-e80a-4056-9285-bd798be13c63";
@@ -110,7 +112,7 @@ public class ObservationFhirResourceProviderWebTest extends BaseFhirResourceProv
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new ObservationFhirResourceProvider();
resourceProvider.setObservationService(observationService);
super.setup();
@@ -118,7 +120,7 @@ public void setup() throws Exception {
@Test
public void shouldGetObservationByUuid() throws Exception {
- Observation observation = new Observation();
+ org.hl7.fhir.r4.model.Observation observation = new org.hl7.fhir.r4.model.Observation();
observation.setId(OBS_UUID);
when(observationService.get(OBS_UUID)).thenReturn(observation);
@@ -150,7 +152,6 @@ public void shouldGetObservationsBySubjectUuid() throws Exception {
assertThat(patientCaptor.getValue(), notNullValue());
assertThat(referenceParam.getIdPart(), equalTo(PATIENT_UUID));
- assertThat(referenceParam.getChain(), equalTo(null));
}
@Test
@@ -166,7 +167,6 @@ public void shouldGetObservationsByPatientUuid() throws Exception {
assertThat(patientCaptor.getValue(), notNullValue());
assertThat(referenceParam.getIdPart(), equalTo(PATIENT_UUID));
assertThat(referenceParam.getResourceType(), equalTo("Patient"));
- assertThat(referenceParam.getChain(), equalTo(null));
}
@Test
@@ -340,7 +340,6 @@ public void shouldGetObservationsByEncounterUuid() throws Exception {
assertThat(encounterCaptor.getValue(), notNullValue());
assertThat(referenceParam.getIdPart(), equalTo("c4aa5682-90cf-48e8-87c9-a6066ffd3a3f"));
- assertThat(referenceParam.getChain(), equalTo(null));
}
@Test
@@ -492,7 +491,7 @@ public void shouldGetObservationsByPatientAndConcept() throws Exception {
@Test
public void shouldGetObservationsByMemberAndConcept() throws Exception {
- verifyUri("/Observation?code=" + URL_ENCODED_CIEL_URN + "|5098&has-member=" + MEMBER_UUID);
+ verifyUri("/Observation?code=" + URL_ENCODED_CIEL_URN + "|5098&related-type=" + MEMBER_UUID);
verify(observationService).searchForObservations(isNull(), isNull(), memberCaptor.capture(), isNull(), isNull(),
isNull(), isNull(), isNull(), codeCaptor.capture(), isNull());
@@ -515,7 +514,7 @@ public void shouldGetObservationsByMemberAndConcept() throws Exception {
@Test
public void shouldGetObservationsByMemberCode() throws Exception {
- verifyUri("/Observation?has-member.code=5098");
+ verifyUri("/Observation?related-type.code=5098");
verify(observationService).searchForObservations(isNull(), isNull(), memberCaptor.capture(), isNull(), isNull(),
isNull(), isNull(), isNull(), isNull(), isNull());
@@ -585,7 +584,7 @@ private void verifyUri(String uri) throws Exception {
Observation observation = new Observation();
observation.setId(OBS_UUID);
when(observationService.searchForObservations(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
- .thenReturn(new BaseFhirIBundleResourceProviderTest<>(Collections.singletonList(observation), 10, 1));
+ .thenReturn(new MockIBundleProvider<>(Collections.singletonList(observation), 10, 1));
MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirR3ResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirR3ResourceProviderWebTest.java
new file mode 100644
index 000000000..4a7c74ff2
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PatientFhirR3ResourceProviderWebTest.java
@@ -0,0 +1,519 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.apache.commons.lang3.time.DateUtils;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Patient;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirPatientService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PatientFhirR3ResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String PATIENT_UUID = "0b42f99b-776e-4388-8f6f-84357ae2a8fb";
+
+ private static final String BAD_PATIENT_UUID = "bb2354c1-e9e4-4020-bda0-d4a9f3232c9c";
+
+ private static final String AUTHOR = "author";
+
+ private static final String AUT = "AUT";
+
+ @Getter(AccessLevel.PUBLIC)
+ private PatientFhirResourceProvider resourceProvider;
+
+ @Mock
+ private FhirPatientService patientService;
+
+ @Captor
+ private ArgumentCaptor stringAndListCaptor;
+
+ @Captor
+ private ArgumentCaptor tokenAndListCaptor;
+
+ @Captor
+ private ArgumentCaptor dateRangeCaptor;
+
+ @Before
+ public void setup() throws ServletException {
+ resourceProvider = new PatientFhirResourceProvider();
+ resourceProvider.setPatientService(patientService);
+ super.setup();
+ }
+
+ @Test
+ public void shouldGetPatientByUuid() throws Exception {
+ org.hl7.fhir.r4.model.Patient patient = new org.hl7.fhir.r4.model.Patient();
+ patient.setId(PATIENT_UUID);
+ when(patientService.get(PATIENT_UUID)).thenReturn(patient);
+
+ MockHttpServletResponse response = get("/Patient/" + PATIENT_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Patient resource = readResponse(response);
+ assertThat(resource.getIdElement().getIdPart(), equalTo(PATIENT_UUID));
+ }
+
+ @Test
+ public void shouldReturn404IfPatientNotFound() throws Exception {
+ MockHttpServletResponse response = get("/Patient/" + BAD_PATIENT_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void shouldGetPatientByName() throws Exception {
+ verifyUri("/Patient/?name=Hannibal Lector");
+
+ verify(patientService).searchForPatients(stringAndListCaptor.capture(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("Hannibal Lector"));
+ }
+
+ @Test
+ public void shouldGetPatientByGivenName() throws Exception {
+ verifyUri("/Patient/?given=Hannibal");
+
+ verify(patientService).searchForPatients(isNull(), stringAndListCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("Hannibal"));
+ }
+
+ @Test
+ public void shouldGetPatientByFamilyName() throws Exception {
+ verifyUri("/Patient/?family=Lector");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), stringAndListCaptor.capture(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("Lector"));
+ }
+
+ @Test
+ public void shouldGetPatientByIdentifier() throws Exception {
+ verifyUri("/Patient/?identifier=M10000");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), tokenAndListCaptor.capture(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(tokenAndListCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("M10000"));
+ }
+
+ @Test
+ public void shouldGetPatientByGender() throws Exception {
+ verifyUri("/Patient/?gender=male");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), tokenAndListCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(tokenAndListCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("male"));
+ }
+
+ @Test
+ public void shouldGetPatientByBirthDate() throws Exception {
+ verifyUri("/Patient/?birthdate=eq1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), dateRangeCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByBirthDateGreaterThanOrEqualTo() throws Exception {
+ verifyUri("/Patient/?birthdate=ge1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), dateRangeCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetPatientByBirthDateGreaterThan() throws Exception {
+ verifyUri("/Patient/?birthdate=gt1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), dateRangeCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetPatientByBirthDateLessThanOrEqualTo() throws Exception {
+ verifyUri("/Patient/?birthdate=le1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), dateRangeCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound(), nullValue());
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByBirthDateLessThan() throws Exception {
+ verifyUri("/Patient/?birthdate=lt1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), dateRangeCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound(), nullValue());
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByBirthDateBetween() throws Exception {
+ verifyUri("/Patient/?birthdate=ge1975-02-02&birthdate=le1980-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), dateRangeCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar lowerBound = Calendar.getInstance();
+ lowerBound.set(1975, 1, 2);
+
+ Calendar upperBound = Calendar.getInstance();
+ upperBound.set(1980, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(lowerBound.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(upperBound.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByDeathDate() throws Exception {
+ verifyUri("/Patient/?death-date=eq1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ dateRangeCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByDeathDateGreaterThanOrEqualTo() throws Exception {
+ verifyUri("/Patient/?death-date=ge1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ dateRangeCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetPatientByDeathDateGreaterThan() throws Exception {
+ verifyUri("/Patient/?death-date=gt1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ dateRangeCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetPatientByDeathDateLessThanOrEqualTo() throws Exception {
+ verifyUri("/Patient/?death-date=le1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ dateRangeCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound(), nullValue());
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByDeathDateLessThan() throws Exception {
+ verifyUri("/Patient/?death-date=lt1975-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ dateRangeCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound(), nullValue());
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByDeathDateBetween() throws Exception {
+ verifyUri("/Patient/?death-date=ge1975-02-02&death-date=le1980-02-02");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ dateRangeCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar lowerBound = Calendar.getInstance();
+ lowerBound.set(1975, 1, 2);
+
+ Calendar upperBound = Calendar.getInstance();
+ upperBound.set(1980, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(lowerBound.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(upperBound.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPatientByDeceased() throws Exception {
+ verifyUri("/Patient/?deceased=true");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ tokenAndListCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ assertThat(tokenAndListCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("true"));
+ }
+
+ @Test
+ public void shouldGetPatientByCity() throws Exception {
+ verifyUri("/Patient/?address-city=Washington");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), stringAndListCaptor.capture(), isNull(), isNull(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("Washington"));
+ }
+
+ @Test
+ public void shouldGetPatientByState() throws Exception {
+ verifyUri("/Patient/?address-state=Washington");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), stringAndListCaptor.capture(), isNull(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("Washington"));
+ }
+
+ @Test
+ public void shouldGetPatientByCountry() throws Exception {
+ verifyUri("/Patient/?address-country=Washington");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), stringAndListCaptor.capture(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("Washington"));
+ }
+
+ @Test
+ public void shouldGetPatientByPostalCode() throws Exception {
+ verifyUri("/Patient/?address-postalcode=98136");
+
+ verify(patientService).searchForPatients(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), stringAndListCaptor.capture(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("98136"));
+ }
+
+ @Test
+ public void shouldVerifyGetPatientResourceHistoryUri() throws Exception {
+ org.hl7.fhir.r4.model.Patient patient = new org.hl7.fhir.r4.model.Patient();
+ patient.setId(PATIENT_UUID);
+ when(patientService.get(PATIENT_UUID)).thenReturn(patient);
+
+ MockHttpServletResponse response = getPatientHistoryRequest();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void shouldGetPatientResourceHistory() throws IOException, ServletException {
+ Provenance provenance = new Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+ org.hl7.fhir.r4.model.Patient patient = new org.hl7.fhir.r4.model.Patient();
+ patient.setId(PATIENT_UUID);
+ patient.addContained(provenance);
+
+ when(patientService.get(PATIENT_UUID)).thenReturn(patient);
+
+ MockHttpServletResponse response = getPatientHistoryRequest();
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results, notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+
+ }
+
+ @Test
+ public void shouldReturnBundleWithEmptyEntriesIfContainedIsEmpty() throws Exception {
+ org.hl7.fhir.r4.model.Patient patient = new org.hl7.fhir.r4.model.Patient();
+ patient.setId(PATIENT_UUID);
+ patient.setContained(new ArrayList<>());
+ when(patientService.get(PATIENT_UUID)).thenReturn(patient);
+
+ MockHttpServletResponse response = getPatientHistoryRequest();
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getPatientHistory_shouldReturn404IfPatientIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/Patient/" + BAD_PATIENT_UUID + "/_history").accept(FhirMediaTypes.JSON)
+ .go();
+
+ assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getPatientHistoryRequest() throws IOException, ServletException {
+ return get("/Patient/" + PATIENT_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+ }
+
+ private void verifyUri(String uri) throws Exception {
+ org.hl7.fhir.r4.model.Patient patient = new org.hl7.fhir.r4.model.Patient();
+ patient.setId(PATIENT_UUID);
+ when(patientService.searchForPatients(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(),
+ any(), any())).thenReturn(Collections.singletonList(patient));
+
+ MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(PATIENT_UUID));
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..3f29ad7e7
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PersonFhirResourceProviderWebTest.java
@@ -0,0 +1,410 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.apache.commons.lang3.time.DateUtils;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.Person;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.IdType;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirPersonService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PersonFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String PERSON_NAME = "Hannibal Lector";
+
+ private static final String PERSON_GENDER = "male";
+
+ private static final String PERSON_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
+
+ private static final String WRONG_PERSON_UUID = "9bf0d1ac-62a8-4440-a5a1-eb1015a7cc65";
+
+ private static final String ADDRESS_FIELD = "Washington";
+
+ private static final String POSTAL_CODE = "98136";
+
+ private static final String AUTHOR = "author";
+
+ private static final String AUT = "AUT";
+
+ @Mock
+ private FhirPersonService personService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private PersonFhirResourceProvider resourceProvider;
+
+ @Captor
+ private ArgumentCaptor stringAndListCaptor;
+
+ @Captor
+ private ArgumentCaptor tokenAndListCaptor;
+
+ @Captor
+ private ArgumentCaptor dateRangeCaptor;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new PersonFhirResourceProvider();
+ resourceProvider.setPersonService(personService);
+ super.setup();
+ }
+
+ @Test
+ public void shouldReturnPersonByUuid() throws Exception {
+ org.hl7.fhir.r4.model.Person person = new org.hl7.fhir.r4.model.Person();
+ person.setId(PERSON_UUID);
+ when(personService.get(PERSON_UUID)).thenReturn(person);
+
+ MockHttpServletResponse response = get("/Person/" + PERSON_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Person resource = readResponse(response);
+ assertThat(resource.getIdElement().getIdPart(), equalTo(PERSON_UUID));
+ }
+
+ @Test
+ public void shouldReturn404IfPersonNotFound() throws Exception {
+ when(personService.get(WRONG_PERSON_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/Person/" + WRONG_PERSON_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void shouldGetPersonByName() throws Exception {
+ verifyUri(String.format("/Person/?name=%s", PERSON_NAME));
+
+ verify(personService).searchForPeople(stringAndListCaptor.capture(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(PERSON_NAME));
+ }
+
+ @Test
+ public void shouldGetPersonByGender() throws Exception {
+ verifyUri(String.format("/Person/?gender=%s", PERSON_GENDER));
+
+ verify(personService).searchForPeople(isNull(), tokenAndListCaptor.capture(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull());
+
+ assertThat(tokenAndListCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(PERSON_GENDER));
+ }
+
+ @Test
+ public void shouldGetPersonByBirthDate() throws Exception {
+ verifyUri("/Person/?birthdate=eq1975-02-02");
+
+ verify(personService).searchForPeople(isNull(), isNull(), dateRangeCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPersonByBirthDateGreaterThanOrEqualTo() throws Exception {
+ verifyUri("/Person/?birthdate=ge1975-02-02");
+
+ verify(personService).searchForPeople(isNull(), isNull(), dateRangeCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetPersonByBirthDateGreaterThan() throws Exception {
+ verifyUri("/Person/?birthdate=gt1975-02-02");
+
+ verify(personService).searchForPeople(isNull(), isNull(), dateRangeCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetPersonByBirthDateLessThanOrEqualTo() throws Exception {
+ verifyUri("/Person/?birthdate=le1975-02-02");
+
+ verify(personService).searchForPeople(isNull(), isNull(), dateRangeCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound(), nullValue());
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPersonByBirthDateLessThan() throws Exception {
+ verifyUri("/Person/?birthdate=lt1975-02-02");
+
+ verify(personService).searchForPeople(isNull(), isNull(), dateRangeCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull());
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound(), nullValue());
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPersonByBirthDateBetween() throws Exception {
+ verifyUri("/Person/?birthdate=ge1975-02-02&birthdate=le1980-02-02");
+
+ verify(personService).searchForPeople(isNull(), isNull(), dateRangeCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull());
+
+ Calendar lowerBound = Calendar.getInstance();
+ lowerBound.set(1975, 1, 2);
+ Calendar upperBound = Calendar.getInstance();
+ upperBound.set(1980, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(lowerBound.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(upperBound.getTime(), Calendar.DATE)));
+ }
+
+ @Test
+ public void shouldGetPersonByCity() throws Exception {
+ verifyUri(String.format("/Person/?address-city=%s", ADDRESS_FIELD));
+
+ verify(personService).searchForPeople(isNull(), isNull(), isNull(), stringAndListCaptor.capture(), isNull(),
+ isNull(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(ADDRESS_FIELD));
+ }
+
+ @Test
+ public void shouldGetPersonByState() throws Exception {
+ verifyUri(String.format("/Person/?address-state=%s", ADDRESS_FIELD));
+
+ verify(personService).searchForPeople(isNull(), isNull(), isNull(), isNull(), stringAndListCaptor.capture(),
+ isNull(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(ADDRESS_FIELD));
+ }
+
+ @Test
+ public void shouldGetPersonByPostalCode() throws Exception {
+ verifyUri(String.format("/Person/?address-postalcode=%s", POSTAL_CODE));
+
+ verify(personService).searchForPeople(isNull(), isNull(), isNull(), isNull(), isNull(),
+ stringAndListCaptor.capture(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(POSTAL_CODE));
+ }
+
+ @Test
+ public void shouldGetPersonByCountry() throws Exception {
+ verifyUri(String.format("/Person/?address-country=%s", ADDRESS_FIELD));
+
+ verify(personService).searchForPeople(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ stringAndListCaptor.capture(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(ADDRESS_FIELD));
+ }
+
+ @Test
+ public void shouldGetPersonByComplexQuery() throws Exception {
+ verifyUri(String.format("/Person/?name=%s&gender=%s&birthdate=eq1975-02-02", PERSON_NAME, PERSON_GENDER));
+
+ verify(personService).searchForPeople(stringAndListCaptor.capture(), tokenAndListCaptor.capture(),
+ dateRangeCaptor.capture(), isNull(), isNull(), isNull(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(PERSON_NAME));
+
+ assertThat(tokenAndListCaptor.getValue(), notNullValue());
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(tokenAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(PERSON_GENDER));
+
+ assertThat(dateRangeCaptor.getValue(), notNullValue());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateRangeCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateRangeCaptor.getValue().getUpperBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ }
+
+ private void verifyUri(String uri) throws Exception {
+ org.hl7.fhir.r4.model.Person person = new org.hl7.fhir.r4.model.Person();
+ person.setId(PERSON_UUID);
+ when(personService.searchForPeople(any(), any(), any(), any(), any(), any(), any(), any()))
+ .thenReturn(Collections.singletonList(person));
+
+ MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(PERSON_UUID));
+ }
+
+ @Test
+ public void shouldVerifyGetPersonHistoryByIdUri() throws Exception {
+ org.hl7.fhir.r4.model.Person person = new org.hl7.fhir.r4.model.Person();
+ person.setId(PERSON_UUID);
+ when(personService.get(PERSON_UUID)).thenReturn(person);
+
+ MockHttpServletResponse response = getPersonHistoryByIdRequest();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void shouldGetPersonHistoryById() throws IOException, ServletException {
+ Provenance provenance = new Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+ org.hl7.fhir.r4.model.Person person = new org.hl7.fhir.r4.model.Person();
+ person.setId(PERSON_UUID);
+ person.addContained(provenance);
+
+ when(personService.get(PERSON_UUID)).thenReturn(person);
+
+ MockHttpServletResponse response = getPersonHistoryByIdRequest();
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results, notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+
+ }
+
+ @Test
+ public void getPersonHistoryById_shouldReturnBundleWithEmptyEntriesIfPersonContainedIsEmpty() throws Exception {
+ org.hl7.fhir.r4.model.Person person = new org.hl7.fhir.r4.model.Person();
+ person.setId(PERSON_UUID);
+ person.setContained(new ArrayList<>());
+ when(personService.get(PERSON_UUID)).thenReturn(person);
+
+ MockHttpServletResponse response = getPersonHistoryByIdRequest();
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getPersonHistoryById_shouldReturn404IfObservationIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/Person/" + WRONG_PERSON_UUID + "/_history").accept(FhirMediaTypes.JSON)
+ .go();
+
+ assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getPersonHistoryByIdRequest() throws IOException, ServletException {
+ return get("/Person/" + PERSON_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+ }
+
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProviderWebTest.java
similarity index 88%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProviderWebTest.java
index f538f4c8c..6a8341330 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/PractitionerFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/PractitionerFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r3;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -28,11 +28,11 @@
import lombok.AccessLevel;
import lombok.Getter;
-import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Practitioner;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
-import org.hl7.fhir.r4.model.IdType;
-import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.Provenance;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -41,11 +41,10 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirPractitionerService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class PractitionerFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class PractitionerFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
private static final String PRACTITIONER_UUID = "c51d0879-ed58-4655-a450-6527afba831f";
@@ -66,7 +65,7 @@ public class PractitionerFhirResourceProviderWebTest extends BaseFhirResourcePro
private FhirPractitionerService practitionerService;
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new PractitionerFhirResourceProvider();
resourceProvider.setPractitionerService(practitionerService);
super.setup();
@@ -74,7 +73,7 @@ public void setup() throws Exception {
@Test
public void getPractitionerById_shouldReturnPractitioner() throws Exception {
- Practitioner practitioner = new Practitioner();
+ org.hl7.fhir.r4.model.Practitioner practitioner = new org.hl7.fhir.r4.model.Practitioner();
practitioner.setId(PRACTITIONER_UUID);
when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
@@ -95,7 +94,7 @@ public void getEncounterByWrongUuid_shouldReturn404() throws Exception {
@Test
public void findPractitionersByName_shouldReturnBundleOfPractitioners() throws IOException, ServletException {
- Practitioner practitioner = new Practitioner();
+ org.hl7.fhir.r4.model.Practitioner practitioner = new org.hl7.fhir.r4.model.Practitioner();
practitioner.setId(PRACTITIONER_UUID);
when(practitionerService.findPractitionerByName(NAME)).thenReturn(Collections.singletonList(practitioner));
@@ -119,7 +118,7 @@ public void findPractitionersByBadName_shouldReturnBundleWithEmptyEntries() thro
@Test
public void findPractitionersByIdentifier_shouldReturnBundleOfPractitioners() throws IOException, ServletException {
- Practitioner practitioner = new Practitioner();
+ org.hl7.fhir.r4.model.Practitioner practitioner = new org.hl7.fhir.r4.model.Practitioner();
practitioner.setId(PRACTITIONER_UUID);
when(practitionerService.findPractitionerByIdentifier(PRACTITIONER_IDENTIFIER))
.thenReturn(Collections.singletonList(practitioner));
@@ -146,14 +145,14 @@ public void findPractitionersByBadIdentifier_shouldReturnBundleWithEmptyEntries(
@Test
public void shouldVerifyGetPractitionerHistoryByIdUri() throws Exception {
- Practitioner practitioner = new Practitioner();
+ org.hl7.fhir.r4.model.Practitioner practitioner = new org.hl7.fhir.r4.model.Practitioner();
practitioner.setId(PRACTITIONER_UUID);
when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
MockHttpServletResponse response = getPractitionerHistoryByIdRequest();
assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderTest.FhirMediaTypes.JSON.toString()));
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
}
@Test
@@ -168,7 +167,7 @@ public void shouldGetPractitionerHistoryById() throws IOException, ServletExcept
.setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
.addRole(new CodeableConcept().addCoding(
new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
- Practitioner practitioner = new Practitioner();
+ org.hl7.fhir.r4.model.Practitioner practitioner = new org.hl7.fhir.r4.model.Practitioner();
practitioner.setId(PRACTITIONER_UUID);
practitioner.addContained(provenance);
@@ -188,7 +187,7 @@ public void shouldGetPractitionerHistoryById() throws IOException, ServletExcept
@Test
public void getPractitionerHistoryById_shouldReturnBundleWithEmptyEntriesIfPractitionerContainedIsEmpty()
throws Exception {
- Practitioner practitioner = new Practitioner();
+ org.hl7.fhir.r4.model.Practitioner practitioner = new org.hl7.fhir.r4.model.Practitioner();
practitioner.setId(PRACTITIONER_UUID);
practitioner.setContained(new ArrayList<>());
when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
@@ -201,14 +200,13 @@ public void getPractitionerHistoryById_shouldReturnBundleWithEmptyEntriesIfPract
@Test
public void getPractitionerHistoryById_shouldReturn404IfPractitionerIdIsWrong() throws Exception {
MockHttpServletResponse response = get("/Practitioner/" + WRONG_PRACTITIONER_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ .accept(FhirMediaTypes.JSON).go();
assertThat(response, isNotFound());
}
private MockHttpServletResponse getPractitionerHistoryByIdRequest() throws IOException, ServletException {
- return get("/Practitioner/" + PRACTITIONER_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ return get("/Practitioner/" + PRACTITIONER_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
}
}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..a2c15bc84
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/ProcedureRequestFhirResourceProviderWebTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.providers.r3;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hl7.fhir.dstu3.model.ProcedureRequest;
+import org.hl7.fhir.r4.model.ServiceRequest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirServiceRequestService;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ProcedureRequestFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
+
+ private static final String SERVICE_REQUEST_UUID = "7d13b03b-58c2-43f5-b34d-08750c51aea9";
+
+ private static final String WRONG_SERVICE_REQUEST_UUID = "92b04062-e57d-43aa-8c38-90a1ad70080c";
+
+ @Getter(AccessLevel.PUBLIC)
+ private ProcedureRequestFhirResourceProvider resourceProvider;
+
+ @Mock
+ private FhirServiceRequestService service;
+
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new ProcedureRequestFhirResourceProvider();
+ resourceProvider.setServiceRequestService(service);
+ super.setup();
+ }
+
+ @Test
+ public void getServiceRequestById_shouldReturnServiceRequest() throws Exception {
+ ServiceRequest serviceRequest = new ServiceRequest();
+ serviceRequest.setId(SERVICE_REQUEST_UUID);
+
+ when(service.get(SERVICE_REQUEST_UUID)).thenReturn(serviceRequest);
+
+ MockHttpServletResponse response = get("/ProcedureRequest/" + SERVICE_REQUEST_UUID).accept(FhirMediaTypes.JSON).go();
+
+ assertThat(response, isOk());
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(SERVICE_REQUEST_UUID));
+ }
+
+ @Test
+ public void getEncounterByWrongUuid_shouldReturn404() throws Exception {
+ MockHttpServletResponse response = get("/ProcedureRequest/" + WRONG_SERVICE_REQUEST_UUID).accept(FhirMediaTypes.JSON)
+ .go();
+
+ assertThat(response, isNotFound());
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProviderWebTest.java
similarity index 87%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProviderWebTest.java
index f149c20f7..c31dceb87 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/RelatedPersonFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/RelatedPersonFhirResourceProviderWebTest.java
@@ -7,26 +7,27 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r3;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
+import javax.servlet.ServletException;
+
import lombok.AccessLevel;
import lombok.Getter;
-import org.hl7.fhir.r4.model.RelatedPerson;
+import org.hl7.fhir.dstu3.model.RelatedPerson;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirRelatedPersonService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class RelatedPersonFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class RelatedPersonFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
private static final String RELATED_PERSON_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
@@ -40,7 +41,7 @@ public class RelatedPersonFhirResourceProviderWebTest extends BaseFhirResourcePr
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new RelatedPersonFhirResourceProvider();
resourceProvider.setRelatedPersonService(relatedPersonService);
super.setup();
@@ -48,7 +49,7 @@ public void setup() throws Exception {
@Test
public void shouldReturnRelatedPersonById() throws Exception {
- RelatedPerson relatedPerson = new RelatedPerson();
+ org.hl7.fhir.r4.model.RelatedPerson relatedPerson = new org.hl7.fhir.r4.model.RelatedPerson();
relatedPerson.setId(RELATED_PERSON_UUID);
when(relatedPersonService.get(RELATED_PERSON_UUID)).thenReturn(relatedPerson);
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderWebTest.java
similarity index 89%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderWebTest.java
index ae2020b39..c83fd0f43 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/TaskFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r3;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsStringIgnoringCase;
@@ -32,12 +32,12 @@
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.io.IOUtils;
-import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.dstu3.model.Bundle;
+import org.hl7.fhir.dstu3.model.IdType;
+import org.hl7.fhir.dstu3.model.Task;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
-import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Provenance;
-import org.hl7.fhir.r4.model.Task;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -46,11 +46,10 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirTaskService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class TaskFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class TaskFhirResourceProviderWebTest extends BaseFhirR3ResourceProviderWebTest {
private static final String TASK_UUID = "55616228-dc6d-446f-ab50-4ec711ea9243";
@@ -62,7 +61,7 @@ public class TaskFhirResourceProviderWebTest extends BaseFhirResourceProviderTes
private static final String JSON_TASK_WRONG_ID_PATH = "org/openmrs/module/fhir2/providers/TestTask_CreateUpdate_WrongId.json";
- private Task task;
+ private org.hl7.fhir.r4.model.Task task;
@Mock
private FhirTaskService service;
@@ -71,13 +70,13 @@ public class TaskFhirResourceProviderWebTest extends BaseFhirResourceProviderTes
private TaskFhirResourceProvider resourceProvider;
@Before
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new TaskFhirResourceProvider();
- resourceProvider.setService(service);
+ resourceProvider.setFhirTaskService(service);
super.setup();
- task = new Task();
+ task = new org.hl7.fhir.r4.model.Task();
task.setId(TASK_UUID);
when(service.get(TASK_UUID)).thenReturn(task);
}
@@ -105,7 +104,7 @@ public void getTaskHistoryByIdRequest_shouldVerifyGetTaskHistoryByIdUri() throws
MockHttpServletResponse response = getTaskHistoryByIdRequest();
assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderTest.FhirMediaTypes.JSON.toString()));
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
}
@Test
@@ -145,14 +144,13 @@ public void getTaskHistoryById_shouldReturnBundleWithEmptyEntriesIfPractitionerC
@Test
public void getTaskHistoryById_shouldReturn404IfPractitionerIdIsWrong() throws Exception {
- MockHttpServletResponse response = get("/Task/" + WRONG_TASK_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ MockHttpServletResponse response = get("/Task/" + WRONG_TASK_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
assertThat(response, isNotFound());
}
private MockHttpServletResponse getTaskHistoryByIdRequest() throws IOException, ServletException {
- return get("/Task/" + TASK_UUID + "/_history").accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ return get("/Task/" + TASK_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
}
@Test
@@ -162,7 +160,7 @@ public void createTask_shouldCreateNewTask() throws Exception {
jsonTask = IOUtils.toString(is);
}
- when(service.create(any(Task.class))).thenReturn(task);
+ when(service.create(any(org.hl7.fhir.r4.model.Task.class))).thenReturn(task);
MockHttpServletResponse response = post("/Task").jsonContent(jsonTask).accept(FhirMediaTypes.JSON).go();
@@ -176,7 +174,7 @@ public void updateTask_shouldUpdateExistingTask() throws Exception {
jsonTask = IOUtils.toString(is);
}
- when(service.update(anyString(), any(Task.class))).thenReturn(task);
+ when(service.update(anyString(), any(org.hl7.fhir.r4.model.Task.class))).thenReturn(task);
MockHttpServletResponse response = put("/Task/" + TASK_UUID).jsonContent(jsonTask).accept(FhirMediaTypes.JSON).go();
@@ -217,7 +215,7 @@ public void updateTask_shouldErrorForNonexistentTask() throws Exception {
jsonTask = IOUtils.toString(is);
}
- when(service.update(eq(WRONG_TASK_UUID), any(Task.class)))
+ when(service.update(eq(WRONG_TASK_UUID), any(org.hl7.fhir.r4.model.Task.class)))
.thenThrow(new MethodNotAllowedException("Can't find Task"));
MockHttpServletResponse response = put("/Task/" + WRONG_TASK_UUID).jsonContent(jsonTask).accept(FhirMediaTypes.JSON)
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderWebTest.java
similarity index 97%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderWebTest.java
index cb5ad6624..95ee59867 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/AllergyIntoleranceFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -56,11 +56,11 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
+import org.openmrs.module.fhir2.providers.BaseFhirResourceProviderWebTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class AllergyIntoleranceFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class AllergyIntoleranceFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String ALLERGY_UUID = "1085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
@@ -86,7 +86,7 @@ public class AllergyIntoleranceFhirResourceProviderWebTest extends BaseFhirResou
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
allergyProvider = new AllergyIntoleranceFhirResourceProvider();
allergyProvider.setFhirAllergyIntoleranceService(allergyService);
allergyIntolerance = new AllergyIntolerance();
@@ -335,7 +335,7 @@ public void shouldVerifyAllergyIntoleranceHistoryByIdUri() throws Exception {
MockHttpServletResponse response = getAllergyIntoleranceHistoryRequest();
assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderTest.FhirMediaTypes.JSON.toString()));
+ assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderWebTest.FhirMediaTypes.JSON.toString()));
}
@Test
@@ -383,14 +383,14 @@ public void getAllergyIntoleranceHistoryById_shouldReturnBundleWithEmptyEntriesI
@Test
public void getAllergyIntoleranceHistoryById_shouldReturn404IfAllergyIntoleranceIdIsWrong() throws Exception {
MockHttpServletResponse response = get("/AllergyIntolerance/" + WRONG_ALLERGY_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ .accept(BaseFhirResourceProviderWebTest.FhirMediaTypes.JSON).go();
assertThat(response, isNotFound());
}
private MockHttpServletResponse getAllergyIntoleranceHistoryRequest() throws IOException, ServletException {
return get("/AllergyIntolerance/" + ALLERGY_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ .accept(BaseFhirResourceProviderWebTest.FhirMediaTypes.JSON).go();
}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/BaseFhirR4ResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/BaseFhirR4ResourceProviderWebTest.java
new file mode 100644
index 000000000..5c65a38bb
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/BaseFhirR4ResourceProviderWebTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.providers.r4;
+
+import java.io.UnsupportedEncodingException;
+import java.util.stream.Collectors;
+
+import ca.uhn.fhir.context.FhirContext;
+import ca.uhn.fhir.rest.server.IResourceProvider;
+import org.hamcrest.Description;
+import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
+import org.hl7.fhir.instance.model.api.IBaseResource;
+import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.r4.model.OperationOutcome;
+import org.openmrs.module.fhir2.providers.BaseFhirResourceProviderWebTest;
+import org.openmrs.module.fhir2.web.servlet.FhirRestServlet;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public abstract class BaseFhirR4ResourceProviderWebTest extends BaseFhirResourceProviderWebTest {
+
+ private static final FhirContext FHIR_CONTEXT = FhirContext.forR4();
+
+ @Override
+ public String getServletName() {
+ return "fhir2Servlet";
+ }
+
+ @Override
+ public FhirContext getFhirContext() {
+ return FHIR_CONTEXT;
+ }
+
+ @Override
+ public FhirRestServlet getRestfulServer() {
+ return new FhirRestServlet();
+ }
+
+ @Override
+ public void describeOperationOutcome(Description mismatchDescription, IBaseOperationOutcome baseOperationOutcome) {
+ if (baseOperationOutcome instanceof OperationOutcome) {
+ OperationOutcome operationOutcome = (OperationOutcome) baseOperationOutcome;
+ if (operationOutcome.hasIssue() && operationOutcome.getIssue().stream()
+ .anyMatch(o -> o.getSeverity().ordinal() <= OperationOutcome.IssueSeverity.WARNING.ordinal())) {
+ mismatchDescription.appendText(" with message ");
+ mismatchDescription.appendValue(operationOutcome.getIssue().stream()
+ .filter(o -> o.getSeverity().ordinal() <= OperationOutcome.IssueSeverity.WARNING.ordinal())
+ .map(OperationOutcome.OperationOutcomeIssueComponent::getDiagnostics)
+ .collect(Collectors.joining(". ")));
+ }
+ }
+ }
+
+ @Override
+ public Bundle readBundleResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
+ return (Bundle) super.readBundleResponse(response);
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderWebTest.java
similarity index 93%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderWebTest.java
index cd18af955..c0f77b145 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ConditionFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -42,11 +42,10 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirConditionService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class ConditionFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class ConditionFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String CONDITION_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
@@ -62,7 +61,7 @@ public class ConditionFhirResourceProviderWebTest extends BaseFhirResourceProvid
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new ConditionFhirResourceProvider();
resourceProvider.setConditionService(conditionService);
super.setup();
@@ -101,7 +100,7 @@ public void shouldVerifyConditionHistoryByIdUri() throws Exception {
MockHttpServletResponse response = getConditionHistoryRequest();
assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderTest.FhirMediaTypes.JSON.toString()));
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
}
@Test
@@ -148,14 +147,13 @@ public void getConditionHistoryById_shouldReturnBundleWithEmptyEntriesIfConditio
@Test
public void getConditionHistoryById_shouldReturn404IfConditionIdIsWrong() throws Exception {
MockHttpServletResponse response = get("/Condition/" + WRONG_CONDITION_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ .accept(FhirMediaTypes.JSON).go();
assertThat(response, isNotFound());
}
private MockHttpServletResponse getConditionHistoryRequest() throws IOException, ServletException {
- return get("/Condition/" + CONDITION_UUID + "/_history").accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON)
- .go();
+ return get("/Condition/" + CONDITION_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
}
@Test
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProviderWebTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProviderWebTest.java
index 0971abeb8..19fc6b6e5 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/DiagnosticReportFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/DiagnosticReportFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsStringIgnoringCase;
@@ -22,6 +22,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import javax.servlet.ServletException;
+
import java.io.InputStream;
import java.util.Calendar;
import java.util.Collections;
@@ -45,11 +47,10 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirDiagnosticReportService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class DiagnosticReportFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class DiagnosticReportFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String DIAGNOSTIC_REPORT_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
@@ -90,7 +91,7 @@ public class DiagnosticReportFhirResourceProviderWebTest extends BaseFhirResourc
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new DiagnosticReportFhirResourceProvider();
resourceProvider.setService(service);
super.setup();
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProviderWebTest.java
similarity index 98%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProviderWebTest.java
index 2dd2f52fc..d91bbeb7b 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/EncounterFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/EncounterFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -55,11 +55,10 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirEncounterService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class EncounterFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class EncounterFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String ENCOUNTER_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
@@ -111,7 +110,7 @@ public class EncounterFhirResourceProviderWebTest extends BaseFhirResourceProvid
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new EncounterFhirResourceProvider();
resourceProvider.setEncounterService(encounterService);
super.setup();
@@ -532,7 +531,7 @@ public void shouldVerifyEncounterHistoryByIdUri() throws Exception {
MockHttpServletResponse response = getEncounterHistoryRequest();
assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderTest.FhirMediaTypes.JSON.toString()));
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
}
@Test
@@ -579,14 +578,13 @@ public void getEncounterHistoryById_shouldReturnBundleWithEmptyEntriesIfPractiti
@Test
public void getEncounterHistoryById_shouldReturn404IfEncounterIdIsWrong() throws Exception {
MockHttpServletResponse response = get("/Encounter/" + WRONG_ENCOUNTER_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ .accept(FhirMediaTypes.JSON).go();
assertThat(response, isNotFound());
}
private MockHttpServletResponse getEncounterHistoryRequest() throws IOException, ServletException {
- return get("/Encounter/" + ENCOUNTER_UUID + "/_history").accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON)
- .go();
+ return get("/Encounter/" + ENCOUNTER_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
}
}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ListFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProviderWebTest.java
similarity index 89%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ListFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProviderWebTest.java
index fec5cc039..2d0067bfe 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ListFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ListFhirResourceProviderWebTest.java
@@ -7,12 +7,14 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
+import javax.servlet.ServletException;
+
import lombok.AccessLevel;
import lombok.Getter;
import org.hl7.fhir.r4.model.ListResource;
@@ -23,11 +25,10 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.Cohort;
import org.openmrs.module.fhir2.api.FhirListService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class ListFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class ListFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String LIST_UUID = "c0b1f314-1691-11df-97a5-7038c432aab88";
@@ -41,7 +42,7 @@ public class ListFhirResourceProviderWebTest extends BaseFhirResourceProviderTes
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
listFhirResourceProvider = new ListFhirResourceProvider();
listFhirResourceProvider.setCohortFhirListService(cohortFhirListService);
super.setup();
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProviderWebTest.java
similarity index 97%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProviderWebTest.java
index 075aa7a2a..c236230e3 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/LocationFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/LocationFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -50,11 +50,10 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirLocationService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class LocationFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class LocationFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String LOCATION_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
@@ -105,7 +104,7 @@ public class LocationFhirResourceProviderWebTest extends BaseFhirResourceProvide
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
locationProvider = new LocationFhirResourceProvider();
locationProvider.setFhirLocationService(locationService);
super.setup();
@@ -342,7 +341,7 @@ public void shouldVerifyGetLocationHistoryByIdUri() throws Exception {
MockHttpServletResponse response = getLocationHistoryByIdRequest();
assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderTest.FhirMediaTypes.JSON.toString()));
+ assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
}
@Test
@@ -388,14 +387,14 @@ public void getLocationHistoryById_shouldReturnBundleWithEmptyEntriesIfPractitio
@Test
public void getLocationHistoryById_shouldReturn404IfPractitionerIdIsWrong() throws Exception {
- MockHttpServletResponse response = get("/Location/" + WRONG_LOCATION_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ MockHttpServletResponse response = get("/Location/" + WRONG_LOCATION_UUID + "/_history").accept(FhirMediaTypes.JSON)
+ .go();
assertThat(response, isNotFound());
}
private MockHttpServletResponse getLocationHistoryByIdRequest() throws IOException, ServletException {
- return get("/Location/" + LOCATION_UUID + "/_history").accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ return get("/Location/" + LOCATION_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
}
private void verifyURI(String uri) throws Exception {
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderWebTest.java
similarity index 97%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderWebTest.java
index e3a856abb..00518a4b0 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsStringIgnoringCase;
@@ -22,6 +22,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import javax.servlet.ServletException;
+
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
@@ -43,11 +45,10 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirMedicationService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class MedicationFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class MedicationFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String MEDICATION_UUID = "a2749656-1bc0-4d22-9c11-1d60026b672b";
@@ -76,7 +77,7 @@ public class MedicationFhirResourceProviderWebTest extends BaseFhirResourceProvi
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new MedicationFhirResourceProvider();
resourceProvider.setFhirMedicationService(fhirMedicationService);
medication = new Medication();
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProviderWebTest.java
similarity index 91%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProviderWebTest.java
index 69ff8ba80..0aa36affd 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/MedicationRequestFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationRequestFhirResourceProviderWebTest.java
@@ -7,12 +7,14 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
+import javax.servlet.ServletException;
+
import lombok.AccessLevel;
import lombok.Getter;
import org.hl7.fhir.r4.model.MedicationRequest;
@@ -22,11 +24,10 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirMedicationRequestService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class MedicationRequestFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class MedicationRequestFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String MEDICATION_REQUEST_UUID = "c0938432-1691-11df-97a5-7038c432aaba";
@@ -40,7 +41,7 @@ public class MedicationRequestFhirResourceProviderWebTest extends BaseFhirResour
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new MedicationRequestFhirResourceProvider();
resourceProvider.setFhirMedicationRequestService(fhirMedicationRequestService);
super.setup();
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..044f55467
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ObservationFhirResourceProviderWebTest.java
@@ -0,0 +1,604 @@
+/*
+ * 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.providers.r4;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import java.io.UnsupportedEncodingException;
+import java.math.BigDecimal;
+import java.net.URLEncoder;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.List;
+
+import ca.uhn.fhir.rest.param.DateRangeParam;
+import ca.uhn.fhir.rest.param.QuantityAndListParam;
+import ca.uhn.fhir.rest.param.ReferenceAndListParam;
+import ca.uhn.fhir.rest.param.ReferenceOrListParam;
+import ca.uhn.fhir.rest.param.ReferenceParam;
+import ca.uhn.fhir.rest.param.StringAndListParam;
+import ca.uhn.fhir.rest.param.TokenAndListParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.apache.commons.lang.time.DateUtils;
+import org.hamcrest.MatcherAssert;
+import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.r4.model.Observation;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirObservationService;
+import org.openmrs.module.fhir2.providers.MockIBundleProvider;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ObservationFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
+
+ private static final String OBS_UUID = "39fb7f47-e80a-4056-9285-bd798be13c63";
+
+ private static final String BAD_OBS_UUID = "121b73a6-e1a4-4424-8610-d5765bf2fdf7";
+
+ private static final String PATIENT_UUID = "d9bc6c12-6adc-4ca6-8bde-441ec1a1c344";
+
+ private static final String MEMBER_UUID = "d9bc6c12-6adc-4ca6-8bde-441ec1a1c344";
+
+ private static final String CIEL_URN = "urn:oid:2.16.840.1.113883.3.7201";
+
+ private static final String URL_ENCODED_CIEL_URN;
+
+ static {
+ try {
+ URL_ENCODED_CIEL_URN = URLEncoder.encode(CIEL_URN, "utf-8");
+ }
+ catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Getter(AccessLevel.PUBLIC)
+ private ObservationFhirResourceProvider resourceProvider;
+
+ @Mock
+ private FhirObservationService observationService;
+
+ @Captor
+ private ArgumentCaptor patientCaptor;
+
+ @Captor
+ private ArgumentCaptor encounterCaptor;
+
+ @Captor
+ private ArgumentCaptor codeCaptor;
+
+ @Captor
+ private ArgumentCaptor memberCaptor;
+
+ @Captor
+ private ArgumentCaptor valueCodeCaptor;
+
+ @Captor
+ private ArgumentCaptor dateCaptor;
+
+ @Captor
+ private ArgumentCaptor valueQuantityCaptor;
+
+ @Captor
+ private ArgumentCaptor stringAndListCaptor;
+
+ @Captor
+ private ArgumentCaptor valueDateCaptor;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new ObservationFhirResourceProvider();
+ resourceProvider.setObservationService(observationService);
+ super.setup();
+ }
+
+ @Test
+ public void shouldGetObservationByUuid() throws Exception {
+ Observation observation = new Observation();
+ observation.setId(OBS_UUID);
+ when(observationService.get(OBS_UUID)).thenReturn(observation);
+
+ MockHttpServletResponse response = get("/Observation/" + OBS_UUID).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Observation resource = readResponse(response);
+ assertThat(resource.getIdElement().getIdPart(), equalTo(OBS_UUID));
+ }
+
+ @Test
+ public void shouldReturn404IfObservationNotFound() throws Exception {
+ MockHttpServletResponse response = get("/Observation/" + BAD_OBS_UUID).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void shouldGetObservationsBySubjectUuid() throws Exception {
+ verifyUri("/Observation?subject=" + PATIENT_UUID);
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getIdPart(), equalTo(PATIENT_UUID));
+ assertThat(referenceParam.getChain(), equalTo(null));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientUuid() throws Exception {
+ verifyUri("/Observation?subject:Patient=" + PATIENT_UUID);
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getIdPart(), equalTo(PATIENT_UUID));
+ assertThat(referenceParam.getResourceType(), equalTo("Patient"));
+ assertThat(referenceParam.getChain(), equalTo(null));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientIdentifier() throws Exception {
+ verifyUri("/Observation?subject.identifier=M4001-1");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo("M4001-1"));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientIdentifierWithOr() throws Exception {
+ verifyUri("/Observation?subject.identifier=M4001-1,ABS098,YT56RE,IU23O");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(patientCaptor.getAllValues().iterator().next().getValuesAsQueryTokens().iterator().next()
+ .getValuesAsQueryTokens().iterator().next().getChain(),
+ equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo("M4001-1"));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(4));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientIdentifierWithAnd() throws Exception {
+ verifyUri(
+ "/Observation?subject.identifier=M4001-1&subject.identifier=ABS098&subject.identifier=YT56RE&subject.identifier=IU23O");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("identifier"));
+ assertThat(referenceParam.getValue(), equalTo("M4001-1"));
+ assertThat(patientCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(4));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientName() throws Exception {
+ verifyUri("/Observation?subject.name=Hannibal Lector");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("name"));
+ assertThat(referenceParam.getValue(), equalTo("Hannibal Lector"));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientGivenName() throws Exception {
+ verifyUri("/Observation?subject.given=Hannibal");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("given"));
+ assertThat(referenceParam.getValue(), equalTo("Hannibal"));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientGivenNameWithOr() throws Exception {
+ verifyUri("/Observation?subject.given=Hannibal,Smith");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("given"));
+ assertThat(referenceParam.getValue(), equalTo("Hannibal"));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientGivenNameWithAnd() throws Exception {
+ verifyUri("/Observation?subject.given=Hannibal&subject.given=Smith");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("given"));
+ assertThat(referenceParam.getValue(), equalTo("Hannibal"));
+ assertThat(patientCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientFamilyName() throws Exception {
+ verifyUri("/Observation?subject.family=Lector");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo("Lector"));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientFamilyNameWithOr() throws Exception {
+ verifyUri("/Observation?subject.family=Lector,Rick,Tom");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo("Lector"));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(3));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientFamilyNameWithAnd() throws Exception {
+ verifyUri("/Observation?subject.family=Lector&subject.family=Rick&subject.family=Tom");
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getChain(), equalTo("family"));
+ assertThat(referenceParam.getValue(), equalTo("Lector"));
+ assertThat(patientCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(3));
+ }
+
+ @Test
+ public void shouldGetObservationsByEncounterUuid() throws Exception {
+ verifyUri("/Observation?encounter=c4aa5682-90cf-48e8-87c9-a6066ffd3a3f");
+
+ verify(observationService).searchForObservations(encounterCaptor.capture(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = encounterCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(encounterCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getIdPart(), equalTo("c4aa5682-90cf-48e8-87c9-a6066ffd3a3f"));
+ assertThat(referenceParam.getChain(), equalTo(null));
+ }
+
+ @Test
+ public void shouldGetObservationsByEncounterUuidWithOr() throws Exception {
+ verifyUri("/Observation?encounter=c4aa5682-90cf-48e8-87c9-a6066ffd3a3f,c4aa5682-90cf-48e8-87c9-auyt23ffd3a3f");
+
+ verify(observationService).searchForObservations(encounterCaptor.capture(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = encounterCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(encounterCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getIdPart(), equalTo("c4aa5682-90cf-48e8-87c9-a6066ffd3a3f"));
+ assertThat(orListParams.get(0).getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetObservationsByEncounterUuidWithAnd() throws Exception {
+ verifyUri(
+ "/Observation?encounter=c4aa5682-90cf-48e8-87c9-a6066ffd3a3f&encounter=c4aa5682-90cf-48e8-87c9-auyt23ffd3a3f");
+
+ verify(observationService).searchForObservations(encounterCaptor.capture(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ List orListParams = encounterCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ assertThat(encounterCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getIdPart(), equalTo("c4aa5682-90cf-48e8-87c9-a6066ffd3a3f"));
+ assertThat(encounterCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(2));
+ }
+
+ @Test
+ public void shouldGetObservationsByConceptId() throws Exception {
+ verifyUri("/Observation?code=5098");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), codeCaptor.capture(), isNull());
+ assertThat(codeCaptor.getValue(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(1));
+
+ TokenOrListParam orListParam = codeCaptor.getValue().getValuesAsQueryTokens().get(0);
+ assertThat(orListParam.getValuesAsQueryTokens(), notNullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().size(), equalTo(1));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getSystem(), nullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getValue(), equalTo("5098"));
+ }
+
+ @Test
+ public void shouldGetObservationsByValueConceptId() throws Exception {
+ verifyUri("/Observation?value-concept=5098");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), valueCodeCaptor.capture(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(valueCodeCaptor.getValue(), notNullValue());
+ assertThat(valueCodeCaptor.getValue().getValuesAsQueryTokens(), notNullValue());
+ assertThat(valueCodeCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(1));
+
+ TokenOrListParam orListParam = valueCodeCaptor.getValue().getValuesAsQueryTokens().get(0);
+ assertThat(orListParam.getValuesAsQueryTokens(), notNullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().size(), equalTo(1));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getSystem(), nullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getValue(), equalTo("5098"));
+ }
+
+ @Test
+ public void shouldGetObservationsByConceptAndSystem() throws Exception {
+ verifyUri("/Observation?code=" + URL_ENCODED_CIEL_URN + "|5098");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), codeCaptor.capture(), isNull());
+ assertThat(codeCaptor.getValue(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(1));
+
+ TokenOrListParam orListParam = codeCaptor.getValue().getValuesAsQueryTokens().get(0);
+ assertThat(orListParam.getValuesAsQueryTokens(), notNullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().size(), equalTo(1));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getSystem(), equalTo(CIEL_URN));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getValue(), equalTo("5098"));
+ }
+
+ @Test
+ public void shouldGetObservationsByConceptsAndSystem() throws Exception {
+ verifyUri("/Observation?code=" + URL_ENCODED_CIEL_URN + "|5098," + URL_ENCODED_CIEL_URN + "|5001");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), codeCaptor.capture(), isNull());
+ assertThat(codeCaptor.getValue(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(1));
+
+ TokenOrListParam orListParam = codeCaptor.getValue().getValuesAsQueryTokens().get(0);
+ assertThat(orListParam.getValuesAsQueryTokens(), notNullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().size(), equalTo(2));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getSystem(), equalTo(CIEL_URN));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getValue(), equalTo("5098"));
+ assertThat(orListParam.getValuesAsQueryTokens().get(1).getSystem(), equalTo(CIEL_URN));
+ assertThat(orListParam.getValuesAsQueryTokens().get(1).getValue(), equalTo("5001"));
+ }
+
+ @Test
+ public void shouldGetObservationsByValueConceptsAndSystem() throws Exception {
+ verifyUri("/Observation?value-concept=" + URL_ENCODED_CIEL_URN + "|5098," + URL_ENCODED_CIEL_URN + "|5001");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), valueCodeCaptor.capture(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+ assertThat(valueCodeCaptor.getValue(), notNullValue());
+ assertThat(valueCodeCaptor.getValue().getValuesAsQueryTokens(), notNullValue());
+ assertThat(valueCodeCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(1));
+
+ TokenOrListParam orListParam = valueCodeCaptor.getValue().getValuesAsQueryTokens().get(0);
+ assertThat(orListParam.getValuesAsQueryTokens(), notNullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().size(), equalTo(2));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getSystem(), equalTo(CIEL_URN));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getValue(), equalTo("5098"));
+ assertThat(orListParam.getValuesAsQueryTokens().get(1).getSystem(), equalTo(CIEL_URN));
+ assertThat(orListParam.getValuesAsQueryTokens().get(1).getValue(), equalTo("5001"));
+ }
+
+ @Test
+ public void shouldGetObservationsByPatientAndConcept() throws Exception {
+ verifyUri("/Observation?code=" + URL_ENCODED_CIEL_URN + "|5098&subject:Patient=" + PATIENT_UUID);
+
+ verify(observationService).searchForObservations(isNull(), patientCaptor.capture(), isNull(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), codeCaptor.capture(), isNull());
+
+ List orListParams = patientCaptor.getValue().getValuesAsQueryTokens();
+ ReferenceParam referenceParam = orListParams.get(0).getValuesAsQueryTokens().get(0);
+
+ // verify patient parameter
+ assertThat(patientCaptor.getValue(), notNullValue());
+ assertThat(referenceParam.getIdPart(), equalTo(PATIENT_UUID));
+ assertThat(referenceParam.getResourceType(), equalTo("Patient"));
+
+ // verify code parameter
+ assertThat(codeCaptor.getValue(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(1));
+
+ TokenOrListParam orListParam = codeCaptor.getValue().getValuesAsQueryTokens().get(0);
+ assertThat(orListParam.getValuesAsQueryTokens(), notNullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().size(), equalTo(1));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getSystem(), equalTo(CIEL_URN));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getValue(), equalTo("5098"));
+ }
+
+ @Test
+ public void shouldGetObservationsByMemberAndConcept() throws Exception {
+ verifyUri("/Observation?code=" + URL_ENCODED_CIEL_URN + "|5098&has-member=" + MEMBER_UUID);
+
+ verify(observationService).searchForObservations(isNull(), isNull(), memberCaptor.capture(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), codeCaptor.capture(), isNull());
+
+ // verify member parameter
+ assertThat(memberCaptor.getValue(), notNullValue());
+ assertThat(memberCaptor.getValue().getIdPart(), equalTo(MEMBER_UUID));
+
+ // verify code parameter
+ assertThat(codeCaptor.getValue(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens(), notNullValue());
+ assertThat(codeCaptor.getValue().getValuesAsQueryTokens().size(), equalTo(1));
+
+ TokenOrListParam orListParam = codeCaptor.getValue().getValuesAsQueryTokens().get(0);
+ assertThat(orListParam.getValuesAsQueryTokens(), notNullValue());
+ assertThat(orListParam.getValuesAsQueryTokens().size(), equalTo(1));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getSystem(), equalTo(CIEL_URN));
+ assertThat(orListParam.getValuesAsQueryTokens().get(0).getValue(), equalTo("5098"));
+ }
+
+ @Test
+ public void shouldGetObservationsByMemberCode() throws Exception {
+ verifyUri("/Observation?has-member.code=5098");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), memberCaptor.capture(), isNull(), isNull(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ assertThat(memberCaptor.getValue(), notNullValue());
+ assertThat(memberCaptor.getValue().getChain(), equalTo(Observation.SP_CODE));
+ assertThat(memberCaptor.getValue().getValue(), equalTo("5098"));
+ }
+
+ @Test
+ public void shouldGetObservationsByValueDate() throws Exception {
+ verifyUri("/Observation?value-date=ge1975-02-02");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), isNull(), valueDateCaptor.capture(),
+ isNull(), isNull(), isNull(), isNull(), isNull());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(valueDateCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(valueDateCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ @Test
+ public void shouldGetObservationsByValueQuantity() throws Exception {
+ verifyUri("/Observation?value-quantity=134.0");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), isNull(), isNull(),
+ valueQuantityCaptor.capture(), isNull(), isNull(), isNull(), isNull());
+
+ assertThat(valueQuantityCaptor.getValue(), notNullValue());
+ assertThat(valueQuantityCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(valueQuantityCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo(BigDecimal.valueOf(134.0)));
+ }
+
+ @Test
+ public void shouldGetObservationsByValueString() throws Exception {
+ verifyUri("/Observation?value-string=AFH56");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ stringAndListCaptor.capture(), isNull(), isNull(), isNull());
+
+ assertThat(stringAndListCaptor.getValue(), notNullValue());
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens(), not(empty()));
+ assertThat(stringAndListCaptor.getValue().getValuesAsQueryTokens().get(0).getValuesAsQueryTokens().get(0).getValue(),
+ equalTo("AFH56"));
+ }
+
+ @Test
+ public void shouldGetObservationsByDate() throws Exception {
+ verifyUri("/Observation?date=ge1975-02-02");
+
+ verify(observationService).searchForObservations(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
+ isNull(), dateCaptor.capture(), isNull(), isNull());
+
+ Calendar calendar = Calendar.getInstance();
+ calendar.set(1975, 1, 2);
+
+ assertThat(dateCaptor.getValue().getLowerBound().getValue(),
+ equalTo(DateUtils.truncate(calendar.getTime(), Calendar.DATE)));
+ assertThat(dateCaptor.getValue().getUpperBound(), nullValue());
+ }
+
+ private void verifyUri(String uri) throws Exception {
+ Observation observation = new Observation();
+ observation.setId(OBS_UUID);
+ when(observationService.searchForObservations(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
+ .thenReturn(new MockIBundleProvider<>(Collections.singletonList(observation), 10, 1));
+
+ MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results.getEntry(), notNullValue());
+ assertThat(results.getEntry(), not(empty()));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(OBS_UUID));
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderWebTest.java
similarity index 99%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderWebTest.java
index 318696ab2..1a3da24b9 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/PatientFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PatientFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -51,11 +51,10 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirPatientService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class PatientFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class PatientFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String PATIENT_UUID = "0b42f99b-776e-4388-8f6f-84357ae2a8fb";
@@ -81,7 +80,7 @@ public class PatientFhirResourceProviderWebTest extends BaseFhirResourceProvider
private ArgumentCaptor dateRangeCaptor;
@Before
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new PatientFhirResourceProvider();
resourceProvider.setPatientService(patientService);
super.setup();
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProviderWebTest.java
similarity index 94%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProviderWebTest.java
index 57052732a..fe5b03e98 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/PersonFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PersonFhirResourceProviderWebTest.java
@@ -7,7 +7,7 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
@@ -35,6 +35,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.lang3.time.DateUtils;
+import org.hamcrest.MatcherAssert;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
@@ -51,11 +52,10 @@
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirPersonService;
import org.openmrs.module.fhir2.api.util.FhirUtils;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class PersonFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class PersonFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String PERSON_NAME = "Hannibal Lector";
@@ -90,7 +90,7 @@ public class PersonFhirResourceProviderWebTest extends BaseFhirResourceProviderT
@Before
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new PersonFhirResourceProvider();
resourceProvider.setFhirPersonService(personService);
super.setup();
@@ -104,8 +104,8 @@ public void shouldReturnPersonByUuid() throws Exception {
MockHttpServletResponse response = get("/Person/" + PERSON_UUID).accept(FhirMediaTypes.JSON).go();
- assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
Person resource = readResponse(response);
assertThat(resource.getIdElement().getIdPart(), equalTo(PERSON_UUID));
@@ -117,7 +117,7 @@ public void shouldReturn404IfPersonNotFound() throws Exception {
MockHttpServletResponse response = get("/Person/" + WRONG_PERSON_UUID).accept(FhirMediaTypes.JSON).go();
- assertThat(response, isNotFound());
+ MatcherAssert.assertThat(response, isNotFound());
}
@Test
@@ -334,8 +334,8 @@ private void verifyUri(String uri) throws Exception {
MockHttpServletResponse response = get(uri).accept(FhirMediaTypes.JSON).go();
- assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
Bundle results = readBundleResponse(response);
assertThat(results.hasEntry(), is(true));
@@ -351,8 +351,8 @@ public void shouldVerifyGetPersonHistoryByIdUri() throws Exception {
MockHttpServletResponse response = getPersonHistoryByIdRequest();
- assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(BaseFhirResourceProviderTest.FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
}
@Test
@@ -398,14 +398,14 @@ public void getPersonHistoryById_shouldReturnBundleWithEmptyEntriesIfPersonConta
@Test
public void getPersonHistoryById_shouldReturn404IfObservationIdIsWrong() throws Exception {
- MockHttpServletResponse response = get("/Person/" + WRONG_PERSON_UUID + "/_history")
- .accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ MockHttpServletResponse response = get("/Person/" + WRONG_PERSON_UUID + "/_history").accept(FhirMediaTypes.JSON)
+ .go();
- assertThat(response, isNotFound());
+ MatcherAssert.assertThat(response, isNotFound());
}
private MockHttpServletResponse getPersonHistoryByIdRequest() throws IOException, ServletException {
- return get("/Person/" + PERSON_UUID + "/_history").accept(BaseFhirResourceProviderTest.FhirMediaTypes.JSON).go();
+ return get("/Person/" + PERSON_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
}
}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..08cb75202
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/PractitionerFhirResourceProviderWebTest.java
@@ -0,0 +1,213 @@
+/*
+ * 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.providers.r4;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.when;
+import static org.openmrs.module.fhir2.FhirConstants.AUT;
+import static org.openmrs.module.fhir2.FhirConstants.AUTHOR;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hamcrest.MatcherAssert;
+import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.IdType;
+import org.hl7.fhir.r4.model.Practitioner;
+import org.hl7.fhir.r4.model.Provenance;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirPractitionerService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PractitionerFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
+
+ private static final String PRACTITIONER_UUID = "c51d0879-ed58-4655-a450-6527afba831f";
+
+ private static final String WRONG_PRACTITIONER_UUID = "810abbe5-4eca-47de-8e00-3f334ec89036";
+
+ private static final String NAME = "Ricky sanchez";
+
+ private static final String BAD_NAME = "bad name";
+
+ private static final String PRACTITIONER_IDENTIFIER = "eu984ot-k";
+
+ private static final String BAD_PRACTITIONER_IDENTIFIER = "bad identifier";
+
+ @Getter(AccessLevel.PUBLIC)
+ private PractitionerFhirResourceProvider resourceProvider;
+
+ @Mock
+ private FhirPractitionerService practitionerService;
+
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new PractitionerFhirResourceProvider();
+ resourceProvider.setPractitionerService(practitionerService);
+ super.setup();
+ }
+
+ @Test
+ public void getPractitionerById_shouldReturnPractitioner() throws Exception {
+ Practitioner practitioner = new Practitioner();
+ practitioner.setId(PRACTITIONER_UUID);
+ when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
+
+ MockHttpServletResponse response = get("/Practitioner/" + PRACTITIONER_UUID).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(PRACTITIONER_UUID));
+ }
+
+ @Test
+ public void getEncounterByWrongUuid_shouldReturn404() throws Exception {
+
+ MockHttpServletResponse response = get("/Practitioner/" + WRONG_PRACTITIONER_UUID).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void findPractitionersByName_shouldReturnBundleOfPractitioners() throws IOException, ServletException {
+ Practitioner practitioner = new Practitioner();
+ practitioner.setId(PRACTITIONER_UUID);
+ when(practitionerService.findPractitionerByName(NAME)).thenReturn(Collections.singletonList(practitioner));
+
+ MockHttpServletResponse response = get("/Practitioner?name=" + NAME).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(readBundleResponse(response).getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void findPractitionersByBadName_shouldReturnBundleWithEmptyEntries() throws IOException, ServletException {
+ when(practitionerService.findPractitionerByName(BAD_NAME)).thenReturn(new ArrayList<>());
+
+ MockHttpServletResponse response = get("/Practitioner?name=" + BAD_NAME).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(readBundleResponse(response).getEntry(), is(empty()));
+ }
+
+ @Test
+ public void findPractitionersByIdentifier_shouldReturnBundleOfPractitioners() throws IOException, ServletException {
+ Practitioner practitioner = new Practitioner();
+ practitioner.setId(PRACTITIONER_UUID);
+ when(practitionerService.findPractitionerByIdentifier(PRACTITIONER_IDENTIFIER))
+ .thenReturn(Collections.singletonList(practitioner));
+
+ MockHttpServletResponse response = get("/Practitioner?identifier=" + PRACTITIONER_IDENTIFIER)
+ .accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(readBundleResponse(response).getEntry().size(), greaterThanOrEqualTo(1));
+ }
+
+ @Test
+ public void findPractitionersByBadIdentifier_shouldReturnBundleWithEmptyEntries() throws IOException, ServletException {
+ when(practitionerService.findPractitionerByIdentifier(BAD_PRACTITIONER_IDENTIFIER)).thenReturn(new ArrayList<>());
+
+ MockHttpServletResponse response = get("/Practitioner?identifier=" + BAD_PRACTITIONER_IDENTIFIER)
+ .accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(readBundleResponse(response).getEntry(), is(empty()));
+ }
+
+ @Test
+ public void shouldVerifyGetPractitionerHistoryByIdUri() throws Exception {
+ Practitioner practitioner = new Practitioner();
+ practitioner.setId(PRACTITIONER_UUID);
+ when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
+
+ MockHttpServletResponse response = getPractitionerHistoryByIdRequest();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void shouldGetPractitionerHistoryById() throws IOException, ServletException {
+ Provenance provenance = new Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+ Practitioner practitioner = new Practitioner();
+ practitioner.setId(PRACTITIONER_UUID);
+ practitioner.addContained(provenance);
+
+ when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
+
+ MockHttpServletResponse response = getPractitionerHistoryByIdRequest();
+
+ Bundle results = readBundleResponse(response);
+ assertThat(results, notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+
+ }
+
+ @Test
+ public void getPractitionerHistoryById_shouldReturnBundleWithEmptyEntriesIfPractitionerContainedIsEmpty()
+ throws Exception {
+ Practitioner practitioner = new Practitioner();
+ practitioner.setId(PRACTITIONER_UUID);
+ practitioner.setContained(new ArrayList<>());
+ when(practitionerService.getPractitionerByUuid(PRACTITIONER_UUID)).thenReturn(practitioner);
+
+ MockHttpServletResponse response = getPractitionerHistoryByIdRequest();
+ Bundle results = readBundleResponse(response);
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getPractitionerHistoryById_shouldReturn404IfPractitionerIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/Practitioner/" + WRONG_PRACTITIONER_UUID + "/_history")
+ .accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getPractitionerHistoryByIdRequest() throws IOException, ServletException {
+ return get("/Practitioner/" + PRACTITIONER_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+ }
+
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..d1e86f926
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/RelatedPersonFhirResourceProviderWebTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.providers.r4;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletException;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.hamcrest.MatcherAssert;
+import org.hl7.fhir.r4.model.RelatedPerson;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.api.FhirRelatedPersonService;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RelatedPersonFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
+
+ private static final String RELATED_PERSON_UUID = "8a849d5e-6011-4279-a124-40ada5a687de";
+
+ private static final String WRONG_RELATED_PERSON_UUID = "9bf0d1ac-62a8-4440-a5a1-eb1015a7cc65";
+
+ @Mock
+ private FhirRelatedPersonService relatedPersonService;
+
+ @Getter(AccessLevel.PUBLIC)
+ private RelatedPersonFhirResourceProvider resourceProvider;
+
+ @Before
+ @Override
+ public void setup() throws ServletException {
+ resourceProvider = new RelatedPersonFhirResourceProvider();
+ resourceProvider.setRelatedPersonService(relatedPersonService);
+ super.setup();
+ }
+
+ @Test
+ public void shouldReturnRelatedPersonById() throws Exception {
+ RelatedPerson relatedPerson = new RelatedPerson();
+ relatedPerson.setId(RELATED_PERSON_UUID);
+ when(relatedPersonService.get(RELATED_PERSON_UUID)).thenReturn(relatedPerson);
+
+ MockHttpServletResponse response = get("/RelatedPerson/" + RELATED_PERSON_UUID).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+
+ RelatedPerson resource = readResponse(response);
+ assertThat(resource.getIdElement().getIdPart(), equalTo(RELATED_PERSON_UUID));
+ }
+
+ @Test
+ public void shouldReturn404IfRelatedPersonNotFound() throws Exception {
+ when(relatedPersonService.get(WRONG_RELATED_PERSON_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/RelatedPerson/" + WRONG_RELATED_PERSON_UUID).accept(FhirMediaTypes.JSON)
+ .go();
+
+ MatcherAssert.assertThat(response, isNotFound());
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProviderWebTest.java
similarity index 79%
rename from omod/src/test/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProviderWebTest.java
rename to omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProviderWebTest.java
index 70ad49d01..48e9ce267 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/providers/ServiceRequestFhirResourceProviderWebTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/ServiceRequestFhirResourceProviderWebTest.java
@@ -7,25 +7,27 @@
* 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.providers;
+package org.openmrs.module.fhir2.providers.r4;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
+import javax.servlet.ServletException;
+
import lombok.AccessLevel;
import lombok.Getter;
+import org.hamcrest.MatcherAssert;
import org.hl7.fhir.r4.model.ServiceRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirServiceRequestService;
-import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
-public class ServiceRequestFhirResourceProviderWebTest extends BaseFhirResourceProviderTest {
+public class ServiceRequestFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
private static final String SERVICE_REQUEST_UUID = "7d13b03b-58c2-43f5-b34d-08750c51aea9";
@@ -38,7 +40,7 @@ public class ServiceRequestFhirResourceProviderWebTest extends BaseFhirResourceP
private FhirServiceRequestService service;
@Override
- public void setup() throws Exception {
+ public void setup() throws ServletException {
resourceProvider = new ServiceRequestFhirResourceProvider();
resourceProvider.setServiceRequestService(service);
super.setup();
@@ -53,9 +55,9 @@ public void getServiceRequestById_shouldReturnServiceRequest() throws Exception
MockHttpServletResponse response = get("/ServiceRequest/" + SERVICE_REQUEST_UUID).accept(FhirMediaTypes.JSON).go();
- assertThat(response, isOk());
- assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
- assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(SERVICE_REQUEST_UUID));
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(SERVICE_REQUEST_UUID));
}
@Test
@@ -63,6 +65,6 @@ public void getEncounterByWrongUuid_shouldReturn404() throws Exception {
MockHttpServletResponse response = get("/ServiceRequest/" + WRONG_SERVICE_REQUEST_UUID).accept(FhirMediaTypes.JSON)
.go();
- assertThat(response, isNotFound());
+ MatcherAssert.assertThat(response, isNotFound());
}
}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProviderWebTest.java
new file mode 100644
index 000000000..1969f9caa
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/TaskFhirResourceProviderWebTest.java
@@ -0,0 +1,227 @@
+/*
+ * 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.providers.r4;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsStringIgnoringCase;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+import static org.openmrs.module.fhir2.FhirConstants.AUT;
+import static org.openmrs.module.fhir2.FhirConstants.AUTHOR;
+
+import javax.servlet.ServletException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Date;
+
+import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException;
+import lombok.AccessLevel;
+import lombok.Getter;
+import org.apache.commons.io.IOUtils;
+import org.hamcrest.MatcherAssert;
+import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.r4.model.CodeableConcept;
+import org.hl7.fhir.r4.model.Coding;
+import org.hl7.fhir.r4.model.IdType;
+import org.hl7.fhir.r4.model.Provenance;
+import org.hl7.fhir.r4.model.Task;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openmrs.module.fhir2.FhirConstants;
+import org.openmrs.module.fhir2.api.FhirTaskService;
+import org.openmrs.module.fhir2.api.util.FhirUtils;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TaskFhirResourceProviderWebTest extends BaseFhirR4ResourceProviderWebTest {
+
+ private static final String TASK_UUID = "55616228-dc6d-446f-ab50-4ec711ea9243";
+
+ private static final String WRONG_TASK_UUID = "df34a1c1-f57b-4c33-bee5-e601b56b9d5b";
+
+ private static final String JSON_TASK_PATH = "org/openmrs/module/fhir2/providers/TestTask_CreateUpdate.json";
+
+ private static final String JSON_TASK_NO_ID_PATH = "org/openmrs/module/fhir2/providers/TestTask_CreateUpdate_NoId.json";
+
+ private static final String JSON_TASK_WRONG_ID_PATH = "org/openmrs/module/fhir2/providers/TestTask_CreateUpdate_WrongId.json";
+
+ private Task task;
+
+ @Mock
+ private FhirTaskService service;
+
+ @Getter(AccessLevel.PUBLIC)
+ private TaskFhirResourceProvider resourceProvider;
+
+ @Before
+ public void setup() throws ServletException {
+ resourceProvider = new TaskFhirResourceProvider();
+ resourceProvider.setService(service);
+
+ super.setup();
+
+ task = new Task();
+ task.setId(TASK_UUID);
+ when(service.get(TASK_UUID)).thenReturn(task);
+ }
+
+ @Test
+ public void getTaskByUuid_shouldReturnTask() throws Exception {
+ MockHttpServletResponse response = get("/Task/" + TASK_UUID).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ MatcherAssert.assertThat(readResponse(response).getIdElement().getIdPart(), equalTo(TASK_UUID));
+ }
+
+ @Test
+ public void getTaskByWrongUuid_shouldReturn404() throws Exception {
+ when(service.get(WRONG_TASK_UUID)).thenReturn(null);
+
+ MockHttpServletResponse response = get("/Task/" + WRONG_TASK_UUID).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isNotFound());
+ }
+
+ @Test
+ public void getTaskHistoryByIdRequest_shouldVerifyGetTaskHistoryByIdUri() throws Exception {
+ MockHttpServletResponse response = getTaskHistoryByIdRequest();
+
+ MatcherAssert.assertThat(response, isOk());
+ MatcherAssert.assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));
+ }
+
+ @Test
+ public void getTaskHistoryByIdRequest_shouldGetTaskHistoryById() throws IOException, ServletException {
+ Provenance provenance = new Provenance();
+ provenance.setId(new IdType(FhirUtils.uniqueUuid()));
+ provenance.setRecorded(new Date());
+ provenance.setActivity(new CodeableConcept().addCoding(
+ new Coding().setCode("CREATE").setSystem(FhirConstants.FHIR_TERMINOLOGY_DATA_OPERATION).setDisplay("create")));
+ provenance.addAgent(new Provenance.ProvenanceAgentComponent()
+ .setType(new CodeableConcept().addCoding(new Coding().setCode(AUT).setDisplay(AUTHOR)
+ .setSystem(FhirConstants.FHIR_TERMINOLOGY_PROVENANCE_PARTICIPANT_TYPE)))
+ .addRole(new CodeableConcept().addCoding(
+ new Coding().setCode("").setDisplay("").setSystem(FhirConstants.FHIR_TERMINOLOGY_PARTICIPATION_TYPE))));
+
+ task.addContained(provenance);
+
+ MockHttpServletResponse response = getTaskHistoryByIdRequest();
+ Bundle results = readBundleResponse(response);
+
+ assertThat(results, notNullValue());
+ assertThat(results.hasEntry(), is(true));
+ assertThat(results.getEntry().get(0).getResource(), notNullValue());
+ assertThat(results.getEntry().get(0).getResource().getResourceType().name(),
+ equalTo(Provenance.class.getSimpleName()));
+ }
+
+ @Test
+ public void getTaskHistoryById_shouldReturnBundleWithEmptyEntriesIfPractitionerContainedIsEmpty() throws Exception {
+ task.setContained(new ArrayList<>());
+
+ MockHttpServletResponse response = getTaskHistoryByIdRequest();
+ Bundle results = readBundleResponse(response);
+
+ assertThat(results.hasEntry(), is(false));
+ }
+
+ @Test
+ public void getTaskHistoryById_shouldReturn404IfPractitionerIdIsWrong() throws Exception {
+ MockHttpServletResponse response = get("/Task/" + WRONG_TASK_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isNotFound());
+ }
+
+ private MockHttpServletResponse getTaskHistoryByIdRequest() throws IOException, ServletException {
+ return get("/Task/" + TASK_UUID + "/_history").accept(FhirMediaTypes.JSON).go();
+ }
+
+ @Test
+ public void createTask_shouldCreateNewTask() throws Exception {
+ String jsonTask;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_TASK_PATH)) {
+ jsonTask = IOUtils.toString(is);
+ }
+
+ when(service.create(any(Task.class))).thenReturn(task);
+
+ MockHttpServletResponse response = post("/Task").jsonContent(jsonTask).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isCreated());
+ }
+
+ @Test
+ public void updateTask_shouldUpdateExistingTask() throws Exception {
+ String jsonTask;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_TASK_PATH)) {
+ jsonTask = IOUtils.toString(is);
+ }
+
+ when(service.update(anyString(), any(Task.class))).thenReturn(task);
+
+ MockHttpServletResponse response = put("/Task/" + TASK_UUID).jsonContent(jsonTask).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isOk());
+ }
+
+ @Test
+ public void updateTask_shouldErrorForIdMismatch() throws Exception {
+ String jsonTask;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_TASK_WRONG_ID_PATH)) {
+ jsonTask = IOUtils.toString(is);
+ }
+
+ MockHttpServletResponse response = put("/Task/" + TASK_UUID).jsonContent(jsonTask).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isBadRequest());
+ assertThat(response.getContentAsString(),
+ containsStringIgnoringCase("body must contain an ID element which matches the request URL"));
+ }
+
+ @Test
+ public void updateTask_shouldErrorForNoId() throws Exception {
+ String jsonTask;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_TASK_NO_ID_PATH)) {
+ jsonTask = IOUtils.toString(is);
+ }
+
+ MockHttpServletResponse response = put("/Task/" + TASK_UUID).jsonContent(jsonTask).accept(FhirMediaTypes.JSON).go();
+
+ MatcherAssert.assertThat(response, isBadRequest());
+ assertThat(response.getContentAsString(), containsStringIgnoringCase("body must contain an ID element for update"));
+ }
+
+ @Test
+ public void updateTask_shouldErrorForNonexistentTask() throws Exception {
+ String jsonTask;
+ try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_TASK_WRONG_ID_PATH)) {
+ jsonTask = IOUtils.toString(is);
+ }
+
+ when(service.update(eq(WRONG_TASK_UUID), any(Task.class)))
+ .thenThrow(new MethodNotAllowedException("Can't find Task"));
+
+ MockHttpServletResponse response = put("/Task/" + WRONG_TASK_UUID).jsonContent(jsonTask).accept(FhirMediaTypes.JSON)
+ .go();
+
+ MatcherAssert.assertThat(response, isMethodNotAllowed());
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/filter/ForwardingFilterTest.java b/omod/src/test/java/org/openmrs/module/fhir2/web/filter/ForwardingFilterTest.java
new file mode 100644
index 000000000..7211b6fd7
--- /dev/null
+++ b/omod/src/test/java/org/openmrs/module/fhir2/web/filter/ForwardingFilterTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.web.filter;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockFilterChain;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class ForwardingFilterTest {
+
+ private ForwardingFilter filter;
+
+ @Before
+ public void setup() {
+ filter = new ForwardingFilter();
+ }
+
+ @Test
+ public void shouldRedirectForR4() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ request.setRequestURI("/ws/fhir2/R4/Person");
+
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ filter.doFilter(request, response, new MockFilterChain());
+
+ assertThat(response.getForwardedUrl(), notNullValue());
+ assertThat(response.getForwardedUrl(), equalTo("/ms/fhir2Servlet/Person"));
+ }
+
+ @Test
+ public void shouldRedirectForR3() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ request.setRequestURI("/ws/fhir2/R3/Person");
+
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ filter.doFilter(request, response, new MockFilterChain());
+
+ assertThat(response.getForwardedUrl(), notNullValue());
+ assertThat(response.getForwardedUrl(), equalTo("/ms/fhir2R3Servlet/Person"));
+ }
+
+ @Test
+ public void shouldReturn404WhenUsedWithoutVersion() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ request.setRequestURI("/ws/fhir2/Person");
+
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ filter.doFilter(request, response, new MockFilterChain());
+
+ assertThat(response.getStatus(), equalTo(404));
+ }
+
+ @Test
+ public void shouldReturn404WhenUsedWithInvalidVersion() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ request.setRequestURI("/ws/fhir2/R27/Person");
+
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ filter.doFilter(request, response, new MockFilterChain());
+
+ assertThat(response.getStatus(), equalTo(404));
+ }
+
+ @Test
+ public void shouldReturn404WhenUsedWithEmptyVersion() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ request.setRequestURI("/ws/fhir2//Person");
+
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ filter.doFilter(request, response, new MockFilterChain());
+
+ assertThat(response.getStatus(), equalTo(404));
+ }
+
+ @Test
+ public void shouldReturn404WhenUsedWithoutPath() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ request.setRequestURI("/ws/fhir2");
+
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ filter.doFilter(request, response, new MockFilterChain());
+
+ assertThat(response.getStatus(), equalTo(404));
+ }
+
+ @Test
+ public void shouldReturn404WhenUsedWithoutPathAndUrlEndsWithSlash() throws Exception {
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ request.setRequestURI("/ws/fhir2/");
+
+ MockHttpServletResponse response = new MockHttpServletResponse();
+
+ filter.doFilter(request, response, new MockFilterChain());
+
+ assertThat(response.getStatus(), equalTo(404));
+ }
+}
diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategyTest.java b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategyTest.java
index 4ba644ec8..f45d80fab 100644
--- a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategyTest.java
+++ b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/OpenmrsFhirAddressStrategyTest.java
@@ -42,7 +42,7 @@ public void setup() {
}
@Test
- public void should_determineServerBase() {
+ public void should_determineServerBaseR3() {
ServletContext servletContext = Mockito.mock(ServletContext.class);
HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
@@ -50,9 +50,28 @@ public void should_determineServerBase() {
when(httpServletRequest.getServerPort()).thenReturn(8080);
when(httpServletRequest.getContextPath()).thenReturn("/openmrs");
when(httpServletRequest.getServerName()).thenReturn("localhost");
+ when(httpServletRequest.getRequestURI()).thenReturn("/openmrs/ws/fhir2/R3/Person");
String serverBase = fhirAddressStrategy.determineServerBase(servletContext, httpServletRequest);
+
+ assertThat(serverBase, notNullValue());
+ assertThat(serverBase, equalTo(FHIR_SERVER_BASE_URL + "R3"));
+ }
+
+ @Test
+ public void should_determineServerBaseR4() {
+ ServletContext servletContext = Mockito.mock(ServletContext.class);
+ HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
+
+ when(httpServletRequest.getScheme()).thenReturn("https");
+ when(httpServletRequest.getServerPort()).thenReturn(8080);
+ when(httpServletRequest.getContextPath()).thenReturn("/openmrs");
+ when(httpServletRequest.getServerName()).thenReturn("localhost");
+ when(httpServletRequest.getRequestURI()).thenReturn("/openmrs/ws/fhir2/R4/Person");
+
+ String serverBase = fhirAddressStrategy.determineServerBase(servletContext, httpServletRequest);
+
assertThat(serverBase, notNullValue());
- assertThat(serverBase, equalTo(FHIR_SERVER_BASE_URL));
+ assertThat(serverBase, equalTo(FHIR_SERVER_BASE_URL + "R4"));
}
}
diff --git a/pom.xml b/pom.xml
index 237d72e91..7f65fda0c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -188,11 +188,21 @@
hapi-fhir-server
${hapifhirVersion}
+
+ ca.uhn.hapi.fhir
+ hapi-fhir-structures-dstu3
+ ${hapifhirVersion}
+
ca.uhn.hapi.fhir
hapi-fhir-structures-r4
${hapifhirVersion}
+
+ ca.uhn.hapi.fhir
+ hapi-fhir-converter
+ ${hapifhirVersion}
+
org.projectlombok
lombok
@@ -623,6 +633,6 @@
1.18.10
2.0.5
2.0.5
- 4.2.0
+ 5.0.0