2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.config;
24 import java.util.Properties;
25 import javax.sql.DataSource;
26 import org.hibernate.SessionFactory;
27 import org.onap.portalsdk.core.service.DataAccessService;
28 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.context.annotation.Configuration;
32 import org.springframework.core.io.ClassPathResource;
33 import org.springframework.core.io.Resource;
34 import org.springframework.jdbc.datasource.DriverManagerDataSource;
35 import org.springframework.orm.hibernate4.HibernateTransactionManager;
36 import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
37 import org.springframework.transaction.PlatformTransactionManager;
38 import org.springframework.transaction.annotation.EnableTransactionManagement;
41 this class enable using in-memory DB in Unit test.
44 @EnableTransactionManagement
45 public class DataSourceConfig {
49 public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
50 LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
51 sessionFactory.setDataSource(dataSource);
52 //I used this class org.onap.portalsdk.core.conf.HibernateConfiguration to learn how to config the session factory
53 // and use the following url for actual h2 properties
54 //https://github.com/levi-putna/Hibernate-H2-Example/blob/master/hibernate-h2-example/src/hibernate.cfg.xml
55 Properties properties = getH2Properties();
57 properties.put("connection.pool_size", 10);
58 properties.put("cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider");
59 properties.put("hibernate.show_sql", false);
60 properties.put("hbm2ddl.auto", "create");
61 properties.put("hibernate.hbm2ddl.auto", "create");
63 Resource[] mappingLocations = {
64 new ClassPathResource("WEB-INF/fusion/orm/Fusion.hbm.xml"),
65 new ClassPathResource("WEB-INF/fusion/orm/Workflow.hbm.xml"),
66 // new ClassPathResource("WEB-INF/fusion/orm/RNoteBookIntegration.hbm.xml")
69 sessionFactory.setHibernateProperties(properties);
70 sessionFactory.setPackagesToScan("org.onap");
71 sessionFactory.setMappingLocations(mappingLocations);
72 return sessionFactory;
76 public DataSource getDataSource() {
77 DriverManagerDataSource dataSource = new DriverManagerDataSource();
78 dataSource.setDriverClassName("org.h2.Driver");
79 dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
80 dataSource.setUsername("sa");
81 dataSource.setPassword("");
85 public Properties getH2Properties() {
86 Properties properties = new Properties();
87 properties.put("dialect", "org.hibernate.dialect.H2Dialect");
88 properties.put("hibernate.default_schema", "PUBLIC");
92 public Properties getSqliteProperties() {
93 Properties properties = new Properties();
94 properties.put("connection.driver_class", "org.sqlite.JDBC");
95 properties.put("connection.url", "jdbc:sqlite:memory:myDb");
96 properties.put("connection.username", "sa");
97 properties.put("connection.password", "sa");
98 properties.put("hibernate.default_schema", "PUBLIC");
99 properties.put("dialect", "com.enigmabridge.hibernate.dialect.SQLiteDialect");
104 public DataAccessService dataAccessService() {
105 return new DataAccessServiceImpl();
110 public PlatformTransactionManager transactionManager(SessionFactory sessionFactory) {
111 return new HibernateTransactionManager(sessionFactory);