diff --git a/PuferFishContracts/PuferFishBusinessLogic/Implementations/BuyerBusinessLogicContract.cs b/PuferFishContracts/PuferFishBusinessLogic/Implementations/BuyerBusinessLogicContract.cs index aa3766e..c506a98 100644 --- a/PuferFishContracts/PuferFishBusinessLogic/Implementations/BuyerBusinessLogicContract.cs +++ b/PuferFishContracts/PuferFishBusinessLogic/Implementations/BuyerBusinessLogicContract.cs @@ -2,12 +2,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using PuferFishContracts.BusinessLogicsContracts; using PuferFishContracts.DataModels; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Extensions; using PuferFishContracts.StoragesContracts; -namespace PuferFishBusinessLogic.Implementationsж +namespace PuferFishBusinessLogic.Implementations { internal class BuyerBusinessLogicContract(IBuyerStorageContract buyerStorageContract, ILogger logger) : IBuyerBusinessLogicContract { @@ -16,20 +21,57 @@ namespace PuferFishBusinessLogic.Implementationsж buyerStorageContract; public List GetAllBuyers() { - return []; + _logger.LogInformation("GetAllBuyers"); + return _buyerStorageContract.GetList() ?? throw new NullListException(); } public BuyerDataModel GetBuyerByData(string data) { - return new("", "", "", 0); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _buyerStorageContract.GetElementById(data) ?? throw new + ElementNotFoundException(data); + } + if (Regex.IsMatch(data, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\-]{7,10}$")) + { + return _buyerStorageContract.GetElementByPhoneNumber(data) ?? + throw new ElementNotFoundException(data); + } + return _buyerStorageContract.GetElementByFIO(data) ?? throw new + ElementNotFoundException(data); } public void InsertBuyer(BuyerDataModel buyerDataModel) { + _logger.LogInformation("New data: {json}", + JsonSerializer.Serialize(buyerDataModel)); + ArgumentNullException.ThrowIfNull(buyerDataModel); + buyerDataModel.Validate(); + _buyerStorageContract.AddElement(buyerDataModel); } public void UpdateBuyer(BuyerDataModel buyerDataModel) { + _logger.LogInformation("Update data: {json}", + JsonSerializer.Serialize(buyerDataModel)); + ArgumentNullException.ThrowIfNull(buyerDataModel); + buyerDataModel.Validate(); + _buyerStorageContract.UpdElement(buyerDataModel); } public void DeleteBuyer(string id) { + _logger.LogInformation("Delete by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + _buyerStorageContract.DelElement(id); } } } diff --git a/PuferFishContracts/PuferFishBusinessLogic/Implementations/PointsBusinessLogicContract.cs b/PuferFishContracts/PuferFishBusinessLogic/Implementations/PointsBusinessLogicContract.cs index 0035c19..b8b9386 100644 --- a/PuferFishContracts/PuferFishBusinessLogic/Implementations/PointsBusinessLogicContract.cs +++ b/PuferFishContracts/PuferFishBusinessLogic/Implementations/PointsBusinessLogicContract.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using PuferFishContracts.BusinessLogicsContracts; using PuferFishContracts.DataModels; using PuferFishContracts.StoragesContracts; diff --git a/PuferFishContracts/PuferFishBusinessLogic/Implementations/PostBusinessLogicContract.cs b/PuferFishContracts/PuferFishBusinessLogic/Implementations/PostBusinessLogicContract.cs index 32beec4..7afc413 100644 --- a/PuferFishContracts/PuferFishBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/PuferFishContracts/PuferFishBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using PuferFishContracts.BusinessLogicsContracts; using PuferFishContracts.DataModels; using PuferFishContracts.Enums; diff --git a/PuferFishContracts/PuferFishBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/PuferFishContracts/PuferFishBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 1c12f6a..51529d2 100644 --- a/PuferFishContracts/PuferFishBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/PuferFishContracts/PuferFishBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using PuferFishContracts.BusinessLogicsContracts; using PuferFishContracts.DataModels; using PuferFishContracts.Enums; diff --git a/PuferFishContracts/PuferFishBusinessLogic/Implementations/SaleBusinessLogicContract.cs b/PuferFishContracts/PuferFishBusinessLogic/Implementations/SaleBusinessLogicContract.cs index 96508f6..989c67a 100644 --- a/PuferFishContracts/PuferFishBusinessLogic/Implementations/SaleBusinessLogicContract.cs +++ b/PuferFishContracts/PuferFishBusinessLogic/Implementations/SaleBusinessLogicContract.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using PuferFishContracts.BusinessLogicsContracts; using PuferFishContracts.DataModels; using PuferFishContracts.StoragesContracts; diff --git a/PuferFishContracts/PuferFishBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/PuferFishContracts/PuferFishBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index 27617da..e711a47 100644 --- a/PuferFishContracts/PuferFishBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/PuferFishContracts/PuferFishBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using PuferFishContracts.BusinessLogicsContracts; using PuferFishContracts.DataModels; using PuferFishContracts.StoragesContracts; diff --git a/PuferFishContracts/PuferFishBusinessLogic/PuferFishBusinessLogic.csproj b/PuferFishContracts/PuferFishBusinessLogic/PuferFishBusinessLogic.csproj index 0adc9e6..1a3e3f2 100644 --- a/PuferFishContracts/PuferFishBusinessLogic/PuferFishBusinessLogic.csproj +++ b/PuferFishContracts/PuferFishBusinessLogic/PuferFishBusinessLogic.csproj @@ -6,6 +6,15 @@ enable + + + + + + + + + diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/DateTimeExtensions.cs b/PuferFishContracts/PuferFishContracts/Exceptions/DateTimeExtensions.cs new file mode 100644 index 0000000..2c029b6 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/DateTimeExtensions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Exceptions; + +public static class DateTimeExtensions +{ + public static bool IsDateNotOlder(this DateTime date, DateTime olderDate) + { + return date >= olderDate; + } +} diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/ElementExistsException.cs b/PuferFishContracts/PuferFishContracts/Exceptions/ElementExistsException.cs new file mode 100644 index 0000000..5ebf415 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/ElementExistsException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Exceptions; + +public class ElementExistsException : Exception +{ + public string ParamName { get; private set; } + public string ParamValue { get; private set; } + public ElementExistsException(string paramName, string paramValue) : base($"There is already an element with value{paramValue} of parameter {paramName}") + { + ParamName = paramName; + ParamValue = paramValue; + } +} diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/ElementNotFoundException.cs b/PuferFishContracts/PuferFishContracts/Exceptions/ElementNotFoundException.cs new file mode 100644 index 0000000..709b56c --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/ElementNotFoundException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Exceptions; + +public class ElementNotFoundException : Exception +{ + public string Value { get; private set; } + public ElementNotFoundException(string value) : base($"Element not found at value = {value}") + { + Value = value; + } +} diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/IncorrectDatesException.cs b/PuferFishContracts/PuferFishContracts/Exceptions/IncorrectDatesException.cs new file mode 100644 index 0000000..362897a --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/IncorrectDatesException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace PuferFishContracts.Exceptions; + +public class IncorrectDatesException : Exception +{ + public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date..StartDate: { start: dd.MM.YYYY}.EndDate: { end:dd.MM.YYYY}") { } +} diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/NullListException.cs b/PuferFishContracts/PuferFishContracts/Exceptions/NullListException.cs new file mode 100644 index 0000000..577d2e7 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/NullListException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Exceptions; + +public class NullListException : Exception +{ + public NullListException() : base("The returned list is null") { } +} diff --git a/PuferFishContracts/PuferFishContracts/Exceptions/StorageException.cs b/PuferFishContracts/PuferFishContracts/Exceptions/StorageException.cs new file mode 100644 index 0000000..a3a7411 --- /dev/null +++ b/PuferFishContracts/PuferFishContracts/Exceptions/StorageException.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PuferFishContracts.Exceptions; + +public class StorageException : Exception +{ + public StorageException(Exception ex) : base($"Error while working in storage: { ex.Message}", ex) { } +} diff --git a/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/BuyerBusinessLogicContractTests.cs b/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/BuyerBusinessLogicContractTests.cs new file mode 100644 index 0000000..7d69db0 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/BuyerBusinessLogicContractTests.cs @@ -0,0 +1,408 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PuferFishContracts.BusinessLogicsContracts; +using PuferFishContracts.DataModels; +using PuferFishContracts.StoragesContracts; +using Moq; +using PuferFishBusinessLogic.Implementations; +using PuferFishContracts.Exceptions; +using PuferFishContracts.Extensions; +using PuferFishBusinessLogic.Implementations; +using Microsoft.Extensions.Logging; + +namespace PuferFishTests.BusinessLogicsContractsTests; + +[TestFixture] +internal class BuyerBusinessLogicContractTests +{ + private BuyerBusinessLogicContract _buyerBusinessLogicContract; + private Mock _buyerStorageContract; + [OneTimeSetUp] + public void OneTimeSetUp() + { + _buyerStorageContract = new Mock(); + _buyerBusinessLogicContract = new BuyerBusinessLogicContract(_buyerStorageContract.Object, new Mock().Object); + } + [SetUp] + public void SetUp() + { + _buyerStorageContract.Reset(); + } + [Test] + public void GetAllBuyers_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() +{ +new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", 0), +new(Guid.NewGuid().ToString(), "fio 2", "+7-555-444-33-23", 10), +new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777", 0),}; + _buyerStorageContract.Setup(x => x.GetList()).Returns(listOriginal); + //Act + var list = _buyerBusinessLogicContract.GetAllBuyers(); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + } + [Test] + public void GetAllBuyers_ReturnEmptyList_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.GetList()).Returns([]); + //Act + var list = _buyerBusinessLogicContract.GetAllBuyers(); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _buyerStorageContract.Verify(x => x.GetList(), Times.Once); + } + [Test] + public void GetAllBuyers_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetList(), Times.Once); + } + [Test] + public void GetAllBuyers_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.GetList()).Throws(new + StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetList(), Times.Once); + } + [Test] + public void GetBuyerByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new BuyerDataModel(id, "fio", "+7-111-111-11-11", 0); + _buyerStorageContract.Setup(x => + x.GetElementById(id)).Returns(record); + //Act + var element = _buyerBusinessLogicContract.GetBuyerByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _buyerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + } + [Test] + public void GetBuyerByData_GetByFio_ReturnRecord_Test() + { + //Arrange + var fio = "fio"; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), fio, "+7 - 111 - 111 - 11 - 11", 0); + _buyerStorageContract.Setup(x => + x.GetElementByFIO(fio)).Returns(record); + //Act + var element = _buyerBusinessLogicContract.GetBuyerByData(fio); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.FIO, Is.EqualTo(fio)); + _buyerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Once); + } + [Test] + public void GetBuyerByData_GetByPhoneNumber_ReturnRecord_Test() + { + //Arrange + var phoneNumber = "+7-111-111-11-11"; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio", + phoneNumber, 0); + _buyerStorageContract.Setup(x => + x.GetElementByPhoneNumber(phoneNumber)).Returns(record); + //Act + var element = +_buyerBusinessLogicContract.GetBuyerByData(phoneNumber); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber)); + _buyerStorageContract.Verify(x => + x.GetElementByPhoneNumber(It.IsAny()), Times.Once); + } + [Test] + public void GetBuyerByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(null), + Throws.TypeOf()); + Assert.That(() => + _buyerBusinessLogicContract.GetBuyerByData(string.Empty), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => + x.GetElementByPhoneNumber(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Never); + } + [Test] + public void GetBuyerByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => + x.GetElementByPhoneNumber(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Never); + } + [Test] + public void GetBuyerByData_GetByFio_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("fio"), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => + x.GetElementByPhoneNumber(It.IsAny()), Times.Never); + } + [Test] + public void + GetBuyerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); + } + [Test] + public void GetBuyerByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => + x.GetElementById(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + _buyerStorageContract.Setup(x => + x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + _buyerStorageContract.Setup(x => + x.GetElementByPhoneNumber(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), + Throws.TypeOf()); + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("Фамилия Имя Отчество"), + Throws.TypeOf()); + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.GetElementById(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => + x.GetElementByFIO(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => + x.GetElementByPhoneNumber(It.IsAny()), Times.Once); + } + [Test] + public void InsertBuyer_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 10); + _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())).Callback((BuyerDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && x.PhoneNumber == record.PhoneNumber && x.Points == record.Points; + }); + //Act + _buyerBusinessLogicContract.InsertBuyer(record); + //Assert + _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void InsertBuyer_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + [Test] + public void InsertBuyer_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(null), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + [Test] + public void InsertBuyer_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new + BuyerDataModel("id", "fio", "+7-111-111-11-11", 10)), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Never); + } + [Test] + public void InsertBuyer_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + } + [Test] + public void UpdateBuyer_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0); + _buyerStorageContract.Setup(x => + x.UpdElement(It.IsAny())) + .Callback((BuyerDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && x.PhoneNumber == record.PhoneNumber && x.Points == record.Points; + }); + //Act + _buyerBusinessLogicContract.UpdateBuyer(record); + //Assert + _buyerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void UpdateBuyer_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new + ElementNotFoundException("")); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + [Test] + public void UpdateBuyer_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + [Test] + public void UpdateBuyer_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(null), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Never); + } + [Test] + public void UpdateBuyer_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new + BuyerDataModel("id", "fio", "+7-111-111-11-11", 10)), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Never); + } + [Test] + public void UpdateBuyer_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Фамилия Имя Отчество", "+7-111-111-11-11", 0)), Throws.TypeOf()); + _buyerStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + [Test] + public void DeleteBuyer_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _buyerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x + == id))).Callback(() => { flag = true; }); + //Act + _buyerBusinessLogicContract.DeleteBuyer(id); + //Assert + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + Assert.That(flag); + } + [Test] + public void DeleteBuyer_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } + [Test] + public void DeleteBuyer_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(null), + Throws.TypeOf()); + Assert.That(() => + _buyerBusinessLogicContract.DeleteBuyer(string.Empty), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + [Test] + public void DeleteBuyer_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer("id"), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + [Test] + public void DeleteBuyer_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } +} diff --git a/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs b/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs new file mode 100644 index 0000000..2296671 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/PostBusinessLogicContractTests.cs @@ -0,0 +1,546 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Moq; +using PuferFishBusinessLogic.Implementations; +using PuferFishContracts.DataModels; +using PuferFishContracts.Enums; +using PuferFishContracts.Exceptions; +using PuferFishContracts.StoragesContracts; + +namespace PuferFishTests.BusinessLogicsContractsTests +{ + [TestFixture] + public 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.chef, true, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 2", PostType.chef, false, DateTime.UtcNow), + new(Guid.NewGuid().ToString(), "name 3", PostType.chef, 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.chef, true, DateTime.UtcNow), + new(postId, "name 2", PostType.chef, 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.chef, 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.chef, 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.chef, true, DateTime.UtcNow.AddDays(-1)); + _postStorageContract.Setup(x => + x.AddElement(It.IsAny())) + .Callback((PostDataModel x) => + { + flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ChangeDate == record.ChangeDate; + }); + //Act + _postBusinessLogicContract.InsertPost(record); + //Assert + _postStorageContract.Verify(x => + x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void InsertPost_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => + x.AddElement(It.IsAny())).Throws(new + ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => + _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.chef, 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.chef, 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.chef, 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.chef, true, DateTime.UtcNow.AddDays(-1)); + _postStorageContract.Setup(x => + x.UpdElement(It.IsAny())) + .Callback((PostDataModel x) => + { + flag = x.Id == record.Id && x.PostName == + record.PostName && x.PostType == record.PostType && x.ChangeDate == record.ChangeDate; + }); + //Act + _postBusinessLogicContract.UpdatePost(record); + //Assert + _postStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } + [Test] + public void UpdatePost_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _postStorageContract.Setup(x => + x.UpdElement(It.IsAny())).Throws(new + ElementNotFoundException("")); + //Act&Assert + Assert.That(() => + _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.chef, 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(), "name", PostType.chef, 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.chef, 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.chef, 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/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs b/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs new file mode 100644 index 0000000..5ca5624 --- /dev/null +++ b/PuferFishContracts/PuferFishTests/BusinessLogicsContractsTests/ProductBusinessLogicContractTests.cs @@ -0,0 +1,598 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Moq; +using PuferFishBusinessLogic.Implementations; +using PuferFishContracts.DataModels; +using PuferFishContracts.Enums; +using PuferFishContracts.Exceptions; +using PuferFishContracts.StoragesContracts; + +namespace PuferFishTests.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.Onigiri, 10, false), +new(Guid.NewGuid().ToString(), "name 2", ProductType.Onigiri, 10, true), +new(Guid.NewGuid().ToString(), "name 3", ProductType.Onigiri, 10, false), +}; + _productStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = + _productBusinessLogicContract.GetAllProducts(true); + var list = _productBusinessLogicContract.GetAllProducts(false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal)); + Assert.That(list, Is.EquivalentTo(listOriginal)); + }); + _productStorageContract.Verify(x => x.GetList(true, null), + Times.Once); + _productStorageContract.Verify(x => x.GetList(false, null), + Times.Once); + } + [Test] + public void GetAllProducts_ReturnEmptyList_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny())).Returns([]); + //Act + var listOnlyActive = + _productBusinessLogicContract.GetAllProducts(true); + var list = _productBusinessLogicContract.GetAllProducts(false); + //Assert + Assert.Multiple(() => + { + Assert.That(listOnlyActive, Is.Not.Null); + Assert.That(list, Is.Not.Null); + Assert.That(listOnlyActive, Has.Count.EqualTo(0)); + Assert.That(list, Has.Count.EqualTo(0)); + }); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), + null), Times.Exactly(2)); + } + [Test] + public void GetAllProducts_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProducts(It.IsAny()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), +It.IsAny()), Times.Once); + } + [Test] + public void GetAllProducts_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProducts(It.IsAny()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetAllProductsByManufacturer_ReturnListOfRecords_Test() + { + //Arrange + var manufacturerId = Guid.NewGuid().ToString(); + var listOriginal = new List() +{ +new(Guid.NewGuid().ToString(), "name 1", ProductType.Onigiri, 10, false), +new(Guid.NewGuid().ToString(), "name 2", ProductType.Onigiri, 10, true), +new(Guid.NewGuid().ToString(), "name 3", ProductType.Onigiri, 10, false), +}; + _productStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny())).Returns(listOriginal); + //Act + var listOnlyActive = + _productBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, true); + var list = + _productBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, + 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, manufacturerId), + Times.Once); + _productStorageContract.Verify(x => x.GetList(false, manufacturerId), + Times.Once); + } + [Test] + public void GetAllProductsByManufacturer_ReturnEmptyList_Test() + { + //Arrange + var manufacturerId = Guid.NewGuid().ToString(); + _productStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny())).Returns([]); + //Act + var listOnlyActive = +_productBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, true); + var list = + _productBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, + 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(), + manufacturerId), Times.Exactly(2)); + } + [Test] + public void + GetAllProductsByManufacturer_ManufacturerIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByManufacturer(null, + It.IsAny()), Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByManufacturer(string.Empty, + It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void + GetAllProductsByManufacturer_ManufacturerIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByManufacturer("manufacturerId", + It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny()), Times.Never); + } + [Test] + public void GetAllProductsByManufacturer_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetAllProductsByManufacturer(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void + GetAllProductsByManufacturer_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => x.GetList(It.IsAny(), + It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => +_productBusinessLogicContract.GetAllProductsByManufacturer(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); + _productStorageContract.Verify(x => x.GetList(It.IsAny(), + It.IsAny()), Times.Once); + } + [Test] + public void GetProductHistoryByProduct_ReturnListOfRecords_Test() + { + //Arrange + var productId = Guid.NewGuid().ToString(); + var listOriginal = new List() +{ +new(Guid.NewGuid().ToString(), 10), +new(Guid.NewGuid().ToString(), 15), +new(Guid.NewGuid().ToString(), 10), +}; + _productStorageContract.Setup(x => + x.GetHistoryByProductId(It.IsAny())).Returns(listOriginal); + //Act + var list = + _productBusinessLogicContract.GetProductHistoryByProduct(productId); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(productId), Times.Once); + } + [Test] + public void GetProductHistoryByProduct_ReturnEmptyList_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.GetHistoryByProductId(It.IsAny())).Returns([]); + //Act + var list = + _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString( + )); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + [Test] + public void + GetProductHistoryByProduct_ProductIdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(null), + Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(string.Empty), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Never); + } + [Test] + public void +GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct("productId"), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Never); + } + [Test] + public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString( + )), Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + [Test] + public void + GetProductHistoryByProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.GetHistoryByProductId(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString( + )), Throws.TypeOf()); + _productStorageContract.Verify(x => + x.GetHistoryByProductId(It.IsAny()), Times.Once); + } + [Test] + public void GetProductByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new ProductDataModel(id, "name", ProductType.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 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.Onigiri, 10, false)), + Throws.TypeOf()); + _productStorageContract.Verify(x => + x.UpdElement(It.IsAny()), Times.Once); + } + [Test] + public void DeleteProduct_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _productStorageContract.Setup(x => x.DelElement(It.Is((string x) => x + == id))).Callback(() => { flag = true; }); + //Act + _productBusinessLogicContract.DeleteProduct(id); + //Assert + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), +Times.Once); + Assert.That(flag); + } + [Test] + public void DeleteProduct_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + _productStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } + [Test] + public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct(null), + Throws.TypeOf()); + Assert.That(() => + _productBusinessLogicContract.DeleteProduct(string.Empty), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + [Test] + public void DeleteProduct_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct("id"), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Never); + } + [Test] + public void DeleteProduct_StorageThrowError_ThrowException_Test() + { + //Arrange + _productStorageContract.Setup(x => + x.DelElement(It.IsAny())).Throws(new StorageException(new + InvalidOperationException())); + //Act&Assert + Assert.That(() => + _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), + Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), + Times.Once); + } +} diff --git a/PuferFishContracts/PuferFishTests/PuferFishTests.csproj b/PuferFishContracts/PuferFishTests/PuferFishTests.csproj index 126f72f..59a1627 100644 --- a/PuferFishContracts/PuferFishTests/PuferFishTests.csproj +++ b/PuferFishContracts/PuferFishTests/PuferFishTests.csproj @@ -11,12 +11,14 @@ + +