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_LoadsAndParsesSqlServerConnectionStringFromSecureStore() { // Arrange var model = new ConnectionStringsSection { Entries = new List { new ConnectionStringEntry { Name = "LotFinder" } } }; _secureStoreManager.IsStoreOpen.Returns(true); _secureStoreManager.GetSecret("LotFinder") .Returns("Server=localhost,1434;Database=ScopingTool;User Id=scopingapp;Password=pass;TrustServerCertificate=true"); // Act var sut = new ConnectionStringsFormViewModel(model, _secureStoreManager, () => { }, _dialogService, _connectionTestService); // Assert sut.Connections.Count.ShouldBe(1); sut.Connections[0].Provider.ShouldBe(ConnectionProvider.SqlServer); sut.Connections[0].Server.ShouldBe("localhost,1434"); sut.Connections[0].Database.ShouldBe("ScopingTool"); sut.Connections[0].UserId.ShouldBe("scopingapp"); sut.Connections[0].Password.ShouldBe("pass"); sut.Connections[0].TrustServerCertificate.ShouldBeTrue(); } [Fact] public void Constructor_LoadsAndParsesOracleConnectionStringFromSecureStore() { // Arrange var model = new ConnectionStringsSection { Entries = new List { new ConnectionStringEntry { Name = "CMS" } } }; _secureStoreManager.IsStoreOpen.Returns(true); _secureStoreManager.GetSecret("CMS") .Returns("HOST=ha-iman;Service Name=imanprd;Fetch Array Size=1280000;Port=1522;User ID=app_teamcenter;Password=pass;"); // Act var sut = new ConnectionStringsFormViewModel(model, _secureStoreManager, () => { }, _dialogService, _connectionTestService); // Assert sut.Connections.Count.ShouldBe(1); sut.Connections[0].Provider.ShouldBe(ConnectionProvider.Oracle); sut.Connections[0].Host.ShouldBe("ha-iman"); sut.Connections[0].ServiceName.ShouldBe("imanprd"); sut.Connections[0].Port.ShouldBe(1522); sut.Connections[0].UserId.ShouldBe("app_teamcenter"); sut.Connections[0].Password.ShouldBe("pass"); } [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); } }