diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/BuyerBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/BuyerBusinessLogicContract.cs index 5774654..f1e0ff3 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/BuyerBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/BuyerBusinessLogicContract.cs @@ -11,31 +11,60 @@ namespace NorthBridgeBusinessLogic.Implementations; internal class BuyerBusinessLogicContract(IBuyerStorageContract buyerStorageContract, ILogger logger) : IBuyerBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly IBuyerStorageContract _buyerStorageContract = buyerStorageContract; + private readonly ILogger _logger = logger; + private readonly IBuyerStorageContract _buyerStorageContract = buyerStorageContract; - public List GetAllBuyers() - { - return new List(); - } + public List GetAllBuyers() + { + _logger.LogInformation("GetAllBuyers"); + return _buyerStorageContract.GetList() ?? throw new NullListException(); + } - public BuyerDataModel GetBuyerByData(string data) - { - return new BuyerDataModel("", "", ""); - } + public BuyerDataModel GetBuyerByData(string data) + { + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _buyerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + if (Regex.IsMatch(data, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$")) + { + return _buyerStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data); + } + return _buyerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data); + } - public void InsertBuyer(BuyerDataModel buyerDataModel) - { - - } + public void InsertBuyer(BuyerDataModel buyerDataModel) + { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(buyerDataModel)); + ArgumentNullException.ThrowIfNull(buyerDataModel); + buyerDataModel.Validate(); + _buyerStorageContract.AddElement(buyerDataModel); + } - public void UpdateBuyer(BuyerDataModel buyerDataModel) - { - - } + public void UpdateBuyer(BuyerDataModel buyerDataModel) + { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(buyerDataModel)); + ArgumentNullException.ThrowIfNull(buyerDataModel); + buyerDataModel.Validate(); + _buyerStorageContract.UpdElement(buyerDataModel); + } - public void DeleteBuyer(string id) - { - - } + public void DeleteBuyer(string id) + { + _logger.LogInformation("Delete by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + _buyerStorageContract.DelElement(id); + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ComponentBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ComponentBusinessLogicContract.cs index 5d3a3ea..493c118 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ComponentBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ComponentBusinessLogicContract.cs @@ -3,7 +3,9 @@ using NorthBridgeContract.BusinessLogicsContracts; using NorthBridgeContract.DataModels; using NorthBridgeContract.Enums; using NorthBridgeContract.Exceptions; +using NorthBridgeContract.Extensions; using NorthBridgeContract.StoragesContracts; +using System.Text.Json; namespace NorthBridgeBusinessLogic.Implementations { @@ -14,30 +16,67 @@ namespace NorthBridgeBusinessLogic.Implementations public List GetAllComponents(bool onlyActive) { - return []; + _logger.LogInformation("GetAllComponents, onlyActive: {onlyActive}", onlyActive); + return _componentStorageContract.GetList(onlyActive) ?? throw new NullListException(); } - public ComponentDataModel? GetComponentById(string id) + public ComponentDataModel GetComponentById(string id) { - return new ComponentDataModel("", "", ComponentType.None, "", 0, false); + _logger.LogInformation("GetComponentById: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + return _componentStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id); } - public ComponentDataModel? GetComponentByName(string name) + public ComponentDataModel GetComponentByName(string name) { - return new ComponentDataModel("", "", ComponentType.None, "", 0, false); + _logger.LogInformation("GetComponentByName: {name}", name); + if (name.IsEmpty()) + { + throw new ArgumentNullException(nameof(name)); + } + return _componentStorageContract.GetElementByName(name) ?? throw new ElementNotFoundException(name); + } + + public ComponentDataModel GetComponentByData(string data) + { + _logger.LogInformation("GetComponentByData: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return GetComponentById(data); + } + return GetComponentByName(data); } public void InsertComponent(ComponentDataModel componentDataModel) { + _logger.LogInformation("InsertComponent: {json}", JsonSerializer.Serialize(componentDataModel)); + ArgumentNullException.ThrowIfNull(componentDataModel); componentDataModel.Validate(); _componentStorageContract.AddElement(componentDataModel); } public void UpdateComponent(ComponentDataModel componentDataModel) { + _logger.LogInformation("UpdateComponent: {json}", JsonSerializer.Serialize(componentDataModel)); + ArgumentNullException.ThrowIfNull(componentDataModel); + componentDataModel.Validate(); + var existingComponent = _componentStorageContract.GetElementById(componentDataModel.Id); if (existingComponent == null) - throw new ValidationException("Component not found"); + { + throw new ElementNotFoundException(componentDataModel.Id); + } if (existingComponent.Price != componentDataModel.Price) { @@ -46,29 +85,52 @@ namespace NorthBridgeBusinessLogic.Implementations _componentStorageContract.AddComponentHistory(historyEntry); } - componentDataModel.Validate(); _componentStorageContract.UpdElement(componentDataModel); } public void DeleteComponent(string id) { + _logger.LogInformation("DeleteComponent by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } _componentStorageContract.DelElement(id); } public void RestoreComponent(string id) { + _logger.LogInformation("RestoreComponent by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } _componentStorageContract.ResElement(id); } public List GetComponentHistory(string componentId) { - return _componentStorageContract.GetComponentHistory(componentId); - } - - public ComponentDataModel GetComponentByData(string data) - { - return new ComponentDataModel("", "", ComponentType.None, "", 0, false); + _logger.LogInformation("GetComponentHistory for componentId: {componentId}", componentId); + if (componentId.IsEmpty()) + { + throw new ArgumentNullException(nameof(componentId)); + } + if (!componentId.IsGuid()) + { + throw new ValidationException("ComponentId is not a unique identifier"); + } + return _componentStorageContract.GetComponentHistory(componentId) ?? []; } } + + } diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ManufacturerBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ManufacturerBusinessLogicContract.cs index 90efdc4..eb2b041 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ManufacturerBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ManufacturerBusinessLogicContract.cs @@ -10,31 +10,57 @@ namespace NorthBridgeBusinessLogic.Implementations; internal class ManufacturerBusinessLogicContract(IManufacturerStorageContract manufacturerStorageContract, ILogger logger) : IManufacturerBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly IManufacturerStorageContract _manufacturerStorageContract = manufacturerStorageContract; + private readonly ILogger _logger = logger; + private readonly IManufacturerStorageContract _manufacturerStorageContract = manufacturerStorageContract; public List GetAllManufacturers() - { - return []; + { + _logger.LogInformation("GetAllManufacturers"); + return _manufacturerStorageContract.GetList() ?? throw new NullListException(); } public ManufacturerDataModel GetManufacturerByData(string data) { - return new("", "", "", "'"); + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _manufacturerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _manufacturerStorageContract.GetElementByName(data) ?? _manufacturerStorageContract.GetElementByOldName(data) ?? + throw new ElementNotFoundException(data); } public void InsertManufacturer(ManufacturerDataModel manufacturerDataModel) - { - - } + { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(manufacturerDataModel)); + ArgumentNullException.ThrowIfNull(manufacturerDataModel); + manufacturerDataModel.Validate(); + _manufacturerStorageContract.AddElement(manufacturerDataModel); + } - public void UpdateManufacturer(ManufacturerDataModel manufacturerDataModel) - { - - } + public void UpdateManufacturer(ManufacturerDataModel manufacturerDataModel) + { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(manufacturerDataModel)); + ArgumentNullException.ThrowIfNull(manufacturerDataModel); + manufacturerDataModel.Validate(); + _manufacturerStorageContract.UpdElement(manufacturerDataModel); + } - public void DeleteManufacturer(string id) - { - - } + public void DeleteManufacturer(string id) + { + _logger.LogInformation("Delete by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + _manufacturerStorageContract.DelElement(id); + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/PostBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/PostBusinessLogicContract.cs index b40572f..3d33dc5 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/PostBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/PostBusinessLogicContract.cs @@ -11,41 +11,84 @@ namespace NorthBridgeBusinessLogic.Implementations; internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly IPostStorageContract _postStorageContract = postStorageContract; + private readonly ILogger _logger = logger; + private readonly IPostStorageContract _postStorageContract = postStorageContract; - public List GetAllPosts(bool onlyActive = true) - { - return new List(); - } + public List GetAllPosts(bool onlyActive = true) + { + _logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive); + return _postStorageContract.GetList(onlyActive) ?? throw new NullListException(); + } - public List GetAllDataOfPost(string postId) - { - return new List(); - } + public List GetAllDataOfPost(string postId) + { + _logger.LogInformation("GetAllDataOfPost for {postId}", postId); + if (postId.IsEmpty()) + { + throw new ArgumentNullException(nameof(postId)); + } + if (!postId.IsGuid()) + { + throw new ValidationException("The value in the field postId is not a unique identifier."); + } + return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException(); + } - public PostDataModel GetPostByData(string data) - { - return new PostDataModel("", "", PostType.None, 0, false, DateTime.Now); - } + public PostDataModel GetPostByData(string data) + { + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); + } - public void InsertPost(PostDataModel postDataModel) - { - - } + public void InsertPost(PostDataModel postDataModel) + { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel)); + ArgumentNullException.ThrowIfNull(postDataModel); + postDataModel.Validate(); + _postStorageContract.AddElement(postDataModel); + } - public void UpdatePost(PostDataModel postDataModel) - { - - } + public void UpdatePost(PostDataModel postDataModel) + { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(postDataModel)); + ArgumentNullException.ThrowIfNull(postDataModel); + postDataModel.Validate(); + _postStorageContract.UpdElement(postDataModel); + } - public void DeletePost(string id) - { - - } + public void DeletePost(string id) + { + _logger.LogInformation("Delete by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + _postStorageContract.DelElement(id); + } - public void RestorePost(string id) - { - - } + public void RestorePost(string id) + { + _logger.LogInformation("Restore by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + _postStorageContract.ResElement(id); + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs index 19715ad..1481e44 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/ProductBusinessLogicContract.cs @@ -10,52 +10,107 @@ namespace NorthBridgeBusinessLogic.Implementations; internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly IProductStorageContract _productStorageContract = productStorageContract; + private readonly ILogger _logger = logger; + private readonly IProductStorageContract _productStorageContract = productStorageContract; + public List GetAllProducts(bool onlyActive) + { + _logger.LogInformation("Getting all products (onlyActive: {onlyActive})", onlyActive); + var products = _productStorageContract.GetList(onlyActive); + return products ?? throw new NullListException(); + } - public List GetAllProducts(bool onlyActive) - { - return new List(); - } + public List GetAllProductsByManufacturer(string manufacturerId, bool onlyActive = true) + { + _logger.LogInformation("Getting all products for manufacturer {manufacturerId} (onlyActive: {onlyActive})", manufacturerId, onlyActive); + if (manufacturerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(manufacturerId)); + } + var products = _productStorageContract.GetListByManufacturer(manufacturerId, onlyActive); + return products ?? throw new NullListException(); + } - public List GetAllProductsByManufacturer(string manufacturerId, bool onlyActive = true) - { - return new List(); - } + public ProductDataModel GetProductByData(string data) + { + _logger.LogInformation("Getting product by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } - public ProductDataModel GetProductByData(string data) - { - return new ProductDataModel("", "", "", false, new List()); - } + if (data.IsGuid()) + { + return _productStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } - public void InsertProduct(ProductDataModel productDataModel) - { - - } + return _productStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data); + } - public void UpdateProduct(ProductDataModel productDataModel) - { - - } + public void InsertProduct(ProductDataModel productDataModel) + { + _logger.LogInformation("Inserting new product: {json}", JsonSerializer.Serialize(productDataModel)); + ArgumentNullException.ThrowIfNull(productDataModel); + productDataModel.Validate(); + _productStorageContract.AddElement(productDataModel); + } - public void DeleteProduct(string id) - { - - } + public void UpdateProduct(ProductDataModel productDataModel) + { + _logger.LogInformation("Updating product: {json}", JsonSerializer.Serialize(productDataModel)); + ArgumentNullException.ThrowIfNull(productDataModel); + productDataModel.Validate(); + + var existingProduct = _productStorageContract.GetElementById(productDataModel.Id); + if (existingProduct == null) + { + throw new ValidationException("Product not found"); + } + + _productStorageContract.UpdElement(productDataModel); + } + + public void DeleteProduct(string id) + { + _logger.LogInformation("Deleting product with id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a valid GUID"); + } + _productStorageContract.DelElement(id); + } public List GetComponentsByProductId(string productId) { - return new List(); - } + _logger.LogInformation("Getting components for product {productId}", productId); + if (productId.IsEmpty()) + { + throw new ArgumentNullException(nameof(productId)); + } - public void AddComponentToProduct(ComponentInProductDataModel componentInProduct) - { - - } - - public void RemoveComponentFromProduct(string componentId, string productId) - { - + var components = _productStorageContract.GetComponentsByProductId(productId); + return components ?? throw new NullListException(); } -} \ No newline at end of file + + public void AddComponentToProduct(ComponentInProductDataModel componentInProduct) + { + _logger.LogInformation("Adding component to product: {json}", JsonSerializer.Serialize(componentInProduct)); + ArgumentNullException.ThrowIfNull(componentInProduct); + componentInProduct.Validate(); + _productStorageContract.AddComponentToProduct(componentInProduct); + } + + public void RemoveComponentFromProduct(string componentId, string productId) + { + _logger.LogInformation("Removing component {componentId} from product {productId}", componentId, productId); + if (componentId.IsEmpty() || productId.IsEmpty()) + { + throw new ArgumentNullException("ComponentId or ProductId cannot be null or empty"); + } + _productStorageContract.RemoveComponentFromProduct(componentId, productId); + } +} diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/SalaryBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/SalaryBusinessLogicContract.cs index 807f0f4..7207c61 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/SalaryBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/SalaryBusinessLogicContract.cs @@ -6,29 +6,58 @@ using NorthBridgeContract.StoragesContracts; using Microsoft.Extensions.Logging; namespace NorthBridgeBusinessLogic.Implementations; - internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, - ISaleStorageContract saleStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract + ISaleStorageContract saleStorageContract, IPostStorageContract postStorageContract, IWorkerStorageContract workerStorageContract, ILogger logger) : ISalaryBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; - private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; - private readonly IPostStorageContract _postStorageContract = postStorageContract; - private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; + private readonly ILogger _logger = logger; + private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; + private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; + private readonly IPostStorageContract _postStorageContract = postStorageContract; + private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; - public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) - { - return new List(); - } + public List GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); + } - public List GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId) - { - return new List(); - } + public List GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId) + { + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (workerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(workerId)); + } + if (!workerId.IsGuid()) + { + throw new ValidationException("The value in the field workerId is not a unique identifier."); + } + _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {workerId}", fromDate, toDate, workerId); + return _salaryStorageContract.GetList(fromDate, toDate, workerId) ?? throw new NullListException(); + } - public void CalculateSalaryByMounth(DateTime date) - { - - - } + public void CalculateSalaryByMounth(DateTime date) + { + _logger.LogInformation("CalculateSalaryByMounth: {date}", date); + var startDate = new DateTime(date.Year, date.Month, 1); + var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); + var workers = _workerStorageContract.GetList() ?? throw new NullListException(); + foreach (var worker in workers) + { + var sales = _saleStorageContract.GetList(startDate, finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ?? + throw new NullListException(); + var post = _postStorageContract.GetElementById(worker.PostId) ?? + throw new NullListException(); + var salary = post.Salary + sales * 0.1; + _logger.LogDebug("The employee {workerId} was paid a salary of {salary}", worker.Id, salary); + _salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary)); + } + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/SaleBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/SaleBusinessLogicContract.cs index 8bb4fec..b2ed102 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/SaleBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/SaleBusinessLogicContract.cs @@ -6,45 +6,108 @@ using NorthBridgeContract.StoragesContracts; using Microsoft.Extensions.Logging; using System.Text.Json; -namespace NorthBridgeBusinessLogic.Implementations; - internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; + private readonly ILogger _logger = logger; + private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; - public List GetAllSalesByPeriod(DateTime fromDate, DateTime toDate) - { - return new List(); - } + public List GetAllSalesByPeriod(DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _saleStorageContract.GetList(fromDate, toDate) ?? throw new NullListException(); + } - public List GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) - { - return new List(); + public List GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) + { + _logger.LogInformation("GetAllSales params: {workerId}, {fromDate}, {toDate}", workerId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (workerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(workerId)); + } + if (!workerId.IsGuid()) + { + throw new ValidationException("The value in the field workerId is not a unique identifier."); + } + return _saleStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException(); } public List GetAllSalesByBuyerByPeriod(string buyerId, DateTime fromDate, DateTime toDate) - { - return new List(); + { + _logger.LogInformation("GetAllSales params: {buyerId}, {fromDate}, {toDate}", buyerId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (buyerId.IsEmpty()) + { + throw new ArgumentNullException(nameof(buyerId)); + } + if (!buyerId.IsGuid()) + { + throw new ValidationException("The value in the field buyerId is not a unique identifier."); + } + return _saleStorageContract.GetList(fromDate, toDate, buyerId: buyerId) ?? throw new NullListException(); } public List GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate) - { - return new List(); + { + _logger.LogInformation("GetAllSales params: {productId}, {fromDate}, {toDate}", productId, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + if (productId.IsEmpty()) + { + throw new ArgumentNullException(nameof(productId)); + } + if (!productId.IsGuid()) + { + throw new ValidationException("The value in the field productId is not a unique identifier."); + } + return _saleStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException(); } public SaleDataModel GetSaleByData(string data) - { - return new SaleDataModel("", "", "", 0, false, new List()); - } + { + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (!data.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + return _saleStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } - public void InsertSale(SaleDataModel saleDataModel) - { - - } + public void InsertSale(SaleDataModel saleDataModel) + { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(saleDataModel)); + ArgumentNullException.ThrowIfNull(saleDataModel); + saleDataModel.Validate(); + _saleStorageContract.AddElement(saleDataModel); + } - public void CancelSale(string id) - { - - } + public void CancelSale(string id) + { + _logger.LogInformation("Cancel by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + _saleStorageContract.DelElement(id); + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeBusinessLogic/Implementations/WorkerBusinessLogicContract.cs b/NorthBridge/NorthBridgeBusinessLogic/Implementations/WorkerBusinessLogicContract.cs index baaf422..be6fd13 100644 --- a/NorthBridge/NorthBridgeBusinessLogic/Implementations/WorkerBusinessLogicContract.cs +++ b/NorthBridge/NorthBridgeBusinessLogic/Implementations/WorkerBusinessLogicContract.cs @@ -11,46 +11,90 @@ namespace NorthBridgeBusinessLogic.Implementations; internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract { - private readonly ILogger _logger = logger; - private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; + private readonly ILogger _logger = logger; + private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; - public List GetAllWorkers(bool onlyActive = true) - { - return new List(); - } + public List GetAllWorkers(bool onlyActive = true) + { + _logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive); + return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException(); + } - public List GetAllWorkersByPost(string postId, bool onlyActive = true) - { - return new List(); - } + public List GetAllWorkersByPost(string postId, bool onlyActive = true) + { + _logger.LogInformation("GetAllWorkers params: {postId}, {onlyActive},", postId, onlyActive); + if (postId.IsEmpty()) + { + throw new ArgumentNullException(nameof(postId)); + } + if (!postId.IsGuid()) + { + throw new ValidationException("The value in the field postId is not a unique identifier."); + } + return _workerStorageContract.GetList(onlyActive, postId) ?? throw new NullListException(); + } - public List GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) - { - return new List(); - } + public List GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) + { + _logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException(); + } - public List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) - { - return new List(); - } + public List GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) + { + _logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate); + if (fromDate.IsDateNotOlder(toDate)) + { + throw new IncorrectDatesException(fromDate, toDate); + } + return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException(); + } - public WorkerDataModel GetWorkerByData(string data) - { - return new WorkerDataModel("", "", "", DateTime.Now, DateTime.Now, false); - } + public WorkerDataModel GetWorkerByData(string data) + { + _logger.LogInformation("Get element by data: {data}", data); + if (data.IsEmpty()) + { + throw new ArgumentNullException(nameof(data)); + } + if (data.IsGuid()) + { + return _workerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data); + } + return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data); + } - public void InsertWorker(WorkerDataModel workerDataModel) - { - - } + public void InsertWorker(WorkerDataModel workerDataModel) + { + _logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel)); + ArgumentNullException.ThrowIfNull(workerDataModel); + workerDataModel.Validate(); + _workerStorageContract.AddElement(workerDataModel); + } - public void UpdateWorker(WorkerDataModel workerDataModel) - { - - } + public void UpdateWorker(WorkerDataModel workerDataModel) + { + _logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workerDataModel)); + ArgumentNullException.ThrowIfNull(workerDataModel); + workerDataModel.Validate(); + _workerStorageContract.UpdElement(workerDataModel); + } - public void DeleteWorker(string id) - { - - } + public void DeleteWorker(string id) + { + _logger.LogInformation("Delete by id: {id}", id); + if (id.IsEmpty()) + { + throw new ArgumentNullException(nameof(id)); + } + if (!id.IsGuid()) + { + throw new ValidationException("Id is not a unique identifier"); + } + _workerStorageContract.DelElement(id); + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeContract/Extensions/DateTimeExtensions.cs b/NorthBridge/NorthBridgeContract/Extensions/DateTimeExtensions.cs new file mode 100644 index 0000000..71be1ba --- /dev/null +++ b/NorthBridge/NorthBridgeContract/Extensions/DateTimeExtensions.cs @@ -0,0 +1,9 @@ +namespace NorthBridgeContract.Extensions; + +public static class DateTimeExtensions +{ + public static bool IsDateNotOlder(this DateTime date, DateTime olderDate) + { + return date >= olderDate; + } +} \ No newline at end of file diff --git a/NorthBridge/NorthBridgeContract/StoragesContracts/IProductStorageContract.cs b/NorthBridge/NorthBridgeContract/StoragesContracts/IProductStorageContract.cs index 320bb75..4cf9968 100644 --- a/NorthBridge/NorthBridgeContract/StoragesContracts/IProductStorageContract.cs +++ b/NorthBridge/NorthBridgeContract/StoragesContracts/IProductStorageContract.cs @@ -4,19 +4,23 @@ namespace NorthBridgeContract.StoragesContracts; public interface IProductStorageContract { - List GetList(bool onlyActual = true); + List GetList(bool onlyActive); - List GetPostWithHistory(string productId); + List GetListByManufacturer(string manufacturerId, bool onlyActive); ProductDataModel? GetElementById(string id); ProductDataModel? GetElementByName(string name); - void AddElement(ProductDataModel productDataModel); + void AddElement(ProductDataModel productDataModel); - void UpdElement(ProductDataModel productDataModel); + void UpdElement(ProductDataModel productDataModel); - void DelElement(string id); + void DelElement(string id); - void ResElement(string id); + List GetComponentsByProductId(string productId); + + void AddComponentToProduct(ComponentInProductDataModel componentInProduct); + + void RemoveComponentFromProduct(string componentId, string productId); } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/BuyerBusinessLogicContractTests.cs b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/BuyerBusinessLogicContractTests.cs index aa48949..f61600b 100644 --- a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/BuyerBusinessLogicContractTests.cs +++ b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/BuyerBusinessLogicContractTests.cs @@ -10,341 +10,341 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests; [TestFixture] internal class BuyerBusinessLogicContractTests { - private BuyerBusinessLogicContract _buyerBusinessLogicContract; - private Mock _buyerStorageContract; + private BuyerBusinessLogicContract _buyerBusinessLogicContract; + private Mock _buyerStorageContract; - [OneTimeSetUp] - public void OneTimeSetUp() - { - _buyerStorageContract = new Mock(); - _buyerBusinessLogicContract = new BuyerBusinessLogicContract(_buyerStorageContract.Object, new Mock().Object); - } + [OneTimeSetUp] + public void OneTimeSetUp() + { + _buyerStorageContract = new Mock(); + _buyerBusinessLogicContract = new BuyerBusinessLogicContract(_buyerStorageContract.Object, new Mock().Object); + } - [SetUp] - public void SetUp() - { - _buyerStorageContract.Reset(); - } + [SetUp] + public void SetUp() + { + _buyerStorageContract.Reset(); + } - [Test] - public void GetAllBuyers_ReturnListOfRecords_Test() - { - //Arrange - var listOriginal = new List() - { - new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11"), - new(Guid.NewGuid().ToString(), "fio 2", "+7-555-444-33-23"), - new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777"), - }; - _buyerStorageContract.Setup(x => x.GetList()).Returns(listOriginal); - //Act - var list = _buyerBusinessLogicContract.GetAllBuyers(); - //Assert - Assert.That(list, Is.Not.Null); - Assert.That(list, Is.EquivalentTo(listOriginal)); - } + [Test] + public void GetAllBuyers_ReturnListOfRecords_Test() + { + //Arrange + var listOriginal = new List() + { + new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11"), + new(Guid.NewGuid().ToString(), "fio 2", "+7-555-444-33-23"), + new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777"), + }; + _buyerStorageContract.Setup(x => x.GetList()).Returns(listOriginal); + //Act + var list = _buyerBusinessLogicContract.GetAllBuyers(); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Is.EquivalentTo(listOriginal)); + } - [Test] - public void GetAllBuyers_ReturnEmptyList_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.GetList()).Returns([]); - //Act - var list = _buyerBusinessLogicContract.GetAllBuyers(); - //Assert - Assert.That(list, Is.Not.Null); - Assert.That(list, Has.Count.EqualTo(0)); - _buyerStorageContract.Verify(x => x.GetList(), Times.Once); - } + [Test] + public void GetAllBuyers_ReturnEmptyList_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.GetList()).Returns([]); + //Act + var list = _buyerBusinessLogicContract.GetAllBuyers(); + //Assert + Assert.That(list, Is.Not.Null); + Assert.That(list, Has.Count.EqualTo(0)); + _buyerStorageContract.Verify(x => x.GetList(), Times.Once); + } - [Test] - public void GetAllBuyers_ReturnNull_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.GetList(), Times.Once); - } + [Test] + public void GetAllBuyers_ReturnNull_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetList(), Times.Once); + } - [Test] - public void GetAllBuyers_StorageThrowError_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException())); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.GetList(), Times.Once); - } + [Test] + public void GetAllBuyers_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetList(), Times.Once); + } - [Test] - public void GetBuyerByData_GetById_ReturnRecord_Test() - { - //Arrange - var id = Guid.NewGuid().ToString(); - var record = new BuyerDataModel(id, "fio", "+7-111-111-11-11"); - _buyerStorageContract.Setup(x => x.GetElementById(id)).Returns(record); - //Act - var element = _buyerBusinessLogicContract.GetBuyerByData(id); - //Assert - Assert.That(element, Is.Not.Null); - Assert.That(element.Id, Is.EqualTo(id)); - _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); - } + [Test] + public void GetBuyerByData_GetById_ReturnRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var record = new BuyerDataModel(id, "Иванов Иван Иванович", "+7-111-111-11-11"); + _buyerStorageContract.Setup(x => x.GetElementById(id)).Returns(record); + //Act + var element = _buyerBusinessLogicContract.GetBuyerByData(id); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.Id, Is.EqualTo(id)); + _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + } - [Test] - public void GetBuyerByData_GetByFio_ReturnRecord_Test() - { - //Arrange - var fio = "fio"; - var record = new BuyerDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11"); - _buyerStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record); - //Act - var element = _buyerBusinessLogicContract.GetBuyerByData(fio); - //Assert - Assert.That(element, Is.Not.Null); - Assert.That(element.FIO, Is.EqualTo(fio)); - _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); - } + [Test] + public void GetBuyerByData_GetByFio_ReturnRecord_Test() + { + //Arrange + var fio = "Иванов Иван Иванович"; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11"); + _buyerStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record); + //Act + var element = _buyerBusinessLogicContract.GetBuyerByData(fio); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.FIO, Is.EqualTo(fio)); + _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); + } - [Test] - public void GetBuyerByData_GetByPhoneNumber_ReturnRecord_Test() - { - //Arrange - var phoneNumber = "+7-111-111-11-11"; - var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio", phoneNumber); - _buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record); - //Act - var element = _buyerBusinessLogicContract.GetBuyerByData(phoneNumber); - //Assert - Assert.That(element, Is.Not.Null); - Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber)); - _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); - } + [Test] + public void GetBuyerByData_GetByPhoneNumber_ReturnRecord_Test() + { + //Arrange + var phoneNumber = "+7-111-111-11-11"; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Иванов Иван Иванович", phoneNumber); + _buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record); + //Act + var element = _buyerBusinessLogicContract.GetBuyerByData(phoneNumber); + //Assert + Assert.That(element, Is.Not.Null); + Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber)); + _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); + } - [Test] - public void GetBuyerByData_EmptyData_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(null), Throws.TypeOf()); - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(string.Empty), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); - _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); - _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); - } + [Test] + public void GetBuyerByData_EmptyData_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(null), Throws.TypeOf()); + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(string.Empty), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + } - [Test] - public void GetBuyerByData_GetById_NotFoundRecord_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); - _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); - _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); - } + [Test] + public void GetBuyerByData_GetById_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + } - [Test] - public void GetBuyerByData_GetByFio_NotFoundRecord_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("fio"), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); - _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); - _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); - } + [Test] + public void GetBuyerByData_GetByFio_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("Иванов Иван Иванович"), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); + } - [Test] - public void GetBuyerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); - _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); - _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); - } + [Test] + public void GetBuyerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); + _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); + } - [Test] - public void GetBuyerByData_StorageThrowError_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); - _buyerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); - _buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("fio"), Throws.TypeOf()); - Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); - _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); - _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); - } + [Test] + public void GetBuyerByData_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _buyerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + _buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("Иванов Иван Иванович"), Throws.TypeOf()); + Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); + _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); + } - [Test] - public void InsertBuyer_CorrectRecord_Test() - { - //Arrange - var flag = false; - var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11"); - _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())) - .Callback((BuyerDataModel x) => - { - flag = x.Id == record.Id && x.FIO == record.FIO && - x.PhoneNumber == record.PhoneNumber; - }); - //Act - _buyerBusinessLogicContract.InsertBuyer(record); - //Assert - _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); - Assert.That(flag); - } + [Test] + public void InsertBuyer_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11"); + _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())) + .Callback((BuyerDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && + x.PhoneNumber == record.PhoneNumber; + }); + //Act + _buyerBusinessLogicContract.InsertBuyer(record); + //Assert + _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + Assert.That(flag); + } - [Test] - public void InsertBuyer_RecordWithExistsData_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); - } + [Test] + public void InsertBuyer_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } - [Test] - public void InsertBuyer_NullRecord_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(null), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); - } + [Test] + public void InsertBuyer_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(null), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } - [Test] - public void InsertBuyer_InvalidRecord_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new BuyerDataModel("id", "fio", "+7-111-111-11-11")), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); - } + [Test] + public void InsertBuyer_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new BuyerDataModel("id", "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); + } - [Test] - public void InsertBuyer_StorageThrowError_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); - } + [Test] + public void InsertBuyer_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); + } - [Test] - public void UpdateBuyer_CorrectRecord_Test() - { - //Arrange - var flag = false; - var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11"); - _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())) - .Callback((BuyerDataModel x) => - { - flag = x.Id == record.Id && x.FIO == record.FIO && - x.PhoneNumber == record.PhoneNumber; - }); - //Act - _buyerBusinessLogicContract.UpdateBuyer(record); - //Assert - _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); - Assert.That(flag); - } + [Test] + public void UpdateBuyer_CorrectRecord_Test() + { + //Arrange + var flag = false; + var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11"); + _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())) + .Callback((BuyerDataModel x) => + { + flag = x.Id == record.Id && x.FIO == record.FIO && + x.PhoneNumber == record.PhoneNumber; + }); + //Act + _buyerBusinessLogicContract.UpdateBuyer(record); + //Assert + _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + Assert.That(flag); + } - [Test] - public void UpdateBuyer_RecordWithIncorrectData_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); - } + [Test] + public void UpdateBuyer_RecordWithIncorrectData_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } - [Test] - public void UpdateBuyer_RecordWithExistsData_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); - } + [Test] + public void UpdateBuyer_RecordWithExistsData_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } - [Test] - public void UpdateBuyer_NullRecord_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(null), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); - } + [Test] + public void UpdateBuyer_NullRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(null), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } - [Test] - public void UpdateBuyer_InvalidRecord_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new BuyerDataModel("id", "fio", "+7-111-111-11-11")), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); - } + [Test] + public void UpdateBuyer_InvalidRecord_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new BuyerDataModel("id", "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); + } - [Test] - public void UpdateBuyer_StorageThrowError_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); - } + [Test] + public void UpdateBuyer_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + } - [Test] - public void DeleteBuyer_CorrectRecord_Test() - { - //Arrange - var id = Guid.NewGuid().ToString(); - var flag = false; - _buyerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); - //Act - _buyerBusinessLogicContract.DeleteBuyer(id); - //Assert - _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); - Assert.That(flag); - } + [Test] + public void DeleteBuyer_CorrectRecord_Test() + { + //Arrange + var id = Guid.NewGuid().ToString(); + var flag = false; + _buyerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); + //Act + _buyerBusinessLogicContract.DeleteBuyer(id); + //Assert + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + Assert.That(flag); + } - [Test] - public void DeleteBuyer_RecordWithIncorrectId_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException("")); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); - } + [Test] + public void DeleteBuyer_RecordWithIncorrectId_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException("")); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } - [Test] - public void DeleteBuyer_IdIsNullOrEmpty_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(null), Throws.TypeOf()); - Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(string.Empty), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); - } + [Test] + public void DeleteBuyer_IdIsNullOrEmpty_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(null), Throws.TypeOf()); + Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(string.Empty), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } - [Test] - public void DeleteBuyer_IdIsNotGuid_ThrowException_Test() - { - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer("id"), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); - } + [Test] + public void DeleteBuyer_IdIsNotGuid_ThrowException_Test() + { + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer("id"), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } - [Test] - public void DeleteBuyer_StorageThrowError_ThrowException_Test() - { - //Arrange - _buyerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); - //Act&Assert - Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf()); - _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); - } + [Test] + public void DeleteBuyer_StorageThrowError_ThrowException_Test() + { + //Arrange + _buyerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); + //Act&Assert + Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf()); + _buyerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); + } } \ No newline at end of file diff --git a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ComponentBusinessLogicTests.cs b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ComponentBusinessLogicTests.cs index f054082..10b965e 100644 --- a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ComponentBusinessLogicTests.cs +++ b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ComponentBusinessLogicTests.cs @@ -140,21 +140,31 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests [Test] public void UpdateComponent_CorrectRecord_Test() { + // Arrange var flag = false; - var record = new ComponentDataModel(Guid.NewGuid().ToString(), "component name", ComponentType.Processor, Guid.NewGuid().ToString(), 150, false); + var existingRecord = new ComponentDataModel(Guid.NewGuid().ToString(), "component name", ComponentType.Processor, Guid.NewGuid().ToString(), 140, true); + var updatedRecord = new ComponentDataModel(existingRecord.Id, existingRecord.ComponentName, existingRecord.ComponentType, existingRecord.ManufacturerId, 150, true); + + _componentStorageContract.Setup(x => x.GetElementById(existingRecord.Id)).Returns(existingRecord); _componentStorageContract.Setup(x => x.UpdElement(It.IsAny())) .Callback((ComponentDataModel x) => { - flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType && - x.ManufacturerId == record.ManufacturerId && x.Price == record.Price && x.IsDeleted == record.IsDeleted; + flag = x.Id == updatedRecord.Id && x.ComponentName == updatedRecord.ComponentName && + x.ComponentType == updatedRecord.ComponentType && x.ManufacturerId == updatedRecord.ManufacturerId && + x.Price == updatedRecord.Price && x.IsDeleted == updatedRecord.IsDeleted; }); - _componentBusinessLogicContract.UpdateComponent(record); + // Act + _componentBusinessLogicContract.UpdateComponent(updatedRecord); + // Assert + _componentStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _componentStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); + _componentStorageContract.Verify(x => x.AddComponentHistory(It.IsAny()), Times.Once); Assert.That(flag); } + [Test] public void DeleteComponent_CorrectRecord_Test() { diff --git a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs index fb20546..4682885 100644 --- a/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs +++ b/NorthBridge/NorthBridgeTest/BusinessLogicsContractsTests/ProductBusinessLogicTests.cs @@ -1,4 +1,10 @@ -using System; +using Microsoft.Extensions.Logging; +using Moq; +using NorthBridgeBusinessLogic.Implementations; +using NorthBridgeContract.DataModels; +using NorthBridgeContract.Enums; +using NorthBridgeContract.StoragesContracts; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +12,127 @@ using System.Threading.Tasks; namespace NorthBridgeTest.BusinessLogicsContractsTests { + [TestFixture] internal class ProductBusinessLogicTests { + private ProductBusinessLogicContract _productBusinessLogicContract; + private Mock _productStorageContract; + private Mock _logger; + + [OneTimeSetUp] + public void OneTimeSetUp() + { + _logger = new Mock(); + _productStorageContract = new Mock(); + _productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, _logger.Object); + } + + [SetUp] + public void SetUp() + { + _productStorageContract.Reset(); + } + + [Test] + public void GetAllProducts_ReturnsListOfProducts_Test() + { + // 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); + + // Act + var result = _productBusinessLogicContract.GetAllProducts(true); + + // Assert + Assert.Multiple(() => + { + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.EquivalentTo(products)); + }); + + _productStorageContract.Verify(x => x.GetList(true), Times.Once); + } + + [Test] + public void InsertProduct_CorrectProduct_Test() + { + // 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); + } + + [Test] + public void UpdateProduct_CorrectProduct_Test() + { + // 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 productId = "6F9619FF-8B86-D011-B42D-00CF4FC964FF"; + var product = new ProductDataModel(productId, "Product 1", Guid.NewGuid().ToString(), true, components); + + _productStorageContract.Setup(x => x.GetElementById(productId)).Returns(product); + _productStorageContract.Setup(x => x.UpdElement(It.IsAny())); + + // Act + _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); + } + + + [Test] + public void DeleteProduct_ValidId_Test() + { + // Arrange + var id = Guid.NewGuid().ToString(); + _productStorageContract.Setup(x => x.DelElement(id)).Verifiable(); + + // Act + _productBusinessLogicContract.DeleteProduct(id); + + // Assert + _productStorageContract.Verify(x => x.DelElement(id), Times.Once); + } + + [Test] + public void DeleteProduct_InvalidId_ThrowsException_Test() + { + // Arrange + var id = string.Empty; + + // Act & Assert + Assert.That(() => _productBusinessLogicContract.DeleteProduct(id), Throws.TypeOf()); + _productStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); + } } + }