Лабораторная работа 2

This commit is contained in:
Есения Андрианова 2025-02-18 02:34:31 +04:00
parent c040470da2
commit 1789fc17d1
13 changed files with 1016 additions and 516 deletions

View File

@ -11,31 +11,60 @@ namespace NorthBridgeBusinessLogic.Implementations;
internal class BuyerBusinessLogicContract(IBuyerStorageContract buyerStorageContract, ILogger logger) : IBuyerBusinessLogicContract internal class BuyerBusinessLogicContract(IBuyerStorageContract buyerStorageContract, ILogger logger) : IBuyerBusinessLogicContract
{ {
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IBuyerStorageContract _buyerStorageContract = buyerStorageContract; private readonly IBuyerStorageContract _buyerStorageContract = buyerStorageContract;
public List<BuyerDataModel> GetAllBuyers() public List<BuyerDataModel> GetAllBuyers()
{ {
return new List<BuyerDataModel>(); _logger.LogInformation("GetAllBuyers");
} return _buyerStorageContract.GetList() ?? throw new NullListException();
}
public BuyerDataModel GetBuyerByData(string data) public BuyerDataModel GetBuyerByData(string data)
{ {
return new BuyerDataModel("", "", ""); _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);
}
} }

View File

@ -3,7 +3,9 @@ using NorthBridgeContract.BusinessLogicsContracts;
using NorthBridgeContract.DataModels; using NorthBridgeContract.DataModels;
using NorthBridgeContract.Enums; using NorthBridgeContract.Enums;
using NorthBridgeContract.Exceptions; using NorthBridgeContract.Exceptions;
using NorthBridgeContract.Extensions;
using NorthBridgeContract.StoragesContracts; using NorthBridgeContract.StoragesContracts;
using System.Text.Json;
namespace NorthBridgeBusinessLogic.Implementations namespace NorthBridgeBusinessLogic.Implementations
{ {
@ -14,30 +16,67 @@ namespace NorthBridgeBusinessLogic.Implementations
public List<ComponentDataModel> GetAllComponents(bool onlyActive) public List<ComponentDataModel> 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) public void InsertComponent(ComponentDataModel componentDataModel)
{ {
_logger.LogInformation("InsertComponent: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate(); componentDataModel.Validate();
_componentStorageContract.AddElement(componentDataModel); _componentStorageContract.AddElement(componentDataModel);
} }
public void UpdateComponent(ComponentDataModel componentDataModel) public void UpdateComponent(ComponentDataModel componentDataModel)
{ {
_logger.LogInformation("UpdateComponent: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate();
var existingComponent = _componentStorageContract.GetElementById(componentDataModel.Id); var existingComponent = _componentStorageContract.GetElementById(componentDataModel.Id);
if (existingComponent == null) if (existingComponent == null)
throw new ValidationException("Component not found"); {
throw new ElementNotFoundException(componentDataModel.Id);
}
if (existingComponent.Price != componentDataModel.Price) if (existingComponent.Price != componentDataModel.Price)
{ {
@ -46,29 +85,52 @@ namespace NorthBridgeBusinessLogic.Implementations
_componentStorageContract.AddComponentHistory(historyEntry); _componentStorageContract.AddComponentHistory(historyEntry);
} }
componentDataModel.Validate();
_componentStorageContract.UpdElement(componentDataModel); _componentStorageContract.UpdElement(componentDataModel);
} }
public void DeleteComponent(string id) 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); _componentStorageContract.DelElement(id);
} }
public void RestoreComponent(string 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); _componentStorageContract.ResElement(id);
} }
public List<ComponentHistoryDataModel> GetComponentHistory(string componentId) public List<ComponentHistoryDataModel> GetComponentHistory(string componentId)
{ {
return _componentStorageContract.GetComponentHistory(componentId); _logger.LogInformation("GetComponentHistory for componentId: {componentId}", componentId);
} if (componentId.IsEmpty())
{
public ComponentDataModel GetComponentByData(string data) throw new ArgumentNullException(nameof(componentId));
{ }
return new ComponentDataModel("", "", ComponentType.None, "", 0, false); if (!componentId.IsGuid())
{
throw new ValidationException("ComponentId is not a unique identifier");
}
return _componentStorageContract.GetComponentHistory(componentId) ?? [];
} }
} }
} }

View File

@ -10,31 +10,57 @@ namespace NorthBridgeBusinessLogic.Implementations;
internal class ManufacturerBusinessLogicContract(IManufacturerStorageContract manufacturerStorageContract, ILogger logger) : IManufacturerBusinessLogicContract internal class ManufacturerBusinessLogicContract(IManufacturerStorageContract manufacturerStorageContract, ILogger logger) : IManufacturerBusinessLogicContract
{ {
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IManufacturerStorageContract _manufacturerStorageContract = manufacturerStorageContract; private readonly IManufacturerStorageContract _manufacturerStorageContract = manufacturerStorageContract;
public List<ManufacturerDataModel> GetAllManufacturers() public List<ManufacturerDataModel> GetAllManufacturers()
{ {
return []; _logger.LogInformation("GetAllManufacturers");
return _manufacturerStorageContract.GetList() ?? throw new NullListException();
} }
public ManufacturerDataModel GetManufacturerByData(string data) 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) 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);
}
} }

View File

@ -11,41 +11,84 @@ namespace NorthBridgeBusinessLogic.Implementations;
internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract
{ {
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IPostStorageContract _postStorageContract = postStorageContract; private readonly IPostStorageContract _postStorageContract = postStorageContract;
public List<PostDataModel> GetAllPosts(bool onlyActive = true) public List<PostDataModel> GetAllPosts(bool onlyActive = true)
{ {
return new List<PostDataModel>(); _logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
} return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<PostDataModel> GetAllDataOfPost(string postId) public List<PostDataModel> GetAllDataOfPost(string postId)
{ {
return new List<PostDataModel>(); _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) public PostDataModel GetPostByData(string data)
{ {
return new PostDataModel("", "", PostType.None, 0, false, DateTime.Now); _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);
}
} }

View File

@ -10,52 +10,107 @@ namespace NorthBridgeBusinessLogic.Implementations;
internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract
{ {
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IProductStorageContract _productStorageContract = productStorageContract; private readonly IProductStorageContract _productStorageContract = productStorageContract;
public List<ProductDataModel> GetAllProducts(bool onlyActive)
{
_logger.LogInformation("Getting all products (onlyActive: {onlyActive})", onlyActive);
var products = _productStorageContract.GetList(onlyActive);
return products ?? throw new NullListException();
}
public List<ProductDataModel> GetAllProducts(bool onlyActive) public List<ProductDataModel> GetAllProductsByManufacturer(string manufacturerId, bool onlyActive = true)
{ {
return new List<ProductDataModel>(); _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<ProductDataModel> GetAllProductsByManufacturer(string manufacturerId, bool onlyActive = true) public ProductDataModel GetProductByData(string data)
{ {
return new List<ProductDataModel>(); _logger.LogInformation("Getting product by data: {data}", data);
} if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
public ProductDataModel GetProductByData(string data) if (data.IsGuid())
{ {
return new ProductDataModel("", "", "", false, new List<ComponentDataModel>()); 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<ComponentDataModel> GetComponentsByProductId(string productId) public List<ComponentDataModel> GetComponentsByProductId(string productId)
{ {
return new List<ComponentDataModel>(); _logger.LogInformation("Getting components for product {productId}", productId);
} if (productId.IsEmpty())
{
throw new ArgumentNullException(nameof(productId));
}
public void AddComponentToProduct(ComponentInProductDataModel componentInProduct) var components = _productStorageContract.GetComponentsByProductId(productId);
{ return components ?? throw new NullListException();
}
public void RemoveComponentFromProduct(string componentId, string productId)
{
} }
}
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);
}
}

View File

@ -6,29 +6,58 @@ using NorthBridgeContract.StoragesContracts;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace NorthBridgeBusinessLogic.Implementations; namespace NorthBridgeBusinessLogic.Implementations;
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, 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 ILogger _logger = logger;
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly IPostStorageContract _postStorageContract = postStorageContract; private readonly IPostStorageContract _postStorageContract = postStorageContract;
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{ {
return new List<SalaryDataModel>(); _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<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId) public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
{ {
return new List<SalaryDataModel>(); 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));
}
}
} }

View File

@ -6,45 +6,108 @@ using NorthBridgeContract.StoragesContracts;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.Json; using System.Text.Json;
namespace NorthBridgeBusinessLogic.Implementations;
internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBusinessLogicContract internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContract, ILogger logger) : ISaleBusinessLogicContract
{ {
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate) public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
{ {
return new List<SaleDataModel>(); _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<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate) public List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
{ {
return new List<SaleDataModel>(); _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<SaleDataModel> GetAllSalesByBuyerByPeriod(string buyerId, DateTime fromDate, DateTime toDate) public List<SaleDataModel> GetAllSalesByBuyerByPeriod(string buyerId, DateTime fromDate, DateTime toDate)
{ {
return new List<SaleDataModel>(); _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<SaleDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate) public List<SaleDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate)
{ {
return new List<SaleDataModel>(); _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) public SaleDataModel GetSaleByData(string data)
{ {
return new SaleDataModel("", "", "", 0, false, new List<SaleProductDataModel>()); _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);
}
} }

View File

@ -11,46 +11,90 @@ namespace NorthBridgeBusinessLogic.Implementations;
internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract
{ {
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract; private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true) public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
{ {
return new List<WorkerDataModel>(); _logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive);
} return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool onlyActive = true) public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool onlyActive = true)
{ {
return new List<WorkerDataModel>(); _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<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{ {
return new List<WorkerDataModel>(); _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<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true) public List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{ {
return new List<WorkerDataModel>(); _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) public WorkerDataModel GetWorkerByData(string data)
{ {
return new WorkerDataModel("", "", "", DateTime.Now, DateTime.Now, false); _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);
}
} }

View File

@ -0,0 +1,9 @@
namespace NorthBridgeContract.Extensions;
public static class DateTimeExtensions
{
public static bool IsDateNotOlder(this DateTime date, DateTime olderDate)
{
return date >= olderDate;
}
}

View File

@ -4,19 +4,23 @@ namespace NorthBridgeContract.StoragesContracts;
public interface IProductStorageContract public interface IProductStorageContract
{ {
List<ProductDataModel> GetList(bool onlyActual = true); List<ProductDataModel> GetList(bool onlyActive);
List<ProductDataModel> GetPostWithHistory(string productId); List<ProductDataModel> GetListByManufacturer(string manufacturerId, bool onlyActive);
ProductDataModel? GetElementById(string id); ProductDataModel? GetElementById(string id);
ProductDataModel? GetElementByName(string name); 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<ComponentDataModel> GetComponentsByProductId(string productId);
void AddComponentToProduct(ComponentInProductDataModel componentInProduct);
void RemoveComponentFromProduct(string componentId, string productId);
} }

View File

@ -10,341 +10,341 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests;
[TestFixture] [TestFixture]
internal class BuyerBusinessLogicContractTests internal class BuyerBusinessLogicContractTests
{ {
private BuyerBusinessLogicContract _buyerBusinessLogicContract; private BuyerBusinessLogicContract _buyerBusinessLogicContract;
private Mock<IBuyerStorageContract> _buyerStorageContract; private Mock<IBuyerStorageContract> _buyerStorageContract;
[OneTimeSetUp] [OneTimeSetUp]
public void OneTimeSetUp() public void OneTimeSetUp()
{ {
_buyerStorageContract = new Mock<IBuyerStorageContract>(); _buyerStorageContract = new Mock<IBuyerStorageContract>();
_buyerBusinessLogicContract = new BuyerBusinessLogicContract(_buyerStorageContract.Object, new Mock<ILogger>().Object); _buyerBusinessLogicContract = new BuyerBusinessLogicContract(_buyerStorageContract.Object, new Mock<ILogger>().Object);
} }
[SetUp] [SetUp]
public void SetUp() public void SetUp()
{ {
_buyerStorageContract.Reset(); _buyerStorageContract.Reset();
} }
[Test] [Test]
public void GetAllBuyers_ReturnListOfRecords_Test() public void GetAllBuyers_ReturnListOfRecords_Test()
{ {
//Arrange //Arrange
var listOriginal = new List<BuyerDataModel>() var listOriginal = new List<BuyerDataModel>()
{ {
new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11"), 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 2", "+7-555-444-33-23"),
new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777"), new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777"),
}; };
_buyerStorageContract.Setup(x => x.GetList()).Returns(listOriginal); _buyerStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act //Act
var list = _buyerBusinessLogicContract.GetAllBuyers(); var list = _buyerBusinessLogicContract.GetAllBuyers();
//Assert //Assert
Assert.That(list, Is.Not.Null); Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal)); Assert.That(list, Is.EquivalentTo(listOriginal));
} }
[Test] [Test]
public void GetAllBuyers_ReturnEmptyList_Test() public void GetAllBuyers_ReturnEmptyList_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.GetList()).Returns([]); _buyerStorageContract.Setup(x => x.GetList()).Returns([]);
//Act //Act
var list = _buyerBusinessLogicContract.GetAllBuyers(); var list = _buyerBusinessLogicContract.GetAllBuyers();
//Assert //Assert
Assert.That(list, Is.Not.Null); Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0)); Assert.That(list, Has.Count.EqualTo(0));
_buyerStorageContract.Verify(x => x.GetList(), Times.Once); _buyerStorageContract.Verify(x => x.GetList(), Times.Once);
} }
[Test] [Test]
public void GetAllBuyers_ReturnNull_ThrowException_Test() public void GetAllBuyers_ReturnNull_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf<NullListException>()); Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf<NullListException>());
_buyerStorageContract.Verify(x => x.GetList(), Times.Once); _buyerStorageContract.Verify(x => x.GetList(), Times.Once);
} }
[Test] [Test]
public void GetAllBuyers_StorageThrowError_ThrowException_Test() public void GetAllBuyers_StorageThrowError_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException())); _buyerStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf<StorageException>()); Assert.That(() => _buyerBusinessLogicContract.GetAllBuyers(), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.GetList(), Times.Once); _buyerStorageContract.Verify(x => x.GetList(), Times.Once);
} }
[Test] [Test]
public void GetBuyerByData_GetById_ReturnRecord_Test() public void GetBuyerByData_GetById_ReturnRecord_Test()
{ {
//Arrange //Arrange
var id = Guid.NewGuid().ToString(); var id = Guid.NewGuid().ToString();
var record = new BuyerDataModel(id, "fio", "+7-111-111-11-11"); var record = new BuyerDataModel(id, "Иванов Иван Иванович", "+7-111-111-11-11");
_buyerStorageContract.Setup(x => x.GetElementById(id)).Returns(record); _buyerStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act //Act
var element = _buyerBusinessLogicContract.GetBuyerByData(id); var element = _buyerBusinessLogicContract.GetBuyerByData(id);
//Assert //Assert
Assert.That(element, Is.Not.Null); Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id)); Assert.That(element.Id, Is.EqualTo(id));
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
} }
[Test] [Test]
public void GetBuyerByData_GetByFio_ReturnRecord_Test() public void GetBuyerByData_GetByFio_ReturnRecord_Test()
{ {
//Arrange //Arrange
var fio = "fio"; var fio = "Иванов Иван Иванович";
var record = new BuyerDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11"); var record = new BuyerDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11");
_buyerStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record); _buyerStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
//Act //Act
var element = _buyerBusinessLogicContract.GetBuyerByData(fio); var element = _buyerBusinessLogicContract.GetBuyerByData(fio);
//Assert //Assert
Assert.That(element, Is.Not.Null); Assert.That(element, Is.Not.Null);
Assert.That(element.FIO, Is.EqualTo(fio)); Assert.That(element.FIO, Is.EqualTo(fio));
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
} }
[Test] [Test]
public void GetBuyerByData_GetByPhoneNumber_ReturnRecord_Test() public void GetBuyerByData_GetByPhoneNumber_ReturnRecord_Test()
{ {
//Arrange //Arrange
var phoneNumber = "+7-111-111-11-11"; var phoneNumber = "+7-111-111-11-11";
var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio", phoneNumber); var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Иванов Иван Иванович", phoneNumber);
_buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record); _buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record);
//Act //Act
var element = _buyerBusinessLogicContract.GetBuyerByData(phoneNumber); var element = _buyerBusinessLogicContract.GetBuyerByData(phoneNumber);
//Assert //Assert
Assert.That(element, Is.Not.Null); Assert.That(element, Is.Not.Null);
Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber)); Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber));
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
} }
[Test] [Test]
public void GetBuyerByData_EmptyData_ThrowException_Test() public void GetBuyerByData_EmptyData_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(null), Throws.TypeOf<ArgumentNullException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(string.Empty), Throws.TypeOf<ArgumentNullException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
} }
[Test] [Test]
public void GetBuyerByData_GetById_NotFoundRecord_ThrowException_Test() public void GetBuyerByData_GetById_NotFoundRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
} }
[Test] [Test]
public void GetBuyerByData_GetByFio_NotFoundRecord_ThrowException_Test() public void GetBuyerByData_GetByFio_NotFoundRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("fio"), Throws.TypeOf<ElementNotFoundException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("Иванов Иван Иванович"), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
} }
[Test] [Test]
public void GetBuyerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test() public void GetBuyerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf<ElementNotFoundException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
} }
[Test] [Test]
public void GetBuyerByData_StorageThrowError_ThrowException_Test() public void GetBuyerByData_StorageThrowError_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException())); _buyerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_buyerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException())); _buyerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException())); _buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("fio"), Throws.TypeOf<StorageException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("Иванов Иван Иванович"), Throws.TypeOf<StorageException>());
Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf<StorageException>()); Assert.That(() => _buyerBusinessLogicContract.GetBuyerByData("+7-111-111-11-12"), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
} }
[Test] [Test]
public void InsertBuyer_CorrectRecord_Test() public void InsertBuyer_CorrectRecord_Test()
{ {
//Arrange //Arrange
var flag = false; var flag = false;
var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11"); var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11");
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())) _buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>()))
.Callback((BuyerDataModel x) => .Callback((BuyerDataModel x) =>
{ {
flag = x.Id == record.Id && x.FIO == record.FIO && flag = x.Id == record.Id && x.FIO == record.FIO &&
x.PhoneNumber == record.PhoneNumber; x.PhoneNumber == record.PhoneNumber;
}); });
//Act //Act
_buyerBusinessLogicContract.InsertBuyer(record); _buyerBusinessLogicContract.InsertBuyer(record);
//Assert //Assert
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once); _buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once);
Assert.That(flag); Assert.That(flag);
} }
[Test] [Test]
public void InsertBuyer_RecordWithExistsData_ThrowException_Test() public void InsertBuyer_RecordWithExistsData_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())).Throws(new ElementExistsException("Data", "Data")); _buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf<ElementExistsException>()); Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf<ElementExistsException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once); _buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once);
} }
[Test] [Test]
public void InsertBuyer_NullRecord_ThrowException_Test() public void InsertBuyer_NullRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(null), Throws.TypeOf<ArgumentNullException>()); Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(null), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Never); _buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Never);
} }
[Test] [Test]
public void InsertBuyer_InvalidRecord_ThrowException_Test() public void InsertBuyer_InvalidRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new BuyerDataModel("id", "fio", "+7-111-111-11-11")), Throws.TypeOf<ValidationException>()); Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new BuyerDataModel("id", "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Never); _buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Never);
} }
[Test] [Test]
public void InsertBuyer_StorageThrowError_ThrowException_Test() public void InsertBuyer_StorageThrowError_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())).Throws(new StorageException(new InvalidOperationException())); _buyerStorageContract.Setup(x => x.AddElement(It.IsAny<BuyerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf<StorageException>()); Assert.That(() => _buyerBusinessLogicContract.InsertBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once); _buyerStorageContract.Verify(x => x.AddElement(It.IsAny<BuyerDataModel>()), Times.Once);
} }
[Test] [Test]
public void UpdateBuyer_CorrectRecord_Test() public void UpdateBuyer_CorrectRecord_Test()
{ {
//Arrange //Arrange
var flag = false; var flag = false;
var record = new BuyerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11"); var record = new BuyerDataModel(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11");
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())) _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>()))
.Callback((BuyerDataModel x) => .Callback((BuyerDataModel x) =>
{ {
flag = x.Id == record.Id && x.FIO == record.FIO && flag = x.Id == record.Id && x.FIO == record.FIO &&
x.PhoneNumber == record.PhoneNumber; x.PhoneNumber == record.PhoneNumber;
}); });
//Act //Act
_buyerBusinessLogicContract.UpdateBuyer(record); _buyerBusinessLogicContract.UpdateBuyer(record);
//Assert //Assert
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once); _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
Assert.That(flag); Assert.That(flag);
} }
[Test] [Test]
public void UpdateBuyer_RecordWithIncorrectData_ThrowException_Test() public void UpdateBuyer_RecordWithIncorrectData_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new ElementNotFoundException("")); _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf<ElementNotFoundException>()); Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once); _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
} }
[Test] [Test]
public void UpdateBuyer_RecordWithExistsData_ThrowException_Test() public void UpdateBuyer_RecordWithExistsData_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new ElementExistsException("Data", "Data")); _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf<ElementExistsException>()); Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf<ElementExistsException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once); _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
} }
[Test] [Test]
public void UpdateBuyer_NullRecord_ThrowException_Test() public void UpdateBuyer_NullRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(null), Throws.TypeOf<ArgumentNullException>()); Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(null), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Never); _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Never);
} }
[Test] [Test]
public void UpdateBuyer_InvalidRecord_ThrowException_Test() public void UpdateBuyer_InvalidRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new BuyerDataModel("id", "fio", "+7-111-111-11-11")), Throws.TypeOf<ValidationException>()); Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new BuyerDataModel("id", "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Never); _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Never);
} }
[Test] [Test]
public void UpdateBuyer_StorageThrowError_ThrowException_Test() public void UpdateBuyer_StorageThrowError_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new StorageException(new InvalidOperationException())); _buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<BuyerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11")), Throws.TypeOf<StorageException>()); Assert.That(() => _buyerBusinessLogicContract.UpdateBuyer(new(Guid.NewGuid().ToString(), "Иванов Иван Иванович", "+7-111-111-11-11")), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once); _buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<BuyerDataModel>()), Times.Once);
} }
[Test] [Test]
public void DeleteBuyer_CorrectRecord_Test() public void DeleteBuyer_CorrectRecord_Test()
{ {
//Arrange //Arrange
var id = Guid.NewGuid().ToString(); var id = Guid.NewGuid().ToString();
var flag = false; var flag = false;
_buyerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); _buyerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act //Act
_buyerBusinessLogicContract.DeleteBuyer(id); _buyerBusinessLogicContract.DeleteBuyer(id);
//Assert //Assert
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag); Assert.That(flag);
} }
[Test] [Test]
public void DeleteBuyer_RecordWithIncorrectId_ThrowException_Test() public void DeleteBuyer_RecordWithIncorrectId_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException("")); _buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>()); Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
} }
[Test] [Test]
public void DeleteBuyer_IdIsNullOrEmpty_ThrowException_Test() public void DeleteBuyer_IdIsNullOrEmpty_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(null), Throws.TypeOf<ArgumentNullException>()); Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(string.Empty), Throws.TypeOf<ArgumentNullException>()); Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(string.Empty), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
} }
[Test] [Test]
public void DeleteBuyer_IdIsNotGuid_ThrowException_Test() public void DeleteBuyer_IdIsNotGuid_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer("id"), Throws.TypeOf<ValidationException>()); Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer("id"), Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never); _buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
} }
[Test] [Test]
public void DeleteBuyer_StorageThrowError_ThrowException_Test() public void DeleteBuyer_StorageThrowError_ThrowException_Test()
{ {
//Arrange //Arrange
_buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException())); _buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>()); Assert.That(() => _buyerBusinessLogicContract.DeleteBuyer(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); _buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
} }
} }

View File

@ -140,21 +140,31 @@ namespace NorthBridgeTest.BusinessLogicsContractsTests
[Test] [Test]
public void UpdateComponent_CorrectRecord_Test() public void UpdateComponent_CorrectRecord_Test()
{ {
// Arrange
var flag = false; 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<ComponentDataModel>())) _componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>()))
.Callback((ComponentDataModel x) => .Callback((ComponentDataModel x) =>
{ {
flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType && flag = x.Id == updatedRecord.Id && x.ComponentName == updatedRecord.ComponentName &&
x.ManufacturerId == record.ManufacturerId && x.Price == record.Price && x.IsDeleted == record.IsDeleted; 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<string>()), Times.Once);
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once); _componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
_componentStorageContract.Verify(x => x.AddComponentHistory(It.IsAny<ComponentHistoryDataModel>()), Times.Once);
Assert.That(flag); Assert.That(flag);
} }
[Test] [Test]
public void DeleteComponent_CorrectRecord_Test() public void DeleteComponent_CorrectRecord_Test()
{ {

View File

@ -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.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,7 +12,127 @@ using System.Threading.Tasks;
namespace NorthBridgeTest.BusinessLogicsContractsTests namespace NorthBridgeTest.BusinessLogicsContractsTests
{ {
[TestFixture]
internal class ProductBusinessLogicTests internal class ProductBusinessLogicTests
{ {
private ProductBusinessLogicContract _productBusinessLogicContract;
private Mock<IProductStorageContract> _productStorageContract;
private Mock<ILogger> _logger;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_logger = new Mock<ILogger>();
_productStorageContract = new Mock<IProductStorageContract>();
_productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, _logger.Object);
}
[SetUp]
public void SetUp()
{
_productStorageContract.Reset();
}
[Test]
public void GetAllProducts_ReturnsListOfProducts_Test()
{
// 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);
// 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<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);
}
[Test]
public void UpdateProduct_CorrectProduct_Test()
{
// 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 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<ProductDataModel>()));
// Act
_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);
}
[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<ArgumentNullException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
} }
} }