|
| 1 | +package com.digitalsanctuary.spring.user.persistence.schema; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import com.digitalsanctuary.spring.user.test.app.TestApplication; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 12 | + |
| 13 | +/** |
| 14 | + * Abstract base class for database schema validation tests. Subclasses provide a real database via Testcontainers and |
| 15 | + * configure Spring to connect to it. This test verifies that Hibernate can create the full schema without errors on each |
| 16 | + * target database. |
| 17 | + * |
| 18 | + * <p> |
| 19 | + * The test uses {@code ddl-auto: create} (via Spring Boot properties) and then queries |
| 20 | + * {@code INFORMATION_SCHEMA.TABLES} to verify all expected tables were created. This catches silent DDL failures like |
| 21 | + * the one described in GitHub issue #286. |
| 22 | + * </p> |
| 23 | + */ |
| 24 | +@SpringBootTest(classes = TestApplication.class) |
| 25 | +abstract class AbstractSchemaValidationTest { |
| 26 | + |
| 27 | + /** |
| 28 | + * All tables expected to be created by Hibernate from the entity model. Includes entity tables and join tables. |
| 29 | + */ |
| 30 | + private static final Set<String> EXPECTED_TABLES = Set.of( |
| 31 | + // Entity tables |
| 32 | + "user_account", "role", "privilege", "verification_token", "password_reset_token", |
| 33 | + "password_history_entry", "user_entities", "user_credentials", |
| 34 | + // Join tables |
| 35 | + "users_roles", "roles_privileges"); |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private JdbcTemplate jdbcTemplate; |
| 39 | + |
| 40 | + @Test |
| 41 | + @DisplayName("should create all expected tables without errors") |
| 42 | + void shouldCreateAllExpectedTables() { |
| 43 | + List<String> tables = jdbcTemplate.queryForList( |
| 44 | + "SELECT LOWER(table_name) FROM INFORMATION_SCHEMA.TABLES WHERE LOWER(table_schema) = LOWER(?)", |
| 45 | + String.class, getSchemaName()); |
| 46 | + |
| 47 | + assertThat(tables) |
| 48 | + .as("All entity and join tables should be created by Hibernate on %s", getDatabaseName()) |
| 49 | + .containsAll(EXPECTED_TABLES); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @DisplayName("should create WebAuthn byte[] columns as BLOB-compatible types (not inline VARBINARY)") |
| 54 | + void shouldCreateWebAuthnBlobColumns() { |
| 55 | + List<String> blobColumns = List.of("public_key", "attestation_object", "attestation_client_data_json"); |
| 56 | + |
| 57 | + for (String column : blobColumns) { |
| 58 | + String dataType = jdbcTemplate.queryForObject( |
| 59 | + "SELECT LOWER(data_type) FROM INFORMATION_SCHEMA.COLUMNS " |
| 60 | + + "WHERE LOWER(table_schema) = LOWER(?) AND LOWER(table_name) = 'user_credentials' " |
| 61 | + + "AND LOWER(column_name) = ?", |
| 62 | + String.class, getSchemaName(), column); |
| 63 | + |
| 64 | + assertThat(dataType) |
| 65 | + .as("Column '%s' on %s should be a BLOB-compatible type, not VARBINARY", column, getDatabaseName()) |
| 66 | + .isIn(getAllowedBlobTypes()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the human-readable database name for assertion messages. |
| 72 | + */ |
| 73 | + protected abstract String getDatabaseName(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the schema name used in INFORMATION_SCHEMA queries. MariaDB/MySQL uses the database name as schema; |
| 77 | + * PostgreSQL uses 'public' by default. |
| 78 | + */ |
| 79 | + protected abstract String getSchemaName(); |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the set of column data types considered acceptable for BLOB columns on this database. |
| 83 | + */ |
| 84 | + protected abstract Set<String> getAllowedBlobTypes(); |
| 85 | +} |
0 commit comments