File size: 3,136 Bytes
442299c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.dalab.discovery.sd.config;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Provides the EntityManagerFactory and TransactionManager for tests,
 * relying on application-test.yml for DataSource and most JPA properties.
 */
@TestConfiguration
@EnableJpaRepositories(
    basePackages = {"com.dalab.discovery.common.model.repository", "com.dalab.discovery.sd.domain.repository", 
    "com.dalab.discovery.catalog.repository", "com.dalab.discovery.config.repository", "com.dalab.discovery.config.model"},
    entityManagerFactoryRef = "entityManagerFactory",
    transactionManagerRef = "transactionManager"
)
@EnableTransactionManagement
public class TestDatabaseConfiguration {

    @SuppressWarnings("rawtypes")
    @Bean(name = "entityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) { 
        
        Map<String, Object> jpaProperties = new HashMap<>();
        // Explicitly set H2 dialect for this EntityManagerFactory
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        // ddl-auto should be picked from application-test.yml (create-drop)
        // Other properties like show-sql, format_sql also from application-test.yml

        return builder
                .dataSource(dataSource)
                .packages(
                    "com.dalab.discovery.common.model.entity", 
                    "com.dalab.discovery.common.model",
                    "com.dalab.discovery.catalog.model",
                    "com.dalab.discovery.config.model", 
                    "com.dalab.discovery.sd.model",
                    "com.dalab.discovery.sd.repository",
                    "com.dalab.discovery.crawler.model", 
                    "com.dalab.discovery.domain.model",
                    "com.dalab.discovery.log.service.gcp.persistence.entity"
                ) 
                .persistenceUnit("testPU") 
                .properties(jpaProperties) // Set explicit properties
                .build();
    }

    @Bean(name = "transactionManager")
    @Primary
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory.getObject());
    }
}