using e_suite.API.Common.exceptions;
using e_suite.Database.Core.Tables.Domain;
using e_suite.Modules.DomainManager.UnitTests.Helpers;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;

namespace e_suite.Modules.DomainManager.UnitTests;

[TestFixture]
public class GetDomainAsyncUnitTests : DomainManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public void GetDomainAsync_DomainNotExisting_ThrowsNotFoundException()
    {
        //Arrange
        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("8d5c8114-ad04-492c-a5d3-271584763126")
        };


        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var result = await DomainManager.GetDomainAsync(generalIdRef, CancellationToken.None);
        });

        Assert.That(actualResult!.Message, Is.EqualTo("Domain not found"));
    }

    [Test]
    public async Task GetDomainAsync_WhenDomainFound_ReturnsDomain()
    {
        //Arrange
        var domain = new Domain
        {
            Guid = new Guid("a618a2a8-ea9f-403b-9730-cc73ba12d8b3"),
            Name = "Testing domain"
        };

        DomainManagerRepository.Domains.Add(domain);

        var generalIdRef = new GeneralIdRef
        {
            Guid = domain.Guid,
        };
        
        //Act
        var result = await DomainManager.GetDomainAsync(generalIdRef, CancellationToken.None);
        
        //Assert
        Assert.That( result, Is.Not.Null);
        Assert.That( result.Guid, Is.EqualTo(domain.Guid));
    }
}