diff --git a/North_Bridge/North_Bridge_BusinessLogics/Implementations/PostBusinessLogicContract.cs b/North_Bridge/North_Bridge_BusinessLogics/Implementations/PostBusinessLogicContract.cs index e4bdc58..e1264f2 100644 --- a/North_Bridge/North_Bridge_BusinessLogics/Implementations/PostBusinessLogicContract.cs +++ b/North_Bridge/North_Bridge_BusinessLogics/Implementations/PostBusinessLogicContract.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; + +using Microsoft.Extensions.Logging; using North_Bridge_Contract.BusinessLogicsContracts; using North_Bridge_Contract.DataModels; using North_Bridge_Contract.Enums; @@ -6,7 +7,7 @@ using North_Bridge_Contract.StoragesContracts; namespace North_Bridge_BusinessLogics.Implementations; -public class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract +internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { private readonly ILogger _logger = logger; diff --git a/North_Bridge/North_Bridge_BusinessLogics/Implementations/ProductBusinessLogicContract.cs b/North_Bridge/North_Bridge_BusinessLogics/Implementations/ProductBusinessLogicContract.cs index 1f9566f..7d8860d 100644 --- a/North_Bridge/North_Bridge_BusinessLogics/Implementations/ProductBusinessLogicContract.cs +++ b/North_Bridge/North_Bridge_BusinessLogics/Implementations/ProductBusinessLogicContract.cs @@ -1,8 +1,8 @@ -using Microsoft.Extensions.Logging; -using North_Bridge_Contract.BusinessLogicsContracts; +using North_Bridge_Contract.BusinessLogicsContracts; using North_Bridge_Contract.DataModels; using North_Bridge_Contract.Enums; using North_Bridge_Contract.StoragesContracts; +using Microsoft.Extensions.Logging; namespace North_Bridge_BusinessLogics.Implementations; diff --git a/North_Bridge/North_Bridge_BusinessLogics/Implementations/SalaryBusinessLogicContract.cs b/North_Bridge/North_Bridge_BusinessLogics/Implementations/SalaryBusinessLogicContract.cs index 82decf5..a03e27b 100644 --- a/North_Bridge/North_Bridge_BusinessLogics/Implementations/SalaryBusinessLogicContract.cs +++ b/North_Bridge/North_Bridge_BusinessLogics/Implementations/SalaryBusinessLogicContract.cs @@ -1,11 +1,11 @@ -using Microsoft.Extensions.Logging; -using North_Bridge_Contract.BusinessLogicsContracts; +using North_Bridge_Contract.BusinessLogicsContracts; using North_Bridge_Contract.DataModels; using North_Bridge_Contract.StoragesContracts; +using Microsoft.Extensions.Logging; namespace North_Bridge_BusinessLogics.Implementations; -public class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, ISaleStorageContract saleStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract +internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, ISaleStorageContract saleStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { private readonly ILogger _logger = logger; diff --git a/North_Bridge/North_Bridge_BusinessLogics/North_Bridge_BusinessLogics.csproj b/North_Bridge/North_Bridge_BusinessLogics/North_Bridge_BusinessLogics.csproj index f8b1a4e..3db437a 100644 --- a/North_Bridge/North_Bridge_BusinessLogics/North_Bridge_BusinessLogics.csproj +++ b/North_Bridge/North_Bridge_BusinessLogics/North_Bridge_BusinessLogics.csproj @@ -6,9 +6,14 @@ enable - - - + + + + + + + + diff --git a/North_Bridge/North_Bridge_Contract/Exceptions/ElementExistsException.cs b/North_Bridge/North_Bridge_Contract/Exceptions/ElementExistsException.cs new file mode 100644 index 0000000..04cada5 --- /dev/null +++ b/North_Bridge/North_Bridge_Contract/Exceptions/ElementExistsException.cs @@ -0,0 +1,15 @@ + +namespace North_Bridge_Contract.Extentions; + +public class ElementExistsException : Exception +{ + public string ParamName { get; private set; } + + public string ParamValue { get; private set; } + + public ElementExistsException(string paramName, string paramValue) : base($"There is already an element with value{paramValue} of parameter { paramName}") + { + ParamName = paramName; + ParamValue = paramValue; + } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Contract/Exceptions/ElementNotFoundException.cs b/North_Bridge/North_Bridge_Contract/Exceptions/ElementNotFoundException.cs new file mode 100644 index 0000000..2893100 --- /dev/null +++ b/North_Bridge/North_Bridge_Contract/Exceptions/ElementNotFoundException.cs @@ -0,0 +1,12 @@ + +namespace North_Bridge_Contract.Exceptions; + +public class ElementNotFoundException : Exception +{ + public string Value { get; private set; } + + public ElementNotFoundException(string value) : base($"Element not found at value = { value}") + { + Value = value; + } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Contract/Exceptions/IncorrectDatesException.cs b/North_Bridge/North_Bridge_Contract/Exceptions/IncorrectDatesException.cs new file mode 100644 index 0000000..c5772b5 --- /dev/null +++ b/North_Bridge/North_Bridge_Contract/Exceptions/IncorrectDatesException.cs @@ -0,0 +1,7 @@ + +namespace North_Bridge_Contract.Exceptions; + +public class IncorrectDatesException : Exception +{ + public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date..StartDate: { start: dd.MM.YYYY}. EndDate: {end:dd.MM.YYYY}") { } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Contract/Exceptions/NullListException.cs b/North_Bridge/North_Bridge_Contract/Exceptions/NullListException.cs new file mode 100644 index 0000000..5ece098 --- /dev/null +++ b/North_Bridge/North_Bridge_Contract/Exceptions/NullListException.cs @@ -0,0 +1,6 @@ +namespace North_Bridge_Contract.Exceptions; + +public class NullListException : Exception +{ + public NullListException() : base("The returned list is null") { } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Contract/Exceptions/StorageException.cs b/North_Bridge/North_Bridge_Contract/Exceptions/StorageException.cs new file mode 100644 index 0000000..c363106 --- /dev/null +++ b/North_Bridge/North_Bridge_Contract/Exceptions/StorageException.cs @@ -0,0 +1,6 @@ +namespace North_Bridge_Contract.Exceptions; + +public class StorageException : Exception +{ + public StorageException(Exception ex) : base($"Error while working in storage: {ex.Message}", ex) { } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Contract/Extentions/DateTimeExtensions.cs b/North_Bridge/North_Bridge_Contract/Extentions/DateTimeExtensions.cs new file mode 100644 index 0000000..f455c43 --- /dev/null +++ b/North_Bridge/North_Bridge_Contract/Extentions/DateTimeExtensions.cs @@ -0,0 +1,10 @@ + +namespace North_Bridge_Contract.Extentions; + +public static class DateTimeExtensions +{ + public static bool IsDateNotOlder(this DateTime date, DateTime olderDate) + { + return date >= olderDate; + } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Contract/StoragesContracts/IProductStorageContract.cs b/North_Bridge/North_Bridge_Contract/StoragesContracts/IProductStorageContract.cs index bac7705..970468d 100644 --- a/North_Bridge/North_Bridge_Contract/StoragesContracts/IProductStorageContract.cs +++ b/North_Bridge/North_Bridge_Contract/StoragesContracts/IProductStorageContract.cs @@ -16,5 +16,5 @@ public interface IProductStorageContract void UpdElement(ProductDataModel productDataModel); - void DelElement(ProductDataModel productDataModel); + void DelElement(string id); } \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs new file mode 100644 index 0000000..cedf49a --- /dev/null +++ b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs @@ -0,0 +1,599 @@ +using Moq; +using North_Bridge_BusinessLogics.Implementations; +using North_Bridge_Contract.DataModels; +using North_Bridge_Contract.Enums; +using North_Bridge_Contract.Exceptions; +using North_Bridge_Contract.Extentions; +using North_Bridge_Contract.StoragesContracts; +using Microsoft.Extensions.Logging; + +namespace North_Bridge_Tests.BusinessLogicsContractsTests; + +[TestFixture] +internal class PostBusinessLogicContractTests +{ + private PostBusinessLogicContract _postBusinessLogicContract; + + private Mock _postStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _postStorageContract = new Mock(); + _postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _postStorageContract.Reset(); + } + + [Test] + public void GetAllPosts_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(),"name 1", PostType.Consultant, + 10, true, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 2", PostType.Consultant, + 10, false, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 3", PostType.Consultant, + 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.Consultant, 10, true, + DateTime.UtcNow), + new(postId, "name 2", PostType.Consultant, 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.Consultant, 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.Consultant, 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.Supervisor, 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.Supervisor, 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.Supervisor, 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.Supervisor, 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.Supervisor, 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.Supervisor, 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.Supervisor, 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.Supervisor, 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.Supervisor, 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/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs new file mode 100644 index 0000000..9759fb6 --- /dev/null +++ b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs @@ -0,0 +1,531 @@ +using Moq; +using North_Bridge_BusinessLogics.Implementations; +using North_Bridge_Contract.DataModels; +using North_Bridge_Contract.Enums; +using North_Bridge_Contract.Exceptions; +using North_Bridge_Contract.Extentions; +using North_Bridge_Contract.StoragesContracts; +using Microsoft.Extensions.Logging; + +namespace North_Bridge_Tests.BusinessLogicsContractsTests; + +[TestFixture] +internal class ProductBusinessLogicContractTests +{ + private ProductBusinessLogicContract _productBusinessLogicContract; + + private Mock _productStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _productStorageContract = new Mock(); + _productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _productStorageContract.Reset(); + } + + [Test] + public void GetAllProducts_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "name 1", + ProductType.SystemUnit , 10, false), + new(Guid.NewGuid().ToString(), "name 2", + ProductType.SystemUnit, 10, true), + new(Guid.NewGuid().ToString(), "name 3", + ProductType.SystemUnit, 10, false), + }; + _productStorageContract.Setup(x => x.GetList(It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = + _productBusinessLogicContract.GetAllProducts(true); + var list = _productBusinessLogicContract.GetAllProducts(false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal)); + Assert.That(list, Is.EquivalentTo(listOriginal)); + }); + _productStorageContract.Verify(x => x.GetList(true), + Times.Once); + _productStorageContract.Verify(x => x.GetList(false), + Times.Once); + } + + [Test] + public void GetAllProducts_ReturnEmptyList_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny())).Returns([]); + //Act + var listOnlyActive = + _productBusinessLogicContract.GetAllProducts(true); + var list = _productBusinessLogicContract.GetAllProducts(false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Has.Count.EqualTo(0)); + Assert.That(list, Has.Count.EqualTo(0)); + }); + _productStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Exactly(2)); + } + + [Test] + public void GetAllProducts_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProducts(It.IsAny()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Once); + } + + [Test] + public void GetAllProducts_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProducts(It.IsAny()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductHistoryByProduct_ReturnListOfRecords_Test() + { + //Arrange + var productId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), 10), + new(Guid.NewGuid().ToString(), 15), + new(Guid.NewGuid().ToString(), 10), + }; + _productStorageContract.Setup(x => + x.GetHistoryByProductId(It.IsAny())).Returns(listOriginal); + //Act + var list = + _productBusinessLogicContract.GetProductHistoryByProduct(productId); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(productId), Times.Once); + } + + [Test] + public void GetProductHistoryByProduct_ReturnEmptyList_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.GetHistoryByProductId(It.IsAny())).Returns([]); + //Act + var list = + _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString( + )); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + + [Test] + public void + GetProductHistoryByProduct_ProductIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(null), + Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(string.Empty), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Never); + } + + [Test] + public void GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct("productId"), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Never); + } + + [Test] + public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString( + )), Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + + [Test] + public void + GetProductHistoryByProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.GetHistoryByProductId(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString( + )), Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new ProductDataModel(id, "name", ProductType.SystemUnit, + 10, false); + _productStorageContract.Setup(x => + x.GetElementById(id)).Returns(record); + //Act + var element = _productBusinessLogicContract.GetProductByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _productStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_GetByName_ReturnRecord_Test() + { + //Arrange + var name = "name"; + var record = new ProductDataModel(Guid.NewGuid().ToString(), name, + ProductType.SystemUnit, 10, false); + _productStorageContract.Setup(x => + x.GetElementByName(name)).Returns(record); + //Act + var element = _productBusinessLogicContract.GetProductByData(name); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.ProductName, Is.EqualTo(name)); + _productStorageContract.Verify(x => + x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductByData(null), + Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.GetProductByData(string.Empty), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetElementByName(It.IsAny()), Times.Never); + _productStorageContract.Verify(x => + x.GetElementByName(It.IsAny()), Times.Never); + } + + [Test] + public void GetProductByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_GetByName_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductByData("name"), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void GetProductByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.GetElementById(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + _productStorageContract.Setup(x => + x.GetElementByName(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.GetProductByData("name"), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + _productStorageContract.Verify(x => + x.GetElementByName(It.IsAny()), Times.Once); + } + + [Test] + public void InsertProduct_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", + ProductType.SystemUnit, 10, false); + _productStorageContract.Setup(x => + x.AddElement(It.IsAny())) + .Callback((ProductDataModel x) => + { + flag = x.Id == record.Id && x.ProductName == + record.ProductName && x.ProductType == record.ProductType && + x.Price == record.Price && x.IsDeleted == record.IsDeleted; + }); + //Act + _productBusinessLogicContract.InsertProduct(record); + //Assert + _productStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertProduct_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new + ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), + "name", ProductType.SystemUnit, 10, false)), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertProduct_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.InsertProduct(null), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertProduct_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.InsertProduct(new + ProductDataModel("id", "name", ProductType.SystemUnit, + 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), + "name", ProductType.SystemUnit, 10, false)), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateProduct_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", + ProductType.SystemUnit, 10, false); + _productStorageContract.Setup(x => + x.UpdElement(It.IsAny())) + .Callback((ProductDataModel x) => + { + flag = x.Id == record.Id && x.ProductName == + record.ProductName && x.ProductType == record.ProductType && + x.Price == record.Price && x.IsDeleted == record.IsDeleted; + }); + //Act + _productBusinessLogicContract.UpdateProduct(record); + //Assert + _productStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new + ElementNotFoundException("")); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), + "name", ProductType.SystemUnit, 10, false)), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateProduct_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new + ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), + "anme", ProductType.SystemUnit, 10, false)), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateProduct_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.UpdateProduct(null), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateProduct_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.UpdateProduct(new + ProductDataModel("id", "name", ProductType.SystemUnit, + 10, false)), Throws.TypeOf()); + _productStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), + "name", ProductType.SystemUnit, 10, false)), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteProduct_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _productStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + //Act + _productBusinessLogicContract.DeleteProduct(id); + //Assert + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + Assert.That(flag); + } + + [Test] + public void DeleteProduct_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _productStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } + + [Test] + public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct(null), + Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.DeleteProduct(string.Empty), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + + [Test] + public void DeleteProduct_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct("id"), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + + [Test] + public void DeleteProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTests.cs b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTests.cs new file mode 100644 index 0000000..f72cb7d --- /dev/null +++ b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/SalaryBusinessLogicContractTests.cs @@ -0,0 +1,484 @@ +using Moq; +using North_Bridge_BusinessLogics.Implementations; +using North_Bridge_Contract.DataModels; +using North_Bridge_Contract.Enums; +using North_Bridge_Contract.Exceptions; +using North_Bridge_Contract.StoragesContracts; +using Microsoft.Extensions.Logging; + +namespace North_Bridge_Tests.BusinessLogicsContractsTests; + +[TestFixture] +internal class SalaryBusinessLogicContractTests +{ + private SalaryBusinessLogicContract _salaryBusinessLogicContract; + + private Mock _salaryStorageContract; + + private Mock _saleStorageContract; + + private Mock _postStorageContract; + + private Mock _workerStorageContract; + [OneTimeSetUp] + public void OneTimeSetUp() + { + _salaryStorageContract = new Mock(); + + _saleStorageContract = new Mock(); + + _postStorageContract = new Mock(); + + _workerStorageContract = new Mock(); + + _salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object, _saleStorageContract.Object, _postStorageContract.Object, _workerStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _salaryStorageContract.Reset(); + _saleStorageContract.Reset(); + _postStorageContract.Reset(); + _workerStorageContract.Reset(); + } + + [Test] + public void GetAllSalaries_ReturnListOfRecords_Test() + { + //Arrange + var startDate = DateTime.UtcNow; + var endDate = DateTime.UtcNow.AddDays(1); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10), + new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), + 14), + new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), + 30), + }; + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = + _salaryBusinessLogicContract.GetAllSalariesByPeriod(startDate, endDate); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _salaryStorageContract.Verify(x => x.GetList(startDate, endDate, + null), Times.Once); + } + + [Test] + public void GetAllSalaries_ReturnEmptyList_Test() + { + //Arrange + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = + _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalaries_IncorrectDates_ThrowException_Test() + { + //Arrange + var dateTime = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime), + Throws.TypeOf()); + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, + dateTime.AddSeconds(-1)), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalaries_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalaries_StorageThrowError_ThrowException_Test() + { + //Arrange + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalariesByWorker_ReturnListOfRecords_Test() + { + //Arrange + var startDate = DateTime.UtcNow; + var endDate = DateTime.UtcNow.AddDays(1); + var workerId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10), + new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), + 14), + new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), + 30), + }; + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(startDate, endDate, + workerId); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _salaryStorageContract.Verify(x => x.GetList(startDate, endDate, + workerId), Times.Once); + } + + [Test] + public void GetAllSalariesByWorker_ReturnEmptyList_Test() + { + //Arrange + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalariesByWorker_IncorrectDates_ThrowException_Test() + { + //Arrange + var dateTime = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime, + Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, + dateTime.AddSeconds(-1), Guid.NewGuid().ToString()), + Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void + GetAllSalariesByWorker_WorkerIdIsNUllOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), null), Throws.TypeOf()); + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), string.Empty), + Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalariesByWorker_WorkerIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), "workerId"), Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalariesByWorker_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), + Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalariesByWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _salaryStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), + Throws.TypeOf()); + _salaryStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void CalculateSalaryByMounth_CalculateSalary_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + var saleSum = 200.0; + var postSalary = 2000.0; + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new SaleDataModel(Guid.NewGuid().ToString(), + workerId, saleSum, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Consultant, postSalary, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test", + "example@example.com",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + var sum = 0.0; + var expectedSum = postSalary + saleSum * 0.1; + _salaryStorageContract.Setup(x => + x.AddElement(It.IsAny())) + .Callback((SalaryDataModel x) => + { + sum = x.Salary; + }); + //Act + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow); + //Assert + Assert.That(sum, Is.EqualTo(expectedSum)); + } + + [Test] + public void CalculateSalaryByMounth_WithSeveralWorkers_Test() + { + //Arrange + var worker1Id = Guid.NewGuid().ToString(); + var worker2Id = Guid.NewGuid().ToString(); + var worker3Id = Guid.NewGuid().ToString(); + var list = new List() { + new(worker1Id, "Test","example@example.com", Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow, false), + new(worker2Id, "Test","example@example.com", Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow, false), + new(worker3Id, "Test","example@example.com", Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow, false) + }; + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new SaleDataModel(Guid.NewGuid().ToString(), + worker1Id, 1, false, []), + new SaleDataModel(Guid.NewGuid().ToString(), worker1Id, + 1, false, []), + new SaleDataModel(Guid.NewGuid().ToString(), worker2Id, + 1, false, []), + new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, + 1, false, []), + new SaleDataModel(Guid.NewGuid().ToString(), worker3Id, + 1, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Consultant, 2000, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns(list); + //Act + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow); + //Assert + _salaryStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Exactly(list.Count)); + } + + [Test] + public void CalculateSalaryByMounth_WithoitSalesByWorker_Test() + { + //Arrange + var postSalary = 2000.0; + var workerId = Guid.NewGuid().ToString(); + _saleStorageContract.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.Consultant, postSalary, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test", + "example@example.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_SaleStorageReturnNull_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Consultant, 2000, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test", + "example@example.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 workerId = Guid.NewGuid().ToString(); + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new SaleDataModel(Guid.NewGuid().ToString(), + workerId, 200, false, [])]); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test", + "example@example.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), + Throws.TypeOf()); + } + + [Test] + public void + CalculateSalaryByMounth_WorkerStorageReturnNull_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new SaleDataModel(Guid.NewGuid().ToString(), + workerId, 200, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Consultant, 2000, true, DateTime.UtcNow)); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), + Throws.TypeOf()); + } + + [Test] + public void + CalculateSalaryByMounth_SaleStorageThrowException_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _saleStorageContract.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.Consultant, 2000, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test", + "example@example.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 workerId = Guid.NewGuid().ToString(); + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new SaleDataModel(Guid.NewGuid().ToString(), + workerId, 200, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Throws(new StorageException(new + InvalidOperationException())); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Returns([new WorkerDataModel(workerId, "Test", + "example@example.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), + Throws.TypeOf()); + } + + [Test] + public void + CalculateSalaryByMounth_WorkerStorageThrowException_ThrowException_Test() + { + //Arrange + var workerId = Guid.NewGuid().ToString(); + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Returns([new SaleDataModel(Guid.NewGuid().ToString(), + workerId, 200, false, [])]); + _postStorageContract.Setup(x => x.GetElementById(It.IsAny())) + .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", + PostType.Consultant, 2000, true, DateTime.UtcNow)); + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), + Throws.TypeOf()); + } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/SaleBusinessLogicContractTests.cs b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/SaleBusinessLogicContractTests.cs new file mode 100644 index 0000000..feaf6fb --- /dev/null +++ b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/SaleBusinessLogicContractTests.cs @@ -0,0 +1,583 @@ +using Moq; +using North_Bridge_BusinessLogics.Implementations; +using North_Bridge_Contract.DataModels; +using North_Bridge_Contract.Exceptions; +using North_Bridge_Contract.Extentions; +using North_Bridge_Contract.StoragesContracts; +using Microsoft.Extensions.Logging; + +namespace North_Bridge_Tests.BusinessLogicsContractsTests; + +[TestFixture] +internal class SaleBusinessLogicContractTests +{ + private SaleBusinessLogicContract _saleBusinessLogicContract; + + private Mock _saleStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _saleStorageContract = new Mock(); + _saleBusinessLogicContract = new SaleBusinessLogicContract(_saleStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _saleStorageContract.Reset(); + } + + [Test] + public void GetAllSalesByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, + [new ProductForSaleDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, []), + }; + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = _saleBusinessLogicContract.GetAllSalesByPeriod(date, + date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), + null, null), Times.Once); + } + + [Test] + public void GetAllSalesByPeriod_ReturnEmptyList_Test() + { + //Arrange + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = + _saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalesByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByPeriod(date, date), + Throws.TypeOf()); + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByPeriod(date, date.AddSeconds(-1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalesByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalesByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByPeriod(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalesByWorkerByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var workerId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, + [new ProductForSaleDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, []), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, []), + }; + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(workerId, date, + date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), + workerId, null), Times.Once); + } + + [Test] + public void GetAllSalesByWorkerByPeriod_ReturnEmptyList_Test() + { + //Arrange + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void + GetAllSalesByWorkerByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), + date, date), Throws.TypeOf()); + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), + date, date.AddSeconds(-1)), Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void + GetAllSalesByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(null, DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(string.Empty, + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalesByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod("workerId", + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalesByWorkerByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void + GetAllSalesByWorkerByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())) + .Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByWorkerByPeriod(Guid.NewGuid().ToString(), + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllSalesByProductByPeriod_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var productId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, + [new ProductForSaleDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]), + new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, []),new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), + 10, false, []), + }; + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var list = + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(productId, date, + date.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), + null, productId), Times.Once); + } + + [Test] + public void GetAllSalesByProductByPeriod_ReturnEmptyList_Test() + { + //Arrange + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); + //Act + var list = + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString() + , DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void + GetAllSalesByProductByPeriod_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString() + , date, date), Throws.TypeOf()); + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString() + , date, date.AddSeconds(-1)), Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void + GetAllSalesByProductByPeriod_ProductIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(null, DateTime.UtcNow, + DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(string.Empty, + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void + GetAllSalesByProductByPeriod_ProductIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByProductByPeriod("productId", + DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllSalesByProductByPeriod_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString() + , DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void + GetAllSalesByProductByPeriod_StorageThrowError_ThrowException_Test() + { + //Arrange + _saleStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetAllSalesByProductByPeriod(Guid.NewGuid().ToString() + , DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetSaleByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new SaleDataModel(id, Guid.NewGuid().ToString(), + 10, false, + [new ProductForSaleDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]); + _saleStorageContract.Setup(x => + x.GetElementById(id)).Returns(record); + //Act + var element = _saleBusinessLogicContract.GetSaleByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _saleStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetSaleByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _saleBusinessLogicContract.GetSaleByData(null), + Throws.TypeOf()); + Assert.That(() => + _saleBusinessLogicContract.GetSaleByData(string.Empty), + Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Never); + } + + [Test] + public void GetSaleByData_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _saleBusinessLogicContract.GetSaleByData("saleId"), + Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Never); + } + + [Test] + public void GetSaleByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetSaleByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetSaleByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _saleStorageContract.Setup(x => + x.GetElementById(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.GetSaleByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void InsertSale_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new SaleDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, + false, [new ProductForSaleDataModel(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 5)]); + _saleStorageContract.Setup(x => + x.AddElement(It.IsAny())) + .Callback((SaleDataModel x) => + { + flag = x.Id == record.Id && x.WorkerId == record.WorkerId && + x.SaleDate == record.SaleDate && x.Sum == record.Sum && + x.IsCancel == record.IsCancel && x.Products.Count == + record.Products.Count && + x.Products.First().ProductId == + record.Products.First().ProductId && + x.Products.First().SaleId == + record.Products.First().SaleId && + x.Products.First().Count == + record.Products.First().Count; + }); + //Act + _saleBusinessLogicContract.InsertSale(record); + //Assert + _saleStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertSale_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _saleStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new + ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, false, + [new ProductForSaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),5)])), + Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertSale_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _saleBusinessLogicContract.InsertSale(null), + Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertSale_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _saleBusinessLogicContract.InsertSale(new + SaleDataModel("id", Guid.NewGuid().ToString(), 10, + false, [])), Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertSale_StorageThrowError_ThrowException_Test() + { + //Arrange + _saleStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), + Guid.NewGuid().ToString(), 10, false, + [new ProductForSaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),5)])), + Throws.TypeOf()); + _saleStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void CancelSale_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _saleStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == + id))).Callback(() => { flag = true; }); + //Act + _saleBusinessLogicContract.CancelSale(id); + //Assert + _saleStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + Assert.That(flag); + } + + [Test] + public void CancelSale_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _saleStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } + + [Test] + public void CancelSale_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _saleBusinessLogicContract.CancelSale(null), + Throws.TypeOf()); + Assert.That(() => + _saleBusinessLogicContract.CancelSale(string.Empty), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + + [Test] + public void CancelSale_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _saleBusinessLogicContract.CancelSale("id"), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + + [Test] + public void CancelSale_StorageThrowError_ThrowException_Test() + { + //Arrange + _saleStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _saleBusinessLogicContract.CancelSale(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _saleStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs new file mode 100644 index 0000000..f6b9cd0 --- /dev/null +++ b/North_Bridge/North_Bridge_Tests/BusinessLogicsContractsTests/WorkerBusinessLogicContractTests.cs @@ -0,0 +1,804 @@ +using Moq; +using North_Bridge_BusinessLogics.Implementations; +using North_Bridge_Contract.DataModels; +using North_Bridge_Contract.Exceptions; +using North_Bridge_Contract.Extentions; +using North_Bridge_Contract.StoragesContracts; +using Microsoft.Extensions.Logging; + +namespace North_Bridge_Tests.BusinessLogicsContractsTests; + +[TestFixture] +internal class WorkerBusinessLogicContractTests +{ + private WorkerBusinessLogicContract _workerBusinessLogicContract; + + private Mock _workerStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _workerStorageContract = new Mock(); + + _workerBusinessLogicContract = new WorkerBusinessLogicContract(_workerStorageContract.Object, new Mock().Object); + } + + [SetUp] + public void SetUp() + { + _workerStorageContract.Reset(); + } + + [Test] + public void GetAllWorkers_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + }; + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkers(true); + var list = _workerBusinessLogicContract.GetAllWorkers(false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal)); + Assert.That(list, Is.EquivalentTo(listOriginal)); + }); + _workerStorageContract.Verify(x => x.GetList(true, null, null, null, + null, null), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, null, null, null, + null, null), Times.Once); + } + + [Test] + public void GetAllWorkers_ReturnEmptyList_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkers(true); + var list = _workerBusinessLogicContract.GetAllWorkers(false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Has.Count.EqualTo(0)); + Assert.That(list, Has.Count.EqualTo(0)); + }); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), null, + null, null, null, null), Times.Exactly(2)); + } + + [Test] + public void GetAllWorkers_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkers(It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkers_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkers(It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), null, + null, null, null, null), Times.Once); + } + + [Test] + public void GetAllWorkersByPost_ReturnListOfRecords_Test() + { + //Arrange + var postId = Guid.NewGuid().ToString(); + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + }; + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkersByPost(postId, true); + var list = _workerBusinessLogicContract.GetAllWorkersByPost(postId, + false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal)); + Assert.That(list, Is.EquivalentTo(listOriginal)); + }); + _workerStorageContract.Verify(x => x.GetList(true, postId, null, + null, null, null), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, postId, null, + null, null, null), Times.Once); + } + + [Test] + public void GetAllWorkersByPost_ReturnEmptyList_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), + true); + var list = + _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), + false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Has.Count.EqualTo(0)); + Assert.That(list, Has.Count.EqualTo(0)); + }); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Exactly(2)); + } + + [Test] + public void GetAllWorkersByPost_PostIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByPost(null, It.IsAny()), + Throws.TypeOf()); + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByPost(string.Empty, It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByPost_PostIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByPost("postId", It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByPost_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), + It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByPost_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), + It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + }; + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), + true); + var list = + _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), + false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal)); + Assert.That(list, Is.EquivalentTo(listOriginal)); + }); + _workerStorageContract.Verify(x => x.GetList(true, null, date, + date.AddDays(1), null, null), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, null, date, + date.AddDays(1), null, null), Times.Once); + } + + [Test] + public void GetAllWorkersByBirthDate_ReturnEmptyList_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), true); + var list = _workerBusinessLogicContract.GetAllWorkers(false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Has.Count.EqualTo(0)); + Assert.That(list, Has.Count.EqualTo(0)); + }); + _workerStorageContract.Verify(x => x.GetList(true, null, + It.IsAny(), It.IsAny(), null, null), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, null, + It.IsAny(), It.IsAny(), null, null), Times.Once); + } + + [Test] + public void GetAllWorkersByBirthDate_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date, + It.IsAny()), Throws.TypeOf()); + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddSeconds(-1), + It.IsAny()), Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void + GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test() + { + //Arrange + var date = DateTime.UtcNow; + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + new(Guid.NewGuid().ToString(), "fio 2", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + true), + new(Guid.NewGuid().ToString(), "fio 3", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false), + }; + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), + true); + var list = + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), + false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal)); + Assert.That(list, Is.EquivalentTo(listOriginal)); + }); + _workerStorageContract.Verify(x => x.GetList(true, null, null, null, + date, date.AddDays(1)), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, null, null, null, + date, date.AddDays(1)), Times.Once); + } + + [Test] + public void GetAllWorkersByEmploymentDate_ReturnEmptyList_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Returns([]); + //Act + var listOnlyActive = + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), true); + var list = + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Has.Count.EqualTo(0)); + Assert.That(list, Has.Count.EqualTo(0)); + }); + _workerStorageContract.Verify(x => x.GetList(true, null, null, null, + It.IsAny(), It.IsAny()), Times.Once); + _workerStorageContract.Verify(x => x.GetList(false, null, null, null, + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void + GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test() + { + //Arrange + var date = DateTime.UtcNow; + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date, + It.IsAny()), Throws.TypeOf()); + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, + date.AddSeconds(-1), It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void + GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, + DateTime.UtcNow.AddDays(1), It.IsAny()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void GetWorkerByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new WorkerDataModel(id, "fio", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false); + _workerStorageContract.Setup(x => + x.GetElementById(id)).Returns(record); + //Act + var element = _workerBusinessLogicContract.GetWorkerByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _workerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + + [Test] + public void GetWorkerByData_GetByFio_ReturnRecord_Test() + { + //Arrange + var fio = "fio"; + var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio, "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false); + _workerStorageContract.Setup(x => + x.GetElementByFIO(fio)).Returns(record); + //Act + var element = _workerBusinessLogicContract.GetWorkerByData(fio); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.FIO, Is.EqualTo(fio)); + _workerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Once); + } + + [Test] + public void GetWorkerByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(null), + Throws.TypeOf()); + Assert.That(() => + _workerBusinessLogicContract.GetWorkerByData(string.Empty), + Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Never); + _workerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Never); + } + + [Test] + public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + _workerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Never); + } + + [Test] + public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetWorkerByData("fio"), + Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Never); + _workerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Once); + } + + [Test] + public void GetWorkerByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => + x.GetElementById(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + _workerStorageContract.Setup(x => + x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + Assert.That(() => + _workerBusinessLogicContract.GetWorkerByData("fio"), + Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + _workerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Once); + } + + [Test] + public void InsertWorker_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false); + _workerStorageContract.Setup(x => + x.AddElement(It.IsAny())) + .Callback((WorkerDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && x.Email == record.Email && + x.PostId == record.PostId && x.BirthDate == record.BirthDate && + x.EmploymentDate == record.EmploymentDate && + x.IsDeleted == record.IsDeleted; + }); + //Act + _workerBusinessLogicContract.InsertWorker(record); + //Assert + _workerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void InsertWorker_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new + ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void InsertWorker_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.InsertWorker(null), + Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertWorker_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.InsertWorker(new + WorkerDataModel("id", "fio", "example@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(- + 16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + + [Test] + public void InsertWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateWorker_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false); + _workerStorageContract.Setup(x => + x.UpdElement(It.IsAny())) + .Callback((WorkerDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && x.Email == record.Email && + x.PostId == record.PostId && x.BirthDate == record.BirthDate && + x.EmploymentDate == record.EmploymentDate && + x.IsDeleted == record.IsDeleted; + }); + //Act + _workerBusinessLogicContract.UpdateWorker(record); + //Assert + _workerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + + [Test] + public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new + ElementNotFoundException("")); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void UpdateWorker_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.UpdateWorker(null), + Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateWorker_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new + WorkerDataModel("id", "fio", "example@example.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears( + -16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com", + Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, + false)), Throws.TypeOf()); + _workerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + + [Test] + public void DeleteWorker_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x + == id))).Callback(() => { flag = true; }); + //Act + _workerBusinessLogicContract.DeleteWorker(id); + //Assert + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + Assert.That(flag); + } + + [Test] + public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x + != id))).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } + + [Test] + public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null), + Throws.TypeOf()); + Assert.That(() => + _workerBusinessLogicContract.DeleteWorker(string.Empty), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + + [Test] + public void DeleteWorker_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + + [Test] + public void DeleteWorker_StorageThrowError_ThrowException_Test() + { + //Arrange + _workerStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } +} \ No newline at end of file diff --git a/North_Bridge/North_Bridge_Tests/North_Bridge_Tests.csproj b/North_Bridge/North_Bridge_Tests/North_Bridge_Tests.csproj index 6168504..363321f 100644 --- a/North_Bridge/North_Bridge_Tests/North_Bridge_Tests.csproj +++ b/North_Bridge/North_Bridge_Tests/North_Bridge_Tests.csproj @@ -12,12 +12,14 @@ + +