From c3b6b9602b9b824bae12d81991ac547cc67eee09 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 05:17:05 +0400 Subject: [PATCH 01/14] fix post model and post tests --- .../DataModels/PostDataModel.cs | 10 +---- .../DataModelsTests/PostDataModelTests.cs | 44 +++++-------------- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs index 6025af1..e49c318 100644 --- a/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs @@ -4,12 +4,10 @@ using TheCyclopsContracts.Extensions; namespace TheCyclopsContracts.DataModels; -public class PostDataModel(string id, string PostId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) +public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) { public string Id { get; private set; } = id; - public string PostId { get; private set; } = PostId; - public string PostName { get; private set; } = postName; public PostType PostType { get; private set; } = postType; @@ -28,12 +26,6 @@ public class PostDataModel(string id, string PostId, string postName, PostType p 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"); - if (PostName.IsEmpty()) throw new ValidationException("Field PostName is empty"); diff --git a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/PostDataModelTests.cs b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/PostDataModelTests.cs index d942348..66d9ee5 100644 --- a/TheCyclopsProject/TheCyclopsTests/DataModelsTests/PostDataModelTests.cs +++ b/TheCyclopsProject/TheCyclopsTests/DataModelsTests/PostDataModelTests.cs @@ -10,11 +10,11 @@ internal class PostDataModelTests [Test] public void IdIsNullOrEmptyTest() { - var post = CreateDataModel(null, Guid.NewGuid().ToString(), "post", + var post = CreateDataModel(null, "post", PostType.Designer, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "post", + post = CreateDataModel(string.Empty, "post", PostType.Designer, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } @@ -22,27 +22,7 @@ internal class PostDataModelTests [Test] public void IdIsNotGuidTest() { - var post = CreateDataModel("id", Guid.NewGuid().ToString(), "post", - PostType.Designer, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - } - - [Test] - public void PostIdIsNullEmptyTest() - { - var post = CreateDataModel(Guid.NewGuid().ToString(), null, "post", - PostType.Designer, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - - post = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "post", - PostType.Designer, 10, true, DateTime.UtcNow); - Assert.That(() => post.Validate(), Throws.TypeOf()); - } - - [Test] - public void PostIdIsNotGuidTest() - { - var post = CreateDataModel(Guid.NewGuid().ToString(), "postId", "post", + var post = CreateDataModel("id", "post", PostType.Designer, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } @@ -50,11 +30,11 @@ internal class PostDataModelTests [Test] public void PostNameIsEmptyTest() { - var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, + var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Designer, 10, true, DateTime.UtcNow); Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); - manufacturer = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, + manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Designer, 10, true, DateTime.UtcNow); Assert.That(() => manufacturer.Validate(), Throws.TypeOf()); } @@ -62,7 +42,7 @@ internal class PostDataModelTests [Test] public void PostTypeIsNoneTest() { - var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post", + var post = CreateDataModel(Guid.NewGuid().ToString(), "post", PostType.None, 10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } @@ -70,11 +50,11 @@ internal class PostDataModelTests [Test] public void SalaryIsLessOrZeroTest() { - var post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post", + var post = CreateDataModel(Guid.NewGuid().ToString(), "post", PostType.Designer, 0, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); - post = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "post", + post = CreateDataModel(Guid.NewGuid().ToString(), "post", PostType.Designer, -10, true, DateTime.UtcNow); Assert.That(() => post.Validate(), Throws.TypeOf()); } @@ -83,20 +63,18 @@ internal class PostDataModelTests public void AllFieldsIsCorrectTest() { var postId = Guid.NewGuid().ToString(); - var postPostId = Guid.NewGuid().ToString(); var postName = "name"; var postType = PostType.Designer; 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)); @@ -105,6 +83,6 @@ internal class PostDataModelTests }); } - 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 765933f1de19ff413768dcee9b6b3df990f4fbcf Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 05:17:18 +0400 Subject: [PATCH 02/14] add storages --- .../IComponentStorageContract.cs | 20 +++++++++++++++++ .../IEmployeeStorageContract.cs | 22 +++++++++++++++++++ .../IInstallationStorageContract.cs | 15 +++++++++++++ .../StoragesContracts/IPostStorageContract.cs | 22 +++++++++++++++++++ .../ISalaryStorageContract.cs | 11 ++++++++++ 5 files changed, 90 insertions(+) create mode 100644 TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IComponentStorageContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IEmployeeStorageContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IPostStorageContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/StoragesContracts/ISalaryStorageContract.cs diff --git a/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IComponentStorageContract.cs b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IComponentStorageContract.cs new file mode 100644 index 0000000..27c37e0 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IComponentStorageContract.cs @@ -0,0 +1,20 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.StoragesContracts; + +public interface IComponentStorageContract +{ + List GetList(bool onlyActive = true); + + List GetHistoryByProductId(string componentId); + + ComponentDataModel? GetElementById(string id); + + ComponentDataModel? GetElementByName(string name); + + void AddElement(ComponentDataModel componentDataModel); + + void UpdElement(ComponentDataModel componentDataModel); + + void DelElement(string id); +} diff --git a/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IEmployeeStorageContract.cs b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IEmployeeStorageContract.cs new file mode 100644 index 0000000..8922357 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IEmployeeStorageContract.cs @@ -0,0 +1,22 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.StoragesContracts; + +public interface IEmployeeStorageContract +{ + List GetList(bool onlyActive = true, string? postId = null, + DateTime? fromBirthDate = null, DateTime? toBirthDate = null, + DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null); + + EmployeeDataModel? GetElementById(string id); + + EmployeeDataModel? GetElementByFIO(string fio); + + EmployeeDataModel? GetElementByEmail(string email); + + void AddElement(EmployeeDataModel employeeDataModel); + + void UpdElement(EmployeeDataModel employeeDataModel); + + void DelElement(string id); +} diff --git a/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs new file mode 100644 index 0000000..041ee26 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs @@ -0,0 +1,15 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.StoragesContracts; + +public interface IInstallationStorageContract +{ + List GetList(DateTime? startDate = null, DateTime? endDate = null, + string? employeeId = null, string? productId = null); + + InstallationDataModel? GetElementById(string id); + + void AddElement(InstallationDataModel installationDataModel); + + void DelElement(string id); +} diff --git a/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IPostStorageContract.cs b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IPostStorageContract.cs new file mode 100644 index 0000000..38db2ef --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IPostStorageContract.cs @@ -0,0 +1,22 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.StoragesContracts; + +public interface IPostStorageContract +{ + List GetList(bool onlyActual = true); + + List GetPostWithHistory(string postId); + + PostDataModel? GetElementById(string id); + + PostDataModel? GetElementByName(string name); + + void AddElement(PostDataModel postDataModel); + + void UpdElement(PostDataModel postDataModel); + + void DelElement(string id); + + void ResElement(string id); +} diff --git a/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/ISalaryStorageContract.cs b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/ISalaryStorageContract.cs new file mode 100644 index 0000000..402ea53 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/ISalaryStorageContract.cs @@ -0,0 +1,11 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.StoragesContracts; + +public interface ISalaryStorageContract +{ + List GetList(DateTime startDate, DateTime endDate, + string? employeeId = null); + + void AddElement(SalaryDataModel salaryDataModel); +} -- 2.25.1 From 4d3cf6cefb5a17d27c7208ed33d5140798f45f69 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 05:17:33 +0400 Subject: [PATCH 03/14] add business logic --- .../IComponentBusinessLogicContract.cs | 16 +++++++++++++ .../IEmployeeBusinessLogicContract.cs | 24 +++++++++++++++++++ .../IInstallationBusinessLogicContract.cs | 20 ++++++++++++++++ .../IPostBusinessLogicContract.cs | 20 ++++++++++++++++ .../ISalaryBusinessLogicContract.cs | 13 ++++++++++ 5 files changed, 93 insertions(+) create mode 100644 TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IComponentBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IEmployeeBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IInstallationBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IPostBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/ISalaryBusinessLogicContract.cs diff --git a/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IComponentBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IComponentBusinessLogicContract.cs new file mode 100644 index 0000000..aef646a --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IComponentBusinessLogicContract.cs @@ -0,0 +1,16 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.BusinessLogicContracts; + +public interface IComponentBusinessLogicContract +{ + List GetAllComponents(bool onlyActive = true); + + ComponentDataModel GetComponentByData(string data); + + void InsertComponent(ComponentDataModel componentDataModel); + + void UpdateComponent(ComponentDataModel componentDataModel); + + void DeleteComponent(string id); +} diff --git a/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IEmployeeBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IEmployeeBusinessLogicContract.cs new file mode 100644 index 0000000..aed6028 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IEmployeeBusinessLogicContract.cs @@ -0,0 +1,24 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.BusinessLogicContracts; + +public interface IEmployeeBusinessLogicContract +{ + List GetAllEmployees(bool onlyActive = true); + + List GetAllEmployeesByPost(string postId, bool onlyActive = true); + + List GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate, + bool onlyActive = true); + + List GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate, + bool onlyActive = true); + + EmployeeDataModel GetEmployeeByData(string data); + + void InsertEmployee(EmployeeDataModel employeeDataModel); + + void UpdateEmployee(EmployeeDataModel employeeDataModel); + + void DeleteEmployee(string id); +} diff --git a/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IInstallationBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IInstallationBusinessLogicContract.cs new file mode 100644 index 0000000..57305e6 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IInstallationBusinessLogicContract.cs @@ -0,0 +1,20 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.BusinessLogicContracts; + +public interface IInstallationBusinessLogicContract +{ + List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate); + + List GetAllInstallationsByEmployeeByPeriod(string employeeId, + DateTime fromDate, DateTime toDate); + + List GetAllInstallationsByComponentByPeriod(string componentId, + DateTime fromDate, DateTime toDate); + + InstallationDataModel GetInstallationByData(string data); + + void InsertInstallation(InstallationDataModel installationDataModel); + + void CancelInstallation(string id); +} diff --git a/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IPostBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IPostBusinessLogicContract.cs new file mode 100644 index 0000000..5cbdc5f --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/IPostBusinessLogicContract.cs @@ -0,0 +1,20 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.BusinessLogicContracts; + +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/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/ISalaryBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/ISalaryBusinessLogicContract.cs new file mode 100644 index 0000000..98cc044 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/BusinessLogicContracts/ISalaryBusinessLogicContract.cs @@ -0,0 +1,13 @@ +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsContracts.BusinessLogicContracts; + +public interface ISalaryBusinessLogicContract +{ + List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate); + + List GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, + string employeeId); + + void CalculateSalaryByMounth(DateTime date); +} -- 2.25.1 From 4fdff7b3359ddcba5d068b033c6bb617078cd807 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 05:35:41 +0400 Subject: [PATCH 04/14] add temporary business logic implemantation --- .../ComponentBusinessLogicContract.cs | 30 +++++++++++++ .../EmployeeBusinessLogicContract.cs | 44 +++++++++++++++++++ .../InstallationBusinessLogicContract.cs | 35 +++++++++++++++ .../PostBusinessLogicContract.cs | 39 ++++++++++++++++ .../SalaryBusinessLogicContract.cs | 21 +++++++++ .../TheCyclopsBusinessLogic.csproj | 13 ++++++ TheCyclopsProject/TheCyclopsProject.sln | 8 +++- 7 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs create mode 100644 TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs new file mode 100644 index 0000000..6e01abd --- /dev/null +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs @@ -0,0 +1,30 @@ +using TheCyclopsContracts.BusinessLogicContracts; +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Enums; + +namespace TheCyclopsBusinessLogic.Implementations; + +internal class ComponentBusinessLogicContract : IComponentBusinessLogicContract +{ + public List GetAllComponents(bool onlyActive = true) + { + return []; + } + + public ComponentDataModel GetComponentByData(string data) + { + return new ComponentDataModel("", "", ComponentType.None, 0, false); + } + + public void InsertComponent(ComponentDataModel componentDataModel) + { + } + + public void UpdateComponent(ComponentDataModel componentDataModel) + { + } + + public void DeleteComponent(string id) + { + } +} diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs new file mode 100644 index 0000000..58ddf7e --- /dev/null +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs @@ -0,0 +1,44 @@ +using TheCyclopsContracts.BusinessLogicContracts; +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsBusinessLogic.Implementations; + +internal class EmployeeBusinessLogicContract : IEmployeeBusinessLogicContract +{ + public List GetAllEmployees(bool onlyActive = true) + { + return []; + } + + public List GetAllEmployeesByPost(string postId, bool onlyActive = true) + { + return []; + } + + public List GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) + { + return []; + } + + public List GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) + { + return []; + } + + public EmployeeDataModel GetEmployeeByData(string data) + { + return new EmployeeDataModel("", "", "", "", DateTime.Now, DateTime.Now, false); + } + + public void InsertEmployee(EmployeeDataModel employeeDataModel) + { + } + + public void UpdateEmployee(EmployeeDataModel employeeDataModel) + { + } + + public void DeleteEmployee(string id) + { + } +} diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs new file mode 100644 index 0000000..af1e3d8 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs @@ -0,0 +1,35 @@ +using TheCyclopsContracts.BusinessLogicContracts; +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsBusinessLogic.Implementations; + +internal class InstallationBusinessLogicContract : IInstallationBusinessLogicContract +{ + public List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) + { + return []; + } + + public List GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate) + { + return []; + } + + public List GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate) + { + return []; + } + + public InstallationDataModel GetInstallationByData(string data) + { + return new InstallationDataModel("", "", DateTime.Now, 0, 0, false, []); + } + + public void InsertInstallation(InstallationDataModel installationDataModel) + { + } + + public void CancelInstallation(string id) + { + } +} diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs new file mode 100644 index 0000000..9381bed --- /dev/null +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -0,0 +1,39 @@ +using TheCyclopsContracts.BusinessLogicContracts; +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Enums; + +namespace TheCyclopsBusinessLogic.Implementations; + +internal class PostBusinessLogicContract : IPostBusinessLogicContract +{ + public List GetAllPosts(bool onlyActive) + { + return []; + } + + public List GetAllDataOfPost(string postId) + { + return []; + } + + public PostDataModel GetPostByData(string data) + { + return new PostDataModel("", "", PostType.None, 0, false, DateTime.Now); + } + + public void InsertPost(PostDataModel postDataModel) + { + } + + public void UpdatePost(PostDataModel postDataModel) + { + } + + public void DeletePost(string id) + { + } + + public void RestorePost(string id) + { + } +} diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs new file mode 100644 index 0000000..647ff26 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -0,0 +1,21 @@ +using TheCyclopsContracts.BusinessLogicContracts; +using TheCyclopsContracts.DataModels; + +namespace TheCyclopsBusinessLogic.Implementations; + +internal class SalaryBusinessLogicContract : ISalaryBusinessLogicContract +{ + public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) + { + return []; + } + + public List GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId) + { + return []; + } + + public void CalculateSalaryByMounth(DateTime date) + { + } +} diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj b/TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj new file mode 100644 index 0000000..a864438 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/TheCyclopsProject/TheCyclopsProject.sln b/TheCyclopsProject/TheCyclopsProject.sln index 4fa5a94..72c62c5 100644 --- a/TheCyclopsProject/TheCyclopsProject.sln +++ b/TheCyclopsProject/TheCyclopsProject.sln @@ -1,12 +1,14 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.12.35707.178 d17.12 +VisualStudioVersion = 17.12.35707.178 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsContracts", "TheCyclopsContracts\TheCyclopsContracts.csproj", "{01934235-715E-4B95-B859-48E8C89DA7CB}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsTests", "TheCyclopsTests\TheCyclopsTests.csproj", "{78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsBusinessLogic", "TheCyclopsBusinessLogic\TheCyclopsBusinessLogic.csproj", "{A696C749-D71B-4D1E-B2B0-1E1503F033F0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Debug|Any CPU.Build.0 = Debug|Any CPU {78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Release|Any CPU.ActiveCfg = Release|Any CPU {78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Release|Any CPU.Build.0 = Release|Any CPU + {A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- 2.25.1 From bebac80679f7a8cbbf257e5bc4ddaee38914c0c0 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 18:01:38 +0400 Subject: [PATCH 05/14] add exceptions --- .../Exceptions/ElementExistsException.cs | 14 ++++++++++++++ .../Exceptions/ElementNotFoundException.cs | 11 +++++++++++ .../Exceptions/IncorrectDatesException.cs | 8 ++++++++ .../Exceptions/NullListException.cs | 6 ++++++ .../Exceptions/StorageException.cs | 6 ++++++ 5 files changed, 45 insertions(+) create mode 100644 TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementExistsException.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementNotFoundException.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/Exceptions/IncorrectDatesException.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/Exceptions/NullListException.cs create mode 100644 TheCyclopsProject/TheCyclopsContracts/Exceptions/StorageException.cs diff --git a/TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementExistsException.cs b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementExistsException.cs new file mode 100644 index 0000000..00494d9 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementExistsException.cs @@ -0,0 +1,14 @@ +namespace TheCyclopsContracts.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/TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementNotFoundException.cs b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementNotFoundException.cs new file mode 100644 index 0000000..d7e45bb --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ElementNotFoundException.cs @@ -0,0 +1,11 @@ +namespace TheCyclopsContracts.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/TheCyclopsProject/TheCyclopsContracts/Exceptions/IncorrectDatesException.cs b/TheCyclopsProject/TheCyclopsContracts/Exceptions/IncorrectDatesException.cs new file mode 100644 index 0000000..7df6003 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Exceptions/IncorrectDatesException.cs @@ -0,0 +1,8 @@ +namespace TheCyclopsContracts.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/TheCyclopsProject/TheCyclopsContracts/Exceptions/NullListException.cs b/TheCyclopsProject/TheCyclopsContracts/Exceptions/NullListException.cs new file mode 100644 index 0000000..2a9b8b6 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Exceptions/NullListException.cs @@ -0,0 +1,6 @@ +namespace TheCyclopsContracts.Exceptions; + +public class NullListException : Exception +{ + public NullListException() : base("The returned list is null") { } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Exceptions/StorageException.cs b/TheCyclopsProject/TheCyclopsContracts/Exceptions/StorageException.cs new file mode 100644 index 0000000..64cf31e --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Exceptions/StorageException.cs @@ -0,0 +1,6 @@ +namespace TheCyclopsContracts.Exceptions; + +public class StorageException : Exception +{ + public StorageException(Exception ex) : base($"Error while working in storage: {ex.Message}", ex) { } +} -- 2.25.1 From 542506a77e7877a60b96a35cd91f566f6cda3b1a Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 18:02:02 +0400 Subject: [PATCH 06/14] add DateTime extension --- .../TheCyclopsContracts/Extensions/DateTimeExtensions.cs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 TheCyclopsProject/TheCyclopsContracts/Extensions/DateTimeExtensions.cs diff --git a/TheCyclopsProject/TheCyclopsContracts/Extensions/DateTimeExtensions.cs b/TheCyclopsProject/TheCyclopsContracts/Extensions/DateTimeExtensions.cs new file mode 100644 index 0000000..2c14c48 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Extensions/DateTimeExtensions.cs @@ -0,0 +1,9 @@ +namespace TheCyclopsContracts.Extensions; + +public static class DateTimeExtensions +{ + public static bool IsDateNotOlder(this DateTime date, DateTime olderDate) + { + return date >= olderDate; + } +} -- 2.25.1 From eb995c94b8db0b9a8ac5cd485913c8d25b885642 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 21:09:05 +0400 Subject: [PATCH 07/14] add tests for temporary business logic implementations --- .../ComponentBusinessLogicContract.cs | 9 +- .../EmployeeBusinessLogicContract.cs | 9 +- .../InstallationBusinessLogicContract.cs | 9 +- .../PostBusinessLogicContract.cs | 9 +- .../SalaryBusinessLogicContract.cs | 15 +- .../TheCyclopsBusinessLogic.csproj | 9 + .../ComponentBusinessLogicContractTests.cs | 356 +++++++++++ .../EmployeeBusinessLogicContractTest.cs | 557 ++++++++++++++++++ .../InstallationBusinessLogicContractTest.cs | 418 +++++++++++++ .../PostBusinessLogicContractTest.cs | 454 ++++++++++++++ .../SalaryBusinessLogicContractTest.cs | 348 +++++++++++ .../TheCyclopsTests/TheCyclopsTests.csproj | 2 + 12 files changed, 2185 insertions(+), 10 deletions(-) create mode 100644 TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs create mode 100644 TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs create mode 100644 TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs create mode 100644 TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs create mode 100644 TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs index 6e01abd..8c11d7a 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs @@ -1,11 +1,16 @@ -using TheCyclopsContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Enums; +using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; -internal class ComponentBusinessLogicContract : IComponentBusinessLogicContract +internal class ComponentBusinessLogicContract(IComponentStorageContract componentStorageContract, ILogger logger) : IComponentBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IComponentStorageContract _componentStorageContract = componentStorageContract; + public List GetAllComponents(bool onlyActive = true) { return []; diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs index 58ddf7e..f751d04 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs @@ -1,10 +1,15 @@ -using TheCyclopsContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; -internal class EmployeeBusinessLogicContract : IEmployeeBusinessLogicContract +internal class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeStorageContract, ILogger logger) : IEmployeeBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract; + public List GetAllEmployees(bool onlyActive = true) { return []; diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs index af1e3d8..ea93dcb 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs @@ -1,10 +1,15 @@ -using TheCyclopsContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; -internal class InstallationBusinessLogicContract : IInstallationBusinessLogicContract +internal class InstallationBusinessLogicContract(IInstallationStorageContract installationStorageContract, ILogger logger) : IInstallationBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract; + public List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) { return []; diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs index 9381bed..57cd823 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -1,11 +1,16 @@ -using TheCyclopsContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Enums; +using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; -internal class PostBusinessLogicContract : IPostBusinessLogicContract +internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly IPostStorageContract _postStorageContract = postStorageContract; + public List GetAllPosts(bool onlyActive) { return []; diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index 647ff26..8cde41a 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,10 +1,21 @@ -using TheCyclopsContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; -internal class SalaryBusinessLogicContract : ISalaryBusinessLogicContract +internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, + IInstallationStorageContract installationStorageContract, + IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract, + ILogger logger) : ISalaryBusinessLogicContract { + private readonly ILogger _logger = logger; + private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; + private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract; + private readonly IPostStorageContract _postStorageContract = postStorageContract; + private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract; + public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) { return []; diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj b/TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj index a864438..2125c29 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/TheCyclopsBusinessLogic.csproj @@ -6,6 +6,15 @@ enable + + + + + + + + + diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs new file mode 100644 index 0000000..1c59a4f --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs @@ -0,0 +1,356 @@ +using Microsoft.Extensions.Logging; +using Moq; +using TheCyclopsBusinessLogic.Implementations; +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.StoragesContracts; + +namespace TheCyclopsTests.BusinessLogicsContractsTests; + +[TestFixture] +internal class ComponentBusinessLogicContractTests +{ + private ComponentBusinessLogicContract _componentBusinessLogicContract; + private Mock _componentStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _componentStorageContract = new Mock(); + _componentBusinessLogicContract = new ComponentBusinessLogicContract(_componentStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _componentStorageContract.Reset(); + } + + [Test] + public void GetAllComponents_ReturnListOfRecords_Test() + { + // Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "name 1", ComponentType.Other, 10, false), + new(Guid.NewGuid().ToString(), "name 2", ComponentType.Other, 10, true), + new(Guid.NewGuid().ToString(), "name 3", ComponentType.Other, 10, false), + }; + _componentStorageContract.Setup(x => x.GetList(It.IsAny())).Returns(listOriginal); + + // Act + var listOnlyActive = _componentBusinessLogicContract.GetAllComponents(true); + var list = _componentBusinessLogicContract.GetAllComponents(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)); + }); + _componentStorageContract.Verify(x => x.GetList(true), Times.Once); + _componentStorageContract.Verify(x => x.GetList(false), Times.Once); + } + + [Test] + public void GetAllComponents_ReturnEmptyList_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.GetList(It.IsAny())).Returns(new List()); + + // Act + var listOnlyActive = _componentBusinessLogicContract.GetAllComponents(true); + var list = _componentBusinessLogicContract.GetAllComponents(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)); + }); + _componentStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Exactly(2)); + } + + [Test] + public void GetAllComponents_ReturnNull_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.GetAllComponents(It.IsAny()), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Once); + } + + [Test] + public void GetAllComponents_StorageThrowError_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.GetList(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.GetAllComponents(It.IsAny()), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Once); + } + + [Test] + public void GetComponentByData_GetById_ReturnRecord_Test() + { + // Arrange + var id = Guid.NewGuid().ToString(); + var record = new ComponentDataModel(id, "name", ComponentType.Other, 10, false); + _componentStorageContract.Setup(x => x.GetElementById(id)).Returns(record); + + // Act + var element = _componentBusinessLogicContract.GetComponentByData(id); + + // Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _componentStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetComponentByData_GetByName_ReturnRecord_Test() + { + // Arrange + var name = "name"; + var record = new ComponentDataModel(Guid.NewGuid().ToString(), name, ComponentType.Other, 10, false); + _componentStorageContract.Setup(x => x.GetElementByName(name)).Returns(record); + + // Act + var element = _componentBusinessLogicContract.GetComponentByData(name); + + // Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.ComponentName, Is.EqualTo(name)); + _componentStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void GetComponentByData_EmptyData_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.GetComponentByData(null), Throws.TypeOf()); + Assert.That(() => _componentBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); + _componentStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + } + + [Test] + public void GetComponentByData_GetById_NotFoundRecord_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetComponentByData_GetByName_NotFoundRecord_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.GetComponentByData("name"), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void GetComponentByData_StorageThrowError_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _componentStorageContract.Setup(x => x.GetElementByName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => _componentBusinessLogicContract.GetComponentByData("name"), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _componentStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void InsertComponent_CorrectRecord_Test() + { + // Arrange + var flag = false; + var record = new ComponentDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false); + _componentStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((ComponentDataModel x) => + { + flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType && + x.Price == record.Price && x.IsDeleted == record.IsDeleted; + }); + + // Act + _componentBusinessLogicContract.InsertComponent(record); + + // Assert + _componentStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertComponent_RecordWithExistsData_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertComponent_NullRecord_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.InsertComponent(null), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertComponent_InvalidRecord_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.InsertComponent(new ComponentDataModel("id", "name", ComponentType.Other, 10, false)), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertComponent_StorageThrowError_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateComponent_CorrectRecord_Test() + { + // Arrange + var flag = false; + var record = new ComponentDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false); + _componentStorageContract.Setup(x => x.UpdElement(It.IsAny())) + .Callback((ComponentDataModel x) => + { + flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType && + x.Price == record.Price && x.IsDeleted == record.IsDeleted; + }); + + // Act + _componentBusinessLogicContract.UpdateComponent(record); + + // Assert + _componentStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void UpdateComponent_RecordWithIncorrectData_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateComponent_RecordWithExistsData_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "anme", ComponentType.Other, 10, false)), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateComponent_NullRecord_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.UpdateComponent(null), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateComponent_InvalidRecord_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new ComponentDataModel("id", "name", ComponentType.Other, 10, false)), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateComponent_StorageThrowError_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteComponent_CorrectRecord_Test() + { + // Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _componentStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + + // Act + _componentBusinessLogicContract.DeleteComponent(id); + + // Assert + _componentStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void DeleteComponent_RecordWithIncorrectId_ThrowException_Test() + { + // Arrange + var id = Guid.NewGuid().ToString(); + _componentStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteComponent_IdIsNullOrEmpty_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.DeleteComponent(null), Throws.TypeOf()); + Assert.That(() => _componentBusinessLogicContract.DeleteComponent(string.Empty), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteComponent_IdIsNotGuid_ThrowException_Test() + { + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.DeleteComponent("id"), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteComponent_StorageThrowError_ThrowException_Test() + { + // Arrange + _componentStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + + // Act & Assert + Assert.That(() => _componentBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf()); + _componentStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } +} diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs new file mode 100644 index 0000000..962255e --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs @@ -0,0 +1,557 @@ +using Microsoft.Extensions.Logging; +using Moq; +using TheCyclopsBusinessLogic.Implementations; +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.StoragesContracts; + +namespace TheCyclopsTests.BusinessLogicsContractsTests; + +[TestFixture] +internal class EmployeeBusinessLogicContractTest +{ + private EmployeeBusinessLogicContract _employeeBusinessLogicContract; + private Mock _employeeStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _employeeStorageContract = new Mock(); + _employeeBusinessLogicContract = new EmployeeBusinessLogicContract(_employeeStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _employeeStorageContract.Reset(); + } + + [Test] + public void GetAllEmployees_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), + new(Guid.NewGuid().ToString(), "fio 3", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + }; + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployees(true); + var list = _employeeBusinessLogicContract.GetAllEmployees(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(true, null, null, null, null, null), Times.Once); + _employeeStorageContract.Verify(x => x.GetList(false, null, null, null, null, null), Times.Once); + } + + [Test] + public void GetAllEmployees_ReturnEmptyList_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployees(true); + var list = _employeeBusinessLogicContract.GetAllEmployees(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), null, null, null, null, null), Times.Exactly(2)); + } + + [Test] + public void GetAllEmployees_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployees(It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllEmployees_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.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(() => _employeeBusinessLogicContract.GetAllEmployees(It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), null, null, null, null, null), Times.Once); + } + + [Test] + public void GetAllEmployeesByPost_ReturnListOfRecords_Test() + { + //Arrange + var postId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1","email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), + new(Guid.NewGuid().ToString(), "fio 3", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + }; + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByPost(postId, true); + var list = _employeeBusinessLogicContract.GetAllEmployeesByPost(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(true, postId, null, null, null, null), Times.Once); + _employeeStorageContract.Verify(x => x.GetList(false, postId, null, null, null, null), Times.Once); + } + + [Test] + public void GetAllEmployeesByPost_ReturnEmptyList_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByPost(Guid.NewGuid().ToString(), true); + var list = _employeeBusinessLogicContract.GetAllEmployeesByPost(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); + } + + [Test] + public void GetAllEmployeesByPost_PostIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(null, It.IsAny()), Throws.TypeOf()); + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(string.Empty, It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllEmployeesByPost_PostIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost("postId", It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllEmployeesByPost_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllEmployeesByPost_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.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(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllEmployeesByBirthDate_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), + new(Guid.NewGuid().ToString(), "fio 3","email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + }; + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(date, date.AddDays(1), true); + var list = _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(true, null, date, date.AddDays(1), null, null), Times.Once); + _employeeStorageContract.Verify(x => x.GetList(false, null, date, date.AddDays(1), null, null), Times.Once); + } + + [Test] + public void GetAllEmployeesByBirthDate_ReturnEmptyList_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true); + var list = _employeeBusinessLogicContract.GetAllEmployees(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(true, null, It.IsAny(), It.IsAny(), null, null), Times.Once); + _employeeStorageContract.Verify(x => x.GetList(false, null, It.IsAny(), It.IsAny(), null, null), Times.Once); + } + + [Test] + public void GetAllEmployeesByBirthDate_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(date, date, It.IsAny()), Throws.TypeOf()); + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(date, date.AddSeconds(-1), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllEmployeesByBirthDate_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllEmployeesByBirthDate_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.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(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllEmployeesByEmploymentDate_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), + new(Guid.NewGuid().ToString(), "fio 3", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), + }; + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(date, date.AddDays(1), true); + var list = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(true, null, null, null, date, date.AddDays(1)), Times.Once); + _employeeStorageContract.Verify(x => x.GetList(false, null, null, null, date, date.AddDays(1)), Times.Once); + } + + [Test] + public void GetAllEmployeesByEmploymentDate_ReturnEmptyList_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true); + var list = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(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)); + }); + _employeeStorageContract.Verify(x => x.GetList(true, null, null, null, It.IsAny(), It.IsAny()), Times.Once); + _employeeStorageContract.Verify(x => x.GetList(false, null, null, null, It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllEmployeesByEmploymentDate_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(date, date, It.IsAny()), Throws.TypeOf()); + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(date, date.AddSeconds(-1), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllEmployeesByEmploymentDate_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllEmployeesByEmploymentDate_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.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(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetEmployeeByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new EmployeeDataModel(id, "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); + _employeeStorageContract.Setup(x => x.GetElementById(id)).Returns(record); + //Act + var element = _employeeBusinessLogicContract.GetEmployeeByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _employeeStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetEmployeeByData_GetByFio_ReturnRecord_Test() + { + //Arrange + var fio = "fio"; + var record = new EmployeeDataModel(Guid.NewGuid().ToString(), fio, "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); + _employeeStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record); + //Act + var element = _employeeBusinessLogicContract.GetEmployeeByData(fio); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.FIO, Is.EqualTo(fio)); + _employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); + } + + [Test] + public void GetEmployeeByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(null), Throws.TypeOf()); + Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(string.Empty), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + } + + [Test] + public void GetEmployeeByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + } + + [Test] + public void GetEmployeeByData_GetByFio_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData("fio"), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); + } + + [Test] + public void GetEmployeeByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _employeeStorageContract.Setup(x => x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData("fio"), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); + } + + [Test] + public void InsertEmployee_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); + _employeeStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((EmployeeDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate && + x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted; + }); + //Act + _employeeBusinessLogicContract.InsertEmployee(record); + //Assert + _employeeStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertEmployee_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertEmployee_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(null), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertEmployee_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(new EmployeeDataModel("id", "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertEmployee_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateEmployee_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); + _employeeStorageContract.Setup(x => x.UpdElement(It.IsAny())) + .Callback((EmployeeDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate && + x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted; + }); + //Act + _employeeBusinessLogicContract.UpdateEmployee(record); + //Assert + _employeeStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void UpdateEmployee_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateEmployee_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(null), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateEmployee_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(new EmployeeDataModel("id", "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateEmployee_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteEmployee_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _employeeStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + //Act + _employeeBusinessLogicContract.DeleteEmployee(id); + //Assert + _employeeStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void DeleteEmployee_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _employeeStorageContract.Setup(x => x.DelElement(It.Is((string x) => x != id))).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(Guid.NewGuid().ToString()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteEmployee_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(null), Throws.TypeOf()); + Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(string.Empty), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteEmployee_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee("id"), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void DeleteEmployee_StorageThrowError_ThrowException_Test() + { + //Arrange + _employeeStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(Guid.NewGuid().ToString()), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } +} diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs new file mode 100644 index 0000000..78ae462 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs @@ -0,0 +1,418 @@ +using Microsoft.Extensions.Logging; +using Moq; +using TheCyclopsBusinessLogic.Implementations; +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.StoragesContracts; + +namespace TheCyclopsTests.BusinessLogicsContractsTests; + +internal class InstallationBusinessLogicContractTest +{ + private InstallationBusinessLogicContract _installationBusinessLogicContract; + private Mock _installationStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _installationStorageContract = new Mock(); + _installationBusinessLogicContract = new InstallationBusinessLogicContract(_installationStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _installationStorageContract.Reset(); + } + + [Test] + public void GetAllInstallationsByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, + [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []), + }; + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = _installationBusinessLogicContract.GetAllInstallationsByPeriod(date, date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _installationStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null), Times.Once); + } + + [Test] + public void GetAllInstallationsByPeriod_ReturnEmptyList_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = _installationBusinessLogicContract.GetAllInstallationsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(date, date), Throws.TypeOf()); + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllInstallationsByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByEmployeeByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var employeeId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, + [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []), + }; + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(employeeId, date, date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _installationStorageContract.Verify(x => x.GetList(date, date.AddDays(1), employeeId, null), Times.Once); + } + + [Test] + public void GetAllInstallationsByEmployeeByPeriod_ReturnEmptyList_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByEmployeeByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf()); + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllInstallationsByEmployeeByPeriod_EmployeeIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllInstallationsByEmployeeByPeriod_EmployeeIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod("employeeId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllInstallationsByEmployeeByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByEmployeeByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByComponentByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var componentId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, + [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []), + }; + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(componentId, date, date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _installationStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, componentId), Times.Once); + } + + [Test] + public void GetAllInstallationsByComponentByPeriod_ReturnEmptyList_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByComponentByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf()); + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllInstallationsByComponentByPeriod_ComponentIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllInstallationsByComponentByPeriod_ComponentIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod("componentId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllInstallationsByComponentByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllInstallationsByComponentByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetInstallationByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new InstallationDataModel(id, Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, + [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); + _installationStorageContract.Setup(x => x.GetElementById(id)).Returns(record); + //Act + var element = _installationBusinessLogicContract.GetInstallationByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _installationStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetInstallationByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(null), Throws.TypeOf()); + Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(string.Empty), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + } + + + [Test] + public void GetInstallationByData_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetInstallationByData("installationId"), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + } + + [Test] + public void GetInstallationByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetInstallationByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void InsertInstallation_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new InstallationDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, + [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); + _installationStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((InstallationDataModel x) => + { + flag = x.Id == record.Id && x.EmployeeId == record.EmployeeId && + x.InstallationDate == record.InstallationDate && x.Sum == record.Sum && + x.IsCanceled == record.IsCanceled && x.Components.Count == record.Components.Count && + x.Components.First().ComponentId == record.Components.First().ComponentId && + x.Components.First().InstallationId == record.Components.First().InstallationId && + x.Components.First().Count == record.Components.First().Count; + }); + //Act + _installationBusinessLogicContract.InsertInstallation(record); + //Assert + _installationStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertInstallation_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 0, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertInstallation_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.InsertInstallation(null), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertInstallation_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new InstallationDataModel("id", Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, [])), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertInstallation_StorageThrowError_ThrowException_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + DateTime.UtcNow, 10, 0, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void CancelInstallation_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _installationStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + //Act + _installationBusinessLogicContract.CancelInstallation(id); + //Assert + _installationStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void CancelInstallation_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _installationStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.CancelInstallation(Guid.NewGuid().ToString()), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } + + [Test] + public void CancelInstallation_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.CancelInstallation(null), Throws.TypeOf()); + Assert.That(() => _installationBusinessLogicContract.CancelInstallation(string.Empty), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void CancelInstallation_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.CancelInstallation("id"), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } + + [Test] + public void CancelInstallation_StorageThrowError_ThrowException_Test() + { + //Arrange + _installationStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _installationBusinessLogicContract.CancelInstallation(Guid.NewGuid().ToString()), Throws.TypeOf()); + _installationStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } +} diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs new file mode 100644 index 0000000..715de47 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs @@ -0,0 +1,454 @@ +using Microsoft.Extensions.Logging; +using Moq; +using TheCyclopsBusinessLogic.Implementations; +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.StoragesContracts; + +namespace TheCyclopsTests.BusinessLogicsContractsTests; + +[TestFixture] +internal class PostBusinessLogicContractTest +{ + private PostBusinessLogicContract _postBusinessLogicContract; + private Mock _postStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _postStorageContract = new Mock(); + _postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _postStorageContract.Reset(); + } + + [Test] + public void GetAllPosts_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(),"name 1", PostType.Administrator, 10, true, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 2", PostType.Administrator, 10, false, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 3", PostType.Administrator, 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.Administrator, 10, true, DateTime.UtcNow), + new(postId, "name 2", PostType.Administrator, 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.Administrator, 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.Administrator, 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.Designer, 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.Designer, 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.Designer, 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.Designer, 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.Designer, 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.Designer, 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.Designer, 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.Designer, 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.Designer, 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/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs new file mode 100644 index 0000000..d1a630e --- /dev/null +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs @@ -0,0 +1,348 @@ +using Microsoft.Extensions.Logging; +using Moq; +using TheCyclopsBusinessLogic.Implementations; +using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.StoragesContracts; + +namespace TheCyclopsTests.BusinessLogicsContractsTests; + +[TestFixture] +internal class SalaryBusinessLogicContractTest +{ + private SalaryBusinessLogicContract _salaryBusinessLogicContract; + private Mock _salaryStorageContract; + private Mock _installationStorageContract; + private Mock _postStorageContract; + private Mock _employeeStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _salaryStorageContract = new Mock(); + _installationStorageContract = new Mock(); + _postStorageContract = new Mock(); + _employeeStorageContract = new Mock(); + _salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object, + _installationStorageContract.Object, _postStorageContract.Object, + _employeeStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _salaryStorageContract.Reset(); + _installationStorageContract.Reset(); + _postStorageContract.Reset(); + _employeeStorageContract.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 GetAllSalariesByEmployee_ReturnListOfRecords_Test() + { + //Arrange + var startDate = DateTime.UtcNow; + var endDate = DateTime.UtcNow.AddDays(1); + var employeeId = 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.GetAllSalariesByPeriodByEmployee(startDate, endDate, employeeId); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _salaryStorageContract.Verify(x => x.GetList(startDate, endDate, employeeId), Times.Once); + } + + [Test] + public void GetAllSalariesByEmployee_ReturnEmptyList_Test() + { + //Arrange + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(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 GetAllSalariesByEmployee_IncorrectDates_ThrowException_Test() + { + //Arrange + var dateTime = DateTime.UtcNow; + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(dateTime, dateTime, Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(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 GetAllSalariesByEmployee_EmployeeIdIsNUllOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf()); + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(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 GetAllSalariesByEmployee_EmployeeIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "employeeId"), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalariesByEmployee_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(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 GetAllSalariesByEmployee_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.GetAllSalariesByPeriodByEmployee(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 employeeId = Guid.NewGuid().ToString(); + var installationPrice = 200.0; + var installationSum = 200.0; + var postSalary = 2000.0; + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, installationPrice, installationSum, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, postSalary, true, DateTime.UtcNow)); + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + var sum = 0.0; + var expectedSum = postSalary + installationPrice * 0.1; + _salaryStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((SalaryDataModel x) => + { + installationSum = x.Salary; + }); + //Act + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow); + //Assert + Assert.That(installationSum, Is.EqualTo(expectedSum)); + } + + [Test] + public void CalculateSalaryByMounth_WithSeveralEmployees_Test() + { + //Arrange + var employee1Id = Guid.NewGuid().ToString(); + var employee2Id = Guid.NewGuid().ToString(); + var employee3Id = Guid.NewGuid().ToString(); + var list = new List() { + new(employee1Id, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false), + new(employee2Id, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false), + new(employee3Id, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false) + }; + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employee1Id, DateTime.UtcNow, 1, 0, false, []), + new InstallationDataModel(Guid.NewGuid().ToString(), employee1Id, DateTime.UtcNow, 1, 0, false, []), + new InstallationDataModel(Guid.NewGuid().ToString(), employee2Id, DateTime.UtcNow, 1, 0, false, []), + new InstallationDataModel(Guid.NewGuid().ToString(), employee3Id, DateTime.UtcNow, 1, 0, false, []), + new InstallationDataModel(Guid.NewGuid().ToString(), employee3Id, DateTime.UtcNow, 1, 0, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow)); + _employeeStorageContract.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_WithoitInstallationsByEmployee_Test() + { + //Arrange + var postSalary = 2000.0; + var employeeId = Guid.NewGuid().ToString(); + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, postSalary, true, DateTime.UtcNow)); + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", 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_InstallationStorageReturnNull_ThrowException_Test() + { + //Arrange + var employeeId = Guid.NewGuid().ToString(); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow)); + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", 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 employeeId = Guid.NewGuid().ToString(); + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]); + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf()); + } + + [Test] + public void CalculateSalaryByMounth_EmployeeStorageReturnNull_ThrowException_Test() + { + //Arrange + var employeeId = Guid.NewGuid().ToString(); + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow)); + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf()); + } + + [Test] + public void CalculateSalaryByMounth_InstallationStorageThrowException_ThrowException_Test() + { + //Arrange + var employeeId = Guid.NewGuid().ToString(); + _installationStorageContract.Setup(x => x.GetList(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.Designer, 2000, true, DateTime.UtcNow)); + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", 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 employeeId = Guid.NewGuid().ToString(); + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Throws(new StorageException(new InvalidOperationException())); + _employeeStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf()); + } + + [Test] + public void CalculateSalaryByMounth_EmployeeStorageThrowException_ThrowException_Test() + { + //Arrange + var employeeId = Guid.NewGuid().ToString(); + _installationStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow)); + _employeeStorageContract.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/TheCyclopsProject/TheCyclopsTests/TheCyclopsTests.csproj b/TheCyclopsProject/TheCyclopsTests/TheCyclopsTests.csproj index 349b926..b5ecb3a 100644 --- a/TheCyclopsProject/TheCyclopsTests/TheCyclopsTests.csproj +++ b/TheCyclopsProject/TheCyclopsTests/TheCyclopsTests.csproj @@ -11,12 +11,14 @@ + + -- 2.25.1 From 7f7cbd26af06e1412f6f299aa47e343b61e2a0cc Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 21:24:21 +0400 Subject: [PATCH 08/14] fix var name --- .../StoragesContracts/IInstallationStorageContract.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs index 041ee26..4150ef8 100644 --- a/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs +++ b/TheCyclopsProject/TheCyclopsContracts/StoragesContracts/IInstallationStorageContract.cs @@ -5,7 +5,7 @@ namespace TheCyclopsContracts.StoragesContracts; public interface IInstallationStorageContract { List GetList(DateTime? startDate = null, DateTime? endDate = null, - string? employeeId = null, string? productId = null); + string? employeeId = null, string? componentId = null); InstallationDataModel? GetElementById(string id); -- 2.25.1 From 3a1b867c29edfcaf1c5da2a55cca17fb28810370 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 21:30:51 +0400 Subject: [PATCH 09/14] fix test --- .../InstallationBusinessLogicContractTest.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs index 78ae462..d3cbe8a 100644 --- a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs @@ -305,15 +305,14 @@ internal class InstallationBusinessLogicContractTest [Test] public void InsertInstallation_CorrectRecord_Test() { - //Arrange var flag = false; - var record = new InstallationDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, + var record = new InstallationDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 10, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); _installationStorageContract.Setup(x => x.AddElement(It.IsAny())) .Callback((InstallationDataModel x) => { flag = x.Id == record.Id && x.EmployeeId == record.EmployeeId && - x.InstallationDate == record.InstallationDate && x.Sum == record.Sum && + x.InstallationDate == record.InstallationDate && x.Sum == record.Sum && x.IsCanceled == record.IsCanceled && x.Components.Count == record.Components.Count && x.Components.First().ComponentId == record.Components.First().ComponentId && x.Components.First().InstallationId == record.Components.First().InstallationId && @@ -333,7 +332,7 @@ internal class InstallationBusinessLogicContractTest _installationStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), - DateTime.UtcNow, 10, 0, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); + DateTime.UtcNow, 10, 10, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _installationStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } @@ -360,7 +359,7 @@ internal class InstallationBusinessLogicContractTest _installationStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), - DateTime.UtcNow, 10, 0, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); + DateTime.UtcNow, 10, 10, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _installationStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } -- 2.25.1 From e69e5b7f67d8af5db6a1f07d64436c692552ae6e Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 21:31:02 +0400 Subject: [PATCH 10/14] add real implementations --- .../ComponentBusinessLogicContract.cs | 37 +++++++- .../EmployeeBusinessLogicContract.cs | 60 +++++++++++- .../InstallationBusinessLogicContract.cs | 92 ++++++++++++++++++- .../PostBusinessLogicContract.cs | 58 +++++++++++- .../SalaryBusinessLogicContract.cs | 38 +++++++- 5 files changed, 267 insertions(+), 18 deletions(-) diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs index 8c11d7a..7c5fa94 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/ComponentBusinessLogicContract.cs @@ -1,7 +1,10 @@ using Microsoft.Extensions.Logging; +using System.Text.Json; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -11,25 +14,53 @@ internal class ComponentBusinessLogicContract(IComponentStorageContract componen private readonly ILogger _logger = logger; private readonly IComponentStorageContract _componentStorageContract = componentStorageContract; - public List GetAllComponents(bool onlyActive = true) + public List GetAllComponents(bool onlyActive) { - return []; + _logger.LogInformation("GetAllComponents params: {onlyActive}", onlyActive); + return _componentStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public ComponentDataModel GetComponentByData(string data) { - return new ComponentDataModel("", "", ComponentType.None, 0, false); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _componentStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _componentStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); } public void InsertComponent(ComponentDataModel componentDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(componentDataModel)); + ArgumentNullException.ThrowIfNull(componentDataModel); + componentDataModel.Validate(); + _componentStorageContract.AddElement(componentDataModel); } public void UpdateComponent(ComponentDataModel componentDataModel) { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(componentDataModel)); + ArgumentNullException.ThrowIfNull(componentDataModel); + componentDataModel.Validate(); + _componentStorageContract.UpdElement(componentDataModel); } public void DeleteComponent(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"); + } + _componentStorageContract.DelElement(id); } } diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs index f751d04..d6545da 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs @@ -1,6 +1,9 @@ using Microsoft.Extensions.Logging; +using System.Text.Json; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -12,38 +15,85 @@ internal class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeSt public List GetAllEmployees(bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllEmployees params: {onlyActive}", onlyActive); + return _employeeStorageContract.GetList(onlyActive) ?? throw new NullListException(); } public List GetAllEmployeesByPost(string postId, bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllEmployees 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 _employeeStorageContract.GetList(onlyActive, postId) ?? throw new NullListException(); } public List GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllEmployees params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _employeeStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException(); } public List GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) { - return []; + _logger.LogInformation("GetAllEmployees params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _employeeStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException(); } public EmployeeDataModel GetEmployeeByData(string data) { - return new EmployeeDataModel("", "", "", "", DateTime.Now, DateTime.Now, false); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _employeeStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _employeeStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data); } public void InsertEmployee(EmployeeDataModel employeeDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(employeeDataModel)); + ArgumentNullException.ThrowIfNull(employeeDataModel); + employeeDataModel.Validate(); + _employeeStorageContract.AddElement(employeeDataModel); } public void UpdateEmployee(EmployeeDataModel employeeDataModel) { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(employeeDataModel)); + ArgumentNullException.ThrowIfNull(employeeDataModel); + employeeDataModel.Validate(); + _employeeStorageContract.UpdElement(employeeDataModel); } public void DeleteEmployee(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"); + } + _employeeStorageContract.DelElement(id); } } diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs index ea93dcb..f17e5a0 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs @@ -1,6 +1,9 @@ using Microsoft.Extensions.Logging; +using System.Text.Json; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -10,31 +13,112 @@ internal class InstallationBusinessLogicContract(IInstallationStorageContract in private readonly ILogger _logger = logger; private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract; + //public List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) + //{ + // return []; + //} + + //public List GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate) + //{ + // return []; + //} + + //public List GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate) + //{ + // return []; + //} + + //public InstallationDataModel GetInstallationByData(string data) + //{ + // return new InstallationDataModel("", "", DateTime.Now, 0, 0, false, []); + //} + + //public void InsertInstallation(InstallationDataModel installationDataModel) + //{ + //} + + //public void CancelInstallation(string id) + //{ + //} public List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllInstallations params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _installationStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); } public List GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllInstallations params: {employeeId}, {fromDate}, {toDate}", employeeId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (employeeId.IsEmpty()) + { + throw new ArgumentNullException(nameof(employeeId)); + } + if (!employeeId.IsGuid()) + { + throw new ValidationException("The value in the field employeeId is not a unique identifier."); + } + return _installationStorageContract.GetList(fromDate, toDate, employeeId: employeeId) ?? throw new NullListException(); } public List GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate) { - return []; + _logger.LogInformation("GetAllInstallations params: {componentId}, {fromDate}, {toDate}", componentId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (componentId.IsEmpty()) + { + throw new ArgumentNullException(nameof(componentId)); + } + if (!componentId.IsGuid()) + { + throw new ValidationException("The value in the field componentId is not a unique identifier."); + } + return _installationStorageContract.GetList(fromDate, toDate, componentId: componentId) ?? throw new NullListException(); } public InstallationDataModel GetInstallationByData(string data) { - return new InstallationDataModel("", "", DateTime.Now, 0, 0, false, []); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (!data.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + return _installationStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); } public void InsertInstallation(InstallationDataModel installationDataModel) { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(installationDataModel)); + ArgumentNullException.ThrowIfNull(installationDataModel); + installationDataModel.Validate(); + _installationStorageContract.AddElement(installationDataModel); } public void CancelInstallation(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"); + } + _installationStorageContract.DelElement(id); } } diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs index 57cd823..9fd6bd9 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -1,7 +1,10 @@ using Microsoft.Extensions.Logging; +using System.Text.Json; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -11,34 +14,81 @@ internal class PostBusinessLogicContract(IPostStorageContract postStorageContrac private readonly ILogger _logger = logger; private readonly IPostStorageContract _postStorageContract = postStorageContract; - public List GetAllPosts(bool onlyActive) + public List GetAllPosts(bool onlyActive = true) { - 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 PostDataModel("", "", PostType.None, 0, false, DateTime.Now); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); } public void InsertPost(PostDataModel postDataModel) { + _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/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index 8cde41a..9b8015b 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.Logging; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; using TheCyclopsContracts.StoragesContracts; namespace TheCyclopsBusinessLogic.Implementations; @@ -18,15 +20,47 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC 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 GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId) { - return []; + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (employeeId.IsEmpty()) + { + throw new ArgumentNullException(nameof(employeeId)); + } + if (!employeeId.IsGuid()) + { + throw new ValidationException("The value in the field employeeId is not a unique identifier."); + } + _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {employeeId}", fromDate, toDate, employeeId); + return _salaryStorageContract.GetList(fromDate, toDate, employeeId) ?? 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 employees = _employeeStorageContract.GetList() ?? throw new NullListException(); + foreach (var employee in employees) + { + var installations = _installationStorageContract.GetList(startDate, finishDate, employeeId: employee.Id)?.Sum(x => x.InstallationPrice) ?? + throw new NullListException(); + var post = _postStorageContract.GetElementById(employee.PostId) ?? + throw new NullListException(); + var salary = post.Salary + installations * 0.1; + _logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary); + _salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, finishDate, salary)); + } } } -- 2.25.1 From 18355dcb6c3d6e28dbf25608581cd5bd76d73f89 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Mon, 17 Feb 2025 15:29:11 +0400 Subject: [PATCH 11/14] remove commented temporary emplementation --- .../InstallationBusinessLogicContract.cs | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs index f17e5a0..c3fceb8 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/InstallationBusinessLogicContract.cs @@ -13,33 +13,6 @@ internal class InstallationBusinessLogicContract(IInstallationStorageContract in private readonly ILogger _logger = logger; private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract; - //public List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) - //{ - // return []; - //} - - //public List GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate) - //{ - // return []; - //} - - //public List GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate) - //{ - // return []; - //} - - //public InstallationDataModel GetInstallationByData(string data) - //{ - // return new InstallationDataModel("", "", DateTime.Now, 0, 0, false, []); - //} - - //public void InsertInstallation(InstallationDataModel installationDataModel) - //{ - //} - - //public void CancelInstallation(string id) - //{ - //} public List GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate) { _logger.LogInformation("GetAllInstallations params: {fromDate}, {toDate}", fromDate, toDate); -- 2.25.1 From aa6c5a8721409e08636db53e20722c00b757e774 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Mon, 17 Feb 2025 15:36:22 +0400 Subject: [PATCH 12/14] fix tests with requirement, replace SetUp with TearDown --- .../ComponentBusinessLogicContractTests.cs | 4 ++-- .../EmployeeBusinessLogicContractTest.cs | 4 ++-- .../InstallationBusinessLogicContractTest.cs | 4 ++-- .../PostBusinessLogicContractTest.cs | 4 ++-- .../SalaryBusinessLogicContractTest.cs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs index 1c59a4f..5257099 100644 --- a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/ComponentBusinessLogicContractTests.cs @@ -21,8 +21,8 @@ internal class ComponentBusinessLogicContractTests _componentBusinessLogicContract = new ComponentBusinessLogicContract(_componentStorageContract.Object, new Mock().Object); } - [SetUp] - public void SetUp() + [TearDown] + public void TearDown() { _componentStorageContract.Reset(); } diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs index 962255e..90bb78b 100644 --- a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs @@ -20,8 +20,8 @@ internal class EmployeeBusinessLogicContractTest _employeeBusinessLogicContract = new EmployeeBusinessLogicContract(_employeeStorageContract.Object, new Mock().Object); } - [SetUp] - public void SetUp() + [TearDown] + public void TearDown() { _employeeStorageContract.Reset(); } diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs index d3cbe8a..4afbab1 100644 --- a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/InstallationBusinessLogicContractTest.cs @@ -19,8 +19,8 @@ internal class InstallationBusinessLogicContractTest _installationBusinessLogicContract = new InstallationBusinessLogicContract(_installationStorageContract.Object, new Mock().Object); } - [SetUp] - public void SetUp() + [TearDown] + public void TearDown() { _installationStorageContract.Reset(); } diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs index 715de47..4ea686e 100644 --- a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/PostBusinessLogicContractTest.cs @@ -21,8 +21,8 @@ internal class PostBusinessLogicContractTest _postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock().Object); } - [SetUp] - public void SetUp() + [TearDown] + public void TearDown() { _postStorageContract.Reset(); } diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs index d1a630e..aef4efe 100644 --- a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTest.cs @@ -29,8 +29,8 @@ internal class SalaryBusinessLogicContractTest _employeeStorageContract.Object, new Mock().Object); } - [SetUp] - public void SetUp() + [TearDown] + public void TearDown() { _salaryStorageContract.Reset(); _installationStorageContract.Reset(); -- 2.25.1 From b05f91020bd00d9a0cc23f913e7f469080ef52a8 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Mon, 17 Feb 2025 17:13:35 +0400 Subject: [PATCH 13/14] add getting employee by email in data --- .../Implementations/EmployeeBusinessLogicContract.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs index d6545da..d275d3c 100644 --- a/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs +++ b/TheCyclopsProject/TheCyclopsBusinessLogic/Implementations/EmployeeBusinessLogicContract.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using System.Text.Json; +using System.Text.RegularExpressions; using TheCyclopsContracts.BusinessLogicContracts; using TheCyclopsContracts.DataModels; using TheCyclopsContracts.Exceptions; @@ -64,6 +65,10 @@ internal class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeSt { return _employeeStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); } + if (Regex.IsMatch(data, @"^\S+@\S+\.\S+$")) + { + return _employeeStorageContract.GetElementByEmail(data) ?? throw new ElementNotFoundException(data); + } return _employeeStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data); } -- 2.25.1 From ac296c3b1747b14a2fa0c97278aa9d0e8b74c21a Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Mon, 17 Feb 2025 18:23:10 +0400 Subject: [PATCH 14/14] add tests for email case --- .../EmployeeBusinessLogicContractTest.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs index 90bb78b..8de5004 100644 --- a/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs +++ b/TheCyclopsProject/TheCyclopsTests/BusinessLogicsContractsTests/EmployeeBusinessLogicContractTest.cs @@ -352,6 +352,21 @@ internal class EmployeeBusinessLogicContractTest _employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); } + [Test] + public void GetEmployeeByData_GetByEmail_ReturnRecord_Test() + { + //Arrange + var email = "email@mail.com"; + var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "фио", email, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); + _employeeStorageContract.Setup(x => x.GetElementByEmail(email)).Returns(record); + //Act + var element = _employeeBusinessLogicContract.GetEmployeeByData(email); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Email, Is.EqualTo(email)); + _employeeStorageContract.Verify(x => x.GetElementByEmail(It.IsAny()), Times.Once); + } + [Test] public void GetEmployeeByData_EmptyData_ThrowException_Test() { @@ -380,6 +395,15 @@ internal class EmployeeBusinessLogicContractTest _employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); } + [Test] + public void GetEmployeeByData_GetByEmail_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData("email@mail.com"), Throws.TypeOf()); + _employeeStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _employeeStorageContract.Verify(x => x.GetElementByEmail(It.IsAny()), Times.Once); + } + [Test] public void GetEmployeeByData_StorageThrowError_ThrowException_Test() { -- 2.25.1