Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-590: Add support for Json Merge Patch operations - Medication Resource #488

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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");

Check warning on line 97 in api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java#L97

Added line #L97 was not covered by tests
}

Medication medication = fhirMedicationService.patch(id.getIdPart(), patchType, body, requestDetails);

Check warning on line 100 in api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java#L100

Added line #L100 was not covered by tests

return FhirProviderUtils.buildPatch(medication);

Check warning on line 102 in api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/providers/r4/MedicationFhirResourceProvider.java#L102

Added line #L102 was not covered by tests
}

@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"
}