using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; using Schaad.Accounting.Interfaces; using Schaad.Accounting.Repositories; using Shouldly; namespace Schaad.Accounting.Tests; public class XmlToJsonMigratorTestShould : IDisposable { // Shaped like a file written by the old XmlSerializer-based repository, including the // BOM-less declaration, namespaces and an xsi:nil FxRate. private const string TransactionsXml = """ t1 2026-01-30T00:00:00 2026-01-31T00:00:00 Gebäudeversicherung 114.20 a1 a2 t2 2026-02-01T00:00:00 2026-02-01T00:00:00 EUR Zahlung -50.5 a2 a1 0.9312 """; private const string AccountsXml = """ a1 1020 Bank CHF 1000.50 0 """; private readonly string dataPath; private readonly string dataDir; private readonly XmlToJsonMigrator sut = new(NullLogger.Instance); public XmlToJsonMigratorTestShould() { dataPath = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N")); dataDir = Path.Combine(dataPath, "2026", "Mandator", "Data"); Directory.CreateDirectory(dataDir); } public void Dispose() { if (Directory.Exists(dataPath)) { Directory.Delete(dataPath, recursive: true); } } [Fact] public async Task WriteJsonAndMoveXmlToBackupWhenDataFolderHasXml() { File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), TransactionsXml); var converted = await sut.MigrateAsync(dataPath); converted.ShouldBe(1); File.Exists(Path.Combine(dataDir, "Transactions.json")).ShouldBeTrue(); File.Exists(Path.Combine(dataDir, "Transactions.xml")).ShouldBeFalse(); var backup = Directory.GetFiles(Path.Combine(dataPath, "2026", "backup", "Mandator"), "Transactions.xml", SearchOption.AllDirectories); backup.Length.ShouldBe(1); File.ReadAllText(backup[0]).ShouldBe(TransactionsXml); } [Fact] public async Task WriteHumanReadableJsonWhenConvertingXml() { File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), TransactionsXml); await sut.MigrateAsync(dataPath); var json = File.ReadAllText(Path.Combine(dataDir, "Transactions.json")); json.ShouldContain("\"text\": \"Gebäudeversicherung\""); json.ShouldContain("\n \"id\": \"t1\""); json.ShouldNotContain("\"fxRate\": null"); json.ShouldNotContain("readOnly"); } [Fact] public async Task PreserveAllValuesWhenReadingMigratedDataThroughRepository() { File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), TransactionsXml); File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml); await sut.MigrateAsync(dataPath); var settingsService = Substitute.For(); settingsService.GetDbPath().Returns(dataDir); settingsService.GetLastYearDbPath().Returns(Path.Combine(dataPath, "no-such-dir")); var accountRepository = new AccountRepository(settingsService, new RepositoryCache(), NullLogger.Instance); var transactionRepository = new TransactionRepository(settingsService, new RepositoryCache(), accountRepository, NullLogger.Instance); var transactions = await transactionRepository.GetTransactionListAsync(); var accounts = await accountRepository.GetAccountListAsync(); transactions.Count.ShouldBe(2); transactions[0].Text.ShouldBe("Gebäudeversicherung"); transactions[0].Value.ShouldBe(114.20m); transactions[0].ValueDate.ShouldBe(new DateTime(2026, 1, 30)); transactions[0].BookingDate.ShouldBe(new DateTime(2026, 1, 31)); transactions[0].OriginAccountId.ShouldBe("a1"); transactions[0].TargetAccountId.ShouldBe("a2"); transactions[0].FxRate.ShouldBeNull(); transactions[1].Value.ShouldBe(-50.5m); transactions[1].FxRate.ShouldBe(0.9312m); accounts.Single().Number.ShouldBe(1020); accounts.Single().StartBalance.ShouldBe(1000.50m); } [Fact] public async Task LeaveUnknownXmlFilesUntouchedWhenMigrating() { var statement = Path.Combine(dataDir, "camt053.xml"); var upload = Path.Combine(dataPath, "2026", "Mandator", "uploads", "Accounts.xml"); Directory.CreateDirectory(Path.GetDirectoryName(upload)!); File.WriteAllText(statement, ""); File.WriteAllText(upload, ""); var converted = await sut.MigrateAsync(dataPath); converted.ShouldBe(0); File.Exists(statement).ShouldBeTrue(); File.Exists(upload).ShouldBeTrue(); } [Fact] public async Task SkipBackupFolderWhenScanningMandators() { var backupData = Path.Combine(dataPath, "2026", "backup", "Data"); Directory.CreateDirectory(backupData); File.WriteAllText(Path.Combine(backupData, "Accounts.xml"), AccountsXml); var converted = await sut.MigrateAsync(dataPath); converted.ShouldBe(0); File.Exists(Path.Combine(backupData, "Accounts.xml")).ShouldBeTrue(); } [Fact] public async Task DoNothingWhenAllDataIsAlreadyJson() { File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml); await sut.MigrateAsync(dataPath); var converted = await sut.MigrateAsync(dataPath); converted.ShouldBe(0); } [Fact] public async Task FinishMigrationWhenJsonAlreadyMatchesXml() { // Simulates a crash after the JSON was written but before the XML was moved. File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml); await sut.MigrateAsync(dataPath); File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml); var converted = await sut.MigrateAsync(dataPath); converted.ShouldBe(1); File.Exists(Path.Combine(dataDir, "Accounts.xml")).ShouldBeFalse(); } [Fact] public async Task ThrowAndKeepBothFilesWhenJsonExistsWithDifferentContent() { File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml); File.WriteAllText(Path.Combine(dataDir, "Accounts.json"), "[]"); await Should.ThrowAsync(() => sut.MigrateAsync(dataPath)); File.Exists(Path.Combine(dataDir, "Accounts.xml")).ShouldBeTrue(); File.ReadAllText(Path.Combine(dataDir, "Accounts.json")).ShouldBe("[]"); } [Fact] public async Task ConvertRemainingFilesAndThrowWhenOneXmlIsCorrupt() { File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml); File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), ""); await Should.ThrowAsync(() => sut.MigrateAsync(dataPath)); File.Exists(Path.Combine(dataDir, "Accounts.json")).ShouldBeTrue(); File.Exists(Path.Combine(dataDir, "Transactions.xml")).ShouldBeTrue(); File.Exists(Path.Combine(dataDir, "Transactions.json")).ShouldBeFalse(); } }