using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using ZB.MOM.WW.LmxOpcUa.Host.Domain; namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers { /// /// In-memory Galaxy repository used by tests to control hierarchy rows, attribute rows, and deploy metadata without SQL access. /// public class FakeGalaxyRepository : IGalaxyRepository { /// /// Occurs when the fake repository simulates a Galaxy deploy change. /// public event Action? OnGalaxyChanged; /// /// Gets or sets the hierarchy rows returned to address-space construction logic. /// public List Hierarchy { get; set; } = new List(); /// /// Gets or sets the attribute rows returned to address-space construction logic. /// public List Attributes { get; set; } = new List(); /// /// Gets or sets the deploy timestamp returned to change-detection logic. /// public DateTime? LastDeployTime { get; set; } = DateTime.UtcNow; /// /// Gets or sets a value indicating whether connection checks should report success. /// public bool ConnectionSucceeds { get; set; } = true; /// /// Gets or sets a value indicating whether repository calls should throw to simulate database failures. /// public bool ShouldThrow { get; set; } /// /// Returns the configured hierarchy rows or throws to simulate a repository failure. /// /// A cancellation token ignored by the in-memory fake. /// The configured hierarchy rows. public Task> GetHierarchyAsync(CancellationToken ct = default) { if (ShouldThrow) throw new Exception("Simulated DB failure"); return Task.FromResult(Hierarchy); } /// /// Returns the configured attribute rows or throws to simulate a repository failure. /// /// A cancellation token ignored by the in-memory fake. /// The configured attribute rows. public Task> GetAttributesAsync(CancellationToken ct = default) { if (ShouldThrow) throw new Exception("Simulated DB failure"); return Task.FromResult(Attributes); } /// /// Returns the configured deploy timestamp or throws to simulate a repository failure. /// /// A cancellation token ignored by the in-memory fake. /// The configured deploy timestamp. public Task GetLastDeployTimeAsync(CancellationToken ct = default) { if (ShouldThrow) throw new Exception("Simulated DB failure"); return Task.FromResult(LastDeployTime); } /// /// Returns the configured connection result or throws to simulate a repository failure. /// /// A cancellation token ignored by the in-memory fake. /// The configured connection result. public Task TestConnectionAsync(CancellationToken ct = default) { if (ShouldThrow) throw new Exception("Simulated DB failure"); return Task.FromResult(ConnectionSucceeds); } /// /// Raises the deploy-change event so tests can trigger rebuild logic. /// public void RaiseGalaxyChanged() => OnGalaxyChanged?.Invoke(); } }