using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Data.WellSections; using AsbCloudApp.Exceptions; using AsbCloudApp.Services; using AsbCloudInfrastructure.Services.WellSections; using NSubstitute; using Xunit; namespace AsbCloudWebApi.Tests.UnitTests.Services.WellSections; public class WellSectionPlanServiceTests { private const int idWellSectionPlan = 1; private const int idWell = 3; private const int idWellSectionType = 54; private readonly IEnumerable fakePlanWellSections = new WellSectionPlanDto[] { new() { Id = idWellSectionPlan, IdWell = idWell, IdSectionType = idWellSectionType } }; private readonly IEnumerable fakeWellSectionTypes = new WellSectionTypeDto[] { new() { Id = idWellSectionType } }; private readonly IRepositoryWellRelated wellSectionPlanRepositoryMock = Substitute.For>(); private readonly ICrudRepository wellSectionTypeRepositoryMock = Substitute.For>(); private readonly WellSectionPlanService wellSectionPlanService; public WellSectionPlanServiceTests() { wellSectionPlanService = new WellSectionPlanService(wellSectionPlanRepositoryMock, wellSectionTypeRepositoryMock); wellSectionTypeRepositoryMock.GetAllAsync(Arg.Any()) .ReturnsForAnyArgs(fakeWellSectionTypes); } [Fact] public async Task InsertAsync_InsertNewWellSectionTypeWithUniqueSectionTypeInWell_InvokesWellSectionPlanRepositoryInsertAsync() { //arrange var insertedWellSection = new WellSectionPlanDto(); //act await wellSectionPlanService.InsertAsync(insertedWellSection, CancellationToken.None); //assert await wellSectionPlanRepositoryMock.Received().InsertAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task InsertAsync_InsertNewWellSectionTypeWithNotUniqueSectionTypeInWell_ReturnsDuplicateException() { //arrange var insertedWellSection = new WellSectionPlanDto { Id = 2, IdSectionType = idWellSectionType }; wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(fakePlanWellSections); //act Task Result() => wellSectionPlanService.InsertAsync(insertedWellSection, CancellationToken.None); //assert await Assert.ThrowsAsync(Result); } [Fact] public async Task UpdateAsync_UpdateExistingWellSectionTypeWithUniqueSectionTypeInWell_InvokesWellSectionPlanRepositoryUpdateAsync() { //arrange var updatedWellSection = new WellSectionPlanDto { Id = idWellSectionPlan, IdSectionType = idWellSectionType }; //act await wellSectionPlanService.UpdateAsync(updatedWellSection, CancellationToken.None); //assert await wellSectionPlanRepositoryMock.Received().UpdateAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task UpdateAsync_ReturnsLastUpdateDateNotNull() { //arrange var updatedWellSection = new WellSectionPlanDto { Id = idWellSectionPlan, IdSectionType = idWellSectionType }; //act await wellSectionPlanService.UpdateAsync(updatedWellSection, CancellationToken.None); //assert Assert.NotNull(updatedWellSection.LastUpdateDate); } [Fact] public async Task UpdateAsync_UpdateExistingWellSectionTypeWithNotUniqueSectionTypeInWell_ReturnsDuplicateException() { //arrange var updatedWellSection = new WellSectionPlanDto { Id = 2, IdSectionType = idWellSectionType }; wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(fakePlanWellSections); //act Task Result() => wellSectionPlanService.UpdateAsync(updatedWellSection, CancellationToken.None); //assert await Assert.ThrowsAsync(Result); } [Fact] public async Task GetWellSectionTypesAsync_EmptyCollectionWellSectionPlan_ReturnsCollectionWellSectionType() { //arrange wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(Enumerable.Empty()); //act var result = await wellSectionPlanService.GetWellSectionTypesAsync(idWell, CancellationToken.None); //assert Assert.Single(result); } [Fact] public async Task GetWellSectionTypesAsync_NotEmptyCollectionWellSectionPlan_ReturnsCollectionWellSectionType() { //arrange wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(fakePlanWellSections); //act var result = await wellSectionPlanService.GetWellSectionTypesAsync(idWell, CancellationToken.None); //assert Assert.Single(result); } }