Skip to content

Commit

Permalink
FM2-605: Add Support for FHIR's ETag spec on Encounter resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Jul 25, 2023
1 parent 22ef36c commit 50e1b5b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static org.apache.commons.lang3.Validate.notNull;
import static org.openmrs.module.fhir2.api.translators.impl.FhirTranslatorUtils.getLastUpdated;
import static org.openmrs.module.fhir2.api.translators.impl.FhirTranslatorUtils.getVersionId;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -83,6 +84,7 @@ public Encounter toFhirResource(@Nonnull org.openmrs.Encounter openmrsEncounter)

encounter.getMeta().addTag(FhirConstants.OPENMRS_FHIR_EXT_ENCOUNTER_TAG, "encounter", "Encounter");
encounter.getMeta().setLastUpdated(getLastUpdated(openmrsEncounter));
encounter.getMeta().setVersionId(getVersionId(openmrsEncounter));
encounter.setClass_(mapLocationToClass(openmrsEncounter.getLocation()));

return encounter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,16 @@ public void toFhirResource_shouldTranslateToLastUpdatedDate() {
assertThat(result.getMeta().getLastUpdated(), DateMatchers.sameDay(new Date()));
}

@Test
public void toFhirResource_shouldTranslateToVersionId() {
omrsEncounter.setDateChanged(new Date());

Encounter result = encounterTranslator.toFhirResource(omrsEncounter);

assertThat(result, notNullValue());
assertThat(result.getMeta().getVersionId(), notNullValue());
}

@Test
public void toFhirResource_shouldTranslateToEncounterClassFhirType() {
when(encounterClassMap.getFhirClass(LOCATION_UUID)).thenReturn(TEST_FHIR_CLASS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import lombok.Getter;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Encounter;
import org.hl7.fhir.r4.model.Location;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.hl7.fhir.r4.model.ResourceType;
import org.junit.Before;
Expand Down Expand Up @@ -1049,4 +1050,40 @@ private Set<ResourceType> getEncounterWithMedicationRequestsValidResourceTypes()

return validTypes;
}

@Test
public void shouldReturnAnEtagHeaderWhenRetrievingAnExistingEncounter() throws Exception {
MockHttpServletResponse response = get("/Encounter/" + ENCOUNTER_UUID).accept(FhirMediaTypes.JSON).go();

assertThat(response, isOk());
assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString()));

assertThat(response.getHeader("etag"), notNullValue());
assertThat(response.getHeader("etag"), startsWith("W/"));

assertThat(response.getContentAsString(), notNullValue());

Encounter encounter = readResponse(response);

assertThat(encounter, notNullValue());
assertThat(encounter.getMeta().getVersionId(), notNullValue());
assertThat(encounter, validResource());
}

@Test
public void shouldReturnNotModifiedWhenRetrievingAnExistingEncounterWithAnEtag() throws Exception {
MockHttpServletResponse response = get("/Encounter/" + ENCOUNTER_UUID).accept(FhirMediaTypes.JSON).go();

assertThat(response, isOk());
assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString()));
assertThat(response.getContentAsString(), notNullValue());
assertThat(response.getHeader("etag"), notNullValue());

String etagValue = response.getHeader("etag");

response = get("/Encounter/" + ENCOUNTER_UUID).accept(FhirMediaTypes.JSON).ifNoneMatchHeader(etagValue).go();

assertThat(response, isOk());
assertThat(response, statusEquals(HttpStatus.NOT_MODIFIED));
}
}

0 comments on commit 50e1b5b

Please sign in to comment.