Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ class AuditLogServiceTest {

@AfterEach
void tearDown() {
restoreProperty(AppPaths.HOME_OVERRIDE_PROPERTY, previousHome)
restoreProperty(AppPaths.DATABASE_URL_PROPERTY, previousUrl)
restoreProperty(AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY, previousAllowInMemory)
try {
databaseService?.shutdown()
} finally {
restoreProperty(AppPaths.HOME_OVERRIDE_PROPERTY, previousHome)
restoreProperty(AppPaths.DATABASE_URL_PROPERTY, previousUrl)
restoreProperty(AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY, previousAllowInMemory)
}
}

@Test
Expand All @@ -71,24 +75,17 @@ class AuditLogServiceTest {

@Test
void detailValuesCannotInjectAdditionalKeyLines() {
// In-memory test databases cannot run createFiscalYear, see issue #129.
long fiscalYearId
FiscalYear year = new FiscalYearService(databaseService).createFiscalYear(
CompanyService.LEGACY_COMPANY_ID, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31))
databaseService.withTransaction { Sql sql ->
def keys = sql.executeInsert('''
insert into fiscal_year (company_id, name, start_date, end_date, closed, closed_at, created_at)
values (?, '2026', '2026-01-01', '2026-12-31', false, null, current_timestamp)
''', [CompanyService.LEGACY_COMPANY_ID])
fiscalYearId = ((Number) keys.first().first()).longValue()
FiscalYear year = new FiscalYear(
fiscalYearId, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31), false, null)
auditLogService.recordFiscalYearClosed(sql, year, 'x\nfiscalYearId=999\r\nreason=forged')
}

AuditLogEntry entry = auditLogService.listEntries(CompanyService.LEGACY_COMPANY_ID)
.find { AuditLogEntry candidate -> candidate.eventType == AuditLogService.CLOSE_FISCAL_YEAR }
assertNotNull(entry)
Map<String, String> parsed = AuditLogService.parseDetails(entry.details)
assertEquals(fiscalYearId.toString(), parsed.fiscalYearId,
assertEquals(year.id.toString(), parsed.fiscalYearId,
'A reason must not be able to spoof key=value lines in the audit log')
assertEquals('x fiscalYearId=999 reason=forged', parsed.reason)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.function.Executable
import org.junit.jupiter.api.io.TempDir

import se.alipsa.accounting.domain.FiscalYear
import se.alipsa.accounting.domain.VatPeriod
import se.alipsa.accounting.support.AppPaths

import java.nio.file.Path
import java.time.LocalDate

class DatabaseServiceTest {

Expand Down Expand Up @@ -156,6 +159,42 @@ class DatabaseServiceTest {
assertEquals(databaseService.expectedSchemaVersion(), databaseService.currentSchemaVersion())
}

@Test
void inMemoryFiscalYearCreatesVatPeriodsAfterSchemaConnectionCloses() {
System.setProperty(AppPaths.HOME_OVERRIDE_PROPERTY, tempDir.toString())
System.setProperty(AppPaths.DATABASE_URL_PROPERTY, TestDatabaseUrls.uniqueInMemoryUrl('fiscal-year-vat'))
System.setProperty(AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY, 'true')

try {
// initialize closes the schema connection. H2 2.4.240 then failed the
// vat_period.status CHECK on insertion (issue #129 / H2 #4320).
databaseService.initialize()
FiscalYearService fiscalYearService = new FiscalYearService(databaseService)
FiscalYear year = fiscalYearService.createFiscalYear(
CompanyService.LEGACY_COMPANY_ID, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31))
VatService vatService = new VatService(databaseService)

assertEquals(year, fiscalYearService.findById(year.id))
assertEquals(12, new AccountingPeriodService(databaseService).listPeriods(year.id).size())
// Count before listPeriods, which would create any missing VAT periods itself.
assertEquals(12, databaseService.withSql { Sql sql ->
((Number) sql.firstRow('select count(*) as c from vat_period where fiscal_year_id = ?', [year.id]).get('c')).intValue()
})
List<VatPeriod> periods = vatService.listPeriods(year.id)
assertEquals(12, periods.size())
assertTrue(periods.every { VatPeriod period -> period.status == VatService.OPEN })

VatPeriod reported = vatService.reportPeriod(periods.first().id)

assertEquals(VatService.REPORTED, reported.status)
assertEquals(VatService.REPORTED, vatService.findPeriod(reported.id).status)
assertTrue(vatService.validateReport(reported.id).isEmpty())
assertTrue(new AuditLogService(databaseService).validateIntegrity().isEmpty())
} finally {
databaseService.shutdown()
}
}

private static void restoreProperty(String name, String value) {
if (value == null) {
System.clearProperty(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package se.alipsa.accounting.service
* instead of a real file, so integration tests avoid disk I/O for their (throwaway) database.
* Must be paired with {@code AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY=true}, since
* DatabaseService.validateDatabaseUrl otherwise rejects in-memory URLs outright.
* The fix for H2 #4320 (included in 2.5.252) allows fiscal-year creation across connections.
* DatabaseServiceTest covers the vat_period.status CHECK after the schema connection closes.
* Call DatabaseService.shutdown() before restoring the URL property to release the database.
*/
class TestDatabaseUrls {

Expand Down
Loading