From e05876924f8b2f5a584a36e71d2ebbf0d7bc7fa5 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Tue, 18 Feb 2025 16:41:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=8B=20=D0=B7=D0=B5=D0=BB=D0=B5=D0=BD=D1=8B=D0=B5,=20=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B0=D0=B4=D0=BE=20=D0=B5=D1=89=D0=B5=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D1=81=D1=87=D0=B5=D1=82=20=D0=BC=D0=B5=D1=81=D1=8F=D1=87=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B7=D0=B0=D1=80=D0=BF=D0=BB=D0=B0=D1=82=D1=8B?= =?UTF-8?q?=20=D0=B8=20=D0=BF=D0=B0=D1=80=D1=83=20=D0=B4=D1=80=D1=83=D0=B3?= =?UTF-8?q?=D0=B8=D1=85=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CuttingBusinessLogicContract.cs | 133 ++++- .../PostBusinessLogicContract.cs | 79 ++- .../ProductBusinessLogicContract.cs | 76 ++- .../SalaryBusinessLogicContract.cs | 53 +- .../WorkerBusinessLogicContract.cs | 85 ++- .../ICuttingBusinessLogicContract.cs | 7 +- .../Exceptions/IncorrectDatesException.cs | 12 + .../Extensions/DateTimeExtensions.cs | 9 + .../CuttingBusinessLogicContractTests.cs | 338 +++++++++++ .../ISalaryStorageContractTests.cs | 190 ++++++ .../PostBusinessLogicContractTests.cs | 459 ++++++++++++++ .../ProductBusinessLogicsContractTests.cs | 486 +++++++++++++++ .../WorkerBusinessLogicContractTests.cs | 562 ++++++++++++++++++ 13 files changed, 2410 insertions(+), 79 deletions(-) create mode 100644 PapaCarloProject/PapaCarloContracts/Exceptions/IncorrectDatesException.cs create mode 100644 PapaCarloProject/PapaCarloContracts/Extensions/DateTimeExtensions.cs create mode 100644 PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/CuttingBusinessLogicContractTests.cs create mode 100644 PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ISalaryStorageContractTests.cs create mode 100644 PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/PostBusinessLogicContractTests.cs create mode 100644 PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ProductBusinessLogicsContractTests.cs create mode 100644 PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/WorkerBusinessLogicContractTests.cs diff --git a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/CuttingBusinessLogicContract.cs b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/CuttingBusinessLogicContract.cs index 7361235..f631f4e 100644 --- a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/CuttingBusinessLogicContract.cs +++ b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/CuttingBusinessLogicContract.cs @@ -1,43 +1,120 @@ using Microsoft.Extensions.Logging; using PapaCarloContracts.BusinessLogicContracts; using PapaCarloContracts.DataModels; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.Extensions; using PapaCarloContracts.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; -namespace PapaCarloBusinessLogic.Implementations +namespace PapaCarloBusinessLogic.Implementations; + +internal class CuttingBusinessLogicContract(ICuttingStorageContract cuttingStorageContract, ILogger logger) : ICuttingBusinessLogicContract { - internal class CuttingBusinessLogicContract(ICuttingStorageContract cuttingStorageContract, ILogger logger) : ICuttingBusinessLogicContract + private readonly ILogger _logger = logger; + private readonly ICuttingStorageContract _cuttingStorageContract = cuttingStorageContract; + + public List GetAllCuttingsByPeriod(DateTime fromDate, DateTime toDate) { - public void CancelCutting(string id) - { - } - - public List GetAllCuttingsByBlankByPeriod(string blankId, DateTime fromDate, DateTime toDate) - { - return []; - } - - public List GetAllCuttingsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) - { - return []; - } - - public List GetAllCuttningsByPeriod(DateTime fromDate, DateTime toDate) - { - return []; - } - - public CuttingDataModel GetCuttingByData(string data) - { - return new("", "", "", true, []); - } - - public void InsertSale(CuttingDataModel cuttingDataModel) + _logger.LogInformation("GetAllCuttings params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) { + throw new IncorrectDatesException(fromDate, toDate); } + return _cuttingStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); } -} + + public List GetAllCuttingsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllCuttings 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 _cuttingStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException(); + } + + public List GetAllCuttingsByBuyerByPeriod(string blankId, DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllCuttings params: {blankId}, {fromDate}, {toDate}", blankId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (blankId.IsEmpty()) + { + throw new ArgumentNullException(nameof(blankId)); + } + if (!blankId.IsGuid()) + { + throw new ValidationException("The value in the field blankId is not a unique identifier."); + } + return _cuttingStorageContract.GetList(fromDate, toDate, blankId: blankId) ?? throw new NullListException(); + } + + public List GetAllCuttingsByProductByPeriod(string productId, DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllCuttings 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 _cuttingStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException(); + } + + public CuttingDataModel GetCuttingByData(string data) + { + _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 _cuttingStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + + public void InsertCutting(CuttingDataModel cuttingDataModel) + { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(cuttingDataModel)); + ArgumentNullException.ThrowIfNull(cuttingDataModel); + cuttingDataModel.Validate(); + _cuttingStorageContract.AddElement(cuttingDataModel); + } + + public void CancelCutting(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"); + } + _cuttingStorageContract.DelElement(id); + } +} \ No newline at end of file diff --git a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/PostBusinessLogicContract.cs index 8bf5004..1964087 100644 --- a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -1,45 +1,98 @@ using Microsoft.Extensions.Logging; using PapaCarloContracts.BuisinessLogicsContracts; using PapaCarloContracts.DataModels; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.Extensions; using PapaCarloContracts.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace PapaCarloBusinessLogic.Implementations; internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { - public void DeletePost(string id) + private readonly ILogger _logger = logger; + private readonly IPostStorageContract _postStorageContract = postStorageContract; + + public List GetAllPosts(bool onlyActive = true) { + _logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive); + return _postStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetAllDataOfPost(string postId) { - return []; - } - - public List GetAllPosts(bool onlyActive) - { - 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) { - return new("", "", 0, true, DateTime.Now); + _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 RestorePost(string id) - { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel)); + ArgumentNullException.ThrowIfNull(postDataModel); + postDataModel.Validate(); + _postStorageContract.AddElement(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) + { + _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) + { + _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); + } +} \ No newline at end of file diff --git a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 17f2707..3bb0a7b 100644 --- a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -1,46 +1,98 @@ using Microsoft.Extensions.Logging; using PapaCarloContracts.BusinessLogicContracts; using PapaCarloContracts.DataModels; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.Extensions; using PapaCarloContracts.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace PapaCarloBusinessLogic.Implementations; internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract { - public void DeleteProduct(string id) - { - } + private readonly ILogger _logger = logger; + private readonly IProductStorageContract _productStorageContract = productStorageContract; - public List GetAllProducts(bool onlyActive = true) + public List GetAllProducts(bool onlyActive) { - return []; + _logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive); + return _productStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetAllProductsByBlank(string blankId, bool onlyActive = true) { - return []; - } - - public ProductDataModel GetProductByData(string data) - { - return new("", "", 0, 0, false); + if (blankId.IsEmpty()) + { + throw new ArgumentNullException(nameof(blankId)); + } + if (!blankId.IsGuid()) + { + throw new ValidationException("The value in the field blankId is not a unique identifier."); + } + _logger.LogInformation("GetAllProducts params: {blankId}, {onlyActive}", blankId, onlyActive); + return _productStorageContract.GetList(onlyActive, blankId) ?? throw new NullListException(); } public List GetProductHistoryByProduct(string 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) + { + _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) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(productDataModel)); + ArgumentNullException.ThrowIfNull(productDataModel); + productDataModel.Validate(); + _productStorageContract.AddElement(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) + { + _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); } } diff --git a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index 2667cc7..813f16c 100644 --- a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.Logging; using PapaCarloContracts.BusinessLogicContracts; using PapaCarloContracts.DataModels; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.Extensions; using PapaCarloContracts.StoragesContracts; using System; using System.Collections.Generic; @@ -10,19 +12,58 @@ using System.Threading.Tasks; namespace PapaCarloBusinessLogic.Implementations; -internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, ILogger logger) : ISalaryBusinessLogicContract +internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, + ICuttingStorageContract cuttingStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { - public void CalculateSalaryByMounth(DateTime date) - { - } + private readonly ILogger _logger = logger; + private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; + private readonly ICuttingStorageContract _cuttingStorageContract = cuttingStorageContract; + private readonly IPostStorageContract _postStorageContract = postStorageContract; + private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; public List 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 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) + { + _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 cuttings = _cuttingStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Products.Count) ?? + throw new NullListException(); + var post = _postStorageContract.GetElementById(worker.PostId) ?? + throw new NullListException(); + var salary = cuttings * 100; + _logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary); + _salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary)); + } } } diff --git a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index 102db75..7727819 100644 --- a/PapaCarloProject/PapaCarloBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/PapaCarloProject/PapaCarloBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -1,51 +1,104 @@ using Microsoft.Extensions.Logging; using PapaCarloContracts.BusinessLogicContracts; using PapaCarloContracts.DataModels; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.Extensions; using PapaCarloContracts.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace PapaCarloBusinessLogic.Implementations; internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract { - public void DeleteWorker(string id) - { - } + private readonly ILogger _logger = logger; + private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; public List GetAllWorkers(bool onlyActive = true) { - return []; - } - - public List GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) - { - return []; - } - - public List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) - { - return []; + _logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive); + return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetAllWorkersByPost(string postId, bool 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 GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) + { + _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 GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) + { + _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) { - 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) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel)); + ArgumentNullException.ThrowIfNull(workerDataModel); + workerDataModel.Validate(); + _workerStorageContract.AddElement(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) + { + _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); } } diff --git a/PapaCarloProject/PapaCarloContracts/BusinessLogicContracts/ICuttingBusinessLogicContract.cs b/PapaCarloProject/PapaCarloContracts/BusinessLogicContracts/ICuttingBusinessLogicContract.cs index e8ca2aa..4a7d967 100644 --- a/PapaCarloProject/PapaCarloContracts/BusinessLogicContracts/ICuttingBusinessLogicContract.cs +++ b/PapaCarloProject/PapaCarloContracts/BusinessLogicContracts/ICuttingBusinessLogicContract.cs @@ -9,13 +9,12 @@ namespace PapaCarloContracts.BusinessLogicContracts; public interface ICuttingBusinessLogicContract { - List GetAllCuttningsByPeriod(DateTime fromDate, DateTime + List GetAllCuttingsByPeriod(DateTime fromDate, DateTime toDate); List GetAllCuttingsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate); - List GetAllCuttingsByBlankByPeriod(string blankId, DateTime - fromDate, DateTime toDate); + CuttingDataModel GetCuttingByData(string data); - void InsertSale(CuttingDataModel cuttingDataModel); + void InsertCutting(CuttingDataModel cuttingDataModel); void CancelCutting(string id); } diff --git a/PapaCarloProject/PapaCarloContracts/Exceptions/IncorrectDatesException.cs b/PapaCarloProject/PapaCarloContracts/Exceptions/IncorrectDatesException.cs new file mode 100644 index 0000000..823592a --- /dev/null +++ b/PapaCarloProject/PapaCarloContracts/Exceptions/IncorrectDatesException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PapaCarloContracts.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}") { } +} diff --git a/PapaCarloProject/PapaCarloContracts/Extensions/DateTimeExtensions.cs b/PapaCarloProject/PapaCarloContracts/Extensions/DateTimeExtensions.cs new file mode 100644 index 0000000..b74b1b5 --- /dev/null +++ b/PapaCarloProject/PapaCarloContracts/Extensions/DateTimeExtensions.cs @@ -0,0 +1,9 @@ +namespace PapaCarloContracts.Extensions; + +public static class DateTimeExtensions +{ + public static bool IsDateNotOlder(this DateTime date, DateTime olderDate) + { + return date >= olderDate; + } +} \ No newline at end of file diff --git a/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/CuttingBusinessLogicContractTests.cs b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/CuttingBusinessLogicContractTests.cs new file mode 100644 index 0000000..cb81a14 --- /dev/null +++ b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/CuttingBusinessLogicContractTests.cs @@ -0,0 +1,338 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PapaCarloBusinessLogic.Implementations; +using PapaCarloContracts.DataModels; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PapaCarloTests.BusinessLogicsContracts; + +[TestFixture] +internal class CuttingBusinessLogicContractTests +{ + private CuttingBusinessLogicContract _cuttingBusinessLogicContract; + private Mock _cuttingStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _cuttingStorageContract = new Mock(); + _cuttingBusinessLogicContract = new CuttingBusinessLogicContract(_cuttingStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _cuttingStorageContract.Reset(); + } + + [Test] + public void GetAllCuttingsByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, + [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, []), + }; + _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _cuttingStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, null), Times.Once); + } + + [Test] + public void GetAllCuttingsByPeriod_ReturnEmptyList_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllCuttingsByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date), Throws.TypeOf()); + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllCuttingsByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllCuttingsByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllCuttingsByWorkerByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var workerId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, + [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, []), + }; + _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(workerId, date, date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _cuttingStorageContract.Verify(x => x.GetList(date, date.AddDays(1), workerId, null, null), Times.Once); + } + + [Test] + public void GetAllCuttingsByWorkerByPeriod_ReturnEmptyList_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllCuttingsByWorkerByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf()); + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllCuttingsByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllCuttingsByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllCuttingsByWorkerByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllCuttingsByWorkerByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetCuttingByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new CuttingDataModel(id, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, + [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); + _cuttingStorageContract.Setup(x => x.GetElementById(id)).Returns(record); + //Act + var element = _cuttingBusinessLogicContract.GetCuttingByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetCuttingByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(null), Throws.TypeOf()); + Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(string.Empty), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + } + + [Test] + public void GetCuttingByData_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData("cuttingId"), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + } + + [Test] + public void GetCuttingByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetCuttingByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void InsertCutting_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new CuttingDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); + _cuttingStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((CuttingDataModel x) => + { + flag = x.Id == record.Id && x.WorkerId == record.WorkerId && x.BlankId == record.BlankId && + x.CutDate == record.CutDate && x.IsCancel == record.IsCancel; + }); + //Act + _cuttingBusinessLogicContract.InsertCutting(record); + //Assert + _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertCutting_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(),false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertCutting_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(null), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertCutting_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(new CuttingDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, [])), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertCutting_StorageThrowError_ThrowException_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(),false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void CancelCutting_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _cuttingStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + //Act + _cuttingBusinessLogicContract.CancelCutting(id); + //Assert + _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void CancelCutting_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _cuttingStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(Guid.NewGuid().ToString()), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + [Test] + public void CancelCutting_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(null), Throws.TypeOf()); + Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(string.Empty), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void CancelCutting_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.CancelCutting("id"), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void CancelCutting_StorageThrowError_ThrowException_Test() + { + //Arrange + _cuttingStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(Guid.NewGuid().ToString()), Throws.TypeOf()); + _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } +} diff --git a/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ISalaryStorageContractTests.cs b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ISalaryStorageContractTests.cs new file mode 100644 index 0000000..a3894f2 --- /dev/null +++ b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ISalaryStorageContractTests.cs @@ -0,0 +1,190 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PapaCarloBusinessLogic.Implementations; +using PapaCarloContracts.DataModels; +using PapaCarloContracts.Enums; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PapaCarloTests.BusinessLogicsContracts; + +[TestFixture] +internal class SalaryBusinessLogicContractTests +{ + private SalaryBusinessLogicContract _salaryBusinessLogicContract; + private Mock _salaryStorageContract; + private Mock _cuttingStorageContract; + private Mock _postStorageContract; + private Mock _workerStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _salaryStorageContract = new Mock(); + _cuttingStorageContract = new Mock(); + _postStorageContract = new Mock(); + _workerStorageContract = new Mock(); + _salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object, + _cuttingStorageContract.Object, _postStorageContract.Object, _workerStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _salaryStorageContract.Reset(); + _cuttingStorageContract.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() + { + 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(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalaries_IncorrectDates_ThrowException_Test() + { + //Arrange + var dateTime = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime), Throws.TypeOf()); + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime.AddSeconds(-1)), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalaries_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalaries_StorageThrowError_ThrowException_Test() + { + //Arrange + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), 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() + { + 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(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny()), 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()); + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime.AddSeconds(-1), Guid.NewGuid().ToString()), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalariesByWorker_WorkerIdIsNUllOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf()); + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), string.Empty), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalariesByWorker_WorkerIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "workerId"), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), 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()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalariesByWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + +} diff --git a/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/PostBusinessLogicContractTests.cs b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/PostBusinessLogicContractTests.cs new file mode 100644 index 0000000..bf00510 --- /dev/null +++ b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/PostBusinessLogicContractTests.cs @@ -0,0 +1,459 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PapaCarloBusinessLogic.Implementations; +using PapaCarloContracts.DataModels; +using PapaCarloContracts.Enums; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PapaCarloTests.BusinessLogicsContracts; + +[TestFixture] +internal class PostBusinessLogicContractTests +{ + private PostBusinessLogicContract _postBusinessLogicContract; + private Mock _postStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _postStorageContract = new Mock(); + _postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _postStorageContract.Reset(); + } + + [Test] + public void GetAllPosts_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(),"name 1", PostType.Carpenter, true, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 2", PostType.Carpenter, false, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 3", PostType.Carpenter, true, DateTime.UtcNow), + }; + _postStorageContract.Setup(x => x.GetList(It.IsAny())).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())).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()), Times.Exactly(2)); + } + + [Test] + public void GetAllPosts_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Once); + } + + [Test] + public void GetAllPosts_StorageThrowError_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.GetList(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Once); + } + + [Test] + public void GetAllDataOfPost_ReturnListOfRecords_Test() + { + //Arrange + var postId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(postId, "name 1", PostType.Carpenter, true, DateTime.UtcNow), + new(postId, "name 2", PostType.Carpenter, false, DateTime.UtcNow) + }; + _postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny())).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())).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()), Times.Once); + } + + [Test] + public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null), Throws.TypeOf()); + Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(string.Empty), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny()), Times.Never); + } + + [Test] + public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny()), Times.Never); + } + + [Test] + public void GetAllDataOfPost_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny()), Times.Once); + } + + [Test] + public void GetAllDataOfPost_StorageThrowError_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny()), Times.Once); + } + + [Test] + public void GetPostByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new PostDataModel(id, "name", PostType.Carpenter, 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()), Times.Once); + } + + [Test] + public void GetPostByData_GetByName_ReturnRecord_Test() + { + //Arrange + var postName = "name"; + var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Carpenter, 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()), Times.Once); + } + + [Test] + public void GetPostByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetPostByData(null), Throws.TypeOf()); + Assert.That(() => _postBusinessLogicContract.GetPostByData(string.Empty), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _postStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); + } + + [Test] + public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _postStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); + } + + [Test] + public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _postStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void GetPostByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _postStorageContract.Setup(x => x.GetElementByName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf()); + _postStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _postStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void InsertPost_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Carpenter, true, DateTime.UtcNow.AddDays(-1)); + _postStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((PostDataModel x) => + { + flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && + x.ChangeDate == record.ChangeDate; + }); + //Act + _postBusinessLogicContract.InsertPost(record); + //Assert + _postStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertPost_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Carpenter, true, DateTime.UtcNow)), Throws.TypeOf()); + _postStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertPost_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.InsertPost(null), Throws.TypeOf()); + _postStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertPost_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Carpenter, true, DateTime.UtcNow)), Throws.TypeOf()); + _postStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertPost_StorageThrowError_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Carpenter, true, DateTime.UtcNow)), Throws.TypeOf()); + _postStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdatePost_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Carpenter, true, DateTime.UtcNow.AddDays(-1)); + _postStorageContract.Setup(x => x.UpdElement(It.IsAny())) + .Callback((PostDataModel x) => + { + flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && + x.ChangeDate == record.ChangeDate; + }); + //Act + _postBusinessLogicContract.UpdatePost(record); + //Assert + _postStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void UpdatePost_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Carpenter, true, DateTime.UtcNow)), Throws.TypeOf()); + _postStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdatePost_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Carpenter, true, DateTime.UtcNow)), Throws.TypeOf()); + _postStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdatePost_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.UpdatePost(null), Throws.TypeOf()); + _postStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdatePost_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Carpenter, true, DateTime.UtcNow)), Throws.TypeOf()); + _postStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdatePost_StorageThrowError_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Carpenter, true, DateTime.UtcNow)), Throws.TypeOf()); + _postStorageContract.Verify(x => x.UpdElement(It.IsAny()), 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()), 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())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeletePost_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf()); + Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf()); + _postStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeletePost_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf()); + _postStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeletePost_StorageThrowError_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.DelElement(It.IsAny()), 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()), 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())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.ResElement(It.IsAny()), Times.Once); + } + + [Test] + public void RestorePost_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.RestorePost(null), Throws.TypeOf()); + Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf()); + _postStorageContract.Verify(x => x.ResElement(It.IsAny()), Times.Never); + } + + [Test] + public void RestorePost_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf()); + _postStorageContract.Verify(x => x.ResElement(It.IsAny()), Times.Never); + } + + [Test] + public void RestorePost_StorageThrowError_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => x.ResElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf()); + _postStorageContract.Verify(x => x.ResElement(It.IsAny()), Times.Once); + } +} diff --git a/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ProductBusinessLogicsContractTests.cs b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ProductBusinessLogicsContractTests.cs new file mode 100644 index 0000000..bc5bb02 --- /dev/null +++ b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/ProductBusinessLogicsContractTests.cs @@ -0,0 +1,486 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PapaCarloBusinessLogic.Implementations; +using PapaCarloContracts.DataModels; +using PapaCarloContracts.Enums; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PapaCarloTests.BusinessLogicsContracts; + +[TestFixture] +internal class ProductBusinessLogicContractTests +{ + private ProductBusinessLogicContract _productBusinessLogicContract; + private Mock _productStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _productStorageContract = new Mock(); + _productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _productStorageContract.Reset(); + } + + [Test] + public void GetAllProducts_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "name 1", ProductType.Furniture, 10, false), + new(Guid.NewGuid().ToString(), "name 2", ProductType.Furniture, 10, false), + new(Guid.NewGuid().ToString(), "name 3", ProductType.Furniture, 10, false), + }; + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).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, null), Times.Once); + _productStorageContract.Verify(x => x.GetList(false, null), Times.Once); + } + + [Test] + public void GetAllProducts_ReturnEmptyList_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).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(), null), Times.Exactly(2)); + } + + [Test] + public void GetAllProducts_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetAllProducts(It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllProducts_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetAllProducts(It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllProductsByBlank_ReturnListOfRecords_Test() + { + //Arrange + var blankId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "name 1", ProductType.Furniture, 10, false), + new(Guid.NewGuid().ToString(), "name 2", ProductType.Furniture, 10, false), + new(Guid.NewGuid().ToString(), "name 3", ProductType.Furniture, 10, false), + }; + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = _productBusinessLogicContract.GetAllProductsByBlank(blankId, true); + var list = _productBusinessLogicContract.GetAllProductsByBlank(blankId, 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, blankId), Times.Once); + _productStorageContract.Verify(x => x.GetList(false, blankId), Times.Once); + } + + [Test] + public void GetAllProductsByBlank_ReturnEmptyList_Test() + { + //Arrange + var blankId = Guid.NewGuid().ToString(); + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = _productBusinessLogicContract.GetAllProductsByBlank(blankId, true); + var list = _productBusinessLogicContract.GetAllProductsByBlank(blankId, 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(), blankId), Times.Exactly(2)); + } + + [Test] + public void GetAllProductsByBlank_BlankIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetAllProductsByBlank(null, It.IsAny()), Throws.TypeOf()); + Assert.That(() => _productBusinessLogicContract.GetAllProductsByBlank(string.Empty, It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllProductsByBlank_BlankIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetAllProductsByBlank("blankId", It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllProductsByBlank_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetAllProductsByBlank(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllProductsByBlank_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetAllProductsByBlank(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetProductHistoryByProduct_ReturnListOfRecords_Test() + { + //Arrange + var productId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), 10), + new(Guid.NewGuid().ToString(), 15), + new(Guid.NewGuid().ToString(), 10), + }; + _productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny())).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())).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()), Times.Once); + } + + [Test] + public void GetProductHistoryByProduct_ProductIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(null), Throws.TypeOf()); + Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(string.Empty), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny()), Times.Never); + } + + [Test] + public void GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct("productId"), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny()), Times.Never); + } + + [Test] + public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductHistoryByProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new ProductDataModel(id, "name", ProductType.Furniture, 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()), Times.Once); + } + + [Test] + public void GetProductByData_GetByName_ReturnRecord_Test() + { + //Arrange + var name = "name"; + var record = new ProductDataModel(Guid.NewGuid().ToString(), name, ProductType.Furniture, 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()), Times.Once); + } + + [Test] + public void GetProductByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductByData(null), Throws.TypeOf()); + Assert.That(() => _productBusinessLogicContract.GetProductByData(string.Empty), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); + _productStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); + } + + [Test] + public void GetProductByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_GetByName_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductByData("name"), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _productStorageContract.Setup(x => x.GetElementByName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => _productBusinessLogicContract.GetProductByData("name"), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _productStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void InsertProduct_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Furniture, 10, false); + _productStorageContract.Setup(x => x.AddElement(It.IsAny())) + .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.InsertProduct(record); + //Assert + _productStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertProduct_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Furniture , 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertProduct_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.InsertProduct(null), Throws.TypeOf()); + _productStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertProduct_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.InsertProduct(new ProductDataModel("id", "name", ProductType.Furniture, 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Furniture, 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateProduct_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Furniture, 10, false); + _productStorageContract.Setup(x => x.UpdElement(It.IsAny())) + .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()), Times.Once); + Assert.That(flag); + } + + [Test] + public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Furniture, 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateProduct_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "anme", ProductType.Furniture, 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateProduct_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.UpdateProduct(null), Throws.TypeOf()); + _productStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateProduct_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.UpdateProduct(new ProductDataModel("id", "name", ProductType.Furniture, 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Furniture, 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => x.UpdElement(It.IsAny()), 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()), 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())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct(null), Throws.TypeOf()); + Assert.That(() => _productBusinessLogicContract.DeleteProduct(string.Empty), Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteProduct_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct("id"), Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } +} diff --git a/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/WorkerBusinessLogicContractTests.cs b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/WorkerBusinessLogicContractTests.cs new file mode 100644 index 0000000..7b6dc10 --- /dev/null +++ b/PapaCarloProject/PapaCarloTests/BusinessLogicsContracts/WorkerBusinessLogicContractTests.cs @@ -0,0 +1,562 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PapaCarloBusinessLogic.Implementations; +using PapaCarloContracts.DataModels; +using PapaCarloContracts.Exceptions; +using PapaCarloContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PapaCarloTests.BusinessLogicsContracts; + +[TestFixture] +internal class WorkerBusinessLogicContractTests +{ + private WorkerBusinessLogicContract _workerBusinessLogicContract; + private Mock _workerStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _workerStorageContract = new Mock(); + _workerBusinessLogicContract = new WorkerBusinessLogicContract(_workerStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _workerStorageContract.Reset(); + } + + [Test] + public void GetAllWorkers_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + 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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), null, null, null, null, null), Times.Exactly(2)); + } + + [Test] + public void GetAllWorkers_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkers_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), null, null, null, null, null), Times.Once); + } + + [Test] + public void GetAllWorkersByPost_ReturnListOfRecords_Test() + { + //Arrange + var postId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + 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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); + } + + [Test] + public void GetAllWorkersByPost_PostIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(null, It.IsAny()), Throws.TypeOf()); + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(string.Empty, It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByPost_PostIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost("postId", It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByPost_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByPost_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + 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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), null, null), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, null, It.IsAny(), It.IsAny(), 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()), Throws.TypeOf()); + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddSeconds(-1), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + 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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny()), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, null, null, null, It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date, It.IsAny()), Throws.TypeOf()); + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddSeconds(-1), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), 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()), 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()), Times.Once); + } + + [Test] + public void GetWorkerByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(null), Throws.TypeOf()); + Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(string.Empty), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + } + + [Test] + public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + } + + [Test] + public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); + } + + [Test] + public void GetWorkerByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _workerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), 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(-16).AddDays(-1), DateTime.Now, false); + _workerStorageContract.Setup(x => x.AddElement(It.IsAny())) + .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()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertWorker_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertWorker_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.InsertWorker(null), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), 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()); + _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), 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(-16).AddDays(-1), DateTime.Now, false); + _workerStorageContract.Setup(x => x.UpdElement(It.IsAny())) + .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()), Times.Once); + Assert.That(flag); + } + + [Test] + public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateWorker_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.UpdateWorker(null), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), 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()); + _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), 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()), 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()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null), Throws.TypeOf()); + Assert.That(() => _workerBusinessLogicContract.DeleteWorker(string.Empty), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteWorker_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } +}