using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudInfrastructure.Services; using Moq; using Xunit; namespace AsbCloudWebApi.Tests.ServicesTests; public class HelpPageServiceTest { private const string directoryNameHelpPageFiles = "helpPages"; private static List HelpPages = new() { new() { Id = 123, IdCategory = 20000, Name = "Справка1.pdf", Size = 54000, UrlPage = "test" }, new() { Id = 134, IdCategory = 20000, Name = "Справка2.pdf", Size = 51000, UrlPage = "test1" }, new() { Id = 178, IdCategory = 10000, Name = "Справка3.pdf", Size = 49000, UrlPage = "test2" } }; private readonly Mock helpPageRepository = new(); private readonly Mock fileStorageRepository = new(); private readonly IHelpPageService helpPageService; public HelpPageServiceTest() { helpPageService = new HelpPageService(helpPageRepository.Object, fileStorageRepository.Object, directoryNameHelpPageFiles); } [Fact] public async Task CreateAsync_ShouldReturn_PositiveId() { //arrange int idHelpPage = new Random().Next(1, 100); string urlPage = "test"; int idCategory = 20000; string fullName = "test.pdf"; MemoryStream fileStream = new MemoryStream(Array.Empty()); helpPageRepository.Setup(x => x.InsertAsync(It.IsAny(), It.IsAny())) .Returns(() => Task.FromResult(idHelpPage)); fileStorageRepository.Setup(x => x.SaveFileAsync(It.IsAny(), It.IsAny(), It.IsAny())); //act int result = await helpPageService.CreateAsync(urlPage, idCategory, fullName, fileStream, CancellationToken.None); //assert Assert.True(result > 0); } [Fact] public async Task CreateAsync_ShouldReturn_ArgumentsInvalidException() { //arrange string urlPage = "test"; int idCategory = 20000; string fullName = "test.pdf"; MemoryStream fileStream = new MemoryStream(Array.Empty()); bool isExistingHelpPage = true; helpPageRepository.Setup(x => x.IsCheckHelpPageWithUrlPageAndIdCategoryAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(() => Task.FromResult(isExistingHelpPage)); //act Task Result () => helpPageService.CreateAsync(urlPage, idCategory, fullName, fileStream, CancellationToken.None); //assert await Assert.ThrowsAsync(Result); } [Fact] public async Task UpdateAsync_ShouldReturn_UpdatedHelpPage() { //arrange HelpPageDto helpPage = new() { Id = 123, IdCategory = 134, UrlPage = "test", Name = "Справка.pdf", Size = 54000 }; int newIdCategory = 451; string newFileName = "Новая справка.pdf"; MemoryStream newFileStream = new MemoryStream(Array.Empty()); //act await helpPageService.UpdateAsync(helpPage, newIdCategory, newFileName, newFileStream, CancellationToken.None); //assert Assert.Equal(newFileName, helpPage.Name); Assert.Equal(newIdCategory, helpPage.IdCategory); Assert.Equal(newFileStream.Length, helpPage.Size); } [Theory] [InlineData(20000, "test")] [InlineData(20000, "test1")] public async Task GetOrDefaultByUrlPageAndIdCategoryAsync_ShouldReturn_HelpPageDto(int idCategory, string urlPage) { //arrange helpPageRepository.Setup(x => x.GetOrDefaultByUrlPageAndIdCategoryAsync(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(() => { var helpPage = HelpPages.FirstOrDefault(x => x.UrlPage == urlPage && x.IdCategory == idCategory); return Task.FromResult(helpPage); }); //act var result = await helpPageService.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage, idCategory, CancellationToken.None); //assert Assert.NotNull(result); } [Theory] [InlineData(123)] [InlineData(178)] public async Task GetOrDefaultByIdAsync_ShouldReturn_HelpPageDto(int id) { //arrange helpPageRepository.Setup(x => x.GetOrDefaultAsync(It.IsAny(), It.IsAny())) .Returns(() => { var helpPage = HelpPages.FirstOrDefault(x => x.Id == id); return Task.FromResult(helpPage); }); //act var result = await helpPageService.GetOrDefaultByIdAsync(id, CancellationToken.None); //assert Assert.NotNull(result); } }