content: package org.pmiops.workbench.testconfig;
  
  import jakarta.persistence.EntityManagerFactory;
  import java.util.Properties;
  import javax.sql.DataSource;
  import org.springframework.boot.test.context.TestConfiguration;
  import org.springframework.context.annotation.Bean;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  @TestConfiguration
  @EnableJpaRepositories(basePackages = {"org.pmiops.workbench.cdr", "org.pmiops.workbench.db"})
  @EnableTransactionManagement
  public class TestJpaConfig {
  
    @Bean
    public DataSource dataSource() {
      final DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("org.h2.Driver");
      dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
      dataSource.setUsername("sa");
      dataSource.setPassword("sa");
  
      return dataSource;
    }
  
    @Bean
    public JdbcTemplate getJdbcTemplate() {
      return new JdbcTemplate(dataSource());
    }
  
    @Bean
    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
      return new NamedParameterJdbcTemplate(dataSource());
    }
  
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[] {"org.pmiops.workbench.cdr", "org.pmiops.workbench.db"});
      em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
      em.setJpaProperties(additionalProperties());
      return em;
    }
  
    @Bean
    public JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
      final JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(entityManagerFactory);
      return transactionManager;
    }
  
    public final Properties additionalProperties() {
      final Properties hibernateProperties = new Properties();
  
      hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
      hibernateProperties.setProperty(
          "hibernate.dialect", "org.pmiops.workbench.cdr.CommonTestDialect");
  
      return hibernateProperties;
    }
  }
  