diff --git a/api-2.2/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2.java b/api-2.2/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2.java index 439cf39e9..283f300cb 100644 --- a/api-2.2/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2.java +++ b/api-2.2/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2.java @@ -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; @@ -88,6 +89,7 @@ public org.hl7.fhir.r4.model.Condition toFhirResource(@Nonnull Condition conditi fhirCondition.setRecordedDate(condition.getDateCreated()); fhirCondition.getMeta().setLastUpdated(getLastUpdated(condition)); + fhirCondition.getMeta().setVersionId(getVersionId(condition)); return fhirCondition; } diff --git a/api-2.2/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2Test.java b/api-2.2/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2Test.java index 63a9d0e8f..385c9d939 100644 --- a/api-2.2/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2Test.java +++ b/api-2.2/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl_2_2Test.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.Date; +import org.hamcrest.Matchers; import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Condition; @@ -395,4 +396,26 @@ public void shouldTranslateConditionCreatorToRecorderFhirType() { assertThat(condition.getRecorder(), notNullValue()); assertThat(condition.getRecorder().getReference(), equalTo(PRACTITIONER_REFERENCE)); } + + @Test + public void shouldTranslateOpenMrsDateChangedToLastUpdatedDate() { + org.openmrs.Condition condition = new org.openmrs.Condition(); + condition.setDateChanged(new Date()); + + org.hl7.fhir.r4.model.Condition result = conditionTranslator.toFhirResource(condition); + + assertThat(result, Matchers.notNullValue()); + assertThat(result.getMeta().getLastUpdated(), Matchers.notNullValue()); + } + + @Test + public void shouldTranslateOpenMrsDateChangedToVersionId() { + org.openmrs.Condition condition = new org.openmrs.Condition(); + condition.setDateChanged(new Date()); + + org.hl7.fhir.r4.model.Condition result = conditionTranslator.toFhirResource(condition); + + assertThat(result, Matchers.notNullValue()); + assertThat(result.getMeta().getVersionId(), Matchers.notNullValue()); + } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java index d7128e188..f99585aff 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImpl.java @@ -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; @@ -82,6 +83,7 @@ public org.hl7.fhir.r4.model.Condition toFhirResource(@Nonnull Obs obsCondition) fhirCondition.setRecordedDate(obsCondition.getDateCreated()); fhirCondition.getMeta().setLastUpdated(getLastUpdated(obsCondition)); + fhirCondition.getMeta().setVersionId(getVersionId(obsCondition)); return fhirCondition; } diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java index 038a1574e..e2623c2c0 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/api/translators/impl/ConditionTranslatorImplTest.java @@ -20,6 +20,7 @@ import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import org.exparity.hamcrest.date.DateMatchers; +import org.hamcrest.Matchers; import org.hl7.fhir.r4.model.CodeableConcept; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Condition; @@ -264,4 +265,26 @@ public void shouldTranslateConditionCreatorToRecorderFhirType() { assertThat(condition.getRecorder(), notNullValue()); assertThat(condition.getRecorder().getReference(), equalTo(PRACTITIONER_REFERENCE)); } + + @Test + public void shouldTranslateOpenMrsDateChangedToLastUpdatedDate() { + org.openmrs.Obs obsCondition = new org.openmrs.Obs(); + obsCondition.setDateChanged(new Date()); + + org.hl7.fhir.r4.model.Condition result = conditionTranslator.toFhirResource(obsCondition); + + assertThat(result, Matchers.notNullValue()); + assertThat(result.getMeta().getLastUpdated(), Matchers.notNullValue()); + } + + @Test + public void shouldTranslateOpenMrsDateChangedToVersionId() { + org.openmrs.Obs obsCondition = new org.openmrs.Obs(); + obsCondition.setDateChanged(new Date()); + + org.hl7.fhir.r4.model.Condition result = conditionTranslator.toFhirResource(obsCondition); + + assertThat(result, Matchers.notNullValue()); + assertThat(result.getMeta().getVersionId(), Matchers.notNullValue()); + } } diff --git a/integration-tests-2.2/src/test/java/org/openmrs/module/fhir2/provider/r4/ConditionResourceProviderIntegrationTest.java b/integration-tests-2.2/src/test/java/org/openmrs/module/fhir2/provider/r4/ConditionResourceProviderIntegrationTest.java index 37ac3c054..2a0427045 100644 --- a/integration-tests-2.2/src/test/java/org/openmrs/module/fhir2/provider/r4/ConditionResourceProviderIntegrationTest.java +++ b/integration-tests-2.2/src/test/java/org/openmrs/module/fhir2/provider/r4/ConditionResourceProviderIntegrationTest.java @@ -623,4 +623,40 @@ public void shouldReturnSortedAndFilteredSearchResultsForConditionsAsXML() throw hasResource(hasProperty("onsetDateTimeType", hasProperty("value", equalTo( Date.from(LocalDateTime.of(2020, 3, 5, 19, 0, 0).atZone(ZoneId.systemDefault()).toInstant()))))))); } + + @Test + public void shouldReturnAnEtagHeaderWhenRetrievingAnExistingCondition() throws Exception { + MockHttpServletResponse response = get("/Condition/" + CONDITION_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()); + + Condition condition = readResponse(response); + + assertThat(condition, notNullValue()); + assertThat(condition.getMeta().getVersionId(), notNullValue()); + assertThat(condition, validResource()); + } + + @Test + public void shouldReturnNotModifiedWhenRetrievingAnExistingConditionWithAnEtag() throws Exception { + MockHttpServletResponse response = get("/Condition/" + CONDITION_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("/Condition/" + CONDITION_UUID).accept(FhirMediaTypes.JSON).ifNoneMatchHeader(etagValue).go(); + + assertThat(response, isOk()); + assertThat(response, statusEquals(HttpStatus.NOT_MODIFIED)); + } } diff --git a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderIntegrationTest.java b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderIntegrationTest.java index 068d7d1a9..ad74ae502 100644 --- a/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderIntegrationTest.java +++ b/integration-tests/src/test/java/org/openmrs/module/fhir2/providers/r4/ConditionFhirResourceProviderIntegrationTest.java @@ -28,13 +28,17 @@ import java.sql.Date; import java.time.LocalDateTime; import java.time.ZoneId; +import java.util.Calendar; import java.util.List; import lombok.AccessLevel; import lombok.Getter; +import org.apache.commons.lang3.time.DateUtils; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Condition; +import org.hl7.fhir.r4.model.Enumerations; import org.hl7.fhir.r4.model.OperationOutcome; +import org.hl7.fhir.r4.model.Person; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -462,4 +466,40 @@ public void shouldReturnCountForConditionAsXml() throws Exception { assertThat(result.getType(), equalTo(Bundle.BundleType.SEARCHSET)); assertThat(result, hasProperty("total", equalTo(2))); } + + @Test + public void shouldReturnAnEtagHeaderWhenRetrievingAnExistingCondition() throws Exception { + MockHttpServletResponse response = get("/Condition/" + CONDITION_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()); + + Condition condition = readResponse(response); + + assertThat(condition, notNullValue()); + assertThat(condition.getMeta().getVersionId(), notNullValue()); + assertThat(condition, validResource()); + } + + @Test + public void shouldReturnNotModifiedWhenRetrievingAnExistingConditionWithAnEtag() throws Exception { + MockHttpServletResponse response = get("/Condition/" + CONDITION_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("/Condition/" + CONDITION_UUID).accept(FhirMediaTypes.JSON).ifNoneMatchHeader(etagValue).go(); + + assertThat(response, isOk()); + assertThat(response, statusEquals(HttpStatus.NOT_MODIFIED)); + } }