using AutoMapper; using Diligent.WebAPI.Business.MappingProfiles; using Diligent.WebAPI.Business.Services; using Diligent.WebAPI.Business.Settings; using Diligent.WebAPI.Contracts.DTOs.Comment; using Diligent.WebAPI.Data.Entities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace Diligent.WebAPI.Tests.Services { public class CommentServiceTests { private readonly IMapper _mapper; private readonly List _comments; private readonly List _applicants; private readonly ILogger _logger = Substitute.For>(); private readonly List _users; public CommentServiceTests() { // mock data _applicants = MockData.GetListOfApplicants(); _comments = MockData.GetListOfComments(); _users = MockData.GetListOfUsers(); // configure mapper var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( new List { new CommentMappingProfile() })); _mapper = new Mapper(configuration); } [Fact] public async Task CreateComment_ShouldSendMailAndCreateComments_WhenThereIsUserToNotify() { var databaseContext = await Helpers.GetDatabaseContext(_comments); var frontSettings = Options.Create(new FrontEndSettings { BaseUrl = "some url" }); var mailer = Substitute.For(); mailer.When(x => x.SendEmailAsync(Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any())).Do(x => { }); CommentService commentService = new(frontSettings,databaseContext, _mapper, _logger, mailer); var commentCreateDto = new CommentCreateDto { ApplicantId = _applicants[0].ApplicantId, Content = "dsadasd", UserId = _users[0].Id, UsersToNotify = new List { "dzenis@dilig.net" } }; await commentService.CreateComment(commentCreateDto); var comments = await databaseContext.Comments.ToListAsync(); Assert.Equal(2, comments.Count); await mailer.Received().SendEmailAsync(Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any()); } [Fact] public async Task CreateComment_ShouldCreateComments_WhenThereIsNoUserToNotify() { var databaseContext = await Helpers.GetDatabaseContext(_comments); var frontSettings = Options.Create(new FrontEndSettings { BaseUrl = "some url" }); var mailer = Substitute.For(); mailer.When(x => x.SendEmailAsync(Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any())).Do(x => { }); CommentService commentService = new(frontSettings, databaseContext, _mapper, _logger, mailer); var commentCreateDto = new CommentCreateDto { ApplicantId = _applicants[0].ApplicantId, Content = "dsadasd", UserId = _users[0].Id, UsersToNotify = new List() }; await commentService.CreateComment(commentCreateDto); var comments = await databaseContext.Comments.ToListAsync(); Assert.Equal(2, comments.Count); await mailer.DidNotReceive().SendEmailAsync(Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any()); } } }