Skip to content

Introduce Testcontainers as a means to run integration tests against a wider set of RDBMSs #2256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gregturn opened this issue Jul 9, 2021 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@gregturn
Copy link
Contributor

gregturn commented Jul 9, 2021

Testcontainers affords us the means to test against more database types. Introducing it can increase confidence in supported technologies. Additionally, certain test cases are disabled because the related features are not support on HSQL, the embedded engine currently used.

Related: #409.

Also see: https://github.com/GabrielBB/spring-data-jpa-procedure-tests

For an example of using Java Config to spin up Testcontainers for a given test case, see the following:

@ContextConfiguration
@Transactional
@ExtendWith(SpringExtension.class)
public class ScratchIntegrationTests {

	@PersistenceContext EntityManager em;

      // insert test methods here

	@Configuration
	@EnableJpaRepositories(basePackageClasses = DummyRepository.class,
			includeFilters = { @Filter(pattern = ".*DummyRepository", type = FilterType.REGEX) })
	static class MySqlConfiguration {

		private static PostgreSQLContainer<?> POSTGRESQL_CONTAINER;

		@Bean
		public DataSource dataSource() {

			if (POSTGRESQL_CONTAINER == null) {

				PostgreSQLContainer<?> container = new PostgreSQLContainer<>();
				container.start();

				POSTGRESQL_CONTAINER = container;
			}

			PGSimpleDataSource dataSource = new PGSimpleDataSource();
			dataSource.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl());
			dataSource.setUser(POSTGRESQL_CONTAINER.getUsername());
			dataSource.setPassword(POSTGRESQL_CONTAINER.getPassword());

			return dataSource;
		}

		@Bean
		public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
				AbstractJpaVendorAdapter jpaVendorAdapter) {

			LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
			entityManagerFactoryBean.setDataSource(dataSource);
			entityManagerFactoryBean.setPersistenceUnitName("spring-data-jpa");
			entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
			entityManagerFactoryBean.setJpaProperties(this.additionalProperties());

			return entityManagerFactoryBean;
		}

		@Bean
		DataSourceInitializer initializer(DataSource dataSource, Environment environment) {

			DataSourceInitializer initializer = new DataSourceInitializer();
			initializer.setDataSource(dataSource);

			ClassPathResource script = new ClassPathResource("scripts/schema-stored-procedures.sql", getClass().getClassLoader());
			ResourceDatabasePopulator populator = new ResourceDatabasePopulator(script);
			initializer.setDatabasePopulator(populator);

			return initializer;
		}


		@Bean
		public AbstractJpaVendorAdapter jpaVendorAdapter() {

			HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
			jpaVendorAdapter.setGenerateDdl(true);
			jpaVendorAdapter.setDatabase(Database.POSTGRESQL);

			return jpaVendorAdapter;
		}

		@Bean
		public TransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
			return new JpaTransactionManager(entityManagerFactory);
		}

		@Bean
		public SampleEvaluationContextExtension sampleEvaluationContextExtension() {
			return new SampleEvaluationContextExtension();
		}

		@Bean
		public PropertiesFactoryBean jpaProperties() {
			return new PropertiesFactoryBean();
		}

		private Properties additionalProperties() {

			Properties properties = new Properties();
			properties.setProperty("hibernate.dialect", PostgresPlusDialect.class.getCanonicalName());

			return properties;
		}
	}
@gregturn gregturn self-assigned this Jul 9, 2021
@gregturn gregturn added the type: enhancement A general enhancement label Jul 9, 2021
@gregturn gregturn added this to the 2.6 M1 (2021.1.0) milestone Jul 9, 2021
gregturn pushed a commit that referenced this issue Jul 9, 2021
When registered procedure output parameters, if the method annotated with @procedure has a collection return type or an entity return type, then use ResultSet. Otherwise, handle either the array (Object[]) or the primitive types.

* Throw a proper exception when using an @Procedure-annotated method and no transaction is active.
* Introduce refCursor as a boolean flag to @procedure denoting when to use REF_CURSORs for OUT parameters..
* Extract a ProcedureParameter command object to ease usage of a parameter's name, type and mode. (NOTE: javax.persistence already has "StoredProcedureParameter".)

NOTE: Integration testing of stored procedures is very limited by lack of HSQL's support for REF CURSORS. See ##2256 to track future work for potential testing against MySQL, Oracle, Postgres, or SQL Server.

See: #1959, #409.
Related: #2014.
gregturn added a commit that referenced this issue Jul 22, 2021
gregturn added a commit that referenced this issue Jul 23, 2021
* Introduce Testcontainers as the mechanism to test against real data stores.
* Alter CI to handle running Testcontainers in reduced privilege mode.
* Run the data store specific test cases ONLY for the baseline Java 8 test execution.

See #2256.
gregturn added a commit that referenced this issue Jul 23, 2021
* Introduce Testcontainers as the mechanism to test against real data stores.
* Alter CI to handle running Testcontainers in reduced privilege mode.
* Run the data store specific test cases ONLY for the baseline Java 8 test execution.

See #2256.
@gregturn
Copy link
Contributor Author

gregturn commented Jul 23, 2021

Resolved via a4ef56d.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant