From b18bb033bf5b33ffcdabb899c77df060926a7087 Mon Sep 17 00:00:00 2001 From: darktyco Date: Tue, 6 Mar 2018 12:03:34 -0500 Subject: [PATCH] Fixes issue #878 where resources in a bundle are updated even when no data changes --- .../ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java | 2 +- .../jpa/dao/dstu2/FhirSystemDaoDstu2Test.java | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java index 96007600126..8c06a482c5f 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java @@ -129,7 +129,7 @@ public abstract class BaseHapiFhirDao implements IDao { HashSet excludeElementsInEncoded = new HashSet(); excludeElementsInEncoded.add("id"); - excludeElementsInEncoded.add("meta"); + excludeElementsInEncoded.add("*.meta"); EXCLUDE_ELEMENTS_IN_ENCODED = Collections.unmodifiableSet(excludeElementsInEncoded); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirSystemDaoDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirSystemDaoDstu2Test.java index cd1f4652783..b894e83a823 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirSystemDaoDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/FhirSystemDaoDstu2Test.java @@ -1193,6 +1193,62 @@ public class FhirSystemDaoDstu2Test extends BaseJpaDstu2SystemTest { } + @Test + public void testTransactionUpdateResourceNewVersionCreatedWhenDataChanges() { + + Bundle request = new Bundle(); + String patientId = "Patient/IShouldUpdate"; + Patient p = new Patient(); + p.addName().addFamily("Hello"); + p.setId(patientId); + request.addEntry().setResource(p).getRequest().setMethod(HTTPVerbEnum.PUT).setUrl(patientId); + + Bundle initialBundleResponse = mySystemDao.transaction(mySrd, request); + assertEquals(1, initialBundleResponse.getEntry().size()); + ourLog.info(myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(initialBundleResponse)); + + Entry initialBundleResponseEntry = initialBundleResponse.getEntry().get(0); + assertEquals("201 Created", initialBundleResponseEntry.getResponse().getStatus()); + assertThat(initialBundleResponseEntry.getResponse().getEtag(), is(equalTo("1"))); + + p.addName().addFamily("AnotherName"); + + Bundle secondBundleResponse = mySystemDao.transaction(mySrd, request); + assertEquals(1, secondBundleResponse.getEntry().size()); + ourLog.info(myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(secondBundleResponse)); + + Entry secondBundleResponseEntry = secondBundleResponse.getEntry().get(0); + assertEquals("200 OK", secondBundleResponseEntry.getResponse().getStatus()); + assertThat(secondBundleResponseEntry.getResponse().getEtag(), is(equalTo("2"))); + } + + @Test + public void testTransactionUpdateResourceNewVersionNotCreatedWhenDataNotChanged() { + + Bundle request = new Bundle(); + String patientId = "Patient/IShouldNotUpdate"; + Patient p = new Patient(); + p.addName().addFamily("Hello"); + p.setId(patientId); + request.addEntry().setResource(p).getRequest().setMethod(HTTPVerbEnum.PUT).setUrl(patientId); + + Bundle initialBundleResponse = mySystemDao.transaction(mySrd, request); + assertEquals(1, initialBundleResponse.getEntry().size()); + ourLog.info(myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(initialBundleResponse)); + + Entry initialBundleResponseEntry = initialBundleResponse.getEntry().get(0); + assertEquals("201 Created", initialBundleResponseEntry.getResponse().getStatus()); + assertThat(initialBundleResponseEntry.getResponse().getEtag(), is(equalTo("1"))); + + Bundle secondBundleResponse = mySystemDao.transaction(mySrd, request); + assertEquals(1, secondBundleResponse.getEntry().size()); + ourLog.info(myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(secondBundleResponse)); + + Entry secondBundleResponseEntry = secondBundleResponse.getEntry().get(0); + assertEquals("200 OK", secondBundleResponseEntry.getResponse().getStatus()); + assertThat(secondBundleResponseEntry.getResponse().getEtag(), is(equalTo("1"))); + } + @Test public void testTransactionUpdateMatchUrlWithTwoMatch() { String methodName = "testTransactionUpdateMatchUrlWithTwoMatch";