Enable AsyncInstantiationBusinessLogic tests
[vid.git] / vid-app-common / src / test / java / org / onap / vid / config / DataSourceConfig.java
1 package org.onap.vid.config;
2
3
4 import java.util.Properties;
5 import javax.sql.DataSource;
6 import org.hibernate.SessionFactory;
7 import org.onap.portalsdk.core.service.DataAccessService;
8 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12 import org.springframework.core.io.ClassPathResource;
13 import org.springframework.core.io.Resource;
14 import org.springframework.jdbc.datasource.DriverManagerDataSource;
15 import org.springframework.orm.hibernate4.HibernateTransactionManager;
16 import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
17 import org.springframework.transaction.PlatformTransactionManager;
18 import org.springframework.transaction.annotation.EnableTransactionManagement;
19
20 /*
21 this class enable using in-memory DB in Unit test.
22 */
23 @Configuration
24 @EnableTransactionManagement
25 public class DataSourceConfig {
26
27     @Bean
28     @Autowired
29     public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
30         LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
31         sessionFactory.setDataSource(dataSource);
32         //I used this class org.onap.portalsdk.core.conf.HibernateConfiguration to learn how to config the session factory
33         // and use the following url for actual h2 properties
34         //https://github.com/levi-putna/Hibernate-H2-Example/blob/master/hibernate-h2-example/src/hibernate.cfg.xml
35         Properties properties = getH2Properties();
36
37         properties.put("connection.pool_size", 10);
38         properties.put("cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider");
39         properties.put("hibernate.show_sql", false);
40         properties.put("hbm2ddl.auto", "create");
41         properties.put("hibernate.hbm2ddl.auto", "create");
42
43         Resource[] mappingLocations = {
44                 new ClassPathResource("WEB-INF/fusion/orm/Fusion.hbm.xml"),
45                 new ClassPathResource("WEB-INF/fusion/orm/Workflow.hbm.xml"),
46                 new ClassPathResource("WEB-INF/fusion/orm/RNoteBookIntegration.hbm.xml")
47         };
48
49         sessionFactory.setHibernateProperties(properties);
50         sessionFactory.setPackagesToScan("org.onap");
51         sessionFactory.setMappingLocations(mappingLocations);
52         return sessionFactory;
53     }
54
55     @Bean
56     public DataSource getDataSource() {
57         DriverManagerDataSource dataSource = new DriverManagerDataSource();
58         dataSource.setDriverClassName("org.h2.Driver");
59         dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
60         dataSource.setUsername("sa");
61         dataSource.setPassword("");
62         return dataSource;
63     }
64
65     public Properties getH2Properties() {
66         Properties properties = new Properties();
67         properties.put("dialect", "org.hibernate.dialect.H2Dialect");
68         properties.put("hibernate.default_schema", "PUBLIC");
69         return properties;
70     }
71
72     public Properties getSqliteProperties() {
73         Properties properties = new Properties();
74         properties.put("connection.driver_class", "org.sqlite.JDBC");
75         properties.put("connection.url", "jdbc:sqlite:memory:myDb");
76         properties.put("connection.username", "sa");
77         properties.put("connection.password", "sa");
78         properties.put("hibernate.default_schema", "PUBLIC");
79         properties.put("dialect", "com.enigmabridge.hibernate.dialect.SQLiteDialect");
80         return properties;
81     }
82
83     @Bean
84     public DataAccessService dataAccessService() {
85         return new DataAccessServiceImpl();
86     }
87
88     @Bean
89     @Autowired
90     public PlatformTransactionManager transactionManager(SessionFactory sessionFactory) {
91         return new HibernateTransactionManager(sessionFactory);
92     }
93 }