From 137d39e80f360393445c27d57ee7791c847c6fe9 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 4 Jan 2019 13:53:24 -0500 Subject: [PATCH] Another test fix --- .../fhir/jpa/dao/BaseHapiFhirResourceDao.java | 12 ++++--- .../ca/uhn/fhir/jpa/dao/DaoMethodOutcome.java | 7 ++++ .../provider/r4/ResourceProviderR4Test.java | 33 +++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java index dce0a201659..8fc9a511c4d 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java @@ -380,7 +380,8 @@ public abstract class BaseHapiFhirResourceDao extends B } else if (match.size() == 1) { Long pid = match.iterator().next(); entity = myEntityManager.find(ResourceTable.class, pid); - return toMethodOutcome(entity, null).setCreated(false); + IBaseResource resource = toResource(entity, false); + return toMethodOutcome(entity, resource).setCreated(false); } } @@ -1141,15 +1142,15 @@ public abstract class BaseHapiFhirResourceDao extends B return retVal; } - private DaoMethodOutcome toMethodOutcome(@Nonnull final ResourceTable theEntity, @Nullable IBaseResource theResource) { + private DaoMethodOutcome toMethodOutcome(@Nonnull final ResourceTable theEntity, @Nonnull IBaseResource theResource) { DaoMethodOutcome outcome = new DaoMethodOutcome(); IIdType id = null; - if (theResource != null) { + if (theResource.getIdElement().getValue() != null) { id = theResource.getIdElement(); } if (id == null) { - id = ((BaseHasResource) theEntity).getIdDt(); + id = theEntity.getIdDt(); if (getContext().getVersion().getVersion().isRi()) { id = getContext().getVersion().newIdType().setValue(id.getValue()); } @@ -1294,7 +1295,8 @@ public abstract class BaseHapiFhirResourceDao extends B * directly. So we just bail now. */ if (!thePerformIndexing) { - DaoMethodOutcome outcome = toMethodOutcome(entity, null).setCreated(false); + theResource.setId(entity.getIdDt().getValue()); + DaoMethodOutcome outcome = toMethodOutcome(entity, theResource).setCreated(false); outcome.setPreviousResource(oldResource); return outcome; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoMethodOutcome.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoMethodOutcome.java index 5a2b398e52e..a2defed681e 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoMethodOutcome.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoMethodOutcome.java @@ -29,6 +29,13 @@ public class DaoMethodOutcome extends MethodOutcome { private ResourceTable myEntity; private IBaseResource myPreviousResource; + /** + * Constructor + */ + public DaoMethodOutcome() { + super(); + } + public ResourceTable getEntity() { return myEntity; } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java index a887294a80c..ec5c4fcd125 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java @@ -37,6 +37,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; +import ca.uhn.fhir.rest.api.PreferReturnEnum; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; @@ -403,6 +404,38 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test { } + @Test + public void testCreateConditionalWithPreferRepresentation() { + Patient p = new Patient(); + p.setActive(false); + p.addIdentifier().setSystem("foo").setValue("bar"); + IIdType id = ourClient.create().resource(p).execute().getId(); + + p = new Patient(); + p.setId(id); + p.setActive(true); + p.addIdentifier().setSystem("foo").setValue("bar"); + ourClient.update().resource(p).execute().getId(); + + // Now conditional create + p = new Patient(); + p.setActive(true); + p.setBirthDateElement(new DateType("2011-01-01")); + p.addIdentifier().setSystem("foo").setValue("bar"); + MethodOutcome outcome = ourClient + .create() + .resource(p) + .conditionalByUrl("Patient?identifier=foo|bar") + .prefer(PreferReturnEnum.REPRESENTATION) + .execute(); + + assertEquals(id.getIdPart(), outcome.getId().getIdPart()); + assertEquals("2", outcome.getId().getVersionIdPart()); + p = (Patient) outcome.getResource(); + assertNull(p.getBirthDate()); + } + + /** * See #438 */