lab2 green

This commit is contained in:
2025-02-27 02:14:25 +04:00
parent 79cdd4b5d7
commit b7c31860e1
16 changed files with 1036 additions and 426 deletions

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="8.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<PropertyGroup>
@@ -63,6 +63,9 @@
<HintPath>..\packages\Moq.4.20.72\lib\net462\Moq.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System"/>
<Reference Include="System.Buffers, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.6.0\lib\net462\System.Buffers.dll</HintPath>
@@ -74,6 +77,9 @@
<Reference Include="System.Diagnostics.DiagnosticSource, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.10.0.0-preview.1.25080.5\lib\net462\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="System.IO.Pipelines, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Pipelines.10.0.0-preview.1.25080.5\lib\net462\System.IO.Pipelines.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.6.0\lib\net462\System.Memory.dll</HintPath>
</Reference>
@@ -84,6 +90,12 @@
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.1.0\lib\net462\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.10.0.0-preview.1.25080.5\lib\net462\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.10.0.0-preview.1.25080.5\lib\net462\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.6.0\lib\net462\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>

View File

@@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
@@ -16,24 +20,67 @@ namespace CandyHouseBase.Implementations
public List<IngredientDataModel> GetAllIngredients()
{
return new List<IngredientDataModel>();
_logger.LogInformation("GetAllIngredients");
var ingredients = _ingredientStorageContact.GetList() ?? throw new NullListException();
return ingredients;
}
public IngredientDataModel GetIngredientByData(string data)
{
return new IngredientDataModel("", "", "", 100);
_logger.LogInformation("GetIngredientByData for data: {data}", data);
if (data == null)
throw new ArgumentNullException(nameof(data));
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
if (data.IsGuid())
{
var ingredient = _ingredientStorageContact.GetElementById(data);
if (ingredient == null)
throw new ElementNotFoundException(data);
return ingredient;
}
else
{
var ingredient = _ingredientStorageContact.GetElementByName(data);
if (ingredient == null)
throw new ElementNotFoundException(data);
return ingredient;
}
}
public void InsertIngredient(IngredientDataModel ingredient)
{
_logger.LogInformation("InsertIngredient: {json}", JsonSerializer.Serialize(ingredient));
if (ingredient == null)
throw new ArgumentNullException(nameof(ingredient));
ingredient.Validate();
_ingredientStorageContact.AddElement(ingredient);
}
public void UpdateIngredient(IngredientDataModel ingredient)
{
_logger.LogInformation("UpdateIngredient: {json}", JsonSerializer.Serialize(ingredient));
if (ingredient == null)
throw new ArgumentNullException(nameof(ingredient));
ingredient.Validate();
_ingredientStorageContact.UpdateElement(ingredient);
}
public void DeleteIngredient(string id)
{
_logger.LogInformation("DeleteIngredient for id: {id}", id);
if (id == null)
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (!id.IsGuid())
throw new ValidationException("id must be a GUID");
var ingredient = _ingredientStorageContact.GetElementById(id);
if (ingredient == null)
throw new ElementNotFoundException(id);
_ingredientStorageContact.DeleteElement(id);
}
}
}

View File

@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
@@ -22,25 +25,102 @@ namespace CandyHouseBase.Implementations
public List<OrderDataModel> GetAllOrders()
{
return new List<OrderDataModel>();
_logger.LogInformation("GetAllOrders");
var orders = _orderStorageContact.GetOrders() ?? throw new NullListException();
return orders;
}
public OrderDataModel GetOrderByData(string data)
{
return new OrderDataModel("", "", new DateTime(),
100, 100m, "", "", StatusType.Cancelled);
_logger.LogInformation("GetOrderByData for data: {data}", data);
if (data == null)
throw new ArgumentNullException(nameof(data));
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
if (!data.IsGuid())
throw new ValidationException("data must be a GUID");
var order = _orderStorageContact.GetElementById(data) ?? throw new ElementNotFoundException(data);
return order;
}
public void InsertOrder(OrderDataModel order)
{
_logger.LogInformation("InsertOrder: {json}", JsonSerializer.Serialize(order));
if (order == null)
throw new ArgumentNullException(nameof(order));
order.Validate();
// Check if Pekar exists
if (_pekarStorageContact.GetElementById(order.PekarId) == null)
throw new ElementNotFoundException(order.PekarId);
// Check if Product exists
if (_productStorageContact.GetElementById(order.ProductId) == null)
throw new ElementNotFoundException(order.ProductId);
// Check if order with this ID already exists
var existingOrder = _orderStorageContact.GetElementById(order.Id);
if (existingOrder != null)
throw new ElementExistsException("ID", order.Id);
try
{
_orderStorageContact.AddElement(order);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
public void UpdateOrder(OrderDataModel order)
{
_logger.LogInformation("UpdateOrder: {json}", JsonSerializer.Serialize(order));
if (order == null)
throw new ArgumentNullException(nameof(order));
order.Validate();
// Check if Pekar exists
if (_pekarStorageContact.GetElementById(order.PekarId) == null)
throw new ElementNotFoundException(order.PekarId);
// Check if Product exists
if (_productStorageContact.GetElementById(order.ProductId) == null)
throw new ElementNotFoundException(order.ProductId);
try
{
_orderStorageContact.UpdateElement(order);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
public void DeleteOrder(string id)
{
_logger.LogInformation("DeleteOrder for id: {id}", id);
if (id == null)
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (!id.IsGuid())
throw new ValidationException("id must be a GUID");
var order = _orderStorageContact.GetElementById(id);
if (order == null)
throw new ElementNotFoundException(id);
try
{
_orderStorageContact.DeleteElement(order);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
}
}

View File

@@ -1,7 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
@@ -22,34 +27,148 @@ namespace CandyHouseBase.Implementations
public List<PekarDataModel> GetAllPekars()
{
return new List<PekarDataModel>();
_logger.LogInformation("GetAllPekars");
var pekars = _pekarStorageContact.GetList() ?? throw new NullListException();
return pekars;
}
public List<PekarDataModel> GetAllDataOfPekar(string pekarId)
{
return new List<PekarDataModel>();
_logger.LogInformation("GetAllDataOfPekar for pekarId: {pekarId}", pekarId);
if (pekarId.IsEmpty())
throw new ArgumentNullException(nameof(pekarId));
if (!pekarId.IsGuid())
throw new ValidationException("pekarId must be a GUID");
var pekarsWithHistory = _pekarStorageContact.GetPekarWithHistory(pekarId);
if (pekarsWithHistory == null || !pekarsWithHistory.Any())
throw new ElementNotFoundException(pekarId);
var historyRecords = new List<PekarDataModel>();
foreach (var pekar in pekarsWithHistory)
{
Guid positionGuid;
if (!Guid.TryParse(pekar.Position, out positionGuid))
{
_logger.LogWarning("Invalid Position GUID: {Position}", pekar.Position);
continue;
}
var position = _positionStorageContact.GetElementById(positionGuid.ToString());
if (position == null)
throw new ElementNotFoundException(pekar.Position);
historyRecords.Add(new PekarDataModel(pekar.Id, pekar.FIO, position.Id, pekar.BonusCoefficient,
new List<ProductDataModel>()));
}
if (!historyRecords.Any())
{
var firstPekar = pekarsWithHistory.First();
Guid positionGuid;
if (Guid.TryParse(firstPekar.Position, out positionGuid))
{
var position = _positionStorageContact.GetElementById(positionGuid.ToString());
if (position != null)
historyRecords.Add(new PekarDataModel(firstPekar.Id, firstPekar.FIO, position.Id,
firstPekar.BonusCoefficient,
new List<ProductDataModel>()));
}
}
return historyRecords;
}
public PekarDataModel GetPekarByData(string data)
{
return new PekarDataModel("", "", "",
0, new List<ProductDataModel>());
_logger.LogInformation("GetPekarByData for data: {data}", data);
if (data.IsEmpty())
throw new ArgumentNullException(nameof(data));
if (!data.IsGuid() && !Regex.IsMatch(data, @"^[A-Za-zА-Яа-яЁё\s\-]+$"))
throw new ValidationException("data must be a GUID or FIO");
var pekar = _pekarStorageContact.GetElementById(data) ?? _pekarStorageContact.GetElementByFio(data) ??
throw new ElementNotFoundException(data);
return pekar;
}
public void InsertPekar(PekarDataModel order)
public void InsertPekar(PekarDataModel pekar)
{
_logger.LogInformation("InsertPekar: {json}", JsonSerializer.Serialize(pekar));
if (pekar == null)
throw new ArgumentNullException(nameof(pekar));
pekar.Validate();
var existingPekar = _pekarStorageContact.GetElementById(pekar.Id);
if (existingPekar != null)
{
var history = new PekarHistoryDataModel(
existingPekar.Id,
existingPekar.FIO,
existingPekar.Position,
existingPekar.BonusCoefficient,
DateTime.Now
);
}
foreach (var product in pekar.ProductsItems)
{
if (_productStorageContact.GetElementById(product.Id) == null)
throw new ElementNotFoundException(product.Id);
}
_pekarStorageContact.AddElement(pekar);
}
public void UpdatePekar(PekarDataModel order)
public void UpdatePekar(PekarDataModel pekar)
{
_logger.LogInformation("UpdatePekar: {json}", JsonSerializer.Serialize(pekar));
if (pekar == null)
throw new ArgumentNullException(nameof(pekar));
pekar.Validate();
foreach (var product in pekar.ProductsItems)
{
if (_productStorageContact.GetElementById(product.Id) == null)
throw new ElementNotFoundException(product.Id);
}
_pekarStorageContact.UpdateElement(pekar);
}
public void DeletePekar(string id)
{
_logger.LogInformation("DeletePekar for id: {id}", id);
if (id == null)
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (!id.IsGuid())
throw new ValidationException("id must be a GUID");
var pekar = _pekarStorageContact.GetElementById(id);
if (pekar == null)
throw new ElementNotFoundException(id);
_pekarStorageContact.DeleteElement(id);
}
public void RestorePekar(string id)
{
_logger.LogInformation("RestorePekar for id: {id}", id);
if (id == null)
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (!id.IsGuid())
throw new ValidationException("id must be a GUID");
var pekar = _pekarStorageContact.GetElementById(id);
if (pekar == null)
throw new ElementNotFoundException(id);
_pekarStorageContact.RestoreElement(id);
}
}
}

View File

@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
@@ -18,24 +21,67 @@ namespace CandyHouseBase.Implementations
public List<PositionDataModel> GetAllPositions()
{
return new List<PositionDataModel>();
_logger.LogInformation("GetAllPositions");
var positions = _positionStorageContact.GetList() ?? throw new NullListException();
return positions;
}
public PositionDataModel GetPositionByData(string data)
{
return new PositionDataModel("", PositionType.Cool, "");
_logger.LogInformation("GetPositionByData for data: {data}", data);
if (data.IsEmpty())
throw new ArgumentNullException(nameof(data));
var position = _positionStorageContact.GetElementById(data);
if (position == null)
throw new ElementNotFoundException(data);
if (!data.IsGuid())
throw new ValidationException("data must be a GUID");
return position;
}
public void InsertPosition(PositionDataModel position)
{
_logger.LogInformation("InsertPosition: {json}", JsonSerializer.Serialize(position));
if (position == null)
throw new ArgumentNullException(nameof(position));
position.Validate();
_positionStorageContact.AddElement(position);
}
public void UpdatePosition(PositionDataModel position)
{
_logger.LogInformation("UpdatePosition: {json}", JsonSerializer.Serialize(position));
if (position == null)
throw new ArgumentNullException(nameof(position));
position.Validate();
_positionStorageContact.UpdateElement(position);
}
public void DeletePosition(string id)
{
_logger.LogInformation("DeletePosition for id: {id}", id);
if (id.IsEmpty())
throw new ArgumentNullException(nameof(id));
if (!id.IsGuid())
throw new ValidationException("id must be a GUID");
var position = _positionStorageContact.GetElementById(id);
if (position == null)
throw new ElementNotFoundException(id);
try
{
_positionStorageContact.DeleteElement(id);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
}
}

View File

@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
@@ -20,24 +23,92 @@ namespace CandyHouseBase.Implementations
public List<ProductDataModel> GetAllProducts()
{
return new List<ProductDataModel>();
_logger.LogInformation("GetAllProducts");
var products = _productStorageContact.GetList() ?? throw new NullListException();
return products;
}
public ProductDataModel GetProductByData(string data)
{
return new ProductDataModel("", "", "", new List<IngredientDataModel>());
_logger.LogInformation("GetProductByData for data: {data}", data);
if (data == null)
throw new ArgumentNullException(nameof(data));
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
if (data.IsGuid())
{
var product = _productStorageContact.GetElementById(data);
if (product == null)
throw new ElementNotFoundException(data);
if (!data.IsGuid()) // Validate after existence check (redundant but kept for consistency)
throw new ValidationException("data must be a GUID");
return product;
}
else
{
var products = _productStorageContact.GetList();
var productByName = products.FirstOrDefault(p => p.Name == data || (p.OldName != null && p.OldName == data));
if (productByName == null)
throw new ElementNotFoundException(data);
return productByName;
}
}
public void InsertProduct(ProductDataModel product)
{
_logger.LogInformation("InsertProduct: {json}", JsonSerializer.Serialize(product));
if (product == null)
throw new ArgumentNullException(nameof(product));
product.Validate();
foreach (var ingredient in product.IngredientsItems)
{
if (_ingredientStorageContact.GetElementById(ingredient.Id) == null)
throw new ElementNotFoundException(ingredient.Id);
}
_productStorageContact.AddElement(product);
}
public void UpdateProduct(ProductDataModel product)
{
_logger.LogInformation("UpdateProduct: {json}", JsonSerializer.Serialize(product));
if (product == null)
throw new ArgumentNullException(nameof(product));
product.Validate();
foreach (var ingredient in product.IngredientsItems)
{
if (_ingredientStorageContact.GetElementById(ingredient.Id) == null)
throw new ElementNotFoundException(ingredient.Id);
}
_productStorageContact.UpdateElement(product);
}
public void DeleteProduct(string id)
{
_logger.LogInformation("DeleteProduct for id: {id}", id);
if (id == null)
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (!id.IsGuid())
throw new ValidationException("id must be a GUID");
var product = _productStorageContact.GetElementById(id);
if (product == null)
throw new ElementNotFoundException(id);
try
{
_productStorageContact.DeleteElement(id);
}
catch (Exception ex)
{
throw new StorageException(ex);
}
}
}
}

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
@@ -12,32 +14,72 @@ namespace CandyHouseBase.Implementations
ISalaryStorageContact salaryStorageContact,
IPekarStorageContact pekarStorageContact,
ILogger logger)
: ISalaryStorageContact
: ISalaryBusinessLogicContact
{
private readonly ISalaryStorageContact _salaryStorageContact = salaryStorageContact;
private readonly IPekarStorageContact _pekarStorageContact = pekarStorageContact;
private readonly ILogger _logger = logger;
public List<SalaryDataModel> GetList()
public List<SalaryDataModel> GetAllSalaries()
{
return new List<SalaryDataModel>();
_logger.LogInformation("GetAllSalaries");
var salaries = _salaryStorageContact.GetList() ?? throw new NullListException();
return salaries;
}
public SalaryDataModel GetElementById(string id)
public SalaryDataModel GetSalaryByData(string data)
{
return new SalaryDataModel("", "", new DateTime(), 0, 0, 0);
_logger.LogInformation("GetSalaryByData for data: {data}", data);
if (data == null)
throw new ArgumentNullException(nameof(data));
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
if (!data.IsGuid())
throw new ValidationException("data must be a GUID");
var salary = _salaryStorageContact.GetElementById(data) ?? throw new ElementNotFoundException(data);
if (_pekarStorageContact.GetElementById(salary.PekarId) == null)
throw new ElementNotFoundException(salary.PekarId);
return salary;
}
public void AddElement(SalaryDataModel element)
public void InsertSalary(SalaryDataModel salary)
{
_logger.LogInformation("InsertSalary: {json}", JsonSerializer.Serialize(salary));
if (salary == null)
throw new ArgumentNullException(nameof(salary));
salary.Validate();
if (_pekarStorageContact.GetElementById(salary.PekarId) == null)
throw new ElementNotFoundException(salary.PekarId);
_salaryStorageContact.AddElement(salary);
}
public void UpdateElement(SalaryDataModel element)
public void UpdateSalary(SalaryDataModel salary)
{
_logger.LogInformation("UpdateSalary: {json}", JsonSerializer.Serialize(salary));
if (salary == null)
throw new ArgumentNullException(nameof(salary));
salary.Validate();
if (_pekarStorageContact.GetElementById(salary.PekarId) == null)
throw new ElementNotFoundException(salary.PekarId);
_salaryStorageContact.UpdateElement(salary);
}
public void DeleteElement(string id)
public void DeleteSalary(string id)
{
_logger.LogInformation("DeleteSalary for id: {id}", id);
if (id == null)
throw new ArgumentNullException(nameof(id));
if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(nameof(id));
if (!id.IsGuid())
throw new ValidationException("id must be a GUID");
var salary = _salaryStorageContact.GetElementById(id);
if (salary == null)
throw new ElementNotFoundException(id);
_salaryStorageContact.DeleteElement(id);
}
}
}

View File

@@ -9,6 +9,6 @@ namespace CandyHouseBase.Interfaces.StoragesContracts
void AddElement(OrderDataModel order);
void UpdateElement(OrderDataModel order);
void DeleteElement(OrderDataModel order);
OrderDataModel GetElementById(int orderId);
OrderDataModel GetElementById(string orderId);
}
}

View File

@@ -2,11 +2,14 @@ using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Implementations;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
using CandyHouseBase.Implementations;
namespace CandyHouseTests.BusinessLogicsContractsTests
{
@@ -20,8 +23,10 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void OneTimeSetUp()
{
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_ingredientBusinessLogicContract =
new IngredientBusinessLogicContract(_ingredientStorageContact.Object, new Mock<ILogger>().Object);
_ingredientBusinessLogicContract = new IngredientBusinessLogicContract(
_ingredientStorageContact.Object,
new Mock<ILogger>().Object
);
}
[SetUp]
@@ -36,9 +41,9 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Arrange
var ingredients = new List<IngredientDataModel>
{
new(Guid.NewGuid().ToString(), "Sugar", "kg", 100),
new(Guid.NewGuid().ToString(), "Flour", "kg", 200),
new(Guid.NewGuid().ToString(), "Cocoa", "kg", 50)
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100),
new IngredientDataModel(Guid.NewGuid().ToString(), "Flour", "kg", 200),
new IngredientDataModel(Guid.NewGuid().ToString(), "Cocoa", "kg", 50)
};
_ingredientStorageContact.Setup(x => x.GetList()).Returns(ingredients);
@@ -48,6 +53,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(ingredients));
Assert.That(list.All(i => Guid.TryParse(i.Id, out _) && !i.Name.IsEmpty() && !i.Unit.IsEmpty() && i.Cost >= 0), Is.True);
}
[Test]
@@ -80,8 +86,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void GetAllIngredients_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetList())
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetAllIngredients(), Throws.TypeOf<StorageException>());
@@ -89,7 +94,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
}
[Test]
public void GetIngredientById_ReturnsRecord_Test()
public void GetIngredientByData_ReturnsIngredientById_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
@@ -102,11 +107,15 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(!element.Name.IsEmpty());
Assert.That(!element.Unit.IsEmpty());
Assert.That(element.Cost >= 0);
_ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByName_ReturnsRecord_Test()
public void GetIngredientByData_ReturnsIngredientByName_Test()
{
// Arrange
var name = "Sugar";
@@ -119,175 +128,148 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Name, Is.EqualTo(name));
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(!element.Unit.IsEmpty());
Assert.That(element.Cost >= 0);
_ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientById_NotFoundRecord_ThrowException_Test()
public void GetIngredientByData_EmptyData_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
_ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns((IngredientDataModel)null);
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(id),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetIngredientByName_NotFoundRecord_ThrowException_Test()
public void GetIngredientByData_NotFoundById_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString(); // Valid Guid
_ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns((IngredientDataModel)null);
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(id), Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void GetIngredientByData_NotFoundByName_ThrowException_Test()
{
// Arrange
var name = "Nonexistent";
_ingredientStorageContact.Setup(x => x.GetElementByName(name)).Returns((IngredientDataModel)null);
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(name),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(name), Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.GetElementByName(name), Times.Once);
}
[Test]
public void GetIngredientById_StorageThrowError_ThrowException_Test()
public void GetIngredientByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData("Sugar"), Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByName_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetElementByName(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData("Sugar"),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void AddIngredient_CorrectRecord_Test()
public void InsertIngredient_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.AddElement(It.IsAny<IngredientDataModel>()))
.Callback((IngredientDataModel x) =>
{
flag = x.Id == ingredient.Id && x.Name == ingredient.Name &&
x.Cost == ingredient.Cost && x.Unit == ingredient.Unit;
});
_ingredientStorageContact.Setup(x => x.AddElement(ingredient)).Verifiable();
// Act
_ingredientBusinessLogicContract.InsertIngredient(ingredient);
// Assert
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(flag);
_ingredientStorageContact.Verify(x => x.AddElement(ingredient), Times.Once);
}
[Test]
public void AddIngredient_RecordWithExistsData_ThrowException_Test()
public void InsertIngredient_RecordWithExistsData_ThrowException_Test()
{
var ingredient = It.IsAny<IngredientDataModel>();
// Arrange
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.AddElement(ingredient))
.Throws(new ElementExistsException("ID", ingredient.Id));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.InsertIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<ElementExistsException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(ingredient), Throws.TypeOf<ElementExistsException>());
_ingredientStorageContact.Verify(x => x.AddElement(ingredient), Times.Once);
}
[Test]
public void AddIngredient_NullRecord_ThrowException_Test()
public void InsertIngredient_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(null), Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void AddIngredient_InvalidRecord_ThrowException_Test()
public void InsertIngredient_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.InsertIngredient(new IngredientDataModel("", null, "", -1)),
Throws.TypeOf<ValidationException>());
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(new IngredientDataModel("", "", "", -1)), Throws.TypeOf<ValidationException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void AddIngredient_StorageThrowError_ThrowException_Test()
public void InsertIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.AddElement(It.IsAny<IngredientDataModel>()))
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.AddElement(ingredient))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.InsertIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(ingredient), Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.AddElement(ingredient), Times.Once);
}
[Test]
public void UpdateIngredient_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.UpdateElement(It.IsAny<IngredientDataModel>()))
.Callback((IngredientDataModel x) =>
{
flag = x.Id == ingredient.Id && x.Name == ingredient.Name &&
x.Cost == ingredient.Cost && x.Unit == ingredient.Unit;
});
_ingredientStorageContact.Setup(x => x.UpdateElement(ingredient)).Verifiable();
// Act
_ingredientBusinessLogicContract.UpdateIngredient(ingredient);
// Assert
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(flag);
_ingredientStorageContact.Verify(x => x.UpdateElement(ingredient), Times.Once);
}
[Test]
public void UpdateIngredient_RecordNotFound_ThrowException_Test()
{
var ingredient = It.IsAny<IngredientDataModel>();
// Arrange
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.UpdateElement(ingredient))
.Throws(new ElementNotFoundException(ingredient.ToString()));
.Throws(new ElementNotFoundException(ingredient.Id));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.UpdateIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(ingredient), Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(ingredient), Times.Once);
}
[Test]
public void UpdateIngredient_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(null), Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
@@ -295,9 +277,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdateIngredient_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.UpdateIngredient(new IngredientDataModel("", null, "", -1)),
Throws.TypeOf<ValidationException>());
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(new IngredientDataModel("", "", "", -1)), Throws.TypeOf<ValidationException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
@@ -305,56 +285,69 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdateIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.UpdateElement(It.IsAny<IngredientDataModel>()))
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.UpdateElement(ingredient))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.UpdateIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(ingredient), Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(ingredient), Times.Once);
}
[Test]
public void DeleteIngredient_CorrectId_Test()
{
// Arrange
var id = "1";
var id = Guid.NewGuid().ToString();
var ingredient = new IngredientDataModel(id, "Sugar", "kg", 100);
var flag = false;
_ingredientStorageContact.Setup(x => x.DeleteElement(It.Is((string x) => x == id)))
.Callback(() => { flag = true; });
_ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns(ingredient);
_ingredientStorageContact.Setup(x => x.DeleteElement(id)).Callback(() => { flag = true; });
// Act
_ingredientBusinessLogicContract.DeleteIngredient(id);
// Assert
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
_ingredientStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(ingredient.Id, out _), Is.True);
Assert.That(!ingredient.Name.IsEmpty());
Assert.That(!ingredient.Unit.IsEmpty());
Assert.That(ingredient.Cost >= 0);
}
[Test]
public void DeleteIngredient_RecordNotFound_ThrowException_Test()
{
var ingredient = It.IsAny<IngredientDataModel>();
// Arrange
_ingredientStorageContact.Setup(x => x.UpdateElement(ingredient))
.Throws(new ElementNotFoundException(ingredient.ToString()));
var id = Guid.NewGuid().ToString(); // Use valid Guid
_ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns((IngredientDataModel)null);
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient("nonexistent"),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(id), Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void DeleteIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns(new IngredientDataModel(id, "Sugar", "kg", 100));
_ingredientStorageContact.Setup(x => x.DeleteElement(id)).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(id), Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_ingredientStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
[Test]
public void DeleteIngredient_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(string.Empty),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(string.Empty), Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
@@ -362,22 +355,8 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void DeleteIngredient_InvalidId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient("invalid"),
Throws.TypeOf<ValidationException>());
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient("invalid"), Throws.TypeOf<ValidationException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.DeleteElement(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
}
}
}

View File

@@ -5,6 +5,7 @@ using System.Collections.Generic;
using CandyHouseBase.DataModels;
using CandyHouseBase.Enums;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Extensions;
using CandyHouseBase.Implementations;
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
using CandyHouseBase.Interfaces.StoragesContracts;
@@ -138,7 +139,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Guid.NewGuid().ToString(),
StatusType.Pending
);
_orderStorageContact.Setup(x => x.GetElementById(It.Is<int>(i => i.ToString() == id))).Returns(order);
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns(order);
// Act
var element = _orderBusinessLogicContract.GetOrderByData(id);
@@ -146,7 +147,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_orderStorageContact.Verify(x => x.GetElementById(It.Is<int>(i => i.ToString() == id)), Times.Once);
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -154,35 +155,32 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<int>()), Times.Never);
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetOrderByData_NotFoundRecord_ThrowException_Test()
{
// Arrange
var id = "999";
_orderStorageContact.Setup(x => x.GetElementById(It.Is<int>(i => i.ToString() == id)))
.Returns((OrderDataModel)null);
var id = Guid.NewGuid().ToString();
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns((OrderDataModel)null);
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(id),
Throws.TypeOf<ElementNotFoundException>());
_orderStorageContact.Verify(x => x.GetElementById(It.Is<int>(i => i.ToString() == id)), Times.Once);
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(id), Throws.TypeOf<ElementNotFoundException>());
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void GetOrderByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_orderStorageContact.Setup(x => x.GetElementById(It.IsAny<int>()))
_orderStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.GetOrderByData("1"), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<int>()), Times.Once);
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
@@ -202,14 +200,16 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns((OrderDataModel)null); // No existing order
_orderStorageContact.Setup(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Callback((OrderDataModel x) =>
{
flag = x.Id == order.Id && x.CustomerName == order.CustomerName && x.OrderDate == order.OrderDate &&
x.TotalAmount == order.TotalAmount && x.DiscountAmount == order.DiscountAmount &&
x.ProductId == order.ProductId && x.PekarId == order.PekarId &&
x.StatusType == order.StatusType;
});
})
.Verifiable();
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
@@ -219,7 +219,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_orderBusinessLogicContract.InsertOrder(order);
// Assert
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
_orderStorageContact.Verify(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
Assert.That(flag);
}
@@ -227,22 +227,27 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertOrder_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
DateTime.Now,
100.50m,
10.50m,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
productId,
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
.Throws(new ElementExistsException("ID", order.Id));
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns(order); // Existing order
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<ElementExistsException>());
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
_orderStorageContact.Verify(x => x.GetElementById(order.Id), Times.Once);
}
[Test]
@@ -261,10 +266,10 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
"",
"",
DateTime.Now,
-100.50m,
-10.50m,
"",
"",
-100.50m,
-10.50m,
"",
"",
StatusType.Pending
)), Throws.TypeOf<ValidationException>());
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
@@ -274,22 +279,30 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertOrder_StorageThrowError_ThrowException_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
DateTime.Now,
100.50m,
10.50m,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
productId,
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns((OrderDataModel)null); // No existing order
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
_orderStorageContact.Setup(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
_orderStorageContact.Verify(x => x.GetElementById(order.Id), Times.Once);
_orderStorageContact.Verify(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
}
[Test]
@@ -309,14 +322,15 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>()))
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Callback((OrderDataModel x) =>
{
flag = x.Id == order.Id && x.CustomerName == order.CustomerName && x.OrderDate == order.OrderDate &&
x.TotalAmount == order.TotalAmount && x.DiscountAmount == order.DiscountAmount &&
x.ProductId == order.ProductId && x.PekarId == order.PekarId &&
x.StatusType == order.StatusType;
});
})
.Verifiable();
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
@@ -326,7 +340,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_orderBusinessLogicContract.UpdateOrder(order);
// Assert
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Once);
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
Assert.That(flag);
}
@@ -334,23 +348,25 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdateOrder_RecordNotFound_ThrowException_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
DateTime.Now,
100.50m,
10.50m,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
productId,
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>()))
.Throws(new ElementNotFoundException("Order not found"));
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns((PekarDataModel)null); // Trigger not found
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Verifiable();
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order),
Throws.TypeOf<ElementNotFoundException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Once);
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order), Throws.TypeOf<ElementNotFoundException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Never);
}
[Test]
@@ -366,13 +382,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(new OrderDataModel(
"",
"",
"",
DateTime.Now,
-100.50m,
-10.50m,
"",
"",
-10.50m,
"",
"",
StatusType.Pending
)), Throws.TypeOf<ValidationException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Never);
@@ -382,22 +398,28 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdateOrder_StorageThrowError_ThrowException_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var order = new OrderDataModel(
Guid.NewGuid().ToString(),
"John Doe",
DateTime.Now,
100.50m,
10.50m,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
productId,
pekarId,
StatusType.Pending
);
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>()))
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Once);
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
}
[Test]
@@ -416,30 +438,31 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
StatusType.Pending
);
var flag = false;
_orderStorageContact.Setup(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)))
.Callback(() => { flag = true; });
_orderStorageContact.Setup(x => x.GetOrders()).Returns(new List<OrderDataModel> { order });
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns(order);
_orderStorageContact.Setup(x => x.DeleteElement(order)).Callback(() => { flag = true; });
// Act
_orderBusinessLogicContract.DeleteOrder(id);
// Assert
_orderStorageContact.Verify(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)), Times.Once);
_orderStorageContact.Verify(x => x.DeleteElement(order), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(order.Id, out _), Is.True);
Assert.That(!order.CustomerName.IsEmpty());
Assert.That(order.TotalAmount >= 0);
Assert.That(order.DiscountAmount >= 0);
}
[Test]
public void DeleteOrder_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
_orderStorageContact.Setup(x => x.GetOrders()).Returns(new List<OrderDataModel>());
_orderStorageContact.Setup(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)))
.Throws(new ElementNotFoundException("Order not found"));
var id = Guid.NewGuid().ToString();
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns((OrderDataModel)null);
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(id), Throws.TypeOf<ElementNotFoundException>());
_orderStorageContact.Verify(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)), Times.Once);
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -447,8 +470,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(string.Empty),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(string.Empty), Throws.TypeOf<ArgumentNullException>());
_orderStorageContact.Verify(x => x.DeleteElement(It.IsAny<OrderDataModel>()), Times.Never);
}
@@ -464,13 +486,15 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void DeleteOrder_StorageThrowError_ThrowException_Test()
{
// Arrange
_orderStorageContact.Setup(x => x.DeleteElement(It.IsAny<OrderDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
var id = Guid.NewGuid().ToString();
var order = new OrderDataModel(id, "John Doe", DateTime.Now, 100.50m, 10.50m, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), StatusType.Pending);
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns(order);
_orderStorageContact.Setup(x => x.DeleteElement(order)).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.DeleteElement(It.IsAny<OrderDataModel>()), Times.Once);
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(id), Throws.TypeOf<StorageException>());
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_orderStorageContact.Verify(x => x.DeleteElement(order), Times.Once);
}
}
}

View File

@@ -56,7 +56,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
Guid.NewGuid().ToString(), // Use Guid for Position
1.5m,
new List<ProductDataModel>
{
@@ -74,7 +74,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
new PekarDataModel(
Guid.NewGuid().ToString(),
"Maria Petrova",
"Pastry Chef",
Guid.NewGuid().ToString(), // Use Guid for Position
1.8m,
new List<ProductDataModel>
{
@@ -92,6 +92,10 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
};
_pekarStorageContact.Setup(x => x.GetList()).Returns(pekars);
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekars[0].ProductsItems[0]);
_positionStorageContact.Setup(x => x.GetElementById(pekars[0].Position))
.Returns(new PositionDataModel(pekars[0].Position, PositionType.Cool, "Baking position"));
_positionStorageContact.Setup(x => x.GetElementById(pekars[1].Position))
.Returns(new PositionDataModel(pekars[1].Position, PositionType.Cool, "Pastry Chef position"));
// Act
var list = _pekarBusinessLogicContract.GetAllPekars();
@@ -101,7 +105,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(list, Is.EquivalentTo(pekars));
Assert.That(
list.All(p =>
Guid.TryParse(p.Id, out _) && !p.FIO.IsEmpty() && !p.Position.IsEmpty() &&
Guid.TryParse(p.Id, out _) && !p.FIO.IsEmpty() && Guid.TryParse(p.Position, out _) &&
p.BonusCoefficient > 0), Is.True);
}
@@ -148,29 +152,17 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Arrange
var pekarId = Guid.NewGuid().ToString();
var positionId = Guid.NewGuid().ToString();
var historyRecords = new List<PekarHistoryDataModel>
var historyRecords = new List<PekarDataModel>
{
new PekarHistoryDataModel(
pekarId,
"Ivan Ivanov",
positionId.ToString(),
1.5m,
DateTime.Now.AddDays(-30)
),
new PekarHistoryDataModel(
pekarId,
"Ivan Ivanov",
positionId.ToString(),
1.7m,
DateTime.Now.AddDays(-15)
)
new PekarDataModel(pekarId, "Ivan Ivanov", positionId, 1.5m, new List<ProductDataModel>()),
new PekarDataModel(pekarId, "Ivan Ivanov", positionId, 1.7m, new List<ProductDataModel>())
};
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
"Baker",
positionId,
1.8m,
new List<ProductDataModel>
{
@@ -195,8 +187,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list.Count,
Is.EqualTo(historyRecords.Count));
Assert.That(2, Is.EqualTo(historyRecords.Count));
Assert.That(
list.All(h =>
Guid.TryParse(h.Id, out _) && !h.FIO.IsEmpty() && Guid.TryParse(h.Position, out _) &&
@@ -220,7 +211,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void GetAllDataOfPekar_NotFoundPekar_ThrowException_Test()
{
// Arrange
var pekarId = "nonexistent";
var pekarId = Guid.NewGuid().ToString();
_pekarStorageContact.Setup(x => x.GetPekarWithHistory(pekarId)).Returns(new List<PekarDataModel>());
// Act & Assert
@@ -247,13 +238,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
var positionId = "999"; // Invalid position ID
var positionId = Guid.NewGuid().ToString();
var pekarsWithHistory = new List<PekarDataModel>
{
new PekarDataModel(
pekarId,
"Ivan Ivanov",
"Baker",
positionId,
1.8m,
new List<ProductDataModel>
{
@@ -286,10 +277,11 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
var id = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var positionId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
"Baker",
positionId,
1.5m,
new List<ProductDataModel>
{
@@ -306,6 +298,8 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
);
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act
var element = _pekarBusinessLogicContract.GetPekarByData(id);
@@ -316,7 +310,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(Guid.TryParse(element.Id, out _), Is.True);
Assert.That(!element.FIO.IsEmpty());
Assert.That(Regex.IsMatch(element.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
Assert.That(!element.Position.IsEmpty());
Assert.That(Guid.TryParse(element.Position, out _), Is.True);
Assert.That(element.BonusCoefficient > 0);
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
@@ -335,13 +329,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void GetPekarByData_NotFoundPekar_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString();
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.GetPekarByData(id),
Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -363,11 +357,12 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Arrange
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var positionId = Guid.NewGuid().ToString();
var flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
positionId,
1.5m,
new List<ProductDataModel>
{
@@ -382,7 +377,9 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
)
}
);
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id))
.Returns((PekarDataModel)null);
_pekarStorageContact.Setup(x => x.AddElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)))
.Callback((PekarDataModel x) =>
{
flag = x.Id == pekar.Id && x.FIO == pekar.FIO && x.Position == pekar.Position &&
@@ -390,17 +387,19 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
x.ProductsItems.SequenceEqual(pekar.ProductsItems);
});
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act
_pekarBusinessLogicContract.InsertPekar(pekar);
// Assert
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Once);
_pekarStorageContact.Verify(x => x.AddElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(pekar.Id, out _), Is.True);
Assert.That(!pekar.FIO.IsEmpty());
Assert.That(Regex.IsMatch(pekar.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
Assert.That(!pekar.Position.IsEmpty());
Assert.That(Guid.TryParse(pekar.Position, out _), Is.True);
Assert.That(pekar.BonusCoefficient > 0);
}
@@ -408,30 +407,42 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertPekar_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
positionId,
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
var existingPekar = new PekarDataModel(pekar.Id, "Old Name", positionId, 1.0m, new List<ProductDataModel>
{
new ProductDataModel(productId, "Old Cake", "Old cake", new List<IngredientDataModel>())
});
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id)).Returns(existingPekar);
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new ElementExistsException("ID", pekar.Id));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(existingPekar.ProductsItems[0]); // Mock product existence
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<ElementExistsException>());
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(pekar.Id), Times.Once);
}
[Test]
@@ -448,18 +459,18 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(new PekarDataModel(
"",
"123",
"",
"123",
Guid.NewGuid().ToString(),
-1.0m,
new List<ProductDataModel>
new List<ProductDataModel>
{
new ProductDataModel(
"",
"",
"",
"",
"",
"",
new List<IngredientDataModel>
{
new IngredientDataModel("", "Sugar", "kg", -100) // Invalid Ingredient
new IngredientDataModel("", "Sugar", "kg", -100)
}
)
}
@@ -471,43 +482,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertPekar_StorageThrowError_ThrowException_Test()
{
// Arrange
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
)
}
);
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Once);
}
[Test]
public void UpdatePekar_CorrectRecord_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
positionId,
1.5m,
new List<ProductDataModel>
{
@@ -522,7 +503,47 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
)
}
);
_pekarStorageContact.Setup(x => x.UpdateElement(It.IsAny<PekarDataModel>()))
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id)).Returns((PekarDataModel)null);
_pekarStorageContact.Setup(x => x.AddElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_productStorageContact.Setup(x => x.GetElementById(productId))
.Returns(pekar.ProductsItems[0]); // Mock product existence
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.InsertPekar(pekar), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(pekar.Id), Times.Once);
_pekarStorageContact.Verify(x => x.AddElement(It.IsAny<PekarDataModel>()), Times.Once);
}
[Test]
public void UpdatePekar_CorrectRecord_Test()
{
// Arrange
var productId = Guid.NewGuid().ToString();
var ingredientId = Guid.NewGuid().ToString();
var positionId = Guid.NewGuid().ToString();
var flag = false;
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
positionId,
1.5m,
new List<ProductDataModel>
{
new ProductDataModel(
productId,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
)
}
);
_pekarStorageContact.Setup(x => x.UpdateElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)))
.Callback((PekarDataModel x) =>
{
flag = x.Id == pekar.Id && x.FIO == pekar.FIO && x.Position == pekar.Position &&
@@ -530,17 +551,19 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
x.ProductsItems.SequenceEqual(pekar.ProductsItems);
});
_productStorageContact.Setup(x => x.GetElementById(productId)).Returns(pekar.ProductsItems[0]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
// Act
_pekarBusinessLogicContract.UpdatePekar(pekar);
// Assert
_pekarStorageContact.Verify(x => x.UpdateElement(It.IsAny<PekarDataModel>()), Times.Once);
_pekarStorageContact.Verify(x => x.UpdateElement(It.Is<PekarDataModel>(p => p.Id == pekar.Id)), Times.Once);
Assert.That(flag);
Assert.That(Guid.TryParse(pekar.Id, out _), Is.True);
Assert.That(!pekar.FIO.IsEmpty());
Assert.That(Regex.IsMatch(pekar.FIO, @"^[A-Za-zА-Яа-яЁё\s\-]+$"), Is.True);
Assert.That(!pekar.Position.IsEmpty());
Assert.That(Guid.TryParse(pekar.Position, out _), Is.True);
Assert.That(pekar.BonusCoefficient > 0);
}
@@ -548,10 +571,11 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdatePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
positionId,
1.5m,
new List<ProductDataModel>
{
@@ -566,6 +590,10 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
)
}
);
_productStorageContact.Setup(x => x.GetElementById(pekar.ProductsItems[0].Id))
.Returns(pekar.ProductsItems[0]);
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
_pekarStorageContact.Setup(x => x.UpdateElement(It.IsAny<PekarDataModel>()))
.Throws(new ElementNotFoundException("Pekar not found"));
@@ -590,17 +618,17 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(() => _pekarBusinessLogicContract.UpdatePekar(new PekarDataModel(
"",
"123",
"",
-1.0m,
new List<ProductDataModel>
Guid.NewGuid().ToString(),
-1.0m,
new List<ProductDataModel>
{
new ProductDataModel(
"",
"",
"",
"",
"",
"",
new List<IngredientDataModel>
{
new IngredientDataModel("", "Sugar", "kg", -100) // Invalid Ingredient
new IngredientDataModel("", "Sugar", "kg", -100)
}
)
}
@@ -612,10 +640,11 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void UpdatePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
var positionId = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
Guid.NewGuid().ToString(),
"Ivan Ivanov",
"Baker",
positionId,
1.5m,
new List<ProductDataModel>
{
@@ -630,8 +659,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
)
}
);
_pekarStorageContact.Setup(x => x.GetElementById(pekar.Id)).Returns(pekar); // Ensure Pekar exists
_pekarStorageContact.Setup(x => x.UpdateElement(It.IsAny<PekarDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_positionStorageContact.Setup(x => x.GetElementById(positionId))
.Returns(new PositionDataModel(positionId, PositionType.Cool, "Baking position"));
_productStorageContact.Setup(x => x.GetElementById(pekar.ProductsItems[0].Id))
.Returns(pekar.ProductsItems[0]);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.UpdatePekar(pekar), Throws.TypeOf<StorageException>());
@@ -648,7 +682,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
"Baker",
Guid.NewGuid().ToString(),
1.5m,
new List<ProductDataModel>
{
@@ -686,12 +720,12 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void DeletePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString();
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -701,28 +735,41 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<PekarDataModel>().Id), Times.Never);
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePekar_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should prevent it
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar("invalid"), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<PekarDataModel>().Id), Times.Never);
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeletePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.DeleteElement(It.IsAny<PekarDataModel>().Id))
var id = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
Guid.NewGuid().ToString(),
1.5m,
new List<ProductDataModel>()
);
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_pekarStorageContact.Setup(x => x.DeleteElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.DeleteElement(It.IsAny<PekarDataModel>().Id), Times.Once);
Assert.That(() => _pekarBusinessLogicContract.DeletePekar(id), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
[Test]
@@ -735,7 +782,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
"Baker",
Guid.NewGuid().ToString(),
1.5m,
new List<ProductDataModel>
{
@@ -773,12 +820,12 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void RestorePekar_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString();
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<ElementNotFoundException>());
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -788,29 +835,41 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<PekarDataModel>().Id), Times.Never);
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePekar_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should prevent it
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar("invalid"),
Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<PekarDataModel>().Id), Times.Never);
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<ValidationException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void RestorePekar_StorageThrowError_ThrowException_Test()
{
// Arrange
_pekarStorageContact.Setup(x => x.RestoreElement(It.IsAny<PekarDataModel>().Id))
var id = Guid.NewGuid().ToString();
var pekar = new PekarDataModel(
id,
"Ivan Ivanov",
Guid.NewGuid().ToString(),
1.5m,
new List<ProductDataModel>()
);
_pekarStorageContact.Setup(x => x.GetElementById(id)).Returns(pekar);
_pekarStorageContact.Setup(x => x.RestoreElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.RestoreElement(It.IsAny<PekarDataModel>().Id), Times.Once);
Assert.That(() => _pekarBusinessLogicContract.RestorePekar(id), Throws.TypeOf<StorageException>());
_pekarStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.RestoreElement(id), Times.Once);
}
}
}

View File

@@ -146,13 +146,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void GetPositionByData_NotFoundPosition_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString(); // Use a valid Guid to test not found scenario
_positionStorageContact.Setup(x => x.GetElementById(id)).Returns((PositionDataModel)null);
// Act & Assert
Assert.That(() => _positionBusinessLogicContract.GetPositionByData(id),
Throws.TypeOf<ElementNotFoundException>());
_positionStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_positionStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -368,13 +368,13 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void DeletePosition_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString(); // Use a valid Guid to test not found scenario
_positionStorageContact.Setup(x => x.GetElementById(id)).Returns((PositionDataModel)null);
// Act & Assert
Assert.That(() => _positionBusinessLogicContract.DeletePosition(id),
Throws.TypeOf<ElementNotFoundException>());
_positionStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_positionStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -391,23 +391,31 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
[Test]
public void DeletePosition_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should be after existence check
// Act & Assert
Assert.That(() => _positionBusinessLogicContract.DeletePosition("invalid"),
Assert.That(() => _positionBusinessLogicContract.DeletePosition(id),
Throws.TypeOf<ValidationException>());
_positionStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
_positionStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeletePosition_StorageThrowError_ThrowException_Test()
{
// Arrange
_positionStorageContact.Setup(x => x.DeleteElement(It.IsAny<string>()))
var id = Guid.NewGuid().ToString();
var position = new PositionDataModel(id, PositionType.Cool, "Baker");
_positionStorageContact.Setup(x => x.GetElementById(id)).Returns(position);
_positionStorageContact.Setup(x => x.DeleteElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _positionBusinessLogicContract.DeletePosition(Guid.NewGuid().ToString()),
Assert.That(() => _positionBusinessLogicContract.DeletePosition(id),
Throws.TypeOf<StorageException>());
_positionStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
_positionStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_positionStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
}
}

View File

@@ -152,7 +152,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(elementById.IngredientsItems.Count > 0);
Assert.That(elementById.OldName, Is.EqualTo("Cake"));
Assert.That(elementById.OldDescription, Is.EqualTo("Delicious cake"));
_productStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_productStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
@@ -239,7 +239,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void GetProductByData_NotFoundProduct_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString();
_productStorageContact.Setup(x => x.GetElementById(id)).Returns((ProductDataModel)null);
_productStorageContact.Setup(x => x.GetList()).Returns(new List<ProductDataModel>());
@@ -247,25 +247,23 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(() => _productBusinessLogicContract.GetProductByData(id), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _productBusinessLogicContract.GetProductByData("NonExistentProduct"), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _productBusinessLogicContract.GetProductByData("OldNonExistent"), Throws.TypeOf<ElementNotFoundException>());
_productStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_productStorageContact.Verify(x => x.GetList(), Times.AtLeast(2));
_productStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_productStorageContact.Verify(x => x.GetList(), Times.AtLeast(2));
}
[Test]
public void GetProductByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_productStorageContact.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
var id = Guid.NewGuid().ToString();
_productStorageContact.Setup(x => x.GetElementById(id)).Throws(new StorageException(new InvalidOperationException()));
_productStorageContact.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _productBusinessLogicContract.GetProductByData("Cake"), Throws.TypeOf<StorageException>());
Assert.That(() => _productBusinessLogicContract.GetProductByData("OldCake"), Throws.TypeOf<StorageException>());
_productStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_productStorageContact.Verify(x => x.GetList(), Times.AtLeast(2));
Assert.That(() => _productBusinessLogicContract.GetProductByData(id), Throws.TypeOf<StorageException>());
_productStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_productStorageContact.Verify(x => x.GetList(), Times.Never);
}
[Test]
public void InsertProduct_CorrectRecord_Test()
{
@@ -313,19 +311,21 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertProduct_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var product = new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
);
product.Name = "Updated Cake";
product.Description = "Updated delicious cake";
_productStorageContact.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
.Throws(new ElementExistsException("ID", product.Id));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(product.IngredientsItems[0]);
// Act & Assert
Assert.That(() => _productBusinessLogicContract.InsertProduct(product), Throws.TypeOf<ElementExistsException>());
@@ -360,19 +360,21 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void InsertProduct_StorageThrowError_ThrowException_Test()
{
// Arrange
var ingredientId = Guid.NewGuid().ToString();
var product = new ProductDataModel(
Guid.NewGuid().ToString(),
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
new IngredientDataModel(ingredientId, "Sugar", "kg", 100)
}
);
product.Name = "Updated Cake";
product.Description = "Updated delicious cake";
_productStorageContact.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementById(ingredientId)).Returns(product.IngredientsItems[0]);
// Act & Assert
Assert.That(() => _productBusinessLogicContract.InsertProduct(product), Throws.TypeOf<StorageException>());
@@ -439,6 +441,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
product.Description = "Updated delicious cake";
_productStorageContact.Setup(x => x.UpdateElement(It.IsAny<ProductDataModel>()))
.Throws(new ElementNotFoundException("Product not found"));
_ingredientStorageContact.Setup(x => x.GetElementById(product.IngredientsItems[0].Id)).Returns(product.IngredientsItems[0]); // Mock ingredient existence
// Act & Assert
Assert.That(() => _productBusinessLogicContract.UpdateProduct(product), Throws.TypeOf<ElementNotFoundException>());
@@ -486,6 +489,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
product.Description = "Updated delicious cake";
_productStorageContact.Setup(x => x.UpdateElement(It.IsAny<ProductDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
_ingredientStorageContact.Setup(x => x.GetElementById(product.IngredientsItems[0].Id)).Returns(product.IngredientsItems[0]); // Mock ingredient existence
// Act & Assert
Assert.That(() => _productBusinessLogicContract.UpdateProduct(product), Throws.TypeOf<StorageException>());
@@ -533,40 +537,50 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
public void DeleteProduct_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString(); // Use a valid Guid
_productStorageContact.Setup(x => x.GetElementById(id)).Returns((ProductDataModel)null);
// Act & Assert
Assert.That(() => _productBusinessLogicContract.DeleteProduct(id), Throws.TypeOf<ElementNotFoundException>());
_productStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteProduct_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _productBusinessLogicContract.DeleteProduct(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productBusinessLogicContract.DeleteProduct(string.Empty), Throws.TypeOf<ArgumentNullException>());
_productStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
_productStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void DeleteProduct_InvalidId_ThrowException_Test()
{
// Arrange
var id = "invalid";
// No need to setup GetElementById for invalid ID since validation should occur first
// Act & Assert
Assert.That(() => _productBusinessLogicContract.DeleteProduct("invalid"), Throws.TypeOf<ValidationException>());
_productStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
Assert.That(() => _productBusinessLogicContract.DeleteProduct(id), Throws.TypeOf<ValidationException>());
_productStorageContact.Verify(x => x.GetElementById(id), Times.Never); // Should not call GetElementById for invalid GUID
}
[Test]
public void DeleteProduct_StorageThrowError_ThrowException_Test()
{
// Arrange
_productStorageContact.Setup(x => x.DeleteElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
var id = Guid.NewGuid().ToString();
var product = new ProductDataModel(
id,
"Cake",
"Delicious cake",
new List<IngredientDataModel>
{
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)
}
);
product.Name = "Updated Cake";
product.Description = "Updated delicious cake";
_productStorageContact.Setup(x => x.GetElementById(id)).Returns(product);
_productStorageContact.Setup(x => x.DeleteElement(id))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_productStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
Assert.That(() => _productBusinessLogicContract.DeleteProduct(id), Throws.TypeOf<StorageException>());
_productStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_productStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
}
}

View File

@@ -40,7 +40,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
}
[Test]
public void GetList_ReturnsListOfRecords_Test()
public void GetAllSalaries_ReturnsListOfRecords_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
@@ -67,22 +67,23 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act
var list = _salaryBusinessLogicContract.GetList();
var list = _salaryBusinessLogicContract.GetAllSalaries();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(salaries));
Assert.That(list.All(s => Guid.TryParse(s.Id, out _) && Guid.TryParse(s.PekarId, out _) && s.BaseRate >= 0 && s.BonusRate >= 0 && s.TotalSalary >= 0), Is.True);
_salaryStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetList_ReturnsEmptyList_Test()
public void GetAllSalaries_ReturnsEmptyList_Test()
{
// Arrange
_salaryStorageContact.Setup(x => x.GetList()).Returns(new List<SalaryDataModel>());
// Act
var list = _salaryBusinessLogicContract.GetList();
var list = _salaryBusinessLogicContract.GetAllSalaries();
// Assert
Assert.That(list, Is.Not.Null);
@@ -91,29 +92,29 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
}
[Test]
public void GetList_ReturnsNull_ThrowException_Test()
public void GetAllSalaries_ReturnsNull_ThrowException_Test()
{
// Arrange
_salaryStorageContact.Setup(x => x.GetList()).Returns((List<SalaryDataModel>)null);
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.GetList(), Throws.TypeOf<NullListException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalaries(), Throws.TypeOf<NullListException>());
_salaryStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetList_StorageThrowError_ThrowException_Test()
public void GetAllSalaries_StorageThrowError_ThrowException_Test()
{
// Arrange
_salaryStorageContact.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.GetList(), Throws.TypeOf<StorageException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalaries(), Throws.TypeOf<StorageException>());
_salaryStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetElementById_ReturnsSalaryById_Test()
public void GetSalaryByData_ReturnsSalaryById_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
@@ -130,7 +131,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act
var element = _salaryBusinessLogicContract.GetElementById(id);
var element = _salaryBusinessLogicContract.GetSalaryByData(id);
// Assert
Assert.That(element, Is.Not.Null);
@@ -140,43 +141,71 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(element.BaseRate >= 0);
Assert.That(element.BonusRate >= 0);
Assert.That(element.TotalSalary >= 0);
_salaryStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_salaryStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(pekarId), Times.Once);
}
[Test]
public void GetElementById_EmptyId_ThrowException_Test()
public void GetSalaryByData_EmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.GetElementById(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.GetElementById(string.Empty), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.GetSalaryByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.GetSalaryByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_salaryStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetElementById_NotFoundSalary_ThrowException_Test()
public void GetSalaryByData_InvalidId_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
_salaryStorageContact.Setup(x => x.GetElementById(id)).Returns((SalaryDataModel)null);
var id = "invalid";
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.GetElementById(id), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
Assert.That(() => _salaryBusinessLogicContract.GetSalaryByData(id), Throws.TypeOf<ValidationException>());
_salaryStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void GetElementById_StorageThrowError_ThrowException_Test()
public void GetSalaryByData_NotFoundSalary_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_salaryStorageContact.Setup(x => x.GetElementById(id)).Returns((SalaryDataModel)null);
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.GetSalaryByData(id), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void GetSalaryByData_PekarNotFound_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var pekarId = Guid.NewGuid().ToString();
var salary = new SalaryDataModel(id, pekarId, DateTime.Now, 1000.00m, 200.00m, 1200.00m);
_salaryStorageContact.Setup(x => x.GetElementById(id)).Returns(salary);
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.GetSalaryByData(id), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(pekarId), Times.Once);
}
[Test]
public void GetSalaryByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_salaryStorageContact.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.GetElementById(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _salaryBusinessLogicContract.GetSalaryByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_salaryStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void AddElement_CorrectRecord_Test()
public void InsertSalary_CorrectRecord_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
@@ -198,7 +227,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act
_salaryBusinessLogicContract.AddElement(salary);
_salaryBusinessLogicContract.InsertSalary(salary);
// Assert
_salaryStorageContact.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Once);
@@ -208,10 +237,31 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(salary.BaseRate >= 0);
Assert.That(salary.BonusRate >= 0);
Assert.That(salary.TotalSalary >= 0);
_pekarStorageContact.Verify(x => x.GetElementById(pekarId), Times.Once);
}
[Test]
public void AddElement_RecordWithExistsData_ThrowException_Test()
public void InsertSalary_PekarNotFound_ThrowException_Test()
{
// Arrange
var salary = new SalaryDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
DateTime.Now,
1000.00m,
200.00m,
1200.00m
);
_pekarStorageContact.Setup(x => x.GetElementById(salary.PekarId)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.InsertSalary(salary), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Never);
_pekarStorageContact.Verify(x => x.GetElementById(salary.PekarId), Times.Once);
}
[Test]
public void InsertSalary_RecordWithExistsData_ThrowException_Test()
{
// Arrange
var salary = new SalaryDataModel(
@@ -223,25 +273,28 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
1200.00m
);
_salaryStorageContact.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>())).Throws(new ElementExistsException("ID", salary.Id));
_pekarStorageContact.Setup(x => x.GetElementById(salary.PekarId)).Returns(new PekarDataModel(salary.PekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.AddElement(salary), Throws.TypeOf<ElementExistsException>());
Assert.That(() => _salaryBusinessLogicContract.InsertSalary(salary), Throws.TypeOf<ElementExistsException>());
_salaryStorageContact.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(salary.PekarId), Times.Once);
}
[Test]
public void AddElement_NullRecord_ThrowException_Test()
public void InsertSalary_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.AddElement(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.InsertSalary(null), Throws.TypeOf<ArgumentNullException>());
_salaryStorageContact.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Never);
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void AddElement_InvalidRecord_ThrowException_Test()
public void InsertSalary_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.AddElement(new SalaryDataModel(
Assert.That(() => _salaryBusinessLogicContract.InsertSalary(new SalaryDataModel(
"",
"",
DateTime.Now,
@@ -250,10 +303,11 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
-1200.00m
)), Throws.TypeOf<ValidationException>());
_salaryStorageContact.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Never);
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void AddElement_StorageThrowError_ThrowException_Test()
public void InsertSalary_StorageThrowError_ThrowException_Test()
{
// Arrange
var salary = new SalaryDataModel(
@@ -265,14 +319,16 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
1200.00m
);
_salaryStorageContact.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_pekarStorageContact.Setup(x => x.GetElementById(salary.PekarId)).Returns(new PekarDataModel(salary.PekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.AddElement(salary), Throws.TypeOf<StorageException>());
Assert.That(() => _salaryBusinessLogicContract.InsertSalary(salary), Throws.TypeOf<StorageException>());
_salaryStorageContact.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(salary.PekarId), Times.Once);
}
[Test]
public void UpdateElement_CorrectRecord_Test()
public void UpdateSalary_CorrectRecord_Test()
{
// Arrange
var pekarId = Guid.NewGuid().ToString();
@@ -294,7 +350,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act
_salaryBusinessLogicContract.UpdateElement(salary);
_salaryBusinessLogicContract.UpdateSalary(salary);
// Assert
_salaryStorageContact.Verify(x => x.UpdateElement(It.IsAny<SalaryDataModel>()), Times.Once);
@@ -304,10 +360,31 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
Assert.That(salary.BaseRate >= 0);
Assert.That(salary.BonusRate >= 0);
Assert.That(salary.TotalSalary >= 0);
_pekarStorageContact.Verify(x => x.GetElementById(pekarId), Times.Once);
}
[Test]
public void UpdateElement_RecordNotFound_ThrowException_Test()
public void UpdateSalary_PekarNotFound_ThrowException_Test()
{
// Arrange
var salary = new SalaryDataModel(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
DateTime.Now,
1000.00m,
200.00m,
1200.00m
);
_pekarStorageContact.Setup(x => x.GetElementById(salary.PekarId)).Returns((PekarDataModel)null);
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.UpdateSalary(salary), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.UpdateElement(It.IsAny<SalaryDataModel>()), Times.Never);
_pekarStorageContact.Verify(x => x.GetElementById(salary.PekarId), Times.Once);
}
[Test]
public void UpdateSalary_RecordNotFound_ThrowException_Test()
{
// Arrange
var salary = new SalaryDataModel(
@@ -319,25 +396,28 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
1200.00m
);
_salaryStorageContact.Setup(x => x.UpdateElement(It.IsAny<SalaryDataModel>())).Throws(new ElementNotFoundException("Salary not found"));
_pekarStorageContact.Setup(x => x.GetElementById(salary.PekarId)).Returns(new PekarDataModel(salary.PekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.UpdateElement(salary), Throws.TypeOf<ElementNotFoundException>());
Assert.That(() => _salaryBusinessLogicContract.UpdateSalary(salary), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.UpdateElement(It.IsAny<SalaryDataModel>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(salary.PekarId), Times.Once);
}
[Test]
public void UpdateElement_NullRecord_ThrowException_Test()
public void UpdateSalary_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.UpdateElement(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.UpdateSalary(null), Throws.TypeOf<ArgumentNullException>());
_salaryStorageContact.Verify(x => x.UpdateElement(It.IsAny<SalaryDataModel>()), Times.Never);
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void UpdateElement_InvalidRecord_ThrowException_Test()
public void UpdateSalary_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.UpdateElement(new SalaryDataModel(
Assert.That(() => _salaryBusinessLogicContract.UpdateSalary(new SalaryDataModel(
"",
"",
DateTime.Now,
@@ -346,10 +426,11 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
-1200.00m
)), Throws.TypeOf<ValidationException>());
_salaryStorageContact.Verify(x => x.UpdateElement(It.IsAny<SalaryDataModel>()), Times.Never);
_pekarStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void UpdateElement_StorageThrowError_ThrowException_Test()
public void UpdateSalary_StorageThrowError_ThrowException_Test()
{
// Arrange
var salary = new SalaryDataModel(
@@ -361,14 +442,16 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
1200.00m
);
_salaryStorageContact.Setup(x => x.UpdateElement(It.IsAny<SalaryDataModel>())).Throws(new StorageException(new InvalidOperationException()));
_pekarStorageContact.Setup(x => x.GetElementById(salary.PekarId)).Returns(new PekarDataModel(salary.PekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.UpdateElement(salary), Throws.TypeOf<StorageException>());
Assert.That(() => _salaryBusinessLogicContract.UpdateSalary(salary), Throws.TypeOf<StorageException>());
_salaryStorageContact.Verify(x => x.UpdateElement(It.IsAny<SalaryDataModel>()), Times.Once);
_pekarStorageContact.Verify(x => x.GetElementById(salary.PekarId), Times.Once);
}
[Test]
public void DeleteElement_CorrectId_Test()
public void DeleteSalary_CorrectId_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
@@ -387,7 +470,7 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act
_salaryBusinessLogicContract.DeleteElement(id);
_salaryBusinessLogicContract.DeleteSalary(id);
// Assert
_salaryStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
@@ -400,43 +483,53 @@ namespace CandyHouseTests.BusinessLogicsContractsTests
}
[Test]
public void DeleteElement_RecordNotFound_ThrowException_Test()
public void DeleteSalary_RecordNotFound_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
var id = Guid.NewGuid().ToString();
_salaryStorageContact.Setup(x => x.GetElementById(id)).Returns((SalaryDataModel)null);
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.DeleteElement(id), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
Assert.That(() => _salaryBusinessLogicContract.DeleteSalary(id), Throws.TypeOf<ElementNotFoundException>());
_salaryStorageContact.Verify(x => x.GetElementById(id), Times.Once);
}
[Test]
public void DeleteElement_NullOrEmptyId_ThrowException_Test()
public void DeleteSalary_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.DeleteElement(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.DeleteElement(string.Empty), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.DeleteSalary(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.DeleteSalary(string.Empty), Throws.TypeOf<ArgumentNullException>());
_salaryStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteElement_InvalidId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.DeleteElement("invalid"), Throws.TypeOf<ValidationException>());
_salaryStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteElement_StorageThrowError_ThrowException_Test()
public void DeleteSalary_InvalidId_ThrowException_Test()
{
// Arrange
_salaryStorageContact.Setup(x => x.DeleteElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
var id = "invalid";
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.DeleteElement(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_salaryStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
Assert.That(() => _salaryBusinessLogicContract.DeleteSalary(id), Throws.TypeOf<ValidationException>());
_salaryStorageContact.Verify(x => x.DeleteElement(id), Times.Never);
_salaryStorageContact.Verify(x => x.GetElementById(id), Times.Never);
}
[Test]
public void DeleteSalary_StorageThrowError_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var pekarId = Guid.NewGuid().ToString();
var salary = new SalaryDataModel(id, pekarId, DateTime.Now, 1000.00m, 200.00m, 1200.00m);
_salaryStorageContact.Setup(x => x.GetElementById(id)).Returns(salary);
_salaryStorageContact.Setup(x => x.DeleteElement(id)).Throws(new StorageException(new InvalidOperationException()));
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
// Act & Assert
Assert.That(() => _salaryBusinessLogicContract.DeleteSalary(id), Throws.TypeOf<StorageException>());
_salaryStorageContact.Verify(x => x.GetElementById(id), Times.Once);
_salaryStorageContact.Verify(x => x.DeleteElement(id), Times.Once);
}
}
}

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="8.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit3TestAdapter.5.0.0\build\net462\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.5.0.0\build\net462\NUnit3TestAdapter.props')" />
<Import Project="..\packages\Microsoft.Testing.Extensions.Telemetry.1.5.3\build\netstandard2.0\Microsoft.Testing.Extensions.Telemetry.props" Condition="Exists('..\packages\Microsoft.Testing.Extensions.Telemetry.1.5.3\build\netstandard2.0\Microsoft.Testing.Extensions.Telemetry.props')" />
<Import Project="..\packages\Microsoft.Testing.Platform.MSBuild.1.5.3\build\Microsoft.Testing.Platform.MSBuild.props" Condition="Exists('..\packages\Microsoft.Testing.Platform.MSBuild.1.5.3\build\Microsoft.Testing.Platform.MSBuild.props')" />
@@ -94,6 +94,9 @@
<HintPath>..\packages\Moq.4.20.72\lib\net462\Moq.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.4.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.6.0\lib\net462\System.Buffers.dll</HintPath>
@@ -108,6 +111,9 @@
<Reference Include="System.Diagnostics.DiagnosticSource, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.10.0.0-preview.1.25080.5\lib\net462\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="System.IO.Pipelines, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Pipelines.10.0.0-preview.1.25080.5\lib\net462\System.IO.Pipelines.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.6.0\lib\net462\System.Memory.dll</HintPath>
</Reference>
@@ -124,6 +130,12 @@
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.1.0\lib\net462\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Text.Encodings.Web, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.10.0.0-preview.1.25080.5\lib\net462\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.10.0.0-preview.1.25080.5\lib\net462\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.6.0\lib\net462\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>

View File

@@ -9,6 +9,7 @@
<package id="Microsoft.Extensions.Logging.Abstractions" version="10.0.0-preview.1.25080.5" targetFramework="net471" />
<package id="Microsoft.Extensions.Options" version="10.0.0-preview.1.25080.5" targetFramework="net471" />
<package id="Moq" version="4.20.72" targetFramework="net471" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net471" />
<package id="System.Diagnostics.DiagnosticSource" version="10.0.0-preview.1.25080.5" targetFramework="net471" />
<package id="Microsoft.Extensions.Primitives" version="10.0.0-preview.1.25080.5" targetFramework="net471" />
<package id="Microsoft.TestPlatform.ObjectModel" version="17.12.0" targetFramework="net471" />
@@ -21,10 +22,13 @@
<package id="NUnit3TestAdapter" version="5.0.0" targetFramework="net471" />
<package id="System.Buffers" version="4.6.0" targetFramework="net471" />
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net471" />
<package id="System.IO.Pipelines" version="10.0.0-preview.1.25080.5" targetFramework="net471" />
<package id="System.Memory" version="4.6.0" targetFramework="net471" />
<package id="System.Numerics.Vectors" version="4.6.0" targetFramework="net471" />
<package id="System.Reflection.Metadata" version="1.6.0" targetFramework="net471" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.1.0" targetFramework="net471" />
<package id="System.Text.Encodings.Web" version="10.0.0-preview.1.25080.5" targetFramework="net471" />
<package id="System.Text.Json" version="10.0.0-preview.1.25080.5" targetFramework="net471" />
<package id="System.Threading.Tasks.Extensions" version="4.6.0" targetFramework="net471" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net471" />
</packages>