2023-06-28 16:33:27 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudInfrastructure.Services;
|
2023-06-29 16:31:37 +05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-06-28 16:33:27 +05:00
|
|
|
|
using Moq;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Tests.ServicesTests;
|
|
|
|
|
|
|
|
|
|
public class HelpPageServiceTest
|
|
|
|
|
{
|
2023-06-29 16:31:37 +05:00
|
|
|
|
private static Dictionary<string, string> configSettings = new (){
|
|
|
|
|
{"DirectoryNameHelpPageFiles", "helpPages"}
|
|
|
|
|
};
|
2023-06-28 16:33:27 +05:00
|
|
|
|
|
|
|
|
|
private static List<HelpPageDto> HelpPages = new()
|
|
|
|
|
{
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
Id = 123,
|
|
|
|
|
IdCategory = 20000,
|
2023-06-28 17:09:53 +05:00
|
|
|
|
Name = "Справка1.pdf",
|
2023-06-28 16:33:27 +05:00
|
|
|
|
Size = 54000,
|
|
|
|
|
UrlPage = "test"
|
|
|
|
|
},
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
Id = 134,
|
|
|
|
|
IdCategory = 20000,
|
2023-06-28 17:09:53 +05:00
|
|
|
|
Name = "Справка2.pdf",
|
2023-06-28 16:33:27 +05:00
|
|
|
|
Size = 51000,
|
|
|
|
|
UrlPage = "test1"
|
|
|
|
|
},
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
Id = 178,
|
|
|
|
|
IdCategory = 10000,
|
2023-06-28 17:09:53 +05:00
|
|
|
|
Name = "Справка3.pdf",
|
2023-06-28 16:33:27 +05:00
|
|
|
|
Size = 49000,
|
|
|
|
|
UrlPage = "test2"
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-06-29 16:31:37 +05:00
|
|
|
|
|
2023-06-28 16:33:27 +05:00
|
|
|
|
private readonly Mock<IHelpPageRepository> helpPageRepository = new();
|
|
|
|
|
private readonly Mock<IFileStorageRepository> fileStorageRepository = new();
|
|
|
|
|
|
|
|
|
|
private readonly IHelpPageService helpPageService;
|
|
|
|
|
|
|
|
|
|
public HelpPageServiceTest()
|
|
|
|
|
{
|
2023-06-29 16:31:37 +05:00
|
|
|
|
IConfiguration configuration = new ConfigurationBuilder()
|
|
|
|
|
.AddInMemoryCollection(configSettings)
|
|
|
|
|
.Build();
|
|
|
|
|
|
2023-06-28 16:33:27 +05:00
|
|
|
|
helpPageService = new HelpPageService(helpPageRepository.Object,
|
|
|
|
|
fileStorageRepository.Object,
|
2023-06-29 16:31:37 +05:00
|
|
|
|
configuration);
|
2023-06-28 16:33:27 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2023-06-29 16:31:37 +05:00
|
|
|
|
public async Task AddOrUpdateAsync_ShouldReturn_NewHelpPage()
|
2023-06-28 16:33:27 +05:00
|
|
|
|
{
|
|
|
|
|
//arrange
|
|
|
|
|
int idHelpPage = new Random().Next(1, 100);
|
|
|
|
|
string urlPage = "test";
|
|
|
|
|
int idCategory = 20000;
|
2023-06-29 16:31:37 +05:00
|
|
|
|
string fileName = "test.pdf";
|
2023-06-28 16:33:27 +05:00
|
|
|
|
MemoryStream fileStream = new MemoryStream(Array.Empty<byte>());
|
|
|
|
|
|
2023-06-29 16:31:37 +05:00
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-28 16:33:27 +05:00
|
|
|
|
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
|
2023-06-29 16:31:37 +05:00
|
|
|
|
int result = await helpPageService.AddOrUpdateAsync(urlPage,
|
2023-06-28 16:33:27 +05:00
|
|
|
|
idCategory,
|
2023-06-29 16:31:37 +05:00
|
|
|
|
fileName,
|
2023-06-28 16:33:27 +05:00
|
|
|
|
fileStream,
|
|
|
|
|
CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
//assert
|
|
|
|
|
Assert.True(result > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 16:31:37 +05:00
|
|
|
|
|
2023-06-28 16:33:27 +05:00
|
|
|
|
[Fact]
|
2023-06-29 16:31:37 +05:00
|
|
|
|
public async Task UpdateAsync_ShouldReturn_UpdatedHelpPage()
|
2023-06-28 16:33:27 +05:00
|
|
|
|
{
|
|
|
|
|
//arrange
|
2023-06-29 16:31:37 +05:00
|
|
|
|
int idHelpPage = new Random().Next(1, 100);
|
2023-06-28 16:33:27 +05:00
|
|
|
|
string urlPage = "test";
|
2023-06-29 16:31:37 +05:00
|
|
|
|
int newIdCategory = 20000;
|
|
|
|
|
string newFileName = "test.pdf";
|
|
|
|
|
MemoryStream newFileStream = new MemoryStream(Array.Empty<byte>());
|
|
|
|
|
HelpPageDto existingHelpPage = HelpPages.First(x =>
|
|
|
|
|
x.UrlPage == urlPage &&
|
|
|
|
|
x.IdCategory == newIdCategory);
|
|
|
|
|
|
|
|
|
|
helpPageRepository.Setup(x => x.GetOrDefaultByUrlPageAndIdCategoryAsync(It.IsAny<string>(),
|
|
|
|
|
It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
|
|
|
|
.Returns(() => Task.FromResult(existingHelpPage)!);
|
|
|
|
|
|
|
|
|
|
helpPageRepository.Setup(x => x.InsertAsync(It.IsAny<HelpPageDto>(),
|
2023-06-28 16:33:27 +05:00
|
|
|
|
It.IsAny<CancellationToken>()))
|
2023-06-29 16:31:37 +05:00
|
|
|
|
.Returns(() => Task.FromResult(idHelpPage));
|
2023-06-28 16:33:27 +05:00
|
|
|
|
|
2023-06-29 16:31:37 +05:00
|
|
|
|
fileStorageRepository.Setup(x => x.SaveFileAsync(It.IsAny<string>(),
|
|
|
|
|
It.IsAny<Stream>(),
|
|
|
|
|
It.IsAny<CancellationToken>()));
|
2023-06-28 16:33:27 +05:00
|
|
|
|
|
|
|
|
|
//act
|
2023-06-29 16:31:37 +05:00
|
|
|
|
await helpPageService.AddOrUpdateAsync(urlPage,
|
2023-06-28 16:33:27 +05:00
|
|
|
|
newIdCategory,
|
|
|
|
|
newFileName,
|
|
|
|
|
newFileStream,
|
|
|
|
|
CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
//assert
|
2023-06-29 16:31:37 +05:00
|
|
|
|
Assert.Equal(newFileName, existingHelpPage.Name);
|
|
|
|
|
Assert.Equal(newIdCategory, existingHelpPage.IdCategory);
|
|
|
|
|
Assert.Equal(newFileStream.Length, existingHelpPage.Size);
|
2023-06-28 16:33:27 +05:00
|
|
|
|
}
|
|
|
|
|
}
|