Skip to content

Instantly share code, notes, and snippets.

@omidp
Last active April 8, 2022 04:44
Show Gist options
  • Save omidp/f928616f1766173f531a09e04f0f4491 to your computer and use it in GitHub Desktop.
Save omidp/f928616f1766173f531a09e04f0f4491 to your computer and use it in GitHub Desktop.
Spring integration test
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import com.chatbot.app.dao.ProductCategoryDao;
import com.chatbot.app.domain.BotEntity;
import com.chatbot.app.domain.ProductEntity;
@TestConfiguration
@EntityScan(basePackageClasses = { ProductEntity.class })
@EnableJpaRepositories(basePackageClasses = { ProductCategoryDao.class }, queryLookupStrategy = Key.USE_DECLARED_QUERY)
public class DBTestConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:postgresql://127.0.0.1:5432/chatbotdb",
"chatbot", "chatbot");
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaProperties jpaProps) {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
jpaVendorAdapter.setGenerateDdl(true);
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setJpaPropertyMap(jpaProps.getProperties());
factoryBean.setPackagesToScan(BotEntity.class.getPackage().getName());
factoryBean.setPersistenceUnitName("persistenceUnit");
return factoryBean;
}
@Bean("jpaProps")
public JpaProperties jpaProperties() {
JpaProperties prop = new JpaProperties();
prop.setShowSql(true);
prop.setDatabase(Database.POSTGRESQL);
prop.setGenerateDdl(true);
Map<String, String> m = new HashMap<>();
m.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
m.put("hibernate.jdbc.lob.non_contextual_creation", "true");
m.put("hibernate.temp.use_jdbc_metadata_defaults", "false");
prop.setProperties(m);
return prop;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
}
package com.integration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import javax.sql.DataSource;
import org.apache.commons.configuration.ConfigurationException;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.omidbiz.core.axon.Axon;
import org.omidbiz.core.axon.AxonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import com.integration.JsonSerializeTest.DBTestConfig;
import com.jedlab.framework.exceptions.ServiceException;
import com.jedlab.framework.json.xml.XmlConfigFilter;
import lombok.extern.slf4j.Slf4j;
@RunWith(SpringRunner.class)
@Slf4j
// @TestPropertySource(properties = { })
// @ActiveProfiles("sit")
public class FundServiceTest
{
@Autowired
FundService fundService;
private static final Axon axon = new AxonBuilder().preventRecursion().addFilter(new XmlConfigFilter(FundEntity.class, "default")).create();
@Test
public void testFundSettlement()
{
long balance = fundService.settleWithMerchantByIban("".trim());
assert balance == 0;
}
@TestConfiguration
@EnableJpaRepositories(basePackageClasses = FundDao.class)
@ComponentScan(basePackageClasses = FundService.class)
public static class DBFundTestConfig
{
@Bean
public DataSource dataSource()
{
DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:postgresql://127.0.0.1:5432/testdb", "test",
"test");
return ds;
}
@Bean
JdbcTemplate jdbcTemplate()
{
return new JdbcTemplate(dataSource());
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory)
{
// return new DataSourceTransactionManager(dataSource());
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaProperties jpaProperties)
{
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setJpaPropertyMap(jpaProperties.getProperties());
factoryBean.setPackagesToScan(FundEntity.class.getPackage().getName());
factoryBean.setPersistenceUnitName("persistenceUnit");
return factoryBean;
}
@Bean
JpaProperties jpaProperties()
{
JpaProperties jpaProps = new JpaProperties();
jpaProps.setDatabase(Database.POSTGRESQL);
jpaProps.setGenerateDdl(true);
jpaProps.setOpenInView(false);
jpaProps.setShowSql(true);
Map<String, String> p = new HashMap<>();
p.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
p.put("hibernate.jdbc.lob.non_contextual_creation", "true");
p.put("hibernate.temp.use_jdbc_metadata_defaults", "false");
p.put("hibernate.jdbc.batch_size", "1000");
p.put("hibernate.order_inserts", "true");
p.put("hibernate.order_updates", "true");
p.put("hibernate.cache.use_query_cache", "true");
p.put("hibernate.cache.use_second_level_cache", "false");
p.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
p.put("connection-test-query", "SELECT 1");
jpaProps.setProperties(p);
return jpaProps;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver()
{
return new PropertySourcesPlaceholderConfigurer();
}
}
}
@ExtendWith(SpringExtension.class)
//@DataJpaTest()
//@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2, replace = Replace.ANY)
@Import(value= {DBTestConfig.class, TestContextLoader.class})
public class NodeEntityTypeTest {
@TestConfiguration
public static class TestContextLoader
{
@Bean
NodeService nodeService()
{
return new NodeService();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment