Compare commits
2 Commits
Task_1_Mod
...
Task_2_Bui
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ee7cc1bef | |||
| a52a37f9e1 |
@@ -0,0 +1,22 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<InternalsVisibleTo Include="AndDietCokeTests" />
|
||||||
|
<InternalsVisibleTo Include ="DynamicProxyGenAssembly2"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AndDietCokeContracts\AndDietCokeContracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace AndDietCokeBusinessLogic.Implementations;
|
||||||
|
internal class ClientBusinessLogicContract(IClientStorageContract clientStorageContract, ILogger logger) : IClientBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly IClientStorageContract _clientStorageContract = clientStorageContract;
|
||||||
|
|
||||||
|
public List<СlientDataModel> GetAllClients()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllClients");
|
||||||
|
return _clientStorageContract.GetList() ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public СlientDataModel GetClientByData(string data)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Get client by data: {data}", data);
|
||||||
|
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.IsGuid())
|
||||||
|
{
|
||||||
|
return _clientStorageContract.GetElementById(data) ??
|
||||||
|
throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Regex.IsMatch(data, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
|
||||||
|
{
|
||||||
|
return _clientStorageContract.GetElementByPhoneNumber(data) ??
|
||||||
|
throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _clientStorageContract.GetElementByFIO(data) ??
|
||||||
|
throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertClient(СlientDataModel clientDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("New client data: {json}",
|
||||||
|
JsonSerializer.Serialize(clientDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(clientDataModel);
|
||||||
|
clientDataModel.Validate();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_clientStorageContract.AddElement(clientDataModel);
|
||||||
|
}
|
||||||
|
catch (ElementExistsException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Failed to insert client - already exists");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateClient(СlientDataModel clientDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Update client data: {json}",
|
||||||
|
JsonSerializer.Serialize(clientDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(clientDataModel);
|
||||||
|
clientDataModel.Validate();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_clientStorageContract.UpdElement(clientDataModel);
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Failed to update client - not found");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteClient(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Delete client by id: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_clientStorageContract.DelElement(id);
|
||||||
|
}
|
||||||
|
catch (ElementNotFoundException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Failed to delete client - not found");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Enums;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace AndDietCokeBusinessLogic.Implementations;
|
||||||
|
|
||||||
|
internal class DishBusinessLogicContract(
|
||||||
|
IDishStorageContracts dishStorageContracts,
|
||||||
|
ILogger<DishBusinessLogicContract> logger) : IDishBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly IDishStorageContracts _dishStorageContract = dishStorageContracts;
|
||||||
|
|
||||||
|
public List<DishDataModel> GetAllDishes()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllDishes called");
|
||||||
|
|
||||||
|
var result = _dishStorageContract.GetList();
|
||||||
|
return result ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DishHistoryDataModel> GetDishHistory(string dishId)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetDishHistory called for dishId: {dishId}", dishId);
|
||||||
|
|
||||||
|
if (dishId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(dishId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dishId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("DishId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = _dishStorageContract.GetHistoryByProductId(dishId);
|
||||||
|
return result ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DishDataModel? GetDishById(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetDishById called with id: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
return _dishStorageContract.GetElementById(id)
|
||||||
|
?? throw new ElementNotFoundException(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DishDataModel? GetDishByName(string name)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetDishByName called with name: {name}", name);
|
||||||
|
|
||||||
|
if (name.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
return _dishStorageContract.GetElementByName(name)
|
||||||
|
?? throw new ElementNotFoundException(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertDish(DishDataModel dishDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("InsertDish called with data: {data}",
|
||||||
|
JsonSerializer.Serialize(dishDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(dishDataModel);
|
||||||
|
dishDataModel.Validate();
|
||||||
|
|
||||||
|
// Проверка на уникальность имени
|
||||||
|
var existingByName = _dishStorageContract.GetElementByName(dishDataModel.Name);
|
||||||
|
if (existingByName != null)
|
||||||
|
{
|
||||||
|
throw new ValidationException($"Dish with name '{dishDataModel.Name}' already exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверка на уникальность ID
|
||||||
|
var existingById = _dishStorageContract.GetElementById(dishDataModel.Id);
|
||||||
|
if (existingById != null)
|
||||||
|
{
|
||||||
|
throw new ValidationException($"Dish with ID {dishDataModel.Id} already exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
_dishStorageContract.AddElement(dishDataModel);
|
||||||
|
_logger.LogInformation("Dish successfully inserted: {id}", dishDataModel.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateDish(DishDataModel dishDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("UpdateDish called with data: {data}",
|
||||||
|
JsonSerializer.Serialize(dishDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(dishDataModel);
|
||||||
|
dishDataModel.Validate();
|
||||||
|
|
||||||
|
// Проверка что блюдо существует
|
||||||
|
var existingDish = _dishStorageContract.GetElementById(dishDataModel.Id);
|
||||||
|
if (existingDish == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(dishDataModel.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверка на уникальность имени (если имя изменилось)
|
||||||
|
if (!existingDish.Name.Equals(dishDataModel.Name, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var dishWithSameName = _dishStorageContract.GetElementByName(dishDataModel.Name);
|
||||||
|
if (dishWithSameName != null)
|
||||||
|
{
|
||||||
|
throw new ValidationException($"Dish with name '{dishDataModel.Name}' already exists");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_dishStorageContract.UpdElement(dishDataModel);
|
||||||
|
_logger.LogInformation("Dish successfully updated: {id}", dishDataModel.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteDish(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("DeleteDish called with id: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверка что блюдо существует
|
||||||
|
var existingDish = _dishStorageContract.GetElementById(id);
|
||||||
|
if (existingDish == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
_dishStorageContract.DelElement(id);
|
||||||
|
_logger.LogInformation("Dish successfully deleted: {id}", id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace AndDietCokeBusinessLogic.Implementations;
|
||||||
|
|
||||||
|
internal class Dish_OrderBusinessLogicContract(
|
||||||
|
IDish_OrderStorageContract dishOrderStorageContract,
|
||||||
|
ILogger<Dish_OrderBusinessLogicContract> logger) : IDish_OrderBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly IDish_OrderStorageContract _dishOrderStorageContract = dishOrderStorageContract;
|
||||||
|
|
||||||
|
public List<Dish_OrderDataModel> GetAllDishOrders(string? orderId = null, string? dishId = null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"GetAllDishOrders called with params: orderId={orderId}, dishId={dishId}",
|
||||||
|
orderId, dishId);
|
||||||
|
|
||||||
|
if (orderId != null && !orderId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("OrderId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dishId != null && !dishId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("DishId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = _dishOrderStorageContract.GetList(orderId, dishId);
|
||||||
|
|
||||||
|
return result ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dish_OrderDataModel? GetDishOrderById(string orderId, string dishId)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"GetDishOrderById called with params: orderId={orderId}, dishId={dishId}",
|
||||||
|
orderId, dishId);
|
||||||
|
|
||||||
|
if (orderId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(orderId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!orderId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("OrderId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dishId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(dishId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dishId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("DishId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
return _dishOrderStorageContract.GetElementById(orderId, dishId)
|
||||||
|
?? throw new ElementNotFoundException($"DishOrder with orderId={orderId} and dishId={dishId} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertDishOrder(Dish_OrderDataModel dishOrderDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"InsertDishOrder called with data: {data}",
|
||||||
|
JsonSerializer.Serialize(dishOrderDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(dishOrderDataModel);
|
||||||
|
dishOrderDataModel.Validate();
|
||||||
|
|
||||||
|
// Проверка на существование записи
|
||||||
|
var existing = _dishOrderStorageContract.GetElementById(
|
||||||
|
dishOrderDataModel.OrderId,
|
||||||
|
dishOrderDataModel.DishId);
|
||||||
|
|
||||||
|
if (existing != null)
|
||||||
|
{
|
||||||
|
throw new ValidationException(
|
||||||
|
$"DishOrder with orderId={dishOrderDataModel.OrderId} and dishId={dishOrderDataModel.DishId} already exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
_dishOrderStorageContract.AddElement(dishOrderDataModel);
|
||||||
|
_logger.LogInformation(
|
||||||
|
"DishOrder successfully inserted: orderId={orderId}, dishId={dishId}",
|
||||||
|
dishOrderDataModel.OrderId, dishOrderDataModel.DishId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateDishOrder(Dish_OrderDataModel dishOrderDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"UpdateDishOrder called with data: {data}",
|
||||||
|
JsonSerializer.Serialize(dishOrderDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(dishOrderDataModel);
|
||||||
|
dishOrderDataModel.Validate();
|
||||||
|
|
||||||
|
// Проверка что запись существует
|
||||||
|
var existing = _dishOrderStorageContract.GetElementById(
|
||||||
|
dishOrderDataModel.OrderId,
|
||||||
|
dishOrderDataModel.DishId);
|
||||||
|
|
||||||
|
if (existing == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(
|
||||||
|
$"DishOrder with orderId={dishOrderDataModel.OrderId} and dishId={dishOrderDataModel.DishId} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
_dishOrderStorageContract.UpdElement(dishOrderDataModel);
|
||||||
|
_logger.LogInformation(
|
||||||
|
"DishOrder successfully updated: orderId={orderId}, dishId={dishId}",
|
||||||
|
dishOrderDataModel.OrderId, dishOrderDataModel.DishId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteDishOrder(string orderId, string dishId)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"DeleteDishOrder called with params: orderId={orderId}, dishId={dishId}",
|
||||||
|
orderId, dishId);
|
||||||
|
|
||||||
|
if (orderId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(orderId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!orderId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("OrderId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dishId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(dishId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dishId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("DishId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверка что запись существует
|
||||||
|
var existing = _dishOrderStorageContract.GetElementById(orderId, dishId);
|
||||||
|
if (existing == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(
|
||||||
|
$"DishOrder with orderId={orderId} and dishId={dishId} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
_dishOrderStorageContract.DelElement(orderId, dishId);
|
||||||
|
_logger.LogInformation(
|
||||||
|
"DishOrder successfully deleted: orderId={orderId}, dishId={dishId}",
|
||||||
|
orderId, dishId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Enums;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace AndDietCokeBusinessLogic.Implementations;
|
||||||
|
|
||||||
|
internal class EmployeeBusinessLogicContract(
|
||||||
|
IEmployeeStoragesContracts employeeStoragesContracts,
|
||||||
|
ILogger<EmployeeBusinessLogicContract> logger) : IEmployeeBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly IEmployeeStoragesContracts _employeeStorageContract = employeeStoragesContracts;
|
||||||
|
|
||||||
|
public List<EmployeeDataModel> GetAllEmployees(
|
||||||
|
bool onlyActive = true,
|
||||||
|
string? positionId = null,
|
||||||
|
DateTime? fromBirthDate = null,
|
||||||
|
DateTime? toBirthDate = null,
|
||||||
|
DateTime? fromEmploymentDate = null,
|
||||||
|
DateTime? toEmploymentDate = null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"GetAllEmployees params: onlyActive={onlyActive}, positionId={positionId}, " +
|
||||||
|
"fromBirthDate={fromBirthDate}, toBirthDate={toBirthDate}, " +
|
||||||
|
"fromEmploymentDate={fromEmploymentDate}, toEmploymentDate={toEmploymentDate}",
|
||||||
|
onlyActive, positionId, fromBirthDate, toBirthDate, fromEmploymentDate, toEmploymentDate);
|
||||||
|
|
||||||
|
if (positionId != null && positionId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(positionId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (positionId != null && !positionId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field positionId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fromBirthDate.HasValue && toBirthDate.HasValue && fromBirthDate > toBirthDate)
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromBirthDate.Value, toBirthDate.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fromEmploymentDate.HasValue && toEmploymentDate.HasValue && fromEmploymentDate > toEmploymentDate)
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromEmploymentDate.Value, toEmploymentDate.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = _employeeStorageContract.GetList(
|
||||||
|
onlyActive,
|
||||||
|
positionId,
|
||||||
|
fromBirthDate,
|
||||||
|
toBirthDate,
|
||||||
|
fromEmploymentDate,
|
||||||
|
toEmploymentDate);
|
||||||
|
|
||||||
|
return result ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeDataModel? GetEmployeeById(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetEmployeeById params: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field id is not a unique identifier.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return _employeeStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmployeeDataModel? GetEmployeeByFIO(string fio)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetEmployeeByFIO params: {fio}", fio);
|
||||||
|
|
||||||
|
if (fio.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(fio));
|
||||||
|
}
|
||||||
|
|
||||||
|
return _employeeStorageContract.GetElementByFIO(fio) ?? throw new ElementNotFoundException(fio);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertEmployee(EmployeeDataModel employeeDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("InsertEmployee params: {json}", JsonSerializer.Serialize(employeeDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(employeeDataModel);
|
||||||
|
employeeDataModel.Validate();
|
||||||
|
|
||||||
|
if (_employeeStorageContract.GetElementById(employeeDataModel.Id) != null)
|
||||||
|
{
|
||||||
|
throw new DuplicateEmployeeException(employeeDataModel.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
_employeeStorageContract.AddElement(employeeDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateEmployee(EmployeeDataModel employeeDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("UpdateEmployee params: {json}", JsonSerializer.Serialize(employeeDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(employeeDataModel);
|
||||||
|
employeeDataModel.Validate();
|
||||||
|
|
||||||
|
if (_employeeStorageContract.GetElementById(employeeDataModel.Id) == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(employeeDataModel.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
_employeeStorageContract.UpdElement(employeeDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteEmployee(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("DeleteEmployee params: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field id is not a unique identifier.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_employeeStorageContract.GetElementById(id) == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
_employeeStorageContract.DelElement(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StorageContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace AndDietCokeBusinessLogic.Implementations;
|
||||||
|
|
||||||
|
internal class OrderBusinessLogicContract(
|
||||||
|
IOrderStorageContracts orderStorageContract,
|
||||||
|
ILogger<OrderBusinessLogicContract> logger) : IOrderBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly IOrderStorageContracts _orderStorageContract = orderStorageContract;
|
||||||
|
|
||||||
|
public List<OrderDataModels> GetAllOrders(string? clientId = null, string? employeedId = null, string? dishId = null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"GetAllOrders called with filters - ClientId: {clientId}, EmployeeId: {employeedId}, DishId: {dishId}",
|
||||||
|
clientId, employeedId, dishId);
|
||||||
|
|
||||||
|
// Валидация параметров фильтрации
|
||||||
|
if (clientId != null && !clientId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("ClientId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (employeedId != null && !employeedId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("EmployeeId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dishId != null && !dishId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("DishId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = _orderStorageContract.GetList(clientId, employeedId, dishId);
|
||||||
|
return result ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderDataModels? GetOrderById(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetOrderById called with id: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("OrderId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
var order = _orderStorageContract.GetElementById(id);
|
||||||
|
return order ?? throw new ElementNotFoundException($"Order with id {id} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertOrder(OrderDataModels orderDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("InsertOrder called with data: {data}",
|
||||||
|
JsonSerializer.Serialize(orderDataModel));
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(orderDataModel);
|
||||||
|
orderDataModel.Validate();
|
||||||
|
|
||||||
|
// Проверка на существование заказа с таким ID
|
||||||
|
var existingOrder = _orderStorageContract.GetElementById(orderDataModel.Id);
|
||||||
|
if (existingOrder != null)
|
||||||
|
{
|
||||||
|
throw new ValidationException($"Order with id {orderDataModel.Id} already exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
_orderStorageContract.AddElement(orderDataModel);
|
||||||
|
_logger.LogInformation("Order successfully created with id: {id}", orderDataModel.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteOrder(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("DeleteOrder called for id: {id}", id);
|
||||||
|
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("OrderId must be a valid GUID");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверка что заказ существует
|
||||||
|
var existingOrder = _orderStorageContract.GetElementById(id);
|
||||||
|
if (existingOrder == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException($"Order with id {id} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
_orderStorageContract.DelElement(id);
|
||||||
|
_logger.LogInformation("Order successfully deleted with id: {id}", id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Enums;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace AndDietCokeBusinessLogic.Implementations;
|
||||||
|
|
||||||
|
internal class PositionBusinessLogicContract(IPositionStorageContract positionStorageContract, ILogger logger)
|
||||||
|
: IPositionBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly IPositionStorageContract _positionStorageContract = positionStorageContract;
|
||||||
|
|
||||||
|
public List<PositionDataModel> GetAllPositions(bool onlyActive = true)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllPositions params: {onlyActive}", onlyActive);
|
||||||
|
throw new NotImplementedException("GetList method is not properly implemented in storage contract");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PositionDataModel> GetAllDataOfPosition(string positionId)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllDataOfPosition for {positionId}", positionId);
|
||||||
|
if (positionId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(positionId));
|
||||||
|
}
|
||||||
|
if (!positionId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field positionId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var position = _positionStorageContract.GetElementById(positionId);
|
||||||
|
if (position == null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException(positionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new List<PositionDataModel> { position };
|
||||||
|
}
|
||||||
|
|
||||||
|
public PositionDataModel GetPositionByData(string data)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Get element by data: {data}", data);
|
||||||
|
if (data.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
PositionDataModel position;
|
||||||
|
if (data.IsGuid())
|
||||||
|
{
|
||||||
|
position = _positionStorageContract.GetElementById(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
position = _positionStorageContract.GetElementByName(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return position ?? throw new ElementNotFoundException(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void InsertPosition(PositionDataModel positionDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(positionDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(positionDataModel);
|
||||||
|
positionDataModel.Validate();
|
||||||
|
_positionStorageContract.AddElement(positionDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdatePosition(PositionDataModel positionDataModel)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(positionDataModel));
|
||||||
|
ArgumentNullException.ThrowIfNull(positionDataModel);
|
||||||
|
positionDataModel.Validate();
|
||||||
|
_positionStorageContract.UpdElement(positionDataModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeletePosition(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Delete by id: {id}", id);
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_positionStorageContract.DelElement(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RestorePosition(string id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Restore by id: {id}", id);
|
||||||
|
if (id.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
}
|
||||||
|
if (!id.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("Id is not a unique identifier");
|
||||||
|
}
|
||||||
|
_positionStorageContract.ResElement(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StorageContract;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract, ILogger logger) : ISalaryBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = logger;
|
||||||
|
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
|
||||||
|
|
||||||
|
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllSalariesByPeriod params: {fromDate}, {toDate}", fromDate, toDate);
|
||||||
|
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SalaryDataModel> GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("GetAllSalariesByPeriodByEmployee params: {fromDate}, {toDate}, {employeeId}",
|
||||||
|
fromDate, toDate, employeeId);
|
||||||
|
|
||||||
|
if (fromDate.IsDateNotOlder(toDate))
|
||||||
|
{
|
||||||
|
throw new IncorrectDatesException(fromDate, toDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (employeeId.IsEmpty())
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(employeeId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!employeeId.IsGuid())
|
||||||
|
{
|
||||||
|
throw new ValidationException("The value in the field employeeId is not a unique identifier.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return _salaryStorageContract.GetList(fromDate, toDate, employeeId) ?? throw new NullListException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CalculateSalaryByMonth(DateTime date)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("CalculateSalaryByMonth: {date}", date);
|
||||||
|
|
||||||
|
var startDate = new DateTime(date.Year, date.Month, 1);
|
||||||
|
var endDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
|
||||||
|
|
||||||
|
// Get all salaries for the month to avoid duplicate calculations
|
||||||
|
var existingSalaries = _salaryStorageContract.GetList(startDate, endDate);
|
||||||
|
|
||||||
|
if (existingSalaries != null && existingSalaries.Any())
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Salaries for period {startDate} to {endDate} already exist", startDate, endDate);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
|
||||||
|
public interface IClientBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
List<СlientDataModel> GetAllClients();
|
||||||
|
|
||||||
|
СlientDataModel GetClientByData(string data);
|
||||||
|
|
||||||
|
void InsertClient(СlientDataModel clientDataModel);
|
||||||
|
|
||||||
|
void UpdateClient(СlientDataModel clientDataModel);
|
||||||
|
|
||||||
|
void DeleteClient(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
|
||||||
|
public interface IDishBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
List<DishDataModel> GetAllDishes();
|
||||||
|
|
||||||
|
List<DishHistoryDataModel> GetDishHistory(string dishId);
|
||||||
|
|
||||||
|
DishDataModel? GetDishById(string id);
|
||||||
|
|
||||||
|
DishDataModel? GetDishByName(string name);
|
||||||
|
|
||||||
|
void InsertDish(DishDataModel dishDataModel);
|
||||||
|
|
||||||
|
void UpdateDish(DishDataModel dishDataModel);
|
||||||
|
|
||||||
|
void DeleteDish(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
|
||||||
|
public interface IDish_OrderBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
List<Dish_OrderDataModel> GetAllDishOrders(string? orderId = null, string? dishId = null);
|
||||||
|
Dish_OrderDataModel? GetDishOrderById(string orderId, string dishId);
|
||||||
|
void InsertDishOrder(Dish_OrderDataModel dishOrderDataModel);
|
||||||
|
void UpdateDishOrder(Dish_OrderDataModel dishOrderDataModel);
|
||||||
|
void DeleteDishOrder(string orderId, string dishId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
|
||||||
|
public interface IEmployeeBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
List<EmployeeDataModel> GetAllEmployees(bool onlyActive = true, string? positionId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
|
||||||
|
|
||||||
|
EmployeeDataModel? GetEmployeeById(string id);
|
||||||
|
|
||||||
|
EmployeeDataModel? GetEmployeeByFIO(string fio);
|
||||||
|
|
||||||
|
void InsertEmployee(EmployeeDataModel employeeDataModel);
|
||||||
|
|
||||||
|
void UpdateEmployee(EmployeeDataModel employeeDataModel);
|
||||||
|
|
||||||
|
void DeleteEmployee(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
|
||||||
|
public interface IOrderBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
List<OrderDataModels> GetAllOrders(string? clientId = null, string? employeedId = null, string? dishId = null);
|
||||||
|
|
||||||
|
OrderDataModels? GetOrderById(string id);
|
||||||
|
|
||||||
|
void InsertOrder(OrderDataModels orderDataModel);
|
||||||
|
|
||||||
|
void DeleteOrder(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
|
||||||
|
public interface IPositionBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
List<PositionDataModel> GetAllPositions(bool onlyActive);
|
||||||
|
List<PositionDataModel> GetAllDataOfPosition(string positionId);
|
||||||
|
PositionDataModel GetPositionByData(string data);
|
||||||
|
void InsertPosition(PositionDataModel positionDataModel);
|
||||||
|
void UpdatePosition(PositionDataModel positionDataModel);
|
||||||
|
void DeletePosition(string id);
|
||||||
|
void RestorePosition(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.BusinessLogicsContracts
|
||||||
|
{
|
||||||
|
public interface ISalaryBusinessLogicsContracts
|
||||||
|
{
|
||||||
|
List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate);
|
||||||
|
|
||||||
|
List<SalaryDataModel> GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId);
|
||||||
|
|
||||||
|
void CalculateSalaryByMonth(DateTime date);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,9 @@ namespace AndDietCokeContracts.DataModels;
|
|||||||
public class DishHistoryDataModel(string productId, double oldPrice, string orderid) : IValidation
|
public class DishHistoryDataModel(string productId, double oldPrice, string orderid) : IValidation
|
||||||
|
|
||||||
{
|
{
|
||||||
|
private DateTime dateTime;
|
||||||
|
private double v;
|
||||||
|
|
||||||
public string DishId { get; private set; } = productId;
|
public string DishId { get; private set; } = productId;
|
||||||
public double OldPrice { get; private set; } = oldPrice;
|
public double OldPrice { get; private set; } = oldPrice;
|
||||||
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
|
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
|
||||||
|
|||||||
@@ -1,13 +1,5 @@
|
|||||||
using AndDietCokeContracts.Enums;
|
using AndDietCokeContracts.Exceptions;
|
||||||
using AndDietCokeContracts.Exceptions;
|
|
||||||
using AndDietCokeContracts.Extensions;
|
using AndDietCokeContracts.Extensions;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Xml.Linq;
|
|
||||||
|
|
||||||
namespace AndDietCokeContracts.DataModels;
|
namespace AndDietCokeContracts.DataModels;
|
||||||
public class Dish_OrderDataModel(string orderid, string dishid, int count)
|
public class Dish_OrderDataModel(string orderid, string dishid, int count)
|
||||||
@@ -16,14 +8,8 @@ public string OrderId { get; private set; } = orderid;
|
|||||||
public string DishId { get; private set; } = dishid;
|
public string DishId { get; private set; } = dishid;
|
||||||
public int Count { get; private set; } = count;
|
public int Count { get; private set; } = count;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void Validate()
|
public void Validate()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
if (OrderId.IsEmpty())
|
if (OrderId.IsEmpty())
|
||||||
throw new ValidationException("Field OrderId is empty");
|
throw new ValidationException("Field OrderId is empty");
|
||||||
if (!OrderId.IsGuid())
|
if (!OrderId.IsGuid())
|
||||||
@@ -34,6 +20,5 @@ public void Validate()
|
|||||||
throw new ValidationException("The value in the field DishId is not a unique identifier");
|
throw new ValidationException("The value in the field DishId is not a unique identifier");
|
||||||
if (Count <= 0)
|
if (Count <= 0)
|
||||||
throw new ValidationException("Field Count is less than or equal to 0");
|
throw new ValidationException("Field Count is less than or equal to 0");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ using System.Text.RegularExpressions;
|
|||||||
using System.Xml;
|
using System.Xml;
|
||||||
namespace AndDietCokeContracts.DataModels;
|
namespace AndDietCokeContracts.DataModels;
|
||||||
|
|
||||||
public class PositionDataModell(string id, string positionId, string positionName, PositionType positionType, double salary, bool isActual, DateTime changeDate) : IValidation
|
public class PositionDataModel(string id, string positionId, string positionName, PositionType positionType, double salary, bool isActual, DateTime changeDate) : IValidation
|
||||||
{
|
{
|
||||||
public string Id { get; private set; } = id;
|
public string Id { get; private set; } = id;
|
||||||
public string PositionId { get; private set; } = positionId;
|
public string PositionId { get; private set; } = positionId;
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ public enum DishType
|
|||||||
Pepperoni = 2,
|
Pepperoni = 2,
|
||||||
BBQChicken = 3,
|
BBQChicken = 3,
|
||||||
Veggie = 4,
|
Veggie = 4,
|
||||||
Cola = 5
|
Cola = 5,
|
||||||
|
Soup = 6,
|
||||||
|
MainCourse = 7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,5 +12,8 @@ public enum PositionType
|
|||||||
Waiter = 1,
|
Waiter = 1,
|
||||||
Сhef = 2,
|
Сhef = 2,
|
||||||
Manager = 3,
|
Manager = 3,
|
||||||
Assistant = 4
|
Assistant = 4,
|
||||||
|
Staff = 5,
|
||||||
|
Developer = 6,
|
||||||
|
QA = 7
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
|
||||||
|
public class DuplicateEmployeeException : Exception
|
||||||
|
{
|
||||||
|
public DuplicateEmployeeException(string id)
|
||||||
|
: base($"Employee with ID {id} already exists") { }
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace AndDietCokeContracts.Exceptions;
|
||||||
|
public class ElementExistsException : Exception
|
||||||
|
{
|
||||||
|
public string ParamName { get; private set; }
|
||||||
|
|
||||||
|
public string ParamValue { get; private set; }
|
||||||
|
|
||||||
|
public ElementExistsException(string paramName, string paramValue) : base($"There is already an element with value{paramValue} of parameter {paramName}")
|
||||||
|
{
|
||||||
|
ParamName = paramName;
|
||||||
|
ParamValue = paramValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace AndDietCokeContracts.Exceptions;
|
||||||
|
public class ElementNotFoundException : Exception
|
||||||
|
{
|
||||||
|
public string Value { get; private set; }
|
||||||
|
|
||||||
|
public ElementNotFoundException(string value) : base($"Element not found at value = {value}")
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace AndDietCokeContracts.Exceptions;
|
||||||
|
public class IncorrectDatesException : Exception
|
||||||
|
{
|
||||||
|
public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date.. StartDate: {start:dd.MM.YYYY}. EndDate: {end:dd.MM.YYYY}") { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace AndDietCokeContracts.Exceptions;
|
||||||
|
|
||||||
|
public class NullListException : Exception
|
||||||
|
{
|
||||||
|
public NullListException() : base("The returned list is null") { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace AndDietCokeContracts.Exceptions;
|
||||||
|
public class StorageException : Exception
|
||||||
|
{
|
||||||
|
public StorageException(Exception ex) : base($"Error while working in storage: {ex.Message}", ex) { }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace AndDietCokeContracts.Extensions;
|
||||||
|
|
||||||
|
public static class DateTimeExtensions
|
||||||
|
{
|
||||||
|
public static bool IsDateNotOlder(this DateTime date, DateTime olderDate)
|
||||||
|
{
|
||||||
|
return date >= olderDate;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.StoragesContracts;
|
||||||
|
|
||||||
|
public interface IClientStorageContract
|
||||||
|
{
|
||||||
|
List<СlientDataModel> GetList();
|
||||||
|
|
||||||
|
СlientDataModel? GetElementById(string id);
|
||||||
|
|
||||||
|
СlientDataModel? GetElementByPhoneNumber(string phoneNumber);
|
||||||
|
|
||||||
|
СlientDataModel? GetElementByFIO(string fio);
|
||||||
|
|
||||||
|
void AddElement(СlientDataModel clientDataModel);
|
||||||
|
|
||||||
|
void UpdElement(СlientDataModel clientDataModel);
|
||||||
|
|
||||||
|
void DelElement(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.StoragesContracts;
|
||||||
|
|
||||||
|
public interface IDishStorageContracts
|
||||||
|
{
|
||||||
|
List<DishDataModel> GetList();
|
||||||
|
|
||||||
|
List<DishHistoryDataModel> GetHistoryByProductId(string dishId);
|
||||||
|
|
||||||
|
DishDataModel? GetElementById(string id);
|
||||||
|
|
||||||
|
DishDataModel? GetElementByName(string name);
|
||||||
|
|
||||||
|
void AddElement(DishDataModel dishDataModel);
|
||||||
|
|
||||||
|
void UpdElement(DishDataModel dishDataModel);
|
||||||
|
|
||||||
|
void DelElement(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.StoragesContracts;
|
||||||
|
|
||||||
|
public interface IDish_OrderStorageContract
|
||||||
|
{
|
||||||
|
List<Dish_OrderDataModel> GetList(string? orderId = null, string? dishId = null);
|
||||||
|
|
||||||
|
Dish_OrderDataModel? GetElementById(string orderId, string dishId);
|
||||||
|
|
||||||
|
void AddElement(Dish_OrderDataModel dishOrderDataModel);
|
||||||
|
|
||||||
|
void UpdElement(Dish_OrderDataModel dishOrderDataModel);
|
||||||
|
|
||||||
|
void DelElement(string orderId, string dishId);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
namespace AndDietCokeContracts.StoragesContracts;
|
||||||
|
public interface IEmployeeStoragesContracts
|
||||||
|
{
|
||||||
|
List<EmployeeDataModel> GetList(bool onlyActive = true, string? positionId = null, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
|
||||||
|
|
||||||
|
EmployeeDataModel? GetElementById(string id);
|
||||||
|
|
||||||
|
EmployeeDataModel? GetElementByFIO(string fio);
|
||||||
|
|
||||||
|
void AddElement(EmployeeDataModel employeeDataModel);
|
||||||
|
|
||||||
|
void UpdElement(EmployeeDataModel employeeDataModel);
|
||||||
|
|
||||||
|
void DelElement(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.StorageContracts;
|
||||||
|
|
||||||
|
public interface IOrderStorageContracts
|
||||||
|
{
|
||||||
|
List<OrderDataModels> GetList(string? clientId = null, string? employeedId = null, string? dishId = null);
|
||||||
|
|
||||||
|
OrderDataModels? GetElementById(string id);
|
||||||
|
|
||||||
|
void AddElement(OrderDataModels orderDataModel);
|
||||||
|
|
||||||
|
void DelElement(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.StoragesContracts;
|
||||||
|
public interface IPositionStorageContract
|
||||||
|
{
|
||||||
|
List<PositionDataModel> GetList(bool onlyActual = true);
|
||||||
|
List<PositionDataModel> GetPostWithHistory(string postId);
|
||||||
|
PositionDataModel? GetElementById(string id);
|
||||||
|
PositionDataModel? GetElementByName(string name);
|
||||||
|
void AddElement(PositionDataModel positionDataModell);
|
||||||
|
void UpdElement(PositionDataModel positionDataModell);
|
||||||
|
void DelElement(string id);
|
||||||
|
void ResElement(string id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
|
||||||
|
namespace AndDietCokeContracts.StorageContract
|
||||||
|
{
|
||||||
|
public interface ISalaryStorageContract
|
||||||
|
{
|
||||||
|
List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? employeeId = null);
|
||||||
|
|
||||||
|
void AddElement(SalaryDataModel salaryDataModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndDietCokeContracts", "And
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndDietCokeTests", "AndDietCokeTests\AndDietCokeTests.csproj", "{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndDietCokeTests", "AndDietCokeTests\AndDietCokeTests.csproj", "{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndDietCokeBusinessLogic", "AndDietCokeBusinessLogic\AndDietCokeBusinessLogic.csproj", "{950B374D-94CC-4EE2-BD25-E78D5CDA5F2F}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -21,8 +23,15 @@ Global
|
|||||||
{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}.Release|Any CPU.Build.0 = Release|Any CPU
|
{97CF49C2-6F17-4FCB-B2AD-D84AED6A1AF9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{950B374D-94CC-4EE2-BD25-E78D5CDA5F2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{950B374D-94CC-4EE2-BD25-E78D5CDA5F2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{950B374D-94CC-4EE2-BD25-E78D5CDA5F2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{950B374D-94CC-4EE2-BD25-E78D5CDA5F2F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {6545237B-96EA-4C80-963A-F4CB39480436}
|
||||||
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
@@ -11,13 +11,16 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||||
|
<PackageReference Include="Moq" Version="4.20.72" />
|
||||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AndDietCokeBusinessLogic\AndDietCokeBusinessLogic.csproj" />
|
||||||
<ProjectReference Include="..\AndDietCokeContracts\AndDietCokeContracts.csproj" />
|
<ProjectReference Include="..\AndDietCokeContracts\AndDietCokeContracts.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,228 @@
|
|||||||
|
using AndDietCokeBusinessLogic.Implementations;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Enums;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace AndDietCokeTests.BusinessLogicsContractsTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
internal class DishBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private DishBusinessLogicContract _dishBusinessLogic;
|
||||||
|
private Mock<IDishStorageContracts> _dishStorageMock;
|
||||||
|
private Mock<ILogger<DishBusinessLogicContract>> _loggerMock;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_dishStorageMock = new Mock<IDishStorageContracts>();
|
||||||
|
_loggerMock = new Mock<ILogger<DishBusinessLogicContract>>();
|
||||||
|
_dishBusinessLogic = new DishBusinessLogicContract(
|
||||||
|
_dishStorageMock.Object,
|
||||||
|
_loggerMock.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_dishStorageMock.Reset();
|
||||||
|
_loggerMock.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishes_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testDishes = new List<DishDataModel>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "Борщ", DishType.Soup, 250.50, Guid.NewGuid().ToString()),
|
||||||
|
new(Guid.NewGuid().ToString(), "Стейк", DishType.MainCourse, 1200.00, Guid.NewGuid().ToString())
|
||||||
|
};
|
||||||
|
|
||||||
|
_dishStorageMock.Setup(x => x.GetList()).Returns(testDishes);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishBusinessLogic.GetAllDishes();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result, Is.EquivalentTo(testDishes));
|
||||||
|
});
|
||||||
|
_dishStorageMock.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishes_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_dishStorageMock.Setup(x => x.GetList()).Returns(new List<DishDataModel>());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishBusinessLogic.GetAllDishes();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result, Has.Count.EqualTo(0));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishes_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_dishStorageMock.Setup(x => x.GetList()).Returns((List<DishDataModel>)null);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishBusinessLogic.GetAllDishes(),
|
||||||
|
Throws.TypeOf<NullListException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishHistory_InvalidId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishBusinessLogic.GetDishHistory("invalid-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishById_ValidId_ReturnsDish_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
var testDish = new DishDataModel(dishId, "Борщ", DishType.Soup, 250.50, Guid.NewGuid().ToString());
|
||||||
|
|
||||||
|
_dishStorageMock.Setup(x => x.GetElementById(dishId))
|
||||||
|
.Returns(testDish);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishBusinessLogic.GetDishById(dishId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(testDish));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishById_NotFound_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishBusinessLogic.GetDishById(dishId),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishByName_ValidName_ReturnsDish_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dishName = "Борщ";
|
||||||
|
var testDish = new DishDataModel(Guid.NewGuid().ToString(), dishName, DishType.Soup, 250.50, Guid.NewGuid().ToString());
|
||||||
|
|
||||||
|
_dishStorageMock.Setup(x => x.GetElementByName(dishName))
|
||||||
|
.Returns(testDish);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishBusinessLogic.GetDishByName(dishName);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(testDish));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertDish_ValidData_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testDish = new DishDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
"Борщ",
|
||||||
|
DishType.Soup,
|
||||||
|
250.50,
|
||||||
|
Guid.NewGuid().ToString());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_dishBusinessLogic.InsertDish(testDish);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_dishStorageMock.Verify(x => x.AddElement(testDish), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertDish_DuplicateName_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testDish = new DishDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
"Борщ",
|
||||||
|
DishType.Soup,
|
||||||
|
250.50,
|
||||||
|
Guid.NewGuid().ToString());
|
||||||
|
|
||||||
|
_dishStorageMock.Setup(x => x.GetElementByName(testDish.Name))
|
||||||
|
.Returns(testDish);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishBusinessLogic.InsertDish(testDish),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateDish_ValidData_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testDish = new DishDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
"Борщ",
|
||||||
|
DishType.Soup,
|
||||||
|
250.50,
|
||||||
|
Guid.NewGuid().ToString());
|
||||||
|
|
||||||
|
_dishStorageMock.Setup(x => x.GetElementById(testDish.Id))
|
||||||
|
.Returns(testDish);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_dishBusinessLogic.UpdateDish(testDish);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_dishStorageMock.Verify(x => x.UpdElement(testDish), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteDish_ValidId_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
var testDish = new DishDataModel(
|
||||||
|
dishId,
|
||||||
|
"Борщ",
|
||||||
|
DishType.Soup,
|
||||||
|
250.50,
|
||||||
|
Guid.NewGuid().ToString());
|
||||||
|
|
||||||
|
_dishStorageMock.Setup(x => x.GetElementById(dishId))
|
||||||
|
.Returns(testDish);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_dishBusinessLogic.DeleteDish(dishId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_dishStorageMock.Verify(x => x.DelElement(dishId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteDish_InvalidId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishBusinessLogic.DeleteDish("invalid-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,313 @@
|
|||||||
|
using AndDietCokeBusinessLogic.Implementations;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AndDietCokeTests.BusinessLogicsContractsTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
internal class Dish_OrderBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private Dish_OrderBusinessLogicContract _dishOrderBusinessLogic;
|
||||||
|
private Mock<IDish_OrderStorageContract> _dishOrderStorageMock;
|
||||||
|
private Mock<ILogger<Dish_OrderBusinessLogicContract>> _loggerMock;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_dishOrderStorageMock = new Mock<IDish_OrderStorageContract>();
|
||||||
|
_loggerMock = new Mock<ILogger<Dish_OrderBusinessLogicContract>>();
|
||||||
|
_dishOrderBusinessLogic = new Dish_OrderBusinessLogicContract(
|
||||||
|
_dishOrderStorageMock.Object,
|
||||||
|
_loggerMock.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_dishOrderStorageMock.Reset();
|
||||||
|
_loggerMock.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishOrders_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testData = new List<Dish_OrderDataModel>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 2),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns(testData);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishOrderBusinessLogic.GetAllDishOrders();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result, Is.EquivalentTo(testData));
|
||||||
|
});
|
||||||
|
_dishOrderStorageMock.Verify(x => x.GetList(null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishOrders_WithOrderIdFilter_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var orderId = Guid.NewGuid().ToString();
|
||||||
|
var testData = new List<Dish_OrderDataModel>
|
||||||
|
{
|
||||||
|
new(orderId, Guid.NewGuid().ToString(), 3)
|
||||||
|
};
|
||||||
|
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetList(orderId, It.IsAny<string>()))
|
||||||
|
.Returns(testData);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishOrderBusinessLogic.GetAllDishOrders(orderId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EquivalentTo(testData));
|
||||||
|
_dishOrderStorageMock.Verify(x => x.GetList(orderId, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishOrders_WithDishIdFilter_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
var testData = new List<Dish_OrderDataModel>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), dishId, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetList(It.IsAny<string>(), dishId))
|
||||||
|
.Returns(testData);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishOrderBusinessLogic.GetAllDishOrders(null, dishId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EquivalentTo(testData));
|
||||||
|
_dishOrderStorageMock.Verify(x => x.GetList(null, dishId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishOrders_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns((List<Dish_OrderDataModel>)null);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.GetAllDishOrders(),
|
||||||
|
Throws.TypeOf<NullListException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishOrders_InvalidOrderId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.GetAllDishOrders("invalid-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDishOrders_InvalidDishId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.GetAllDishOrders(null, "invalid-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishOrderById_ValidIds_ReturnsRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var orderId = Guid.NewGuid().ToString();
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
var testRecord = new Dish_OrderDataModel(orderId, dishId, 2);
|
||||||
|
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetElementById(orderId, dishId))
|
||||||
|
.Returns(testRecord);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _dishOrderBusinessLogic.GetDishOrderById(orderId, dishId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(testRecord));
|
||||||
|
_dishOrderStorageMock.Verify(x => x.GetElementById(orderId, dishId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishOrderById_InvalidOrderId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.GetDishOrderById("invalid", Guid.NewGuid().ToString()),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishOrderById_InvalidDishId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.GetDishOrderById(Guid.NewGuid().ToString(), "invalid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetDishOrderById_NotFound_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var orderId = Guid.NewGuid().ToString();
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.GetDishOrderById(orderId, dishId),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertDishOrder_ValidData_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testRecord = new Dish_OrderDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
2);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_dishOrderBusinessLogic.InsertDishOrder(testRecord);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_dishOrderStorageMock.Verify(x => x.AddElement(testRecord), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertDishOrder_NullData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.InsertDishOrder(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertDishOrder_InvalidCount_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var invalidRecord = new Dish_OrderDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
0); // Invalid count
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.InsertDishOrder(invalidRecord),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertDishOrder_Duplicate_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testRecord = new Dish_OrderDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
1);
|
||||||
|
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetElementById(testRecord.OrderId, testRecord.DishId))
|
||||||
|
.Returns(testRecord);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.InsertDishOrder(testRecord),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateDishOrder_ValidData_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testRecord = new Dish_OrderDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
3);
|
||||||
|
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetElementById(testRecord.OrderId, testRecord.DishId))
|
||||||
|
.Returns(testRecord);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_dishOrderBusinessLogic.UpdateDishOrder(testRecord);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_dishOrderStorageMock.Verify(x => x.UpdElement(testRecord), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateDishOrder_NotExists_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testRecord = new Dish_OrderDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
2);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.UpdateDishOrder(testRecord),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteDishOrder_ValidIds_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var orderId = Guid.NewGuid().ToString();
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
var testRecord = new Dish_OrderDataModel(orderId, dishId, 1);
|
||||||
|
|
||||||
|
_dishOrderStorageMock.Setup(x => x.GetElementById(orderId, dishId))
|
||||||
|
.Returns(testRecord);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_dishOrderBusinessLogic.DeleteDishOrder(orderId, dishId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_dishOrderStorageMock.Verify(x => x.DelElement(orderId, dishId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteDishOrder_NotExists_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var orderId = Guid.NewGuid().ToString();
|
||||||
|
var dishId = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.DeleteDishOrder(orderId, dishId),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteDishOrder_InvalidOrderId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.DeleteDishOrder("invalid", Guid.NewGuid().ToString()),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteDishOrder_InvalidDishId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _dishOrderBusinessLogic.DeleteDishOrder(Guid.NewGuid().ToString(), "invalid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,268 @@
|
|||||||
|
using AndDietCokeBusinessLogic.Implementations;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Enums;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AndDietCokeTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class EmployeeBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private EmployeeBusinessLogicContract _employeeBusinessLogicContract;
|
||||||
|
private Mock<IEmployeeStoragesContracts> _employeeStorageContract;
|
||||||
|
private Mock<ILogger<EmployeeBusinessLogicContract>> _logger;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_employeeStorageContract = new Mock<IEmployeeStoragesContracts>();
|
||||||
|
_logger = new Mock<ILogger<EmployeeBusinessLogicContract>>();
|
||||||
|
_employeeBusinessLogicContract = new EmployeeBusinessLogicContract(
|
||||||
|
_employeeStorageContract.Object,
|
||||||
|
_logger.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_employeeStorageContract.Reset();
|
||||||
|
_logger.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllEmployees_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testEmployees = new List<EmployeeDataModel>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "Иванов И.И.", Guid.NewGuid().ToString(),
|
||||||
|
PositionType.Manager, DateTime.Now.AddYears(-30), DateTime.Now.AddYears(-5), false),
|
||||||
|
new(Guid.NewGuid().ToString(), "Петров П.П.", Guid.NewGuid().ToString(),
|
||||||
|
PositionType.Developer, DateTime.Now.AddYears(-25), DateTime.Now.AddYears(-3), true)
|
||||||
|
};
|
||||||
|
|
||||||
|
_employeeStorageContract.Setup(x => x.GetList(
|
||||||
|
It.IsAny<bool>(), It.IsAny<string>(),
|
||||||
|
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||||
|
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns(testEmployees);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var resultActive = _employeeBusinessLogicContract.GetAllEmployees(true);
|
||||||
|
var resultAll = _employeeBusinessLogicContract.GetAllEmployees(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(resultActive, Is.Not.Null);
|
||||||
|
Assert.That(resultAll, Is.Not.Null);
|
||||||
|
Assert.That(resultActive, Is.EquivalentTo(testEmployees));
|
||||||
|
Assert.That(resultAll, Is.EquivalentTo(testEmployees));
|
||||||
|
});
|
||||||
|
|
||||||
|
_employeeStorageContract.Verify(x => x.GetList(true, null, null, null, null, null), Times.Once);
|
||||||
|
_employeeStorageContract.Verify(x => x.GetList(false, null, null, null, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllEmployees_WithPositionFilter_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var positionId = Guid.NewGuid().ToString();
|
||||||
|
var testEmployees = new List<EmployeeDataModel>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "Сидоров С.С.", positionId,
|
||||||
|
PositionType.Developer, DateTime.Now.AddYears(-28), DateTime.Now.AddYears(-4), false)
|
||||||
|
};
|
||||||
|
|
||||||
|
_employeeStorageContract.Setup(x => x.GetList(
|
||||||
|
It.IsAny<bool>(), positionId,
|
||||||
|
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||||
|
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns(testEmployees);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _employeeBusinessLogicContract.GetAllEmployees(true, positionId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EquivalentTo(testEmployees));
|
||||||
|
_employeeStorageContract.Verify(x => x.GetList(true, positionId, null, null, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllEmployees_WithDateFilters_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var fromDate = DateTime.Now.AddYears(-5);
|
||||||
|
var toDate = DateTime.Now;
|
||||||
|
var testEmployees = new List<EmployeeDataModel>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "Кузнецов К.К.", Guid.NewGuid().ToString(),
|
||||||
|
PositionType.QA, DateTime.Now.AddYears(-35), DateTime.Now.AddYears(-2), false)
|
||||||
|
};
|
||||||
|
|
||||||
|
_employeeStorageContract.Setup(x => x.GetList(
|
||||||
|
It.IsAny<bool>(), It.IsAny<string>(),
|
||||||
|
fromDate, toDate,
|
||||||
|
fromDate, toDate))
|
||||||
|
.Returns(testEmployees);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _employeeBusinessLogicContract.GetAllEmployees(
|
||||||
|
true, null, fromDate, toDate, fromDate, toDate);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EquivalentTo(testEmployees));
|
||||||
|
_employeeStorageContract.Verify(x => x.GetList(
|
||||||
|
true, null, fromDate, toDate, fromDate, toDate), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllEmployees_ReturnNull_ThrowsException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_employeeStorageContract.Setup(x => x.GetList(
|
||||||
|
It.IsAny<bool>(), It.IsAny<string>(),
|
||||||
|
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||||
|
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||||
|
.Returns((List<EmployeeDataModel>)null);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployees(),
|
||||||
|
Throws.TypeOf<NullListException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetEmployeeById_ValidId_ReturnsEmployee_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var employeeId = Guid.NewGuid().ToString();
|
||||||
|
var testEmployee = new EmployeeDataModel(
|
||||||
|
employeeId, "Иванов И.И.", Guid.NewGuid().ToString(),
|
||||||
|
PositionType.Manager, DateTime.Now.AddYears(-30), DateTime.Now.AddYears(-5), false);
|
||||||
|
|
||||||
|
_employeeStorageContract.Setup(x => x.GetElementById(employeeId))
|
||||||
|
.Returns(testEmployee);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _employeeBusinessLogicContract.GetEmployeeById(employeeId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(testEmployee));
|
||||||
|
_employeeStorageContract.Verify(x => x.GetElementById(employeeId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetEmployeeById_InvalidId_ThrowsException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var invalidId = "not-a-guid";
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeById(invalidId),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetEmployeeByFIO_ValidFIO_ReturnsEmployee_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var fio = "Иванов И.И.";
|
||||||
|
var testEmployee = new EmployeeDataModel(
|
||||||
|
Guid.NewGuid().ToString(), fio, Guid.NewGuid().ToString(),
|
||||||
|
PositionType.Manager, DateTime.Now.AddYears(-30), DateTime.Now.AddYears(-5), false);
|
||||||
|
|
||||||
|
_employeeStorageContract.Setup(x => x.GetElementByFIO(fio))
|
||||||
|
.Returns(testEmployee);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _employeeBusinessLogicContract.GetEmployeeByFIO(fio);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(testEmployee));
|
||||||
|
_employeeStorageContract.Verify(x => x.GetElementByFIO(fio), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertEmployee_ValidEmployee_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testEmployee = new EmployeeDataModel(
|
||||||
|
Guid.NewGuid().ToString(), "Иванов И.И.", Guid.NewGuid().ToString(),
|
||||||
|
PositionType.Manager, DateTime.Now.AddYears(-30), DateTime.Now.AddYears(-5), false);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_employeeBusinessLogicContract.InsertEmployee(testEmployee);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_employeeStorageContract.Verify(x => x.AddElement(testEmployee), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertEmployee_InvalidEmployee_ThrowsException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var invalidEmployee = new EmployeeDataModel(
|
||||||
|
"invalid-id", "Иванов", "invalid-post",
|
||||||
|
PositionType.None, DateTime.Now, DateTime.Now, false);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(invalidEmployee),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateEmployee_ValidEmployee_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testEmployee = new EmployeeDataModel(
|
||||||
|
Guid.NewGuid().ToString(), "Иванов И.И.", Guid.NewGuid().ToString(),
|
||||||
|
PositionType.Manager, DateTime.Now.AddYears(-30), DateTime.Now.AddYears(-5), false);
|
||||||
|
|
||||||
|
_employeeStorageContract.Setup(x => x.GetElementById(testEmployee.Id))
|
||||||
|
.Returns(testEmployee);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_employeeBusinessLogicContract.UpdateEmployee(testEmployee);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_employeeStorageContract.Verify(x => x.UpdElement(testEmployee), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteEmployee_ValidId_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var employeeId = Guid.NewGuid().ToString();
|
||||||
|
var testEmployee = new EmployeeDataModel(
|
||||||
|
employeeId, "Иванов И.И.", Guid.NewGuid().ToString(),
|
||||||
|
PositionType.Manager, DateTime.Now.AddYears(-30), DateTime.Now.AddYears(-5), false);
|
||||||
|
|
||||||
|
_employeeStorageContract.Setup(x => x.GetElementById(employeeId))
|
||||||
|
.Returns(testEmployee);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_employeeBusinessLogicContract.DeleteEmployee(employeeId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_employeeStorageContract.Verify(x => x.DelElement(employeeId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteEmployee_InvalidId_ThrowsException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var invalidId = "not-a-guid";
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(invalidId),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
using AndDietCokeBusinessLogic.Implementations;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.StorageContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AndDietCokeTests.BusinessLogicsContractsTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
internal class OrderBusinessLogicContractTests
|
||||||
|
{
|
||||||
|
private OrderBusinessLogicContract _orderBusinessLogic;
|
||||||
|
private Mock<IOrderStorageContracts> _orderStorageMock;
|
||||||
|
private Mock<ILogger<OrderBusinessLogicContract>> _loggerMock;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_orderStorageMock = new Mock<IOrderStorageContracts>();
|
||||||
|
_loggerMock = new Mock<ILogger<OrderBusinessLogicContract>>();
|
||||||
|
_orderBusinessLogic = new OrderBusinessLogicContract(
|
||||||
|
_orderStorageMock.Object,
|
||||||
|
_loggerMock.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_orderStorageMock.Reset();
|
||||||
|
_loggerMock.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllOrders_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testOrders = new List<OrderDataModels>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||||
|
new List<Dish_OrderDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) }),
|
||||||
|
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||||
|
new List<Dish_OrderDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 2) })
|
||||||
|
};
|
||||||
|
|
||||||
|
_orderStorageMock.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns(testOrders);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _orderBusinessLogic.GetAllOrders();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Multiple(() =>
|
||||||
|
{
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result, Is.EquivalentTo(testOrders));
|
||||||
|
});
|
||||||
|
_orderStorageMock.Verify(x => x.GetList(null, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllOrders_WithClientFilter_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var clientId = Guid.NewGuid().ToString();
|
||||||
|
var testOrders = new List<OrderDataModels>
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), clientId, Guid.NewGuid().ToString(),
|
||||||
|
new List<Dish_OrderDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) })
|
||||||
|
};
|
||||||
|
|
||||||
|
_orderStorageMock.Setup(x => x.GetList(clientId, It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns(testOrders);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _orderBusinessLogic.GetAllOrders(clientId: clientId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EquivalentTo(testOrders));
|
||||||
|
_orderStorageMock.Verify(x => x.GetList(clientId, null, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllOrders_InvalidClientId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _orderBusinessLogic.GetAllOrders(clientId: "invalid-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetOrderById_ValidId_ReturnsOrder_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var orderId = Guid.NewGuid().ToString();
|
||||||
|
var testOrder = new OrderDataModels(
|
||||||
|
orderId,
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
new List<Dish_OrderDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) });
|
||||||
|
|
||||||
|
_orderStorageMock.Setup(x => x.GetElementById(orderId))
|
||||||
|
.Returns(testOrder);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _orderBusinessLogic.GetOrderById(orderId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.EqualTo(testOrder));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetOrderById_InvalidId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _orderBusinessLogic.GetOrderById("invalid-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertOrder_ValidData_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testOrder = new OrderDataModels(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
new List<Dish_OrderDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_orderBusinessLogic.InsertOrder(testOrder);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_orderStorageMock.Verify(x => x.AddElement(testOrder), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertOrder_DuplicateId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testOrder = new OrderDataModels(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
new List<Dish_OrderDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) });
|
||||||
|
|
||||||
|
_orderStorageMock.Setup(x => x.GetElementById(testOrder.Id))
|
||||||
|
.Returns(testOrder);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _orderBusinessLogic.InsertOrder(testOrder),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteOrder_ValidId_Success_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var orderId = Guid.NewGuid().ToString();
|
||||||
|
var testOrder = new OrderDataModels(
|
||||||
|
orderId,
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
new List<Dish_OrderDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) });
|
||||||
|
|
||||||
|
_orderStorageMock.Setup(x => x.GetElementById(orderId))
|
||||||
|
.Returns(testOrder);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_orderBusinessLogic.DeleteOrder(orderId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_orderStorageMock.Verify(x => x.DelElement(orderId), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,322 @@
|
|||||||
|
using AndDietCokeBusinessLogic.Implementations;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Enums;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace AndDietCokeTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class PositionBusinessLogicsContractsTests
|
||||||
|
{
|
||||||
|
private PositionBusinessLogicContract _positionBusinessLogicContract;
|
||||||
|
private Mock<IPositionStorageContract> _positionStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_positionStorageContract = new Mock<IPositionStorageContract>();
|
||||||
|
_positionBusinessLogicContract = new PositionBusinessLogicContract(
|
||||||
|
_positionStorageContract.Object,
|
||||||
|
new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_positionStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
//[Test]
|
||||||
|
//public void GetAllPositions_ReturnListOfRecords_Test()
|
||||||
|
//{
|
||||||
|
// // Arrange
|
||||||
|
// var listOriginal = new List<PositionDataModel>()
|
||||||
|
// {
|
||||||
|
// new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Position 1", PositionType.Manager, 1000, true, DateTime.UtcNow),
|
||||||
|
// new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Position 2", PositionType.Staff, 800, false, DateTime.UtcNow)
|
||||||
|
// };
|
||||||
|
|
||||||
|
// _positionStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||||
|
|
||||||
|
// // Act
|
||||||
|
// var resultActive = _positionBusinessLogicContract.GetAllPositions(true);
|
||||||
|
// var resultAll = _positionBusinessLogicContract.GetAllPositions(false);
|
||||||
|
|
||||||
|
// // Assert
|
||||||
|
// Assert.Multiple(() =>
|
||||||
|
// {
|
||||||
|
// Assert.That(resultActive, Is.Not.Null);
|
||||||
|
// Assert.That(resultAll, Is.Not.Null);
|
||||||
|
// Assert.That(resultActive, Is.EquivalentTo(listOriginal));
|
||||||
|
// Assert.That(resultAll, Is.EquivalentTo(listOriginal));
|
||||||
|
// });
|
||||||
|
// _positionStorageContract.Verify(x => x.GetList(true), Times.Once);
|
||||||
|
// _positionStorageContract.Verify(x => x.GetList(false), Times.Once);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//[Test]
|
||||||
|
//public void GetAllPositions_ReturnEmptyList_Test()
|
||||||
|
//{
|
||||||
|
// // Arrange
|
||||||
|
// _positionStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
|
||||||
|
|
||||||
|
// // Act
|
||||||
|
// var result = _positionBusinessLogicContract.GetAllPositions(true);
|
||||||
|
// var listAll = _positionBusinessLogicContract.GetAllPositions(false);
|
||||||
|
|
||||||
|
// // Assert
|
||||||
|
// Assert.Multiple(() =>
|
||||||
|
// {
|
||||||
|
// Assert.That(result, Is.Not.Null);
|
||||||
|
// Assert.That(listAll, Is.Not.Null);
|
||||||
|
// Assert.That(result, Has.Count.EqualTo(0));
|
||||||
|
// Assert.That(listAll, Has.Count.EqualTo(0));
|
||||||
|
// });
|
||||||
|
// _positionStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
|
||||||
|
//}
|
||||||
|
|
||||||
|
//[Test]
|
||||||
|
//public void GetAllPositions_ReturnNull_ThrowException_Test()
|
||||||
|
//{
|
||||||
|
// // Arrange
|
||||||
|
// _positionStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(null as List<PositionDataModel>);
|
||||||
|
|
||||||
|
// // Act & Assert
|
||||||
|
// Assert.That(() => _positionBusinessLogicContract.GetAllPositions(true),
|
||||||
|
// Throws.TypeOf<NullListException>());
|
||||||
|
// _positionStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||||
|
//}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPosition_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var positionId = Guid.NewGuid().ToString();
|
||||||
|
var record = new PositionDataModel(Guid.NewGuid().ToString(), positionId, "Position", PositionType.Manager, 1000, true, DateTime.UtcNow);
|
||||||
|
|
||||||
|
_positionStorageContract.Setup(x => x.GetElementById(positionId)).Returns(record);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _positionBusinessLogicContract.GetAllDataOfPosition(positionId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result, Has.Count.EqualTo(1));
|
||||||
|
Assert.That(result[0], Is.EqualTo(record));
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementById(positionId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPosition_PositionIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.GetAllDataOfPosition(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.GetAllDataOfPosition(string.Empty),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllDataOfPosition_PositionIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.GetAllDataOfPosition("not-a-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPositionByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new PositionDataModel(id, Guid.NewGuid().ToString(), "Position", PositionType.Manager, 1000, true, DateTime.UtcNow);
|
||||||
|
|
||||||
|
_positionStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _positionBusinessLogicContract.GetPositionByData(id);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result.Id, Is.EqualTo(id));
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementById(id), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPositionByData_GetByName_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var name = "Position Name";
|
||||||
|
var record = new PositionDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), name, PositionType.Manager, 1000, true, DateTime.UtcNow);
|
||||||
|
|
||||||
|
_positionStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _positionBusinessLogicContract.GetPositionByData(name);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result.PositionName, Is.EqualTo(name));
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementByName(name), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPositionByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.GetPositionByData(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.GetPositionByData(string.Empty),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPositionByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.GetPositionByData(Guid.NewGuid().ToString()),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetPositionByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.GetPositionByData("Position Name"),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_positionStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPosition_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new PositionDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
"Position",
|
||||||
|
PositionType.Manager,
|
||||||
|
1000,
|
||||||
|
true,
|
||||||
|
DateTime.UtcNow);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_positionBusinessLogicContract.InsertPosition(record);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_positionStorageContract.Verify(x => x.AddElement(record), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPosition_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.InsertPosition(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_positionStorageContract.Verify(x => x.AddElement(It.IsAny<PositionDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertPosition_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var invalidRecord = new PositionDataModel(
|
||||||
|
"invalid-id",
|
||||||
|
"invalid-id",
|
||||||
|
"",
|
||||||
|
PositionType.None,
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
DateTime.UtcNow);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.InsertPosition(invalidRecord),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_positionStorageContract.Verify(x => x.AddElement(It.IsAny<PositionDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePosition_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new PositionDataModel(
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
Guid.NewGuid().ToString(),
|
||||||
|
"Position",
|
||||||
|
PositionType.Manager,
|
||||||
|
1000,
|
||||||
|
true,
|
||||||
|
DateTime.UtcNow);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_positionBusinessLogicContract.UpdatePosition(record);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_positionStorageContract.Verify(x => x.UpdElement(record), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdatePosition_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.UpdatePosition(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_positionStorageContract.Verify(x => x.UpdElement(It.IsAny<PositionDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePosition_CorrectId_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_positionBusinessLogicContract.DeletePosition(id);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_positionStorageContract.Verify(x => x.DelElement(id), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePosition_IdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.DeletePosition(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.DeletePosition(string.Empty),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_positionStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeletePosition_IdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _positionBusinessLogicContract.DeletePosition("not-a-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_positionStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RestorePosition_CorrectId_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_positionBusinessLogicContract.RestorePosition(id);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_positionStorageContract.Verify(x => x.ResElement(id), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
using AndDietCokeContracts.BusinessLogicsContracts;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.Extensions;
|
||||||
|
using AndDietCokeContracts.StorageContract;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace AndDietCokeTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class SalaryBusinessLogicsContractsTests
|
||||||
|
{
|
||||||
|
private SalaryBusinessLogicContract _salaryBusinessLogicContract;
|
||||||
|
private Mock<ISalaryStorageContract> _salaryStorageContract;
|
||||||
|
private Mock<ILogger> _logger;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_salaryStorageContract = new Mock<ISalaryStorageContract>();
|
||||||
|
_logger = new Mock<ILogger>();
|
||||||
|
_salaryBusinessLogicContract = new SalaryBusinessLogicContract(
|
||||||
|
_salaryStorageContract.Object,
|
||||||
|
_logger.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_salaryStorageContract.Reset();
|
||||||
|
_logger.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByPeriod_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var startDate = DateTime.UtcNow;
|
||||||
|
var endDate = DateTime.UtcNow.AddDays(1);
|
||||||
|
var listOriginal = new List<SalaryDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 1000, Guid.NewGuid().ToString()),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 1500, Guid.NewGuid().ToString()),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 800, Guid.NewGuid().ToString()),
|
||||||
|
};
|
||||||
|
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), null))
|
||||||
|
.Returns(listOriginal);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _salaryBusinessLogicContract.GetAllSalariesByPeriod(startDate, endDate);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result, Is.EquivalentTo(listOriginal));
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, null), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByPeriod_IncorrectDates_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var dateTime = DateTime.UtcNow;
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime),
|
||||||
|
Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime.AddSeconds(-1)),
|
||||||
|
Throws.TypeOf<IncorrectDatesException>());
|
||||||
|
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByPeriod_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), null))
|
||||||
|
.Returns((List<SalaryDataModel>)null);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
|
||||||
|
Throws.TypeOf<NullListException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByPeriod_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), null))
|
||||||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
|
||||||
|
Throws.TypeOf<StorageException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByPeriodByEmployee_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var startDate = DateTime.UtcNow;
|
||||||
|
var endDate = DateTime.UtcNow.AddDays(1);
|
||||||
|
var employeeId = Guid.NewGuid().ToString();
|
||||||
|
var listOriginal = new List<SalaryDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 1000, employeeId),
|
||||||
|
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 1500, employeeId),
|
||||||
|
};
|
||||||
|
|
||||||
|
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), employeeId))
|
||||||
|
.Returns(listOriginal);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(startDate, endDate, employeeId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(result, Is.Not.Null);
|
||||||
|
Assert.That(result, Is.EquivalentTo(listOriginal));
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, employeeId), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByPeriodByEmployee_EmployeeIdIsNullOrEmpty_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), string.Empty),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
|
||||||
|
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllSalariesByPeriodByEmployee_EmployeeIdIsNotGuid_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "invalid-id"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,289 @@
|
|||||||
|
using AndDietCokeBusinessLogic.Implementations;
|
||||||
|
using AndDietCokeContracts.DataModels;
|
||||||
|
using AndDietCokeContracts.Exceptions;
|
||||||
|
using AndDietCokeContracts.StoragesContracts;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace AndDietCokeTests.BusinessLogicsContractsTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
internal class СlientBusinessLogicsContractsTests
|
||||||
|
{
|
||||||
|
private ClientBusinessLogicContract _clientLogicContract;
|
||||||
|
private Mock<IClientStorageContract> _clientStorageContract;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
_clientStorageContract = new Mock<IClientStorageContract>();
|
||||||
|
_clientLogicContract = new ClientBusinessLogicContract(
|
||||||
|
_clientStorageContract.Object,
|
||||||
|
new Mock<ILogger>().Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_clientStorageContract.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllClients tests
|
||||||
|
[Test]
|
||||||
|
public void GetAllClients_ReturnListOfRecords_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var listOriginal = new List<СlientDataModel>()
|
||||||
|
{
|
||||||
|
new(Guid.NewGuid().ToString(), "Client 1", "+7-111-111-11-11", 0),
|
||||||
|
new(Guid.NewGuid().ToString(), "Client 2", "+7-222-222-22-22", 5),
|
||||||
|
};
|
||||||
|
|
||||||
|
_clientStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var list = _clientLogicContract.GetAllClients();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||||
|
_clientStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllClients_ReturnEmptyList_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_clientStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var list = _clientLogicContract.GetAllClients();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(list, Is.Not.Null);
|
||||||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||||||
|
_clientStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllClients_ReturnNull_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_clientStorageContract.Setup(x => x.GetList()).Returns(null as List<СlientDataModel>);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.GetAllClients(),
|
||||||
|
Throws.TypeOf<NullListException>());
|
||||||
|
_clientStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetAllClients_StorageThrowError_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_clientStorageContract.Setup(x => x.GetList())
|
||||||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.GetAllClients(),
|
||||||
|
Throws.TypeOf<StorageException>());
|
||||||
|
_clientStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetClientByData tests
|
||||||
|
[Test]
|
||||||
|
public void GetClientByData_GetById_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var record = new СlientDataModel(id, "Client", "+7-111-111-11-11", 0);
|
||||||
|
_clientStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var element = _clientLogicContract.GetClientByData(id);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementById(id), Times.Once);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetClientByData_GetByFio_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var fio = "Client Name";
|
||||||
|
var record = new СlientDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11", 0);
|
||||||
|
_clientStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var element = _clientLogicContract.GetClientByData(fio);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.FIO, Is.EqualTo(fio));
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByFIO(fio), Times.Once);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetClientByData_GetByPhoneNumber_ReturnRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var phone = "+7-111-111-11-11";
|
||||||
|
var record = new СlientDataModel(Guid.NewGuid().ToString(), "Client", phone, 0);
|
||||||
|
_clientStorageContract.Setup(x => x.GetElementByPhoneNumber(phone)).Returns(record);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var element = _clientLogicContract.GetClientByData(phone);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.That(element, Is.Not.Null);
|
||||||
|
Assert.That(element.Contact, Is.EqualTo(phone));
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(phone), Times.Once);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetClientByData_EmptyData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.GetClientByData(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
Assert.That(() => _clientLogicContract.GetClientByData(string.Empty),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||||
|
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetClientByData_NotFound_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var testData = Guid.NewGuid().ToString();
|
||||||
|
_clientStorageContract.Setup(x => x.GetElementById(testData)).Returns(null as СlientDataModel);
|
||||||
|
_clientStorageContract.Setup(x => x.GetElementByFIO(testData)).Returns(null as СlientDataModel);
|
||||||
|
_clientStorageContract.Setup(x => x.GetElementByPhoneNumber(testData)).Returns(null as СlientDataModel);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.GetClientByData(testData),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertClient tests
|
||||||
|
[Test]
|
||||||
|
public void InsertClient_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new СlientDataModel(Guid.NewGuid().ToString(), "Client", "+7-111-111-11-11", 0);
|
||||||
|
_clientStorageContract.Setup(x => x.AddElement(It.IsAny<СlientDataModel>()));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_clientLogicContract.InsertClient(record);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_clientStorageContract.Verify(x => x.AddElement(record), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertClient_NullRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.InsertClient(null),
|
||||||
|
Throws.TypeOf<ArgumentNullException>());
|
||||||
|
_clientStorageContract.Verify(x => x.AddElement(It.IsAny<СlientDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertClient_InvalidRecord_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.InsertClient(
|
||||||
|
new СlientDataModel("invalid-id", "Client", "invalid-phone", 0)),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_clientStorageContract.Verify(x => x.AddElement(It.IsAny<СlientDataModel>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void InsertClient_DuplicateData_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new СlientDataModel(Guid.NewGuid().ToString(), "Client", "+7-111-111-11-11", 0);
|
||||||
|
_clientStorageContract.Setup(x => x.AddElement(record))
|
||||||
|
.Throws(new ElementExistsException("Client", "already exists"));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.InsertClient(record),
|
||||||
|
Throws.TypeOf<ElementExistsException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateClient tests
|
||||||
|
[Test]
|
||||||
|
public void UpdateClient_CorrectRecord_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new СlientDataModel(Guid.NewGuid().ToString(), "Client", "+7-111-111-11-11", 0);
|
||||||
|
_clientStorageContract.Setup(x => x.UpdElement(It.IsAny<СlientDataModel>()));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_clientLogicContract.UpdateClient(record);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_clientStorageContract.Verify(x => x.UpdElement(record), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UpdateClient_NotFound_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var record = new СlientDataModel(Guid.NewGuid().ToString(), "Client", "+7-111-111-11-11", 0);
|
||||||
|
_clientStorageContract.Setup(x => x.UpdElement(record))
|
||||||
|
.Throws(new ElementNotFoundException("Client not found"));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.UpdateClient(record),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteClient tests
|
||||||
|
[Test]
|
||||||
|
public void DeleteClient_CorrectId_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_clientStorageContract.Setup(x => x.DelElement(id));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
_clientLogicContract.DeleteClient(id);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_clientStorageContract.Verify(x => x.DelElement(id), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteClient_NotFound_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
_clientStorageContract.Setup(x => x.DelElement(id))
|
||||||
|
.Throws(new ElementNotFoundException("Client not found"));
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.DeleteClient(id),
|
||||||
|
Throws.TypeOf<ElementNotFoundException>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeleteClient_InvalidId_ThrowException_Test()
|
||||||
|
{
|
||||||
|
// Act & Assert
|
||||||
|
Assert.That(() => _clientLogicContract.DeleteClient("not-a-guid"),
|
||||||
|
Throws.TypeOf<ValidationException>());
|
||||||
|
_clientStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -96,7 +96,7 @@ namespace AndDietCokeTests.DataModelsTests
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PositionDataModell CreateDataModel(string? id, string? positionId, string? positionName, PositionType positionType, double salary, bool isActual, DateTime changeDate) =>
|
private static PositionDataModel CreateDataModel(string? id, string? positionId, string? positionName, PositionType positionType, double salary, bool isActual, DateTime changeDate) =>
|
||||||
new(id, positionId, positionName, positionType, salary, isActual, changeDate);
|
new(id, positionId, positionName, positionType, salary, isActual, changeDate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user