From fb93c3d601272a146904e438c11af93f32e70f1e Mon Sep 17 00:00:00 2001 From: Luke deGruchy Date: Thu, 19 Sep 2024 13:59:58 -0400 Subject: [PATCH] Expose hapi-fhir-storage-cr config to downstream modules for testing (#6272) * Ensure IRepositoryFactory returns a Repository, and not a HapiFhirRepository. * Rename test so that it will run from Maven and the pipeline since it didn't before. Fix small bug that made the test error out. * Move TestCrConfig and TestCrR4Config to main source folder. Extract TestHapiFhirCrPartitionConfig as separate test config to be used only in the test source folder. Add Maven compile dependency to hapi-fhir-jpaserver-base in order to ensure that moved Config classes will compile. * Add possible code change to RequestDetailsClone to clone partition info. * Introduce separate factory interface for Repository to preserve backward compatibility. * Remove IRepositoryFactoryForInterface. * Spotless * Add javadoc. * Get rid of TODO. * Restore RepositoryConfig to master. * Move test config to different packages. * Leverage use of new RepositoryFactoryForRepositoryInterface for CrR4Config and RepositoryFactoryForRepositoryInterface. * Ensure CrR4Config and RepositoryConfig make use of new RepositoryFactoryForRepositoryInterface. * Add copyright header * Add @FunctionalInterface to factory. --- hapi-fhir-storage-cr/pom.xml | 14 ++++++++ .../fhir/cr/common/IRepositoryFactory.java | 2 ++ ...positoryFactoryForRepositoryInterface.java | 31 +++++++++++++++++ .../uhn/fhir/cr/config/RepositoryConfig.java | 8 +++++ .../ca/uhn/fhir/cr/config/r4/CrR4Config.java | 4 ++- .../cr/config/test}/TestCqlProperties.java | 25 ++++++++++++-- .../fhir/cr/config/test}/TestCrConfig.java | 34 ++++++++++++++----- .../cr/config/test}/r4/TestCrR4Config.java | 28 +++++++++++++-- .../cr/TestHapiFhirCrPartitionConfig.java | 10 ++++++ .../uhn/fhir/cr/dstu3/TestCrDstu3Config.java | 6 ++-- .../ca/uhn/fhir/cr/r4/BaseCrR4TestServer.java | 3 ++ 11 files changed, 149 insertions(+), 16 deletions(-) create mode 100644 hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/RepositoryFactoryForRepositoryInterface.java rename hapi-fhir-storage-cr/src/{test/java/ca/uhn/fhir/cr => main/java/ca/uhn/fhir/cr/config/test}/TestCqlProperties.java (92%) rename hapi-fhir-storage-cr/src/{test/java/ca/uhn/fhir/cr => main/java/ca/uhn/fhir/cr/config/test}/TestCrConfig.java (89%) rename hapi-fhir-storage-cr/src/{test/java/ca/uhn/fhir/cr => main/java/ca/uhn/fhir/cr/config/test}/r4/TestCrR4Config.java (88%) create mode 100644 hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestHapiFhirCrPartitionConfig.java diff --git a/hapi-fhir-storage-cr/pom.xml b/hapi-fhir-storage-cr/pom.xml index 8d77500f0de..775a22c5e41 100644 --- a/hapi-fhir-storage-cr/pom.xml +++ b/hapi-fhir-storage-cr/pom.xml @@ -145,6 +145,20 @@ jakarta.xml.bind-api + + + ca.uhn.hapi.fhir + hapi-fhir-jpaserver-base + ${project.version} + + + + org.glassfish + jakarta.json + + + + org.testcontainers diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/IRepositoryFactory.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/IRepositoryFactory.java index 24963a9cc85..82094e62a6c 100644 --- a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/IRepositoryFactory.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/IRepositoryFactory.java @@ -21,8 +21,10 @@ package ca.uhn.fhir.cr.common; import ca.uhn.fhir.cr.repo.HapiFhirRepository; import ca.uhn.fhir.rest.api.server.RequestDetails; +import com.google.common.annotations.Beta; @FunctionalInterface +@Beta public interface IRepositoryFactory { HapiFhirRepository create(RequestDetails theRequestDetails); } diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/RepositoryFactoryForRepositoryInterface.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/RepositoryFactoryForRepositoryInterface.java new file mode 100644 index 00000000000..a01f5736000 --- /dev/null +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/common/RepositoryFactoryForRepositoryInterface.java @@ -0,0 +1,31 @@ +/*- + * #%L + * HAPI FHIR - Clinical Reasoning + * %% + * Copyright (C) 2014 - 2024 Smile CDR, Inc. + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package ca.uhn.fhir.cr.common; + +import ca.uhn.fhir.rest.api.server.RequestDetails; +import org.opencds.cqf.fhir.api.Repository; + +/** + * Factory interface to return a {@link Repository} from a {@link RequestDetails} + */ +@FunctionalInterface +public interface RepositoryFactoryForRepositoryInterface { + Repository create(RequestDetails theRequestDetails); +} diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/RepositoryConfig.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/RepositoryConfig.java index fb40025f58a..93d4c0f58ce 100644 --- a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/RepositoryConfig.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/RepositoryConfig.java @@ -20,6 +20,7 @@ package ca.uhn.fhir.cr.config; import ca.uhn.fhir.cr.common.IRepositoryFactory; +import ca.uhn.fhir.cr.common.RepositoryFactoryForRepositoryInterface; import ca.uhn.fhir.cr.repo.HapiFhirRepository; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.rest.server.RestfulServer; @@ -28,8 +29,15 @@ import org.springframework.context.annotation.Configuration; @Configuration public class RepositoryConfig { + @Bean IRepositoryFactory repositoryFactory(DaoRegistry theDaoRegistry, RestfulServer theRestfulServer) { return rd -> new HapiFhirRepository(theDaoRegistry, rd, theRestfulServer); } + + @Bean + RepositoryFactoryForRepositoryInterface repositoryFactoryForInterface( + DaoRegistry theDaoRegistry, RestfulServer theRestfulServer) { + return rd -> new HapiFhirRepository(theDaoRegistry, rd, theRestfulServer); + } } diff --git a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/r4/CrR4Config.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/r4/CrR4Config.java index 19466eba19b..3cdc398e0c0 100644 --- a/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/r4/CrR4Config.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/r4/CrR4Config.java @@ -22,6 +22,7 @@ package ca.uhn.fhir.cr.config.r4; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.cr.common.IRepositoryFactory; +import ca.uhn.fhir.cr.common.RepositoryFactoryForRepositoryInterface; import ca.uhn.fhir.cr.config.ProviderLoader; import ca.uhn.fhir.cr.config.ProviderSelector; import ca.uhn.fhir.cr.config.RepositoryConfig; @@ -66,7 +67,8 @@ public class CrR4Config { @Bean IMeasureServiceFactory r4MeasureServiceFactory( - IRepositoryFactory theRepositoryFactory, MeasureEvaluationOptions theEvaluationOptions) { + RepositoryFactoryForRepositoryInterface theRepositoryFactory, + MeasureEvaluationOptions theEvaluationOptions) { return rd -> new R4MeasureService(theRepositoryFactory.create(rd), theEvaluationOptions); } diff --git a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestCqlProperties.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/TestCqlProperties.java similarity index 92% rename from hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestCqlProperties.java rename to hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/TestCqlProperties.java index b9c5c081728..1be7c4f3505 100644 --- a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestCqlProperties.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/TestCqlProperties.java @@ -1,4 +1,23 @@ -package ca.uhn.fhir.cr; +/*- + * #%L + * HAPI FHIR - Clinical Reasoning + * %% + * Copyright (C) 2014 - 2024 Smile CDR, Inc. + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package ca.uhn.fhir.cr.config.test; import org.cqframework.cql.cql2elm.CqlCompilerException; import org.cqframework.cql.cql2elm.CqlCompilerOptions; @@ -7,7 +26,9 @@ import org.cqframework.cql.cql2elm.LibraryBuilder; import org.opencds.cqf.fhir.cql.CqlEngineOptions; import org.opencds.cqf.fhir.cql.CqlOptions; - +/** + * Common CQL properties shared with downstream modules. + */ public class TestCqlProperties { //cql settings diff --git a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestCrConfig.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/TestCrConfig.java similarity index 89% rename from hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestCrConfig.java rename to hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/TestCrConfig.java index 4f42ae49d52..9a9f43a902f 100644 --- a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestCrConfig.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/TestCrConfig.java @@ -1,4 +1,23 @@ -package ca.uhn.fhir.cr; +/*- + * #%L + * HAPI FHIR - Clinical Reasoning + * %% + * Copyright (C) 2014 - 2024 Smile CDR, Inc. + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package ca.uhn.fhir.cr.config.test; import ca.uhn.fhir.batch2.jobs.reindex.ReindexProvider; import ca.uhn.fhir.context.FhirContext; @@ -47,6 +66,9 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +/** + * Common hapi-fhir clinical reasoning config shared with downstream modules. + */ @Configuration @Import({SubscriptionSubmitterConfig.class, SubscriptionChannelConfig.class}) public class TestCrConfig { @@ -77,7 +99,9 @@ public class TestCrConfig { } @Bean public TestCqlProperties testCqlProperties(){ - return new TestCqlProperties();} + return new TestCqlProperties(); + } + @Bean public JpaStorageSettings storageSettings() { JpaStorageSettings storageSettings = new JpaStorageSettings(); @@ -87,12 +111,6 @@ public class TestCrConfig { storageSettings.setResourceClientIdStrategy(JpaStorageSettings.ClientIdStrategyEnum.ANY); return storageSettings; } - - @Bean - public PartitionHelper partitionHelper() { - return new PartitionHelper(); - } - @Bean public ModelManager modelManager(Map theGlobalModelCache) { return new ModelManager(theGlobalModelCache); diff --git a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/r4/TestCrR4Config.java b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/r4/TestCrR4Config.java similarity index 88% rename from hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/r4/TestCrR4Config.java rename to hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/r4/TestCrR4Config.java index e80eab49bc4..9e5b4c50466 100644 --- a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/r4/TestCrR4Config.java +++ b/hapi-fhir-storage-cr/src/main/java/ca/uhn/fhir/cr/config/test/r4/TestCrR4Config.java @@ -1,9 +1,28 @@ -package ca.uhn.fhir.cr.r4; +/*- + * #%L + * HAPI FHIR - Clinical Reasoning + * %% + * Copyright (C) 2014 - 2024 Smile CDR, Inc. + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package ca.uhn.fhir.cr.config.test.r4; -import ca.uhn.fhir.cr.TestCqlProperties; -import ca.uhn.fhir.cr.TestCrConfig; import ca.uhn.fhir.cr.common.CqlThreadFactory; import ca.uhn.fhir.cr.config.r4.CrR4Config; +import ca.uhn.fhir.cr.config.test.TestCqlProperties; +import ca.uhn.fhir.cr.config.test.TestCrConfig; import org.cqframework.cql.cql2elm.CqlCompilerOptions; import org.cqframework.cql.cql2elm.model.CompiledLibrary; import org.cqframework.cql.cql2elm.model.Model; @@ -30,6 +49,9 @@ import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +/** + * Common hapi-fhir clinical reasoning config specifically for R4 shared with downstream modules. + */ @Configuration @Import({TestCrConfig.class, CrR4Config.class}) public class TestCrR4Config { diff --git a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestHapiFhirCrPartitionConfig.java b/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestHapiFhirCrPartitionConfig.java new file mode 100644 index 00000000000..6b95b55deed --- /dev/null +++ b/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/TestHapiFhirCrPartitionConfig.java @@ -0,0 +1,10 @@ +package ca.uhn.fhir.cr; + +import org.springframework.context.annotation.Bean; + +public class TestHapiFhirCrPartitionConfig { + @Bean + public PartitionHelper partitionHelper() { + return new PartitionHelper(); + } +} diff --git a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/dstu3/TestCrDstu3Config.java b/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/dstu3/TestCrDstu3Config.java index 39fc0b34651..a79d4968627 100644 --- a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/dstu3/TestCrDstu3Config.java +++ b/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/dstu3/TestCrDstu3Config.java @@ -1,8 +1,9 @@ package ca.uhn.fhir.cr.dstu3; -import ca.uhn.fhir.cr.TestCqlProperties; -import ca.uhn.fhir.cr.TestCrConfig; +import ca.uhn.fhir.cr.TestHapiFhirCrPartitionConfig; +import ca.uhn.fhir.cr.config.test.TestCqlProperties; import ca.uhn.fhir.cr.config.dstu3.CrDstu3Config; +import ca.uhn.fhir.cr.config.test.TestCrConfig; import org.cqframework.cql.cql2elm.CqlCompilerOptions; import org.cqframework.cql.cql2elm.model.CompiledLibrary; import org.cqframework.cql.cql2elm.model.Model; @@ -26,6 +27,7 @@ import java.util.Set; @Configuration @Import({ + TestHapiFhirCrPartitionConfig.class, TestCrConfig.class, CrDstu3Config.class }) diff --git a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/r4/BaseCrR4TestServer.java b/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/r4/BaseCrR4TestServer.java index 686681d6352..49e31bad8c6 100644 --- a/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/r4/BaseCrR4TestServer.java +++ b/hapi-fhir-storage-cr/src/test/java/ca/uhn/fhir/cr/r4/BaseCrR4TestServer.java @@ -2,10 +2,12 @@ package ca.uhn.fhir.cr.r4; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.cr.IResourceLoader; +import ca.uhn.fhir.cr.TestHapiFhirCrPartitionConfig; import ca.uhn.fhir.cr.config.r4.ApplyOperationConfig; import ca.uhn.fhir.cr.config.r4.ExtractOperationConfig; import ca.uhn.fhir.cr.config.r4.PackageOperationConfig; import ca.uhn.fhir.cr.config.r4.PopulateOperationConfig; +import ca.uhn.fhir.cr.config.test.r4.TestCrR4Config; import ca.uhn.fhir.jpa.api.config.JpaStorageSettings; import ca.uhn.fhir.jpa.api.dao.DaoRegistry; import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider; @@ -38,6 +40,7 @@ import java.util.concurrent.TimeUnit; @ContextConfiguration(classes = { + TestHapiFhirCrPartitionConfig.class, TestCrR4Config.class, ApplyOperationConfig.class, ExtractOperationConfig.class,