forked from ddrilling/AsbCloudServer
Справки по страницам
1. Поправил тесты 2. Поправил сервис, репозиторий и контроллер. Сделал небольшой рефакторинг
This commit is contained in:
parent
4b13d1c203
commit
4767335900
@ -20,15 +20,4 @@ public interface IHelpPageRepository : ICrudRepository<HelpPageDto>
|
||||
Task<HelpPageDto?> GetOrDefaultByUrlPageAndIdCategoryAsync(string key,
|
||||
int idCategory,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Проверяет наличие справки для страницы
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="idCategory"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> IsExistingAsync(string key,
|
||||
int idCategory,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
|
@ -31,9 +31,4 @@ public class HelpPageRepository : CrudRepositoryBase<HelpPageDto, HelpPage>,
|
||||
|
||||
return helpPage.Adapt<HelpPageDto>();
|
||||
}
|
||||
|
||||
public Task<bool> IsExistingAsync(string key, int idCategory, CancellationToken cancellationToken) =>
|
||||
dbContext.HelpPages.AnyAsync(h => h.UrlPage == key &&
|
||||
h.IdCategory == idCategory,
|
||||
cancellationToken);
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using AsbCloudApp.Exceptions;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services;
|
||||
|
||||
@ -82,7 +81,6 @@ public class HelpPageService : IHelpPageService
|
||||
/// <param name="idCategory"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotFoundException"></exception>
|
||||
public async Task<(Stream stream, string fileName)?> GetFileStreamAsync(string pageKey,
|
||||
int idCategory,
|
||||
CancellationToken cancellationToken)
|
||||
|
@ -1,145 +0,0 @@
|
||||
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;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace AsbCloudWebApi.Tests.ServicesTests;
|
||||
|
||||
public class HelpPageServiceTest
|
||||
{
|
||||
private static Dictionary<string, string> configSettings = new (){
|
||||
{"DirectoryNameHelpPageFiles", "helpPages"}
|
||||
};
|
||||
|
||||
private static List<HelpPageDto> 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<IHelpPageRepository> helpPageRepository = new();
|
||||
private readonly Mock<IFileStorageRepository> fileStorageRepository = new();
|
||||
|
||||
private readonly IHelpPageService helpPageService;
|
||||
|
||||
public HelpPageServiceTest()
|
||||
{
|
||||
IConfiguration configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(configSettings)
|
||||
.Build();
|
||||
|
||||
helpPageService = new HelpPageService(helpPageRepository.Object,
|
||||
fileStorageRepository.Object,
|
||||
configuration);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOrUpdateAsync_ShouldReturn_NewHelpPage()
|
||||
{
|
||||
//arrange
|
||||
int idHelpPage = new Random().Next(1, 100);
|
||||
string urlPage = "test";
|
||||
int idCategory = 20000;
|
||||
string fileName = "test.pdf";
|
||||
MemoryStream fileStream = new MemoryStream(Array.Empty<byte>());
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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.AddOrUpdateAsync(urlPage,
|
||||
idCategory,
|
||||
fileName,
|
||||
fileStream,
|
||||
CancellationToken.None);
|
||||
|
||||
//assert
|
||||
Assert.True(result > 0);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_ShouldReturn_UpdatedHelpPage()
|
||||
{
|
||||
//arrange
|
||||
int idHelpPage = new Random().Next(1, 100);
|
||||
string urlPage = "test";
|
||||
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>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(() => Task.FromResult(idHelpPage));
|
||||
|
||||
fileStorageRepository.Setup(x => x.SaveFileAsync(It.IsAny<string>(),
|
||||
It.IsAny<Stream>(),
|
||||
It.IsAny<CancellationToken>()));
|
||||
|
||||
//act
|
||||
await helpPageService.AddOrUpdateAsync(urlPage,
|
||||
newIdCategory,
|
||||
newFileName,
|
||||
newFileStream,
|
||||
CancellationToken.None);
|
||||
|
||||
//assert
|
||||
Assert.Equal(newFileName, existingHelpPage.Name);
|
||||
Assert.Equal(newIdCategory, existingHelpPage.IdCategory);
|
||||
Assert.Equal(newFileStream.Length, existingHelpPage.Size);
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace AsbCloudWebApi.Tests.UnitTests.Services;
|
||||
|
||||
public class HelpPageServiceTest
|
||||
{
|
||||
private const string urlPage = "test";
|
||||
private const string fileName = "Справка_для_страницы_test.pdf";
|
||||
private const int idCategory = 20000;
|
||||
|
||||
private static Dictionary<string, string> configSettings = new()
|
||||
{
|
||||
{ "DirectoryNameHelpPageFiles", "helpPages" }
|
||||
};
|
||||
|
||||
private static readonly MemoryStream fileStream = new(Array.Empty<byte>());
|
||||
|
||||
private static readonly HelpPageDto existingHelpPage = new()
|
||||
{
|
||||
Id = 178,
|
||||
IdCategory = idCategory,
|
||||
UrlPage = "test2",
|
||||
Name = "Справка_для_страницы_test2.pdf"
|
||||
};
|
||||
|
||||
private readonly IHelpPageRepository helpPageRepositoryMock = Substitute.For<IHelpPageRepository>();
|
||||
private readonly IFileStorageRepository fileStorageRepositoryMock = Substitute.For<IFileStorageRepository>();
|
||||
|
||||
private readonly HelpPageService helpPageService;
|
||||
|
||||
public HelpPageServiceTest()
|
||||
{
|
||||
IConfiguration configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(configSettings)
|
||||
.Build();
|
||||
|
||||
helpPageRepositoryMock.GetOrDefaultByUrlPageAndIdCategoryAsync(existingHelpPage.UrlPage, existingHelpPage.IdCategory, Arg.Any<CancellationToken>())
|
||||
.Returns(existingHelpPage);
|
||||
|
||||
helpPageRepositoryMock.InsertAsync(Arg.Any<HelpPageDto>(), Arg.Any<CancellationToken>())
|
||||
.ReturnsForAnyArgs(1);
|
||||
|
||||
fileStorageRepositoryMock.SaveFileAsync(Arg.Any<string>(), Arg.Any<Stream>(), Arg.Any<CancellationToken>());
|
||||
|
||||
helpPageService = new HelpPageService(helpPageRepositoryMock,
|
||||
fileStorageRepositoryMock,
|
||||
configuration);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOrUpdateAsync_ShouldReturn_AddedHelpPage()
|
||||
{
|
||||
//act
|
||||
var result = await helpPageService.AddOrUpdateAsync(urlPage,
|
||||
idCategory,
|
||||
fileName,
|
||||
fileStream,
|
||||
CancellationToken.None);
|
||||
|
||||
//assert
|
||||
await helpPageRepositoryMock.Received().InsertAsync(Arg.Any<HelpPageDto>(), Arg.Any<CancellationToken>());
|
||||
await fileStorageRepositoryMock.Received().SaveFileAsync(Arg.Any<string>(), Arg.Any<Stream>(), Arg.Any<CancellationToken>());
|
||||
|
||||
Assert.NotEqual(existingHelpPage.Id, result);
|
||||
Assert.NotEqual(urlPage, existingHelpPage.UrlPage);
|
||||
Assert.NotEqual(fileName, existingHelpPage.Name);
|
||||
Assert.Equal(idCategory, existingHelpPage.IdCategory);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_ShouldReturn_UpdatedHelpPage()
|
||||
{
|
||||
//act
|
||||
var result = await helpPageService.AddOrUpdateAsync(existingHelpPage.UrlPage,
|
||||
existingHelpPage.IdCategory,
|
||||
existingHelpPage.Name,
|
||||
fileStream,
|
||||
CancellationToken.None);
|
||||
|
||||
//assert
|
||||
await helpPageRepositoryMock.Received().UpdateAsync(Arg.Any<HelpPageDto>(), Arg.Any<CancellationToken>());
|
||||
await fileStorageRepositoryMock.Received().SaveFileAsync(Arg.Any<string>(), Arg.Any<Stream>(), Arg.Any<CancellationToken>());
|
||||
|
||||
Assert.Equal(existingHelpPage.Id, result);
|
||||
}
|
||||
}
|
@ -94,26 +94,4 @@ public class HelpPageController : ControllerBase
|
||||
|
||||
return File(file.Value.stream, "application/pdf", file.Value.fileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проверяет наличие справки для страницы
|
||||
/// </summary>
|
||||
/// <param name="key">Ключ страницы</param>
|
||||
/// <param name="idCategory">Id категории файла. Допустимое значение параметра: 20000</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("isExisting")]
|
||||
[ProducesResponseType(typeof(bool), (int)HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> IsExistingAsync(
|
||||
[Required] string key,
|
||||
[Range(minimum: 20000, maximum: 20000, ErrorMessage = "Категория файла недопустима. Допустимые: 20000")]
|
||||
int idCategory,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var helpPage = await helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(key,
|
||||
idCategory,
|
||||
cancellationToken);
|
||||
|
||||
return Ok(helpPage != null);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user