diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java index 9298c20f6..18e79e85a 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java +++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java @@ -21,12 +21,15 @@ import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.IncludeParam; import ca.uhn.fhir.rest.annotation.OptionalParam; +import ca.uhn.fhir.rest.annotation.Patch; 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.Update; import ca.uhn.fhir.rest.api.MethodOutcome; +import ca.uhn.fhir.rest.api.PatchTypeEnum; import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.server.IResourceProvider; @@ -87,6 +90,18 @@ public MethodOutcome updateMedication(@IdParam IdType id, @ResourceParam Medicat return FhirProviderUtils.buildUpdate(fhirMedicationService.update(id.getIdPart(), medication)); } + @Patch + public MethodOutcome patchMedication(@IdParam IdType id, PatchTypeEnum patchType, @ResourceParam String body, + RequestDetails requestDetails) { + if (id == null || id.getIdPart() == null) { + throw new InvalidRequestException("id must be specified to update Medication resource"); + } + + Medication medication = fhirMedicationService.patch(id.getIdPart(), patchType, body, requestDetails); + + return FhirProviderUtils.buildPatch(medication); + } + @Delete @SuppressWarnings("unused") public OperationOutcome deleteMedication(@IdParam @Nonnull IdType id) { diff --git a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderIntegrationTest.java b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderIntegrationTest.java index 7178a0802..e639b30a2 100644 --- a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderIntegrationTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProviderIntegrationTest.java @@ -50,6 +50,10 @@ public class MedicationFhirResourceProviderIntegrationTest extends BaseFhirR4Int private static final String MEDICATION_DATA_XML = "org/openmrs/module/fhir2/api/dao/impl/FhirMedicationDaoImplTest_initial_data.xml"; + private static final String JSON_MERGE_PATCH_MEDICATION_PATH = "org/openmrs/module/fhir2/providers/Medication_patch.json"; + + private static final String JSON_PATCH_MEDICATION_PATH = "org/openmrs/module/fhir2/providers/Medication_json_patch.json"; + private static final String JSON_CREATE_MEDICATION_DOCUMENT = "org/openmrs/module/fhir2/providers/MedicationWebTest_create.json"; private static final String XML_CREATE_MEDICATION_DOCUMENT = "org/openmrs/module/fhir2/providers/MedicationWebTest_create.xml"; @@ -392,6 +396,54 @@ public void shouldReturnNotFoundWhenUpdatingNonExistentMedicationAsXML() throws assertThat(operationOutcome.hasIssue(), is(true)); } + @Test + public void shouldPatchExistingMedicationUsingJsonMergePatch() throws Exception { + String jsonMedicationPatch; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_MERGE_PATCH_MEDICATION_PATH)) { + Objects.requireNonNull(is); + jsonMedicationPatch = inputStreamToString(is, UTF_8); + } + + MockHttpServletResponse response = patch("/Medication/" + MEDICATION_UUID).jsonMergePatch(jsonMedicationPatch) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Medication medication = readResponse(response); + + assertThat(medication, notNullValue()); + assertThat(medication.getIdElement().getIdPart(), equalTo(MEDICATION_UUID)); + assertThat(medication, validResource()); + + assertThat(medication.getStatus(), is(Medication.MedicationStatus.ACTIVE)); + } + + @Test + public void shouldPatchExistingMedicationUsingJsonPatch() throws Exception { + String jsonMedicationPatch; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_PATCH_MEDICATION_PATH)) { + Objects.requireNonNull(is); + jsonMedicationPatch = inputStreamToString(is, UTF_8); + } + + MockHttpServletResponse response = patch("/Medication/" + MEDICATION_UUID).jsonPatch(jsonMedicationPatch) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString())); + assertThat(response.getContentAsString(), notNullValue()); + + Medication medication = readResponse(response); + + assertThat(medication, notNullValue()); + assertThat(medication.getIdElement().getIdPart(), equalTo(MEDICATION_UUID)); + assertThat(medication, validResource()); + + assertThat(medication.getStatus(), is(Medication.MedicationStatus.ACTIVE)); + } + @Test public void shouldDeleteExistingMedication() throws Exception { MockHttpServletResponse response = delete("/Medication/" + MEDICATION_UUID).accept(FhirMediaTypes.JSON).go(); diff --git a/test-data/src/main/resources/org/openmrs/module/fhir2/providers/Medication_json_patch.json b/test-data/src/main/resources/org/openmrs/module/fhir2/providers/Medication_json_patch.json new file mode 100644 index 000000000..c0197f5a3 --- /dev/null +++ b/test-data/src/main/resources/org/openmrs/module/fhir2/providers/Medication_json_patch.json @@ -0,0 +1,7 @@ +[ + { + "op": "replace", + "path": "/status", + "value": "inactive" + } +] diff --git a/test-data/src/main/resources/org/openmrs/module/fhir2/providers/Medication_patch.json b/test-data/src/main/resources/org/openmrs/module/fhir2/providers/Medication_patch.json new file mode 100644 index 000000000..3eda9d21e --- /dev/null +++ b/test-data/src/main/resources/org/openmrs/module/fhir2/providers/Medication_patch.json @@ -0,0 +1,3 @@ +{ + "status": "inactive" +}