using JdeScoping.ConfigManager.Models; using JdeScoping.ConfigManager.Services; using JdeScoping.ConfigManager.Services.SecureStore; using JdeScoping.ConfigManager.ViewModels.Forms; namespace JdeScoping.ConfigManager.Tests.ViewModels.Forms; public class ConnectionStringsFormViewModelTests { private readonly ISecureStoreManager _secureStoreManager; private readonly IDialogService _dialogService; private readonly IConnectionTestService _connectionTestService; public ConnectionStringsFormViewModelTests() { _secureStoreManager = Substitute.For(); _dialogService = Substitute.For(); _connectionTestService = Substitute.For(); // Setup default behavior - SecureStore is not open by default in tests _secureStoreManager.IsStoreOpen.Returns(false); } [Fact] public void Constructor_InitializesFromModel() { // Arrange var model = new ConnectionStringsSection { Entries = new List { new ConnectionStringEntry { Name = "Connection1", Provider = ConnectionProvider.SqlServer, Server = "server1" }, new ConnectionStringEntry { Name = "Connection2", Provider = ConnectionProvider.Oracle, Host = "oracle-host" } } }; // Act var sut = new ConnectionStringsFormViewModel(model, _secureStoreManager, () => { }, _dialogService, _connectionTestService); // Assert sut.Connections.Count.ShouldBe(2); sut.Connections[0].Name.ShouldBe("Connection1"); sut.Connections[0].Provider.ShouldBe(ConnectionProvider.SqlServer); sut.Connections[0].Server.ShouldBe("server1"); sut.Connections[1].Name.ShouldBe("Connection2"); sut.Connections[1].Provider.ShouldBe(ConnectionProvider.Oracle); sut.Connections[1].Host.ShouldBe("oracle-host"); } [Fact] public void Constructor_ThrowsOnNullModel() { // Act & Assert Should.Throw(() => new ConnectionStringsFormViewModel(null!, _secureStoreManager, () => { }, _dialogService, _connectionTestService)); } [Fact] public void Constructor_ThrowsOnNullSecureStoreManager() { // Arrange var model = new ConnectionStringsSection(); // Act & Assert Should.Throw(() => new ConnectionStringsFormViewModel(model, null!, () => { }, _dialogService, _connectionTestService)); } [Fact] public void Constructor_ThrowsOnNullOnChanged() { // Arrange var model = new ConnectionStringsSection(); // Act & Assert Should.Throw(() => new ConnectionStringsFormViewModel(model, _secureStoreManager, null!, _dialogService, _connectionTestService)); } [Fact] public void Constructor_ThrowsOnNullConnectionTestService() { // Arrange var model = new ConnectionStringsSection(); // Act & Assert Should.Throw(() => new ConnectionStringsFormViewModel(model, _secureStoreManager, () => { }, _dialogService, null!)); } [Fact] public void AddConnection_CreatesNewEntryAndSelectsIt() { // Arrange var model = new ConnectionStringsSection(); var changedInvoked = false; var sut = new ConnectionStringsFormViewModel(model, _secureStoreManager, () => changedInvoked = true, _dialogService, _connectionTestService); // Act sut.AddConnectionCommand.Execute(null); // Assert sut.Connections.Count.ShouldBe(1); sut.Connections[0].Name.ShouldBe("NewConnection"); sut.SelectedConnection.ShouldBe(sut.Connections[0]); model.Entries.Count.ShouldBe(1); changedInvoked.ShouldBeTrue(); } [Fact] public void HasSelection_IsFalseWhenNothingSelected() { // Arrange var model = new ConnectionStringsSection { Entries = new List { new ConnectionStringEntry { Name = "Conn1" } } }; var sut = new ConnectionStringsFormViewModel(model, _secureStoreManager, () => { }, _dialogService, _connectionTestService); // Assert - no selection by default sut.SelectedConnection.ShouldBeNull(); sut.HasSelection.ShouldBeFalse(); } [Fact] public void HasSelection_IsTrueWhenConnectionSelected() { // Arrange var model = new ConnectionStringsSection { Entries = new List { new ConnectionStringEntry { Name = "Conn1" } } }; var sut = new ConnectionStringsFormViewModel(model, _secureStoreManager, () => { }, _dialogService, _connectionTestService); // Act sut.SelectedConnection = sut.Connections[0]; // Assert sut.HasSelection.ShouldBeTrue(); } [Fact] public void ConnectionCount_ReflectsCollectionSize() { // Arrange var model = new ConnectionStringsSection { Entries = new List { new ConnectionStringEntry { Name = "Conn1" }, new ConnectionStringEntry { Name = "Conn2" }, new ConnectionStringEntry { Name = "Conn3" } } }; // Act var sut = new ConnectionStringsFormViewModel(model, _secureStoreManager, () => { }, _dialogService, _connectionTestService); // Assert sut.ConnectionCount.ShouldBe(3); // Act - add another sut.AddConnectionCommand.Execute(null); // Assert sut.ConnectionCount.ShouldBe(4); } }