From 195fd57378552dd5666be9375ad9a3e8820b408e Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Wed, 19 Feb 2025 20:48:02 +0400 Subject: [PATCH 1/9] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PipingHot/DataModels/OrderDataModel.cs | 4 ++- .../StoragesContracts/IPostStorageContract.cs | 20 +++++++++++++ .../IProductStorageContract.cs | 18 +++++++++++ .../IRestaurantStorageContract.cs | 19 ++++++++++++ .../DataModelsTests/OrderDataModelTests.cs | 30 ++++++++++--------- 5 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs create mode 100644 PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs create mode 100644 PipingHot/PipingHot/StoragesContracts/IRestaurantStorageContract.cs diff --git a/PipingHot/PipingHot/DataModels/OrderDataModel.cs b/PipingHot/PipingHot/DataModels/OrderDataModel.cs index d3b70dd..7906054 100644 --- a/PipingHot/PipingHot/DataModels/OrderDataModel.cs +++ b/PipingHot/PipingHot/DataModels/OrderDataModel.cs @@ -9,13 +9,15 @@ using System.Threading.Tasks; namespace PipingHotContrast.DataModels; -public class OrderDataModel(string id, string workerId, string? restaurantId, double sum, List products):IValidation +public class OrderDataModel(string id, string workerId, string? restaurantId, double sum, bool isCancel, List products):IValidation { public string Id { get; private set; } = id; public string WorkerId { get; private set; } = workerId; public string? RestaurantId { get; private set; } = restaurantId; public DateTime OrderDate { get; private set; } = DateTime.UtcNow; public double Sum { get; private set; } = sum; + public bool IsCancel { get; private set; } = isCancel; + public List Products { get; private set; } = products; public void Validate() diff --git a/PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs new file mode 100644 index 0000000..c783322 --- /dev/null +++ b/PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs @@ -0,0 +1,20 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.StoragesContracts; + +public interface IPostStorageContract +{ + List GetList(bool onlyActual = true); + List GetPostWithHistory(string postId); + PostDataModel? GetElementById(int id); + PostDataModel? GetElementByName(string name); + void AddElement(PostDataModel postDataModel); + void UpdElement(PostDataModel postDataModel); + void DelElement(string id); + void ResElement(string id); +} diff --git a/PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs new file mode 100644 index 0000000..b90d839 --- /dev/null +++ b/PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs @@ -0,0 +1,18 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.StoragesContracts; + +public interface IProductStorageContract +{ + List GetList(bool onlyActive = true, string? restaurantId = null); + List GetHistoryByProductId(string productId); + ProductDataModel? GetElementById(string id); + ProductDataModel? GetElementByName(string name); + void AddElement(ProductDataModel productDataModel); + void UpdElement(ProductDataModel productDataModel); +} diff --git a/PipingHot/PipingHot/StoragesContracts/IRestaurantStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IRestaurantStorageContract.cs new file mode 100644 index 0000000..1033e59 --- /dev/null +++ b/PipingHot/PipingHot/StoragesContracts/IRestaurantStorageContract.cs @@ -0,0 +1,19 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.StoragesContracts; + +public interface IRestaurantStorageContract +{ + List GetList(); + RestaurantDataModel? GetElementById(string id); + RestaurantDataModel? GetElementByName(string name); + RestaurantDataModel? GetElementByOldName(string name); + void AddElement(RestaurantDataModel restaurantDataModel); + void UpdElement(RestaurantDataModel restaurantDataModel); + void DelElement(string id); +} diff --git a/PipingHot/PipingHotTests/DataModelsTests/OrderDataModelTests.cs b/PipingHot/PipingHotTests/DataModelsTests/OrderDataModelTests.cs index 727f775..d18c41a 100644 --- a/PipingHot/PipingHotTests/DataModelsTests/OrderDataModelTests.cs +++ b/PipingHot/PipingHotTests/DataModelsTests/OrderDataModelTests.cs @@ -14,57 +14,57 @@ internal class OrderDataModelTests [Test] public void IdIsNullOrEmptyTest() { - var order = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, CreateSubDataModel()); + var order = CreateDataModel(null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); - order = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, CreateSubDataModel()); + order = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); } [Test] public void IdIsNotGuidTest() { - var order = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, CreateSubDataModel()); + var order = CreateDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); } [Test] public void WorkerIdIsNullOrEmptyTest() { - var order = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), 10, CreateSubDataModel()); + var order = CreateDataModel(Guid.NewGuid().ToString(), null, Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); - order = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 10, CreateSubDataModel()); + order = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); } [Test] public void WorkerIdIsNotGuidTest() { - var order = CreateDataModel(Guid.NewGuid().ToString(), "employeeId", Guid.NewGuid().ToString(), 10, CreateSubDataModel()); + var order = CreateDataModel(Guid.NewGuid().ToString(), "employeeId", Guid.NewGuid().ToString(), 10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); } [Test] public void RestaurantIdIsNotGuidTest() { - var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "restaurantId", 10, CreateSubDataModel()); + var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "restaurantId", 10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); } [Test] public void SumIsLessOrZeroTest() { - var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, CreateSubDataModel()); + var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); - order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, CreateSubDataModel()); + order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, false, CreateSubDataModel()); Assert.That(() => order.Validate(), Throws.TypeOf()); } [Test] public void ProductsIsNullOrEmptyTest() { - var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, null); + var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, null); Assert.That(() => order.Validate(), Throws.TypeOf()); - order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, []); + order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false,[]); Assert.That(() => order.Validate(), Throws.TypeOf()); } @@ -75,8 +75,9 @@ internal class OrderDataModelTests var workerId = Guid.NewGuid().ToString(); var restaurantId = Guid.NewGuid().ToString(); var sum = 10; + var isCancel = true; var products = CreateSubDataModel(); - var order = CreateDataModel(orderId, workerId, restaurantId, sum, products); + var order = CreateDataModel(orderId, workerId, restaurantId, sum, isCancel, products); Assert.That(() => order.Validate(), Throws.Nothing); Assert.Multiple(() => { @@ -84,12 +85,13 @@ internal class OrderDataModelTests Assert.That(order.WorkerId, Is.EqualTo(workerId)); Assert.That(order.RestaurantId, Is.EqualTo(restaurantId)); Assert.That(order.Sum, Is.EqualTo(sum)); + Assert.That(order.IsCancel, Is.EqualTo(isCancel)); Assert.That(order.Products, Is.EquivalentTo(products)); }); } - private static OrderDataModel CreateDataModel(string? id, string? workerId, string? restaurantId, double sum, List? products) => - new(id, workerId, restaurantId, sum, products); + private static OrderDataModel CreateDataModel(string? id, string? workerId, string? restaurantId, double sum, bool isCancel, List? products) => + new(id, workerId, restaurantId, sum, isCancel, products); private static List CreateSubDataModel() => [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]; -- 2.25.1 From 07f7204aab5e26dd8321a2c2d265607a207277b9 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 20 Feb 2025 17:32:50 +0400 Subject: [PATCH 2/9] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=82=D1=80=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IOrderBusinessLogicContract.cs | 23 +++++++++++++++++++ .../IPostBusinessLogicContract.cs | 20 ++++++++++++++++ .../IProductBusinessLogicContract.cs | 21 +++++++++++++++++ .../IRestaurantBusinessLogicContract.cs | 20 ++++++++++++++++ .../ISalaryBusinessLogicContract.cs | 16 +++++++++++++ .../IWorkerBusinessLogicContract.cs | 20 ++++++++++++++++ .../PipingHot/DataModels/OrderDataModel.cs | 9 +++++--- .../IOrderStorageContract.cs | 17 ++++++++++++++ .../ISalaryStorageContract.cs | 14 +++++++++++ .../IWorkerStorageContract.cs | 19 +++++++++++++++ 10 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 PipingHot/PipingHot/BusinessLogicsContracts/IOrderBusinessLogicContract.cs create mode 100644 PipingHot/PipingHot/BusinessLogicsContracts/IPostBusinessLogicContract.cs create mode 100644 PipingHot/PipingHot/BusinessLogicsContracts/IProductBusinessLogicContract.cs create mode 100644 PipingHot/PipingHot/BusinessLogicsContracts/IRestaurantBusinessLogicContract.cs create mode 100644 PipingHot/PipingHot/BusinessLogicsContracts/ISalaryBusinessLogicContract.cs create mode 100644 PipingHot/PipingHot/BusinessLogicsContracts/IWorkerBusinessLogicContract.cs create mode 100644 PipingHot/PipingHot/StoragesContracts/IOrderStorageContract.cs create mode 100644 PipingHot/PipingHot/StoragesContracts/ISalaryStorageContract.cs create mode 100644 PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs diff --git a/PipingHot/PipingHot/BusinessLogicsContracts/IOrderBusinessLogicContract.cs b/PipingHot/PipingHot/BusinessLogicsContracts/IOrderBusinessLogicContract.cs new file mode 100644 index 0000000..0a36c40 --- /dev/null +++ b/PipingHot/PipingHot/BusinessLogicsContracts/IOrderBusinessLogicContract.cs @@ -0,0 +1,23 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.BusinessLogicsContracts; + +public interface IOrderBusinessLogicContract +{ + List GetAllOrdersByPeriod(DateTime fromDate, DateTime +toDate); + List GetAllOrdersByWorkerByPeriod(string workerId, DateTime + fromDate, DateTime toDate); + List GetAllOrdersByRestaurantByPeriod(string restaurantId, DateTime + fromDate, DateTime toDate); + List GetAllOrdersByProductByPeriod(string productId, DateTime + fromDate, DateTime toDate); + OrderDataModel GetOrderByData(string data); + void InsertOrder(OrderDataModel orderDataModel); + void CancelOrder(string id); +} diff --git a/PipingHot/PipingHot/BusinessLogicsContracts/IPostBusinessLogicContract.cs b/PipingHot/PipingHot/BusinessLogicsContracts/IPostBusinessLogicContract.cs new file mode 100644 index 0000000..2f966ed --- /dev/null +++ b/PipingHot/PipingHot/BusinessLogicsContracts/IPostBusinessLogicContract.cs @@ -0,0 +1,20 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.BusinessLogicsContracts; + + public interface IPostBusinessLogicContract +{ + List GetAllPosts(bool onlyActive); + List GetAllDataOfPost(string postId); + PostDataModel GetPostByData(string data); + void InsertPost(PostDataModel postDataModel); + void UpdatePost(PostDataModel postDataModel); + void DeletePost(string id); + void RestorePost(string id); + +} diff --git a/PipingHot/PipingHot/BusinessLogicsContracts/IProductBusinessLogicContract.cs b/PipingHot/PipingHot/BusinessLogicsContracts/IProductBusinessLogicContract.cs new file mode 100644 index 0000000..9a15d31 --- /dev/null +++ b/PipingHot/PipingHot/BusinessLogicsContracts/IProductBusinessLogicContract.cs @@ -0,0 +1,21 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.BusinessLogicsContracts; + +public interface IProductBusinessLogicContract +{ + List GetAllProducts(bool onlyActive = true); + List GetAllProductsByRestaurant(string restaurantId, + bool onlyActive = true); + List GetProductHistoryByProduct(string productId); + ProductDataModel GetProductByData(string data); + void InsertProduct(ProductDataModel productDataModel); + void UpdateProduct(ProductDataModel productDataModel); + void DeleteProduct(string id); + +} diff --git a/PipingHot/PipingHot/BusinessLogicsContracts/IRestaurantBusinessLogicContract.cs b/PipingHot/PipingHot/BusinessLogicsContracts/IRestaurantBusinessLogicContract.cs new file mode 100644 index 0000000..9d3d11e --- /dev/null +++ b/PipingHot/PipingHot/BusinessLogicsContracts/IRestaurantBusinessLogicContract.cs @@ -0,0 +1,20 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.BusinessLogicsContracts; + +public interface IRestaurantBusinessLogicContract +{ + + List GetAllRestaurants(); + RestaurantDataModel GetRestaurantByData(string data); + void InsertRestaurant(RestaurantDataModel restaurantDataModel); + void UpdateRestaurant(RestaurantDataModel restaurantDataModel); + + void DeleteRestaurant(string id); + +} diff --git a/PipingHot/PipingHot/BusinessLogicsContracts/ISalaryBusinessLogicContract.cs b/PipingHot/PipingHot/BusinessLogicsContracts/ISalaryBusinessLogicContract.cs new file mode 100644 index 0000000..f7e3dad --- /dev/null +++ b/PipingHot/PipingHot/BusinessLogicsContracts/ISalaryBusinessLogicContract.cs @@ -0,0 +1,16 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.BusinessLogicsContracts; + +public interface ISalaryBusinessLogicContract +{ + List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate); + + List GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId); + void CalculateSalaryByMounth(DateTime date); +} diff --git a/PipingHot/PipingHot/BusinessLogicsContracts/IWorkerBusinessLogicContract.cs b/PipingHot/PipingHot/BusinessLogicsContracts/IWorkerBusinessLogicContract.cs new file mode 100644 index 0000000..d17ed44 --- /dev/null +++ b/PipingHot/PipingHot/BusinessLogicsContracts/IWorkerBusinessLogicContract.cs @@ -0,0 +1,20 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.BusinessLogicsContracts; + +public interface IWorkerBusinessLogicContract +{ + List GetAllWorkers(bool onlyActive = true); + List GetAllWorkersByPost(string postId, bool onlyActive = true); + List GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true); + List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true); + WorkerDataModel GetWorkerByData(string data); + void InsertWorker(WorkerDataModel workerDataModel); + void UpdateWorker(WorkerDataModel workerDataModel); + void DeleteWorker(string id); +} diff --git a/PipingHot/PipingHot/DataModels/OrderDataModel.cs b/PipingHot/PipingHot/DataModels/OrderDataModel.cs index 7906054..2eafd1f 100644 --- a/PipingHot/PipingHot/DataModels/OrderDataModel.cs +++ b/PipingHot/PipingHot/DataModels/OrderDataModel.cs @@ -9,11 +9,11 @@ using System.Threading.Tasks; namespace PipingHotContrast.DataModels; -public class OrderDataModel(string id, string workerId, string? restaurantId, double sum, bool isCancel, List products):IValidation +public class OrderDataModel(string id, string workerId, string restaurantId, double sum, bool isCancel, List products):IValidation { public string Id { get; private set; } = id; public string WorkerId { get; private set; } = workerId; - public string? RestaurantId { get; private set; } = restaurantId; + public string RestaurantId { get; private set; } = restaurantId; public DateTime OrderDate { get; private set; } = DateTime.UtcNow; public double Sum { get; private set; } = sum; public bool IsCancel { get; private set; } = isCancel; @@ -34,7 +34,10 @@ public class OrderDataModel(string id, string workerId, string? restaurantId, do if (!WorkerId.IsGuid()) throw new ValidationException("The value in the field WorkerId is not a unique identifier"); - if (!RestaurantId?.IsGuid() ?? !RestaurantId?.IsEmpty() ?? false) + if (RestaurantId.IsEmpty()) + throw new ValidationException("Field RestaurantId is empty"); + + if (!RestaurantId.IsGuid()) throw new ValidationException("The value in the field RestaurantId is not a unique identifier"); if (Sum <= 0) diff --git a/PipingHot/PipingHot/StoragesContracts/IOrderStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IOrderStorageContract.cs new file mode 100644 index 0000000..04f2c72 --- /dev/null +++ b/PipingHot/PipingHot/StoragesContracts/IOrderStorageContract.cs @@ -0,0 +1,17 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.StoragesContracts; + +public interface IOrderStorageContract +{ + List GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null, string? restaurantId = null, string? productId = null); + OrderDataModel? GetElementById(string id); + void AddElement(OrderDataModel orderDataModel); + void DelElement(string id); +} diff --git a/PipingHot/PipingHot/StoragesContracts/ISalaryStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/ISalaryStorageContract.cs new file mode 100644 index 0000000..9cc67c8 --- /dev/null +++ b/PipingHot/PipingHot/StoragesContracts/ISalaryStorageContract.cs @@ -0,0 +1,14 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.StoragesContracts; + +public interface ISalaryStorageContract +{ + List GetList(DateTime startDate, DateTime engDate, string? workerId = null); + void AddElement(SalaryDataModel salaryDataModel); +} diff --git a/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs new file mode 100644 index 0000000..d04782e --- /dev/null +++ b/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs @@ -0,0 +1,19 @@ +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.StoragesContracts; + +public interface IWorkerStorageContract +{ + List GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirtDate = null, DateTime? toBirtDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null); + WorkerDataModel? GetElementById(string id); + WorkerDataModel? GetElementByFIO(string fio); + void AddElement(WorkerDataModel workerDataModel); + void UpdElement(WorkerDataModel woworkerDataModel); + void DelElement(string id); + +} -- 2.25.1 From a7bfe8177a110e1cf2b9535b9194c53d57d779e3 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 20 Feb 2025 18:28:44 +0400 Subject: [PATCH 3/9] =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrderBusinessLogicContract.cs | 42 ++++++++++++++++ .../PostBusinessLogicContract.cs | 47 ++++++++++++++++++ .../ProductBusinessLogicContract.cs | 44 +++++++++++++++++ .../RestaurantBusinessLogicContract.cs | 37 ++++++++++++++ .../SalaryBusinessLogicContract.cs | 26 ++++++++++ .../WorkerBusinessLogicContract.cs | 48 +++++++++++++++++++ .../PipingHotBusinessLogic.csproj | 10 ++++ 7 files changed, 254 insertions(+) create mode 100644 PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs create mode 100644 PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs create mode 100644 PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs create mode 100644 PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs create mode 100644 PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs create mode 100644 PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs create mode 100644 PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs new file mode 100644 index 0000000..b21d83e --- /dev/null +++ b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs @@ -0,0 +1,42 @@ +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotBusinessLogic.Implementations; + +internal class OrderBusinessLogicContract : IOrderBusinessLogicContract +{ + public List GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate) + { + return []; + } + public List GetAllOrdersByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) + { + return []; + } + public List GetAllOrdersByRestaurantByPeriod(string restaurantId, DateTime fromDate, DateTime toDate) + { + return []; + } + public List GetAllOrdersByProductByPeriod(string productId, DateTime fromDate, DateTime toDate) + { + return []; + } + public OrderDataModel GetOrderByData(string data) + { + return new("", "", "", 0, true, []); + } + + public void InsertOrder(OrderDataModel orderDataModel) + { + + } + public void CancelOrder(string id) + { + + } +} diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs new file mode 100644 index 0000000..1a1cf58 --- /dev/null +++ b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -0,0 +1,47 @@ +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using PipingHotContrast.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotBusinessLogic.Implementations; + +internal class PostBusinessLogicContract : IPostBusinessLogicContract +{ + public List GetAllPosts(bool onlyActive) + { + return []; + } + + public List GetAllDataOfPost(string postId) + { + return []; + } + public PostDataModel GetPostByData(string data) + { + return new("", "","", PostType.None, 0, true, DateTime.UtcNow); + } + + public void InsertPost(PostDataModel postDataModel) + { + + } + public void UpdatePost(PostDataModel postDataModel) + { + + } + public void DeletePost(string id) + { + + } + + public void RestorePost(string id) + { + + } + + +} diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs new file mode 100644 index 0000000..5bc0d5a --- /dev/null +++ b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -0,0 +1,44 @@ +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using PipingHotContrast.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotBusinessLogic.Implementations; + +internal class ProductBusinessLogicContract : IProductBusinessLogicContract +{ + public List GetAllProducts(bool onlyActive = true) + { + return []; + } + + public List GetAllProductsByRestaurant(string restaurantId, bool onlyActive = true) + { + return []; + } + public List GetProductHistoryByProduct(string productId) + { + return []; + } + public ProductDataModel GetProductByData(string data) + { + return new("", "", ProductType.None, "", 0, true); + } + public void InsertProduct(ProductDataModel productDataModel) + { + + } + + public void UpdateProduct(ProductDataModel productDataModel) + { + + } + public void DeleteProduct(string id) + { + + } +} diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs new file mode 100644 index 0000000..71313ef --- /dev/null +++ b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs @@ -0,0 +1,37 @@ + +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotBusinessLogic.Implementations; + +internal class RestaurantBusinessLogicContract : IRestaurantBusinessLogicContract +{ + public List GetAllRestaurants() + { + return []; + } + + public RestaurantDataModel GetRestaurantByData(string data) + { + return new("", "", null, null); + } + + public void InsertRestaurant(RestaurantDataModel restaurantDataModel) + { + + } + + public void UpdateRestaurant(RestaurantDataModel restaurantDataModel) + { + + } + public void DeleteRestaurant(string id) + { + + } +} diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs new file mode 100644 index 0000000..bdd6b01 --- /dev/null +++ b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -0,0 +1,26 @@ +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotBusinessLogic.Implementations; + +internal class SalaryBusinessLogicContract : ISalaryBusinessLogicContract +{ + public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) + { + return []; + } + + public List GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId) + { + return []; + } + public void CalculateSalaryByMounth(DateTime date) + { + + } +} diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs new file mode 100644 index 0000000..3aab982 --- /dev/null +++ b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -0,0 +1,48 @@ +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotBusinessLogic.Implementations; + +internal class WorkerBusinessLogicContract : IWorkerBusinessLogicContract +{ + public List GetAllWorkers(bool onlyActive = true) + { + return []; + } + public List GetAllWorkersByPost(string postId, 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 []; + } + public WorkerDataModel GetWorkerByData(string data) + { + return new("", "", "","", DateTime.Now, DateTime.Now, true); + } + + public void InsertWorker(WorkerDataModel workerDataModel) + { + + } + + public void UpdateWorker(WorkerDataModel workerDataModel) + { + + } + public void DeleteWorker(string id) + { + + } +} diff --git a/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj b/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj new file mode 100644 index 0000000..ac07c5b --- /dev/null +++ b/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj @@ -0,0 +1,10 @@ + + + + net9.0-windows + enable + true + enable + + + -- 2.25.1 From d14fa74e384d38f65d9ea6fcc973407056e6871a Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 20 Feb 2025 20:07:10 +0400 Subject: [PATCH 4/9] =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PipingHot/PipingHot.sln | 6 +++ .../PipingHot/DataModels/PostDataModel.cs | 11 ++--- .../Exceptions/DateTimeExtensions.cs | 15 +++++++ .../Exceptions/ElementExistsException.cs | 18 +++++++++ .../Exceptions/ElementNotFoundException.cs | 16 ++++++++ .../Exceptions/IncorrectDatesException.cs | 14 +++++++ .../PipingHot/Exceptions/NullListException.cs | 12 ++++++ .../PipingHot/Exceptions/StorageException.cs | 13 ++++++ .../OrderBusinessLogicContract.cs | 6 ++- .../PostBusinessLogicContract.cs | 8 ++-- .../ProductBusinessLogicContract.cs | 6 ++- .../RestaurantBusinessLogicContract.cs | 4 +- .../SalaryBusinessLogicContract.cs | 7 +++- .../WorkerBusinessLogicContract.cs | 6 ++- .../PipingHotBusinessLogic.csproj | 13 ++++++ .../RestaurantBusinessLogicsContractsTests.cs | 11 +++++ .../DataModelsTests/PostDataModelTests.cs | 40 +++++-------------- 17 files changed, 157 insertions(+), 49 deletions(-) create mode 100644 PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs create mode 100644 PipingHot/PipingHot/Exceptions/ElementExistsException.cs create mode 100644 PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs create mode 100644 PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs create mode 100644 PipingHot/PipingHot/Exceptions/NullListException.cs create mode 100644 PipingHot/PipingHot/Exceptions/StorageException.cs create mode 100644 PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs diff --git a/PipingHot/PipingHot.sln b/PipingHot/PipingHot.sln index 7a54cdd..cfb03a4 100644 --- a/PipingHot/PipingHot.sln +++ b/PipingHot/PipingHot.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PipingHotContrast", "Piping EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PipingHotTests", "PipingHotTests\PipingHotTests.csproj", "{2269D181-1994-4BB3-A024-611BB25239AE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PipingHotBusinessLogic", "PipingHotBusinessLogic\PipingHotBusinessLogic.csproj", "{808448EA-2CE2-474C-9F95-F4ACBF170592}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {2269D181-1994-4BB3-A024-611BB25239AE}.Debug|Any CPU.Build.0 = Debug|Any CPU {2269D181-1994-4BB3-A024-611BB25239AE}.Release|Any CPU.ActiveCfg = Release|Any CPU {2269D181-1994-4BB3-A024-611BB25239AE}.Release|Any CPU.Build.0 = Release|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Debug|Any CPU.Build.0 = Debug|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Release|Any CPU.ActiveCfg = Release|Any CPU + {808448EA-2CE2-474C-9F95-F4ACBF170592}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PipingHot/PipingHot/DataModels/PostDataModel.cs b/PipingHot/PipingHot/DataModels/PostDataModel.cs index bf8e295..da223e1 100644 --- a/PipingHot/PipingHot/DataModels/PostDataModel.cs +++ b/PipingHot/PipingHot/DataModels/PostDataModel.cs @@ -11,10 +11,9 @@ using System.Threading.Tasks; namespace PipingHotContrast.DataModels; -public class PostDataModel(string id, string postId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate): IValidation +public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate): IValidation { - public string Id { get; private set; } = id; - public string PostId { get; private set; } = postId; + public string Id { get; private set; } = id; public string PostName { get; private set; } = postName; public PostType PostType { get; private set; } = postType; public double Salary { get; private set; } = salary; @@ -26,11 +25,7 @@ public class PostDataModel(string id, string postId, string postName, PostType p if (Id.IsEmpty()) throw new ValidationException("Field Id is empty"); if (!Id.IsGuid()) - throw new ValidationException("The value in the field Id is not a unique identifier"); - if (PostId.IsEmpty()) - throw new ValidationException("Field PostId is empty"); - if (!PostId.IsGuid()) - throw new ValidationException("The value in the field PostId is not a unique identifier"); + throw new ValidationException("The value in the field Id is not a unique identifier"); if (PostName.IsEmpty()) throw new ValidationException("Field PostName is empty"); if (PostType==PostType.None) diff --git a/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs b/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs new file mode 100644 index 0000000..4067cfb --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public static class DateTimeExtensions +{ + public static bool IsDateNotOlder(this DateTime date, DateTime olderDate) + { + return date >= olderDate; + } +} diff --git a/PipingHot/PipingHot/Exceptions/ElementExistsException.cs b/PipingHot/PipingHot/Exceptions/ElementExistsException.cs new file mode 100644 index 0000000..6d3a301 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/ElementExistsException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class ElementExistsException:Exception +{ + public string ParamName { get; private set; } + public string ParamValue { get; private set; } + public ElementExistsException(string paramName, string paramValue) :base($"There is already an element with value{paramValue} of parameter { paramName}") + { + ParamName = paramName; + ParamValue = paramValue; + } +} diff --git a/PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs b/PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs new file mode 100644 index 0000000..888dda5 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/ElementNotFoundException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class ElementNotFoundException: Exception +{ + public string Value { get; private set; } + public ElementNotFoundException(string value) : base($"Element not found at value = { value}") + { + Value = value; + } +} diff --git a/PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs b/PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs new file mode 100644 index 0000000..0543e20 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/IncorrectDatesException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace PipingHotContrast.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/PipingHot/PipingHot/Exceptions/NullListException.cs b/PipingHot/PipingHot/Exceptions/NullListException.cs new file mode 100644 index 0000000..c62528b --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/NullListException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class NullListException:Exception +{ + public NullListException() : base("The returned list is null") { } +} diff --git a/PipingHot/PipingHot/Exceptions/StorageException.cs b/PipingHot/PipingHot/Exceptions/StorageException.cs new file mode 100644 index 0000000..dc5bbb1 --- /dev/null +++ b/PipingHot/PipingHot/Exceptions/StorageException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotContrast.Exceptions; + +public class StorageException:Exception +{ + public StorageException(Exception ex) : base($"Error while working instorage: { ex.Message}", ex) { } + +} diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs index b21d83e..375e30b 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs @@ -1,5 +1,7 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -8,7 +10,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class OrderBusinessLogicContract : IOrderBusinessLogicContract +internal class OrderBusinessLogicContract(IOrderStorageContract orderStorageContract, ILogger logger) : IOrderBusinessLogicContract { public List GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate) { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs index 1a1cf58..7207cb4 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -1,6 +1,8 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; using PipingHotContrast.Enums; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +11,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class PostBusinessLogicContract : IPostBusinessLogicContract +internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { public List GetAllPosts(bool onlyActive) { @@ -22,7 +24,7 @@ internal class PostBusinessLogicContract : IPostBusinessLogicContract } public PostDataModel GetPostByData(string data) { - return new("", "","", PostType.None, 0, true, DateTime.UtcNow); + return new("", "", PostType.None, 0, true, DateTime.UtcNow); } public void InsertPost(PostDataModel postDataModel) diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 5bc0d5a..6ef78e7 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -1,6 +1,8 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; using PipingHotContrast.Enums; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +11,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class ProductBusinessLogicContract : IProductBusinessLogicContract +internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract { public List GetAllProducts(bool onlyActive = true) { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs index 71313ef..098ee70 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs @@ -1,6 +1,8 @@  +using Microsoft.Extensions.Logging; using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +11,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class RestaurantBusinessLogicContract : IRestaurantBusinessLogicContract +internal class RestaurantBusinessLogicContract(IRestaurantStorageContract restaurantStorageContract, ILogger logger) : IRestaurantBusinessLogicContract { public List GetAllRestaurants() { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index bdd6b01..3c85957 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,5 +1,7 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -8,7 +10,8 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class SalaryBusinessLogicContract : ISalaryBusinessLogicContract +internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, + IOrderStorageContract orderStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index 3aab982..f1632ef 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -1,5 +1,7 @@ -using PipingHotContrast.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; @@ -8,7 +10,7 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; -internal class WorkerBusinessLogicContract : IWorkerBusinessLogicContract +internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract { public List GetAllWorkers(bool onlyActive = true) { diff --git a/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj b/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj index ac07c5b..80e2698 100644 --- a/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj +++ b/PipingHot/PipingHotBusinessLogic/PipingHotBusinessLogic.csproj @@ -6,5 +6,18 @@ true enable + + + + + + + + + + + + + diff --git a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs new file mode 100644 index 0000000..0e7d344 --- /dev/null +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotTests.BusinessLogicsContractsTests; + +internal class RestaurantBusinessLogicsContractsTests +{ +} diff --git a/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs b/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs index 7a26814..c71b5b6 100644 --- a/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs +++ b/PipingHot/PipingHotTests/DataModelsTests/PostDataModelTests.cs @@ -20,57 +20,41 @@ internal class PostDataModelTests [Test] public void IdIsNullOrEmptyTest() { - var post = CreateDataModel(null, Guid.NewGuid().ToString(), "name", PostType.Operator, 10, true, DateTime.UtcNow); + var post = CreateDataModel(null, "name", PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "name", PostType.Operator, 10, true, DateTime.UtcNow); + post = CreateDataModel(string.Empty, "name", PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } [Test] public void IdIsNotGuidTest() { - var post = CreateDataModel("id", Guid.NewGuid().ToString(), "name", PostType.Operator, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - } - - [Test] - public void PostIdIsNullEmptyTest() - { - var post = CreateDataModel(Guid.NewGuid().ToString(), null, "name", PostType.Operator, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "name", PostType.Operator, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - } - - [Test] - public void PostIdIsNotGuidTest() - { - var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "name", PostType.Operator, 10, true, DateTime.UtcNow); + var post = CreateDataModel("id", "name", PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } [Test] public void PostNameIsEmptyTest()//? { - var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, PostType.Operator, 10, true, DateTime.UtcNow); + var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); - manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, PostType.Operator, 10, true, DateTime.UtcNow); + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Operator, 10, true, DateTime.UtcNow); Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); } [Test] public void PostTypeIsNoneTest() { - var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow); + var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } [Test] public void SalaryIsLessOrZeroTest() { - var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Operator, 0, true, DateTime.UtcNow); + var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Operator, 0, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name", PostType.Operator, -10, true, DateTime.UtcNow); + post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Operator, -10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } @@ -78,18 +62,16 @@ internal class PostDataModelTests public void AllFieldsIsCorrectTest() { var postId = Guid.NewGuid().ToString(); - var postPostId = Guid.NewGuid().ToString(); var postName = "name"; var postType = PostType.Operator; var salary = 10; var isActual = false; var changeDate = DateTime.UtcNow.AddDays(-1); - var post = CreateDataModel(postId, postPostId, postName, postType, salary, isActual, changeDate); + var post = CreateDataModel(postId, postName, postType, salary, isActual, changeDate); Assert.That(() => post.Validate(), Throws.Nothing); Assert.Multiple(() => { Assert.That(post.Id, Is.EqualTo(postId)); - Assert.That(post.PostId, Is.EqualTo(postPostId)); Assert.That(post.PostName, Is.EqualTo(postName)); Assert.That(post.PostType, Is.EqualTo(postType)); Assert.That(post.Salary, Is.EqualTo(salary)); @@ -97,6 +79,6 @@ internal class PostDataModelTests Assert.That(post.ChangeDate, Is.EqualTo(changeDate)); }); } - private static PostDataModel CreateDataModel(string? id, string? postId, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) => - new(id, postId, postName, postType, salary, isActual, changeDate); + private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) => + new(id, postName, postType, salary, isActual, changeDate); } -- 2.25.1 From e6042c4137b0ea46d2284b1bcf78532648f6036c Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 20 Feb 2025 20:58:34 +0400 Subject: [PATCH 5/9] =?UTF-8?q?=D0=B2=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RestaurantBusinessLogicContract.cs | 5 +- .../RestaurantBusinessLogicsContractsTests.cs | 365 +++++++++++++++++- .../PipingHotTests/PipingHotTests.csproj | 2 + 3 files changed, 370 insertions(+), 2 deletions(-) diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs index 098ee70..d290c18 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs @@ -12,7 +12,10 @@ using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; internal class RestaurantBusinessLogicContract(IRestaurantStorageContract restaurantStorageContract, ILogger logger) : IRestaurantBusinessLogicContract -{ +{ + private readonly ILogger _logger = logger; + private readonly IRestaurantStorageContract _restaurantStorageContract = restaurantStorageContract; + public List GetAllRestaurants() { return []; diff --git a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs index 0e7d344..be450c8 100644 --- a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs @@ -1,4 +1,12 @@ -using System; +using Microsoft.Extensions.Logging; +using Moq; + +using PipingHotBusinessLogic.Implementations; +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using PipingHotContrast.Exceptions; +using PipingHotContrast.StoragesContracts; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,4 +16,359 @@ namespace PipingHotTests.BusinessLogicsContractsTests; internal class RestaurantBusinessLogicsContractsTests { + private RestaurantBusinessLogicContract _restaurantBusinessLogicContract; + private Mock _restaurantStorageContract; + [OneTimeSetUp] + public void OneTimeSetUp() + { + _restaurantStorageContract = new + Mock(); + _restaurantBusinessLogicContract = new + RestaurantBusinessLogicContract(_restaurantStorageContract.Object, new Mock().Object); + } + + [TearDown] + public void TearDown() + { + _restaurantStorageContract.Reset(); + } + [Test] + public void GetAllRestaurants_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "name 1", null, null), + new(Guid.NewGuid().ToString(), "name 2", null, null), + new(Guid.NewGuid().ToString(), "name 3", null, null), + }; + _restaurantStorageContract.Setup(x => x.GetList()).Returns(listOriginal); + //Act + var list = _restaurantBusinessLogicContract.GetAllRestaurants(); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + } + [Test] + public void GetAllRestaurants_ReturnEmptyList_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.GetList()).Returns([]); + //Act + var list = _restaurantBusinessLogicContract.GetAllRestaurants(); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _restaurantStorageContract.Verify(x => x.GetList(), Times.Once); + } + [Test] + public void GetAllRestaurants_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _restaurantBusinessLogicContract.GetAllRestaurants(), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.GetList(), Times.Once); + } + [Test] + public void GetAllRestaurants_StorageThrowError_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.GetList()).Throws(new + StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _restaurantBusinessLogicContract.GetAllRestaurants(), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.GetList(), Times.Once); + } + [Test] + public void GetRestaurantByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new RestaurantDataModel(id, "name", null, null); + _restaurantStorageContract.Setup(x => + x.GetElementById(id)).Returns(record); + + //Act + var element =_restaurantBusinessLogicContract.GetRestaurantByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _restaurantStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + [Test] + public void GetRestaurantByData_GetByName_ReturnRecord_Test() + { + //Arrange + var restaurantName = "name"; + var record = new RestaurantDataModel(Guid.NewGuid().ToString(), + restaurantName, null, null); + _restaurantStorageContract.Setup(x => + x.GetElementByName(restaurantName)).Returns(record); + //Act + var element = + _restaurantBusinessLogicContract.GetRestaurantByData(restaurantName); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.RestaurantName, Is.EqualTo(restaurantName)); + _restaurantStorageContract.Verify(x => + x.GetElementByName(It.IsAny()), Times.Once); + } + [Test] + public void GetRestaurantByData_GetByOldName_ReturnRecord_Test() + { + //Arrange + var restaurantOldName = "name before"; + var record = new RestaurantDataModel(Guid.NewGuid().ToString(), + "name", restaurantOldName, null); + _restaurantStorageContract.Setup(x => + x.GetElementByOldName(restaurantOldName)).Returns(record); + //Act + var element = + _restaurantBusinessLogicContract.GetRestaurantByData(restaurantOldName); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.PrevRestaurantName, + Is.EqualTo(restaurantOldName)); + _restaurantStorageContract.Verify(x => + x.GetElementByName(It.IsAny()), Times.Once); + _restaurantStorageContract.Verify(x => + x.GetElementByOldName(It.IsAny()), Times.Once); + } + [Test] + public void GetRestaurantByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _restaurantBusinessLogicContract.GetRestaurantByData(null), + Throws.TypeOf()); + Assert.That(() => + _restaurantBusinessLogicContract.GetRestaurantByData(string.Empty), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Never); + + _restaurantStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); + _restaurantStorageContract.Verify(x => + x.GetElementByOldName(It.IsAny()), Times.Never); + } + [Test] + public void GetRestaurantByData__GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _restaurantBusinessLogicContract.GetRestaurantByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _restaurantStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); + _restaurantStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Never); + } + [Test] + public void GetRestaurantByData_GetByNameOrOldName_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _restaurantBusinessLogicContract.GetRestaurantByData("name"), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _restaurantStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + _restaurantStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Once); + } + [Test] + public void GetRestaurantByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _restaurantStorageContract.Setup(x => x.GetElementByName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.GetRestaurantByData(Guid.NewGuid().ToString( + )), Throws.TypeOf()); + Assert.That(() => _restaurantBusinessLogicContract.GetRestaurantByData("name"), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _restaurantStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + _restaurantStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Never); + } + [Test] + public void GetRestaurantByData_GetByOldName_StorageThrowError_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.GetElementByOldName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.GetRestaurantByData("name"), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + _restaurantStorageContract.Verify(x => x.GetElementByOldName(It.IsAny()), Times.Once); + } + [Test] + public void InsertRestaurant_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new RestaurantDataModel(Guid.NewGuid().ToString(),"name", null, null); + _restaurantStorageContract.Setup(x => x.AddElement(It.IsAny())).Callback((RestaurantDataModel x) => + { + flag = x.Id == record.Id && x.RestaurantName == record.RestaurantName; + }); + //Act + _restaurantBusinessLogicContract.InsertRestaurant(record); + //Assert + _restaurantStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void InsertRestaurant_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() =>_restaurantBusinessLogicContract.InsertRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + [Test] + public void InsertRestaurant_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.InsertRestaurant(null), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + [Test] + public void InsertRestaurant_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.InsertRestaurant(new RestaurantDataModel("id", "name", null, null)), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + [Test] + public void InsertRestaurant_StorageThrowError_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.InsertRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + [Test] + public void UpdateRestaurant_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new RestaurantDataModel(Guid.NewGuid().ToString(),"name", null, null); + _restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny())).Callback((RestaurantDataModel x) => + { + flag = x.Id == record.Id && x.RestaurantName == record.RestaurantName; + }); + //Act + _restaurantBusinessLogicContract.UpdateRestaurant(record); + //Assert + _restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void UpdateRestaurant_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.UpdateRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + + } + [Test] + public void UpdateRestaurant_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.UpdateRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + [Test] + public void UpdateRestaurant_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() =>_restaurantBusinessLogicContract.UpdateRestaurant(null),Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + [Test] + public void UpdateRestaurant_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.UpdateRestaurant(new RestaurantDataModel("id", "name", null, null)), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + [Test] + public void UpdateRestaurant_StorageThrowError_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.UpdateRestaurant(new(Guid.NewGuid().ToString(), "name", null, null)), Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + [Test] + public void DeleteRestaurant_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _restaurantStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + //Act + + _restaurantBusinessLogicContract.DeleteRestaurant(id); + //Assert + _restaurantStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void DeleteRestaurant_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _restaurantStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.DeleteRestaurant(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + [Test] + public void DeleteRestaurant_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.DeleteRestaurant(null), + Throws.TypeOf()); + Assert.That(() => _restaurantBusinessLogicContract.DeleteRestaurant(string.Empty), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + [Test] + public void DeleteRestaurant_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.DeleteRestaurant("id"), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + [Test] + public void DeleteRestaurant_StorageThrowError_ThrowException_Test() + { + //Arrange + _restaurantStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => _restaurantBusinessLogicContract.DeleteRestaurant(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _restaurantStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + + + + + } diff --git a/PipingHot/PipingHotTests/PipingHotTests.csproj b/PipingHot/PipingHotTests/PipingHotTests.csproj index a57125d..ddede1e 100644 --- a/PipingHot/PipingHotTests/PipingHotTests.csproj +++ b/PipingHot/PipingHotTests/PipingHotTests.csproj @@ -11,12 +11,14 @@ + + -- 2.25.1 From c809d02e7b8078c2d5ead9390ad8fdd247654f44 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Sat, 22 Feb 2025 23:41:43 +0400 Subject: [PATCH 6/9] =?UTF-8?q?=D0=B2=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StoragesContracts/IPostStorageContract.cs | 2 +- .../IProductStorageContract.cs | 2 + .../IWorkerStorageContract.cs | 1 + .../PostBusinessLogicContract.cs | 68 +- .../ProductBusinessLogicContract.cs | 2 + .../RestaurantBusinessLogicContract.cs | 42 +- .../OrderBusinessLogicContractTests.cs | 636 +++++++++++++++ .../PostBusinessLogicContractTests.cs | 462 +++++++++++ .../ProductBusinessLogicContractTests.cs | 518 ++++++++++++ .../RestaurantBusinessLogicsContractsTests.cs | 1 + .../SalaryBusinessLogicContractTests.cs | 423 ++++++++++ .../WorkerBusinessLogicContractTests.cs | 746 ++++++++++++++++++ 12 files changed, 2889 insertions(+), 14 deletions(-) create mode 100644 PipingHot/PipingHotTests/BusinessLogicsContractsTests/OrderBusinessLogicContractTests.cs create mode 100644 PipingHot/PipingHotTests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs create mode 100644 PipingHot/PipingHotTests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs create mode 100644 PipingHot/PipingHotTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTests.cs create mode 100644 PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs diff --git a/PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs index c783322..e6dfc86 100644 --- a/PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs +++ b/PipingHot/PipingHot/StoragesContracts/IPostStorageContract.cs @@ -11,7 +11,7 @@ public interface IPostStorageContract { List GetList(bool onlyActual = true); List GetPostWithHistory(string postId); - PostDataModel? GetElementById(int id); + PostDataModel? GetElementById(string id); PostDataModel? GetElementByName(string name); void AddElement(PostDataModel postDataModel); void UpdElement(PostDataModel postDataModel); diff --git a/PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs index b90d839..2a92c7e 100644 --- a/PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs +++ b/PipingHot/PipingHot/StoragesContracts/IProductStorageContract.cs @@ -15,4 +15,6 @@ public interface IProductStorageContract ProductDataModel? GetElementByName(string name); void AddElement(ProductDataModel productDataModel); void UpdElement(ProductDataModel productDataModel); + void DelElement(string id); + } diff --git a/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs index d04782e..7a942e1 100644 --- a/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs +++ b/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs @@ -12,6 +12,7 @@ public interface IWorkerStorageContract List GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirtDate = null, DateTime? toBirtDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null); WorkerDataModel? GetElementById(string id); WorkerDataModel? GetElementByFIO(string fio); + WorkerDataModel? GetElementByEmail(string email); void AddElement(WorkerDataModel workerDataModel); void UpdElement(WorkerDataModel woworkerDataModel); void DelElement(string id); diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs index 7207cb4..6b126a8 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -2,47 +2,99 @@ using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; using PipingHotContrast.Enums; +using PipingHotContrast.Exceptions; +using PipingHotContrast.Extensions; using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract -{ +{ + private readonly ILogger _logger = logger; + private readonly IPostStorageContract _postStorageContract = postStorageContract; public List GetAllPosts(bool onlyActive) { - return []; + logger.LogInformation("GetAllPosts params: {onlyActive}",onlyActive); + return _postStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetAllDataOfPost(string postId) { - return []; + _logger.LogInformation("GetAllDataOfPost for {postId}", postId); + if (postId.IsEmpty()) + { + throw new ArgumentNullException(nameof(postId)); + } + if (!postId.IsGuid()) + { + throw new ValidationException("The value in the field postId is not a unique identifier."); + + } + return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException(); + } public PostDataModel GetPostByData(string data) { - return new("", "", PostType.None, 0, true, DateTime.UtcNow); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _postStorageContract.GetElementById(data) ?? throw new + ElementNotFoundException(data); + } + return _postStorageContract.GetElementByName(data) ?? throw new + ElementNotFoundException(data); } public void InsertPost(PostDataModel postDataModel) { - + _logger.LogInformation("New data: {json}",JsonSerializer.Serialize(postDataModel)); + ArgumentNullException.ThrowIfNull(postDataModel); + postDataModel.Validate(); + _postStorageContract.AddElement(postDataModel); } public void UpdatePost(PostDataModel postDataModel) { - + _logger.LogInformation("New data: {json}",JsonSerializer.Serialize(postDataModel)); + ArgumentNullException.ThrowIfNull(postDataModel); + postDataModel.Validate(); + _postStorageContract.AddElement(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); } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 6ef78e7..5e52713 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -13,6 +13,8 @@ namespace PipingHotBusinessLogic.Implementations; internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IProductStorageContract _productStorageContract = productStorageContract; public List GetAllProducts(bool onlyActive = true) { return []; diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs index d290c18..4f5c7f2 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs @@ -2,11 +2,14 @@ using Microsoft.Extensions.Logging; using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.Exceptions; +using PipingHotContrast.Extensions; using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; @@ -18,25 +21,54 @@ internal class RestaurantBusinessLogicContract(IRestaurantStorageContract resta public List GetAllRestaurants() { - return []; + _logger.LogInformation("GetAllRestaurants"); + return _restaurantStorageContract.GetList() ?? throw new NullListException(); + } public RestaurantDataModel GetRestaurantByData(string data) { - return new("", "", null, null); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _restaurantStorageContract.GetElementById(data) ?? + throw new ElementNotFoundException(data); + } + return _restaurantStorageContract.GetElementByName(data) ?? + _restaurantStorageContract.GetElementByOldName(data) ?? + throw new ElementNotFoundException(data); } public void InsertRestaurant(RestaurantDataModel restaurantDataModel) { - + logger.LogInformation("New data: {json}", JsonSerializer.Serialize(restaurantDataModel)); + ArgumentNullException.ThrowIfNull(restaurantDataModel); + restaurantDataModel.Validate(); + _restaurantStorageContract.AddElement(restaurantDataModel); } public void UpdateRestaurant(RestaurantDataModel restaurantDataModel) { - + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(restaurantDataModel)); + ArgumentNullException.ThrowIfNull(restaurantDataModel); + restaurantDataModel.Validate(); + _restaurantStorageContract.UpdElement(restaurantDataModel); } public void DeleteRestaurant(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"); + } + _restaurantStorageContract.DelElement(id); } } diff --git a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/OrderBusinessLogicContractTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/OrderBusinessLogicContractTests.cs new file mode 100644 index 0000000..5778675 --- /dev/null +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/OrderBusinessLogicContractTests.cs @@ -0,0 +1,636 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PipingHotBusinessLogic.Implementations; +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using PipingHotContrast.Exceptions; +using PipingHotContrast.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotTests.BusinessLogicsContractsTests; + +[TestFixture] +internal class OrderBusinessLogicContractTests +{ + private OrderBusinessLogicContract _orderBusinessLogicContract; + private Mock _orderStorageContract; + [OneTimeSetUp] + public void OneTimeSetUp() + { + _orderStorageContract = new Mock(); + _orderBusinessLogicContract = new + OrderBusinessLogicContract(_orderStorageContract.Object, new + Mock().Object); + } + [TearDown] + public void TearDown() + { + _orderStorageContract.Reset(); + } + [Test] + public void GetAllOrdersByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, + [new OrderProductDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, []), + }; + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns(listOriginal); + //Act + var list = _orderBusinessLogicContract.GetAllOrdersByPeriod(date, date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, null), Times.Once); + } + [Test] + public void GetAllOrdersByPeriod_ReturnEmptyList_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns([]); + //Act + var list = _orderBusinessLogicContract.GetAllOrdersByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByPeriod_IncorrectDates_ThrowException_Test() + { + + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByPeriod(date, date),Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByPeriod(DateTime.UtcNow,DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + + [Test] + public void GetAllOrdersByWorkerByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var workerId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, + [new OrderProductDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, []), + }; + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns(listOriginal); + //Act + + var list =_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(workerId, date, date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), workerId, null, null), Times.Once); + } + [Test] + public void GetAllOrdersByWorkerByPeriod_ReturnEmptyList_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns([]); + //Act + var list = _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByWorkerByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByWorkerByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByWorkerByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByRestaurantByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var restaurantId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, + [new OrderProductDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, []), + }; + + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns(listOriginal); + //Act + var list = _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(restaurantId, date,date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, restaurantId, null), Times.Once); + } + [Test] + public void GetAllOrdersByRestaurantByPeriod_ReturnEmptyList_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns([]); + //Act + var list = + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByRestaurantByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(),date, date.AddSeconds(-1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByRestaurantByPeriod_RestaurantIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByRestaurantByPeriod_RestaurantIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod("restaurantId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByRestaurantByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByRestaurantByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(),DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByProductByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var productId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, + [new OrderProductDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10,false, []), + + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + null, 10, false, []), + }; + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns(listOriginal); + //Act + var list = + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(productId, date, + date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), + null, null, productId), Times.Once); + } + [Test] + public void GetAllOrdersByProductByPeriod_ReturnEmptyList_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Returns([]); + //Act + var list = _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString() + , DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByProductByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByProductByPeriod_ProductIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(null, DateTime.UtcNow,DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(string.Empty,DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByProductByPeriod_ProductIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod("productId",DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllOrdersByProductByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString() + , DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllOrdersByProductByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetOrderByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new OrderDataModel(id, Guid.NewGuid().ToString(), null, + 10, false, + [new OrderProductDataModel(Guid.NewGuid().ToString(),Guid.NewGuid().ToString(), 5)]); + _orderStorageContract.Setup(x =>x.GetElementById(id)).Returns(record); + + //Act + var element = _orderBusinessLogicContract.GetOrderByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _orderStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + [Test] + public void GetOrderByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _orderBusinessLogicContract.GetOrderByData(null), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.GetOrderByData(string.Empty), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + } + [Test] + public void GetOrderByData_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _orderBusinessLogicContract.GetOrderByData("saleId"), + Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + } + [Test] + public void GetOrderByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetOrderByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + [Test] + public void GetOrderByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => + x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.GetOrderByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void InsertOrder_CorrectRecord_Test() + { + //Arrange + var flag = false; + + var record = new OrderDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, + false, [new OrderProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); + _orderStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((OrderDataModel x) => + { + flag = x.Id == record.Id && x.WorkerId == + record.WorkerId && x.RestaurantId == record.RestaurantId && + x.OrderDate == record.OrderDate && x.Sum == + record.Sum && + x.IsCancel == record.IsCancel && x.Products.Count == record.Products.Count && + x.Products.First().ProductId == + record.Products.First().ProductId && + x.Products.First().OrderId == + record.Products.First().OrderId && + x.Products.First().Count == + record.Products.First().Count; + }); + //Act + _orderBusinessLogicContract.InsertOrder(record); + //Assert + _orderStorageContract.Verify(x =>x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void InsertOrder_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.InsertOrder(new(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, false, + [new OrderProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),5)])), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + [Test] + public void InsertOrder_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _orderBusinessLogicContract.InsertOrder(null), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + [Test] + public void InsertOrder_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _orderBusinessLogicContract.InsertOrder(new + OrderDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, + false, [])), Throws.TypeOf()); + + _orderStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertOrder_StorageThrowError_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.InsertOrder(new(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, false, + [new OrderProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),5)])), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + [Test] + public void CancelOrder_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _orderStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + //Act + _orderBusinessLogicContract.CancelOrder(id); + //Assert + _orderStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void CancelOrder_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _orderStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.CancelOrder(Guid.NewGuid().ToString()),Throws.TypeOf()); + _orderStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + [Test] + public void CancelOrder_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _orderBusinessLogicContract.CancelOrder(null), Throws.TypeOf()); + Assert.That(() => + _orderBusinessLogicContract.CancelOrder(string.Empty), Throws.TypeOf()); + _orderStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + [Test] + public void CancelOrder_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _orderBusinessLogicContract.CancelOrder("id"),Throws.TypeOf()); + _orderStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + [Test] + public void CancelOrder_StorageThrowError_ThrowException_Test() + { + //Arrange + _orderStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _orderBusinessLogicContract.CancelOrder(Guid.NewGuid().ToString()),Throws.TypeOf()); + _orderStorageContract.Verify(x => x.DelElement(It.IsAny()),Times.Once); + } +} + + + + + + + + + + + diff --git a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs new file mode 100644 index 0000000..0babedd --- /dev/null +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs @@ -0,0 +1,462 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PipingHotBusinessLogic.Implementations; +using PipingHotContrast.BusinessLogicsContracts; +using PipingHotContrast.DataModels; +using PipingHotContrast.Enums; +using PipingHotContrast.Exceptions; +using PipingHotContrast.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotTests.BusinessLogicsContractsTests; + +[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); + } + + [TearDown] + public void TearDown() + { + _postStorageContract.Reset(); + } + [Test] + public void GetAllPosts_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(),"name 1", PostType.Operator, 10, true, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 2", PostType.Operator, 10, false, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 3", PostType.Operator, 10, 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.Operator, 10, true, + DateTime.UtcNow), + new(postId, "name 2", PostType.Operator, 10, 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.Operator, 10,true, DateTime.UtcNow); + _postStorageContract.Setup(x => x.GetElementById(id)).Returns(record); + //Act + var element = _postBusinessLogicContract.GetPostByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _postStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + + + } + [Test] + public void GetPostByData_GetByName_ReturnRecord_Test() + { + //Arrange + var postName = "name"; + var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Operator, 10, true, DateTime.UtcNow); + _postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record); + //Act + var element = _postBusinessLogicContract.GetPostByData(postName); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.PostName, Is.EqualTo(postName)); + _postStorageContract.Verify(x => x.GetElementByName(It.IsAny()), 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.Deliveryman, 10, 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.Salary == record.Salary && 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.Deliveryman, 10, 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.Deliveryman, 10, 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.Deliveryman, 10, 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.Deliveryman, 10, 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.Salary == record.Salary && + 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.Deliveryman, 10, 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.Deliveryman, 10, 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.Deliveryman, 10, 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.Deliveryman, 10, 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/PipingHot/PipingHotTests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs new file mode 100644 index 0000000..7922fe0 --- /dev/null +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs @@ -0,0 +1,518 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PipingHotBusinessLogic.Implementations; +using PipingHotContrast.DataModels; +using PipingHotContrast.Enums; +using PipingHotContrast.Exceptions; +using PipingHotContrast.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotTests.BusinessLogicsContractsTests; +[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); + } + [TearDown] + public void TearDown() + { + _productStorageContract.Reset(); + } + + [Test] + public void GetAllProducts_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "name 1", + ProductType.Pizza, Guid.NewGuid().ToString(), 10, false), + new(Guid.NewGuid().ToString(), "name 2", + ProductType.Pizza, Guid.NewGuid().ToString(), 10, true), + new(Guid.NewGuid().ToString(), "name 3", + ProductType.Pizza, Guid.NewGuid().ToString(), 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 GetAllProductsByRestaurant_ReturnListOfRecords_Test() + { + //Arrange + var restaurantId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "name 1",ProductType.Pizza, + Guid.NewGuid().ToString(), 10, false), + new(Guid.NewGuid().ToString(), "name 2", + ProductType.Pizza, Guid.NewGuid().ToString(), 10, true), + new(Guid.NewGuid().ToString(), "name 3", + ProductType.Pizza, Guid.NewGuid().ToString(), 10, false), + }; + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, true); + var list = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, 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, restaurantId), Times.Once); + _productStorageContract.Verify(x => x.GetList(false, restaurantId), Times.Once); + } + + [Test] + public void GetAllProductsByRestaurant_ReturnEmptyList_Test() + { + //Arrange + var restaurantId = Guid.NewGuid().ToString(); + _productStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny())).Returns([]); + //Act + + var listOnlyActive = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, true); + var list = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, 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(),restaurantId), Times.Exactly(2)); + } + + [Test] + public void GetAllProductsByRestaurant_RestaurantIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByRestaurant(null, It.IsAny()), Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByRestaurant(string.Empty,It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Never); + } + [Test] + public void GetAllProductsByRestaurant_RestaurantIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByRestaurant("manufacturerId",It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Never); + } + [Test] + public void GetAllProductsByRestaurant_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByRestaurant(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny()), Times.Once); + } + [Test] + public void GetAllProductsByRestaurant_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByRestaurant(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.Pizza, Guid.NewGuid().ToString(), 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.Pizza, Guid.NewGuid().ToString(), 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.Pizza, Guid.NewGuid().ToString(), 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.RestaurantId == record.RestaurantId && 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.Pizza, Guid.NewGuid().ToString(), 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.Pizza, Guid.NewGuid().ToString(),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.Pizza, Guid.NewGuid().ToString(), 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.Pizza, Guid.NewGuid().ToString(), 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.RestaurantId == record.RestaurantId && 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.Pizza, Guid.NewGuid().ToString(), 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(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 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.Pizza, Guid.NewGuid().ToString(), 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.Pizza, Guid.NewGuid().ToString(), 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/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs index be450c8..7c9938d 100644 --- a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/RestaurantBusinessLogicsContractsTests.cs @@ -14,6 +14,7 @@ using System.Threading.Tasks; namespace PipingHotTests.BusinessLogicsContractsTests; +[TestFixture] internal class RestaurantBusinessLogicsContractsTests { private RestaurantBusinessLogicContract _restaurantBusinessLogicContract; diff --git a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTests.cs new file mode 100644 index 0000000..be913a1 --- /dev/null +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTests.cs @@ -0,0 +1,423 @@ +using Microsoft.Extensions.Logging; +using Moq; +using PipingHotBusinessLogic.Implementations; +using PipingHotContrast.DataModels; +using PipingHotContrast.Enums; +using PipingHotContrast.Exceptions; +using PipingHotContrast.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotTests.BusinessLogicsContractsTests; + +[TestFixture] +internal class SalaryBusinessLogicContractTests +{ + private SalaryBusinessLogicContract _salaryBusinessLogicContract; + private Mock _salaryStorageContract; + private Mock _orderStorageContract; + private Mock _postStorageContract; + private Mock _workerStorageContract; + [OneTimeSetUp] + public void OneTimeSetUp() + { + _salaryStorageContract = new Mock(); + _orderStorageContract = new Mock(); + _postStorageContract = new Mock(); + _workerStorageContract = new Mock(); + _salaryBusinessLogicContract = new + SalaryBusinessLogicContract(_salaryStorageContract.Object, + _orderStorageContract.Object, _postStorageContract.Object, + _workerStorageContract.Object, new Mock().Object); + } + [TearDown] + public void TearDown() + { + _salaryStorageContract.Reset(); + _orderStorageContract.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); + } + [Test] + public void CalculateSalaryByMounth_CalculateSalary_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + var orderSum = 200.0; + var postSalary = 2000.0; + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, orderSum, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Operator, postSalary, true, DateTime.UtcNow)); + + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + var sum = 0.0; + var expectedSum = postSalary + orderSum * 0.1; + _salaryStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((SalaryDataModel x) => + { + sum = x.Salary; + }); + //Act + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow); + //Assert + Assert.That(sum, Is.EqualTo(expectedSum)); + } + [Test] + public void CalculateSalaryByMounth_WithSeveralWorkers_Test() + { + //Arrange + var worker1Id = Guid.NewGuid().ToString(); + var worker2Id = Guid.NewGuid().ToString(); + var worker3Id = Guid.NewGuid().ToString(); + var list = new List() { + new(worker1Id, "Test","Test", Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow, false), + new(worker2Id, "Test","Test", Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow, false), + new(worker3Id, "Test","Test", Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow, false) + }; + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Returns([new OrderDataModel(Guid.NewGuid().ToString(), + worker1Id, null, 1, false, []), + new OrderDataModel(Guid.NewGuid().ToString(), worker1Id, null, + 1, false, []), + new OrderDataModel(Guid.NewGuid().ToString(), worker2Id, null, + 1, false, []), + new OrderDataModel(Guid.NewGuid().ToString(), worker3Id, null, + 1, false, []), + new OrderDataModel(Guid.NewGuid().ToString(), worker3Id, null, + 1, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",PostType.Operator, 2000, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns(list); + //Act + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow); + //Assert + _salaryStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Exactly(list.Count)); + } + [Test] + public void CalculateSalaryByMounth_WithoitOrdersByWorker_Test() + { + //Arrange + var postSalary = 2000.0; + var workerId = Guid.NewGuid().ToString(); + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Returns([]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Operator, postSalary, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test","Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + var sum = 0.0; + var expectedSum = postSalary; + _salaryStorageContract.Setup(x =>x.AddElement(It.IsAny())) + .Callback((SalaryDataModel x) => + { + sum = x.Salary; + }); + //Act + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow); + //Assert + Assert.That(sum, Is.EqualTo(expectedSum)); + } + [Test] + public void CalculateSalaryByMounth_OrderStorageReturnNull_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Operator, 2000, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf()); + } + [Test] + public void CalculateSalaryByMounth_PostStorageReturnNull_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, 200, false, [])]); + + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test","Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf()); + } + [Test] + public void CalculateSalaryByMounth_WorkerStorageReturnNull_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, 200, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Operator, 2000, true, DateTime.UtcNow)); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf()); + } + [Test] + public void CalculateSalaryByMounth_OrderStorageThrowException_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Throws(new StorageException(new InvalidOperationException())); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Operator, 2000, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf()); + } + [Test] + public void CalculateSalaryByMounth_PostStorageThrowException_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Returns([new OrderDataModel(Guid.NewGuid().ToString(), workerId, null, 200,false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Throws(new StorageException(new InvalidOperationException())); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf()); + } + [Test] + public void CalculateSalaryByMounth_WorkerStorageThrowException_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _orderStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny())) + .Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, 200, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Operator, 2000, true, DateTime.UtcNow)); + _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(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf()); + } + + + + + + + + + + +} diff --git a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs new file mode 100644 index 0000000..cbbc0e9 --- /dev/null +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs @@ -0,0 +1,746 @@ + +using Microsoft.Extensions.Logging; +using Moq; +using PipingHotBusinessLogic.Implementations; +using PipingHotContrast.DataModels; +using PipingHotContrast.Exceptions; +using PipingHotContrast.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PipingHotTests.BusinessLogicsContractsTests; + +[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); + } + [TearDown] + public void TearDown() + { + _workerStorageContract.Reset(); + } + [Test] + public void GetAllWorkers_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + + { + new(Guid.NewGuid().ToString(), "fio 1","email1", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2","email2", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3","email3", + 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","email 1", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2","email 2", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3","email 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","email 1", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2","email 2", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3","email 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","email 1", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2","email 2", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3","email 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","email",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,"email", + 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_GetByEmail_ReturnRecord_Test() + { + //Arrange + var email = "user@example.com"; + var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", + email, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false); + _workerStorageContract.Setup(x => x.GetElementByEmail(email)).Returns(record); + + //Act + + var element = _workerBusinessLogicContract.GetWorkerByData(email); + + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Email, Is.EqualTo(email)); + _workerStorageContract.Verify(x =>x.GetElementByEmail(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); + _workerStorageContract.Verify(x => x.GetElementByEmail(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); + _workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny()), Times.Never); + } + 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); + _workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny()), Times.Never); + } + public void GetWorkerByData_GetByEmail_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetWorkerByData("email"), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + _workerStorageContract.Verify(x => x.GetElementByEmail(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())); + _workerStorageContract.Setup(x => x.GetElementByEmail(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()); + Assert.That(() => + _workerBusinessLogicContract.GetWorkerByData("email"), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _workerStorageContract.Verify(x =>x.GetElementByFIO(It.IsAny()), Times.Once); + _workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny()), Times.Once); + } + [Test] + public void InsertWorker_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio","email", 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.Email == record.Email && + 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","email", + 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","email", 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","email", + 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","email", + 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.Email == record.Email && + 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","email", + 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","email", 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","email", + 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); + } + + + + + + + + + + +} -- 2.25.1 From 940765f80a9e628c594f1549da6ca162ed791247 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Sun, 23 Feb 2025 16:26:07 +0400 Subject: [PATCH 7/9] =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D0=B5=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DateTimeExtensions.cs | 2 +- .../IWorkerStorageContract.cs | 2 +- .../OrderBusinessLogicContract.cs | 87 +++++++++++++++++-- .../PostBusinessLogicContract.cs | 4 +- .../ProductBusinessLogicContract.cs | 64 ++++++++++++-- .../SalaryBusinessLogicContract.cs | 45 +++++++++- .../WorkerBusinessLogicContract.cs | 76 ++++++++++++++-- .../WorkerBusinessLogicContractTests.cs | 48 +++++----- 8 files changed, 275 insertions(+), 53 deletions(-) rename PipingHot/PipingHot/{Exceptions => Extensions}/DateTimeExtensions.cs (87%) diff --git a/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs b/PipingHot/PipingHot/Extensions/DateTimeExtensions.cs similarity index 87% rename from PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs rename to PipingHot/PipingHot/Extensions/DateTimeExtensions.cs index 4067cfb..3fbb4bd 100644 --- a/PipingHot/PipingHot/Exceptions/DateTimeExtensions.cs +++ b/PipingHot/PipingHot/Extensions/DateTimeExtensions.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PipingHotContrast.Exceptions; +namespace PipingHotContrast.Extensions; public static class DateTimeExtensions { diff --git a/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs b/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs index 7a942e1..44b15ca 100644 --- a/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs +++ b/PipingHot/PipingHot/StoragesContracts/IWorkerStorageContract.cs @@ -9,7 +9,7 @@ namespace PipingHotContrast.StoragesContracts; public interface IWorkerStorageContract { - List GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirtDate = null, DateTime? toBirtDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null); + List GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null); WorkerDataModel? GetElementById(string id); WorkerDataModel? GetElementByFIO(string fio); WorkerDataModel? GetElementByEmail(string email); diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs index 375e30b..06f9035 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs @@ -1,44 +1,117 @@ using Microsoft.Extensions.Logging; using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.Exceptions; +using PipingHotContrast.Extensions; using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; internal class OrderBusinessLogicContract(IOrderStorageContract orderStorageContract, ILogger logger) : IOrderBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IOrderStorageContract _orderStorageContract = orderStorageContract; public List GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllSales params: {fromDate}, {toDate}",fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _orderStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); } public List GetAllOrdersByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllSales params: {workerId}, {fromDate},{ toDate} ", workerId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (workerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(workerId)); + } + if (!workerId.IsGuid()) + { + throw new ValidationException("The value in the field workerId is not a unique identifier."); + } + return _orderStorageContract.GetList(fromDate, toDate, workerId:workerId) ?? throw new NullListException(); } public List GetAllOrdersByRestaurantByPeriod(string restaurantId, DateTime fromDate, DateTime toDate) { - return []; + + _logger.LogInformation("GetAllSales params: {restaurantId}, {fromDate},{ toDate} ", restaurantId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (restaurantId.IsEmpty()) + { + throw new ArgumentNullException(nameof(restaurantId)); + } + if (!restaurantId.IsGuid()) + { + throw new ValidationException("The value in the field restaurantId is not a unique identifier."); + } + return _orderStorageContract.GetList(fromDate, toDate, restaurantId:restaurantId) ?? throw new NullListException(); } public List GetAllOrdersByProductByPeriod(string productId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllSales params: {productId}, {fromDate},{ toDate}", productId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (productId.IsEmpty()) + { + throw new ArgumentNullException(nameof(productId)); + } + if (!productId.IsGuid()) + { + throw new ValidationException("The value in the field productId is not a unique identifier."); + } + return _orderStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException(); + } public OrderDataModel GetOrderByData(string data) { - return new("", "", "", 0, true, []); + _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 _orderStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); } public void InsertOrder(OrderDataModel orderDataModel) { - + _logger.LogInformation("New data: {json}",JsonSerializer.Serialize(orderDataModel)); + ArgumentNullException.ThrowIfNull(orderDataModel); + orderDataModel.Validate(); + _orderStorageContract.AddElement(orderDataModel); + } public void CancelOrder(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"); + } + _orderStorageContract.DelElement(id); } } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs index 6b126a8..3e25401 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -64,10 +64,10 @@ internal class PostBusinessLogicContract(IPostStorageContract postStorageContrac } public void UpdatePost(PostDataModel postDataModel) { - _logger.LogInformation("New data: {json}",JsonSerializer.Serialize(postDataModel)); + _logger.LogInformation("Update data: {json}",JsonSerializer.Serialize(postDataModel)); ArgumentNullException.ThrowIfNull(postDataModel); postDataModel.Validate(); - _postStorageContract.AddElement(postDataModel); + _postStorageContract.UpdElement(postDataModel); } public void DeletePost(string id) { diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 5e52713..07022bf 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -2,11 +2,14 @@ using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; using PipingHotContrast.Enums; +using PipingHotContrast.Exceptions; +using PipingHotContrast.Extensions; using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; @@ -17,32 +20,79 @@ internal class ProductBusinessLogicContract(IProductStorageContract productStora private readonly IProductStorageContract _productStorageContract = productStorageContract; public List GetAllProducts(bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllProducts params: {onlyActive}",onlyActive); + return _productStorageContract.GetList(onlyActive) ?? throw new NullListException(); + } public List GetAllProductsByRestaurant(string restaurantId, bool onlyActive = true) { - return []; + if (restaurantId.IsEmpty()) + { + throw new ArgumentNullException(nameof(restaurantId)); + } + if (!restaurantId.IsGuid()) + { + throw new ValidationException("The value in the field restaurantId is not a unique identifier."); + } + _logger.LogInformation("GetAllProducts params: {restaurantId},{ onlyActive}", restaurantId, onlyActive); + return _productStorageContract.GetList(onlyActive, restaurantId) ?? 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) { - return new("", "", ProductType.None, "", 0, true); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _productStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _productStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); } public void InsertProduct(ProductDataModel productDataModel) { - + _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/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index 3c85957..dfc6a5c 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.Logging; using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.Exceptions; +using PipingHotContrast.Extensions; using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; @@ -13,17 +15,54 @@ namespace PipingHotBusinessLogic.Implementations; internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, IOrderStorageContract orderStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; + private readonly IOrderStorageContract _orderStorageContract = orderStorageContract; + 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 orders = _orderStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ?? throw new NullListException(); + var post = _postStorageContract.GetElementById(worker.PostId) ?? throw new NullListException(); + var salary = post.Salary + orders * 0.1; + _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/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index f1632ef..bdc86fb 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -1,50 +1,110 @@ using Microsoft.Extensions.Logging; using PipingHotContrast.BusinessLogicsContracts; using PipingHotContrast.DataModels; +using PipingHotContrast.Exceptions; +using PipingHotContrast.Extensions; using PipingHotContrast.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace PipingHotBusinessLogic.Implementations; internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; + public List GetAllWorkers(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) { - return []; + _logger.LogInformation("GetAllWorkers params: {onlyActive},{ fromDate}, { toDate} ", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException(); } public List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllWorkers params: {onlyActive},{ fromDate}, { toDate}", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException(); } public WorkerDataModel GetWorkerByData(string data) { - 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); + } + if (Regex.IsMatch(data, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")) + { + return _workerStorageContract.GetElementByEmail(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/PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs index cbbc0e9..7b22865 100644 --- a/PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs +++ b/PipingHot/PipingHotTests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs @@ -38,13 +38,13 @@ internal class WorkerBusinessLogicContractTests var listOriginal = new List() { - new(Guid.NewGuid().ToString(), "fio 1","email1", + new(Guid.NewGuid().ToString(), "fio 1","user1@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), - new(Guid.NewGuid().ToString(), "fio 2","email2", + new(Guid.NewGuid().ToString(), "fio 2","user2@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), - new(Guid.NewGuid().ToString(), "fio 3","email3", + new(Guid.NewGuid().ToString(), "fio 3","user3@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; @@ -123,13 +123,13 @@ internal class WorkerBusinessLogicContractTests var postId = Guid.NewGuid().ToString(); var listOriginal = new List() { - new(Guid.NewGuid().ToString(), "fio 1","email 1", + new(Guid.NewGuid().ToString(), "fio 1","user1@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), - new(Guid.NewGuid().ToString(), "fio 2","email 2", + new(Guid.NewGuid().ToString(), "fio 2","user2@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), - new(Guid.NewGuid().ToString(), "fio 3","email 3", + new(Guid.NewGuid().ToString(), "fio 3","user3@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; @@ -232,13 +232,13 @@ internal class WorkerBusinessLogicContractTests var date = DateTime.UtcNow; var listOriginal = new List() { - new(Guid.NewGuid().ToString(), "fio 1","email 1", + new(Guid.NewGuid().ToString(), "fio 1","user1@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), - new(Guid.NewGuid().ToString(), "fio 2","email 2", + new(Guid.NewGuid().ToString(), "fio 2","user2@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), - new(Guid.NewGuid().ToString(), "fio 3","email 3", + new(Guid.NewGuid().ToString(), "fio 3","user3@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; @@ -333,13 +333,13 @@ internal class WorkerBusinessLogicContractTests var date = DateTime.UtcNow; var listOriginal = new List() { - new(Guid.NewGuid().ToString(), "fio 1","email 1", + new(Guid.NewGuid().ToString(), "fio 1","user1@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), - new(Guid.NewGuid().ToString(), "fio 2","email 2", + new(Guid.NewGuid().ToString(), "fio 2","user2@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), - new(Guid.NewGuid().ToString(), "fio 3","email 3", + new(Guid.NewGuid().ToString(), "fio 3","user3@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; @@ -432,7 +432,7 @@ internal class WorkerBusinessLogicContractTests //Arrange var id = Guid.NewGuid().ToString(); - var record = new WorkerDataModel(id, "fio","email",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); + var record = new WorkerDataModel(id, "fio", "user@example.com", 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); @@ -447,7 +447,7 @@ internal class WorkerBusinessLogicContractTests { //Arrange var fio = "fio"; - var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio,"email", + var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio, "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); _workerStorageContract.Setup(x => @@ -514,7 +514,7 @@ internal class WorkerBusinessLogicContractTests { //Act&Assert Assert.That(() => - _workerBusinessLogicContract.GetWorkerByData("email"), Throws.TypeOf()); + _workerBusinessLogicContract.GetWorkerByData("user@example.com"), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny()), Times.Once); @@ -532,7 +532,7 @@ internal class WorkerBusinessLogicContractTests Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf()); Assert.That(() => - _workerBusinessLogicContract.GetWorkerByData("email"), Throws.TypeOf()); + _workerBusinessLogicContract.GetWorkerByData("user@example.com"), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _workerStorageContract.Verify(x =>x.GetElementByFIO(It.IsAny()), Times.Once); _workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny()), Times.Once); @@ -542,7 +542,7 @@ internal class WorkerBusinessLogicContractTests { //Arrange var flag = false; - var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio","email", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); + var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); _workerStorageContract.Setup(x => x.AddElement(It.IsAny())) .Callback((WorkerDataModel x) => { @@ -567,7 +567,7 @@ internal class WorkerBusinessLogicContractTests _workerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => - _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio","email", + _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x =>x.AddElement(It.IsAny()), Times.Once); @@ -585,7 +585,7 @@ internal class WorkerBusinessLogicContractTests public void InsertWorker_InvalidRecord_ThrowException_Test() { //Act&Assert - Assert.That(() => _workerBusinessLogicContract.InsertWorker(new WorkerDataModel("id", "fio","email", Guid.NewGuid().ToString(), DateTime.Now.AddYears(- + Assert.That(() => _workerBusinessLogicContract.InsertWorker(new WorkerDataModel("id", "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(- 16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } @@ -598,7 +598,7 @@ internal class WorkerBusinessLogicContractTests InvalidOperationException())); //Act&Assert Assert.That(() => - _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio","email", + _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); @@ -608,7 +608,7 @@ internal class WorkerBusinessLogicContractTests { //Arrange var flag = false; - var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio","email", + var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); _workerStorageContract.Setup(x => @@ -635,7 +635,7 @@ internal class WorkerBusinessLogicContractTests _workerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); //Act&Assert Assert.That(() => - _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio","email", + _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); @@ -653,7 +653,7 @@ internal class WorkerBusinessLogicContractTests { //Act&Assert Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new - WorkerDataModel("id", "fio","email", Guid.NewGuid().ToString(), DateTime.Now.AddYears(- + WorkerDataModel("id", "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(- 16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); @@ -667,7 +667,7 @@ internal class WorkerBusinessLogicContractTests InvalidOperationException())); //Act&Assert Assert.That(() => - _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio","email", + _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "user@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); -- 2.25.1 From 2ad081dfb0fc53a77faadbd5b124bef87ee4c40c Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 27 Feb 2025 15:18:20 +0400 Subject: [PATCH 8/9] =?UTF-8?q?=D0=9A=D1=80=D0=B0=D1=81=D0=BD=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=B7=D0=BE=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrderBusinessLogicContract.cs | 80 ++----------------- .../PostBusinessLogicContract.cs | 67 +++------------- .../ProductBusinessLogicContract.cs | 61 ++------------ .../RestaurantBusinessLogicContract.cs | 39 ++------- .../SalaryBusinessLogicContract.cs | 35 +------- .../WorkerBusinessLogicContract.cs | 69 ++-------------- 6 files changed, 38 insertions(+), 313 deletions(-) diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs index 06f9035..3ff3b09 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs @@ -19,99 +19,31 @@ internal class OrderBusinessLogicContract(IOrderStorageContract orderStorageCont private readonly IOrderStorageContract _orderStorageContract = orderStorageContract; public List GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate) { - _logger.LogInformation("GetAllSales params: {fromDate}, {toDate}",fromDate, toDate); - if (fromDate.IsDateNotOlder(toDate)) - { - throw new IncorrectDatesException(fromDate, toDate); - } - return _orderStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); + return []; } public List GetAllOrdersByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) { - _logger.LogInformation("GetAllSales params: {workerId}, {fromDate},{ toDate} ", workerId, fromDate, toDate); - if (fromDate.IsDateNotOlder(toDate)) - { - throw new IncorrectDatesException(fromDate, toDate); - } - if (workerId.IsEmpty()) - { - throw new ArgumentNullException(nameof(workerId)); - } - if (!workerId.IsGuid()) - { - throw new ValidationException("The value in the field workerId is not a unique identifier."); - } - return _orderStorageContract.GetList(fromDate, toDate, workerId:workerId) ?? throw new NullListException(); + return []; } public List GetAllOrdersByRestaurantByPeriod(string restaurantId, DateTime fromDate, DateTime toDate) { - - _logger.LogInformation("GetAllSales params: {restaurantId}, {fromDate},{ toDate} ", restaurantId, fromDate, toDate); - if (fromDate.IsDateNotOlder(toDate)) - { - throw new IncorrectDatesException(fromDate, toDate); - } - if (restaurantId.IsEmpty()) - { - throw new ArgumentNullException(nameof(restaurantId)); - } - if (!restaurantId.IsGuid()) - { - throw new ValidationException("The value in the field restaurantId is not a unique identifier."); - } - return _orderStorageContract.GetList(fromDate, toDate, restaurantId:restaurantId) ?? throw new NullListException(); + return []; } public List GetAllOrdersByProductByPeriod(string productId, DateTime fromDate, DateTime toDate) { - _logger.LogInformation("GetAllSales params: {productId}, {fromDate},{ toDate}", productId, fromDate, toDate); - if (fromDate.IsDateNotOlder(toDate)) - { - throw new IncorrectDatesException(fromDate, toDate); - } - if (productId.IsEmpty()) - { - throw new ArgumentNullException(nameof(productId)); - } - if (!productId.IsGuid()) - { - throw new ValidationException("The value in the field productId is not a unique identifier."); - } - return _orderStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException(); - + return []; } public OrderDataModel GetOrderByData(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 _orderStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + return new("", "", "", 0, true, []); } public void InsertOrder(OrderDataModel orderDataModel) { - _logger.LogInformation("New data: {json}",JsonSerializer.Serialize(orderDataModel)); - ArgumentNullException.ThrowIfNull(orderDataModel); - orderDataModel.Validate(); - _orderStorageContract.AddElement(orderDataModel); } public void CancelOrder(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"); - } - _orderStorageContract.DelElement(id); + } } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs index 3e25401..c10d736 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -16,86 +16,39 @@ namespace PipingHotBusinessLogic.Implementations; internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly IPostStorageContract _postStorageContract = postStorageContract; public List GetAllPosts(bool onlyActive) { - logger.LogInformation("GetAllPosts params: {onlyActive}",onlyActive); - return _postStorageContract.GetList(onlyActive) ?? throw new NullListException(); + return []; } public List GetAllDataOfPost(string postId) { - _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(); - + return []; } public PostDataModel GetPostByData(string data) { - _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); + + return new("", "", PostType.None, 0, true, DateTime.UtcNow); } public void InsertPost(PostDataModel postDataModel) { - _logger.LogInformation("New data: {json}",JsonSerializer.Serialize(postDataModel)); - ArgumentNullException.ThrowIfNull(postDataModel); - postDataModel.Validate(); - _postStorageContract.AddElement(postDataModel); + } public void UpdatePost(PostDataModel postDataModel) { - _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); + } - + + } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 07022bf..4352567 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -20,79 +20,32 @@ internal class ProductBusinessLogicContract(IProductStorageContract productStora private readonly IProductStorageContract _productStorageContract = productStorageContract; public List GetAllProducts(bool onlyActive = true) { - _logger.LogInformation("GetAllProducts params: {onlyActive}",onlyActive); - return _productStorageContract.GetList(onlyActive) ?? throw new NullListException(); - + return []; } public List GetAllProductsByRestaurant(string restaurantId, bool onlyActive = true) { - if (restaurantId.IsEmpty()) - { - throw new ArgumentNullException(nameof(restaurantId)); - } - if (!restaurantId.IsGuid()) - { - throw new ValidationException("The value in the field restaurantId is not a unique identifier."); - } - _logger.LogInformation("GetAllProducts params: {restaurantId},{ onlyActive}", restaurantId, onlyActive); - return _productStorageContract.GetList(onlyActive, restaurantId) ?? throw new NullListException(); - + return []; } public List GetProductHistoryByProduct(string productId) { - 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(); - + return []; } 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); + return new("", "", ProductType.None, "", 0, true); } 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/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs index 4f5c7f2..d1518aa 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs @@ -21,54 +21,25 @@ internal class RestaurantBusinessLogicContract(IRestaurantStorageContract resta public List GetAllRestaurants() { - _logger.LogInformation("GetAllRestaurants"); - return _restaurantStorageContract.GetList() ?? throw new NullListException(); - + return []; } public RestaurantDataModel GetRestaurantByData(string data) { - _logger.LogInformation("Get element by data: {data}", data); - if (data.IsEmpty()) - { - throw new ArgumentNullException(nameof(data)); - } - if (data.IsGuid()) - { - return _restaurantStorageContract.GetElementById(data) ?? - throw new ElementNotFoundException(data); - } - return _restaurantStorageContract.GetElementByName(data) ?? - _restaurantStorageContract.GetElementByOldName(data) ?? - throw new ElementNotFoundException(data); + return new("", "", null, null); } public void InsertRestaurant(RestaurantDataModel restaurantDataModel) { - logger.LogInformation("New data: {json}", JsonSerializer.Serialize(restaurantDataModel)); - ArgumentNullException.ThrowIfNull(restaurantDataModel); - restaurantDataModel.Validate(); - _restaurantStorageContract.AddElement(restaurantDataModel); + } public void UpdateRestaurant(RestaurantDataModel restaurantDataModel) { - _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(restaurantDataModel)); - ArgumentNullException.ThrowIfNull(restaurantDataModel); - restaurantDataModel.Validate(); - _restaurantStorageContract.UpdElement(restaurantDataModel); + } public void DeleteRestaurant(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"); - } - _restaurantStorageContract.DelElement(id); + } } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index dfc6a5c..2619fec 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -23,46 +23,15 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) { - _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(); + return []; } public List GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId) { - 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(); - + return []; } 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 orders = _orderStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ?? throw new NullListException(); - var post = _postStorageContract.GetElementById(worker.PostId) ?? throw new NullListException(); - var salary = post.Salary + orders * 0.1; - _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/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index bdc86fb..7bbe30f 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -21,90 +21,37 @@ internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageC public List GetAllWorkers(bool onlyActive = true) { - _logger.LogInformation("GetAllWorkers params: {onlyActive}",onlyActive); - return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException(); + return []; } public List GetAllWorkersByPost(string postId, bool onlyActive = true) { - _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(); - + return []; } 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(); + return []; } 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(); + return []; } public WorkerDataModel GetWorkerByData(string data) { - _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); - } - if (Regex.IsMatch(data, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")) - { - return _workerStorageContract.GetElementByEmail(data) ?? - throw new ElementNotFoundException(data); - } - - return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data); - + return new("", "", "", "", DateTime.Now, DateTime.Now, true); } 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); + } } -- 2.25.1 From 482b1ddb8dd164a2e4eb3d7b685b0cd9caa6d003 Mon Sep 17 00:00:00 2001 From: Adelina888 Date: Thu, 27 Feb 2025 15:31:28 +0400 Subject: [PATCH 9/9] =?UTF-8?q?=D0=97=D0=B5=D0=BB=D0=B5=D0=BD=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=B7=D0=BE=D0=BD=D0=B0,=20=D1=82=D0=BE=D1=87=D0=BD=D0=BE?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BD=D0=B5=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrderBusinessLogicContract.cs | 80 +++++++++++++++++-- .../PostBusinessLogicContract.cs | 67 +++++++++++++--- .../ProductBusinessLogicContract.cs | 61 ++++++++++++-- .../RestaurantBusinessLogicContract.cs | 40 ++++++++-- .../SalaryBusinessLogicContract.cs | 36 ++++++++- .../WorkerBusinessLogicContract.cs | 69 ++++++++++++++-- 6 files changed, 312 insertions(+), 41 deletions(-) diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs index 3ff3b09..1376640 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/OrderBusinessLogicContract.cs @@ -19,31 +19,99 @@ internal class OrderBusinessLogicContract(IOrderStorageContract orderStorageCont private readonly IOrderStorageContract _orderStorageContract = orderStorageContract; public List GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _orderStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); } public List GetAllOrdersByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllSales params: {workerId}, {fromDate},{ toDate} ", workerId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (workerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(workerId)); + } + if (!workerId.IsGuid()) + { + throw new ValidationException("The value in the field workerId is not a unique identifier."); + } + return _orderStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException(); } public List GetAllOrdersByRestaurantByPeriod(string restaurantId, DateTime fromDate, DateTime toDate) { - return []; + + _logger.LogInformation("GetAllSales params: {restaurantId}, {fromDate},{ toDate} ", restaurantId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (restaurantId.IsEmpty()) + { + throw new ArgumentNullException(nameof(restaurantId)); + } + if (!restaurantId.IsGuid()) + { + throw new ValidationException("The value in the field restaurantId is not a unique identifier."); + } + return _orderStorageContract.GetList(fromDate, toDate, restaurantId: restaurantId) ?? throw new NullListException(); } public List GetAllOrdersByProductByPeriod(string productId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllSales params: {productId}, {fromDate},{ toDate}", productId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (productId.IsEmpty()) + { + throw new ArgumentNullException(nameof(productId)); + } + if (!productId.IsGuid()) + { + throw new ValidationException("The value in the field productId is not a unique identifier."); + } + return _orderStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException(); + } public OrderDataModel GetOrderByData(string data) { - return new("", "", "", 0, true, []); + _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 _orderStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); } public void InsertOrder(OrderDataModel orderDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(orderDataModel)); + ArgumentNullException.ThrowIfNull(orderDataModel); + orderDataModel.Validate(); + _orderStorageContract.AddElement(orderDataModel); } public void CancelOrder(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"); + } + _orderStorageContract.DelElement(id); } } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs index c10d736..978e610 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -16,39 +16,84 @@ namespace PipingHotBusinessLogic.Implementations; internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IPostStorageContract _postStorageContract = postStorageContract; public List GetAllPosts(bool onlyActive) { - return []; + logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive); + return _postStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetAllDataOfPost(string postId) { - return []; + _logger.LogInformation("GetAllDataOfPost for {postId}", postId); + if (postId.IsEmpty()) + { + throw new ArgumentNullException(nameof(postId)); + } + if (!postId.IsGuid()) + { + throw new ValidationException("The value in the field postId is not a unique identifier."); + + } + return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException(); + } public PostDataModel GetPostByData(string data) { - - return new("", "", PostType.None, 0, true, DateTime.UtcNow); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _postStorageContract.GetElementById(data) ?? throw new + ElementNotFoundException(data); + } + return _postStorageContract.GetElementByName(data) ?? throw new + ElementNotFoundException(data); } public void InsertPost(PostDataModel postDataModel) { - + _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); } - - - } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 4352567..07022bf 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -20,32 +20,79 @@ internal class ProductBusinessLogicContract(IProductStorageContract productStora private readonly IProductStorageContract _productStorageContract = productStorageContract; public List GetAllProducts(bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllProducts params: {onlyActive}",onlyActive); + return _productStorageContract.GetList(onlyActive) ?? throw new NullListException(); + } public List GetAllProductsByRestaurant(string restaurantId, bool onlyActive = true) { - return []; + if (restaurantId.IsEmpty()) + { + throw new ArgumentNullException(nameof(restaurantId)); + } + if (!restaurantId.IsGuid()) + { + throw new ValidationException("The value in the field restaurantId is not a unique identifier."); + } + _logger.LogInformation("GetAllProducts params: {restaurantId},{ onlyActive}", restaurantId, onlyActive); + return _productStorageContract.GetList(onlyActive, restaurantId) ?? 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) { - return new("", "", ProductType.None, "", 0, true); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _productStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _productStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); } public void InsertProduct(ProductDataModel productDataModel) { - + _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/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs index d1518aa..4faaa33 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/RestaurantBusinessLogicContract.cs @@ -18,28 +18,56 @@ internal class RestaurantBusinessLogicContract(IRestaurantStorageContract resta { private readonly ILogger _logger = logger; private readonly IRestaurantStorageContract _restaurantStorageContract = restaurantStorageContract; - public List GetAllRestaurants() { - return []; + _logger.LogInformation("GetAllRestaurants"); + return _restaurantStorageContract.GetList() ?? throw new NullListException(); + } public RestaurantDataModel GetRestaurantByData(string data) { - return new("", "", null, null); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _restaurantStorageContract.GetElementById(data) ?? + throw new ElementNotFoundException(data); + } + return _restaurantStorageContract.GetElementByName(data) ?? + _restaurantStorageContract.GetElementByOldName(data) ?? + throw new ElementNotFoundException(data); } public void InsertRestaurant(RestaurantDataModel restaurantDataModel) { - + logger.LogInformation("New data: {json}", JsonSerializer.Serialize(restaurantDataModel)); + ArgumentNullException.ThrowIfNull(restaurantDataModel); + restaurantDataModel.Validate(); + _restaurantStorageContract.AddElement(restaurantDataModel); } public void UpdateRestaurant(RestaurantDataModel restaurantDataModel) { - + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(restaurantDataModel)); + ArgumentNullException.ThrowIfNull(restaurantDataModel); + restaurantDataModel.Validate(); + _restaurantStorageContract.UpdElement(restaurantDataModel); } public void DeleteRestaurant(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"); + } + _restaurantStorageContract.DelElement(id); } } diff --git a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index 2619fec..d1a0c9c 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -20,18 +20,48 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC private readonly IOrderStorageContract _orderStorageContract = orderStorageContract; 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 orders = _orderStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ?? throw new NullListException(); + var post = _postStorageContract.GetElementById(worker.PostId) ?? throw new NullListException(); + var salary = post.Salary + orders * 0.1; + _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/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index 7bbe30f..bdc86fb 100644 --- a/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/PipingHot/PipingHotBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -21,37 +21,90 @@ internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageC public List GetAllWorkers(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) { - return []; + _logger.LogInformation("GetAllWorkers params: {onlyActive},{ fromDate}, { toDate} ", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException(); } public List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllWorkers params: {onlyActive},{ fromDate}, { toDate}", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException(); } public WorkerDataModel GetWorkerByData(string data) { - 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); + } + if (Regex.IsMatch(data, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")) + { + return _workerStorageContract.GetElementByEmail(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); } } -- 2.25.1