diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 1481e44..28f348f 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -8,10 +8,14 @@ using System.Text.Json; using NorthBridgeContract.BusinessLogicContracts; namespace NorthBridgeBusinessLogic.Implementations; -internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract +internal class ProductBusinessLogicContract( + IProductStorageContract productStorageContract, + IComponentStorageContract componentStorageContract, + ILogger logger) : IProductBusinessLogicContract { private readonly ILogger _logger = logger; private readonly IProductStorageContract _productStorageContract = productStorageContract; + private readonly IComponentStorageContract _componentStorageContract = componentStorageContract; public List GetAllProducts(bool onlyActive) { @@ -96,11 +100,21 @@ internal class ProductBusinessLogicContract(IProductStorageContract productStora return components ?? throw new NullListException(); } - public void AddComponentToProduct(ComponentInProductDataModel componentInProduct) + public void AddComponentToProduct(ComponentOnStorageDataModel componentOnStorage, string productId) { - _logger.LogInformation("Adding component to product: {json}", JsonSerializer.Serialize(componentInProduct)); - ArgumentNullException.ThrowIfNull(componentInProduct); - componentInProduct.Validate(); + _logger.LogInformation("Adding component to product: {json}", JsonSerializer.Serialize(componentOnStorage)); + ArgumentNullException.ThrowIfNull(componentOnStorage); + componentOnStorage.Validate(); + + ComponentDataModel component = _componentStorageContract.GetElementById(componentOnStorage.ComponentId)!; + if (component == null) + throw new ArgumentNullException($"Component {componentOnStorage.ComponentId} not found"); + + if (componentOnStorage.Count == 0) + throw new OutOfStockException(component.ComponentName); + + ComponentInProductDataModel componentInProduct = new ComponentInProductDataModel(component.Id, productId); + _productStorageContract.AddComponentToProduct(componentInProduct); } diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/StorageBusinessLogicContract .cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/StorageBusinessLogicContract .cs new file mode 100644 index 0000000..f20d0ec --- /dev/null +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/StorageBusinessLogicContract .cs @@ -0,0 +1,99 @@ +using NorthBridgeContract.BusinessLogicsContracts; +using NorthBridgeContract.DataModels; +using NorthBridgeContract.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeBusinessLogic.Implementations +{ + public class StorageBusinessLogicContract : IStorageBusinessLogicContract + { + private readonly IStorageContract _storageContract; + + public StorageBusinessLogicContract(IStorageContract storageContract) + { + _storageContract = storageContract; + } + + public void CreateStorage(string id, string address, bool isOpen, List components) + { + if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(address)) + { + throw new ArgumentException("Invalid storage parameters"); + } + + var storage = new StorageDataModel(id, address, isOpen, components); + + _storageContract.AddStorage(storage); + } + + public void CloseStorage(string storageId) + { + var storage = _storageContract.GetStorageById(storageId); + if (storage == null) + { + throw new ArgumentNullException("Storage not found"); + } + + _storageContract.CloseStorage(storageId); + } + + public void AddComponentToStorage(string storageId, ComponentOnStorageDataModel component) + { + var storage = _storageContract.GetStorageById(storageId); + if (storage == null) + { + throw new ArgumentNullException("Storage not found"); + } + + if (component == null) + { + throw new ArgumentNullException("Component cannot be null"); + } + + _storageContract.AddOrUpdateComponentOnStorage(component); + } + + public void UpdateComponentCount(string storageId, string componentId, int newCount) + { + var storage = _storageContract.GetStorageById(storageId); + if (storage == null) + { + throw new ArgumentNullException("Storage not found"); + } + + if (newCount < 0) + { + throw new ArgumentException("Count cannot be negative"); + } + + _storageContract.UpdateComponentCount(storageId, componentId, newCount); + } + + public List GetComponentsFromStorage(string storageId) + { + var storage = _storageContract.GetStorageById(storageId); + if (storage == null) + { + throw new ArgumentNullException("Storage not found"); + } + + return _storageContract.GetComponentsOnStorage(storageId); + } + + public StorageDataModel GetStorageById(string storageId) + { + var storage = _storageContract.GetStorageById(storageId); + if (storage == null) + { + throw new ArgumentNullException("Storage not found"); + } + + return storage; + } + } + +} diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/SupplyBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/SupplyBusinessLogicContract.cs new file mode 100644 index 0000000..3de643d --- /dev/null +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/SupplyBusinessLogicContract.cs @@ -0,0 +1,63 @@ +using NorthBridgeContract.BusinessLogicsContracts; +using NorthBridgeContract.DataModels; +using NorthBridgeContract.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeBusinessLogic.Implementations +{ + public class SupplyBusinessLogicContract : ISupplyBusinessLogicContract + { + private readonly ISupplyStorageContract _supplyStorageContract; + + public SupplyBusinessLogicContract(ISupplyStorageContract supplyStorageContract) + { + _supplyStorageContract = supplyStorageContract; + } + + public void CreateSupply(SupplyDataModel supply) + { + supply.Validate(); + _supplyStorageContract.AddSupply(supply); + } + + public SupplyDataModel GetSupplyById(string supplyId) + { + var supply = _supplyStorageContract.GetSupplyById(supplyId); + if (supply == null) + { + throw new ArgumentException($"Supply with ID {supplyId} not found."); + } + return supply; + } + + public List GetSuppliesByStorageId(string storageId) + { + return _supplyStorageContract.GetSuppliesByStorageId(storageId); + } + + public void AddOrUpdateComponentInSupply(ComponentInSupplyDataModel componentInSupply) + { + componentInSupply.Validate(); + _supplyStorageContract.AddOrUpdateComponentInSupply(componentInSupply); + } + + public void UpdateComponentCountInSupply(string supplyId, string componentId, int newCount) + { + if (newCount <= 0) + { + throw new ArgumentException("Component count must be greater than zero."); + } + _supplyStorageContract.UpdateComponentCountInSupply(supplyId, componentId, newCount); + } + + public void RemoveComponentFromSupply(string supplyId, string componentId) + { + _supplyStorageContract.RemoveComponentFromSupply(supplyId, componentId); + } + } + +} diff --git a/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/IProductBusinessLogicContract.cs b/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/IProductBusinessLogicContract.cs index 59fdbee..71fdf00 100644 --- a/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/IProductBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/IProductBusinessLogicContract.cs @@ -18,7 +18,7 @@ namespace NorthBridgeContract.BusinessLogicContracts List GetComponentsByProductId(string productId); - void AddComponentToProduct(ComponentInProductDataModel componentInProduct); + void AddComponentToProduct(ComponentOnStorageDataModel componentOnStorage, string productId); void RemoveComponentFromProduct(string componentId, string productId); } diff --git a/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/IStorageBusinessLogicContract.cs b/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/IStorageBusinessLogicContract.cs new file mode 100644 index 0000000..403e4d1 --- /dev/null +++ b/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/IStorageBusinessLogicContract.cs @@ -0,0 +1,25 @@ +using NorthBridgeContract.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeContract.BusinessLogicsContracts +{ + public interface IStorageBusinessLogicContract + { + void CreateStorage(string id, string address, bool isOpen, List components); + + void CloseStorage(string storageId); + + void AddComponentToStorage(string storageId, ComponentOnStorageDataModel component); + + void UpdateComponentCount(string storageId, string componentId, int newCount); + + List GetComponentsFromStorage(string storageId); + + StorageDataModel GetStorageById(string storageId); + } + +} diff --git a/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/ISupplyBusinessLogicContract.cs b/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/ISupplyBusinessLogicContract.cs new file mode 100644 index 0000000..1602cc2 --- /dev/null +++ b/NorthBridge/NorthBridgeContract/BusinessLogicsContracts/ISupplyBusinessLogicContract.cs @@ -0,0 +1,25 @@ +using NorthBridgeContract.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeContract.BusinessLogicsContracts +{ + public interface ISupplyBusinessLogicContract + { + void CreateSupply(SupplyDataModel supply); + + SupplyDataModel GetSupplyById(string supplyId); + + List GetSuppliesByStorageId(string storageId); + + void AddOrUpdateComponentInSupply(ComponentInSupplyDataModel componentInSupply); + + void UpdateComponentCountInSupply(string supplyId, string componentId, int newCount); + + void RemoveComponentFromSupply(string supplyId, string componentId); + } + +} diff --git a/NorthBridge/NorthBridgeContract/DataModels/ProductsInSupplyDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ComponentInSupplyDataModel.cs similarity index 78% rename from NorthBridge/NorthBridgeContract/DataModels/ProductsInSupplyDataModel.cs rename to NorthBridge/NorthBridgeContract/DataModels/ComponentInSupplyDataModel.cs index bbd30be..1292930 100644 --- a/NorthBridge/NorthBridgeContract/DataModels/ProductsInSupplyDataModel.cs +++ b/NorthBridge/NorthBridgeContract/DataModels/ComponentInSupplyDataModel.cs @@ -3,11 +3,11 @@ using NorthBridgeContract.Extensions; namespace NorthBridgeContract.DataModels { - public class ProductsInSupplyDataModel(string supplyId, string productId, int count) + public class ComponentInSupplyDataModel(string supplyId, string componentId, int count) { public string SupplyId { get; private set; } = supplyId; - public string ProductId { get; private set; } = productId; + public string ComponentId { get; private set; } = componentId; public int Count { get; private set; } = count; @@ -19,10 +19,10 @@ namespace NorthBridgeContract.DataModels if (!SupplyId.IsGuid()) throw new ValidationException("The value in the field SupplyId is not a unique identifier"); - if (ProductId.IsEmpty()) + if (ComponentId.IsEmpty()) throw new ValidationException("Field ProductId is empty"); - if (!ProductId.IsGuid()) + if (!ComponentId.IsGuid()) throw new ValidationException("The value in the field ProductId is not a unique identifier"); if (Count <= 0) diff --git a/NorthBridge/NorthBridgeContract/DataModels/ProductOnStorageDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/ComponentOnStorageDataModel.cs similarity index 68% rename from NorthBridge/NorthBridgeContract/DataModels/ProductOnStorageDataModel.cs rename to NorthBridge/NorthBridgeContract/DataModels/ComponentOnStorageDataModel.cs index 623dc80..83ea564 100644 --- a/NorthBridge/NorthBridgeContract/DataModels/ProductOnStorageDataModel.cs +++ b/NorthBridge/NorthBridgeContract/DataModels/ComponentOnStorageDataModel.cs @@ -4,11 +4,11 @@ using NorthBridgeContract.Infrastructure; namespace NorthBridgeContract.DataModels { - public class ProductOnStorageDataModel(string storageId, string productId, int count) : IValidation + public class ComponentOnStorageDataModel(string storageId, string componentId, int count) : IValidation { public string StorageId { get; private set; } = storageId; - public string ProductId { get; private set; } = productId; + public string ComponentId { get; private set; } = componentId; public int Count { get; private set; } = count; @@ -20,14 +20,11 @@ namespace NorthBridgeContract.DataModels if (!StorageId.IsGuid()) throw new ValidationException("The value in the field StorageId is not a unique identifier"); - if (ProductId.IsEmpty()) + if (ComponentId.IsEmpty()) throw new ValidationException("Field ProductId is empty"); - if (!ProductId.IsGuid()) + if (!ComponentId.IsGuid()) throw new ValidationException("The value in the field ProductId is not a unique identifier"); - - if (Count <= 0) - throw new ValidationException("Field Count is less than or equal to 0"); } } } diff --git a/NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs index a92b3f6..908e436 100644 --- a/NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs +++ b/NorthBridge/NorthBridgeContract/DataModels/StorageDataModel.cs @@ -4,10 +4,12 @@ using NorthBridgeContract.Infrastructure; namespace NorthBridgeContract.DataModels { - public class StorageDataModel(string id, string address) : IValidation + public class StorageDataModel(string id, string address, bool isOpen, List components) : IValidation { public string Id { get; private set; } = id; public string Address { get; private set; } = address; + public bool IsOpen { get; private set; } = isOpen; + public List Components { get; private set; } = components; public void Validate() { diff --git a/NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs b/NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs index 2405b6b..e5c3cc7 100644 --- a/NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs +++ b/NorthBridge/NorthBridgeContract/DataModels/SupplyDataModel.cs @@ -9,11 +9,13 @@ using System.Threading.Tasks; namespace NorthBridgeContract.DataModels { - public class SupplyDataModel(string id, string storageId, DateTime date) : IValidation + public class SupplyDataModel(string id, string storageId, DateTime date, List components) : IValidation { public string Id { get; private set; } = id; public string StorageId { get; private set; } = storageId; public DateTime Date { get; private set; } = date; + public List Components { get; private set; } = components; + public void Validate() { @@ -28,6 +30,9 @@ namespace NorthBridgeContract.DataModels if (!Id.IsGuid()) throw new ValidationException("The value in the field Id is not a unique identifier"); + + if (Components.Count == 0) + throw new ValidationException("At least 1 component is needed"); } } } diff --git a/NorthBridge/NorthBridgeContract/Exceptions/OutOfStockException.cs b/NorthBridge/NorthBridgeContract/Exceptions/OutOfStockException.cs new file mode 100644 index 0000000..1f224ac --- /dev/null +++ b/NorthBridge/NorthBridgeContract/Exceptions/OutOfStockException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeContract.Exceptions +{ + public class OutOfStockException : Exception + { + public OutOfStockException(string itemName) : base($"Item {itemName} is not available at this storage") { } + } +} diff --git a/NorthBridge/NorthBridgeContract/StoragesContracts/IStorageStorageContract.cs b/NorthBridge/NorthBridgeContract/StoragesContracts/IStorageStorageContract.cs new file mode 100644 index 0000000..4a14f82 --- /dev/null +++ b/NorthBridge/NorthBridgeContract/StoragesContracts/IStorageStorageContract.cs @@ -0,0 +1,27 @@ +using NorthBridgeContract.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeContract.StoragesContracts +{ + public interface IStorageContract + { + List GetAllStorages(); + + StorageDataModel? GetStorageById(string storageId); + + void AddStorage(StorageDataModel storage); + + void CloseStorage(string storageId); + + void AddOrUpdateComponentOnStorage(ComponentOnStorageDataModel component); + + void UpdateComponentCount(string storageId, string componentId, int newCount); + + List GetComponentsOnStorage(string storageId); + } + +} diff --git a/NorthBridge/NorthBridgeContract/StoragesContracts/ISupplyStorageContract.cs b/NorthBridge/NorthBridgeContract/StoragesContracts/ISupplyStorageContract.cs new file mode 100644 index 0000000..4fc927d --- /dev/null +++ b/NorthBridge/NorthBridgeContract/StoragesContracts/ISupplyStorageContract.cs @@ -0,0 +1,25 @@ +using NorthBridgeContract.DataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeContract.StoragesContracts +{ + public interface ISupplyStorageContract + { + void AddSupply(SupplyDataModel supply); + + SupplyDataModel? GetSupplyById(string supplyId); + + List GetSuppliesByStorageId(string storageId); + + void AddOrUpdateComponentInSupply(ComponentInSupplyDataModel componentInSupply); + + void UpdateComponentCountInSupply(string supplyId, string componentId, int newCount); + + void RemoveComponentFromSupply(string supplyId, string componentId); + } + +} diff --git a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs index 1756a07..6d4148a 100644 --- a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs +++ b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs @@ -3,6 +3,7 @@ using Moq; using NorthBridgeBusinessLogic.Implementations; using NorthBridgeContract.DataModels; using NorthBridgeContract.Enums; +using NorthBridgeContract.Exceptions; using NorthBridgeContract.StoragesContracts; namespace NorthBridgeTest.BusinessLogicsContractsTests @@ -12,6 +13,7 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests { private ProductBusinessLogicContract _productBusinessLogicContract; private Mock _productStorageContract; + private Mock _componentStorageContract; private Mock _logger; [OneTimeSetUp] @@ -19,13 +21,82 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests { _logger = new Mock(); _productStorageContract = new Mock(); - _productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, _logger.Object); + _componentStorageContract = new Mock(); + + _productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, _componentStorageContract.Object, _logger.Object); } [TearDown] public void TearDown() { _productStorageContract.Reset(); + _componentStorageContract.Reset(); + } + + [Test] + public void AddComponentToProduct_Successful_Test() + { + // Arrange + var productId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + var componentOnStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), componentId, 5); + var component = new ComponentDataModel(componentId, "Test Component", ComponentType.RAM, Guid.NewGuid().ToString(), 100, false); + + _componentStorageContract.Setup(x => x.GetElementById(componentId)).Returns(component); + _productStorageContract.Setup(x => x.AddComponentToProduct(It.IsAny())); + + // Act + _productBusinessLogicContract.AddComponentToProduct(componentOnStorage, productId); + + // Assert + _componentStorageContract.Verify(x => x.GetElementById(componentId), Times.Once); + _productStorageContract.Verify(x => x.AddComponentToProduct(It.Is( + p => p.ComponentId == componentId && p.ProductId == productId)), Times.Once); + } + + [Test] + public void AddComponentToProduct_ComponentNotFound_ThrowsException_Test() + { + // Arrange + var productId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + var componentOnStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), componentId, 5); + + _componentStorageContract.Setup(x => x.GetElementById(componentId)).Returns((ComponentDataModel)null!); + + // Act & Assert + var ex = Assert.Throws(() => _productBusinessLogicContract.AddComponentToProduct(componentOnStorage, productId)); + Assert.That(ex.Message, Does.Contain($"Component {componentId} not found")); + + _componentStorageContract.Verify(x => x.GetElementById(componentId), Times.Once); + _productStorageContract.Verify(x => x.AddComponentToProduct(It.IsAny()), Times.Never); + } + + [Test] + public void AddComponentToProduct_NullComponentOnStorage_ThrowsException_Test() + { + // Arrange + var productId = Guid.NewGuid().ToString(); + + // Act & Assert + Assert.Throws(() => _productBusinessLogicContract.AddComponentToProduct(null!, productId)); + + _componentStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _productStorageContract.Verify(x => x.AddComponentToProduct(It.IsAny()), Times.Never); + } + + [Test] + public void AddComponentToProduct_InvalidComponentOnStorage_ThrowsValidationException_Test() + { + // Arrange + var productId = Guid.NewGuid().ToString(); + var componentOnStorage = new ComponentOnStorageDataModel("", "", 0); // Невалидные данные + + // Act & Assert + Assert.Throws(() => _productBusinessLogicContract.AddComponentToProduct(componentOnStorage, productId)); + + _componentStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _productStorageContract.Verify(x => x.AddComponentToProduct(It.IsAny()), Times.Never); } [Test] @@ -33,16 +104,16 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests { // Arrange var components = new List - { + { new ComponentDataModel(Guid.NewGuid().ToString(), "Component 1", ComponentType.RAM, Guid.NewGuid().ToString(), 100, false), new ComponentDataModel(Guid.NewGuid().ToString(), "Component 2", ComponentType.Processor, Guid.NewGuid().ToString(), 200, true) - }; + }; var products = new List - { + { new ProductDataModel(Guid.NewGuid().ToString(), "Product 1", Guid.NewGuid().ToString(), true, components), new ProductDataModel(Guid.NewGuid().ToString(), "Product 2", Guid.NewGuid().ToString(), false, components) - }; + }; _productStorageContract.Setup(x => x.GetList(It.IsAny())).Returns(products); @@ -64,20 +135,20 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests { // Arrange var components = new List - { + { new ComponentDataModel(Guid.NewGuid().ToString(), "Component 1", ComponentType.RAM, Guid.NewGuid().ToString(), 100, false), new ComponentDataModel(Guid.NewGuid().ToString(), "Component 2", ComponentType.Processor, Guid.NewGuid().ToString(), 200, true) - }; + }; var product = new ProductDataModel(Guid.NewGuid().ToString(), "Product 1", Guid.NewGuid().ToString(), true, components); - // Проверка, что метод добавления работает _productStorageContract.Setup(x => x.AddElement(It.IsAny())); // Act _productBusinessLogicContract.InsertProduct(product); // Assert - _productStorageContract.Verify(x => x.AddElement(It.Is(p => p.Id == product.Id && p.ProductName == product.ProductName && p.WorkerId == product.WorkerId && p.Components == product.Components)), Times.Once); + _productStorageContract.Verify(x => x.AddElement(It.Is(p => + p.Id == product.Id && p.ProductName == product.ProductName && p.WorkerId == product.WorkerId && p.Components == product.Components)), Times.Once); } [Test] @@ -85,10 +156,10 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests { // Arrange var components = new List - { - new ComponentDataModel(Guid.NewGuid().ToString(), "Component 1", ComponentType.RAM, Guid.NewGuid().ToString(), 100, false), - new ComponentDataModel(Guid.NewGuid().ToString(), "Component 2", ComponentType.Processor, Guid.NewGuid().ToString(), 200, true) - }; + { + new ComponentDataModel(Guid.NewGuid().ToString(), "Component 1", ComponentType.RAM, Guid.NewGuid().ToString(), 100, false), + new ComponentDataModel(Guid.NewGuid().ToString(), "Component 2", ComponentType.Processor, Guid.NewGuid().ToString(), 200, true) + }; var productId = "6F9619FF-8B86-D011-B42D-00CF4FC964FF"; var product = new ProductDataModel(productId, "Product 1", Guid.NewGuid().ToString(), true, components); @@ -99,11 +170,11 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests _productBusinessLogicContract.UpdateProduct(product); // Assert - _productStorageContract.Verify(x => x.GetElementById(productId), Times.Once); // Проверка, что продукт был найден - _productStorageContract.Verify(x => x.UpdElement(It.Is(p => p.Id == product.Id && p.ProductName == product.ProductName && p.WorkerId == product.WorkerId && p.Components == product.Components)), Times.Once); + _productStorageContract.Verify(x => x.GetElementById(productId), Times.Once); + _productStorageContract.Verify(x => x.UpdElement(It.Is(p => + p.Id == product.Id && p.ProductName == product.ProductName && p.WorkerId == product.WorkerId && p.Components == product.Components)), Times.Once); } - [Test] public void DeleteProduct_ValidId_Test() { @@ -130,4 +201,5 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests } } + } diff --git a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/StorageBusinessLogicContractTests.cs b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/StorageBusinessLogicContractTests.cs new file mode 100644 index 0000000..01bcb00 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/StorageBusinessLogicContractTests.cs @@ -0,0 +1,233 @@ +using Moq; +using NorthBridgeBusinessLogic.Implementations; +using NorthBridgeContract.DataModels; +using NorthBridgeContract.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeTest.BusinessLogicsContractsTests +{ + [TestFixture] + internal class StorageBusinessLogicContractTests + { + private StorageBusinessLogicContract _storageBusinessLogicContract; + private Mock _storageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _storageContract = new Mock(); + _storageBusinessLogicContract = new StorageBusinessLogicContract(_storageContract.Object); + } + + [TearDown] + public void TearDown() + { + _storageContract.Reset(); + } + + [Test] + public void CreateStorage_ValidParameters_CreatesStorage_Test() + { + // Arrange + var storageId = Guid.NewGuid().ToString(); + var address = "ул. Пионерская д. 2В"; + var isOpen = true; + var components = new List + { + new ComponentOnStorageDataModel("storage1", "component1", 100) + }; + + _storageContract.Setup(x => x.AddStorage(It.IsAny())).Verifiable(); + + // Act + _storageBusinessLogicContract.CreateStorage(storageId, address, isOpen, components); + + // Assert + _storageContract.Verify(x => x.AddStorage(It.Is(s => + s.Id == storageId && s.Address == address && s.IsOpen == isOpen && s.Components == components)), Times.Once); + } + + [Test] + public void CreateStorage_InvalidParameters_ThrowsArgumentException_Test() + { + // Arrange + var storageId = ""; + var address = "ул. Пионерская д. 2В"; + var isOpen = true; + var components = new List + { + new ComponentOnStorageDataModel("storage1", "component1", 100) + }; + + // Act & Assert + Assert.That(() => _storageBusinessLogicContract.CreateStorage(storageId, address, isOpen, components), + Throws.ArgumentException.With.Message.Contains("Invalid storage parameters")); + _storageContract.Verify(x => x.AddStorage(It.IsAny()), Times.Never); + } + + [Test] + public void CloseStorage_ValidStorage_ClosesStorage_Test() + { + // Arrange + var storageId = Guid.NewGuid().ToString(); + var storage = new StorageDataModel(storageId, "ул. Пионерская д. 2В", true, new List()); + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns(storage); + _storageContract.Setup(x => x.CloseStorage(storageId)).Verifiable(); + + // Act + _storageBusinessLogicContract.CloseStorage(storageId); + + // Assert + _storageContract.Verify(x => x.CloseStorage(storageId), Times.Once); + } + + [Test] + public void CloseStorage_StorageNotFound_ThrowsArgumentNullException_Test() + { + // Arrange + var storageId = "non-existent"; + + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns((StorageDataModel?)null); + + // Act & Assert + Assert.That(() => _storageBusinessLogicContract.CloseStorage(storageId), + Throws.ArgumentNullException.With.Message.Contains("Storage not found")); + _storageContract.Verify(x => x.CloseStorage(It.IsAny()), Times.Never); + } + + [Test] + public void AddComponentToStorage_ValidComponent_AddsComponent_Test() + { + // Arrange + var storageId = Guid.NewGuid().ToString(); + var component = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), "component1", 100); + var storage = new StorageDataModel(storageId, "ул. Пионерская д. 2В", true, new List()); + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns(storage); + _storageContract.Setup(x => x.AddOrUpdateComponentOnStorage(It.IsAny())).Verifiable(); + + // Act + _storageBusinessLogicContract.AddComponentToStorage(storageId, component); + + // Assert + _storageContract.Verify(x => x.AddOrUpdateComponentOnStorage(It.Is(c => + c.StorageId == component.StorageId && c.ComponentId == component.ComponentId && c.Count == component.Count)), Times.Once); + } + + [Test] + public void AddComponentToStorage_StorageNotFound_ThrowsArgumentNullException_Test() + { + // Arrange + var storageId = "non-existent"; + var component = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), "component1", 100); + + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns((StorageDataModel?)null); + + // Act & Assert + Assert.That(() => _storageBusinessLogicContract.AddComponentToStorage(storageId, component), + Throws.ArgumentNullException.With.Message.Contains("Storage not found")); + _storageContract.Verify(x => x.AddOrUpdateComponentOnStorage(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateComponentCount_ValidParameters_UpdatesCount_Test() + { + // Arrange + var storageId = Guid.NewGuid().ToString(); + var componentId = "component1"; + var newCount = 200; + var storage = new StorageDataModel(storageId, "ул. Пионерская д. 2В", true, new List + { + new ComponentOnStorageDataModel(storageId, componentId, 100) + }); + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns(storage); + _storageContract.Setup(x => x.UpdateComponentCount(storageId, componentId, newCount)).Verifiable(); + + // Act + _storageBusinessLogicContract.UpdateComponentCount(storageId, componentId, newCount); + + // Assert + _storageContract.Verify(x => x.UpdateComponentCount(storageId, componentId, newCount), Times.Once); + } + + [Test] + public void UpdateComponentCount_StorageNotFound_ThrowsArgumentNullException_Test() + { + // Arrange + var storageId = "non-existent"; + var componentId = "component1"; + var newCount = 200; + + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns((StorageDataModel?)null); + + // Act & Assert + Assert.That(() => _storageBusinessLogicContract.UpdateComponentCount(storageId, componentId, newCount), + Throws.ArgumentNullException.With.Message.Contains("Storage not found")); + _storageContract.Verify(x => x.UpdateComponentCount(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void UpdateComponentCount_NegativeCount_ThrowsArgumentException_Test() + { + // Arrange + var storageId = Guid.NewGuid().ToString(); + var componentId = "component1"; + var newCount = -10; + var storage = new StorageDataModel(storageId, "ул. Пионерская д. 2В", true, new List + { + new ComponentOnStorageDataModel(storageId, componentId, 100) + }); + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns(storage); + + // Act & Assert + Assert.That(() => _storageBusinessLogicContract.UpdateComponentCount(storageId, componentId, newCount), + Throws.ArgumentException.With.Message.Contains("Count cannot be negative")); + _storageContract.Verify(x => x.UpdateComponentCount(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void GetComponentsFromStorage_StorageNotFound_ThrowsArgumentNullException_Test() + { + // Arrange + var storageId = "non-existent"; + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns((StorageDataModel?)null); + + // Act & Assert + Assert.That(() => _storageBusinessLogicContract.GetComponentsFromStorage(storageId), + Throws.ArgumentNullException.With.Message.Contains("Storage not found")); + } + + [Test] + public void GetStorageById_StorageFound_ReturnsStorage_Test() + { + // Arrange + var storageId = Guid.NewGuid().ToString(); + var storage = new StorageDataModel(storageId, "ул. Пионерская д. 2В", true, new List()); + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns(storage); + + // Act + var result = _storageBusinessLogicContract.GetStorageById(storageId); + + // Assert + Assert.That(result, Is.Not.Null); + Assert.That(result.Id, Is.EqualTo(storageId)); + _storageContract.Verify(x => x.GetStorageById(storageId), Times.Once); + } + + [Test] + public void GetStorageById_StorageNotFound_ThrowsArgumentNullException_Test() + { + // Arrange + var storageId = "non-existent"; + _storageContract.Setup(x => x.GetStorageById(storageId)).Returns((StorageDataModel?)null); + + // Act & Assert + Assert.That(() => _storageBusinessLogicContract.GetStorageById(storageId), + Throws.ArgumentNullException.With.Message.Contains("Storage not found")); + } + } + +} diff --git a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/SupplyBusinessLogicContractTests.cs b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/SupplyBusinessLogicContractTests.cs new file mode 100644 index 0000000..e8c1d52 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/SupplyBusinessLogicContractTests.cs @@ -0,0 +1,200 @@ +using Moq; +using NorthBridgeBusinessLogic.Implementations; +using NorthBridgeContract.DataModels; +using NorthBridgeContract.StoragesContracts; +using NorthBridgeContract.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeTest.BusinessLogicsContractsTests +{ + [TestFixture] + internal class SupplyBusinessLogicContractTests + { + private SupplyBusinessLogicContract _supplyBusinessLogicContract; + private Mock _supplyStorageContract; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _supplyStorageContract = new Mock(); + _supplyBusinessLogicContract = new SupplyBusinessLogicContract(_supplyStorageContract.Object); + } + + [TearDown] + public void TearDown() + { + _supplyStorageContract.Reset(); + } + + [Test] + public void CreateSupply_ValidSupply_CreatesSupply_Test() + { + // Arrange + var supply = new SupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, new List + { + new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 100) + }); + + _supplyStorageContract.Setup(x => x.AddSupply(It.IsAny())).Verifiable(); + + // Act + _supplyBusinessLogicContract.CreateSupply(supply); + + // Assert + _supplyStorageContract.Verify(x => x.AddSupply(It.Is(s => + s.Id == supply.Id && s.StorageId == supply.StorageId && s.Date == supply.Date && s.Components == supply.Components)), Times.Once); + } + + [Test] + public void CreateSupply_InvalidSupply_ThrowsValidationException_Test() + { + // Arrange + var supply = new SupplyDataModel(Guid.NewGuid().ToString(), "", DateTime.UtcNow, new List()); + + // Act & Assert + Assert.That(() => _supplyBusinessLogicContract.CreateSupply(supply), + Throws.TypeOf()); + _supplyStorageContract.Verify(x => x.AddSupply(It.IsAny()), Times.Never); + } + + [Test] + public void GetSupplyById_SupplyFound_ReturnsSupply_Test() + { + // Arrange + var supplyId = Guid.NewGuid().ToString(); + var supply = new SupplyDataModel(supplyId, Guid.NewGuid().ToString(), DateTime.UtcNow, new List()); + _supplyStorageContract.Setup(x => x.GetSupplyById(supplyId)).Returns(supply); + + // Act + var result = _supplyBusinessLogicContract.GetSupplyById(supplyId); + + // Assert + Assert.That(result, Is.Not.Null); + Assert.That(result.Id, Is.EqualTo(supplyId)); + _supplyStorageContract.Verify(x => x.GetSupplyById(supplyId), Times.Once); + } + + [Test] + public void GetSupplyById_SupplyNotFound_ThrowsArgumentException_Test() + { + // Arrange + var supplyId = "non-existent"; + _supplyStorageContract.Setup(x => x.GetSupplyById(supplyId)).Returns((SupplyDataModel?)null); + + // Act & Assert + Assert.That(() => _supplyBusinessLogicContract.GetSupplyById(supplyId), + Throws.ArgumentException.With.Message.Contains($"Supply with ID {supplyId} not found.")); + _supplyStorageContract.Verify(x => x.GetSupplyById(supplyId), Times.Once); + } + + [Test] + public void GetSuppliesByStorageId_ReturnsSupplies_Test() + { + // Arrange + var storageId = Guid.NewGuid().ToString(); + var supplies = new List + { + new SupplyDataModel(Guid.NewGuid().ToString(), storageId, DateTime.UtcNow, new List()) + }; + _supplyStorageContract.Setup(x => x.GetSuppliesByStorageId(storageId)).Returns(supplies); + + // Act + var result = _supplyBusinessLogicContract.GetSuppliesByStorageId(storageId); + + // Assert + Assert.That(result, Is.Not.Empty); + Assert.That(result.First().StorageId, Is.EqualTo(storageId)); + _supplyStorageContract.Verify(x => x.GetSuppliesByStorageId(storageId), Times.Once); + } + + [Test] + public void AddOrUpdateComponentInSupply_ValidComponent_AddsOrUpdatesComponent_Test() + { + // Arrange + var component = new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 100); + _supplyStorageContract.Setup(x => x.AddOrUpdateComponentInSupply(It.IsAny())).Verifiable(); + + // Act + _supplyBusinessLogicContract.AddOrUpdateComponentInSupply(component); + + // Assert + _supplyStorageContract.Verify(x => x.AddOrUpdateComponentInSupply(It.Is(c => + c.SupplyId == component.SupplyId && c.ComponentId == component.ComponentId && c.Count == component.Count)), Times.Once); + } + + [Test] + public void AddOrUpdateComponentInSupply_InvalidComponent_ThrowsValidationException_Test() + { + // Arrange + var component = new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), "", -10); + + // Act & Assert + Assert.That(() => _supplyBusinessLogicContract.AddOrUpdateComponentInSupply(component), + Throws.TypeOf()); + _supplyStorageContract.Verify(x => x.AddOrUpdateComponentInSupply(It.IsAny()), Times.Never); + } + + [Test] + public void UpdateComponentCountInSupply_ValidCount_UpdatesCount_Test() + { + // Arrange + var supplyId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + var newCount = 200; + _supplyStorageContract.Setup(x => x.UpdateComponentCountInSupply(supplyId, componentId, newCount)).Verifiable(); + + // Act + _supplyBusinessLogicContract.UpdateComponentCountInSupply(supplyId, componentId, newCount); + + // Assert + _supplyStorageContract.Verify(x => x.UpdateComponentCountInSupply(supplyId, componentId, newCount), Times.Once); + } + + [Test] + public void UpdateComponentCountInSupply_NegativeCount_ThrowsArgumentException_Test() + { + // Arrange + var supplyId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + var newCount = -10; + + // Act & Assert + Assert.That(() => _supplyBusinessLogicContract.UpdateComponentCountInSupply(supplyId, componentId, newCount), + Throws.ArgumentException.With.Message.Contains("Component count must be greater than zero.")); + _supplyStorageContract.Verify(x => x.UpdateComponentCountInSupply(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + } + + [Test] + public void RemoveComponentFromSupply_RemovesComponent_Test() + { + // Arrange + var supplyId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + _supplyStorageContract.Setup(x => x.RemoveComponentFromSupply(supplyId, componentId)).Verifiable(); + + // Act + _supplyBusinessLogicContract.RemoveComponentFromSupply(supplyId, componentId); + + // Assert + _supplyStorageContract.Verify(x => x.RemoveComponentFromSupply(supplyId, componentId), Times.Once); + } + + [Test] + public void RemoveComponentFromSupply_SupplyNotFound_ThrowsArgumentException_Test() + { + // Arrange + var supplyId = "abc"; + var componentId = Guid.NewGuid().ToString(); + _supplyStorageContract.Setup(x => x.RemoveComponentFromSupply(supplyId, componentId)).Throws(new ArgumentException("Supply not found")); + + // Act & Assert + Assert.That(() => _supplyBusinessLogicContract.RemoveComponentFromSupply(supplyId, componentId), + Throws.ArgumentException.With.Message.Contains("Supply not found")); + } + } + +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentInSupplyDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentInSupplyDataModelTests.cs new file mode 100644 index 0000000..eaf6423 --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentInSupplyDataModelTests.cs @@ -0,0 +1,70 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; + +namespace NorthBridgeTest.DataModelsTests +{ + [TestFixture] + internal class ComponentInSupplyDataModelTests + { + [Test] + public void SupplyIdIsNullOrEmptyTest() + { + var componentSupply = CreateDataModel(null, Guid.NewGuid().ToString(), 1); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + componentSupply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void SupplyIdIsNotGuidTest() + { + var componentSupply = CreateDataModel("id", Guid.NewGuid().ToString(), 1); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var componentSupply = CreateDataModel(Guid.NewGuid().ToString(), null, 1); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + componentSupply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var componentSupply = CreateDataModel(Guid.NewGuid().ToString(), "id", 1); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void CountIsLessThanOrEqualToZeroTest() + { + var componentSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + componentSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1); + Assert.That(() => componentSupply.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsAreCorrectTest() + { + var supplyId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + var count = 5; + var componentSupply = CreateDataModel(supplyId, componentId, count); + Assert.That(() => componentSupply.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(componentSupply.SupplyId, Is.EqualTo(supplyId)); + Assert.That(componentSupply.ComponentId, Is.EqualTo(componentId)); + Assert.That(componentSupply.Count, Is.EqualTo(count)); + }); + } + + private static ComponentInSupplyDataModel CreateDataModel(string? supplyId, string? componentId, int count) => + new(supplyId, componentId, count); + } + +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentOnStorageDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentOnStorageDataModelTests.cs new file mode 100644 index 0000000..60d08cf --- /dev/null +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/ComponentOnStorageDataModelTests.cs @@ -0,0 +1,71 @@ +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NorthBridgeTest.DataModelsTests +{ + [TestFixture] + internal class ComponentOnStorageDataModelTests + { + [Test] + public void StorageIdIsNullOrEmptyTest() + { + var componentStorage = new ComponentOnStorageDataModel(null, Guid.NewGuid().ToString(), 1); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + componentStorage = new ComponentOnStorageDataModel(string.Empty, Guid.NewGuid().ToString(), 1); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void StorageIdIsNotGuidTest() + { + var componentStorage = new ComponentOnStorageDataModel("id", Guid.NewGuid().ToString(), 1); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNullOrEmptyTest() + { + var componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), null, 1); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), string.Empty, 1); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void ProductIdIsNotGuidTest() + { + var componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), "id", 1); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void CountIsLessThanOrEqualToZeroTest() + { + var componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1); + Assert.That(() => componentStorage.Validate(), Throws.TypeOf()); + } + + [Test] + public void AllFieldsAreCorrectTest() + { + var storageId = Guid.NewGuid().ToString(); + var componentId = Guid.NewGuid().ToString(); + var count = 5; + var componentStorage = new ComponentOnStorageDataModel(storageId, componentId, count); + Assert.That(() => componentStorage.Validate(), Throws.Nothing); + Assert.Multiple(() => + { + Assert.That(componentStorage.StorageId, Is.EqualTo(storageId)); + Assert.That(componentStorage.ComponentId, Is.EqualTo(componentId)); + Assert.That(componentStorage.Count, Is.EqualTo(count)); + }); + } + } +} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductOnStorageDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductOnStorageDataModelTests.cs deleted file mode 100644 index f314bf0..0000000 --- a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductOnStorageDataModelTests.cs +++ /dev/null @@ -1,71 +0,0 @@ -using NorthBridgeContract.DataModels; -using NorthBridgeContract.Exceptions; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NorthBridgeTest.DataModelsTests -{ - [TestFixture] - internal class ProductOnStorageDataModelTests - { - [Test] - public void StorageIdIsNullOrEmptyTest() - { - var productStorage = new ProductOnStorageDataModel(null, Guid.NewGuid().ToString(), 1); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - productStorage = new ProductOnStorageDataModel(string.Empty, Guid.NewGuid().ToString(), 1); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - } - - [Test] - public void StorageIdIsNotGuidTest() - { - var productStorage = new ProductOnStorageDataModel("id", Guid.NewGuid().ToString(), 1); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - } - - [Test] - public void ProductIdIsNullOrEmptyTest() - { - var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), null, 1); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), string.Empty, 1); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - } - - [Test] - public void ProductIdIsNotGuidTest() - { - var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), "id", 1); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - } - - [Test] - public void CountIsLessThanOrEqualToZeroTest() - { - var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1); - Assert.That(() => productStorage.Validate(), Throws.TypeOf()); - } - - [Test] - public void AllFieldsAreCorrectTest() - { - var storageId = Guid.NewGuid().ToString(); - var productId = Guid.NewGuid().ToString(); - var count = 5; - var productStorage = new ProductOnStorageDataModel(storageId, productId, count); - Assert.That(() => productStorage.Validate(), Throws.Nothing); - Assert.Multiple(() => - { - Assert.That(productStorage.StorageId, Is.EqualTo(storageId)); - Assert.That(productStorage.ProductId, Is.EqualTo(productId)); - Assert.That(productStorage.Count, Is.EqualTo(count)); - }); - } - } -} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductsInSupplyDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/ProductsInSupplyDataModelTests.cs deleted file mode 100644 index 54448e3..0000000 --- a/NorthBridge/NorthBridgeTest/DataModelsTests/ProductsInSupplyDataModelTests.cs +++ /dev/null @@ -1,70 +0,0 @@ -using NorthBridgeContract.DataModels; -using NorthBridgeContract.Exceptions; - -namespace NorthBridgeTest.DataModelsTests -{ - [TestFixture] - internal class ProductsInSupplyDataModelTests - { - [Test] - public void SupplyIdIsNullOrEmptyTest() - { - var productSupply = CreateDataModel(null, Guid.NewGuid().ToString(), 1); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - productSupply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - } - - [Test] - public void SupplyIdIsNotGuidTest() - { - var productSupply = CreateDataModel("id", Guid.NewGuid().ToString(), 1); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - } - - [Test] - public void ProductIdIsNullOrEmptyTest() - { - var productSupply = CreateDataModel(Guid.NewGuid().ToString(), null, 1); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - productSupply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - } - - [Test] - public void ProductIdIsNotGuidTest() - { - var productSupply = CreateDataModel(Guid.NewGuid().ToString(), "id", 1); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - } - - [Test] - public void CountIsLessThanOrEqualToZeroTest() - { - var productSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - productSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1); - Assert.That(() => productSupply.Validate(), Throws.TypeOf()); - } - - [Test] - public void AllFieldsAreCorrectTest() - { - var supplyId = Guid.NewGuid().ToString(); - var productId = Guid.NewGuid().ToString(); - var count = 5; - var productSupply = CreateDataModel(supplyId, productId, count); - Assert.That(() => productSupply.Validate(), Throws.Nothing); - Assert.Multiple(() => - { - Assert.That(productSupply.SupplyId, Is.EqualTo(supplyId)); - Assert.That(productSupply.ProductId, Is.EqualTo(productId)); - Assert.That(productSupply.Count, Is.EqualTo(count)); - }); - } - - private static ProductsInSupplyDataModel CreateDataModel(string? supplyId, string? productId, int count) => - new(supplyId, productId, count); - } - -} diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs index 699bb79..2290041 100644 --- a/NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/StorageDateModelTests.cs @@ -9,25 +9,29 @@ namespace NorthBridgeTest.DataModelsTests [Test] public void IdIsNullOrEmptyTest() { - var storage = CreateDataModel(null, "ул. Ленина, д. 10"); + var components = CreateComponents(); + var storage = CreateDataModel(null, "ул. Ленина, д. 10", components); Assert.That(() => storage.Validate(), Throws.TypeOf()); - storage = CreateDataModel(string.Empty, "ул. Ленина, д. 10"); + + storage = CreateDataModel(string.Empty, "ул. Ленина, д. 10", components); Assert.That(() => storage.Validate(), Throws.TypeOf()); } [Test] public void IdIsNotGuidTest() { - var storage = CreateDataModel("id", "ул. Ленина, д. 10"); + var storage = CreateDataModel("id", "ул. Ленина, д. 10", CreateComponents()); Assert.That(() => storage.Validate(), Throws.TypeOf()); } [Test] public void AddressIsNullOrEmptyTest() { - var storage = CreateDataModel(Guid.NewGuid().ToString(), null); + var components = CreateComponents(); + var storage = CreateDataModel(Guid.NewGuid().ToString(), null, components); Assert.That(() => storage.Validate(), Throws.TypeOf()); - storage = CreateDataModel(Guid.NewGuid().ToString(), string.Empty); + + storage = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, components); Assert.That(() => storage.Validate(), Throws.TypeOf()); } @@ -36,16 +40,27 @@ namespace NorthBridgeTest.DataModelsTests { var storageId = Guid.NewGuid().ToString(); var address = "ул. Ленина, д. 10"; - var storage = CreateDataModel(storageId, address); + var components = CreateComponents(); + + var storage = CreateDataModel(storageId, address, components); Assert.That(() => storage.Validate(), Throws.Nothing); + Assert.Multiple(() => { Assert.That(storage.Id, Is.EqualTo(storageId)); Assert.That(storage.Address, Is.EqualTo(address)); + Assert.That(storage.Components, Is.EquivalentTo(components)); }); } - private static StorageDataModel CreateDataModel(string? id, string? address) => - new(id, address); + private static StorageDataModel CreateDataModel(string? id, string? address, List components) => + new(id, address, true, components); + + private static List CreateComponents() => + new() + { + new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10), + new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 20) + }; } } diff --git a/NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs b/NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs index 8442ab8..6248be4 100644 --- a/NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs +++ b/NorthBridge/NorthBridgeTest/DataModelsTests/SupplyDataModelTests.cs @@ -9,32 +9,43 @@ namespace NorthBridgeTest.DataModelsTests [Test] public void IdIsNullOrEmptyTest() { - var supply = CreateDataModel(null, Guid.NewGuid().ToString(), DateTime.Now); + var components = CreateComponents(); + var supply = CreateDataModel(null, Guid.NewGuid().ToString(), DateTime.Now, components); Assert.That(() => supply.Validate(), Throws.TypeOf()); - supply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), DateTime.Now); + + supply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), DateTime.Now, components); Assert.That(() => supply.Validate(), Throws.TypeOf()); } [Test] public void IdIsNotGuidTest() { - var supply = CreateDataModel("id", Guid.NewGuid().ToString(), DateTime.Now); + var supply = CreateDataModel("id", Guid.NewGuid().ToString(), DateTime.Now, CreateComponents()); Assert.That(() => supply.Validate(), Throws.TypeOf()); } [Test] public void StorageIdIsNullOrEmptyTest() { - var supply = CreateDataModel(Guid.NewGuid().ToString(), null, DateTime.Now); + var components = CreateComponents(); + var supply = CreateDataModel(Guid.NewGuid().ToString(), null, DateTime.Now, components); Assert.That(() => supply.Validate(), Throws.TypeOf()); - supply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, DateTime.Now); + + supply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, DateTime.Now, components); Assert.That(() => supply.Validate(), Throws.TypeOf()); } [Test] public void StorageIdIsNotGuidTest() { - var supply = CreateDataModel(Guid.NewGuid().ToString(), "id", DateTime.Now); + var supply = CreateDataModel(Guid.NewGuid().ToString(), "id", DateTime.Now, CreateComponents()); + Assert.That(() => supply.Validate(), Throws.TypeOf()); + } + + [Test] + public void ComponentsListIsEmptyTest() + { + var supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.Now, new List()); Assert.That(() => supply.Validate(), Throws.TypeOf()); } @@ -44,17 +55,29 @@ namespace NorthBridgeTest.DataModelsTests var supplyId = Guid.NewGuid().ToString(); var storageId = Guid.NewGuid().ToString(); var date = DateTime.Now; - var supply = CreateDataModel(supplyId, storageId, date); + var components = CreateComponents(); + + var supply = CreateDataModel(supplyId, storageId, date, components); Assert.That(() => supply.Validate(), Throws.Nothing); + Assert.Multiple(() => { Assert.That(supply.Id, Is.EqualTo(supplyId)); Assert.That(supply.StorageId, Is.EqualTo(storageId)); Assert.That(supply.Date, Is.EqualTo(date)); + Assert.That(supply.Components, Is.EquivalentTo(components)); }); } - private static SupplyDataModel CreateDataModel(string? id, string? storageId, DateTime date) => - new(id, storageId, date); + private static SupplyDataModel CreateDataModel(string? id, string? storageId, DateTime date, List components) => + new(id, storageId, date, components); + + private static List CreateComponents() => + new() + { + new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5), + new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10) + }; + } }