Compare commits

..

No commits in common. "Task_2_BusinessLogic" and "main" have entirely different histories.

46 changed files with 1 additions and 3715 deletions

View File

@ -1,66 +0,0 @@
using Microsoft.Extensions.Logging;
using System.Text.Json;
using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations;
internal class ComponentBusinessLogicContract(IComponentStorageContract componentStorageContract, ILogger logger) : IComponentBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IComponentStorageContract _componentStorageContract = componentStorageContract;
public List<ComponentDataModel> GetAllComponents(bool onlyActive)
{
_logger.LogInformation("GetAllComponents params: {onlyActive}", onlyActive);
return _componentStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public ComponentDataModel GetComponentByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _componentStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _componentStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
}
public void InsertComponent(ComponentDataModel componentDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate();
_componentStorageContract.AddElement(componentDataModel);
}
public void UpdateComponent(ComponentDataModel componentDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(componentDataModel));
ArgumentNullException.ThrowIfNull(componentDataModel);
componentDataModel.Validate();
_componentStorageContract.UpdElement(componentDataModel);
}
public void DeleteComponent(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");
}
_componentStorageContract.DelElement(id);
}
}

View File

@ -1,104 +0,0 @@
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Text.RegularExpressions;
using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations;
internal class EmployeeBusinessLogicContract(IEmployeeStorageContract employeeStorageContract, ILogger logger) : IEmployeeBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract;
public List<EmployeeDataModel> GetAllEmployees(bool onlyActive = true)
{
_logger.LogInformation("GetAllEmployees params: {onlyActive}", onlyActive);
return _employeeStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<EmployeeDataModel> GetAllEmployeesByPost(string postId, bool onlyActive = true)
{
_logger.LogInformation("GetAllEmployees params: {postId}, {onlyActive},", postId, onlyActive);
if (postId.IsEmpty())
{
throw new ArgumentNullException(nameof(postId));
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier.");
}
return _employeeStorageContract.GetList(onlyActive, postId) ?? throw new NullListException();
}
public List<EmployeeDataModel> GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
_logger.LogInformation("GetAllEmployees params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _employeeStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException();
}
public List<EmployeeDataModel> GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
_logger.LogInformation("GetAllEmployees 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 GetEmployeeByData(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);
}
if (Regex.IsMatch(data, @"^\S+@\S+\.\S+$"))
{
return _employeeStorageContract.GetElementByEmail(data) ?? throw new ElementNotFoundException(data);
}
return _employeeStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
}
public void InsertEmployee(EmployeeDataModel employeeDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(employeeDataModel));
ArgumentNullException.ThrowIfNull(employeeDataModel);
employeeDataModel.Validate();
_employeeStorageContract.AddElement(employeeDataModel);
}
public void UpdateEmployee(EmployeeDataModel employeeDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(employeeDataModel));
ArgumentNullException.ThrowIfNull(employeeDataModel);
employeeDataModel.Validate();
_employeeStorageContract.UpdElement(employeeDataModel);
}
public void DeleteEmployee(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);
}
}

View File

@ -1,97 +0,0 @@
using Microsoft.Extensions.Logging;
using System.Text.Json;
using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations;
internal class InstallationBusinessLogicContract(IInstallationStorageContract installationStorageContract, ILogger logger) : IInstallationBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract;
public List<InstallationDataModel> GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllInstallations params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _installationStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public List<InstallationDataModel> GetAllInstallationsByEmployeeByPeriod(string employeeId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllInstallations params: {employeeId}, {fromDate}, {toDate}", employeeId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (employeeId.IsEmpty())
{
throw new ArgumentNullException(nameof(employeeId));
}
if (!employeeId.IsGuid())
{
throw new ValidationException("The value in the field employeeId is not a unique identifier.");
}
return _installationStorageContract.GetList(fromDate, toDate, employeeId: employeeId) ?? throw new NullListException();
}
public List<InstallationDataModel> GetAllInstallationsByComponentByPeriod(string componentId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllInstallations params: {componentId}, {fromDate}, {toDate}", componentId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (componentId.IsEmpty())
{
throw new ArgumentNullException(nameof(componentId));
}
if (!componentId.IsGuid())
{
throw new ValidationException("The value in the field componentId is not a unique identifier.");
}
return _installationStorageContract.GetList(fromDate, toDate, componentId: componentId) ?? throw new NullListException();
}
public InstallationDataModel GetInstallationByData(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 _installationStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
public void InsertInstallation(InstallationDataModel installationDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(installationDataModel));
ArgumentNullException.ThrowIfNull(installationDataModel);
installationDataModel.Validate();
_installationStorageContract.AddElement(installationDataModel);
}
public void CancelInstallation(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");
}
_installationStorageContract.DelElement(id);
}
}

View File

@ -1,94 +0,0 @@
using Microsoft.Extensions.Logging;
using System.Text.Json;
using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations;
internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly IPostStorageContract _postStorageContract = postStorageContract;
public List<PostDataModel> GetAllPosts(bool onlyActive = true)
{
_logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<PostDataModel> GetAllDataOfPost(string postId)
{
_logger.LogInformation("GetAllDataOfPost for {postId}", postId);
if (postId.IsEmpty())
{
throw new ArgumentNullException(nameof(postId));
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier.");
}
return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException();
}
public PostDataModel GetPostByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
}
return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
}
public void InsertPost(PostDataModel postDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContract.AddElement(postDataModel);
}
public void UpdatePost(PostDataModel postDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContract.UpdElement(postDataModel);
}
public void DeletePost(string id)
{
_logger.LogInformation("Delete by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_postStorageContract.DelElement(id);
}
public void RestorePost(string id)
{
_logger.LogInformation("Restore by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_postStorageContract.ResElement(id);
}
}

View File

@ -1,66 +0,0 @@
using Microsoft.Extensions.Logging;
using TheCyclopsContracts.BusinessLogicContracts;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsBusinessLogic.Implementations;
internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,
IInstallationStorageContract installationStorageContract,
IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract,
ILogger logger) : ISalaryBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
private readonly IInstallationStorageContract _installationStorageContract = installationStorageContract;
private readonly IPostStorageContract _postStorageContract = postStorageContract;
private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract;
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
}
public List<SalaryDataModel> GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate, string employeeId)
{
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (employeeId.IsEmpty())
{
throw new ArgumentNullException(nameof(employeeId));
}
if (!employeeId.IsGuid())
{
throw new ValidationException("The value in the field employeeId is not a unique identifier.");
}
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, {employeeId}", fromDate, toDate, employeeId);
return _salaryStorageContract.GetList(fromDate, toDate, employeeId) ?? throw new NullListException();
}
public void CalculateSalaryByMounth(DateTime date)
{
_logger.LogInformation("CalculateSalaryByMounth: {date}", date);
var startDate = new DateTime(date.Year, date.Month, 1);
var finishDate = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
var employees = _employeeStorageContract.GetList() ?? throw new NullListException();
foreach (var employee in employees)
{
var installations = _installationStorageContract.GetList(startDate, finishDate, employeeId: employee.Id)?.Sum(x => x.InstallationPrice) ??
throw new NullListException();
var post = _postStorageContract.GetElementById(employee.PostId) ??
throw new NullListException();
var salary = post.Salary + installations * 0.1;
_logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary);
_salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, finishDate, salary));
}
}
}

View File

@ -1,22 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="TheCyclopsTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TheCyclopsContracts\TheCyclopsContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -1,16 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.BusinessLogicContracts;
public interface IComponentBusinessLogicContract
{
List<ComponentDataModel> GetAllComponents(bool onlyActive = true);
ComponentDataModel GetComponentByData(string data);
void InsertComponent(ComponentDataModel componentDataModel);
void UpdateComponent(ComponentDataModel componentDataModel);
void DeleteComponent(string id);
}

View File

@ -1,24 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.BusinessLogicContracts;
public interface IEmployeeBusinessLogicContract
{
List<EmployeeDataModel> GetAllEmployees(bool onlyActive = true);
List<EmployeeDataModel> GetAllEmployeesByPost(string postId, bool onlyActive = true);
List<EmployeeDataModel> GetAllEmployeesByBirthDate(DateTime fromDate, DateTime toDate,
bool onlyActive = true);
List<EmployeeDataModel> GetAllEmployeesByEmploymentDate(DateTime fromDate, DateTime toDate,
bool onlyActive = true);
EmployeeDataModel GetEmployeeByData(string data);
void InsertEmployee(EmployeeDataModel employeeDataModel);
void UpdateEmployee(EmployeeDataModel employeeDataModel);
void DeleteEmployee(string id);
}

View File

@ -1,20 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.BusinessLogicContracts;
public interface IInstallationBusinessLogicContract
{
List<InstallationDataModel> GetAllInstallationsByPeriod(DateTime fromDate, DateTime toDate);
List<InstallationDataModel> GetAllInstallationsByEmployeeByPeriod(string employeeId,
DateTime fromDate, DateTime toDate);
List<InstallationDataModel> GetAllInstallationsByComponentByPeriod(string componentId,
DateTime fromDate, DateTime toDate);
InstallationDataModel GetInstallationByData(string data);
void InsertInstallation(InstallationDataModel installationDataModel);
void CancelInstallation(string id);
}

View File

@ -1,20 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.BusinessLogicContracts;
public interface IPostBusinessLogicContract
{
List<PostDataModel> GetAllPosts(bool onlyActive);
List<PostDataModel> GetAllDataOfPost(string postId);
PostDataModel GetPostByData(string data);
void InsertPost(PostDataModel postDataModel);
void UpdatePost(PostDataModel postDataModel);
void DeletePost(string id);
void RestorePost(string id);
}

View File

@ -1,13 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.BusinessLogicContracts;
public interface ISalaryBusinessLogicContract
{
List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate);
List<SalaryDataModel> GetAllSalariesByPeriodByEmployee(DateTime fromDate, DateTime toDate,
string employeeId);
void CalculateSalaryByMounth(DateTime date);
}

View File

@ -1,38 +0,0 @@
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class ComponentDataModel(string id, string componentName,
ComponentType componentType, double price, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string ComponentName { get; private set; } = componentName;
public ComponentType ComponentType { get; private set; } = componentType;
public double Price { get; private set; } = price;
public bool IsDeleted { get; private set; } = isDeleted;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (ComponentName.IsEmpty())
throw new ValidationException("Field ComponentName is empty");
if (ComponentType == ComponentType.None)
throw new ValidationException("Field ComponentType is empty");
if (Price <= 0)
throw new ValidationException("Field Price is less than or equal to 0");
}
}

View File

@ -1,57 +0,0 @@
using System.Text.RegularExpressions;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class EmployeeDataModel(string id, string fio, string email, string postId,
DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string Email { get; private set; } = email;
public string PostId { get; private set; } = postId;
public DateTime BirthDate { get; private set; } = birthDate;
public DateTime EmploymentDate { get; private set; } = employmentDate;
public bool IsDeleted { get; private set; } = isDeleted;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (FIO.IsEmpty())
throw new ValidationException("Field FIO is empty");
if (Email.IsEmpty())
throw new ValidationException("Field Email is empty");
if (!Regex.IsMatch(Email, @"^\S+@\S+\.\S+$"))
throw new ValidationException("Field Email is not an email address");
if (PostId.IsEmpty())
throw new ValidationException("Field PostId is empty");
if (!PostId.IsGuid())
throw new ValidationException("The value in the field PostId is not a unique identifier");
if (BirthDate.Date > DateTime.Now.AddYears(-16).Date)
throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})");
if (EmploymentDate.Date < BirthDate.Date)
throw new ValidationException("The date of employment cannot be less than the date of birth");
if ((EmploymentDate - BirthDate).TotalDays / 365 < 16)
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@ -1,33 +0,0 @@
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class InstallationComponentDataModel(string installationId,
string componentId, int count) : IValidation
{
public string InstallationId { get; private set; } = installationId;
public string ComponentId { get; private set; } = componentId;
public int Count { get; private set; } = count;
public void Validate()
{
if (InstallationId.IsEmpty())
throw new ValidationException("Field InstallationId is empty");
if (!InstallationId.IsGuid())
throw new ValidationException("The value in the field InstallationId is not a unique identifier");
if (ComponentId.IsEmpty())
throw new ValidationException("Field ComponentId is empty");
if (!ComponentId.IsGuid())
throw new ValidationException("The value in the field ComponentId is not a unique identifier");
if (Count <= 0)
throw new ValidationException("Field Count is less than or equal to 0");
}
}

View File

@ -1,50 +0,0 @@
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class InstallationDataModel(string id, string employeeId,
DateTime installationDate, double installationPrice, double sum, bool isCanceled,
List<InstallationComponentDataModel> components) : IValidation
{
public string Id { get; private set; } = id;
public string EmployeeId { get; private set; } = employeeId;
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
public DateTime InstallationDate { get; private set; } = installationDate;
public double InstallationPrice { get; private set; } = installationPrice;
public double Sum { get; private set; } = sum;
public bool IsCanceled { get; private set; } = isCanceled;
public List<InstallationComponentDataModel> Components { get; private set; } = components;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (EmployeeId.IsEmpty())
throw new ValidationException("Field EmployeeId is empty");
if (!EmployeeId.IsGuid())
throw new ValidationException("The value in the field EmployeeId is not a unique identifier");
if (InstallationPrice <= 0)
throw new ValidationException("Field InstallationPrice is less than or equal to 0");
if (Sum <= 0)
throw new ValidationException("Field Sum is less than or equal to 0");
if ((Components?.Count ?? 0) == 0)
throw new ValidationException("The installation must include components");
}
}

View File

@ -1,38 +0,0 @@
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
namespace TheCyclopsContracts.DataModels;
public class PostDataModel(string id, string postName, PostType postType, double salary, bool isActual, DateTime changeDate)
{
public string Id { get; private set; } = id;
public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary;
public bool IsActual { get; private set; } = isActual;
public DateTime ChangeDate { get; private set; } = changeDate;
public void Validate()
{
if (Id.IsEmpty())
throw new ValidationException("Field Id is empty");
if (!Id.IsGuid())
throw new ValidationException("The value in the field Id is not a unique identifier");
if (PostName.IsEmpty())
throw new ValidationException("Field PostName is empty");
if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty");
if (Salary <= 0)
throw new ValidationException("Field Salary is empty");
}
}

View File

@ -1,26 +0,0 @@
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.Extensions;
using TheCyclopsContracts.Infrastructure;
namespace TheCyclopsContracts.DataModels;
public class SalaryDataModel(string employeeId, DateTime salaryDate, double employeeSalary) : IValidation
{
public string EmployeeId { get; private set; } = employeeId;
public DateTime SalaryDate { get; private set; } = salaryDate;
public double Salary { get; private set; } = employeeSalary;
public void Validate()
{
if (EmployeeId.IsEmpty())
throw new ValidationException("Field EmployeeId is empty");
if (!EmployeeId.IsGuid())
throw new ValidationException("The value in the field EmployeeId is not a unique identifier");
if (Salary <= 0)
throw new ValidationException("Field Salary is less than or equal to 0");
}
}

View File

@ -1,11 +0,0 @@
namespace TheCyclopsContracts.Enums;
public enum ComponentType
{
None = 0,
Camera = 1,
Sensor = 2,
ContropPanel = 3,
Cable = 4,
Other = 5
}

View File

@ -1,10 +0,0 @@
namespace TheCyclopsContracts.Enums;
public enum PostType
{
None = 0,
Designer = 1,
Installer = 2,
Seller = 3,
Administrator = 4
}

View File

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

@ -1,11 +0,0 @@
namespace TheCyclopsContracts.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

@ -1,8 +0,0 @@
namespace TheCyclopsContracts.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

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

View File

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

View File

@ -1,5 +0,0 @@
namespace TheCyclopsContracts.Exceptions;
public class ValidationException(string message) : Exception(message)
{
}

View File

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

View File

@ -1,14 +0,0 @@
namespace TheCyclopsContracts.Extensions;
public static class StringExtensions
{
public static bool IsEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
public static bool IsGuid(this string str)
{
return Guid.TryParse(str, out _);
}
}

View File

@ -1,6 +0,0 @@
namespace TheCyclopsContracts.Infrastructure;
public interface IValidation
{
void Validate();
}

View File

@ -1,20 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.StoragesContracts;
public interface IComponentStorageContract
{
List<ComponentDataModel> GetList(bool onlyActive = true);
List<ComponentDataModel> GetHistoryByProductId(string componentId);
ComponentDataModel? GetElementById(string id);
ComponentDataModel? GetElementByName(string name);
void AddElement(ComponentDataModel componentDataModel);
void UpdElement(ComponentDataModel componentDataModel);
void DelElement(string id);
}

View File

@ -1,22 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.StoragesContracts;
public interface IEmployeeStorageContract
{
List<EmployeeDataModel> GetList(bool onlyActive = true, string? postId = null,
DateTime? fromBirthDate = null, DateTime? toBirthDate = null,
DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
EmployeeDataModel? GetElementById(string id);
EmployeeDataModel? GetElementByFIO(string fio);
EmployeeDataModel? GetElementByEmail(string email);
void AddElement(EmployeeDataModel employeeDataModel);
void UpdElement(EmployeeDataModel employeeDataModel);
void DelElement(string id);
}

View File

@ -1,15 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.StoragesContracts;
public interface IInstallationStorageContract
{
List<InstallationDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null,
string? employeeId = null, string? componentId = null);
InstallationDataModel? GetElementById(string id);
void AddElement(InstallationDataModel installationDataModel);
void DelElement(string id);
}

View File

@ -1,22 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.StoragesContracts;
public interface IPostStorageContract
{
List<PostDataModel> GetList(bool onlyActual = true);
List<PostDataModel> GetPostWithHistory(string postId);
PostDataModel? GetElementById(string id);
PostDataModel? GetElementByName(string name);
void AddElement(PostDataModel postDataModel);
void UpdElement(PostDataModel postDataModel);
void DelElement(string id);
void ResElement(string id);
}

View File

@ -1,11 +0,0 @@
using TheCyclopsContracts.DataModels;
namespace TheCyclopsContracts.StoragesContracts;
public interface ISalaryStorageContract
{
List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate,
string? employeeId = null);
void AddElement(SalaryDataModel salaryDataModel);
}

View File

@ -1,14 +1,10 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsContracts", "TheCyclopsContracts\TheCyclopsContracts.csproj", "{01934235-715E-4B95-B859-48E8C89DA7CB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsTests", "TheCyclopsTests\TheCyclopsTests.csproj", "{78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheCyclopsBusinessLogic", "TheCyclopsBusinessLogic\TheCyclopsBusinessLogic.csproj", "{A696C749-D71B-4D1E-B2B0-1E1503F033F0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -19,14 +15,6 @@ Global
{01934235-715E-4B95-B859-48E8C89DA7CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01934235-715E-4B95-B859-48E8C89DA7CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01934235-715E-4B95-B859-48E8C89DA7CB}.Release|Any CPU.Build.0 = Release|Any CPU
{78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78C9F1B1-ABD3-4ED2-A5DC-84766DEEFC77}.Release|Any CPU.Build.0 = Release|Any CPU
{A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A696C749-D71B-4D1E-B2B0-1E1503F033F0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,356 +0,0 @@
using Microsoft.Extensions.Logging;
using Moq;
using TheCyclopsBusinessLogic.Implementations;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsTests.BusinessLogicsContractsTests;
[TestFixture]
internal class ComponentBusinessLogicContractTests
{
private ComponentBusinessLogicContract _componentBusinessLogicContract;
private Mock<IComponentStorageContract> _componentStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_componentStorageContract = new Mock<IComponentStorageContract>();
_componentBusinessLogicContract = new ComponentBusinessLogicContract(_componentStorageContract.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_componentStorageContract.Reset();
}
[Test]
public void GetAllComponents_ReturnListOfRecords_Test()
{
// Arrange
var listOriginal = new List<ComponentDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1", ComponentType.Other, 10, false),
new(Guid.NewGuid().ToString(), "name 2", ComponentType.Other, 10, true),
new(Guid.NewGuid().ToString(), "name 3", ComponentType.Other, 10, false),
};
_componentStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
// Act
var listOnlyActive = _componentBusinessLogicContract.GetAllComponents(true);
var list = _componentBusinessLogicContract.GetAllComponents(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));
});
_componentStorageContract.Verify(x => x.GetList(true), Times.Once);
_componentStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllComponents_ReturnEmptyList_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(new List<ComponentDataModel>());
// Act
var listOnlyActive = _componentBusinessLogicContract.GetAllComponents(true);
var list = _componentBusinessLogicContract.GetAllComponents(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));
});
_componentStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllComponents_ReturnNull_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetAllComponents(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_componentStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllComponents_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetAllComponents(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetComponentByData_GetById_ReturnRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var record = new ComponentDataModel(id, "name", ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
// Act
var element = _componentBusinessLogicContract.GetComponentByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_GetByName_ReturnRecord_Test()
{
// Arrange
var name = "name";
var record = new ComponentDataModel(Guid.NewGuid().ToString(), name, ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
// Act
var element = _componentBusinessLogicContract.GetComponentByData(name);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.ComponentName, Is.EqualTo(name));
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_EmptyData_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetComponentByData_GetById_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_GetByName_NotFoundRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData("name"), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetComponentByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_componentStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _componentBusinessLogicContract.GetComponentByData("name"), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_componentStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertComponent_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new ComponentDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.AddElement(It.IsAny<ComponentDataModel>()))
.Callback((ComponentDataModel x) =>
{
flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType &&
x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
// Act
_componentBusinessLogicContract.InsertComponent(record);
// Assert
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertComponent_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.AddElement(It.IsAny<ComponentDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<ElementExistsException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void InsertComponent_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(null), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void InsertComponent_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(new ComponentDataModel("id", "name", ComponentType.Other, 10, false)), Throws.TypeOf<ValidationException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void InsertComponent_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.AddElement(It.IsAny<ComponentDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.AddElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void UpdateComponent_CorrectRecord_Test()
{
// Arrange
var flag = false;
var record = new ComponentDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false);
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>()))
.Callback((ComponentDataModel x) =>
{
flag = x.Id == record.Id && x.ComponentName == record.ComponentName && x.ComponentType == record.ComponentType &&
x.Price == record.Price && x.IsDeleted == record.IsDeleted;
});
// Act
_componentBusinessLogicContract.UpdateComponent(record);
// Assert
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateComponent_RecordWithIncorrectData_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>())).Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void UpdateComponent_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "anme", ComponentType.Other, 10, false)), Throws.TypeOf<ElementExistsException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void UpdateComponent_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(null), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void UpdateComponent_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new ComponentDataModel("id", "name", ComponentType.Other, 10, false)), Throws.TypeOf<ValidationException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Never);
}
[Test]
public void UpdateComponent_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.UpdElement(It.IsAny<ComponentDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), "name", ComponentType.Other, 10, false)), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.UpdElement(It.IsAny<ComponentDataModel>()), Times.Once);
}
[Test]
public void DeleteComponent_CorrectRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_componentStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
// Act
_componentBusinessLogicContract.DeleteComponent(id);
// Assert
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteComponent_RecordWithIncorrectId_ThrowException_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
_componentStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteComponent_IdIsNullOrEmpty_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(string.Empty), Throws.TypeOf<ArgumentNullException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteComponent_IdIsNotGuid_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent("id"), Throws.TypeOf<ValidationException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteComponent_StorageThrowError_ThrowException_Test()
{
// Arrange
_componentStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _componentBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_componentStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@ -1,581 +0,0 @@
using Microsoft.Extensions.Logging;
using Moq;
using TheCyclopsBusinessLogic.Implementations;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsTests.BusinessLogicsContractsTests;
[TestFixture]
internal class EmployeeBusinessLogicContractTest
{
private EmployeeBusinessLogicContract _employeeBusinessLogicContract;
private Mock<IEmployeeStorageContract> _employeeStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_employeeStorageContract = new Mock<IEmployeeStorageContract>();
_employeeBusinessLogicContract = new EmployeeBusinessLogicContract(_employeeStorageContract.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_employeeStorageContract.Reset();
}
[Test]
public void GetAllEmployees_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<EmployeeDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
new(Guid.NewGuid().ToString(), "fio 3", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
};
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployees(true);
var list = _employeeBusinessLogicContract.GetAllEmployees(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));
});
_employeeStorageContract.Verify(x => x.GetList(true, null, null, null, null, null), Times.Once);
_employeeStorageContract.Verify(x => x.GetList(false, null, null, null, null, null), Times.Once);
}
[Test]
public void GetAllEmployees_ReturnEmptyList_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployees(true);
var list = _employeeBusinessLogicContract.GetAllEmployees(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));
});
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null, null, null, null), Times.Exactly(2));
}
[Test]
public void GetAllEmployees_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployees(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployees_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployees(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null, null, null, null, null), Times.Once);
}
[Test]
public void GetAllEmployeesByPost_ReturnListOfRecords_Test()
{
//Arrange
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<EmployeeDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1","email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
new(Guid.NewGuid().ToString(), "fio 3", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
};
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByPost(postId, true);
var list = _employeeBusinessLogicContract.GetAllEmployeesByPost(postId, 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));
});
_employeeStorageContract.Verify(x => x.GetList(true, postId, null, null, null, null), Times.Once);
_employeeStorageContract.Verify(x => x.GetList(false, postId, null, null, null, null), Times.Once);
}
[Test]
public void GetAllEmployeesByPost_ReturnEmptyList_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByPost(Guid.NewGuid().ToString(), true);
var list = _employeeBusinessLogicContract.GetAllEmployeesByPost(Guid.NewGuid().ToString(), 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));
});
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Exactly(2));
}
[Test]
public void GetAllEmployeesByPost_PostIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(null, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(string.Empty, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllEmployeesByPost_PostIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost("postId", It.IsAny<bool>()), Throws.TypeOf<ValidationException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllEmployeesByPost_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeesByPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByPost(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeesByBirthDate_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<EmployeeDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
new(Guid.NewGuid().ToString(), "fio 3","email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
};
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(date, date.AddDays(1), true);
var list = _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(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));
});
_employeeStorageContract.Verify(x => x.GetList(true, null, date, date.AddDays(1), null, null), Times.Once);
_employeeStorageContract.Verify(x => x.GetList(false, null, date, date.AddDays(1), null, null), Times.Once);
}
[Test]
public void GetAllEmployeesByBirthDate_ReturnEmptyList_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
var list = _employeeBusinessLogicContract.GetAllEmployees(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));
});
_employeeStorageContract.Verify(x => x.GetList(true, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
_employeeStorageContract.Verify(x => x.GetList(false, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
}
[Test]
public void GetAllEmployeesByBirthDate_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllEmployeesByBirthDate_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeesByBirthDate_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeesByEmploymentDate_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<EmployeeDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
new(Guid.NewGuid().ToString(), "fio 2", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true),
new(Guid.NewGuid().ToString(), "fio 3", "email@mail.com",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false),
};
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(date, date.AddDays(1), true);
var list = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(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));
});
_employeeStorageContract.Verify(x => x.GetList(true, null, null, null, date, date.AddDays(1)), Times.Once);
_employeeStorageContract.Verify(x => x.GetList(false, null, null, null, date, date.AddDays(1)), Times.Once);
}
[Test]
public void GetAllEmployeesByEmploymentDate_ReturnEmptyList_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
var list = _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(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));
});
_employeeStorageContract.Verify(x => x.GetList(true, null, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
_employeeStorageContract.Verify(x => x.GetList(false, null, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeesByEmploymentDate_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllEmployeesByEmploymentDate_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllEmployeesByEmploymentDate_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetEmployeeByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new EmployeeDataModel(id, "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
_employeeStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _employeeBusinessLogicContract.GetEmployeeByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_employeeStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetEmployeeByData_GetByFio_ReturnRecord_Test()
{
//Arrange
var fio = "fio";
var record = new EmployeeDataModel(Guid.NewGuid().ToString(), fio, "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
_employeeStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
//Act
var element = _employeeBusinessLogicContract.GetEmployeeByData(fio);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.FIO, Is.EqualTo(fio));
_employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetEmployeeByData_GetByEmail_ReturnRecord_Test()
{
//Arrange
var email = "email@mail.com";
var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "фио", email, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
_employeeStorageContract.Setup(x => x.GetElementByEmail(email)).Returns(record);
//Act
var element = _employeeBusinessLogicContract.GetEmployeeByData(email);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Email, Is.EqualTo(email));
_employeeStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetEmployeeByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_employeeStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetEmployeeByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_employeeStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetEmployeeByData_GetByFio_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData("fio"), Throws.TypeOf<ElementNotFoundException>());
_employeeStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetEmployeeByData_GetByEmail_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData("email@mail.com"), Throws.TypeOf<ElementNotFoundException>());
_employeeStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_employeeStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetEmployeeByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_employeeStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _employeeBusinessLogicContract.GetEmployeeByData("fio"), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_employeeStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertEmployee_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
_employeeStorageContract.Setup(x => x.AddElement(It.IsAny<EmployeeDataModel>()))
.Callback((EmployeeDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
});
//Act
_employeeBusinessLogicContract.InsertEmployee(record);
//Assert
_employeeStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertEmployee_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.AddElement(It.IsAny<EmployeeDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ElementExistsException>());
_employeeStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void InsertEmployee_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(null), Throws.TypeOf<ArgumentNullException>());
_employeeStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void InsertEmployee_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(new EmployeeDataModel("id", "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
_employeeStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void InsertEmployee_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.AddElement(It.IsAny<EmployeeDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.InsertEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.AddElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void UpdateEmployee_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new EmployeeDataModel(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
_employeeStorageContract.Setup(x => x.UpdElement(It.IsAny<EmployeeDataModel>()))
.Callback((EmployeeDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO && x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
});
//Act
_employeeBusinessLogicContract.UpdateEmployee(record);
//Assert
_employeeStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateEmployee_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.UpdElement(It.IsAny<EmployeeDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ElementNotFoundException>());
_employeeStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void UpdateEmployee_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(null), Throws.TypeOf<ArgumentNullException>());
_employeeStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void UpdateEmployee_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(new EmployeeDataModel("id", "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
_employeeStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Never);
}
[Test]
public void UpdateEmployee_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.UpdElement(It.IsAny<EmployeeDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.UpdateEmployee(new(Guid.NewGuid().ToString(), "fio", "email@mail.com", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.UpdElement(It.IsAny<EmployeeDataModel>()), Times.Once);
}
[Test]
public void DeleteEmployee_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_employeeStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_employeeBusinessLogicContract.DeleteEmployee(id);
//Assert
_employeeStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteEmployee_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_employeeStorageContract.Setup(x => x.DelElement(It.Is((string x) => x != id))).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_employeeStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteEmployee_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(string.Empty), Throws.TypeOf<ArgumentNullException>());
_employeeStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteEmployee_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee("id"), Throws.TypeOf<ValidationException>());
_employeeStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteEmployee_StorageThrowError_ThrowException_Test()
{
//Arrange
_employeeStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _employeeBusinessLogicContract.DeleteEmployee(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_employeeStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@ -1,417 +0,0 @@
using Microsoft.Extensions.Logging;
using Moq;
using TheCyclopsBusinessLogic.Implementations;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsTests.BusinessLogicsContractsTests;
internal class InstallationBusinessLogicContractTest
{
private InstallationBusinessLogicContract _installationBusinessLogicContract;
private Mock<IInstallationStorageContract> _installationStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_installationStorageContract = new Mock<IInstallationStorageContract>();
_installationBusinessLogicContract = new InstallationBusinessLogicContract(_installationStorageContract.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_installationStorageContract.Reset();
}
[Test]
public void GetAllInstallationsByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<InstallationDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false,
[new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []),
};
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _installationBusinessLogicContract.GetAllInstallationsByPeriod(date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_installationStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null), Times.Once);
}
[Test]
public void GetAllInstallationsByPeriod_ReturnEmptyList_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _installationBusinessLogicContract.GetAllInstallationsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllInstallationsByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByEmployeeByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var employeeId = Guid.NewGuid().ToString();
var listOriginal = new List<InstallationDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false,
[new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []),
};
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(employeeId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_installationStorageContract.Verify(x => x.GetList(date, date.AddDays(1), employeeId, null), Times.Once);
}
[Test]
public void GetAllInstallationsByEmployeeByPeriod_ReturnEmptyList_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByEmployeeByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllInstallationsByEmployeeByPeriod_EmployeeIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllInstallationsByEmployeeByPeriod_EmployeeIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod("employeeId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllInstallationsByEmployeeByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByEmployeeByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByEmployeeByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByComponentByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var componentId = Guid.NewGuid().ToString();
var listOriginal = new List<InstallationDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false,
[new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, []),
};
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(componentId, date, date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_installationStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, componentId), Times.Once);
}
[Test]
public void GetAllInstallationsByComponentByPeriod_ReturnEmptyList_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
//Act
var list = _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByComponentByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllInstallationsByComponentByPeriod_ComponentIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllInstallationsByComponentByPeriod_ComponentIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod("componentId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllInstallationsByComponentByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllInstallationsByComponentByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetAllInstallationsByComponentByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_installationStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetInstallationByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new InstallationDataModel(id, Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false,
[new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_installationStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _installationBusinessLogicContract.GetInstallationByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_installationStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetInstallationByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_installationStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetInstallationByData_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetInstallationByData("installationId"), Throws.TypeOf<ValidationException>());
_installationStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetInstallationByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_installationStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetInstallationByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.GetInstallationByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_installationStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertInstallation_CorrectRecord_Test()
{
var flag = false;
var record = new InstallationDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 10, false,
[new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_installationStorageContract.Setup(x => x.AddElement(It.IsAny<InstallationDataModel>()))
.Callback((InstallationDataModel x) =>
{
flag = x.Id == record.Id && x.EmployeeId == record.EmployeeId &&
x.InstallationDate == record.InstallationDate && x.Sum == record.Sum &&
x.IsCanceled == record.IsCanceled && x.Components.Count == record.Components.Count &&
x.Components.First().ComponentId == record.Components.First().ComponentId &&
x.Components.First().InstallationId == record.Components.First().InstallationId &&
x.Components.First().Count == record.Components.First().Count;
});
//Act
_installationBusinessLogicContract.InsertInstallation(record);
//Assert
_installationStorageContract.Verify(x => x.AddElement(It.IsAny<InstallationDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertInstallation_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.AddElement(It.IsAny<InstallationDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 10, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_installationStorageContract.Verify(x => x.AddElement(It.IsAny<InstallationDataModel>()), Times.Once);
}
[Test]
public void InsertInstallation_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.InsertInstallation(null), Throws.TypeOf<ArgumentNullException>());
_installationStorageContract.Verify(x => x.AddElement(It.IsAny<InstallationDataModel>()), Times.Never);
}
[Test]
public void InsertInstallation_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new InstallationDataModel("id", Guid.NewGuid().ToString(), DateTime.UtcNow, 10, 0, false, [])), Throws.TypeOf<ValidationException>());
_installationStorageContract.Verify(x => x.AddElement(It.IsAny<InstallationDataModel>()), Times.Never);
}
[Test]
public void InsertInstallation_StorageThrowError_ThrowException_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.AddElement(It.IsAny<InstallationDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.InsertInstallation(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 10, false, [new InstallationComponentDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_installationStorageContract.Verify(x => x.AddElement(It.IsAny<InstallationDataModel>()), Times.Once);
}
[Test]
public void CancelInstallation_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_installationStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_installationBusinessLogicContract.CancelInstallation(id);
//Assert
_installationStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void CancelInstallation_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_installationStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.CancelInstallation(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_installationStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void CancelInstallation_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.CancelInstallation(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _installationBusinessLogicContract.CancelInstallation(string.Empty), Throws.TypeOf<ArgumentNullException>());
_installationStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelInstallation_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.CancelInstallation("id"), Throws.TypeOf<ValidationException>());
_installationStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelInstallation_StorageThrowError_ThrowException_Test()
{
//Arrange
_installationStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _installationBusinessLogicContract.CancelInstallation(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_installationStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@ -1,454 +0,0 @@
using Microsoft.Extensions.Logging;
using Moq;
using TheCyclopsBusinessLogic.Implementations;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsTests.BusinessLogicsContractsTests;
[TestFixture]
internal class PostBusinessLogicContractTest
{
private PostBusinessLogicContract _postBusinessLogicContract;
private Mock<IPostStorageContract> _postStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_postStorageContract = new Mock<IPostStorageContract>();
_postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_postStorageContract.Reset();
}
[Test]
public void GetAllPosts_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<PostDataModel>()
{
new(Guid.NewGuid().ToString(),"name 1", PostType.Administrator, 10, true, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 2", PostType.Administrator, 10, false, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 3", PostType.Administrator, 10, true, DateTime.UtcNow),
};
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(listAll, Is.EquivalentTo(listOriginal));
});
_postStorageContract.Verify(x => x.GetList(true), Times.Once);
_postStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllPosts_ReturnEmptyList_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(listAll, Has.Count.EqualTo(0));
});
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
}
[Test]
public void GetAllPosts_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllPosts_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_ReturnListOfRecords_Test()
{
//Arrange
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<PostDataModel>()
{
new(postId, "name 1", PostType.Administrator, 10, true, DateTime.UtcNow),
new(postId, "name 2", PostType.Administrator, 10, false, DateTime.UtcNow)
};
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _postBusinessLogicContract.GetAllDataOfPost(postId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
_postStorageContract.Verify(x => x.GetPostWithHistory(postId), Times.Once);
}
[Test]
public void GetAllDataOfPost_ReturnEmptyList_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns([]);
//Act
var list = _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString());
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new PostDataModel(id, "name", PostType.Administrator, 10, true, DateTime.UtcNow);
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_GetByName_ReturnRecord_Test()
{
//Arrange
var postName = "name";
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Administrator, 10, true, DateTime.UtcNow);
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(postName);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.PostName, Is.EqualTo(postName));
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.GetPostByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertPost_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 10, true, DateTime.UtcNow.AddDays(-1));
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
});
//Act
_postBusinessLogicContract.InsertPost(record);
//Assert
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertPost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Designer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void InsertPost_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(null), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void InsertPost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Designer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void InsertPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Designer, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 10, true, DateTime.UtcNow.AddDays(-1));
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
});
//Act
_postBusinessLogicContract.UpdatePost(record);
//Assert
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Designer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Designer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(null), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void UpdatePost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Designer, 10, true, DateTime.UtcNow)), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void UpdatePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Designer, 10, true, DateTime.UtcNow)), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void DeletePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.DeletePost(id);
//Assert
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeletePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void RestorePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.RestorePost(id);
//Assert
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void RestorePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
}
}

View File

@ -1,348 +0,0 @@
using Microsoft.Extensions.Logging;
using Moq;
using TheCyclopsBusinessLogic.Implementations;
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
using TheCyclopsContracts.StoragesContracts;
namespace TheCyclopsTests.BusinessLogicsContractsTests;
[TestFixture]
internal class SalaryBusinessLogicContractTest
{
private SalaryBusinessLogicContract _salaryBusinessLogicContract;
private Mock<ISalaryStorageContract> _salaryStorageContract;
private Mock<IInstallationStorageContract> _installationStorageContract;
private Mock<IPostStorageContract> _postStorageContract;
private Mock<IEmployeeStorageContract> _employeeStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_salaryStorageContract = new Mock<ISalaryStorageContract>();
_installationStorageContract = new Mock<IInstallationStorageContract>();
_postStorageContract = new Mock<IPostStorageContract>();
_employeeStorageContract = new Mock<IEmployeeStorageContract>();
_salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object,
_installationStorageContract.Object, _postStorageContract.Object,
_employeeStorageContract.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_salaryStorageContract.Reset();
_installationStorageContract.Reset();
_postStorageContract.Reset();
_employeeStorageContract.Reset();
}
[Test]
public void GetAllSalaries_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var listOriginal = new List<SalaryDataModel>()
{
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.GetAllSalariesByPeriod(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.GetAllSalariesByPeriod(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.GetAllSalariesByPeriod(dateTime, dateTime), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalaries_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriod(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.GetAllSalariesByPeriod(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 GetAllSalariesByEmployee_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var employeeId = Guid.NewGuid().ToString();
var listOriginal = new List<SalaryDataModel>()
{
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 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.GetAllSalariesByPeriodByEmployee(startDate, endDate, employeeId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, employeeId), Times.Once);
}
[Test]
public void GetAllSalariesByEmployee_ReturnEmptyList_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
//Act
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(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 GetAllSalariesByEmployee_IncorrectDates_ThrowException_Test()
{
//Arrange
var dateTime = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(dateTime, dateTime, Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(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 GetAllSalariesByEmployee_EmployeeIdIsNUllOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), string.Empty), Throws.TypeOf<ArgumentNullException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByEmployee_EmployeeIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), "employeeId"), Throws.TypeOf<ValidationException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByEmployee_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.GetAllSalariesByPeriodByEmployee(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 GetAllSalariesByEmployee_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.GetAllSalariesByPeriodByEmployee(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 employeeId = Guid.NewGuid().ToString();
var installationPrice = 200.0;
var installationSum = 200.0;
var postSalary = 2000.0;
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, installationPrice, installationSum, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, postSalary, true, DateTime.UtcNow));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
var expectedSum = postSalary + installationPrice * 0.1;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
installationSum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(installationSum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMounth_WithSeveralEmployees_Test()
{
//Arrange
var employee1Id = Guid.NewGuid().ToString();
var employee2Id = Guid.NewGuid().ToString();
var employee3Id = Guid.NewGuid().ToString();
var list = new List<EmployeeDataModel>() {
new(employee1Id, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
new(employee2Id, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false),
new(employee3Id, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)
};
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employee1Id, DateTime.UtcNow, 1, 0, false, []),
new InstallationDataModel(Guid.NewGuid().ToString(), employee1Id, DateTime.UtcNow, 1, 0, false, []),
new InstallationDataModel(Guid.NewGuid().ToString(), employee2Id, DateTime.UtcNow, 1, 0, false, []),
new InstallationDataModel(Guid.NewGuid().ToString(), employee3Id, DateTime.UtcNow, 1, 0, false, []),
new InstallationDataModel(Guid.NewGuid().ToString(), employee3Id, DateTime.UtcNow, 1, 0, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns(list);
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
_salaryStorageContract.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Exactly(list.Count));
}
[Test]
public void CalculateSalaryByMounth_WithoitInstallationsByEmployee_Test()
{
//Arrange
var postSalary = 2000.0;
var employeeId = Guid.NewGuid().ToString();
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, postSalary, true, DateTime.UtcNow));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
var expectedSum = postSalary;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
sum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMounth_InstallationStorageReturnNull_ThrowException_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_PostStorageReturnNull_ThrowException_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]);
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_EmployeeStorageReturnNull_ThrowException_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
}
[Test]
public void CalculateSalaryByMounth_InstallationStorageThrowException_ThrowException_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
[Test]
public void CalculateSalaryByMounth_PostStorageThrowException_ThrowException_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "email@mail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
[Test]
public void CalculateSalaryByMounth_EmployeeStorageThrowException_ThrowException_Test()
{
//Arrange
var employeeId = Guid.NewGuid().ToString();
_installationStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new InstallationDataModel(Guid.NewGuid().ToString(), employeeId, DateTime.UtcNow, 0, 0, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Designer, 2000, true, DateTime.UtcNow));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<StorageException>());
}
}

View File

@ -1,80 +0,0 @@
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
namespace TheCyclopsTests.DataModelsTests;
[TestFixture]
internal class ComponentDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var component = CreateDataModel(null, "name", ComponentType.Other, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(string.Empty, "name", ComponentType.Other, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var component = CreateDataModel("id", "name", ComponentType.Other, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductNameIsEmptyTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.Other, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, ComponentType.Other, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentTypeIsNoneTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), null, ComponentType.None, 10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, 0, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
component = CreateDataModel(Guid.NewGuid().ToString(), "name", ComponentType.Other, -10, false);
Assert.That(() => component.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var componentId = Guid.NewGuid().ToString();
var componentName = "name";
var componentType = ComponentType.Other;
var componentManufacturerId = Guid.NewGuid().ToString();
var componentPrice = 10;
var componentIsDelete = false;
var component = CreateDataModel(componentId, componentName, componentType, componentPrice, componentIsDelete);
Assert.That(() => component.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(component.Id, Is.EqualTo(componentId));
Assert.That(component.ComponentName, Is.EqualTo(componentName));
Assert.That(component.ComponentType, Is.EqualTo(componentType));
Assert.That(component.Price, Is.EqualTo(componentPrice));
Assert.That(component.IsDeleted, Is.EqualTo(componentIsDelete));
});
}
private static ComponentDataModel CreateDataModel(string? id, string? componentName,
ComponentType componentType, double price, bool isDeleted) =>
new(id, componentName, componentType, price, isDeleted);
}

View File

@ -1,130 +0,0 @@
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
namespace TheCyclopsTests.DataModelsTests;
[TestFixture]
internal class EmployeeDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var employee = CreateDataModel(null, "fio", "mail@mail.com", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
employee = CreateDataModel(null, "fio", "mail@mail.com", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var employee = CreateDataModel("id", "fio", "mail@mail.com", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void FIOIsNullOrEmptyTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), null, "mail@mail.com", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
employee = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "mail@mail.com", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsNullOrEmptyTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", null, Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty, Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmailIsIncorrectTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail", Guid.NewGuid().ToString(),
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNullOrEmptyTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", null,
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com", string.Empty,
DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostIdIsNotGuidTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com",
"postId", DateTime.Now.AddYears(-18), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateIsNotCorrectTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(1), DateTime.Now, false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void BirthDateAndEmploymentDateIsNotCorrectTest()
{
var employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-18).AddDays(-1), false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
employee = CreateDataModel(Guid.NewGuid().ToString(), "fio", "mail@mail.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18), DateTime.Now.AddYears(-16), false);
Assert.That(() => employee.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var employeeId = Guid.NewGuid().ToString();
var fio = "fio";
var email = "mail@mail.com";
var postId = Guid.NewGuid().ToString();
var birthDate = DateTime.Now.AddYears(-16).AddDays(-1);
var employmentDate = DateTime.Now;
var isDeleted = false;
var employee = CreateDataModel(employeeId, fio, email, postId, birthDate, employmentDate, isDeleted);
Assert.That(() => employee.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(employee.Id, Is.EqualTo(employeeId));
Assert.That(employee.FIO, Is.EqualTo(fio));
Assert.That(employee.Email, Is.EqualTo(email));
Assert.That(employee.PostId, Is.EqualTo(postId));
Assert.That(employee.BirthDate, Is.EqualTo(birthDate));
Assert.That(employee.EmploymentDate, Is.EqualTo(employmentDate));
Assert.That(employee.IsDeleted, Is.EqualTo(isDeleted));
});
}
private static EmployeeDataModel CreateDataModel(string? id, string? fio, string? email,
string? postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) =>
new(id, fio, email, postId, birthDate, employmentDate, isDeleted);
}

View File

@ -1,73 +0,0 @@
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
namespace TheCyclopsTests.DataModelsTests;
[TestFixture]
internal class InstallationComponentDataModelTests
{
[Test]
public void InstallationIdIsNullOrEmptyTest()
{
var installationComponent = CreateDataModel(null, Guid.NewGuid().ToString(), 10);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
installationComponent = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void InstallationIdIsNotGuidTest()
{
var installationComponent = CreateDataModel("installationId", Guid.NewGuid().ToString(), 10);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentIdIsNullOrEmptyTest()
{
var installationComponent = CreateDataModel(Guid.NewGuid().ToString(), null, 10);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
installationComponent = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 10);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ComponentIdIsNotGuidTest()
{
var installationComponent = CreateDataModel(Guid.NewGuid().ToString(), "componentId", 10);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void CountIsLessOrZeroTest()
{
var installationComponent = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
installationComponent = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
Assert.That(() => installationComponent.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var installationId = Guid.NewGuid().ToString();
var componentId = Guid.NewGuid().ToString();
var count = 10;
var installationComponent = CreateDataModel(installationId, componentId, count);
Assert.That(() => installationComponent.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(installationComponent.InstallationId, Is.EqualTo(installationId));
Assert.That(installationComponent.ComponentId, Is.EqualTo(componentId));
Assert.That(installationComponent.Count, Is.EqualTo(count));
});
}
private static InstallationComponentDataModel CreateDataModel(string? installationId, string? componentId, int count) =>
new(installationId, componentId, count);
}

View File

@ -1,116 +0,0 @@
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
namespace TheCyclopsTests.DataModelsTests;
[TestFixture]
internal class InstallationDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var installation = CreateDataModel(null, Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
installation = CreateDataModel(string.Empty, Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var installation = CreateDataModel("id", Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmployeeIdIsNullOrEmptyTest()
{
var installation = CreateDataModel(Guid.NewGuid().ToString(), null,
DateTime.UtcNow, 10, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
installation = CreateDataModel(Guid.NewGuid().ToString(), string.Empty,
DateTime.UtcNow, 10, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void EmployeeIdIsNotGuidTest()
{
var installation = CreateDataModel(Guid.NewGuid().ToString(), "employeeId",
DateTime.UtcNow, 10, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void InstallationPriceIsLessOrZeroTest()
{
var installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, 0, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, -10, 20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SumIsLessOrZeroTest()
{
var installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 0, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, -20, false, CreateSubDataModel());
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void ProductsIsNullOrEmptyTest()
{
var installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 20, false, null);
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
installation = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
DateTime.UtcNow, 10, 20, false, []);
Assert.That(() => installation.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var installationId = Guid.NewGuid().ToString();
var employeeId = Guid.NewGuid().ToString();
var installationPrice = 10;
var sum = 20;
var isCanceled = true;
var components = CreateSubDataModel();
var installation = CreateDataModel(installationId, employeeId,
DateTime.UtcNow, installationPrice, sum, isCanceled, components);
Assert.That(() => installation.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(installation.Id, Is.EqualTo(installationId));
Assert.That(installation.EmployeeId, Is.EqualTo(employeeId));
Assert.That(installation.Sum, Is.EqualTo(sum));
Assert.That(installation.IsCanceled, Is.EqualTo(isCanceled));
Assert.That(installation.Components, Is.EquivalentTo(components));
});
}
private static InstallationDataModel CreateDataModel(string id, string employeeId,
DateTime installationDate, double installationPrice, double sum, bool isCanceled,
List<InstallationComponentDataModel> components) =>
new(id, employeeId, installationDate, installationPrice, sum, isCanceled, components);
private static List<InstallationComponentDataModel> CreateSubDataModel()
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
}

View File

@ -1,88 +0,0 @@
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Enums;
using TheCyclopsContracts.Exceptions;
namespace TheCyclopsTests.DataModelsTests;
[TestFixture]
internal class PostDataModelTests
{
[Test]
public void IdIsNullOrEmptyTest()
{
var post = CreateDataModel(null, "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void IdIsNotGuidTest()
{
var post = CreateDataModel("id", "post",
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostNameIsEmptyTest()
{
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null,
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty,
PostType.Designer, 10, true, DateTime.UtcNow);
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PostTypeIsNoneTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "post",
PostType.None, 10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void SalaryIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "post",
PostType.Designer, 0, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "post",
PostType.Designer, -10, true, DateTime.UtcNow);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var postId = Guid.NewGuid().ToString();
var postName = "name";
var postType = PostType.Designer;
var salary = 10;
var isActual = false;
var changeDate = DateTime.UtcNow.AddDays(-1);
var post = CreateDataModel(postId, postName, postType, salary, isActual, changeDate);
Assert.That(() => post.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(post.Id, Is.EqualTo(postId));
Assert.That(post.PostName, Is.EqualTo(postName));
Assert.That(post.PostType, Is.EqualTo(postType));
Assert.That(post.Salary, Is.EqualTo(salary));
Assert.That(post.IsActual, Is.EqualTo(isActual));
Assert.That(post.ChangeDate, Is.EqualTo(changeDate));
});
}
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary, bool isActual, DateTime changeDate) =>
new(id, postName, postType, salary, isActual, changeDate);
}

View File

@ -1,56 +0,0 @@
using TheCyclopsContracts.DataModels;
using TheCyclopsContracts.Exceptions;
namespace TheCyclopsTests.DataModelsTests;
[TestFixture]
internal class SalaryDataModelTests
{
[Test]
public void WorkerIdIsEmptyTest()
{
var salary = CreateDataModel(null, DateTime.Now, 10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
salary = CreateDataModel(string.Empty, DateTime.Now, 10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void WorkerIdIsNotGuidTest()
{
var salary = CreateDataModel("employeeId", DateTime.Now, 10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void PriceIsLessOrZeroTest()
{
var salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, 0);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
salary = CreateDataModel(Guid.NewGuid().ToString(), DateTime.Now, -10);
Assert.That(() => salary.Validate(), Throws.TypeOf<ValidationException>());
}
[Test]
public void AllFieldsIsCorrectTest()
{
var employeeId = Guid.NewGuid().ToString();
var salaryDate = DateTime.Now.AddDays(-3).AddMinutes(-5);
var employeeSalary = 10;
var salary = CreateDataModel(employeeId, salaryDate, employeeSalary);
Assert.That(() => salary.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(salary.EmployeeId, Is.EqualTo(employeeId));
Assert.That(salary.SalaryDate, Is.EqualTo(salaryDate));
Assert.That(salary.Salary, Is.EqualTo(employeeSalary));
});
}
private static SalaryDataModel CreateDataModel(string? employeeId, DateTime salaryDate, double workerSalary) =>
new(employeeId, salaryDate, workerSalary);
}

View File

@ -1,29 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TheCyclopsBusinessLogic\TheCyclopsBusinessLogic.csproj" />
<ProjectReference Include="..\TheCyclopsContracts\TheCyclopsContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>