Лаба 2
This commit is contained in:
parent
ca76528108
commit
89712b1714
@ -1,38 +1,77 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Squirrel.BusinessLogicsContracts;
|
using Squirrel.BusinessLogicsContracts;
|
||||||
using Squirrel.DataModels;
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.Extensions;
|
||||||
using Squirrel.StoragesContracts;
|
using Squirrel.StoragesContracts;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SquirelBusinessLogic.Implementations;
|
namespace SquirelBusinessLogic.Implementations;
|
||||||
|
|
||||||
internal class GuestBusinessLogicContract(IGuestStorageContract
|
internal class GuestBusinessLogicContract(IGuestStorageContract guestStorageContract, ILogger logger) : IGuestBusinessLogicContract
|
||||||
guestStorageContract, ILogger logger) : IGuestBusinessLogicContract
|
|
||||||
|
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger = logger;
|
private readonly ILogger _logger = logger;
|
||||||
private readonly IGuestStorageContract _guestStorageContract =
|
private readonly IGuestStorageContract _guestStorageContract = guestStorageContract;
|
||||||
guestStorageContract;
|
|
||||||
public List<GuestDataModel> GetAllGuests()
|
public List<GuestDataModel> GetAllGuests()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllGuests");
|
||||||
|
|
||||||
|
return _guestStorageContract.GetList() ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public GuestDataModel GetGuestByData(string data)
|
public GuestDataModel GetGuestByData(string data)
|
||||||
{
|
{
|
||||||
return new("", "", "", 0);
|
_logger.LogInformation("Get element by data: {data}", data);
|
||||||
|
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.IsGuid())
|
||||||
|
{
|
||||||
|
return _guestStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Regex.IsMatch(data, @"^((8|\+7)[\-]?)?(\(?\d{3}\)?[\-]?)?[\d]{7,10}$"))
|
||||||
|
{
|
||||||
|
return _guestStorageContract.GetElementByPhoneNumber(data) ??
|
||||||
|
throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _guestStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
|
||||||
}
|
}
|
||||||
public void InsertGuest(GuestDataModel guestDataModel)
|
public void InsertGuest(GuestDataModel guestDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(guestDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(guestDataModel); guestDataModel.Validate();
|
||||||
|
_guestStorageContract.AddElement(guestDataModel);
|
||||||
}
|
}
|
||||||
public void UpdateGuest(GuestDataModel guestDataModel)
|
public void UpdateGuest(GuestDataModel guestDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(guestDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(guestDataModel);
|
||||||
|
guestDataModel.Validate();
|
||||||
|
_guestStorageContract.UpdElement(guestDataModel);
|
||||||
}
|
}
|
||||||
public void DeleteGuest(string id)
|
public void DeleteGuest(string id)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Delete by id: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_guestStorageContract.DelElement(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
using Squirrel.BusinessLogicsContracts;
|
using Squirrel.BusinessLogicsContracts;
|
||||||
using Squirrel.DataModels;
|
using Squirrel.DataModels;
|
||||||
using Squirrel.Enums;
|
using Squirrel.Enums;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.Extensions;
|
||||||
using Squirrel.StoragesContracts;
|
using Squirrel.StoragesContracts;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SquirelBusinessLogic.Implementations;
|
namespace SquirelBusinessLogic.Implementations;
|
||||||
@ -18,27 +21,81 @@ postStorageContract, ILogger logger) : IPostBusinessLogicContract
|
|||||||
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
||||||
public List<PostDataModel> GetAllPosts(bool onlyActive = true)
|
public List<PostDataModel> GetAllPosts(bool onlyActive = true)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
|
||||||
|
return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public List<PostDataModel> GetAllDataOfPost(string postId)
|
public List<PostDataModel> GetAllDataOfPost(string postId)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllDataOfPost for {postId}", postId);
|
||||||
|
if (postId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(postId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!postId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field postId is not a unique identifier.");
|
||||||
|
|
||||||
|
}
|
||||||
|
return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public PostDataModel GetPostByData(string data)
|
public PostDataModel GetPostByData(string data)
|
||||||
{
|
{
|
||||||
return new("", "", PostType.None, 0, true, DateTime.UtcNow);
|
_logger.LogInformation("Get element by data: {data}", data);
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.IsGuid())
|
||||||
|
{
|
||||||
|
return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
|
||||||
}
|
}
|
||||||
public void InsertPost(PostDataModel postDataModel)
|
public void InsertPost(PostDataModel postDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(postDataModel);
|
||||||
|
postDataModel.Validate();
|
||||||
|
_postStorageContract.AddElement(postDataModel);
|
||||||
}
|
}
|
||||||
public void UpdatePost(PostDataModel postDataModel)
|
public void UpdatePost(PostDataModel postDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(postDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(postDataModel);
|
||||||
|
postDataModel.Validate();
|
||||||
|
_postStorageContract.UpdElement(postDataModel);
|
||||||
}
|
}
|
||||||
public void DeletePost(string id)
|
public void DeletePost(string id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Delete by id: {id}", id);
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_postStorageContract.DelElement(id);
|
||||||
}
|
}
|
||||||
public void RestorePost(string id)
|
public void RestorePost(string id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Restore by id: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_postStorageContract.ResElement(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
using Squirrel.BusinessLogicsContracts;
|
using Squirrel.BusinessLogicsContracts;
|
||||||
using Squirrel.DataModels;
|
using Squirrel.DataModels;
|
||||||
using Squirrel.Enums;
|
using Squirrel.Enums;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.Extensions;
|
||||||
using Squirrel.StoragesContracts;
|
using Squirrel.StoragesContracts;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SquirelBusinessLogic.Implementations;
|
namespace SquirelBusinessLogic.Implementations;
|
||||||
@ -15,30 +18,71 @@ internal class ProductBusinessLogicContract(IProductStorageContract
|
|||||||
productStorageContract, ILogger logger) : IProductBusinessLogicContract
|
productStorageContract, ILogger logger) : IProductBusinessLogicContract
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger = logger;
|
private readonly ILogger _logger = logger;
|
||||||
|
|
||||||
private readonly IProductStorageContract _productStorageContract = productStorageContract;
|
private readonly IProductStorageContract _productStorageContract = productStorageContract;
|
||||||
public List<ProductDataModel> GetAllProducts(bool onlyActive)
|
public List<ProductDataModel> GetAllProducts(bool onlyActive)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive);
|
||||||
|
return _productStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ProductHistoryDataModel> GetProductHistoryByProduct(string
|
public List<ProductHistoryDataModel> GetProductHistoryByProduct(string productId)
|
||||||
productId)
|
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetProductHistoryByProduct for {productId}", productId);
|
||||||
|
|
||||||
|
if (productId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(productId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!productId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field productId is not a unique identifier.");
|
||||||
|
|
||||||
|
}
|
||||||
|
return _productStorageContract.GetHistoryByProductId(productId) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public ProductDataModel GetProductByData(string data)
|
public ProductDataModel GetProductByData(string data)
|
||||||
{
|
{
|
||||||
return new("", "", ProductType.None, 0, true);
|
_logger.LogInformation("Get element by data: {data}", data);
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
if (data.IsGuid())
|
||||||
|
{
|
||||||
|
return _productStorageContract.GetElementById(data) ?? throw
|
||||||
|
new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
return _productStorageContract.GetElementByName(data) ?? throw new
|
||||||
|
ElementNotFoundException(data);
|
||||||
}
|
}
|
||||||
public void InsertProduct(ProductDataModel productDataModel)
|
public void InsertProduct(ProductDataModel productDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(productDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(productDataModel);
|
||||||
|
productDataModel.Validate();
|
||||||
|
_productStorageContract.AddElement(productDataModel);
|
||||||
}
|
}
|
||||||
public void UpdateProduct(ProductDataModel productDataModel)
|
public void UpdateProduct(ProductDataModel productDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(productDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(productDataModel);
|
||||||
|
productDataModel.Validate();
|
||||||
|
_productStorageContract.UpdElement(productDataModel);
|
||||||
}
|
}
|
||||||
public void DeleteProduct(string id)
|
public void DeleteProduct(string id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Delete by id: {id}", id);
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_productStorageContract.DelElement(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Squirrel.BusinessLogicsContracts;
|
using Squirrel.BusinessLogicsContracts;
|
||||||
using Squirrel.DataModels;
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.Extensions;
|
||||||
using Squirrel.StoragesContracts;
|
using Squirrel.StoragesContracts;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,9 +12,10 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace SquirelBusinessLogic.Implementations;
|
namespace SquirelBusinessLogic.Implementations;
|
||||||
|
|
||||||
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, ISaleStorageContract saleStorageContract, IPostStorageContract
|
internal class SalaryBusinessLogicContract(ISalaryStorageContract
|
||||||
postStorageContract, IWorkerStorageContract workerStorageContract, ILogger
|
salaryStorageContract,
|
||||||
logger) : ISalaryBusinessLogicContract
|
ISaleStorageContract saleStorageContract, IPostStorageContract
|
||||||
|
postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger = logger;
|
private readonly ILogger _logger = logger;
|
||||||
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
|
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
|
||||||
@ -20,15 +23,47 @@ logger) : ISalaryBusinessLogicContract
|
|||||||
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
||||||
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
||||||
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
|
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
|
||||||
|
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
|
||||||
|
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
|
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
|
||||||
{
|
{
|
||||||
return [];
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (workerId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(workerId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!workerId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field workerId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {workerId}", fromDate, toDate, workerId);
|
||||||
|
return _salaryStorageContract.GetList(fromDate, toDate, workerId) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public void CalculateSalaryByMounth(DateTime date)
|
public void CalculateSalaryByMounth(DateTime date)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("CalculateSalaryByMounth: {date}", date);
|
||||||
|
var startDate = new DateTime(date.Year, date.Month, 1);
|
||||||
|
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
|
||||||
|
var workers = _workerStorageContract.GetList() ?? throw new NullListException();
|
||||||
|
foreach (var worker in workers)
|
||||||
|
{
|
||||||
|
var sales = _saleStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ?? throw new NullListException();
|
||||||
|
var post = _postStorageContract.GetElementById(worker.PostId) ?? throw new NullListException();
|
||||||
|
var salary = post.Salary + sales * 0.1;
|
||||||
|
_logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary);
|
||||||
|
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
using Squirrel.BusinessLogicsContracts;
|
using Squirrel.BusinessLogicsContracts;
|
||||||
using Squirrel.DataModels;
|
using Squirrel.DataModels;
|
||||||
using Squirrel.Enums;
|
using Squirrel.Enums;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.Extensions;
|
||||||
using Squirrel.StoragesContracts;
|
using Squirrel.StoragesContracts;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SquirelBusinessLogic.Implementations;
|
namespace SquirelBusinessLogic.Implementations;
|
||||||
@ -16,33 +19,109 @@ saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
|
|||||||
{
|
{
|
||||||
private readonly ILogger _logger = logger;
|
private readonly ILogger _logger = logger;
|
||||||
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
|
||||||
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
|
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime
|
||||||
|
toDate)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}",
|
||||||
|
fromDate, toDate);
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new
|
||||||
|
NullListException();
|
||||||
}
|
}
|
||||||
public List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
|
public List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId,
|
||||||
|
DateTime fromDate, DateTime toDate)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllSales params: {workerId}, {fromDate}, {toDate}", workerId, fromDate, toDate);
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
if (workerId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(workerId));
|
||||||
|
}
|
||||||
|
if (!workerId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field workerId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
return _saleStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException();
|
||||||
|
|
||||||
}
|
}
|
||||||
public List<SaleDataModel> GetAllSalesByGuestByPeriod(string? guestId, DateTime fromDate, DateTime toDate)
|
public List<SaleDataModel> GetAllSalesByGuestByPeriod(string guestId,
|
||||||
|
DateTime fromDate, DateTime toDate)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllSales params: {guestId}, {fromDate}, { toDate} ", guestId, fromDate, toDate);
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
if (guestId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(guestId));
|
||||||
|
}
|
||||||
|
if (!guestId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field guestId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
return _saleStorageContract.GetList(fromDate, toDate, guestId: guestId) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public List<SaleDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate)
|
public List<SaleDataModel> GetAllSalesByProductByPeriod(string productId,
|
||||||
|
DateTime fromDate, DateTime toDate)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllSales params: {productId}, {fromDate}, { toDate}", productId, fromDate, toDate);
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (productId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(productId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!productId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field productId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
return _saleStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public SaleDataModel GetSaleByData(string data)
|
public SaleDataModel GetSaleByData(string data)
|
||||||
{
|
{
|
||||||
return new("", "", "", 0, DiscountType.None, 0, false, []);
|
_logger.LogInformation("Get element by data: {data}", data);
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
if (!data.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
return _saleStorageContract.GetElementById(data) ?? throw new
|
||||||
|
ElementNotFoundException(data);
|
||||||
}
|
}
|
||||||
public void InsertSale(SaleDataModel saleDataModel)
|
public void InsertSale(SaleDataModel saleDataModel)
|
||||||
{
|
{
|
||||||
}
|
|
||||||
|
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(saleDataModel);
|
||||||
|
saleDataModel.Validate();
|
||||||
|
_saleStorageContract.AddElement(saleDataModel);
|
||||||
|
}
|
||||||
public void CancelSale(string id)
|
public void CancelSale(string id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Cancel by id: {id}", id);
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_saleStorageContract.DelElement(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Squirrel.BusinessLogicsContracts;
|
using Squirrel.BusinessLogicsContracts;
|
||||||
using Squirrel.DataModels;
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.Extensions;
|
||||||
using Squirrel.StoragesContracts;
|
using Squirrel.StoragesContracts;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SquirelBusinessLogic.Implementations;
|
namespace SquirelBusinessLogic.Implementations;
|
||||||
@ -14,38 +17,92 @@ internal class WorkerBusinessLogicContract(IWorkerStorageContract
|
|||||||
workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract
|
workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger = logger;
|
private readonly ILogger _logger = logger;
|
||||||
private readonly IWorkerStorageContract _workerStorageContract =
|
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
||||||
workerStorageContract;
|
|
||||||
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
|
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive);
|
||||||
|
return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool
|
public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool onlyActive = true)
|
||||||
onlyActive = true)
|
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllWorkers params: {postId}, {onlyActive},", postId, onlyActive);
|
||||||
|
|
||||||
|
if (postId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(postId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!postId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field postId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return _workerStorageContract.GetList(onlyActive, postId) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate,
|
public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate,
|
||||||
DateTime toDate, bool onlyActive = true)
|
DateTime toDate, bool onlyActive = true)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate} ", onlyActive, fromDate, toDate);
|
||||||
|
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
return _workerStorageContract.GetList(onlyActive, fromBirthDate:
|
||||||
|
fromDate, toBirthDate: toDate) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime
|
public List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime
|
||||||
fromDate, DateTime toDate, bool onlyActive = true)
|
fromDate, DateTime toDate, bool onlyActive = true)
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
|
||||||
|
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException();
|
||||||
}
|
}
|
||||||
public WorkerDataModel GetWorkerByData(string data)
|
public WorkerDataModel GetWorkerByData(string data)
|
||||||
{
|
{
|
||||||
return new("", "", "", DateTime.Now, DateTime.Now, true);
|
_logger.LogInformation("Get element by data: {data}", data);
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.IsGuid())
|
||||||
|
{
|
||||||
|
return _workerStorageContract.GetElementById(data) ?? throw
|
||||||
|
new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
|
||||||
}
|
}
|
||||||
public void InsertWorker(WorkerDataModel workerDataModel)
|
public void InsertWorker(WorkerDataModel workerDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(workerDataModel);
|
||||||
|
workerDataModel.Validate();
|
||||||
|
_workerStorageContract.AddElement(workerDataModel);
|
||||||
}
|
}
|
||||||
public void UpdateWorker(WorkerDataModel workerDataModel)
|
public void UpdateWorker(WorkerDataModel workerDataModel)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workerDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(workerDataModel);
|
||||||
|
workerDataModel.Validate();
|
||||||
|
_workerStorageContract.UpdElement(workerDataModel);
|
||||||
}
|
}
|
||||||
public void DeleteWorker(string id)
|
|
||||||
|
public void DeleteWorker(string id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Delete by id: {id}", id);
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_workerStorageContract.DelElement(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,6 +10,11 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<InternalsVisibleTo Include="SquirrelTests" />
|
||||||
|
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Squirrel\Squirrel.csproj" />
|
<ProjectReference Include="..\Squirrel\Squirrel.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Squirrel", "Squirrel\Squirr
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SquirrelTests", "SquirrelTests\SquirrelTests.csproj", "{C61A5FF4-E87A-4722-AA58-2E7FC2057217}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SquirrelTests", "SquirrelTests\SquirrelTests.csproj", "{C61A5FF4-E87A-4722-AA58-2E7FC2057217}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquirelBusinessLogic", "SquirelBusinessLogic\SquirelBusinessLogic.csproj", "{9846600E-25A1-423B-B84E-A87CE0563ED7}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquirrelBusinessLogic", "SquirelBusinessLogic\SquirrelBusinessLogic.csproj", "{9846600E-25A1-423B-B84E-A87CE0563ED7}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -34,7 +34,7 @@ public class GuestDataModel(string id, string fio, string phoneNumber, double di
|
|||||||
if (PhoneNumber.IsEmpty())
|
if (PhoneNumber.IsEmpty())
|
||||||
throw new ValidationException("Field PhoneNumber is empty");
|
throw new ValidationException("Field PhoneNumber is empty");
|
||||||
|
|
||||||
if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
|
if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\-]?)?(\(?\d{3}\)?[\-]?)?[\d\-]{7,10}$"))
|
||||||
throw new ValidationException("Field PhoneNumber is not a phone number");
|
throw new ValidationException("Field PhoneNumber is not a phone number");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
Squirrel/Squirrel/Exceptions/DateTimeExtensions.cs
Normal file
15
Squirrel/Squirrel/Exceptions/DateTimeExtensions.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Squirrel.Exceptions;
|
||||||
|
|
||||||
|
public static class DateTimeExtensions
|
||||||
|
{
|
||||||
|
public static bool IsDateNotOlder(this DateTime date, DateTime olderDate)
|
||||||
|
{
|
||||||
|
return date >= olderDate;
|
||||||
|
}
|
||||||
|
}
|
20
Squirrel/Squirrel/Exceptions/ElementExistsException.cs
Normal file
20
Squirrel/Squirrel/Exceptions/ElementExistsException.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Squirrel.Exceptions;
|
||||||
|
|
||||||
|
public class ElementExistsException : Exception
|
||||||
|
{
|
||||||
|
public string ParamName { get; private set; }
|
||||||
|
public string ParamValue { get; private set; }
|
||||||
|
public ElementExistsException(string paramName, string paramValue) :
|
||||||
|
base($"There is already an element with value{paramValue} of parameter { paramName}")
|
||||||
|
|
||||||
|
{
|
||||||
|
ParamName = paramName;
|
||||||
|
ParamValue = paramValue;
|
||||||
|
}
|
||||||
|
}
|
17
Squirrel/Squirrel/Exceptions/ElementNotFoundException.cs
Normal file
17
Squirrel/Squirrel/Exceptions/ElementNotFoundException.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Squirrel.Exceptions;
|
||||||
|
|
||||||
|
public class ElementNotFoundException : Exception
|
||||||
|
{
|
||||||
|
public string Value { get; private set; }
|
||||||
|
public ElementNotFoundException(string value) : base($"Element not found at value = { value}")
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
13
Squirrel/Squirrel/Exceptions/IncorrectDatesException.cs
Normal file
13
Squirrel/Squirrel/Exceptions/IncorrectDatesException.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||||
|
|
||||||
|
namespace Squirrel.Exceptions;
|
||||||
|
|
||||||
|
public class IncorrectDatesException : Exception
|
||||||
|
{
|
||||||
|
public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date..StartDate: { start: dd.MM.YYYY}. EndDate: {end:dd.MM.YYYY}") { }
|
||||||
|
}
|
12
Squirrel/Squirrel/Exceptions/NullListException.cs
Normal file
12
Squirrel/Squirrel/Exceptions/NullListException.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Squirrel.Exceptions;
|
||||||
|
|
||||||
|
public class NullListException : Exception
|
||||||
|
{
|
||||||
|
public NullListException() : base("The returned list is null") { }
|
||||||
|
}
|
13
Squirrel/Squirrel/Exceptions/StorageException .cs
Normal file
13
Squirrel/Squirrel/Exceptions/StorageException .cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Squirrel.Exceptions;
|
||||||
|
|
||||||
|
public class StorageException : Exception
|
||||||
|
{
|
||||||
|
public StorageException(Exception ex) : base($"Error while working in storage: { ex.Message}", ex) { }
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Squirrel.DataModels;
|
||||||
|
|
||||||
namespace Squirrel.StoragesContracts;
|
namespace Squirrel.StoragesContracts;
|
||||||
|
|
||||||
|
@ -0,0 +1,429 @@
|
|||||||
|
|
||||||
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.StoragesContracts;
|
||||||
|
using Moq;
|
||||||
|
using Squirrel.BusinessLogicsContracts;
|
||||||
|
using SquirelBusinessLogic.Implementations;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class GuestBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private GuestBusinessLogicContract _guestBusinessLogicContract;
|
||||||
|
private Mock<IGuestStorageContract> _guestStorageContract;
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_guestStorageContract = new Mock<IGuestStorageContract>();
|
||||||
|
_guestBusinessLogicContract = new GuestBusinessLogicContract(_guestStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_guestStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllGuests_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var listOriginal = new List<GuestDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", 0),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 2", "+7-555-444-33-23", 10),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777", 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
_guestStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var list = _guestBusinessLogicContract.GetAllGuests();
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllGuests_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var list = _guestBusinessLogicContract.GetAllGuests();
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_guestStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllGuests_ReturnNull_ThrowException_Test()
|
||||||
|
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetAllGuests(), Throws.TypeOf<NullListException>());
|
||||||
|
_guestStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllGuests_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetAllGuests(), Throws.TypeOf<StorageException>());
|
||||||
|
_guestStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetGuestByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new GuestDataModel(id, "fio", "+7-111-111-11-11", 0);
|
||||||
|
_guestStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var element = _guestBusinessLogicContract.GetGuestByData(id);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetGuestByData_GetByFio_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fio = "fio";
|
||||||
|
var record = new GuestDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11", 0);
|
||||||
|
_guestStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var element = _guestBusinessLogicContract.GetGuestByData(fio);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.FIO, Is.EqualTo(fio));
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetGuestByData_GetByPhoneNumber_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var phoneNumber = "+7-111-1111111";
|
||||||
|
var record = new GuestDataModel(Guid.NewGuid().ToString(), "fio", phoneNumber, 0);
|
||||||
|
_guestStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var element = _guestBusinessLogicContract.GetGuestByData(phoneNumber);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber));
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetGuestByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetGuestByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetGuestByData(string.Empty),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetGuestByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.GetGuestByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetGuestByData_GetByFio_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("fio"), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void
|
||||||
|
GetGuestByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("+7-111-1111112"), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetGuestByData_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_guestStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_guestStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetGuestByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("fio"), Throws.TypeOf<StorageException>());
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("+7-111-1111112"), Throws.TypeOf<StorageException>());
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||||
|
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void InsertGuest_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new GuestDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 10);
|
||||||
|
_guestStorageContract.Setup(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>()))
|
||||||
|
.Callback((GuestDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.FIO == record.FIO &&
|
||||||
|
x.PhoneNumber == record.PhoneNumber &&
|
||||||
|
x.DiscountSize == record.DiscountSize;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_guestBusinessLogicContract.InsertGuest(record);
|
||||||
|
//Assert
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertGuest_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
|
||||||
|
_guestStorageContract.Setup(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>())).Throws(new
|
||||||
|
ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.InsertGuest(new(Guid.NewGuid().ToString(), "fio",
|
||||||
|
"+7-111-111-11-11", 0)), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertGuest_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.InsertGuest(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertGuest_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.InsertGuest(new
|
||||||
|
GuestDataModel("id", "fio", "+7-111-111-11-11", 10)),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertGuest_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>())).Throws(new StorageException(new
|
||||||
|
InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.InsertGuest(new(Guid.NewGuid().ToString(), "fio",
|
||||||
|
"+7-111-111-11-11", 0)), Throws.TypeOf<StorageException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.AddElement(It.IsAny<GuestDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void UpdateGuest_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new GuestDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0);
|
||||||
|
_guestStorageContract.Setup(x => x.UpdElement(It.IsAny<GuestDataModel>()))
|
||||||
|
.Callback((GuestDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.FIO == record.FIO &&
|
||||||
|
x.PhoneNumber == record.PhoneNumber &&
|
||||||
|
x.DiscountSize == record.DiscountSize;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_guestBusinessLogicContract.UpdateGuest(record);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateGuest_RecordWithIncorrectData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>())).Throws(new
|
||||||
|
ElementNotFoundException(""));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio",
|
||||||
|
"+7-111-111-11-11", 0)), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateGuest_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>())).Throws(new
|
||||||
|
ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio",
|
||||||
|
"+7-111-111-11-11", 0)), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateGuest_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.UpdateGuest(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateGuest_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.UpdateGuest(new
|
||||||
|
GuestDataModel("id", "fio", "+7-111-111-11-11", 10)),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateGuest_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>())).Throws(new StorageException(new
|
||||||
|
InvalidOperationException()));
|
||||||
|
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0)), Throws.TypeOf<StorageException>());
|
||||||
|
_guestStorageContract.Verify(x =>
|
||||||
|
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteGuest_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var flag = false;
|
||||||
|
_guestStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
|
||||||
|
== id))).Callback(() => { flag = true; });
|
||||||
|
//Act
|
||||||
|
_guestBusinessLogicContract.DeleteGuest(id);
|
||||||
|
//Assert
|
||||||
|
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
|
||||||
|
Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteGuest_RecordWithIncorrectId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x =>
|
||||||
|
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.DeleteGuest(Guid.NewGuid().ToString()),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
|
||||||
|
Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteGuest_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.DeleteGuest(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.DeleteGuest(string.Empty),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
|
||||||
|
Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteGuest_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _guestBusinessLogicContract.DeleteGuest("id"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
|
||||||
|
Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteGuest_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
|
||||||
|
//Arrange
|
||||||
|
_guestStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() =>
|
||||||
|
_guestBusinessLogicContract.DeleteGuest(Guid.NewGuid().ToString()),
|
||||||
|
Throws.TypeOf<StorageException>());
|
||||||
|
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
|
||||||
|
Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,459 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using SquirelBusinessLogic.Implementations;
|
||||||
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Enums;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.StoragesContracts;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class PostBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private PostBusinessLogicContract _postBusinessLogicContract;
|
||||||
|
private Mock<IPostStorageContract> _postStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_postStorageContract = new Mock<IPostStorageContract>();
|
||||||
|
_postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_postStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllPosts_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var listOriginal = new List<PostDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(),"name 1", PostType.Waiter, 10, true, DateTime.UtcNow),
|
||||||
|
new(Guid.NewGuid().ToString(), "name 2", PostType.Waiter, 10, false, DateTime.UtcNow),
|
||||||
|
new(Guid.NewGuid().ToString(), "name 3", PostType.Waiter, 10, true, DateTime.UtcNow),
|
||||||
|
};
|
||||||
|
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
|
||||||
|
var listAll = _postBusinessLogicContract.GetAllPosts(false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(listAll, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||||
|
Assert.That(listAll, Is.EquivalentTo(listOriginal));
|
||||||
|
});
|
||||||
|
_postStorageContract.Verify(x => x.GetList(true), Times.Once);
|
||||||
|
_postStorageContract.Verify(x => x.GetList(false), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllPosts_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
|
||||||
|
var listAll = _postBusinessLogicContract.GetAllPosts(false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(listAll, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||||
|
Assert.That(listAll, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllPosts_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllPosts_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPost_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var postId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<PostDataModel>()
|
||||||
|
{
|
||||||
|
new(postId, "name 1", PostType.Waiter, 10, true, DateTime.UtcNow),
|
||||||
|
new(postId, "name 2", PostType.Waiter, 10, false, DateTime.UtcNow)
|
||||||
|
};
|
||||||
|
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _postBusinessLogicContract.GetAllDataOfPost(postId);
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(2));
|
||||||
|
_postStorageContract.Verify(x => x.GetPostWithHistory(postId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPost_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString());
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"), Throws.TypeOf<ValidationException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPost_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPostByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new PostDataModel(id, "name", PostType.Waiter, 10, true, DateTime.UtcNow);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _postBusinessLogicContract.GetPostByData(id);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPostByData_GetByName_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var postName = "name";
|
||||||
|
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Waiter, 10, true, DateTime.UtcNow);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _postBusinessLogicContract.GetPostByData(postName);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.PostName, Is.EqualTo(postName));
|
||||||
|
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPostByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetPostByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetPostByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPostByData_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_postStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<StorageException>());
|
||||||
|
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPost_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 10, true, DateTime.UtcNow.AddDays(-1));
|
||||||
|
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
|
||||||
|
.Callback((PostDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
|
||||||
|
x.ChangeDate == record.ChangeDate;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_postBusinessLogicContract.InsertPost(record);
|
||||||
|
//Assert
|
||||||
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPost_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPost_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.InsertPost(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPost_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Manager, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
||||||
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPost_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
||||||
|
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePost_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 10, true, DateTime.UtcNow.AddDays(-1));
|
||||||
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
|
||||||
|
.Callback((PostDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
|
||||||
|
x.ChangeDate == record.ChangeDate;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_postBusinessLogicContract.UpdatePost(record);
|
||||||
|
//Assert
|
||||||
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Manager, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePost_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.UpdatePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePost_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Manager, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
|
||||||
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePost_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
|
||||||
|
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePost_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var flag = false;
|
||||||
|
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||||
|
//Act
|
||||||
|
_postBusinessLogicContract.DeletePost(id);
|
||||||
|
//Assert
|
||||||
|
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePost_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf<ValidationException>());
|
||||||
|
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePost_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RestorePost_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var flag = false;
|
||||||
|
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||||
|
//Act
|
||||||
|
_postBusinessLogicContract.RestorePost(id);
|
||||||
|
//Assert
|
||||||
|
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.RestorePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RestorePost_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf<ValidationException>());
|
||||||
|
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RestorePost_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,411 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using SquirelBusinessLogic.Implementations;
|
||||||
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Enums;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.StoragesContracts;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class ProductBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private ProductBusinessLogicContract _productBusinessLogicContract;
|
||||||
|
private Mock<IProductStorageContract> _productStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_productStorageContract = new Mock<IProductStorageContract>();
|
||||||
|
_productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_productStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllProducts_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var listOriginal = new List<ProductDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "name 1", ProductType.Desserts, 10, false),
|
||||||
|
new(Guid.NewGuid().ToString(), "name 2", ProductType.Desserts, 10, true),
|
||||||
|
new(Guid.NewGuid().ToString(), "name 3", ProductType.Desserts, 10, false),
|
||||||
|
};
|
||||||
|
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
|
||||||
|
|
||||||
|
var list = _productBusinessLogicContract.GetAllProducts(false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
});
|
||||||
|
_productStorageContract.Verify(x => x.GetList(true), Times.Once);
|
||||||
|
_productStorageContract.Verify(x => x.GetList(false), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllProducts_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
|
||||||
|
var list = _productBusinessLogicContract.GetAllProducts(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
|
||||||
|
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllProducts_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllProducts_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductHistoryByProduct_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var productId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<ProductHistoryDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), 10),
|
||||||
|
new(Guid.NewGuid().ToString(), 15),
|
||||||
|
new(Guid.NewGuid().ToString(), 10),
|
||||||
|
};
|
||||||
|
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _productBusinessLogicContract.GetProductHistoryByProduct(productId);
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_productStorageContract.Verify(x => x.GetHistoryByProductId(productId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductHistoryByProduct_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString());
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductHistoryByProduct_ProductIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct("productId"), Throws.TypeOf<ValidationException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductHistoryByProduct_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new ProductDataModel(id, "name", ProductType.Desserts, 10, false);
|
||||||
|
_productStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _productBusinessLogicContract.GetProductByData(id);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductByData_GetByName_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var name = "name";
|
||||||
|
var record = new ProductDataModel(Guid.NewGuid().ToString(), name, ProductType.Desserts, 10, false);
|
||||||
|
_productStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _productBusinessLogicContract.GetProductByData(name);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.ProductName, Is.EqualTo(name));
|
||||||
|
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductByData("name"), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetProductByData_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_productStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
Assert.That(() => _productBusinessLogicContract.GetProductByData("name"), Throws.TypeOf<StorageException>());
|
||||||
|
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertProduct_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Desserts, 10, false);
|
||||||
|
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
|
||||||
|
.Callback((ProductDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.ProductName == record.ProductName && x.ProductType == record.ProductType;
|
||||||
|
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_productBusinessLogicContract.InsertProduct(record);
|
||||||
|
//Assert
|
||||||
|
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertProduct_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Desserts, 10, false)), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertProduct_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.InsertProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertProduct_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.InsertProduct(new ProductDataModel("id", "name", ProductType.Desserts, 10, false)), Throws.TypeOf<ValidationException>());
|
||||||
|
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertProduct_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Desserts, 10, false)), Throws.TypeOf<StorageException>());
|
||||||
|
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateProduct_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Desserts, 10, false);
|
||||||
|
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>()))
|
||||||
|
.Callback((ProductDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.ProductName == record.ProductName && x.ProductType == record.ProductType &&
|
||||||
|
x.Price == record.Price && x.IsDeleted == record.IsDeleted;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_productBusinessLogicContract.UpdateProduct(record);
|
||||||
|
//Assert
|
||||||
|
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementNotFoundException(""));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Desserts, 10, false)), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateProduct_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "anme", ProductType.Desserts, 10, false)), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateProduct_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.UpdateProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateProduct_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.UpdateProduct(new ProductDataModel("id", "name", ProductType.Desserts, 10, false)), Throws.TypeOf<ValidationException>());
|
||||||
|
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateProduct_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Desserts, 10, false)), Throws.TypeOf<StorageException>());
|
||||||
|
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteProduct_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var flag = false;
|
||||||
|
_productStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||||
|
//Act
|
||||||
|
_productBusinessLogicContract.DeleteProduct(id);
|
||||||
|
//Assert
|
||||||
|
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteProduct_RecordWithIncorrectId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.DeleteProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _productBusinessLogicContract.DeleteProduct(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteProduct_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.DeleteProduct("id"), Throws.TypeOf<ValidationException>());
|
||||||
|
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteProduct_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,353 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using SquirelBusinessLogic.Implementations;
|
||||||
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Enums;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.StoragesContracts;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SalaryBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private SalaryBusinessLogicContract _salaryBusinessLogicContract;
|
||||||
|
private Mock<ISalaryStorageContract> _salaryStorageContract;
|
||||||
|
private Mock<ISaleStorageContract> _saleStorageContract;
|
||||||
|
private Mock<IPostStorageContract> _postStorageContract;
|
||||||
|
private Mock<IWorkerStorageContract> _workerStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_salaryStorageContract = new Mock<ISalaryStorageContract>();
|
||||||
|
_saleStorageContract = new Mock<ISaleStorageContract>();
|
||||||
|
_postStorageContract = new Mock<IPostStorageContract>();
|
||||||
|
_workerStorageContract = new Mock<IWorkerStorageContract>();
|
||||||
|
_salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object,
|
||||||
|
_saleStorageContract.Object, _postStorageContract.Object, _workerStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_salaryStorageContract.Reset();
|
||||||
|
_saleStorageContract.Reset();
|
||||||
|
_postStorageContract.Reset();
|
||||||
|
_workerStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalaries_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var startDate = DateTime.UtcNow;
|
||||||
|
var endDate = DateTime.UtcNow.AddDays(1);
|
||||||
|
var listOriginal = new List<SalaryDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
|
||||||
|
};
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriod(startDate, endDate);
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalaries_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalaries_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var dateTime = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalaries_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalaries_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByWorker_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var startDate = DateTime.UtcNow;
|
||||||
|
var endDate = DateTime.UtcNow.AddDays(1);
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SalaryDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
|
||||||
|
};
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(startDate, endDate, workerId);
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, workerId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByWorker_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString());
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByWorker_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var dateTime = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime, Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime.AddSeconds(-1), Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByWorker_WorkerIdIsNUllOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByWorker_WorkerIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "workerId"), Throws.TypeOf<ValidationException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByWorker_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByWorker_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_CalculateSalary_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
var saleSum = 200.0;
|
||||||
|
var postSalary = 2000.0;
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, saleSum, DiscountType.None, 0, false, [])]);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Waiter, postSalary, true, DateTime.UtcNow));
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||||
|
var sum = 0.0;
|
||||||
|
var expectedSum = postSalary + saleSum * 0.1;
|
||||||
|
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
|
||||||
|
.Callback((SalaryDataModel x) =>
|
||||||
|
{
|
||||||
|
sum = x.Salary;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||||
|
//Assert
|
||||||
|
Assert.That(sum, Is.EqualTo(expectedSum));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_WithSeveralWorkers_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var worker1Id = Guid.NewGuid().ToString();
|
||||||
|
var worker2Id = Guid.NewGuid().ToString();
|
||||||
|
var worker3Id = Guid.NewGuid().ToString();
|
||||||
|
var list = new List<WorkerDataModel>() {
|
||||||
|
new(worker1Id, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
|
||||||
|
new(worker2Id, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
|
||||||
|
new(worker3Id, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)
|
||||||
|
};
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), worker1Id, null, 1, DiscountType.None, 0, false, []),
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), worker1Id, null, 1, DiscountType.None, 0, false, []),
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), worker2Id, null, 1, DiscountType.None, 0, false, []),
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, null, 1, DiscountType.None, 0, false, []),
|
||||||
|
new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, null, 1, DiscountType.None, 0, false, [])]);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Waiter, 2000, true, DateTime.UtcNow));
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns(list);
|
||||||
|
//Act
|
||||||
|
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||||
|
//Assert
|
||||||
|
_salaryStorageContract.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Exactly(list.Count));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_WithoitSalesByWorker_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var postSalary = 2000.0;
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns([]);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Waiter, postSalary, true, DateTime.UtcNow));
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||||
|
var sum = 0.0;
|
||||||
|
var expectedSum = postSalary;
|
||||||
|
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
|
||||||
|
.Callback((SalaryDataModel x) =>
|
||||||
|
{
|
||||||
|
sum = x.Salary;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||||
|
//Assert
|
||||||
|
Assert.That(sum, Is.EqualTo(expectedSum));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_SaleStorageReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Waiter, 2000, true, DateTime.UtcNow));
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_PostStorageReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_WorkerStorageReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Waiter, 2000, true, DateTime.UtcNow));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_SaleStorageThrowException_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Waiter, 2000, true, DateTime.UtcNow));
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_PostStorageThrowException_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns([new WorkerDataModel(workerId, "Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CalculateSalaryByMounth_WorkerStorageThrowException_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), workerId, null, 200, DiscountType.None, 0, false, [])]);
|
||||||
|
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||||
|
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Waiter, 2000, true, DateTime.UtcNow));
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,505 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using SquirelBusinessLogic.Implementations;
|
||||||
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Enums;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.StoragesContracts;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SaleBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private SaleBusinessLogicContract _saleBusinessLogicContract;
|
||||||
|
private Mock<ISaleStorageContract> _saleStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_saleStorageContract = new Mock<ISaleStorageContract>();
|
||||||
|
_saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_saleStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByPeriod_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
var listOriginal = new List<SaleDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
|
||||||
|
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
};
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByPeriod(date, date.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByPeriod_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByPeriod_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByPeriod(date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByPeriod_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByWorkerByPeriod_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
var workerId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SaleDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
|
||||||
|
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
};
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(workerId, date, date.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), workerId, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByWorkerByPeriod_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByWorkerByPeriod_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByWorkerByPeriod_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByWorkerByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByGuestByPeriod_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
var guestId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SaleDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
|
||||||
|
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
};
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(guestId, date, date.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, guestId, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByGuestByPeriod_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByGuestByPeriod_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByGuestByPeriod_GuestIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByGuestByPeriod_GuestIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByGuestByPeriod("guestId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByGuestByPeriod_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByGuestByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByGuestByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByProductByPeriod_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
var productId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SaleDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
|
||||||
|
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false, []),
|
||||||
|
};
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByProductByPeriod(productId, date, date.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, productId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByProductByPeriod_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var list = _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||||
|
//Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByProductByPeriod_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByProductByPeriod_ProductIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByProductByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByProductByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByProductByPeriod_ProductIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByProductByPeriod("productId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByProductByPeriod_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalesByProductByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSaleByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new SaleDataModel(id, Guid.NewGuid().ToString(), null, 10, DiscountType.None, 0, false,
|
||||||
|
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||||
|
_saleStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _saleBusinessLogicContract.GetSaleByData(id);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSaleByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetSaleByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetSaleByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSaleByData_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetSaleByData("saleId"), Throws.TypeOf<ValidationException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSaleByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetSaleByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetSaleByData_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.GetSaleByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSale_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10,
|
||||||
|
false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||||
|
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>()))
|
||||||
|
.Callback((SaleDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.WorkerId == record.WorkerId && x.GuestId == record.GuestId &&
|
||||||
|
x.SaleDate == record.SaleDate && x.Sum == record.Sum && x.DiscountType == record.DiscountType &&
|
||||||
|
x.Discount == record.Discount && x.IsCancel == record.IsCancel && x.Products.Count == record.Products.Count &&
|
||||||
|
x.Products.First().ProductId == record.Products.First().ProductId &&
|
||||||
|
x.Products.First().SaleId == record.Products.First().SaleId &&
|
||||||
|
x.Products.First().Count == record.Products.First().Count;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_saleBusinessLogicContract.InsertSale(record);
|
||||||
|
//Assert
|
||||||
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSale_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSale_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.InsertSale(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSale_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.InsertSale(new SaleDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false, [])), Throws.TypeOf<ValidationException>());
|
||||||
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertSale_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<SaleDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(), 10, DiscountType.None, 10, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||||
|
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CancelSale_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var flag = false;
|
||||||
|
_saleStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||||
|
//Act
|
||||||
|
_saleBusinessLogicContract.CancelSale(id);
|
||||||
|
//Assert
|
||||||
|
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CancelSale_RecordWithIncorrectId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_saleStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CancelSale_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.CancelSale(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.CancelSale(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CancelSale_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.CancelSale("id"), Throws.TypeOf<ValidationException>());
|
||||||
|
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CancelSale_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_saleStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,562 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using SquirelBusinessLogic.Implementations;
|
||||||
|
using Squirrel.DataModels;
|
||||||
|
using Squirrel.Exceptions;
|
||||||
|
using Squirrel.StoragesContracts;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SquirrelTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class WorkerBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private WorkerBusinessLogicContract _workerBusinessLogicContract;
|
||||||
|
private Mock<IWorkerStorageContract> _workerStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_workerStorageContract = new Mock<IWorkerStorageContract>();
|
||||||
|
_workerBusinessLogicContract = new WorkerBusinessLogicContract(_workerStorageContract.Object, new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_workerStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkers_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var listOriginal = new List<WorkerDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
};
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(true, null, null, null, null, null), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(false, null, null, null, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkers_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null, null, null, null), Times.Exactly(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkers_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkers_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null, null, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByPost_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var postId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<WorkerDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
};
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByPost(postId, true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkersByPost(postId, false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(true, postId, null, null, null, null), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(false, postId, null, null, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByPost_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Exactly(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByPost_PostIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(null, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(string.Empty, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByPost_PostIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost("postId", It.IsAny<bool>()), Throws.TypeOf<ValidationException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByPost_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByPost_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
var listOriginal = new List<WorkerDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
};
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(true, null, date, date.AddDays(1), null, null), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(false, null, date, date.AddDays(1), null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByBirthDate_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(true, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(false, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByBirthDate_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
var listOriginal = new List<WorkerDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 1", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 2", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
|
||||||
|
new(Guid.NewGuid().ToString(), "fio 3", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
|
||||||
|
};
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(true, null, null, null, date, date.AddDays(1)), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(false, null, null, null, date, date.AddDays(1)), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByEmploymentDate_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||||
|
//Act
|
||||||
|
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
|
||||||
|
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), false);
|
||||||
|
//Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(listOnlyActive, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(true, null, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(false, null, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var date = DateTime.UtcNow;
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWorkerByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new WorkerDataModel(id, "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
|
||||||
|
_workerStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _workerBusinessLogicContract.GetWorkerByData(id);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWorkerByData_GetByFio_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fio = "fio";
|
||||||
|
var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
|
||||||
|
_workerStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
|
||||||
|
//Act
|
||||||
|
var element = _workerBusinessLogicContract.GetWorkerByData(fio);
|
||||||
|
//Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.FIO, Is.EqualTo(fio));
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWorkerByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetWorkerByData_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
_workerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWorker_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false);
|
||||||
|
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>()))
|
||||||
|
.Callback((WorkerDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
|
||||||
|
x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_workerBusinessLogicContract.InsertWorker(record);
|
||||||
|
//Assert
|
||||||
|
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWorker_RecordWithExistsData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ElementExistsException>());
|
||||||
|
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWorker_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.InsertWorker(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWorker_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new WorkerDataModel("id", "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
|
||||||
|
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertWorker_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWorker_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var flag = false;
|
||||||
|
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false);
|
||||||
|
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>()))
|
||||||
|
.Callback((WorkerDataModel x) =>
|
||||||
|
{
|
||||||
|
flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
|
||||||
|
x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
|
||||||
|
});
|
||||||
|
//Act
|
||||||
|
_workerBusinessLogicContract.UpdateWorker(record);
|
||||||
|
//Assert
|
||||||
|
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new ElementNotFoundException(""));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWorker_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWorker_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new WorkerDataModel("id", "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
|
||||||
|
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateWorker_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWorker_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var flag = false;
|
||||||
|
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||||
|
//Act
|
||||||
|
_workerBusinessLogicContract.DeleteWorker(id);
|
||||||
|
//Assert
|
||||||
|
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
Assert.That(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x != id))).Throws(new ElementNotFoundException(id));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWorker_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"), Throws.TypeOf<ValidationException>());
|
||||||
|
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteWorker_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_workerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
//Act&Assert
|
||||||
|
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||||
|
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||||
|
<PackageReference Include="Moq" Version="4.20.72" />
|
||||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||||
@ -22,11 +23,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\SquirelBusinessLogic\SquirrelBusinessLogic.csproj" />
|
||||||
<ProjectReference Include="..\Squirrel\Squirrel.csproj" />
|
<ProjectReference Include="..\Squirrel\Squirrel.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="BusinessLogicsContractsTests\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user