Skip to content

Commit

Permalink
FM2-590: Add support for Json Merge Patch operations - Medication Res…
Browse files Browse the repository at this point in the history
…ource (#488)
  • Loading branch information
mherman22 authored Jun 30, 2023
1 parent f33535f commit 448a00e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"op": "replace",
"path": "/status",
"value": "inactive"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": "inactive"
}

0 comments on commit 448a00e

Please sign in to comment.