using JdeScoping.Core.Interfaces;
using JdeScoping.DataAccess.Exceptions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
using Xunit;
namespace JdeScoping.DataAccess.Tests;
///
/// Unit tests for DbConnectionFactory GIW connection support.
/// Connection strings are retrieved from ISecureStoreService.
///
public class DbConnectionFactoryGiwTests
{
private readonly ISecureStoreService _secureStore;
private readonly ILogger _logger;
public DbConnectionFactoryGiwTests()
{
_secureStore = Substitute.For();
_logger = Substitute.For>();
}
[Fact]
public async Task CreateGiwConnectionAsync_MissingConnectionString_ThrowsConnectionException()
{
// Arrange
_secureStore.Get("GIW").Returns((string?)null);
var factory = new DbConnectionFactory(_secureStore, _logger);
// Act & Assert
var ex = await Should.ThrowAsync(
async () => await factory.CreateGiwConnectionAsync());
ex.DataSource.ShouldBe("GIW");
ex.Message.ShouldContain("Connection string not found");
}
[Fact]
public async Task CreateGiwConnectionAsync_EmptyConnectionString_ThrowsConnectionException()
{
// Arrange
_secureStore.Get("GIW").Returns(string.Empty);
var factory = new DbConnectionFactory(_secureStore, _logger);
// Act & Assert
var ex = await Should.ThrowAsync(
async () => await factory.CreateGiwConnectionAsync());
ex.DataSource.ShouldBe("GIW");
ex.Message.ShouldContain("Connection string not found");
}
[Fact]
public async Task CreateGiwConnectionAsync_InvalidConnectionString_ThrowsConnectionException()
{
// Arrange
_secureStore.Get("GIW").Returns("Invalid oracle connection");
var factory = new DbConnectionFactory(_secureStore, _logger);
// Act & Assert
var ex = await Should.ThrowAsync(
async () => await factory.CreateGiwConnectionAsync());
ex.DataSource.ShouldBe("GIW");
ex.Message.ShouldContain("Failed to open connection");
ex.InnerException.ShouldNotBeNull();
}
[Fact]
public async Task CreateGiwConnectionAsync_CancellationRequested_ThrowsOperationCanceledException()
{
// Arrange
_secureStore.Get("GIW").Returns("User Id=test;Password=test;Data Source=test");
var factory = new DbConnectionFactory(_secureStore, _logger);
using var cts = new CancellationTokenSource();
cts.Cancel();
// Act & Assert
await Should.ThrowAsync(
async () => await factory.CreateGiwConnectionAsync(cts.Token));
}
}