DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ServicesTests/HelpPageServiceTest.cs

196 lines
5.6 KiB
C#
Raw Normal View History

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<HelpPageDto> HelpPages = new()
{
new()
{
Id = 123,
IdCategory = 20000,
Name = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1.pdf",
Size = 54000,
UrlPage = "test"
},
new()
{
Id = 134,
IdCategory = 20000,
Name = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2.pdf",
Size = 51000,
UrlPage = "test1"
},
new()
{
Id = 178,
IdCategory = 10000,
Name = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3.pdf",
Size = 49000,
UrlPage = "test2"
}
};
private readonly Mock<IHelpPageRepository> helpPageRepository = new();
private readonly Mock<IFileStorageRepository> 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<byte>());
helpPageRepository.Setup(x => x.InsertAsync(It.IsAny<HelpPageDto>(),
It.IsAny<CancellationToken>()))
.Returns(() => Task.FromResult(idHelpPage));
fileStorageRepository.Setup(x => x.SaveFileAsync(It.IsAny<string>(),
It.IsAny<Stream>(),
It.IsAny<CancellationToken>()));
//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<byte>());
bool isExistingHelpPage = true;
helpPageRepository.Setup(x => x.IsCheckHelpPageWithUrlPageAndIdCategoryAsync(It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<CancellationToken>()))
.Returns(() => Task.FromResult(isExistingHelpPage));
//act
Task Result () => helpPageService.CreateAsync(urlPage,
idCategory,
fullName,
fileStream,
CancellationToken.None);
//assert
await Assert.ThrowsAsync<ArgumentsInvalidException>(Result);
}
[Fact]
public async Task UpdateAsync_ShouldReturn_UpdatedHelpPage()
{
//arrange
HelpPageDto helpPage = new()
{
Id = 123,
IdCategory = 134,
UrlPage = "test",
Name = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.pdf",
Size = 54000
};
int newIdCategory = 451;
string newFileName = "<22><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.pdf";
MemoryStream newFileStream = new MemoryStream(Array.Empty<byte>());
//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<string>(),
It.IsAny<int>(), It.IsAny<CancellationToken>()))
.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<int>(),
It.IsAny<CancellationToken>()))
.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);
}
}