This commit is contained in:
2025-03-11 21:11:14 +04:00
parent 514f2adefb
commit 75c5ceabc0
10 changed files with 1191 additions and 0 deletions

View File

@@ -135,6 +135,8 @@
<Compile Include="Implementations\PositionBusinessLogicContract.cs" />
<Compile Include="Implementations\ProductBusinessLogicContract.cs" />
<Compile Include="Implementations\SalaryBusinessLogicContract.cs" />
<Compile Include="Implementations\StorageBusinessLogicContract.cs" />
<Compile Include="Implementations\SupplyBusinessLogicContract.cs" />
<Compile Include="Infrastructure\IValidation.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IIngredientBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IOrderBusinessLogicContact.cs" />
@@ -142,12 +144,16 @@
<Compile Include="Interfaces\BusinessLogicsContracts\IPositionBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IProductBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\ISalaryBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\IStorageBusinessLogicContact.cs" />
<Compile Include="Interfaces\BusinessLogicsContracts\ISupplyBusinessLogicContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IIngredientStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IOrderStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IPekarStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IPositionStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IProductStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\ISalaryStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\IStorageStorageContact.cs" />
<Compile Include="Interfaces\StoragesContracts\ISupplyStorageContact.cs" />
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseBase.Implementations;
internal class StorageBusinessLogicContract
{
private readonly IStorageStorageContact _storageStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public StorageBusinessLogicContract(
IStorageStorageContact storageStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
{
_storageStorageContact = storageStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
public List<StorageDataModel> GetAllStorages()
{
return new List<StorageDataModel>();
}
public StorageDataModel GetStorageByData(string data)
{
if (data.IsGuid())
{
return new StorageDataModel("", "", CandyHouseBase.Enums.StorageType.Cabinet,
new List<IngredientDataModel>());
}
else
{
// If it's not a GUID, assume it's a title
return new StorageDataModel("", data, CandyHouseBase.Enums.StorageType.Cabinet,
new List<IngredientDataModel>());
}
}
public void InsertStorage(StorageDataModel storage)
{
}
public void UpdateStorage(StorageDataModel storage)
{
}
public void DeleteStorage(string id)
{
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using CandyHouseBase.DataModels;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseBase.Implementations;
internal class SupplyBusinessLogicContract
{
private readonly ISupplyStorageContact _supplyStorageContact;
private readonly IIngredientStorageContact _ingredientStorageContact;
private readonly ILogger _logger;
public SupplyBusinessLogicContract(
ISupplyStorageContact supplyStorageContact,
IIngredientStorageContact ingredientStorageContact,
ILogger logger)
{
_supplyStorageContact = supplyStorageContact;
_ingredientStorageContact = ingredientStorageContact;
_logger = logger;
}
public List<SupplyDataModel> GetAllSupplies()
{
return new List<SupplyDataModel>();
}
public SupplyDataModel GetSupplyByData(string data)
{
return new SupplyDataModel("", new DateTime(), 0, 0, new List<IngredientDataModel>());
}
public void InsertSupply(SupplyDataModel supply)
{
}
public void UpdateSupply(SupplyDataModel supply)
{
}
public void DeleteSupply(string id)
{
}
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.BusinessLogicsContracts;
public interface IStorageBusinessLogicContact
{
List<StorageDataModel> GetAllStorages();
StorageDataModel GetStorageByData(string data);
void InsertStorage(StorageDataModel storage);
void UpdateStorage(StorageDataModel storage);
void DeleteStorage(string id);
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.BusinessLogicsContracts;
public interface ISupplyBusinessLogicContact
{
List<SupplyDataModel> GetAllSupplies();
SupplyDataModel GetSupplyByData(string data);
void InsertSupply(SupplyDataModel supply);
void UpdateSupply(SupplyDataModel supply);
void DeleteSupply(string id);
}

View File

@@ -0,0 +1,14 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.StoragesContracts;
public interface IStorageStorageContact
{
List<StorageDataModel> GetList();
StorageDataModel GetElementById(string id);
StorageDataModel GetElementByTitle(string title);
void AddElement(StorageDataModel element);
void UpdateElement(StorageDataModel element);
void DeleteElement(string id);
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using CandyHouseBase.DataModels;
namespace CandyHouseBase.Interfaces.StoragesContracts;
public interface ISupplyStorageContact
{
List<SupplyDataModel> GetList();
SupplyDataModel GetElementById(string id);
void AddElement(SupplyDataModel element);
void UpdateElement(SupplyDataModel element);
void DeleteElement(string id);
}

View File

@@ -0,0 +1,510 @@
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Implementations;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseTests.BusinessLogicsContractsTests
{
[TestFixture]
internal class StorageBusinessLogicContractTests
{
private StorageBusinessLogicContract _storageBusinessLogicContract;
private Mock<IStorageStorageContact> _storageStorageContact;
private Mock<IIngredientStorageContact> _ingredientStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_storageStorageContact = new Mock<IStorageStorageContact>();
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_storageBusinessLogicContract = new StorageBusinessLogicContract(
_storageStorageContact.Object,
_ingredientStorageContact.Object,
new Mock<ILogger>().Object
);
}
[SetUp]
public void SetUp()
{
_storageStorageContact.Reset();
_ingredientStorageContact.Reset();
}
[Test]
public void GetAllStorages_ReturnsListOfRecords_Test()
{
// Arrange
var storages = new List<StorageDataModel>
{
new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
),
new StorageDataModel(
Guid.NewGuid().ToString(),
"Cabninet 1",
StorageType.Cabinet,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Milk", "l", 200)
}
)
};
_storageStorageContact.Setup(x => x.GetList()).Returns(storages);
// Act
var list = _storageBusinessLogicContract.GetAllStorages();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(storages));
Assert.That(
list.All(s =>
Guid.TryParse(s.Id, out _) &&
!s.Title.IsEmpty() &&
Enum.IsDefined(typeof(StorageType), s.StorageType) &&
s.Ingredients != null),
Is.True);
}
[Test]
public void GetAllStorages_ReturnsEmptyList_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetList()).Returns(new List<StorageDataModel>());
// Act
var list = _storageBusinessLogicContract.GetAllStorages();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_storageStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllStorages_ReturnsNull_ThrowException_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetList()).Returns((List<StorageDataModel>)null);
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetAllStorages(), Throws.TypeOf<NullListException>());
_storageStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllStorages_StorageThrowError_ThrowException_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetList())
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetAllStorages(), Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetStorageByData_ReturnsStorageById_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
// Act
var element = _storageBusinessLogicContract.GetStorageByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(!element.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), element.StorageType), Is.True);
Assert.That(element.Ingredients, Is.Not.Null);
_storageStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetStorageByData_ReturnsStorageByTitle_Test()
{
// Arrange
var title = "Fridge 1";
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
title,
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.GetElementByTitle(title)).Returns(storage);
// Act
var element = _storageBusinessLogicContract.GetStorageByData(title);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Title, Is.EqualTo(title));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(Enum.IsDefined(typeof(StorageType), element.StorageType), Is.True);
Assert.That(element.Ingredients, Is.Not.Null);
_storageStorageContact.Verify(x => x.GetElementByTitle(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetStorageByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_storageStorageContact.Verify(x => x.GetElementByTitle(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetStorageByData_NotFoundStorage_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns((StorageDataModel)null);
_storageStorageContact.Setup(x => x.GetElementByTitle(id)).Returns((StorageDataModel)null);
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(id),
Throws.TypeOf<ElementNotFoundException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_storageStorageContact.Verify(x => x.GetElementByTitle(id), Times.Once);
}
[Test]
public void GetStorageByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_storageStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.GetStorageByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertStorage_CorrectRecord_Test()
{
// Arrange
var flag = false;
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
.Callback((StorageDataModel x) =>
{
flag = x.Id == storage.Id && x.Title == storage.Title &&
x.StorageType == storage.StorageType &&
x.Ingredients.SequenceEqual(storage.Ingredients);
});
// Act
_storageBusinessLogicContract.InsertStorage(storage);
// Assert
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(storage.Id, out _), Is.True);
Assert.That(!storage.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), storage.StorageType), Is.True);
Assert.That(storage.Ingredients, Is.Not.Null);
}
[Test]
public void InsertStorage_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
.Throws(new ElementExistsException("ID", storage.Id));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(storage),
Throws.TypeOf<ElementExistsException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void InsertStorage_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(null),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void InsertStorage_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(new StorageDataModel(
"", // Invalid ID
"", // Invalid Title
(StorageType)999, // Invalid StorageType
null // Invalid Ingredients
)), Throws.TypeOf<ValidationException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void InsertStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.AddElement(It.IsAny<StorageDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.InsertStorage(storage),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.AddElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void UpdateStorage_CorrectRecord_Test()
{
// Arrange
var flag = false;
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
.Callback((StorageDataModel x) =>
{
flag = x.Id == storage.Id && x.Title == storage.Title &&
x.StorageType == storage.StorageType &&
x.Ingredients.SequenceEqual(storage.Ingredients);
});
// Act
_storageBusinessLogicContract.UpdateStorage(storage);
// Assert
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(storage.Id, out _), Is.True);
Assert.That(!storage.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), storage.StorageType), Is.True);
Assert.That(storage.Ingredients, Is.Not.Null);
}
[Test]
public void UpdateStorage_RecordNotFound_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
.Throws(new ElementNotFoundException("Storage not found"));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(storage),
Throws.TypeOf<ElementNotFoundException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void UpdateStorage_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(null),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void UpdateStorage_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(new StorageDataModel(
"", // Invalid ID
"", // Invalid Title
(StorageType)999, // Invalid StorageType
null // Invalid Ingredients
)), Throws.TypeOf<ValidationException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Never);
}
[Test]
public void UpdateStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var storage = new StorageDataModel(
Guid.NewGuid().ToString(),
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.UpdateElement(It.IsAny<StorageDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.UpdateStorage(storage),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.UpdateElement(It.IsAny<StorageDataModel>()), Times.Once);
}
[Test]
public void DeleteStorage_CorrectId_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
var flag = false;
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
_storageStorageContact.Setup(x => x.DeleteElement(id)).Callback(() => { flag = true; });
// Act
_storageBusinessLogicContract.DeleteStorage(id);
// Assert
_storageStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(storage.Id, out _), Is.True);
Assert.That(!storage.Title.IsEmpty());
Assert.That(Enum.IsDefined(typeof(StorageType), storage.StorageType), Is.True);
Assert.That(storage.Ingredients, Is.Not.Null);
}
[Test]
public void DeleteStorage_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns((StorageDataModel)null);
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(id),
Throws.TypeOf<ElementNotFoundException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void DeleteStorage_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_storageStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteStorage_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid-id";
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(id),
Throws.TypeOf<ValidationException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeleteStorage_StorageThrowError_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var storage = new StorageDataModel(
id,
"Fridge 1",
StorageType.Fridge,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
_storageStorageContact.Setup(x => x.GetElementById(id)).Returns(storage);
_storageStorageContact.Setup(x => x.DeleteElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _storageBusinessLogicContract.DeleteStorage(id),
Throws.TypeOf<StorageException>());
_storageStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_storageStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
}
}

View File

@@ -0,0 +1,518 @@
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Implementations;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace CandyHouseTests.BusinessLogicsContractsTests
{
[TestFixture]
internal class SupplyBusinessLogicContractTests
{
private SupplyBusinessLogicContract _supplyBusinessLogicContract;
private Mock<ISupplyStorageContact> _supplyStorageContact;
private Mock<IIngredientStorageContact> _ingredientStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_supplyStorageContact = new Mock<ISupplyStorageContact>();
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_supplyBusinessLogicContract = new SupplyBusinessLogicContract(
_supplyStorageContact.Object,
_ingredientStorageContact.Object,
new Mock<ILogger>().Object
);
}
[SetUp]
public void SetUp()
{
_supplyStorageContact.Reset();
_ingredientStorageContact.Reset();
}
[Test]
public void GetAllSupplies_ReturnsListOfRecords_Test()
{
// Arrange
var supplies = new List<SupplyDataModel>
{
new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now.AddDays(-7),
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
),
new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
200,
2000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Flour", "kg", 30.00m)
}
)
};
_supplyStorageContact.Setup(x => x.GetList()).Returns(supplies);
// Act
var list = _supplyBusinessLogicContract.GetAllSupplies();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(supplies));
Assert.That(
list.All(s =>
Guid.TryParse(s.Id, out _) &&
s.Date != default &&
s.TotalQuantity > 0 &&
s.TotalPrice >= 0 &&
s.Ingredients != null &&
s.Ingredients.Count > 0),
Is.True);
}
[Test]
public void GetAllSupplies_ReturnsEmptyList_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetList()).Returns(new List<SupplyDataModel>());
// Act
var list = _supplyBusinessLogicContract.GetAllSupplies();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_supplyStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllSupplies_ReturnsNull_ThrowException_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetList()).Returns((List<SupplyDataModel>)null);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetAllSupplies(), Throws.TypeOf<NullListException>());
_supplyStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetList())
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetAllSupplies(), Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetSupplyByData_ReturnsSupplyById_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
id,
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns(supply);
// Act
var element = _supplyBusinessLogicContract.GetSupplyByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(element.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(element.TotalQuantity, Is.GreaterThan(0));
Assert.That(element.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(element.Ingredients, Is.Not.Null);
Assert.That(element.Ingredients.Count, Is.GreaterThan(0));
_supplyStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSupplyByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSupplyByData_NotFoundSupply_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns((SupplyDataModel)null);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(id),
Throws.TypeOf<ElementNotFoundException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void GetSupplyByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_supplyStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.GetSupplyByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertSupply_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()))
.Callback((SupplyDataModel x) =>
{
flag = x.Id == supply.Id &&
x.Date == supply.Date &&
x.TotalQuantity == supply.TotalQuantity &&
x.TotalPrice == supply.TotalPrice &&
x.Ingredients.SequenceEqual(supply.Ingredients);
});
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act
_supplyBusinessLogicContract.InsertSupply(supply);
// Assert
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(supply.Id, out _), Is.True);
Assert.That(supply.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(supply.TotalQuantity, Is.GreaterThan(0));
Assert.That(supply.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(supply.Ingredients, Is.Not.Null);
Assert.That(supply.Ingredients.Count, Is.GreaterThan(0));
}
[Test]
public void InsertSupply_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()))
.Throws(new ElementExistsException("ID", supply.Id));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(supply),
Throws.TypeOf<ElementExistsException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void InsertSupply_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(null),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(new SupplyDataModel(
"", // Invalid ID
default(DateTime), // Invalid Date
0, // Invalid Quantity
-1, // Invalid Price
new List<IngredientDataModel>() // Empty ingredients list
)), Throws.TypeOf<ValidationException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void InsertSupply_StorageThrowError_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.AddElement(It.IsAny<SupplyDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.InsertSupply(supply),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.AddElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void UpdateSupply_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.UpdateElement(It.IsAny<SupplyDataModel>()))
.Callback((SupplyDataModel x) =>
{
flag = x.Id == supply.Id &&
x.Date == supply.Date &&
x.TotalQuantity == supply.TotalQuantity &&
x.TotalPrice == supply.TotalPrice &&
x.Ingredients.SequenceEqual(supply.Ingredients);
});
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act
_supplyBusinessLogicContract.UpdateSupply(supply);
// Assert
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(supply.Id, out _), Is.True);
Assert.That(supply.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(supply.TotalQuantity, Is.GreaterThan(0));
Assert.That(supply.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(supply.Ingredients, Is.Not.Null);
Assert.That(supply.Ingredients.Count, Is.GreaterThan(0));
}
[Test]
public void UpdateSupply_RecordNotFound_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.UpdateElement(It.IsAny<SupplyDataModel>()))
.Throws(new ElementNotFoundException("Supply not found"));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(supply),
Throws.TypeOf<ElementNotFoundException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void UpdateSupply_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(null),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void UpdateSupply_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(new SupplyDataModel(
"", // Invalid ID
default(DateTime), // Invalid Date
0, // Invalid Quantity
-1, // Invalid Price
new List<IngredientDataModel>() // Empty ingredients list
)), Throws.TypeOf<ValidationException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Never);
}
[Test]
public void UpdateSupply_StorageThrowError_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
Guid.NewGuid().ToString(),
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.UpdateElement(It.IsAny<SupplyDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(supply.Ingredients[0]);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.UpdateSupply(supply),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.UpdateElement(It.IsAny<SupplyDataModel>()), Times.Once);
}
[Test]
public void DeleteSupply_CorrectId_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
id,
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
);
var flag = false;
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns(supply);
_supplyStorageContact.Setup(x => x.DeleteElement(id)).Callback(() => { flag = true; });
// Act
_supplyBusinessLogicContract.DeleteSupply(id);
// Assert
_supplyStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(supply.Id, out _), Is.True);
Assert.That(supply.Date, Is.Not.EqualTo(default(DateTime)));
Assert.That(supply.TotalQuantity, Is.GreaterThan(0));
Assert.That(supply.TotalPrice, Is.GreaterThanOrEqualTo(0));
Assert.That(supply.Ingredients, Is.Not.Null);
Assert.That(supply.Ingredients.Count, Is.GreaterThan(0));
}
[Test]
public void DeleteSupply_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns((SupplyDataModel)null);
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(id),
Throws.TypeOf<ElementNotFoundException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void DeleteSupply_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_supplyStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteSupply_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid-id";
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(id),
Throws.TypeOf<ValidationException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeleteSupply_StorageThrowError_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var supply = new SupplyDataModel(
id,
DateTime.Now,
100,
1000.00m,
new List<IngredientDataModel>
{
new (Guid.NewGuid().ToString(), "Sugar", "kg", 50.00m)
}
);
_supplyStorageContact.Setup(x => x.GetElementById(id)).Returns(supply);
_supplyStorageContact.Setup(x => x.DeleteElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _supplyBusinessLogicContract.DeleteSupply(id),
Throws.TypeOf<StorageException>());
_supplyStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_supplyStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
}
}

View File

@@ -154,6 +154,8 @@
<Compile Include="BusinessLogicsContractsTests\PositionBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\ProductBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\SalaryBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\StorageBusinessLogicContractTests.cs" />
<Compile Include="BusinessLogicsContractsTests\SupplyBusinessLogicContractTests.cs" />
<Compile Include="DataModelsTests\IngredientDataModelTests.cs" />
<Compile Include="DataModelsTests\OrderDataModelTests.cs" />
<Compile Include="DataModelsTests\PekarDataModelTests.cs" />