From 7d4bb8b9cb02f35876c7f8f4b6a4accce642d023 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Fri, 15 Sep 2017 06:18:26 -0500 Subject: [PATCH] Add tests --- .../fhir/jpa/dao/dstu2/BaseJpaDstu2Test.java | 6 ++ .../jpa/dao/dstu2/FhirSystemDaoDstu2Test.java | 37 ++++++++++ .../fhir/jpa/dao/dstu3/BaseJpaDstu3Test.java | 2 + .../jpa/dao/dstu3/FhirSystemDaoDstu3Test.java | 37 ++++++++++ .../ca/uhn/fhir/jpa/dao/r4/BaseJpaR4Test.java | 68 ++++++++++--------- .../fhir/jpa/dao/r4/FhirSystemDaoR4Test.java | 44 ++++++++++-- 6 files changed, 158 insertions(+), 36 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/BaseJpaDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/BaseJpaDstu2Test.java index f1155c89eb4..4eff5ede810 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/BaseJpaDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu2/BaseJpaDstu2Test.java @@ -3,6 +3,8 @@ package ca.uhn.fhir.jpa.dao.dstu2; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.config.TestDstu2Config; import ca.uhn.fhir.jpa.dao.*; +import ca.uhn.fhir.jpa.dao.data.IResourceIndexedSearchParamStringDao; +import ca.uhn.fhir.jpa.dao.data.IResourceTableDao; import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString; import ca.uhn.fhir.jpa.entity.ResourceTable; import ca.uhn.fhir.jpa.provider.JpaSystemProviderDstu2; @@ -134,6 +136,10 @@ protected IFhirResourceDao myMedicationAdministrationD @Qualifier("mySubstanceDaoDstu2") protected IFhirResourceDao mySubstanceDao; @Autowired + protected IResourceIndexedSearchParamStringDao myResourceIndexedSearchParamStringDao; + @Autowired + protected IResourceTableDao myResourceTableDao; + @Autowired @Qualifier("mySystemDaoDstu2") protected IFhirSystemDao mySystemDao; @Autowired 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 23beb7e0fbf..b8121aaa6e5 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 @@ -60,6 +60,43 @@ public class FhirSystemDaoDstu2Test extends BaseJpaDstu2SystemTest { assertEquals(18, resp.getEntry().size()); } + @Test + public void testTransactionWhichFailsPersistsNothing() { + + // Run a transaction which points to that practitioner + // in a field that isn't allowed to refer to a practitioner + Bundle input = new Bundle(); + input.setType(BundleTypeEnum.TRANSACTION); + + Patient pt = new Patient(); + pt.setId("PT"); + pt.setActive(true); + pt.addName().addFamily("FAMILY"); + input.addEntry() + .setResource(pt) + .getRequest().setMethod(HTTPVerbEnum.PUT).setUrl("Patient/PT"); + + Observation obs = new Observation(); + obs.setId("OBS"); + obs.getCode().addCoding().setSystem("foo").setCode("bar"); + obs.addPerformer().setReference("Practicioner/AAAAA"); + input.addEntry() + .setResource(obs) + .getRequest().setMethod(HTTPVerbEnum.PUT).setUrl("Observation/OBS"); + + try { + mySystemDao.transaction(mySrd, input); + fail(); + } catch (UnprocessableEntityException e) { + assertThat(e.getMessage(), containsString("Resource type 'Practicioner' is not valid for this path")); + } + + assertThat(myResourceTableDao.findAll(), empty()); + assertThat(myResourceIndexedSearchParamStringDao.findAll(), empty()); + + } + + /** * Per a message on the mailing list */ diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/BaseJpaDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/BaseJpaDstu3Test.java index 322c645f91c..7d44e1883ea 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/BaseJpaDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/BaseJpaDstu3Test.java @@ -167,6 +167,8 @@ public abstract class BaseJpaDstu3Test extends BaseJpaTest { @Qualifier("myResourceProvidersDstu3") protected Object myResourceProviders; @Autowired + protected IResourceIndexedSearchParamStringDao myResourceIndexedSearchParamStringDao; + @Autowired protected IResourceTableDao myResourceTableDao; @Autowired protected IResourceTagDao myResourceTagDao; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirSystemDaoDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirSystemDaoDstu3Test.java index f53a8b0868b..681cbbf9e4c 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirSystemDaoDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirSystemDaoDstu3Test.java @@ -62,6 +62,43 @@ public class FhirSystemDaoDstu3Test extends BaseJpaDstu3SystemTest { myDaoConfig.setReuseCachedSearchResultsForMillis(null); } + @Test + public void testTransactionWhichFailsPersistsNothing() { + + // Run a transaction which points to that practitioner + // in a field that isn't allowed to refer to a practitioner + Bundle input = new Bundle(); + input.setType(BundleType.TRANSACTION); + + Patient pt = new Patient(); + pt.setId("PT"); + pt.setActive(true); + pt.addName().setFamily("FAMILY"); + input.addEntry() + .setResource(pt) + .getRequest().setMethod(HTTPVerb.PUT).setUrl("Patient/PT"); + + Observation obs = new Observation(); + obs.setId("OBS"); + obs.getCode().addCoding().setSystem("foo").setCode("bar"); + obs.addPerformer().setReference("Practicioner/AAAAA"); + input.addEntry() + .setResource(obs) + .getRequest().setMethod(HTTPVerb.PUT).setUrl("Observation/OBS"); + + try { + mySystemDao.transaction(mySrd, input); + fail(); + } catch (UnprocessableEntityException e) { + assertThat(e.getMessage(), containsString("Resource type 'Practicioner' is not valid for this path")); + } + + assertThat(myResourceTableDao.findAll(), empty()); + assertThat(myResourceIndexedSearchParamStringDao.findAll(), empty()); + + } + + private Bundle createInputTransactionWithPlaceholderIdInMatchUrl(HTTPVerb theVerb) { Patient pat = new Patient(); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/BaseJpaR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/BaseJpaR4Test.java index 56f2307a016..8ad1f1bc76b 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/BaseJpaR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/BaseJpaR4Test.java @@ -1,21 +1,35 @@ package ca.uhn.fhir.jpa.dao.r4; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; - -import javax.persistence.EntityManager; - +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.jpa.config.TestR4Config; +import ca.uhn.fhir.jpa.dao.*; +import ca.uhn.fhir.jpa.dao.data.*; +import ca.uhn.fhir.jpa.dao.dstu2.FhirResourceDaoDstu2SearchNoFtTest; +import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString; +import ca.uhn.fhir.jpa.entity.ResourceTable; +import ca.uhn.fhir.jpa.provider.r4.JpaSystemProviderR4; +import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider; +import ca.uhn.fhir.jpa.search.ISearchCoordinatorSvc; +import ca.uhn.fhir.jpa.search.IStaleSearchDeletingSvc; +import ca.uhn.fhir.jpa.sp.ISearchParamPresenceSvc; +import ca.uhn.fhir.jpa.term.IHapiTerminologySvc; +import ca.uhn.fhir.jpa.validation.JpaValidationSupportChainR4; +import ca.uhn.fhir.parser.IParser; +import ca.uhn.fhir.parser.StrictErrorHandler; +import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.api.EncodingEnum; +import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; +import ca.uhn.fhir.util.TestUtil; +import ca.uhn.fhir.util.UrlUtil; import org.apache.commons.io.IOUtils; import org.hibernate.search.jpa.FullTextEntityManager; import org.hibernate.search.jpa.Search; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.hapi.ctx.IValidationSupport; import org.hl7.fhir.r4.model.*; -import org.junit.*; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -27,33 +41,23 @@ import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionTemplate; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.jpa.config.TestR4Config; -import ca.uhn.fhir.jpa.dao.*; -import ca.uhn.fhir.jpa.dao.data.*; -import ca.uhn.fhir.jpa.dao.dstu2.FhirResourceDaoDstu2SearchNoFtTest; -import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString; -import ca.uhn.fhir.jpa.entity.ResourceTable; -import ca.uhn.fhir.jpa.provider.r4.JpaSystemProviderR4; -import ca.uhn.fhir.jpa.search.*; -import ca.uhn.fhir.jpa.sp.ISearchParamPresenceSvc; -import ca.uhn.fhir.jpa.term.IHapiTerminologySvc; -import ca.uhn.fhir.jpa.validation.JpaValidationSupportChainR4; -import ca.uhn.fhir.parser.IParser; -import ca.uhn.fhir.parser.StrictErrorHandler; -import ca.uhn.fhir.rest.api.Constants; -import ca.uhn.fhir.rest.api.EncodingEnum; -import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; -import ca.uhn.fhir.util.TestUtil; -import ca.uhn.fhir.util.UrlUtil; +import javax.persistence.EntityManager; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes= {TestR4Config.class}) +@ContextConfiguration(classes = {TestR4Config.class}) public abstract class BaseJpaR4Test extends BaseJpaTest { private static JpaValidationSupportChainR4 ourJpaValidationSupportChainR4; private static IFhirResourceDaoValueSet ourValueSetDao; + @Autowired + protected IResourceIndexedSearchParamStringDao myResourceIndexedSearchParamStringDao; @Autowired protected IResourceIndexedCompositeStringUniqueDao myResourceIndexedCompositeStringUniqueDao; @Autowired @@ -112,8 +116,6 @@ public abstract class BaseJpaR4Test extends BaseJpaTest { protected IFhirResourceDao myImmunizationRecommendationDao; protected IServerInterceptor myInterceptor; @Autowired - private JpaValidationSupportChainR4 myJpaValidationSupportChainR4; - @Autowired @Qualifier("myLocationDaoR4") protected IFhirResourceDao myLocationDao; @Autowired @@ -214,6 +216,8 @@ public abstract class BaseJpaR4Test extends BaseJpaTest { @Autowired @Qualifier("myValueSetDaoR4") protected IFhirResourceDaoValueSet myValueSetDao; + @Autowired + private JpaValidationSupportChainR4 myJpaValidationSupportChainR4; @After() public void afterCleanupDao() { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java index f0f39e89256..7d8354f5a08 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java @@ -1665,13 +1665,13 @@ public class FhirSystemDaoR4Test extends BaseJpaR4SystemTest { //@formatter:off /* * Transaction Order, per the spec: - * + * * Process any DELETE interactions * Process any POST interactions * Process any PUT interactions * Process any GET interactions - * - * This test creates a transaction bundle that includes + * + * This test creates a transaction bundle that includes * these four operations in the reverse order and verifies * that they are invoked correctly. */ @@ -2110,6 +2110,42 @@ public class FhirSystemDaoR4Test extends BaseJpaR4SystemTest { } } + @Test + public void testTransactionWhichFailsPersistsNothing() { + + // Run a transaction which points to that practitioner + // in a field that isn't allowed to refer to a practitioner + Bundle input = new Bundle(); + input.setType(BundleType.TRANSACTION); + + Patient pt = new Patient(); + pt.setId("PT"); + pt.setActive(true); + pt.addName().setFamily("FAMILY"); + input.addEntry() + .setResource(pt) + .getRequest().setMethod(HTTPVerb.PUT).setUrl("Patient/PT"); + + Observation obs = new Observation(); + obs.setId("OBS"); + obs.getCode().addCoding().setSystem("foo").setCode("bar"); + obs.addPerformer().setReference("Practicioner/AAAAA"); + input.addEntry() + .setResource(obs) + .getRequest().setMethod(HTTPVerb.PUT).setUrl("Observation/OBS"); + + try { + mySystemDao.transaction(mySrd, input); + fail(); + } catch (UnprocessableEntityException e) { + assertThat(e.getMessage(), containsString("Resource type 'Practicioner' is not valid for this path")); + } + + assertThat(myResourceTableDao.findAll(), empty()); + assertThat(myResourceIndexedSearchParamStringDao.findAll(), empty()); + + } + /** * Format changed, source isn't valid */ @@ -2455,7 +2491,7 @@ public class FhirSystemDaoR4Test extends BaseJpaR4SystemTest { IdType medOrderId1 = new IdType(outcome.getEntry().get(1).getResponse().getLocation()); /* - * Again! + * Again! */ bundle = new Bundle();