Unit tests are starting to work again ...

This commit is contained in:
jvi
2020-09-08 12:39:39 +02:00
parent d208f12546
commit 9f0b32ff71
8 changed files with 249 additions and 114 deletions

View File

@@ -0,0 +1,92 @@
package ca.uhn.fhir.jpa.starter;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class EnvironmentHelper {
public static Properties getHibernateProperties(ConfigurableEnvironment environment) {
Properties properties = new Properties();
if (environment.getProperty("spring.jpa.properties", String.class) == null) {
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.search.model_mapping", "ca.uhn.fhir.jpa.search.LuceneSearchMappingFactory");
properties.put("hibernate.format_sql", "false");
properties.put("hibernate.show_sql", "false");
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.jdbc.batch_size", "20");
properties.put("hibernate.cache.use_query_cache", "false");
properties.put("hibernate.cache.use_second_level_cache", "false");
properties.put("hibernate.cache.use_structured_entries", "false");
properties.put("hibernate.cache.use_minimal_puts", "false");
properties.put("hibernate.search.default.directory_provider", "filesystem");
properties.put("hibernate.search.default.indexBase", "target/lucenefiles");
properties.put("hibernate.search.lucene_version", "LUCENE_CURRENT");
} else {
Arrays.asList(environment.getProperty("spring.jpa.properties", String.class).split(" ")).stream().forEach(s ->
{
String[] values = s.split("=");
properties.put(values[0], values[1]);
});
}
return properties;
}
public static Map<String, Object> getPropertiesStartingWith(ConfigurableEnvironment aEnv,
String aKeyPrefix) {
Map<String, Object> result = new HashMap<>();
Map<String, Object> map = getAllProperties(aEnv);
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (key.startsWith(aKeyPrefix)) {
result.put(key, entry.getValue());
}
}
return result;
}
public static Map<String, Object> getAllProperties(ConfigurableEnvironment aEnv) {
Map<String, Object> result = new HashMap<>();
aEnv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
return result;
}
public static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) {
Map<String, Object> result = new HashMap<>();
if (aPropSource instanceof CompositePropertySource) {
CompositePropertySource cps = (CompositePropertySource) aPropSource;
cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
return result;
}
if (aPropSource instanceof EnumerablePropertySource<?>) {
EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
return result;
}
return result;
}
private static void addAll(Map<String, Object> aBase, Map<String, Object> aToBeAdded) {
for (Map.Entry<String, Object> entry : aToBeAdded.entrySet()) {
if (aBase.containsKey(entry.getKey())) {
continue;
}
aBase.put(entry.getKey(), entry.getValue());
}
}
}

View File

@@ -8,26 +8,31 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@Configuration @Configuration
@Profile("dstu2") @Profile("dstu2")
public class FhirServerConfigDstu2 extends BaseJavaConfigDstu2 { public class FhirServerConfigDstu2 extends BaseJavaConfigDstu2 {
@Autowired @Autowired
private DataSource myDataSource; private DataSource myDataSource;
/** /**
* We override the paging provider definition so that we can customize * We override the paging provider definition so that we can customize
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired @Autowired
AppProperties appProperties; AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
@@ -37,28 +42,31 @@ public class FhirServerConfigDstu2 extends BaseJavaConfigDstu2 {
return pagingProvider; return pagingProvider;
} }
@Override @Autowired
@Bean() private ConfigurableEnvironment configurableEnvironment;
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName("HAPI_PU");
try { @Override
retVal.setDataSource(myDataSource); @Bean()
} catch (Exception e) { public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e); LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
} retVal.setPersistenceUnitName("HAPI_PU");
try {
return retVal; retVal.setDataSource(myDataSource);
} catch (Exception e) {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
} }
retVal.setJpaProperties(EnvironmentHelper.getHibernateProperties(configurableEnvironment));
return retVal;
}
@Bean @Bean
@Primary @Primary
public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) { public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager retVal = new JpaTransactionManager(); JpaTransactionManager retVal = new JpaTransactionManager();
retVal.setEntityManagerFactory(entityManagerFactory); retVal.setEntityManagerFactory(entityManagerFactory);
return retVal; return retVal;
} }
} }

View File

@@ -8,6 +8,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -18,47 +19,50 @@ import javax.sql.DataSource;
@Profile("dstu3") @Profile("dstu3")
public class FhirServerConfigDstu3 extends BaseJavaConfigDstu3 { public class FhirServerConfigDstu3 extends BaseJavaConfigDstu3 {
@Autowired @Autowired
private DataSource myDataSource; private DataSource myDataSource;
/** /**
* We override the paging provider definition so that we can customize * We override the paging provider definition so that we can customize
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired @Autowired
AppProperties appProperties; AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider(); DatabaseBackedPagingProvider pagingProvider = super.databaseBackedPagingProvider();
pagingProvider.setDefaultPageSize(appProperties.getDefault_page_size()); pagingProvider.setDefaultPageSize(appProperties.getDefault_page_size());
pagingProvider.setMaximumPageSize(appProperties.getMax_page_size()); pagingProvider.setMaximumPageSize(appProperties.getMax_page_size());
return pagingProvider; return pagingProvider;
}
@Autowired
private ConfigurableEnvironment configurableEnvironment;
@Override
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName("HAPI_PU");
try {
retVal.setDataSource(myDataSource);
} catch (Exception e) {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
} }
@Override retVal.setJpaProperties(EnvironmentHelper.getHibernateProperties(configurableEnvironment));
@Bean return retVal;
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { }
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName("HAPI_PU");
try { @Bean
retVal.setDataSource(myDataSource); @Primary
} catch (Exception e) { public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e); JpaTransactionManager retVal = new JpaTransactionManager();
} retVal.setEntityManagerFactory(entityManagerFactory);
return retVal;
}
return retVal;
}
@Bean
@Primary
public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager retVal = new JpaTransactionManager();
retVal.setEntityManagerFactory(entityManagerFactory);
return retVal;
}
} }

View File

@@ -8,26 +8,30 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Properties;
@Configuration @Configuration
@Profile("r4") @Profile("r4")
public class FhirServerConfigR4 extends BaseJavaConfigR4 { public class FhirServerConfigR4 extends BaseJavaConfigR4 {
@Autowired @Autowired
private DataSource myDataSource; private DataSource myDataSource;
/** /**
* We override the paging provider definition so that we can customize * We override the paging provider definition so that we can customize
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired @Autowired
AppProperties appProperties; AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
@@ -37,28 +41,31 @@ public class FhirServerConfigR4 extends BaseJavaConfigR4 {
return pagingProvider; return pagingProvider;
} }
@Override @Autowired
@Bean() private ConfigurableEnvironment configurableEnvironment;
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName("HAPI_PU");
try { @Override
retVal.setDataSource(myDataSource); @Bean()
} catch (Exception e) { public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e); LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
} retVal.setPersistenceUnitName("HAPI_PU");
try {
return retVal; retVal.setDataSource(myDataSource);
} catch (Exception e) {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
} }
retVal.setJpaProperties(EnvironmentHelper.getHibernateProperties(configurableEnvironment));
return retVal;
}
@Bean @Bean
@Primary @Primary
public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) { public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager retVal = new JpaTransactionManager(); JpaTransactionManager retVal = new JpaTransactionManager();
retVal.setEntityManagerFactory(entityManagerFactory); retVal.setEntityManagerFactory(entityManagerFactory);
return retVal; return retVal;
} }
} }

View File

@@ -8,6 +8,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@@ -18,16 +19,16 @@ import javax.sql.DataSource;
@Profile("r5") @Profile("r5")
public class FhirServerConfigR5 extends BaseJavaConfigR5 { public class FhirServerConfigR5 extends BaseJavaConfigR5 {
@Autowired @Autowired
private DataSource myDataSource; private DataSource myDataSource;
/** /**
* We override the paging provider definition so that we can customize * We override the paging provider definition so that we can customize
* the default/max page sizes for search results. You can set these however * the default/max page sizes for search results. You can set these however
* you want, although very large page sizes will require a lot of RAM. * you want, although very large page sizes will require a lot of RAM.
*/ */
@Autowired @Autowired
AppProperties appProperties; AppProperties appProperties;
@Override @Override
public DatabaseBackedPagingProvider databaseBackedPagingProvider() { public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
@@ -36,28 +37,32 @@ public class FhirServerConfigR5 extends BaseJavaConfigR5 {
pagingProvider.setMaximumPageSize(appProperties.getMax_page_size()); pagingProvider.setMaximumPageSize(appProperties.getMax_page_size());
return pagingProvider; return pagingProvider;
} }
@Override
@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName("HAPI_PU");
try { @Autowired
retVal.setDataSource(myDataSource); private ConfigurableEnvironment configurableEnvironment;
} catch (Exception e) {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
}
retVal.setJpaProperties(HapiProperties.getJpaProperties()); @Override
return retVal; @Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean retVal = super.entityManagerFactory();
retVal.setPersistenceUnitName("HAPI_PU");
try {
retVal.setDataSource(myDataSource);
} catch (Exception e) {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
} }
retVal.setJpaProperties(EnvironmentHelper.getHibernateProperties(configurableEnvironment));
return retVal;
}
@Bean @Bean
@Primary @Primary
public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) { public JpaTransactionManager hapiTransactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager retVal = new JpaTransactionManager(); JpaTransactionManager retVal = new JpaTransactionManager();
retVal.setEntityManagerFactory(entityManagerFactory); retVal.setEntityManagerFactory(entityManagerFactory);
return retVal; return retVal;
} }
} }

View File

@@ -7,7 +7,23 @@ spring:
max-active: 15 max-active: 15
profiles: profiles:
### This is the FHIR version. Choose between, dstu2, dstu3, r4 or r5 ### This is the FHIR version. Choose between, dstu2, dstu3, r4 or r5
active: r4 active: dstu2
jpa:
properties:
hibernate.dialect: org.hibernate.dialect.H2Dialect
hibernate.search.model_mapping: ca.uhn.fhir.jpa.search.LuceneSearchMappingFactory
hibernate.format_sql: false
hibernate.show_sql: false
hibernate.hbm2ddl.auto: update
hibernate.jdbc.batch_size: 20
hibernate.cache.use_query_cache: false
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_structured_entries: false
hibernate.cache.use_minimal_puts: false
hibernate.search.default.directory_provider: filesystem
hibernate.search.default.indexBase: target/lucenefiles
hibernate.search.lucene_version: LUCENE_CURRENT
hapi: hapi:
fhir: fhir:
#supported_resource_types: #supported_resource_types:

View File

@@ -12,11 +12,13 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@TestPropertySource(locations = "/application-integrationtest.yaml")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties = @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties =
{ {
"spring.batch.job.enabled=false", "spring.batch.job.enabled=false",

View File

@@ -7,7 +7,8 @@ spring:
max-active: 15 max-active: 15
profiles: profiles:
### This is the FHIR version. Choose between, dstu2, dstu3, r4 or r5 ### This is the FHIR version. Choose between, dstu2, dstu3, r4 or r5
active: r4 active: dstu2
hapi: hapi:
fhir: fhir:
#supported_resource_types: #supported_resource_types: