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); } }