From eb995c94b8db0b9a8ac5cd485913c8d25b885642 Mon Sep 17 00:00:00 2001 From: Ivan Gutorov Date: Sun, 16 Feb 2025 21:09:05 +0400 Subject: [PATCH] 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 @@ + +