using System; using System.Collections.Generic; using System.Threading.Tasks; using LCU.API.Internal.RepoApi.Old_Repo_Exceptions; using LCU.API.Internal.RepoApi.Repos.Models; using LCU.API.Internal.RepoApi.Repos.Repositories; using LCU.API.Internal.RepoApi.Old_Repo_Services; using Moq; using Xunit; namespace LCU.API.Internal.RepoApiTests.ServiceTests { public class RepoCompanySelectionServiceTests { private readonly Mock _mockRepoCompanySelectionRepository; private readonly Mock _mockUnitOfWork; private readonly RepoCompanySelectionService _service; public RepoCompanySelectionServiceTests() { _mockRepoCompanySelectionRepository = new Mock(); _mockUnitOfWork = new Mock(); _service = new RepoCompanySelectionService(_mockRepoCompanySelectionRepository.Object, _mockUnitOfWork.Object); } [Fact] public async Task ListAllAsync_ShouldReturnAllRepoCompanySelections() { // Arrange var mockData = new List { new RepoCompanySelection { Id = 1}, new RepoCompanySelection { Id = 2} }; _mockRepoCompanySelectionRepository.Setup(repo => repo.ListAllAsync()).ReturnsAsync(mockData); // Act var result = await _service.ListAllAsync(); // Assert Assert.Equal(mockData, result); _mockRepoCompanySelectionRepository.Verify(repo => repo.ListAllAsync(), Times.Once); } [Fact] public async Task ListAllAsync_ShouldThrowServerErrorException_OnException() { // Arrange _mockRepoCompanySelectionRepository.Setup(repo => repo.ListAllAsync()).ThrowsAsync(new Exception()); // Act & Assert await Assert.ThrowsAsync(() => _service.ListAllAsync()); } [Fact] public async Task GetAsync_ShouldReturnRepoCompanySelection_WhenFound() { // Arrange var mockData = new RepoCompanySelection { Id = 1}; _mockRepoCompanySelectionRepository.Setup(repo => repo.FindByIdAsync(1)).ReturnsAsync(mockData); // Act var result = await _service.GetAsync(1); // Assert Assert.Equal(mockData, result); _mockRepoCompanySelectionRepository.Verify(repo => repo.FindByIdAsync(1), Times.Once); } [Fact] public async Task GetAsync_ShouldThrowNotFoundException_WhenNotFound() { // Arrange _mockRepoCompanySelectionRepository.Setup(repo => repo.FindByIdAsync(1)).ReturnsAsync((RepoCompanySelection)null); // Act & Assert var exception = await Assert.ThrowsAsync(() => _service.GetAsync(1)); Assert.Equal("Repo Company Selection with id 1 not found", exception.Message); } [Fact] public async Task GetAsync_ShouldThrowNotFoundException_OnRepositoryException() { // Arrange _mockRepoCompanySelectionRepository.Setup(repo => repo.FindByIdAsync(1)).ThrowsAsync(new Exception()); // Act & Assert var exception = await Assert.ThrowsAsync(() => _service.GetAsync(1)); Assert.Equal("Repo Company Selection with id 1 not found", exception.Message); } [Fact] public async Task SaveChangesAsync_ShouldSaveRepoCompanySelections() { // Arrange var mockData = new List { new RepoCompanySelection { Id = 1}, new RepoCompanySelection { Id = 2} }; // Act await _service.SaveChangesAsync(mockData); // Assert _mockRepoCompanySelectionRepository.Verify(repo => repo.SaveRange(mockData), Times.Once); _mockUnitOfWork.Verify(uow => uow.CompleteAsync(), Times.Once); } } }