|
| 1 | +package com.openelements.crm; |
| 2 | + |
| 3 | +import com.openelements.spring.base.services.user.SystemUser; |
| 4 | +import com.openelements.spring.base.services.user.UserRepository; |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 10 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 11 | +import org.springframework.test.context.ActiveProfiles; |
| 12 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 13 | + |
| 14 | +/** |
| 15 | + * Shared test base for Spring Boot tests that touch the database. Starts a |
| 16 | + * single {@code postgres:17-alpine} container once per JVM, registers it as |
| 17 | + * the application {@link javax.sql.DataSource} via {@link ServiceConnection}, |
| 18 | + * and truncates all application tables between tests for per-method isolation. |
| 19 | + * |
| 20 | + * <p>The container is reused across runs when {@code testcontainers.reuse.enable=true} |
| 21 | + * is set in {@code ~/.testcontainers.properties} (local dev). CI runs ignore |
| 22 | + * the reuse hint, so each build gets a fresh container. |
| 23 | + * |
| 24 | + * <p>Adding a new table in a Flyway migration requires extending {@link #TABLES_TO_TRUNCATE}. |
| 25 | + * This is intentional — the explicit list keeps the truncate fast and predictable. |
| 26 | + * |
| 27 | + * <p>The {@code @Testcontainers} JUnit extension is intentionally not used — |
| 28 | + * it discovers {@code @Container} fields and does nothing for a manually |
| 29 | + * started container. The {@code static { POSTGRES.start(); }} block is what |
| 30 | + * actually brings the container up before any subclass context loads. |
| 31 | + */ |
| 32 | +@SpringBootTest |
| 33 | +@AutoConfigureMockMvc |
| 34 | +@ActiveProfiles("test") |
| 35 | +public abstract class AbstractDbTest { |
| 36 | + |
| 37 | + /** |
| 38 | + * Tables truncated between tests. Order is irrelevant because |
| 39 | + * {@code CASCADE} drops dependent rows. |
| 40 | + */ |
| 41 | + private static final String TABLES_TO_TRUNCATE = String.join(", ", |
| 42 | + "api_keys", |
| 43 | + "audit_log", |
| 44 | + "comments", |
| 45 | + "company_comments", |
| 46 | + "company_tags", |
| 47 | + "contact_comments", |
| 48 | + "contact_social_links", |
| 49 | + "contact_tags", |
| 50 | + "contacts", |
| 51 | + "companies", |
| 52 | + "settings", |
| 53 | + "task_comments", |
| 54 | + "task_tags", |
| 55 | + "tasks", |
| 56 | + "tags", |
| 57 | + "users", |
| 58 | + "webhooks" |
| 59 | + ); |
| 60 | + |
| 61 | + @ServiceConnection |
| 62 | + static final PostgreSQLContainer<?> POSTGRES = |
| 63 | + new PostgreSQLContainer<>("postgres:17-alpine") |
| 64 | + .withReuse(true); |
| 65 | + |
| 66 | + static { |
| 67 | + POSTGRES.start(); |
| 68 | + } |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private JdbcTemplate jdbcTemplate; |
| 72 | + |
| 73 | + @Autowired |
| 74 | + private UserRepository userRepository; |
| 75 | + |
| 76 | + @AfterEach |
| 77 | + void truncateAllTables() { |
| 78 | + // RESTART IDENTITY keeps sequence-backed columns deterministic; CASCADE |
| 79 | + // drops dependent rows so order of tables is irrelevant. |
| 80 | + jdbcTemplate.execute("TRUNCATE TABLE " + TABLES_TO_TRUNCATE + " RESTART IDENTITY CASCADE"); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * (Re-)inserts the {@link SystemUser} row needed by audit-log / comment |
| 85 | + * tests that reference {@code SystemUser.ID} as a foreign key. Idempotent: |
| 86 | + * subclasses call this from their own {@code @BeforeEach} when needed. |
| 87 | + * The {@code @AfterEach} truncate wipes {@code users}, so the seed must |
| 88 | + * be re-applied per test method. |
| 89 | + */ |
| 90 | + protected void seedSystemUser() { |
| 91 | + if (userRepository.findBySub(SystemUser.SUB).isEmpty()) { |
| 92 | + jdbcTemplate.update( |
| 93 | + "INSERT INTO users (id, sub, name, created_at, updated_at) " |
| 94 | + + "VALUES (?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)", |
| 95 | + SystemUser.ID, SystemUser.SUB, SystemUser.NAME); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments