Skip to content

Commit

Permalink
FM2-605: Add Support for FHIR's ETag spec on Condition Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Jul 25, 2023
1 parent a48adb1 commit 496d28c
Show file tree
Hide file tree
Showing 6 changed files with 126 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 @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
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 @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 496d28c

Please sign in to comment.