From 0964710fd3a8aad8fbd7c7092ff1fa7990ebf98b Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 16 Dec 2024 15:06:15 -0500 Subject: [PATCH] Replace assert with Validate (#6558) * Replace assert with Validate * Test cleanup --- .../ca/uhn/fhir/jpa/entity/TermConcept.java | 4 ++++ .../term/TermCodeSystemStorageSvcImpl.java | 7 +++++-- .../fhir/jpa/dao/r4/FhirSystemDaoR4Test.java | 20 +++++++++++++++++++ .../jpa/provider/r4/SystemProviderR4Test.java | 9 ++++----- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermConcept.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermConcept.java index 58f046b86c9..c4d3b5884c4 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermConcept.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TermConcept.java @@ -633,5 +633,9 @@ public class TermConcept implements Serializable { public String toString() { return String.valueOf(myId); } + + public Long getId() { + return myId; + } } } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java index 644acce3c05..57be4b1856a 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java @@ -478,8 +478,11 @@ public class TermCodeSystemStorageSvcImpl implements ITermCodeSystemStorageSvc { Collection conceptsToSave = theCodeSystemVersion.getConcepts(); for (TermConcept next : conceptsToSave) { totalCodeCount += validateConceptForStorage(next, codeSystemToStore, conceptsStack, allConcepts); - assert next.getId() == null; - assert codeSystemToStore.getPid() != null; + Validate.isTrue(next.getPid().getId() == null); + Validate.isTrue(codeSystemToStore.getPid() != null); + + // Make sure to initialize the PK object so that hibernate doesn't choke on creation + next.setId(null); next.setCodeSystemVersion(codeSystemToStore); next.setUpdated(updated); diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java index 0980230f10c..8a669a04670 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirSystemDaoR4Test.java @@ -61,6 +61,7 @@ import org.hl7.fhir.r4.model.Bundle.BundleEntryResponseComponent; import org.hl7.fhir.r4.model.Bundle.BundleType; import org.hl7.fhir.r4.model.Bundle.HTTPVerb; import org.hl7.fhir.r4.model.CanonicalType; +import org.hl7.fhir.r4.model.CodeSystem; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Communication; import org.hl7.fhir.r4.model.Condition; @@ -1358,6 +1359,25 @@ public class FhirSystemDaoR4Test extends BaseJpaR4SystemTest { } } + @Test + public void testTransactionCreateCodeSystem() { + BundleBuilder bb = new BundleBuilder(myFhirContext); + + CodeSystem cs = new CodeSystem(); + cs.setId("CodeSystem/A"); + cs.setUrl("http://example.com/codesystem"); + cs.setContent(CodeSystem.CodeSystemContentMode.COMPLETE); + cs.addConcept().setCode("1").setDisplay("1"); + bb.addTransactionUpdateEntry(cs); + + Bundle outcome = mySystemDao.transaction(mySrd, bb.getBundleTyped()); + + assertThat(outcome.getEntry()).hasSize(1); + assertThat(outcome.getEntry().get(0).getResponse().getLocation()).endsWith("/_history/1"); + + } + + @Test public void testTransactionCreateInlineMatchUrlWithOneMatch2() { String methodName = "testTransactionCreateInlineMatchUrlWithOneMatch2"; diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java index ac06f20d9d0..a4389cfdefa 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java @@ -1,8 +1,5 @@ package ca.uhn.fhir.jpa.provider.r4; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertNull; import ca.uhn.fhir.batch2.jobs.expunge.DeleteExpungeProvider; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.i18n.Msg; @@ -59,9 +56,9 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.eclipse.jetty.server.Server; import org.eclipse.jetty.ee10.servlet.ServletContextHandler; import org.eclipse.jetty.ee10.servlet.ServletHolder; +import org.eclipse.jetty.server.Server; import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator; import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IIdType; @@ -101,7 +98,9 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail;