1 Commits
lab_1 ... lab_2

Author SHA1 Message Date
1b4f72f67f lab 2 work 2025-03-18 20:05:36 +04:00
34 changed files with 2636 additions and 10 deletions

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DaisiesContracts\DaisiesContracts.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,115 @@
using DaisiesContracts.BusinessLogicsContracts;
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
using DaisiesContracts.Extensions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace DaisiesBusinessLogic.Implementations;
public class BouquetBusinessLogicContract(IBouquetStorageContract bouquetStorageContract, ILogger logger) : IBouquetBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IBouquetStorageContract _bouquetStorageContract = bouquetStorageContract;
public void CancelBouquet(string id)
{
_logger.LogInformation("Cancel by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_bouquetStorageContract.DelElement(id);
}
public List<BouquetDataModel> GetAllBouquetsByCustomerByPeriod(string customerId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales params: {customerId}, {fromDate}, {toDate}", customerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (customerId.IsEmpty())
{
throw new ArgumentNullException(nameof(customerId));
}
if (!customerId.IsGuid())
{
throw new ValidationException("The value in the field customerId is not a unique identifier.");
}
return _bouquetStorageContract.GetList(fromDate, toDate, customerId: customerId) ?? throw new NullListException();
}
public List<BouquetDataModel> GetAllBouquetsByEmployeerByPeriod(string employeerId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales params: {employeerId}, {fromDate}, {toDate}", employeerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (employeerId.IsEmpty())
{
throw new ArgumentNullException(nameof(employeerId));
}
if (!employeerId.IsGuid())
{
throw new ValidationException("The value in the field employeerId is not a unique identifier.");
}
return _bouquetStorageContract.GetList(fromDate, toDate, employeeId: employeerId) ?? throw new NullListException();
}
public List<BouquetDataModel> GetAllBouquetsByFlowerByPeriod(string flowerId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales params: {flowerId}, {fromDate}, {toDate}", flowerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (flowerId.IsEmpty())
{
throw new ArgumentNullException(nameof(flowerId));
}
if (!flowerId.IsGuid())
{
throw new ValidationException("The value in the field flowerId is not a unique identifier.");
}
return _bouquetStorageContract.GetList(fromDate, toDate, flowerId: flowerId) ?? throw new NullListException();
}
public List<BouquetDataModel> GetAllBouquetsByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _bouquetStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public BouquetDataModel GetBouquetByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
return _bouquetStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
public void InsertBouquet(BouquetDataModel bouquetDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(bouquetDataModel));
ArgumentNullException.ThrowIfNull(bouquetDataModel);
bouquetDataModel.Validate();
_bouquetStorageContract.AddElement(bouquetDataModel);
}
}

View File

@@ -0,0 +1,70 @@
using DaisiesContracts.BusinessLogicsContracts;
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
using DaisiesContracts.Extensions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace DaisiesBusinessLogic.Implementations;
public class CustomerBusinessLogicContract(ICustomerStorageContract customerStorageContract, ILogger logger) : ICustomerBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ICustomerStorageContract _customerStorageContract = customerStorageContract;
public void DeleteCustomer(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");
}
_customerStorageContract.DelElement(id);
}
public List<CustomerDataModel> GetAllCustomers()
{
_logger.LogInformation("GetAllCustomers");
return _customerStorageContract.GetList() ?? throw new NullListException();
}
public CustomerDataModel GetCustomerByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _customerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
if (Regex.IsMatch(data, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$"))
{
return _customerStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data);
}
return _customerStorageContract.GetElementByFullName(data) ?? throw new ElementNotFoundException(data);
}
public void InsertCustomer(CustomerDataModel customerDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(customerDataModel));
ArgumentNullException.ThrowIfNull(customerDataModel);
customerDataModel.Validate();
_customerStorageContract.AddElement(customerDataModel);
}
public void UpdateCustomer(CustomerDataModel customerDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(customerDataModel));
ArgumentNullException.ThrowIfNull(customerDataModel);
customerDataModel.Validate();
_customerStorageContract.UpdElement(customerDataModel);
}
}

View File

@@ -0,0 +1,61 @@
using DaisiesContracts.BusinessLogicsContracts;
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
using DaisiesContracts.Extensions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
namespace DaisiesBusinessLogic.Implementations;
public class CustomerDiscountBusinessLogicContract(ICustomerDiscountStorageContract customerDiscountStorageContract, IBouquetStorageContract bouquetStorageContract, ICustomerStorageContract customerStorageContract, ILogger logger) : ICustomerDiscountBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ICustomerDiscountStorageContract _customerDiscountStorageContract = customerDiscountStorageContract;
private readonly ICustomerStorageContract _customerStorageContract = customerStorageContract;
private readonly IBouquetStorageContract _bouquetStorageContract = bouquetStorageContract;
public void CalculateDiscountByMounth(DateTime date)
{
_logger.LogInformation("CalculateDiscountByMounth: {date}", date);
var startDate = new DateTime(date.Year, date.Month, 1);
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
var customers = _customerStorageContract.GetList() ?? throw new NullListException();
foreach (var customer in customers)
{
var sales = _bouquetStorageContract.GetList(startDate, finishDate, customerId: customer.Id)?.Sum(x => x.Items.Count) ??
throw new NullListException();
var salary = sales * 0.1 > 100 ? 100 : sales * 0.1;
_logger.LogDebug("The customer {customerId} was paid a salary of {salary}", customer.Id, salary);
_customerDiscountStorageContract.AddElement(new CustomerDiscountDataModel(customer.Id, DateTime.UtcNow, (decimal)salary));
}
}
public List<CustomerDiscountDataModel> GetAllDiscountsByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllDiscounts params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _customerDiscountStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public List<CustomerDiscountDataModel> GetAllDiscountsByPeriodByCustomer(DateTime fromDate, DateTime toDate, string customerId)
{
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (customerId.IsEmpty())
{
throw new ArgumentNullException(nameof(customerId));
}
if (!customerId.IsGuid())
{
throw new ValidationException("The value in the field customerId is not a unique identifier.");
}
_logger.LogInformation("GetAllDiscounts params: {fromDate}, {toDate}, {workerId}", fromDate, toDate, customerId);
return _customerDiscountStorageContract.GetList(fromDate, toDate, customerId) ?? throw new NullListException();
}
}

View File

@@ -0,0 +1,75 @@
using DaisiesContracts.BusinessLogicsContracts;
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
using DaisiesContracts.Extensions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace DaisiesBusinessLogic.Implementations;
public class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeStorageContract, ILogger logger) : IEmployeeBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract;
public void DeleteEmployeer(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");
}
_employeeStorageContract.DelElement(id);
}
public List<EmployeeDataModel> GetAllEmployeers(bool onlyActive = true)
{
_logger.LogInformation("GetAllEmployeers params: {onlyActive}", onlyActive);
return _employeeStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<EmployeeDataModel> GetAllEmployeersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
_logger.LogInformation("GetAllEmployeers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _employeeStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException();
}
public EmployeeDataModel GetEmployeerByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _employeeStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _employeeStorageContract.GetElementByFullName(data) ?? throw new ElementNotFoundException(data);
}
public void InsertEmployeer(EmployeeDataModel employeeDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(employeeDataModel));
ArgumentNullException.ThrowIfNull(employeeDataModel);
employeeDataModel.Validate();
_employeeStorageContract.AddElement(employeeDataModel);
}
public void UpdateEmployeer(EmployeeDataModel employeeDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(employeeDataModel));
ArgumentNullException.ThrowIfNull(employeeDataModel);
employeeDataModel.Validate();
_employeeStorageContract.UpdElement(employeeDataModel);
}
}

View File

@@ -0,0 +1,65 @@
using DaisiesContracts.BusinessLogicsContracts;
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
using DaisiesContracts.Extensions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace DaisiesBusinessLogic.Implementations;
public class FlowerBusinessLogicContract(IFlowerStorageContract flowerStorageContract, ILogger logger) : IFlowerBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IFlowerStorageContract _flowerStorageContract = flowerStorageContract;
public void DeleteFlower(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");
}
_flowerStorageContract.DelElement(id);
}
public List<FlowerDataModel> GetAllFlowers(bool onlyActive = true)
{
_logger.LogInformation("GetAllFlowers params: {onlyActive}", onlyActive);
return _flowerStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public FlowerDataModel GetFlowerByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _flowerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _flowerStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
}
public void InsertFlower(FlowerDataModel flowerDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(flowerDataModel));
ArgumentNullException.ThrowIfNull(flowerDataModel);
flowerDataModel.Validate();
_flowerStorageContract.AddElement(flowerDataModel);
}
public void UpdateFlower(FlowerDataModel flowerDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(flowerDataModel));
ArgumentNullException.ThrowIfNull(flowerDataModel);
flowerDataModel.Validate();
_flowerStorageContract.UpdElement(flowerDataModel);
}
}

View File

@@ -0,0 +1,20 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.BusinessLogicsContracts;
public interface IBouquetBusinessLogicContract
{
List<BouquetDataModel> GetAllBouquetsByPeriod(DateTime fromDate, DateTime toDate);
List<BouquetDataModel> GetAllBouquetsByEmployeerByPeriod(string employeerId, DateTime fromDate, DateTime toDate);
List<BouquetDataModel> GetAllBouquetsByCustomerByPeriod(string customerId, DateTime fromDate, DateTime toDate);
List<BouquetDataModel> GetAllBouquetsByFlowerByPeriod(string flowerId, DateTime fromDate, DateTime toDate);
BouquetDataModel GetBouquetByData(string data);
void InsertBouquet(BouquetDataModel bouquetDataModel);
void CancelBouquet(string id);
}

View File

@@ -0,0 +1,16 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.BusinessLogicsContracts;
public interface ICustomerBusinessLogicContract
{
List<CustomerDataModel> GetAllCustomers();
CustomerDataModel GetCustomerByData(string data);
void InsertCustomer(CustomerDataModel customerDataModel);
void UpdateCustomer(CustomerDataModel customerDataModel);
void DeleteCustomer(string id);
}

View File

@@ -0,0 +1,12 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.BusinessLogicsContracts;
public interface ICustomerDiscountBusinessLogicContract
{
List<CustomerDiscountDataModel> GetAllDiscountsByPeriod(DateTime fromDate, DateTime toDate);
List<CustomerDiscountDataModel> GetAllDiscountsByPeriodByCustomer(DateTime fromDate, DateTime toDate, string customerId);
void CalculateDiscountByMounth(DateTime date);
}

View File

@@ -0,0 +1,18 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.BusinessLogicsContracts;
public interface IEmployeeBusinessLogicContract
{
List<EmployeeDataModel> GetAllEmployeers(bool onlyActive = true);
List<EmployeeDataModel> GetAllEmployeersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true);
EmployeeDataModel GetEmployeerByData(string data);
void InsertEmployeer(EmployeeDataModel employeeDataModel);
void UpdateEmployeer(EmployeeDataModel employeeDataModel);
void DeleteEmployeer(string id);
}

View File

@@ -0,0 +1,16 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.BusinessLogicsContracts;
public interface IFlowerBusinessLogicContract
{
List<FlowerDataModel> GetAllFlowers(bool onlyActive = true);
FlowerDataModel GetFlowerByData(string data);
void InsertFlower(FlowerDataModel flowerDataModel);
void UpdateFlower(FlowerDataModel flowerDataModel);
void DeleteFlower(string id);
}

View File

@@ -4,13 +4,18 @@ using DaisiesContracts.Infrastructure;
namespace DaisiesContracts.DataModels;
public class BouquetItemDataModel(string flowerId, int quantity) : IValidation
public class BouquetItemDataModel(string bouquetId, string flowerId, int quantity) : IValidation
{
public string BouquetId { get; private set; } = bouquetId;
public string FlowerId { get; private set; } = flowerId;
public int Quantity { get; private set; } = quantity;
public void Validate()
{
if (BouquetId.IsEmpty())
throw new ValidationException("Bouquet Id пустой");
if (!BouquetId.IsGuid())
throw new ValidationException("Bouquet Id не является валидным GUID");
if (FlowerId.IsEmpty())
throw new ValidationException("Flower Id пустой");
if (!FlowerId.IsGuid())

View File

@@ -0,0 +1,25 @@
using DaisiesContracts.Exceptions;
using DaisiesContracts.Extensions;
using DaisiesContracts.Infrastructure;
namespace DaisiesContracts.DataModels;
public class CustomerDiscountDataModel(string customerId, DateTime discountDateTime, decimal discount) : IValidation
{
public string CustomerId { get; private set; } = customerId;
public DateTime DiscountDateTime { get; private set; } = discountDateTime;
/// <summary>
/// Скидка в процентах (от 0 до 100)
/// </summary>
public decimal Discount { get; private set; } = discount;
public void Validate()
{
if (CustomerId.IsEmpty())
throw new ValidationException("Customer Id пустой");
if (!CustomerId.IsGuid())
throw new ValidationException("Customer Id не является валидным GUID");
if (Discount < 0 || Discount > 100)
throw new ValidationException("Скидка должна быть в диапазоне от 0 до 100");
}
}

View File

@@ -0,0 +1,14 @@
namespace DaisiesContracts.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;
}
}

View File

@@ -0,0 +1,11 @@
namespace DaisiesContracts.Exceptions;
public class ElementNotFoundException : Exception
{
public string Value { get; private set; }
public ElementNotFoundException(string value) : base($"Element not found at value = {value}")
{
Value = value;
}
}

View File

@@ -0,0 +1,6 @@
namespace DaisiesContracts.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}") { }
}

View File

@@ -0,0 +1,6 @@
namespace DaisiesContracts.Exceptions;
public class NullListException : Exception
{
public NullListException() : base("The returned list is null") { }
}

View File

@@ -0,0 +1,6 @@
namespace DaisiesContracts.Exceptions;
public class StorageException : Exception
{
public StorageException(Exception ex) : base($"Error while working in storage: {ex.Message}", ex) { }
}

View File

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

View File

@@ -0,0 +1,16 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.StoragesContracts;
public interface IBouquetStorageContract
{
List<BouquetDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? employeeId = null, string? customerId = null, string? flowerId = null);
List<BouquetHistoryDataModel> GetHistoryByBouquetId(string bouquetId);
BouquetDataModel? GetElementById(string id);
void AddElement(BouquetDataModel bouquetDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,10 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.StoragesContracts;
public interface ICustomerDiscountStorageContract
{
List<CustomerDiscountDataModel> GetList(DateTime startDate, DateTime endDate, string? customerId = null);
void AddElement(CustomerDiscountDataModel customerDiscountDataModel);
}

View File

@@ -0,0 +1,20 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.StoragesContracts;
public interface ICustomerStorageContract
{
List<CustomerDataModel> GetList();
CustomerDataModel? GetElementById(string id);
CustomerDataModel? GetElementByPhoneNumber(string phoneNumber);
CustomerDataModel? GetElementByFullName(string fullName);
void AddElement(CustomerDataModel customerDataModel);
void UpdElement(CustomerDataModel customerDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,18 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.StoragesContracts;
public interface IEmployeeStorageContract
{
List<EmployeeDataModel> GetList(bool onlyActive = true, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
EmployeeDataModel? GetElementById(string id);
EmployeeDataModel? GetElementByFullName(string fullName);
void AddElement(EmployeeDataModel employeeDataModel);
void UpdElement(EmployeeDataModel employeeDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,18 @@
using DaisiesContracts.DataModels;
namespace DaisiesContracts.StoragesContracts;
public interface IFlowerStorageContract
{
List<FlowerDataModel> GetList(bool onlyActive = true);
FlowerDataModel? GetElementById(string id);
FlowerDataModel? GetElementByName(string name);
void AddElement(FlowerDataModel flowerDataModel);
void UpdElement(FlowerDataModel flowerDataModel);
void DelElement(string id);
}

View File

@@ -5,7 +5,9 @@ VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DaisiesContracts", "DaisiesContracts\DaisiesContracts.csproj", "{C15353B7-699C-458E-B22F-AF50B4F7D36B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DaisiesTests", "DaisiesTests\DaisiesTests.csproj", "{6DFD5D72-7970-4ED5-864F-2D53F404F4F7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DaisiesTests", "DaisiesTests\DaisiesTests.csproj", "{6DFD5D72-7970-4ED5-864F-2D53F404F4F7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DaisiesBusinessLogic", "DaisiesBusinessLogic\DaisiesBusinessLogic.csproj", "{006545EC-4A37-43DF-AE7F-4120BC98B2E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
{6DFD5D72-7970-4ED5-864F-2D53F404F4F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DFD5D72-7970-4ED5-864F-2D53F404F4F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DFD5D72-7970-4ED5-864F-2D53F404F4F7}.Release|Any CPU.Build.0 = Release|Any CPU
{006545EC-4A37-43DF-AE7F-4120BC98B2E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{006545EC-4A37-43DF-AE7F-4120BC98B2E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{006545EC-4A37-43DF-AE7F-4120BC98B2E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{006545EC-4A37-43DF-AE7F-4120BC98B2E2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,501 @@
using DaisiesContracts.BusinessLogicsContracts;
using DaisiesContracts.Exceptions;
using DaisiesBusinessLogic.Implementations;
using Moq;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using DaisiesContracts.DataModels;
using DaisiesContracts.Enums;
namespace DaisiesTests.BusinessLogicsContractsTests;
[TestFixture]
internal class BouquetBusinessLogicContractTests
{
private BouquetBusinessLogicContract _saleBusinessLogicContract;
private Mock<IBouquetStorageContract> _saleStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_saleStorageContract = new Mock<IBouquetStorageContract>();
_saleBusinessLogicContract = new BouquetBusinessLogicContract(_saleStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_saleStorageContract.Reset();
}
[Test]
public void GetAllBouquetsByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<BouquetDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special,
[new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByPeriod(date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, null), Times.Once);
}
[Test]
public void GetAllBouquetsByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByPeriod(date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByEmployeerByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<BouquetDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special,
[new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(workerId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), workerId, null, null), Times.Once);
}
[Test]
public void GetAllBouquetsByEmployeerByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByEmployeerByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByEmployeerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByEmployeerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByEmployeerByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByEmployeerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByEmployeerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByCustomerByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var buyerId = Guid.NewGuid().ToString();
var listOriginal = new List<BouquetDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special,
[new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(buyerId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, buyerId, null), Times.Once);
}
[Test]
public void GetAllBouquetsByCustomerByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByCustomerByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByCustomerByPeriod_BuyerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByCustomerByPeriod_BuyerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod("buyerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByCustomerByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByCustomerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByCustomerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByFlowerByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var productId = Guid.NewGuid().ToString();
var listOriginal = new List<BouquetDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special,
[new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Special, []),
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(productId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_saleStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, productId), Times.Once);
}
[Test]
public void GetAllBouquetsByFlowerByPeriod_ReturnEmptyList_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByFlowerByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByFlowerByPeriod_ProductIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByFlowerByPeriod_ProductIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod("productId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllBouquetsByFlowerByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllBouquetsByFlowerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetAllBouquetsByFlowerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBouquetByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new BouquetDataModel(id, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Name1", DateTime.UtcNow, BouquetType.Special,
[new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_saleStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _saleBusinessLogicContract.GetBouquetByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBouquetByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetBouquetByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _saleBusinessLogicContract.GetBouquetByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetBouquetByData_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetBouquetByData("saleId"), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetBouquetByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetBouquetByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBouquetByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.GetBouquetByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertBouquet_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new BouquetDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Name1", DateTime.UtcNow, BouquetType.Special,
[new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<BouquetDataModel>()))
.Callback((BouquetDataModel x) =>
{
flag = x.Id == record.Id && x.EmployeeId == record.EmployeeId && x.CustomerId == record.CustomerId &&
x.CompositionDate == record.CompositionDate && x.Type == record.Type && x.Items.Count == record.Items.Count &&
x.Items.First().FlowerId == record.Items.First().FlowerId &&
x.Items.First().BouquetId == record.Items.First().BouquetId &&
x.Items.First().Quantity == record.Items.First().Quantity;
});
//Act
_saleBusinessLogicContract.InsertBouquet(record);
//Assert
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<BouquetDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertBouquet_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<BouquetDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertBouquet(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Wedding, [new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<BouquetDataModel>()), Times.Once);
}
[Test]
public void InsertBouquet_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertBouquet(null), Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<BouquetDataModel>()), Times.Never);
}
[Test]
public void InsertBouquet_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertBouquet(new BouquetDataModel("id", Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Wedding, [])), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<BouquetDataModel>()), Times.Never);
}
[Test]
public void InsertBouquet_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.AddElement(It.IsAny<BouquetDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.InsertBouquet(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(), "name1", DateTime.UtcNow, BouquetType.Wedding, [new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<BouquetDataModel>()), Times.Once);
}
[Test]
public void CancelSale_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_saleStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_saleBusinessLogicContract.CancelBouquet(id);
//Assert
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void CancelSale_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.CancelBouquet(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void CancelSale_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.CancelBouquet(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _saleBusinessLogicContract.CancelBouquet(string.Empty), Throws.TypeOf<ArgumentNullException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelSale_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.CancelBouquet("id"), Throws.TypeOf<ValidationException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelSale_StorageThrowError_ThrowException_Test()
{
//Arrange
_saleStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _saleBusinessLogicContract.CancelBouquet(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_saleStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,350 @@
using DaisiesBusinessLogic.Implementations;
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using Moq;
namespace DaisiesTests.BusinessLogicsContractsTests;
[TestFixture]
internal class CustomerBusinessLogicContractTests
{
private CustomerBusinessLogicContract _buyerBusinessLogicContract;
private Mock<ICustomerStorageContract> _buyerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_buyerStorageContract = new Mock<ICustomerStorageContract>();
_buyerBusinessLogicContract = new CustomerBusinessLogicContract(_buyerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_buyerStorageContract.Reset();
}
[Test]
public void GetAllBuyers_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<CustomerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", 0, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "fio 2", "+7-555-444-33-23", 10, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777", 0, DateTime.UtcNow),
};
_buyerStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act
var list = _buyerBusinessLogicContract.GetAllCustomers();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
}
[Test]
public void GetAllBuyers_ReturnEmptyList_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.GetList()).Returns([]);
//Act
var list = _buyerBusinessLogicContract.GetAllCustomers();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_buyerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllBuyers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetAllCustomers(), Throws.TypeOf<NullListException>());
_buyerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllBuyers_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetAllCustomers(), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetBuyerByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new CustomerDataModel(id, "fio", "+7-111-111-11-11", 0, DateTime.UtcNow);
_buyerStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _buyerBusinessLogicContract.GetCustomerByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_GetByFio_ReturnRecord_Test()
{
//Arrange
var fio = "fio";
var record = new CustomerDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11", 0, DateTime.UtcNow);
_buyerStorageContract.Setup(x => x.GetElementByFullName(fio)).Returns(record);
//Act
var element = _buyerBusinessLogicContract.GetCustomerByData(fio);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.FullName, Is.EqualTo(fio));
_buyerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_GetByPhoneNumber_ReturnRecord_Test()
{
//Arrange
var phoneNumber = "+7-111-111-11-11";
var record = new CustomerDataModel(Guid.NewGuid().ToString(), "fio", phoneNumber, 0, DateTime.UtcNow);
_buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record);
//Act
var element = _buyerBusinessLogicContract.GetCustomerByData(phoneNumber);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber));
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetBuyerByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetBuyerByData_GetByFio_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData("fio"), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetBuyerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData("+7-111-111-11-12"), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Never);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetBuyerByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_buyerStorageContract.Setup(x => x.GetElementByFullName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_buyerStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData("fio"), Throws.TypeOf<StorageException>());
Assert.That(() => _buyerBusinessLogicContract.GetCustomerByData("+7-111-111-11-12"), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Once);
_buyerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertBuyer_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new CustomerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 10, DateTime.UtcNow);
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<CustomerDataModel>()))
.Callback((CustomerDataModel x) =>
{
flag = x.Id == record.Id && x.FullName == record.FullName &&
x.PhoneNumber == record.PhoneNumber && x.RegistrationDate == record.RegistrationDate;
});
//Act
_buyerBusinessLogicContract.InsertCustomer(record);
//Assert
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<CustomerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertBuyer_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<CustomerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertCustomer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<CustomerDataModel>()), Times.Once);
}
[Test]
public void InsertBuyer_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertCustomer(null), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<CustomerDataModel>()), Times.Never);
}
[Test]
public void InsertBuyer_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertCustomer(new CustomerDataModel("id", "fio", "+7-111-111-11-11", 10, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<CustomerDataModel>()), Times.Never);
}
[Test]
public void InsertBuyer_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.AddElement(It.IsAny<CustomerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.InsertCustomer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.AddElement(It.IsAny<CustomerDataModel>()), Times.Once);
}
[Test]
public void UpdateBuyer_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new CustomerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0, DateTime.UtcNow);
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<CustomerDataModel>()))
.Callback((CustomerDataModel x) =>
{
flag = x.Id == record.Id && x.FullName == record.FullName &&
x.PhoneNumber == record.PhoneNumber && x.RegistrationDate == record.RegistrationDate;
});
//Act
_buyerBusinessLogicContract.UpdateCustomer(record);
//Assert
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<CustomerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateBuyer_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<CustomerDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateCustomer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<CustomerDataModel>()), Times.Once);
}
[Test]
public void UpdateBuyer_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<CustomerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateCustomer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<CustomerDataModel>()), Times.Once);
}
[Test]
public void UpdateBuyer_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateCustomer(null), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<CustomerDataModel>()), Times.Never);
}
[Test]
public void UpdateBuyer_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateCustomer(new CustomerDataModel("id", "fio", "+7-111-111-11-11", 10, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<CustomerDataModel>()), Times.Never);
}
[Test]
public void UpdateBuyer_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.UpdElement(It.IsAny<CustomerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.UpdateCustomer(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.UpdElement(It.IsAny<CustomerDataModel>()), Times.Once);
}
[Test]
public void DeleteBuyer_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_buyerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_buyerBusinessLogicContract.DeleteCustomer(id);
//Assert
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteBuyer_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteCustomer(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteBuyer_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteCustomer(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _buyerBusinessLogicContract.DeleteCustomer(string.Empty), Throws.TypeOf<ArgumentNullException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteBuyer_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteCustomer("id"), Throws.TypeOf<ValidationException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteBuyer_StorageThrowError_ThrowException_Test()
{
//Arrange
_buyerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _buyerBusinessLogicContract.DeleteCustomer(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_buyerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,303 @@
using DaisiesBusinessLogic.Implementations;
using DaisiesContracts.DataModels;
using DaisiesContracts.Enums;
using DaisiesContracts.Exceptions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using Moq;
namespace DaisiesTests.BusinessLogicsContractsTests;
[TestFixture]
internal class CustomerDiscountBusinessLogicContractTests
{
private CustomerDiscountBusinessLogicContract _salaryBusinessLogicContract;
private Mock<ICustomerDiscountStorageContract> _salaryStorageContract;
private Mock<IBouquetStorageContract> _saleStorageContract;
private Mock<ICustomerStorageContract> _workerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_salaryStorageContract = new Mock<ICustomerDiscountStorageContract>();
_saleStorageContract = new Mock<IBouquetStorageContract>();
_workerStorageContract = new Mock<ICustomerStorageContract>();
_salaryBusinessLogicContract = new CustomerDiscountBusinessLogicContract(_salaryStorageContract.Object,
_saleStorageContract.Object, _workerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_salaryStorageContract.Reset();
_saleStorageContract.Reset();
_workerStorageContract.Reset();
}
[Test]
public void GetAllSalaries_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var listOriginal = new List<CustomerDiscountDataModel>()
{
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
};
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _salaryBusinessLogicContract.GetAllDiscountsByPeriod(startDate, endDate);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, null), Times.Once);
}
[Test]
public void GetAllSalaries_ReturnEmptyList_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
//Act
var list = _salaryBusinessLogicContract.GetAllDiscountsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalaries_IncorrectDates_ThrowException_Test()
{
//Arrange
var dateTime = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriod(dateTime, dateTime), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriod(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 GetAllSalaries_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalaries_StorageThrowError_ThrowException_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<CustomerDiscountDataModel>()
{
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1), 14),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1), 30),
};
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(startDate, endDate, workerId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, workerId), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_ReturnEmptyList_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
//Act
var list = _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString());
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_IncorrectDates_ThrowException_Test()
{
//Arrange
var dateTime = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(dateTime, dateTime, Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(dateTime, dateTime.AddSeconds(-1), Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByWorker_WorkerIdIsNUllOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(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 GetAllSalariesByWorker_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "workerId"), Throws.TypeOf<ValidationException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByWorker_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllDiscountsByPeriodByCustomer(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void CalculateSalaryByMounth_CalculateSalary_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
var saleSum = 0.0;
var postSalary = 0.0;
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new BouquetDataModel(Guid.NewGuid().ToString(), workerId, workerId, "Name", DateTime.UtcNow, BouquetType.Standard, [])]);
_workerStorageContract.Setup(x => x.GetList())
.Returns([new CustomerDataModel(workerId, "Full Name", "+79999999999", 10, DateTime.UtcNow)]);
var sum = 0.0;
var expectedSum = postSalary + saleSum * 0.1;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<CustomerDiscountDataModel>()))
.Callback((CustomerDiscountDataModel x) =>
{
sum = (double)x.Discount;
});
//Act
_salaryBusinessLogicContract.CalculateDiscountByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMounth_WithSeveralWorkers_Test()
{
//Arrange
var worker1Id = Guid.NewGuid().ToString();
var worker2Id = Guid.NewGuid().ToString();
var worker3Id = Guid.NewGuid().ToString();
var list = new List<CustomerDataModel>() {
new(worker1Id, "Full Name", "+79999999999", 10, DateTime.UtcNow),
new(worker2Id, "Full Name", "+79999999999", 10, DateTime.UtcNow),
new(worker3Id, "Full Name", "+79999999999", 10, DateTime.UtcNow)
};
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([
new BouquetDataModel(Guid.NewGuid().ToString(), worker1Id, worker1Id, "Name", DateTime.UtcNow, BouquetType.Standard, []),
new BouquetDataModel(Guid.NewGuid().ToString(), worker2Id, worker2Id, "Name", DateTime.UtcNow, BouquetType.Standard, []),
new BouquetDataModel(Guid.NewGuid().ToString(), worker2Id, worker2Id, "Name", DateTime.UtcNow, BouquetType.Standard, []),
new BouquetDataModel(Guid.NewGuid().ToString(), worker3Id, worker3Id, "Name", DateTime.UtcNow, BouquetType.Standard, []),
new BouquetDataModel(Guid.NewGuid().ToString(), worker3Id, worker3Id, "Name", DateTime.UtcNow, BouquetType.Standard, [])
]);
_workerStorageContract.Setup(x => x.GetList())
.Returns(list);
//Act
_salaryBusinessLogicContract.CalculateDiscountByMounth(DateTime.UtcNow);
//Assert
_salaryStorageContract.Verify(x => x.AddElement(It.IsAny<CustomerDiscountDataModel>()), Times.Exactly(list.Count));
}
[Test]
public void CalculateSalaryByMounth_WithoitSalesByWorker_Test()
{
//Arrange
var postSalary = 0.0;
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([]);
_workerStorageContract.Setup(x => x.GetList())
.Returns([new CustomerDataModel(workerId, "Full Name", "+79999999999", 10, DateTime.UtcNow)]);
var sum = 0.0;
var expectedSum = postSalary;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<CustomerDiscountDataModel>()))
.Callback((CustomerDiscountDataModel x) =>
{
sum = (double)x.Discount;
});
//Act
_salaryBusinessLogicContract.CalculateDiscountByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMounth_SaleStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_workerStorageContract.Setup(x => x.GetList())
.Returns([new CustomerDataModel(workerId, "Full Name", "+79999999999", 10, DateTime.UtcNow)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateDiscountByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_WorkerStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new BouquetDataModel(Guid.NewGuid().ToString(), workerId, workerId, "Name", DateTime.UtcNow, BouquetType.Standard, [])]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateDiscountByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_SaleStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_workerStorageContract.Setup(x => x.GetList())
.Returns([new CustomerDataModel(workerId, "Full Name", "+79999999999", 10, DateTime.UtcNow)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateDiscountByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
[Test]
public void CalculateSalaryByMounth_WorkerStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new BouquetDataModel(Guid.NewGuid().ToString(), workerId, workerId, "Name", DateTime.UtcNow, BouquetType.Standard, [])]);
_workerStorageContract.Setup(x => x.GetList())
.Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateDiscountByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
}

View File

@@ -0,0 +1,401 @@
using DaisiesBusinessLogic.Implementations;
using DaisiesContracts.DataModels;
using DaisiesContracts.Enums;
using DaisiesContracts.Exceptions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using Moq;
namespace DaisiesTests.BusinessLogicsContractsTests;
[TestFixture]
internal class EmployeeBusinessLogicContractTests
{
private EmployeeBusinessLogicContract _workerBusinessLogicContract;
private Mock<IEmployeeStorageContract> _workerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_workerStorageContract = new Mock<IEmployeeStorageContract>();
_workerBusinessLogicContract = new EmployeeBusinessLogicContract(_workerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_workerStorageContract.Reset();
}
[Test]
public void GetAllEmployeers_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<EmployeeDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", EmployeeRole.Helper, DateTime.Now, false),
new(Guid.NewGuid().ToString(), "fio 2", EmployeeRole.Helper, DateTime.Now, true),
new(Guid.NewGuid().ToString(), "fio 3", EmployeeRole.Helper, DateTime.Now, false),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllEmployeers(true);
var list = _workerBusinessLogicContract.GetAllEmployeers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, null), Times.Once);
}
[Test]
public void GetAllEmployeers_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllEmployeers(true);
var list = _workerBusinessLogicContract.GetAllEmployeers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null), Times.Exactly(2));
}
[Test]
public void GetAllEmployeers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllEmployeers(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeers_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllEmployeers(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null), Times.Once);
}
[Test]
public void GetAllEmployeersByEmploymentDate_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<EmployeeDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", EmployeeRole.Helper, DateTime.Now, false),
new(Guid.NewGuid().ToString(), "fio 2", EmployeeRole.Helper, DateTime.Now, true),
new(Guid.NewGuid().ToString(), "fio 3", EmployeeRole.Helper, DateTime.Now, false),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(date, date.AddDays(1), true);
var list = _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(date, date.AddDays(1), false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, date, date.AddDays(1)), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, date, date.AddDays(1)), Times.Once);
}
[Test]
public void GetAllEmployeersByEmploymentDate_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
var list = _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(true, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeersByEmploymentDate_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllEmployeersByEmploymentDate_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeersByEmploymentDate_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetAllEmployeersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetWorkerByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new EmployeeDataModel(id, "fio", EmployeeRole.Administrator, DateTime.Now, false);
_workerStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _workerBusinessLogicContract.GetEmployeerByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_GetByFio_ReturnRecord_Test()
{
//Arrange
var fio = "fio";
var record = new EmployeeDataModel(Guid.NewGuid().ToString(), fio, EmployeeRole.Administrator, DateTime.Now, false);
_workerStorageContract.Setup(x => x.GetElementByFullName(fio)).Returns(record);
//Act
var element = _workerBusinessLogicContract.GetEmployeerByData(fio);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.FullName, Is.EqualTo(fio));
_workerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetEmployeerByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _workerBusinessLogicContract.GetEmployeerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_workerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetEmployeerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_workerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetEmployeerByData("fio"), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_workerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_workerStorageContract.Setup(x => x.GetElementByFullName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetEmployeerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _workerBusinessLogicContract.GetEmployeerByData("fio"), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_workerStorageContract.Verify(x => x.GetElementByFullName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertWorker_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "Full Name", EmployeeRole.Administrator, DateTime.Now, false);
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<EmployeeDataModel>()))
.Callback((EmployeeDataModel x) =>
{
flag = x.Id == record.Id && x.FullName == record.FullName && x.Role == record.Role &&
x.EmploymentDate == record.EmploymentDate && x.IsActive == record.IsActive;
});
//Act
_workerBusinessLogicContract.InsertEmployeer(record);
//Assert
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertWorker_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<EmployeeDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertEmployeer(new(Guid.NewGuid().ToString(), "Full Name", EmployeeRole.Administrator, DateTime.Now, false)), Throws.TypeOf<ElementExistsException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void InsertWorker_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertEmployeer(null), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void InsertWorker_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertEmployeer(new EmployeeDataModel("id", "fio", EmployeeRole.Administrator, DateTime.Now, false)), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void InsertWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<EmployeeDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertEmployeer(new(Guid.NewGuid().ToString(), "Full Name", EmployeeRole.Administrator, DateTime.Now, false)), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void UpdateWorker_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "Full Name", EmployeeRole.Administrator, DateTime.Now, false);
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<EmployeeDataModel>()))
.Callback((EmployeeDataModel x) =>
{
flag = x.Id == record.Id && x.FullName == record.FullName && x.Role == record.Role &&
x.EmploymentDate == record.EmploymentDate && x.IsActive == record.IsActive;
});
//Act
_workerBusinessLogicContract.UpdateEmployeer(record);
//Assert
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<EmployeeDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateEmployeer(new(Guid.NewGuid().ToString(), "Full Name", EmployeeRole.Administrator, DateTime.Now, false)), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void UpdateWorker_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateEmployeer(null), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void UpdateWorker_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateEmployeer(new EmployeeDataModel("id", "fio", EmployeeRole.Administrator, DateTime.Now, false)), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void UpdateWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<EmployeeDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateEmployeer(new(Guid.NewGuid().ToString(), "Full Name", EmployeeRole.Administrator, DateTime.Now, false)), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void DeleteWorker_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_workerBusinessLogicContract.DeleteEmployeer(id);
//Assert
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x != id))).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteEmployeer(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteEmployeer(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _workerBusinessLogicContract.DeleteEmployeer(string.Empty), Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteWorker_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteEmployeer("id"), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteEmployeer(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,333 @@
using DaisiesBusinessLogic.Implementations;
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
using DaisiesContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using Moq;
using static NUnit.Framework.Internal.OSPlatform;
namespace DaisiesTests.BusinessLogicsContractsTests;
[TestFixture]
internal class FlowerBusinessLogicContractTests
{
private FlowerBusinessLogicContract _productBusinessLogicContract;
private Mock<IFlowerStorageContract> _productStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_productStorageContract = new Mock<IFlowerStorageContract>();
_productBusinessLogicContract = new FlowerBusinessLogicContract(_productStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_productStorageContract.Reset();
}
[Test]
public void GetAllFlowers_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<FlowerDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1", 10),
new(Guid.NewGuid().ToString(), "name 2", 10),
new(Guid.NewGuid().ToString(), "name 3", 10),
};
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
//Act
var listOnlyActive = _productBusinessLogicContract.GetAllFlowers(true);
var list = _productBusinessLogicContract.GetAllFlowers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_productStorageContract.Verify(x => x.GetList(true), Times.Once);
_productStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllFlowers_ReturnEmptyList_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
//Act
var listOnlyActive = _productBusinessLogicContract.GetAllFlowers(true);
var list = _productBusinessLogicContract.GetAllFlowers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllFlowers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetAllFlowers(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllFlowers_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetAllFlowers(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetProductByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new FlowerDataModel(id, "name", 10);
_productStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _productBusinessLogicContract.GetFlowerByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_GetByName_ReturnRecord_Test()
{
//Arrange
var name = "name";
var record = new FlowerDataModel(Guid.NewGuid().ToString(), name, 10);
_productStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
//Act
var element = _productBusinessLogicContract.GetFlowerByData(name);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Name, Is.EqualTo(name));
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetFlowerByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productBusinessLogicContract.GetFlowerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetProductByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetFlowerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_GetByName_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetFlowerByData("name"), Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetProductByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_productStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.GetFlowerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _productBusinessLogicContract.GetFlowerByData("name"), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertProduct_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new FlowerDataModel(Guid.NewGuid().ToString(), "name", 10);
_productStorageContract.Setup(x => x.AddElement(It.IsAny<FlowerDataModel>()))
.Callback((FlowerDataModel x) =>
{
flag = x.Id == record.Id && x.Name == record.Name &&
x.Price == record.Price;
});
//Act
_productBusinessLogicContract.InsertFlower(record);
//Assert
_productStorageContract.Verify(x => x.AddElement(It.IsAny<FlowerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertProduct_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.AddElement(It.IsAny<FlowerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.InsertFlower(new(Guid.NewGuid().ToString(), "name", 10)), Throws.TypeOf<ElementExistsException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<FlowerDataModel>()), Times.Once);
}
[Test]
public void InsertProduct_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.InsertFlower(null), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<FlowerDataModel>()), Times.Never);
}
[Test]
public void InsertProduct_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.InsertFlower(new FlowerDataModel("id", "name", 10)), Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<FlowerDataModel>()), Times.Never);
}
[Test]
public void InsertProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.AddElement(It.IsAny<FlowerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.InsertFlower(new(Guid.NewGuid().ToString(), "name", 10)), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.AddElement(It.IsAny<FlowerDataModel>()), Times.Once);
}
[Test]
public void UpdateProduct_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new FlowerDataModel(Guid.NewGuid().ToString(), "name", 10);
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<FlowerDataModel>()))
.Callback((FlowerDataModel x) =>
{
flag = x.Id == record.Id && x.Name == record.Name &&
x.Price == record.Price;
});
//Act
_productBusinessLogicContract.UpdateFlower(record);
//Assert
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<FlowerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<FlowerDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.UpdateFlower(new(Guid.NewGuid().ToString(), "name", 10)), Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<FlowerDataModel>()), Times.Once);
}
[Test]
public void UpdateProduct_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<FlowerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.UpdateFlower(new(Guid.NewGuid().ToString(), "anme", 10)), Throws.TypeOf<ElementExistsException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<FlowerDataModel>()), Times.Once);
}
[Test]
public void UpdateProduct_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.UpdateFlower(null), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<FlowerDataModel>()), Times.Never);
}
[Test]
public void UpdateProduct_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.UpdateFlower(new FlowerDataModel("id", "name", 10)), Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<FlowerDataModel>()), Times.Never);
}
[Test]
public void UpdateProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<FlowerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.UpdateFlower(new(Guid.NewGuid().ToString(), "name", 10)), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<FlowerDataModel>()), Times.Once);
}
[Test]
public void DeleteProduct_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_productStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_productBusinessLogicContract.DeleteFlower(id);
//Assert
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteProduct_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.DeleteFlower(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.DeleteFlower(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _productBusinessLogicContract.DeleteFlower(string.Empty), Throws.TypeOf<ArgumentNullException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteProduct_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _productBusinessLogicContract.DeleteFlower("id"), Throws.TypeOf<ValidationException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteProduct_StorageThrowError_ThrowException_Test()
{
//Arrange
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _productBusinessLogicContract.DeleteFlower(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -10,7 +10,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
@@ -18,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DaisiesBusinessLogic\DaisiesBusinessLogic.csproj" />
<ProjectReference Include="..\DaisiesContracts\DaisiesContracts.csproj" />
</ItemGroup>

View File

@@ -102,5 +102,5 @@ public class BouquetDataModelTests
new BouquetDataModel(id, employeeId, customerId, name, compositionDate, type, items);
private static List<BouquetItemDataModel> CreateItems() =>
new List<BouquetItemDataModel> { new BouquetItemDataModel(Guid.NewGuid().ToString(), 1) };
new List<BouquetItemDataModel> { new BouquetItemDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) };
}

View File

@@ -6,14 +6,34 @@ namespace DaisiesTests.DataModelTests;
[TestFixture]
public class BouquetItemDataModelTests
{
[Test]
public void Validate_ThrowsException_WhenBouquetIdIsNullOrEmpty()
{
Assert.Multiple(() =>
{
Assert.That(() => CreateDataModel(null, Guid.NewGuid().ToString(), 1).Validate(),
Throws.TypeOf<ValidationException>());
Assert.That(() => CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1).Validate(),
Throws.TypeOf<ValidationException>());
});
}
[Test]
public void Validate_ThrowsException_WhenBouquetIdIsNotGuid()
{
Assert.That(() => CreateDataModel("invalid-guid", Guid.NewGuid().ToString(), 1).Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void Validate_ThrowsException_WhenFlowerIdIsNullOrEmpty()
{
Assert.Multiple(() =>
{
Assert.That(() => CreateDataModel(null, 1).Validate(),
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), null, 1).Validate(),
Throws.TypeOf<ValidationException>());
Assert.That(() => CreateDataModel(string.Empty, 1).Validate(),
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1).Validate(),
Throws.TypeOf<ValidationException>());
});
}
@@ -21,14 +41,15 @@ public class BouquetItemDataModelTests
[Test]
public void Validate_ThrowsException_WhenFlowerIdIsNotGuid()
{
Assert.That(() => CreateDataModel("invalid-guid", 1).Validate(),
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), "invalid-guid", 1).Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void Validate_ThrowsException_WhenQuantityIsNotPositive()
{
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), 0).Validate(),
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0).Validate(),
Throws.TypeOf<ValidationException>());
}
@@ -36,18 +57,20 @@ public class BouquetItemDataModelTests
public void AllFieldsAreCorrectTest()
{
var flowerId = Guid.NewGuid().ToString();
var bouquetId = Guid.NewGuid().ToString();
var quantity = 1;
var item = CreateDataModel(flowerId, quantity);
var item = CreateDataModel(bouquetId, flowerId, quantity);
Assert.That(() => item.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(item.BouquetId, Is.EqualTo(bouquetId));
Assert.That(item.FlowerId, Is.EqualTo(flowerId));
Assert.That(item.Quantity, Is.EqualTo(quantity));
});
}
private static BouquetItemDataModel CreateDataModel(string flowerId, int quantity) =>
new BouquetItemDataModel(flowerId, quantity);
private static BouquetItemDataModel CreateDataModel(string bouquetId, string flowerId, int quantity) =>
new BouquetItemDataModel(bouquetId, flowerId, quantity);
}

View File

@@ -0,0 +1,57 @@
using DaisiesContracts.DataModels;
using DaisiesContracts.Exceptions;
namespace DaisiesTests.DataModelTests;
[TestFixture]
public class CustomerDiscountDataModelTests
{
[Test]
public void Validate_ThrowsException_WhenCustomerIdIsNullOrEmpty()
{
Assert.Multiple(() =>
{
Assert.That(() => CreateDataModel(null, DateTime.UtcNow, 20m).Validate(),
Throws.TypeOf<ValidationException>());
Assert.That(() => CreateDataModel(string.Empty, DateTime.UtcNow, 20m).Validate(),
Throws.TypeOf<ValidationException>());
});
}
[Test]
public void Validate_ThrowsException_WhenCustomerIdIsNotGuid()
{
Assert.That(() => CreateDataModel("invalid-guid", DateTime.UtcNow, 20m).Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void Validate_ThrowsException_WhenDiscountIsOutOfRange()
{
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), DateTime.UtcNow, -1m).Validate(),
Throws.TypeOf<ValidationException>());
Assert.That(() => CreateDataModel(Guid.NewGuid().ToString(), DateTime.UtcNow, 101m).Validate(),
Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsAreCorrectTest()
{
var discountDateTime = DateTime.UtcNow;
var customerId = Guid.NewGuid().ToString();
var discount = 20m;
var customerDiscount = CreateDataModel(customerId, discountDateTime, discount);
Assert.That(() => customerDiscount.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(customerDiscount.DiscountDateTime, Is.EqualTo(discountDateTime));
Assert.That(customerDiscount.CustomerId, Is.EqualTo(customerId));
Assert.That(customerDiscount.Discount, Is.EqualTo(discount));
});
}
private static CustomerDiscountDataModel CreateDataModel(string customerId, DateTime dateTime, decimal discount) =>
new CustomerDiscountDataModel(customerId, dateTime, discount);
}