Лабораторная работа 2. Усложненная
This commit is contained in:
parent
7ece3466ad
commit
44691c91e7
@ -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<ProductDataModel> 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);
|
||||
}
|
||||
|
||||
|
@ -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<ComponentOnStorageDataModel> 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<ComponentOnStorageDataModel> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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<SupplyDataModel> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -18,7 +18,7 @@ namespace NorthBridgeContract.BusinessLogicContracts
|
||||
|
||||
List<ComponentDataModel> GetComponentsByProductId(string productId);
|
||||
|
||||
void AddComponentToProduct(ComponentInProductDataModel componentInProduct);
|
||||
void AddComponentToProduct(ComponentOnStorageDataModel componentOnStorage, string productId);
|
||||
|
||||
void RemoveComponentFromProduct(string componentId, string productId);
|
||||
}
|
||||
|
@ -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<ComponentOnStorageDataModel> components);
|
||||
|
||||
void CloseStorage(string storageId);
|
||||
|
||||
void AddComponentToStorage(string storageId, ComponentOnStorageDataModel component);
|
||||
|
||||
void UpdateComponentCount(string storageId, string componentId, int newCount);
|
||||
|
||||
List<ComponentOnStorageDataModel> GetComponentsFromStorage(string storageId);
|
||||
|
||||
StorageDataModel GetStorageById(string storageId);
|
||||
}
|
||||
|
||||
}
|
@ -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<SupplyDataModel> GetSuppliesByStorageId(string storageId);
|
||||
|
||||
void AddOrUpdateComponentInSupply(ComponentInSupplyDataModel componentInSupply);
|
||||
|
||||
void UpdateComponentCountInSupply(string supplyId, string componentId, int newCount);
|
||||
|
||||
void RemoveComponentFromSupply(string supplyId, string componentId);
|
||||
}
|
||||
|
||||
}
|
@ -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)
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ComponentOnStorageDataModel> 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<ComponentOnStorageDataModel> Components { get; private set; } = components;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
|
@ -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<ComponentInSupplyDataModel> 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<ComponentInSupplyDataModel> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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") { }
|
||||
}
|
||||
}
|
@ -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<StorageDataModel> 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<ComponentOnStorageDataModel> GetComponentsOnStorage(string storageId);
|
||||
}
|
||||
|
||||
}
|
@ -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<SupplyDataModel> GetSuppliesByStorageId(string storageId);
|
||||
|
||||
void AddOrUpdateComponentInSupply(ComponentInSupplyDataModel componentInSupply);
|
||||
|
||||
void UpdateComponentCountInSupply(string supplyId, string componentId, int newCount);
|
||||
|
||||
void RemoveComponentFromSupply(string supplyId, string componentId);
|
||||
}
|
||||
|
||||
}
|
@ -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<IProductStorageContract> _productStorageContract;
|
||||
private Mock<IComponentStorageContract> _componentStorageContract;
|
||||
private Mock<ILogger> _logger;
|
||||
|
||||
[OneTimeSetUp]
|
||||
@ -19,13 +21,82 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests
|
||||
{
|
||||
_logger = new Mock<ILogger>();
|
||||
_productStorageContract = new Mock<IProductStorageContract>();
|
||||
_productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, _logger.Object);
|
||||
_componentStorageContract = new Mock<IComponentStorageContract>();
|
||||
|
||||
_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<ComponentInProductDataModel>()));
|
||||
|
||||
// Act
|
||||
_productBusinessLogicContract.AddComponentToProduct(componentOnStorage, productId);
|
||||
|
||||
// Assert
|
||||
_componentStorageContract.Verify(x => x.GetElementById(componentId), Times.Once);
|
||||
_productStorageContract.Verify(x => x.AddComponentToProduct(It.Is<ComponentInProductDataModel>(
|
||||
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<ArgumentNullException>(() => _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<ComponentInProductDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponentToProduct_NullComponentOnStorage_ThrowsException_Test()
|
||||
{
|
||||
// Arrange
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentNullException>(() => _productBusinessLogicContract.AddComponentToProduct(null!, productId));
|
||||
|
||||
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_productStorageContract.Verify(x => x.AddComponentToProduct(It.IsAny<ComponentInProductDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponentToProduct_InvalidComponentOnStorage_ThrowsValidationException_Test()
|
||||
{
|
||||
// Arrange
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
var componentOnStorage = new ComponentOnStorageDataModel("", "", 0); // Невалидные данные
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ValidationException>(() => _productBusinessLogicContract.AddComponentToProduct(componentOnStorage, productId));
|
||||
|
||||
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_productStorageContract.Verify(x => x.AddComponentToProduct(It.IsAny<ComponentInProductDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -33,16 +104,16 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests
|
||||
{
|
||||
// Arrange
|
||||
var components = new List<ComponentDataModel>
|
||||
{
|
||||
{
|
||||
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<ProductDataModel>
|
||||
{
|
||||
{
|
||||
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<bool>())).Returns(products);
|
||||
|
||||
@ -64,20 +135,20 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests
|
||||
{
|
||||
// Arrange
|
||||
var components = new List<ComponentDataModel>
|
||||
{
|
||||
{
|
||||
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<ProductDataModel>()));
|
||||
|
||||
// Act
|
||||
_productBusinessLogicContract.InsertProduct(product);
|
||||
|
||||
// Assert
|
||||
_productStorageContract.Verify(x => x.AddElement(It.Is<ProductDataModel>(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<ProductDataModel>(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<ComponentDataModel>
|
||||
{
|
||||
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<ProductDataModel>(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<ProductDataModel>(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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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<IStorageContract> _storageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_storageContract = new Mock<IStorageContract>();
|
||||
_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<ComponentOnStorageDataModel>
|
||||
{
|
||||
new ComponentOnStorageDataModel("storage1", "component1", 100)
|
||||
};
|
||||
|
||||
_storageContract.Setup(x => x.AddStorage(It.IsAny<StorageDataModel>())).Verifiable();
|
||||
|
||||
// Act
|
||||
_storageBusinessLogicContract.CreateStorage(storageId, address, isOpen, components);
|
||||
|
||||
// Assert
|
||||
_storageContract.Verify(x => x.AddStorage(It.Is<StorageDataModel>(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<ComponentOnStorageDataModel>
|
||||
{
|
||||
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<StorageDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CloseStorage_ValidStorage_ClosesStorage_Test()
|
||||
{
|
||||
// Arrange
|
||||
var storageId = Guid.NewGuid().ToString();
|
||||
var storage = new StorageDataModel(storageId, "ул. Пионерская д. 2В", true, new List<ComponentOnStorageDataModel>());
|
||||
_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<string>()), 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<ComponentOnStorageDataModel>());
|
||||
_storageContract.Setup(x => x.GetStorageById(storageId)).Returns(storage);
|
||||
_storageContract.Setup(x => x.AddOrUpdateComponentOnStorage(It.IsAny<ComponentOnStorageDataModel>())).Verifiable();
|
||||
|
||||
// Act
|
||||
_storageBusinessLogicContract.AddComponentToStorage(storageId, component);
|
||||
|
||||
// Assert
|
||||
_storageContract.Verify(x => x.AddOrUpdateComponentOnStorage(It.Is<ComponentOnStorageDataModel>(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<ComponentOnStorageDataModel>()), 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<ComponentOnStorageDataModel>
|
||||
{
|
||||
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<string>(), It.IsAny<string>(), It.IsAny<int>()), 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<ComponentOnStorageDataModel>
|
||||
{
|
||||
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<string>(), It.IsAny<string>(), It.IsAny<int>()), 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<ComponentOnStorageDataModel>());
|
||||
_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"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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<ISupplyStorageContract> _supplyStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_supplyStorageContract = new Mock<ISupplyStorageContract>();
|
||||
_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<ComponentInSupplyDataModel>
|
||||
{
|
||||
new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 100)
|
||||
});
|
||||
|
||||
_supplyStorageContract.Setup(x => x.AddSupply(It.IsAny<SupplyDataModel>())).Verifiable();
|
||||
|
||||
// Act
|
||||
_supplyBusinessLogicContract.CreateSupply(supply);
|
||||
|
||||
// Assert
|
||||
_supplyStorageContract.Verify(x => x.AddSupply(It.Is<SupplyDataModel>(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<ComponentInSupplyDataModel>());
|
||||
|
||||
// Act & Assert
|
||||
Assert.That(() => _supplyBusinessLogicContract.CreateSupply(supply),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_supplyStorageContract.Verify(x => x.AddSupply(It.IsAny<SupplyDataModel>()), 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<ComponentInSupplyDataModel>());
|
||||
_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<SupplyDataModel>
|
||||
{
|
||||
new SupplyDataModel(Guid.NewGuid().ToString(), storageId, DateTime.UtcNow, new List<ComponentInSupplyDataModel>())
|
||||
};
|
||||
_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<ComponentInSupplyDataModel>())).Verifiable();
|
||||
|
||||
// Act
|
||||
_supplyBusinessLogicContract.AddOrUpdateComponentInSupply(component);
|
||||
|
||||
// Assert
|
||||
_supplyStorageContract.Verify(x => x.AddOrUpdateComponentInSupply(It.Is<ComponentInSupplyDataModel>(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<ValidationException>());
|
||||
_supplyStorageContract.Verify(x => x.AddOrUpdateComponentInSupply(It.IsAny<ComponentInSupplyDataModel>()), 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<string>(), It.IsAny<string>(), It.IsAny<int>()), 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"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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<ValidationException>());
|
||||
componentSupply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => componentSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SupplyIdIsNotGuidTest()
|
||||
{
|
||||
var componentSupply = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => componentSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNullOrEmptyTest()
|
||||
{
|
||||
var componentSupply = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => componentSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
componentSupply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => componentSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNotGuidTest()
|
||||
{
|
||||
var componentSupply = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => componentSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessThanOrEqualToZeroTest()
|
||||
{
|
||||
var componentSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => componentSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
componentSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => componentSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
}
|
@ -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<ValidationException>());
|
||||
componentStorage = new ComponentOnStorageDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => componentStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StorageIdIsNotGuidTest()
|
||||
{
|
||||
var componentStorage = new ComponentOnStorageDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => componentStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNullOrEmptyTest()
|
||||
{
|
||||
var componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => componentStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => componentStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNotGuidTest()
|
||||
{
|
||||
var componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => componentStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessThanOrEqualToZeroTest()
|
||||
{
|
||||
var componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => componentStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
componentStorage = new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => componentStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[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));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ValidationException>());
|
||||
productStorage = new ProductOnStorageDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => productStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StorageIdIsNotGuidTest()
|
||||
{
|
||||
var productStorage = new ProductOnStorageDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => productStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNullOrEmptyTest()
|
||||
{
|
||||
var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => productStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => productStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNotGuidTest()
|
||||
{
|
||||
var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => productStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessThanOrEqualToZeroTest()
|
||||
{
|
||||
var productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => productStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
productStorage = new ProductOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => productStorage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[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));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ValidationException>());
|
||||
productSupply = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => productSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SupplyIdIsNotGuidTest()
|
||||
{
|
||||
var productSupply = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => productSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNullOrEmptyTest()
|
||||
{
|
||||
var productSupply = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => productSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
productSupply = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => productSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProductIdIsNotGuidTest()
|
||||
{
|
||||
var productSupply = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => productSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsLessThanOrEqualToZeroTest()
|
||||
{
|
||||
var productSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => productSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
productSupply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => productSupply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
}
|
@ -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<ValidationException>());
|
||||
storage = CreateDataModel(string.Empty, "ул. Ленина, д. 10");
|
||||
|
||||
storage = CreateDataModel(string.Empty, "ул. Ленина, д. 10", components);
|
||||
Assert.That(() => storage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var storage = CreateDataModel("id", "ул. Ленина, д. 10");
|
||||
var storage = CreateDataModel("id", "ул. Ленина, д. 10", CreateComponents());
|
||||
Assert.That(() => storage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[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<ValidationException>());
|
||||
storage = CreateDataModel(Guid.NewGuid().ToString(), string.Empty);
|
||||
|
||||
storage = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, components);
|
||||
Assert.That(() => storage.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
@ -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<ComponentOnStorageDataModel> components) =>
|
||||
new(id, address, true, components);
|
||||
|
||||
private static List<ComponentOnStorageDataModel> CreateComponents() =>
|
||||
new()
|
||||
{
|
||||
new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10),
|
||||
new ComponentOnStorageDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 20)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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<ValidationException>());
|
||||
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<ValidationException>());
|
||||
}
|
||||
|
||||
[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<ValidationException>());
|
||||
}
|
||||
|
||||
[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<ValidationException>());
|
||||
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<ValidationException>());
|
||||
}
|
||||
|
||||
[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<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ComponentsListIsEmptyTest()
|
||||
{
|
||||
var supply = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.Now, new List<ComponentInSupplyDataModel>());
|
||||
Assert.That(() => supply.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
@ -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<ComponentInSupplyDataModel> components) =>
|
||||
new(id, storageId, date, components);
|
||||
|
||||
private static List<ComponentInSupplyDataModel> CreateComponents() =>
|
||||
new()
|
||||
{
|
||||
new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5),
|
||||
new ComponentInSupplyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10)
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user