File size: 3,759 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.dalab.discovery.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Database configuration for test profile.
 * Enables JPA repositories and entity scanning for integration tests.
 * Disables schema validation to avoid JSONB vs JSON type conflicts with H2.
 */
@Configuration
@Profile("test")
@EnableJpaRepositories(basePackages = {
    "com.dalab.discovery.catalog.model.repository",
    "com.dalab.discovery.common.model.repository",
    "com.dalab.discovery.crawler.model.repository",
    "com.dalab.discovery.log.service.gcp.persistence.repository"
})
@EntityScan(basePackages = {
    "com.dalab.discovery.catalog.model",
    "com.dalab.discovery.common.model",
    "com.dalab.discovery.crawler.model",
    "com.dalab.discovery.log.service.gcp.persistence.entity"
})
@EnableTransactionManagement
public class TestDatabaseConfig {

    /**
     * Creates a test-specific entity manager factory with disabled schema validation.
     * This avoids JSONB vs JSON type conflicts when using H2 for testing.
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean testEntityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan(
            "com.dalab.discovery.catalog.model",
            "com.dalab.discovery.common.model",
            "com.dalab.discovery.crawler.model",
            "com.dalab.discovery.log.service.gcp.persistence.entity"
        );

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false); // We use Liquibase for DDL
        em.setJpaVendorAdapter(vendorAdapter);

        // Configure JPA properties for testing
        Properties properties = new Properties();
        
        // Use H2 dialect
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        
        // CRITICAL: Disable schema validation to avoid JSONB vs JSON conflicts
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("jakarta.persistence.schema-generation.database.action", "none");
        properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
        
        // Disable SQL logging for cleaner test output
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.format_sql", "false");
        
        // Set timezone
        properties.setProperty("hibernate.jdbc.time_zone", "UTC");
        
        em.setJpaProperties(properties);
        return em;
    }

    /**
     * Creates the transaction manager for tests.
     */
    @Bean
    public PlatformTransactionManager testTransactionManager(LocalContainerEntityManagerFactoryBean testEntityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(testEntityManagerFactory.getObject());
        return transactionManager;
    }
}