ISEbd-21_Savelyev.P.Y._PimpMyRide_Task2 #2
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PimpMyRide", "PimpMyRide\Pi
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PimpMyRideTest", "PimpMyRideTest\PimpMyRideTest.csproj", "{B9E83569-89AA-4293-BC57-84FD67CE584E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PimpMyRideBusinessLogic", "PimpMyRideBusinessLogic\PimpMyRideBusinessLogic.csproj", "{17562325-E1D5-4C8B-A124-4574699DD58C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -21,6 +23,10 @@ Global
|
||||
{B9E83569-89AA-4293-BC57-84FD67CE584E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B9E83569-89AA-4293-BC57-84FD67CE584E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B9E83569-89AA-4293-BC57-84FD67CE584E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{17562325-E1D5-4C8B-A124-4574699DD58C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{17562325-E1D5-4C8B-A124-4574699DD58C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{17562325-E1D5-4C8B-A124-4574699DD58C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{17562325-E1D5-4C8B-A124-4574699DD58C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using PimpMyRide.DataModels;
|
||||
|
||||
namespace PimpMyRide.BusinessLogicsContracts;
|
||||
|
||||
public interface ICarBusinessLogicContract
|
||||
{
|
||||
List<CarDataModel> GetAllCars();
|
||||
|
||||
List<CarDataModel> GetAllCarsByClient(string clientId);
|
||||
|
||||
List<CarDataModel> GetAllCarsByMake(string make, string model);
|
||||
|
||||
CarDataModel GetCarByData(string data);
|
||||
|
||||
void InsertCar(CarDataModel carDataModel);
|
||||
|
||||
void UpdateCar(CarDataModel carDataModel);
|
||||
|
||||
void DeleteCar(string id);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using PimpMyRide.DataModels;
|
||||
namespace PimpMyRide.BusinessLogicsContracts;
|
||||
|
||||
public interface IClientBusinessLogicContract
|
||||
{
|
||||
List<ClientDataModel> GetAllClients();
|
||||
|
||||
ClientDataModel GetClientByData(string data);
|
||||
|
||||
void InsertClient(ClientDataModel clientDataModel);
|
||||
|
||||
void UpdateClient(ClientDataModel clientDataModel);
|
||||
|
||||
void DeleteClient(string id);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
|
||||
namespace PimpMyRide.BusinessLogicsContracts;
|
||||
|
||||
public interface IMaterialBusinessLogicContract
|
||||
{
|
||||
List<MaterialDataModel> GetAllMaterials();
|
||||
|
||||
List<MaterialDataModel> GetAllMaterialsByType(MaterialType materialType);
|
||||
|
||||
List<MaterialHistoryDataModel> GetMaterialHistoryById(string materialId);
|
||||
|
||||
MaterialDataModel GetMaterialByData(string data);
|
||||
|
||||
void InsertMaterial(MaterialDataModel materialDataModel);
|
||||
|
||||
void UpdateMaterial(MaterialDataModel materialDataModel);
|
||||
|
||||
void DeleteMaterial(string id);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using PimpMyRide.DataModels;
|
||||
|
||||
namespace PimpMyRide.BusinessLogicsContracts;
|
||||
|
||||
public interface IOrderBusinessLogicContract
|
||||
{
|
||||
List<OrderDataModel> GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate);
|
||||
|
||||
List<OrderDataModel> GetAllOrdersByCarByPeriod(string carId, DateTime fromDate, DateTime toDate);
|
||||
|
||||
OrderDataModel GetOrderByData(string data);
|
||||
|
||||
void InsertOrder(OrderDataModel orderDataModel);
|
||||
|
||||
void CancelOrder(string id);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
|
||||
namespace PimpMyRide.BusinessLogicsContracts;
|
||||
|
||||
public interface IServiceBusinessLogicContract
|
||||
{
|
||||
List<ServiceDataModel> GetAllServices();
|
||||
|
||||
List<ServiceDataModel> GetAllServicesByWork(string workerId, WorkType workType);
|
||||
|
||||
ServiceDataModel GetServiceByData(string data);
|
||||
|
||||
void InsertService(ServiceDataModel serviceDataModel);
|
||||
|
||||
void CancelService(string id);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
|
||||
namespace PimpMyRide.BusinessLogicsContracts;
|
||||
|
||||
public interface IWorkerBusinessLogicContract
|
||||
{
|
||||
List<WorkerDataModel> GetAllWorkers(bool onlyActive = true);
|
||||
|
||||
List<WorkerDataModel> GetAllWorkersBySpeciality(SpecialityType specialityType, bool onlyActive = true);
|
||||
|
||||
List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromBirthDate, DateTime toBirthDate, bool onlyActive = true);
|
||||
|
||||
List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromEmploymentDate, DateTime toEmploymentDate, bool onlyActive = true);
|
||||
|
||||
WorkerDataModel GetWorkerByData(string data);
|
||||
|
||||
void InsertWorker(WorkerDataModel workerDataModel);
|
||||
|
||||
void UpdateWorker(WorkerDataModel workerDataModel);
|
||||
|
||||
void DeleteWorker(string id);
|
||||
}
|
||||
14
PimpMyRide/PimpMyRide/Exceptions/ElementExistsException.cs
Normal file
14
PimpMyRide/PimpMyRide/Exceptions/ElementExistsException.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace PimpMyRide.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;
|
||||
}
|
||||
}
|
||||
11
PimpMyRide/PimpMyRide/Exceptions/ElementNotFoundException.cs
Normal file
11
PimpMyRide/PimpMyRide/Exceptions/ElementNotFoundException.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace PimpMyRide.Exceptions;
|
||||
|
||||
public class ElementNotFoundException : Exception
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
|
||||
public ElementNotFoundException(string value) : base($"Element not found at value = {value}")
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace PimpMyRide.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}") { }
|
||||
}
|
||||
6
PimpMyRide/PimpMyRide/Exceptions/NullListException.cs
Normal file
6
PimpMyRide/PimpMyRide/Exceptions/NullListException.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PimpMyRide.Exceptions;
|
||||
|
||||
public class NullListException : Exception
|
||||
{
|
||||
public NullListException() : base("The returned list is null") { }
|
||||
}
|
||||
6
PimpMyRide/PimpMyRide/Exceptions/StorageException.cs
Normal file
6
PimpMyRide/PimpMyRide/Exceptions/StorageException.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PimpMyRide.Exceptions;
|
||||
|
||||
public class StorageException : Exception
|
||||
{
|
||||
public StorageException(Exception ex) : base($"Error while working in storage: {ex.Message}", ex) { }
|
||||
}
|
||||
9
PimpMyRide/PimpMyRide/Extensions/DateTimeExtensions.cs
Normal file
9
PimpMyRide/PimpMyRide/Extensions/DateTimeExtensions.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace PimpMyRide.Extensions;
|
||||
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
public static bool IsDateNotOlder(this DateTime date, DateTime olderDate)
|
||||
{
|
||||
return date >= olderDate;
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using PimpMyRide.DataModels;
|
||||
|
||||
namespace PimpMyRide.StoragesContracts;
|
||||
|
||||
public interface ICarStorageContract
|
||||
{
|
||||
List<CarDataModel> GetList(string? clientId = null, string? make = null, string? model = null);
|
||||
|
||||
CarDataModel? GetElementById(string id);
|
||||
|
||||
CarDataModel? GetElementByStateNumber(string stateNumber);
|
||||
|
||||
void AddElement(CarDataModel carDataModel);
|
||||
|
||||
void UpdElement(CarDataModel carDataModel);
|
||||
|
||||
void DelElement(string id);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using PimpMyRide.DataModels;
|
||||
|
||||
namespace PimpMyRide.StoragesContracts;
|
||||
|
||||
public interface IClientStorageContract
|
||||
{
|
||||
List<ClientDataModel> GetList();
|
||||
|
||||
ClientDataModel? GetElementById(string id);
|
||||
|
||||
ClientDataModel? GetElementByPhoneNumber(string phoneNumber);
|
||||
|
||||
ClientDataModel? GetElementByFIO(string fio);
|
||||
|
||||
void AddElement(ClientDataModel clientDataModel);
|
||||
|
||||
void UpdElement(ClientDataModel clientDataModel);
|
||||
|
||||
void DelElement(string id);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
|
||||
namespace PimpMyRide.StoragesContracts;
|
||||
|
||||
public interface IMaterialStorageContracts
|
||||
{
|
||||
List<MaterialDataModel> GetList(MaterialType? materialType = MaterialType.None);
|
||||
|
||||
List<MaterialHistoryDataModel> GetHistoryByMaterialId(string materialId);
|
||||
|
||||
MaterialDataModel? GetElementById(string id);
|
||||
|
||||
MaterialDataModel? GetElementByDescription(string description);
|
||||
|
||||
void AddElement(MaterialDataModel materialDataModel);
|
||||
|
||||
void UpdElement(MaterialDataModel materialDataModel);
|
||||
|
||||
void DelElement(string id);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using PimpMyRide.DataModels;
|
||||
|
||||
namespace PimpMyRide.StoragesContracts;
|
||||
|
||||
public interface IOrderStorageContract
|
||||
{
|
||||
List<OrderDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? carId = null);
|
||||
|
||||
OrderDataModel? GetElementById(string id);
|
||||
|
||||
void AddElement(OrderDataModel orderDataModel);
|
||||
|
||||
void DelElement(string id);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
|
||||
namespace PimpMyRide.StoragesContracts;
|
||||
|
||||
public interface IServiceStorageContracts
|
||||
{
|
||||
List<ServiceDataModel> GetList(string? workerId = null, WorkType? workType = WorkType.None);
|
||||
|
||||
ServiceDataModel? GetElementById(string id);
|
||||
|
||||
void AddElement(ServiceDataModel serviceDataModel);
|
||||
|
||||
void DelElement(string id);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
|
||||
namespace PimpMyRide.StoragesContracts;
|
||||
|
||||
public interface IWorkerStorageContract
|
||||
{
|
||||
List<WorkerDataModel> GetList(bool onlyActive = true, SpecialityType? specialityType = SpecialityType.None, DateTime? fromBirthDate = null, DateTime? toBirthDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
|
||||
|
||||
WorkerDataModel? GetElementById(string id);
|
||||
|
||||
WorkerDataModel? GetElementByFIO(string fio);
|
||||
|
||||
void AddElement(WorkerDataModel workerDataModel);
|
||||
|
||||
void UpdElement(WorkerDataModel workerDataModel);
|
||||
|
||||
void DelElement(string id);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PimpMyRideBusinessLogic.Implementations;
|
||||
|
||||
internal class CarBusinessLogicContract(ICarStorageContract carStorageContract, ILogger logger) : ICarBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly ICarStorageContract _carStorageContract = carStorageContract;
|
||||
|
||||
public List<CarDataModel> GetAllCars()
|
||||
{
|
||||
_logger.LogInformation("GetAllCars");
|
||||
return _carStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<CarDataModel> GetAllCarsByClient(string clientId)
|
||||
{
|
||||
_logger.LogInformation("GetAllCars params: {clientId}", clientId);
|
||||
if (clientId.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(clientId));
|
||||
}
|
||||
if (!clientId.IsGuid())
|
||||
{
|
||||
throw new ValidationException("The value in the field clientId is not a unique identifier.");
|
||||
}
|
||||
return _carStorageContract.GetList(clientId) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<CarDataModel> GetAllCarsByMake(string make, string model)
|
||||
{
|
||||
_logger.LogInformation("GetAllCars params: {make} {model}", make, model);
|
||||
if (make.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(make));
|
||||
}
|
||||
if (model.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
return _carStorageContract.GetList(make, model) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public CarDataModel GetCarByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (data.IsGuid())
|
||||
{
|
||||
return _carStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _carStorageContract.GetElementByStateNumber(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertCar(CarDataModel carDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(carDataModel));
|
||||
ArgumentNullException.ThrowIfNull(carDataModel);
|
||||
carDataModel.Validate();
|
||||
_carStorageContract.AddElement(carDataModel);
|
||||
}
|
||||
|
||||
public void UpdateCar(CarDataModel carDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(carDataModel));
|
||||
ArgumentNullException.ThrowIfNull(carDataModel);
|
||||
carDataModel.Validate();
|
||||
_carStorageContract.UpdElement(carDataModel);
|
||||
}
|
||||
|
||||
public void DeleteCar(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");
|
||||
}
|
||||
_carStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace PimpMyRideBusinessLogic.Implementations;
|
||||
|
||||
internal class ClientBusinessLogicContract(IClientStorageContract clientStorageContract, ILogger logger) : IClientBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IClientStorageContract _clientStorageContract = clientStorageContract;
|
||||
|
||||
public List<ClientDataModel> GetAllClients()
|
||||
{
|
||||
_logger.LogInformation("GetAllClients");
|
||||
return _clientStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public ClientDataModel GetClientByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (data.IsGuid())
|
||||
{
|
||||
return _clientStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
if (Regex.IsMatch(data, @"^((\+7|7|8)+([0-9]){10})$"))
|
||||
{
|
||||
return _clientStorageContract.GetElementByPhoneNumber(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _clientStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertClient(ClientDataModel clientDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(clientDataModel));
|
||||
ArgumentNullException.ThrowIfNull(clientDataModel);
|
||||
clientDataModel.Validate();
|
||||
_clientStorageContract.AddElement(clientDataModel);
|
||||
}
|
||||
|
||||
public void UpdateClient(ClientDataModel clientDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(clientDataModel));
|
||||
ArgumentNullException.ThrowIfNull(clientDataModel);
|
||||
clientDataModel.Validate();
|
||||
_clientStorageContract.UpdElement(clientDataModel);
|
||||
}
|
||||
|
||||
public void DeleteClient(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");
|
||||
}
|
||||
_clientStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PimpMyRideBusinessLogic.Implementations;
|
||||
|
||||
internal class MaterialBusinessLogicContract(IMaterialStorageContracts materialStorageContract, ILogger logger) : IMaterialBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IMaterialStorageContracts _materialStorageContract = materialStorageContract;
|
||||
|
||||
public List<MaterialDataModel> GetAllMaterials()
|
||||
{
|
||||
_logger.LogInformation("GetAllMaterials");
|
||||
return _materialStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<MaterialDataModel> GetAllMaterialsByType(MaterialType materialType)
|
||||
{
|
||||
_logger.LogInformation("GetAllMaterials params: {materialType}", materialType);
|
||||
if (materialType == MaterialType.None)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(materialType));
|
||||
}
|
||||
return _materialStorageContract.GetList(materialType) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public MaterialDataModel GetMaterialByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (data.IsGuid())
|
||||
{
|
||||
return _materialStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _materialStorageContract.GetElementByDescription(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public List<MaterialHistoryDataModel> GetMaterialHistoryById(string materialId)
|
||||
{
|
||||
_logger.LogInformation("GetMaterialHistoryById for {materialId}", materialId);
|
||||
if (materialId.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(materialId));
|
||||
}
|
||||
if (!materialId.IsGuid())
|
||||
{
|
||||
throw new ValidationException("The value in the field materialId is not a unique identifier.");
|
||||
}
|
||||
return _materialStorageContract.GetHistoryByMaterialId(materialId) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public void InsertMaterial(MaterialDataModel materialDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(materialDataModel));
|
||||
ArgumentNullException.ThrowIfNull(materialDataModel);
|
||||
materialDataModel.Validate();
|
||||
_materialStorageContract.AddElement(materialDataModel);
|
||||
}
|
||||
|
||||
public void UpdateMaterial(MaterialDataModel materialDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(materialDataModel));
|
||||
ArgumentNullException.ThrowIfNull(materialDataModel);
|
||||
materialDataModel.Validate();
|
||||
_materialStorageContract.UpdElement(materialDataModel);
|
||||
}
|
||||
|
||||
public void DeleteMaterial(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");
|
||||
}
|
||||
_materialStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PimpMyRideBusinessLogic.Implementations;
|
||||
|
||||
internal class OrderBusinessLogicContract(IOrderStorageContract orderStorageContract, ILogger logger) : IOrderBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IOrderStorageContract _orderStorageContract = orderStorageContract;
|
||||
|
||||
public List<OrderDataModel> GetAllOrdersByCarByPeriod(string carId, DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
_logger.LogInformation("GetAllOrders params: {carId}, {fromDate}, {toDate}", carId, fromDate, toDate);
|
||||
if (fromDate.IsDateNotOlder(toDate))
|
||||
{
|
||||
throw new IncorrectDatesException(fromDate, toDate);
|
||||
}
|
||||
if (carId.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(carId));
|
||||
}
|
||||
if (!carId.IsGuid())
|
||||
{
|
||||
throw new ValidationException("The value in the field carId is not a unique identifier.");
|
||||
}
|
||||
return _orderStorageContract.GetList(fromDate, toDate, carId: carId) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<OrderDataModel> GetAllOrdersByPeriod(DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate);
|
||||
if (fromDate.IsDateNotOlder(toDate))
|
||||
{
|
||||
throw new IncorrectDatesException(fromDate, toDate);
|
||||
}
|
||||
return _orderStorageContract.GetList(fromDate, toDate) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public OrderDataModel GetOrderByData(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 _orderStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertOrder(OrderDataModel orderDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(orderDataModel));
|
||||
ArgumentNullException.ThrowIfNull(orderDataModel);
|
||||
orderDataModel.Validate();
|
||||
_orderStorageContract.AddElement(orderDataModel);
|
||||
}
|
||||
|
||||
public void CancelOrder(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");
|
||||
}
|
||||
_orderStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PimpMyRideBusinessLogic.Implementations;
|
||||
|
||||
internal class ServiceBusinessLogicContract(IServiceStorageContracts serviceStorageContracts, ILogger logger) : IServiceBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IServiceStorageContracts _serviceStorageContract = serviceStorageContracts;
|
||||
|
||||
public List<ServiceDataModel> GetAllServices()
|
||||
{
|
||||
_logger.LogInformation("GetAllServices");
|
||||
return _serviceStorageContract.GetList() ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<ServiceDataModel> GetAllServicesByWork(string workerId, WorkType workType)
|
||||
{
|
||||
_logger.LogInformation("GetAllCars params: {workerId}, {workType}", workerId, workType);
|
||||
if (workerId.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(workerId));
|
||||
}
|
||||
if (!workerId.IsGuid())
|
||||
{
|
||||
throw new ValidationException("The value in the field workerId is not an unique identifier.");
|
||||
}
|
||||
if (workType == WorkType.None)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(workType));
|
||||
}
|
||||
return _serviceStorageContract.GetList(workerId, workType) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public ServiceDataModel GetServiceByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (data.IsGuid())
|
||||
{
|
||||
return _serviceStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
throw new ValidationException("The value in the field data is not an unique identifier.");
|
||||
}
|
||||
|
||||
public void InsertService(ServiceDataModel serviceDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(serviceDataModel));
|
||||
ArgumentNullException.ThrowIfNull(serviceDataModel);
|
||||
serviceDataModel.Validate();
|
||||
_serviceStorageContract.AddElement(serviceDataModel);
|
||||
}
|
||||
|
||||
public void CancelService(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");
|
||||
}
|
||||
_serviceStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
using System.Text.Json;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace PimpMyRideBusinessLogic.Implementations;
|
||||
|
||||
internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageContract, ILogger logger) : IWorkerBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
|
||||
|
||||
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
|
||||
{
|
||||
_logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive);
|
||||
return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
|
||||
{
|
||||
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
|
||||
if (fromDate.IsDateNotOlder(toDate))
|
||||
{
|
||||
throw new IncorrectDatesException(fromDate, toDate);
|
||||
}
|
||||
return _workerStorageContract.GetList(onlyActive, fromBirthDate: fromDate, toBirthDate: toDate) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<WorkerDataModel> GetAllWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
|
||||
{
|
||||
_logger.LogInformation("GetAllWorkers params: {onlyActive}, {fromDate}, {toDate}", onlyActive, fromDate, toDate);
|
||||
if (fromDate.IsDateNotOlder(toDate))
|
||||
{
|
||||
throw new IncorrectDatesException(fromDate, toDate);
|
||||
}
|
||||
return _workerStorageContract.GetList(onlyActive, fromEmploymentDate: fromDate, toEmploymentDate: toDate) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public List<WorkerDataModel> GetAllWorkersBySpeciality(SpecialityType specialityType, bool onlyActive = true)
|
||||
{
|
||||
_logger.LogInformation("GetAllWorkers params: {specialityType}, {onlyActive},", specialityType, onlyActive);
|
||||
if (specialityType == SpecialityType.None)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(specialityType));
|
||||
}
|
||||
return _workerStorageContract.GetList(onlyActive, specialityType) ?? throw new NullListException();
|
||||
}
|
||||
|
||||
public WorkerDataModel GetWorkerByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (data.IsGuid())
|
||||
{
|
||||
return _workerStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _workerStorageContract.GetElementByFIO(data) ?? throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertWorker(WorkerDataModel workerDataModel)
|
||||
{
|
||||
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(workerDataModel));
|
||||
ArgumentNullException.ThrowIfNull(workerDataModel);
|
||||
workerDataModel.Validate();
|
||||
_workerStorageContract.AddElement(workerDataModel);
|
||||
}
|
||||
|
||||
public void UpdateWorker(WorkerDataModel workerDataModel)
|
||||
{
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(workerDataModel));
|
||||
ArgumentNullException.ThrowIfNull(workerDataModel);
|
||||
workerDataModel.Validate();
|
||||
_workerStorageContract.UpdElement(workerDataModel);
|
||||
}
|
||||
|
||||
public void DeleteWorker(string id)
|
||||
{
|
||||
_logger.LogInformation("Delete by id: {id}", id);
|
||||
if (id.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
if (!id.IsGuid())
|
||||
{
|
||||
throw new ValidationException("Id is not a unique identifier");
|
||||
}
|
||||
_workerStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="PimpMyRideTest" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.4" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PimpMyRide\PimpMyRide.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,420 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PimpMyRideBusinessLogic.Implementations;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
|
||||
|
||||
namespace PimpMyRideTest.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class CarBusinessLogicContractTests
|
||||
{
|
||||
private ICarBusinessLogicContract _carBusinessLogicContract;
|
||||
private Mock<ICarStorageContract> _carStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_carStorageContract = new Mock<ICarStorageContract>();
|
||||
_carBusinessLogicContract = new CarBusinessLogicContract(_carStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_carStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCars_ReturnListOfRecords_Test()
|
||||
{
|
||||
var listOriginal = new List<CarDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP"),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Lada", "21123", "O112OO"),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Toyota", "Tundra", "O123AO"),
|
||||
};
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
|
||||
var list = _carBusinessLogicContract.GetAllCars();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_carStorageContract.Verify(x => x.GetList(null, null, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCars_ReturnEmptyList_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns([]);
|
||||
|
||||
var list = _carBusinessLogicContract.GetAllCars();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCars_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCars(), Throws.TypeOf<NullListException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCars_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCars(), Throws.TypeOf<StorageException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByClient_ReturnListOfRecords_Test()
|
||||
{
|
||||
var clientId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<CarDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP"),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Lada", "21123", "O112OO"),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Toyota", "Tundra", "O123AO"),
|
||||
};
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>())).Returns(listOriginal);
|
||||
|
||||
var list = _carBusinessLogicContract.GetAllCarsByClient(clientId);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
|
||||
_carStorageContract.Verify(x => x.GetList(clientId, null, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByClient_ReturnEmptyList_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>())).Returns([]);
|
||||
|
||||
var list = _carBusinessLogicContract.GetAllCarsByClient(Guid.NewGuid().ToString());
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByClient_ClientIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByClient(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByClient(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByClient_ClientIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByClient("clientId"), Throws.TypeOf<ValidationException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByClient_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByClient(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByClient_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByClient(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByMake_ReturnListOfRecords_Test()
|
||||
{
|
||||
var make = "Porsche";
|
||||
var model = "911";
|
||||
var listOriginal = new List<CarDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP"),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Lada", "21123", "O112OO"),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Toyota", "Tundra", "O123AO"),
|
||||
};
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>())).Returns(listOriginal);
|
||||
|
||||
var list = _carBusinessLogicContract.GetAllCarsByMake(make, model);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
|
||||
_carStorageContract.Verify(x => x.GetList(make, model, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByMake_ReturnEmptyList_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>())).Returns([]);
|
||||
|
||||
var list = _carBusinessLogicContract.GetAllCarsByMake("Porsche", "911");
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByMake_MakeIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByMake(null, "911"), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByMake(string.Empty, "911"), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByMake("Porche", null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByMake("Porche", string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByMake_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByMake("Porsche", "911"), Throws.TypeOf<NullListException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllCarsByMake_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.GetAllCarsByMake("Porsche", "911"), Throws.TypeOf<StorageException>());
|
||||
_carStorageContract.Verify(x => x.GetList(It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCarByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new CarDataModel(id, Guid.NewGuid().ToString(), "Porsche", "911", "A911MP");
|
||||
_carStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
|
||||
var element = _carBusinessLogicContract.GetCarByData(id);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_carStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCarByData_GetByStateNumber_ReturnRecord_Test()
|
||||
{
|
||||
var stateNumber = "A911MP";
|
||||
var record = new CarDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", stateNumber);
|
||||
_carStorageContract.Setup(x => x.GetElementByStateNumber(stateNumber)).Returns(record);
|
||||
|
||||
var element = _carBusinessLogicContract.GetCarByData(stateNumber);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.StateNumber, Is.EqualTo(stateNumber));
|
||||
_carStorageContract.Verify(x => x.GetElementByStateNumber(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCarByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetCarByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _carBusinessLogicContract.GetCarByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_carStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_carStorageContract.Verify(x => x.GetElementByStateNumber(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCarByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetCarByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_carStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_carStorageContract.Verify(x => x.GetElementByStateNumber(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCarByData_GetByStateNumber_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.GetCarByData("A911MP"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_carStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_carStorageContract.Verify(x => x.GetElementByStateNumber(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCarByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_carStorageContract.Setup(x => x.GetElementByStateNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.GetCarByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _carBusinessLogicContract.GetCarByData("A911MP"), Throws.TypeOf<StorageException>());
|
||||
_carStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_carStorageContract.Verify(x => x.GetElementByStateNumber(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertCar_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new CarDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP");
|
||||
_carStorageContract.Setup(x => x.AddElement(It.IsAny<CarDataModel>()))
|
||||
.Callback((CarDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.ClientId == record.ClientId &&
|
||||
x.Make == record.Make && x.Model == record.Model && x.StateNumber == record.StateNumber;
|
||||
});
|
||||
|
||||
_carBusinessLogicContract.InsertCar(record);
|
||||
|
||||
_carStorageContract.Verify(x => x.AddElement(It.IsAny<CarDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertCar_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.AddElement(It.IsAny<CarDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.InsertCar(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP")), Throws.TypeOf<ElementExistsException>());
|
||||
_carStorageContract.Verify(x => x.AddElement(It.IsAny<CarDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertCar_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.InsertCar(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_carStorageContract.Verify(x => x.AddElement(It.IsAny<CarDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertCar_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.InsertCar(new CarDataModel("id", Guid.NewGuid().ToString(), "Porsche", "911", "A911MP")), Throws.TypeOf<ValidationException>());
|
||||
_carStorageContract.Verify(x => x.AddElement(It.IsAny<CarDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertCar_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.AddElement(It.IsAny<CarDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.InsertCar(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP")), Throws.TypeOf<StorageException>());
|
||||
_carStorageContract.Verify(x => x.AddElement(It.IsAny<CarDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateCar_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new CarDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP");
|
||||
_carStorageContract.Setup(x => x.UpdElement(It.IsAny<CarDataModel>()))
|
||||
.Callback((CarDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.ClientId == record.ClientId &&
|
||||
x.Make == record.Make && x.Model == record.Model && x.StateNumber == record.StateNumber;
|
||||
});
|
||||
|
||||
_carBusinessLogicContract.UpdateCar(record);
|
||||
|
||||
_carStorageContract.Verify(x => x.UpdElement(It.IsAny<CarDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateCar_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.UpdElement(It.IsAny<CarDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.UpdateCar(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP")), Throws.TypeOf<ElementNotFoundException>());
|
||||
_carStorageContract.Verify(x => x.UpdElement(It.IsAny<CarDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateCar_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.UpdElement(It.IsAny<CarDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.UpdateCar(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP")), Throws.TypeOf<ElementExistsException>());
|
||||
_carStorageContract.Verify(x => x.UpdElement(It.IsAny<CarDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateCar_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.UpdateCar(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_carStorageContract.Verify(x => x.UpdElement(It.IsAny<CarDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateCar_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.UpdateCar(new CarDataModel("id", Guid.NewGuid().ToString(), "Porsche", "911", "A911MP")), Throws.TypeOf<ValidationException>());
|
||||
_carStorageContract.Verify(x => x.UpdElement(It.IsAny<CarDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateCar_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.UpdElement(It.IsAny<CarDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.UpdateCar(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", "A911MP")), Throws.TypeOf<StorageException>());
|
||||
_carStorageContract.Verify(x => x.UpdElement(It.IsAny<CarDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteCar_CorrectRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_carStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
|
||||
_carBusinessLogicContract.DeleteCar(id);
|
||||
|
||||
_carStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteCar_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.DeleteCar(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_carStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteCar_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.DeleteCar(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _carBusinessLogicContract.DeleteCar(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_carStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteCar_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _carBusinessLogicContract.DeleteCar("id"), Throws.TypeOf<ValidationException>());
|
||||
_carStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteCar_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_carStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _carBusinessLogicContract.DeleteCar(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_carStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PimpMyRideBusinessLogic.Implementations;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
|
||||
namespace PimpMyRideTest.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ClientBusinessLogicContractTests
|
||||
{
|
||||
private ClientBusinessLogicContract _clientBusinessLogicContract;
|
||||
private Mock<IClientStorageContract> _clientStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_clientStorageContract = new Mock<IClientStorageContract>();
|
||||
_clientBusinessLogicContract = new ClientBusinessLogicContract(_clientStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_clientStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllClients_ReturnListOfRecords_Test()
|
||||
{
|
||||
var listOriginal = new List<ClientDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1", "88005553535"),
|
||||
new(Guid.NewGuid().ToString(), "fio 2", "88888888888"),
|
||||
new(Guid.NewGuid().ToString(), "fio 3", "89054863548"),
|
||||
};
|
||||
_clientStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
|
||||
|
||||
var list = _clientBusinessLogicContract.GetAllClients();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllClients_ReturnEmptyList_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.GetList()).Returns([]);
|
||||
|
||||
var list = _clientBusinessLogicContract.GetAllClients();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_clientStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllClients_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.GetAllClients(), Throws.TypeOf<NullListException>());
|
||||
_clientStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllClients_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.GetAllClients(), Throws.TypeOf<StorageException>());
|
||||
_clientStorageContract.Verify(x => x.GetList(), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new ClientDataModel(id, "fio", "81111111111");
|
||||
_clientStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
|
||||
var element = _clientBusinessLogicContract.GetClientByData(id);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_GetByFio_ReturnRecord_Test()
|
||||
{
|
||||
var fio = "fio";
|
||||
var record = new ClientDataModel(Guid.NewGuid().ToString(), fio, "81111111111");
|
||||
_clientStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
|
||||
|
||||
var element = _clientBusinessLogicContract.GetClientByData(fio);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.FIO, Is.EqualTo(fio));
|
||||
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_GetByPhoneNumber_ReturnRecord_Test()
|
||||
{
|
||||
var phoneNumber = "81111111111";
|
||||
var record = new ClientDataModel(Guid.NewGuid().ToString(), "fio", phoneNumber);
|
||||
_clientStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record);
|
||||
|
||||
var element = _clientBusinessLogicContract.GetClientByData(phoneNumber);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber));
|
||||
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_GetByFio_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData("fio"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData("81111111112"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetClientByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_clientStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_clientStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData("fio"), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _clientBusinessLogicContract.GetClientByData("81111111112"), Throws.TypeOf<StorageException>());
|
||||
_clientStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_clientStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||
_clientStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertClient_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new ClientDataModel(Guid.NewGuid().ToString(), "fio", "81111111112");
|
||||
_clientStorageContract.Setup(x => x.AddElement(It.IsAny<ClientDataModel>()))
|
||||
.Callback((ClientDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.FIO == record.FIO &&
|
||||
x.PhoneNumber == record.PhoneNumber;
|
||||
});
|
||||
|
||||
_clientBusinessLogicContract.InsertClient(record);
|
||||
|
||||
_clientStorageContract.Verify(x => x.AddElement(It.IsAny<ClientDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertClient_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.AddElement(It.IsAny<ClientDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.InsertClient(new(Guid.NewGuid().ToString(), "fio", "81111111112")), Throws.TypeOf<ElementExistsException>());
|
||||
_clientStorageContract.Verify(x => x.AddElement(It.IsAny<ClientDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertClient_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.InsertClient(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_clientStorageContract.Verify(x => x.AddElement(It.IsAny<ClientDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertClient_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.InsertClient(new ClientDataModel("id", "fio", "81111111112")), Throws.TypeOf<ValidationException>());
|
||||
_clientStorageContract.Verify(x => x.AddElement(It.IsAny<ClientDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertClient_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.AddElement(It.IsAny<ClientDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.InsertClient(new(Guid.NewGuid().ToString(), "fio", "81111111112")), Throws.TypeOf<StorageException>());
|
||||
_clientStorageContract.Verify(x => x.AddElement(It.IsAny<ClientDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateClient_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new ClientDataModel(Guid.NewGuid().ToString(), "fio", "81111111112");
|
||||
_clientStorageContract.Setup(x => x.UpdElement(It.IsAny<ClientDataModel>()))
|
||||
.Callback((ClientDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.FIO == record.FIO &&
|
||||
x.PhoneNumber == record.PhoneNumber;
|
||||
});
|
||||
|
||||
_clientBusinessLogicContract.UpdateClient(record);
|
||||
|
||||
_clientStorageContract.Verify(x => x.UpdElement(It.IsAny<ClientDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateClient_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.UpdElement(It.IsAny<ClientDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.UpdateClient(new(Guid.NewGuid().ToString(), "fio", "81111111112")), Throws.TypeOf<ElementNotFoundException>());
|
||||
_clientStorageContract.Verify(x => x.UpdElement(It.IsAny<ClientDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateClient_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.UpdElement(It.IsAny<ClientDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.UpdateClient(new(Guid.NewGuid().ToString(), "fio", "81111111112")), Throws.TypeOf<ElementExistsException>());
|
||||
_clientStorageContract.Verify(x => x.UpdElement(It.IsAny<ClientDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateClient_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.UpdateClient(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_clientStorageContract.Verify(x => x.UpdElement(It.IsAny<ClientDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateClient_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.UpdateClient(new ClientDataModel("id", "fio", "81111111112")), Throws.TypeOf<ValidationException>());
|
||||
_clientStorageContract.Verify(x => x.UpdElement(It.IsAny<ClientDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateClient_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.UpdElement(It.IsAny<ClientDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.UpdateClient(new(Guid.NewGuid().ToString(), "fio", "81111111112")), Throws.TypeOf<StorageException>());
|
||||
_clientStorageContract.Verify(x => x.UpdElement(It.IsAny<ClientDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteClient_CorrectRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_clientStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
|
||||
_clientBusinessLogicContract.DeleteClient(id);
|
||||
|
||||
_clientStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteClient_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.DeleteClient(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_clientStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteClient_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.DeleteClient(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _clientBusinessLogicContract.DeleteClient(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_clientStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteClient_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _clientBusinessLogicContract.DeleteClient("id"), Throws.TypeOf<ValidationException>());
|
||||
_clientStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteClient_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_clientStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _clientBusinessLogicContract.DeleteClient(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_clientStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,414 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PimpMyRideBusinessLogic.Implementations;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
|
||||
namespace PimpMyRideTest.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class MaterialBusinessLogicContractTests
|
||||
{
|
||||
private MaterialBusinessLogicContract _materialBusinessLogicContract;
|
||||
private Mock<IMaterialStorageContracts> _materialStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_materialStorageContract = new Mock<IMaterialStorageContracts>();
|
||||
_materialBusinessLogicContract = new MaterialBusinessLogicContract(_materialStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_materialStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterials_ReturnListOfRecords_Test()
|
||||
{
|
||||
var listOriginal = new List<MaterialDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10),
|
||||
new(Guid.NewGuid().ToString(), MaterialType.BodyParts, "mirror", 15),
|
||||
new(Guid.NewGuid().ToString(), MaterialType.Wheels, "screw", 20),
|
||||
};
|
||||
_materialStorageContract.Setup(x => x.GetList(It.IsAny<MaterialType>())).Returns(listOriginal);
|
||||
|
||||
var list = _materialBusinessLogicContract.GetAllMaterials();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_materialStorageContract.Verify(x => x.GetList(MaterialType.None), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterials_ReturnEmptyList_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.GetList(It.IsAny<MaterialType>())).Returns([]);
|
||||
|
||||
var list = _materialBusinessLogicContract.GetAllMaterials();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_materialStorageContract.Verify(x => x.GetList(It.IsAny<MaterialType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterials_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetAllMaterials(), Throws.TypeOf<NullListException>());
|
||||
_materialStorageContract.Verify(x => x.GetList(It.IsAny<MaterialType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterials_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.GetList(It.IsAny<MaterialType>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.GetAllMaterials(), Throws.TypeOf<StorageException>());
|
||||
_materialStorageContract.Verify(x => x.GetList(It.IsAny<MaterialType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterialsByType_ReturnListOfRecords_Test()
|
||||
{
|
||||
var type = MaterialType.EngineParts;
|
||||
var listOriginal = new List<MaterialDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10),
|
||||
new(Guid.NewGuid().ToString(), MaterialType.BodyParts, "mirror", 15),
|
||||
new(Guid.NewGuid().ToString(), MaterialType.Wheels, "screw", 20),
|
||||
};
|
||||
_materialStorageContract.Setup(x => x.GetList(It.IsAny<MaterialType>())).Returns(listOriginal);
|
||||
|
||||
var list = _materialBusinessLogicContract.GetAllMaterialsByType(type);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
|
||||
_materialStorageContract.Verify(x => x.GetList(type), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterialsByType_ReturnEmptyList_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.GetList(It.IsAny<MaterialType>())).Returns([]);
|
||||
|
||||
var list = _materialBusinessLogicContract.GetAllMaterialsByType(MaterialType.EngineParts);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
|
||||
_materialStorageContract.Verify(x => x.GetList(It.IsAny<MaterialType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterialsByType_TypeIsNone_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetAllMaterialsByType(MaterialType.None), Throws.TypeOf<ArgumentNullException>());
|
||||
_materialStorageContract.Verify(x => x.GetList(It.IsAny<MaterialType>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterialsByType_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetAllMaterialsByType(MaterialType.EngineParts), Throws.TypeOf<NullListException>());
|
||||
_materialStorageContract.Verify(x => x.GetList(It.IsAny<MaterialType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllMaterialsByType_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.GetList(It.IsAny<MaterialType>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.GetAllMaterialsByType(MaterialType.EngineParts), Throws.TypeOf<StorageException>());
|
||||
_materialStorageContract.Verify(x => x.GetList(It.IsAny<MaterialType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new MaterialDataModel(id, MaterialType.EngineParts, "ring", 10);
|
||||
_materialStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
|
||||
var element = _materialBusinessLogicContract.GetMaterialByData(id);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_materialStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialByData_GetByDescription_ReturnRecord_Test()
|
||||
{
|
||||
var description = "ring";
|
||||
var record = new MaterialDataModel(Guid.NewGuid().ToString(), MaterialType.EngineParts, description, 10);
|
||||
_materialStorageContract.Setup(x => x.GetElementByDescription(description)).Returns(record);
|
||||
|
||||
var element = _materialBusinessLogicContract.GetMaterialByData(description);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Description, Is.EqualTo(description));
|
||||
_materialStorageContract.Verify(x => x.GetElementByDescription(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_materialStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_materialStorageContract.Verify(x => x.GetElementByDescription(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_materialStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_materialStorageContract.Verify(x => x.GetElementByDescription(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialByData_GetByDescription_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialByData("ring"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_materialStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_materialStorageContract.Verify(x => x.GetElementByDescription(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_materialStorageContract.Setup(x => x.GetElementByDescription(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialByData("ring"), Throws.TypeOf<StorageException>());
|
||||
_materialStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_materialStorageContract.Verify(x => x.GetElementByDescription(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialHistoryById_ReturnListOfRecords_Test()
|
||||
{
|
||||
var materialId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<MaterialHistoryDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), 10),
|
||||
new(Guid.NewGuid().ToString(), 15),
|
||||
new(Guid.NewGuid().ToString(), 10),
|
||||
};
|
||||
_materialStorageContract.Setup(x => x.GetHistoryByMaterialId(It.IsAny<string>())).Returns(listOriginal);
|
||||
|
||||
var list = _materialBusinessLogicContract.GetMaterialHistoryById(materialId);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_materialStorageContract.Verify(x => x.GetHistoryByMaterialId(materialId), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialHistoryById_ReturnEmptyList_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.GetHistoryByMaterialId(It.IsAny<string>())).Returns([]);
|
||||
|
||||
var list = _materialBusinessLogicContract.GetMaterialHistoryById(Guid.NewGuid().ToString());
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_materialStorageContract.Verify(x => x.GetHistoryByMaterialId(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialHistoryById_MaterialIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialHistoryById(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialHistoryById(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_materialStorageContract.Verify(x => x.GetHistoryByMaterialId(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialHistoryById_MaterialIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialHistoryById("materialId"), Throws.TypeOf<ValidationException>());
|
||||
_materialStorageContract.Verify(x => x.GetHistoryByMaterialId(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialHistoryById_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialHistoryById(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||
_materialStorageContract.Verify(x => x.GetHistoryByMaterialId(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaterialHistoryById_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.GetHistoryByMaterialId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.GetMaterialHistoryById(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_materialStorageContract.Verify(x => x.GetHistoryByMaterialId(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertMaterial_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new MaterialDataModel(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10);
|
||||
_materialStorageContract.Setup(x => x.AddElement(It.IsAny<MaterialDataModel>()))
|
||||
.Callback((MaterialDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.MaterialType == record.MaterialType &&
|
||||
x.Description == record.Description && x.UnitCost == record.UnitCost;
|
||||
});
|
||||
|
||||
_materialBusinessLogicContract.InsertMaterial(record);
|
||||
|
||||
_materialStorageContract.Verify(x => x.AddElement(It.IsAny<MaterialDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertMaterial_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.AddElement(It.IsAny<MaterialDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.InsertMaterial(new(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10)), Throws.TypeOf<ElementExistsException>());
|
||||
_materialStorageContract.Verify(x => x.AddElement(It.IsAny<MaterialDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertMaterial_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.InsertMaterial(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_materialStorageContract.Verify(x => x.AddElement(It.IsAny<MaterialDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertMaterial_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.InsertMaterial(new MaterialDataModel("id", MaterialType.EngineParts, "ring", 10)), Throws.TypeOf<ValidationException>());
|
||||
_materialStorageContract.Verify(x => x.AddElement(It.IsAny<MaterialDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertMaterial_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.AddElement(It.IsAny<MaterialDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.InsertMaterial(new(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10)), Throws.TypeOf<StorageException>());
|
||||
_materialStorageContract.Verify(x => x.AddElement(It.IsAny<MaterialDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateMaterial_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new MaterialDataModel(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10);
|
||||
_materialStorageContract.Setup(x => x.UpdElement(It.IsAny<MaterialDataModel>()))
|
||||
.Callback((MaterialDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.MaterialType == record.MaterialType &&
|
||||
x.Description == record.Description && x.UnitCost == record.UnitCost;
|
||||
});
|
||||
|
||||
_materialBusinessLogicContract.UpdateMaterial(record);
|
||||
|
||||
_materialStorageContract.Verify(x => x.UpdElement(It.IsAny<MaterialDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateMaterial_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.UpdElement(It.IsAny<MaterialDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.UpdateMaterial(new(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10)), Throws.TypeOf<ElementNotFoundException>());
|
||||
_materialStorageContract.Verify(x => x.UpdElement(It.IsAny<MaterialDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateMaterial_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.UpdElement(It.IsAny<MaterialDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.UpdateMaterial(new(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10)), Throws.TypeOf<ElementExistsException>());
|
||||
_materialStorageContract.Verify(x => x.UpdElement(It.IsAny<MaterialDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateMaterial_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.UpdateMaterial(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_materialStorageContract.Verify(x => x.UpdElement(It.IsAny<MaterialDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateMaterial_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.UpdateMaterial(new MaterialDataModel("id", MaterialType.EngineParts, "ring", 10)), Throws.TypeOf<ValidationException>());
|
||||
_materialStorageContract.Verify(x => x.UpdElement(It.IsAny<MaterialDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateMaterial_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.UpdElement(It.IsAny<MaterialDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.UpdateMaterial(new(Guid.NewGuid().ToString(), MaterialType.EngineParts, "ring", 10)), Throws.TypeOf<StorageException>());
|
||||
_materialStorageContract.Verify(x => x.UpdElement(It.IsAny<MaterialDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMaterial_CorrectRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_materialStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
|
||||
_materialBusinessLogicContract.DeleteMaterial(id);
|
||||
|
||||
_materialStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMaterial_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.DeleteMaterial(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_materialStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMaterial_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.DeleteMaterial(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _materialBusinessLogicContract.DeleteMaterial(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_materialStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMaterial_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _materialBusinessLogicContract.DeleteMaterial("id"), Throws.TypeOf<ValidationException>());
|
||||
_materialStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMaterial_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_materialStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _materialBusinessLogicContract.DeleteMaterial(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_materialStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PimpMyRideBusinessLogic.Implementations;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
|
||||
namespace PimpMyRideTest.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class OrderBusinessLogicContractTests
|
||||
{
|
||||
private OrderBusinessLogicContract _orderBusinessLogicContract;
|
||||
private Mock<IOrderStorageContract> _orderStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_orderStorageContract = new Mock<IOrderStorageContract>();
|
||||
_orderBusinessLogicContract = new OrderBusinessLogicContract(_orderStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_orderStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_ReturnListOfRecords_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
var listOriginal = new List<OrderDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false,
|
||||
[new OrderServiceDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 15, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []),
|
||||
};
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByPeriod(date, date.AddDays(1));
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_ReturnEmptyList_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns([]);
|
||||
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByPeriod(date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByCarByPeriod_ReturnListOfRecords_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
var carId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<OrderDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false,
|
||||
[new OrderServiceDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 15, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []),
|
||||
};
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(carId, date, date.AddDays(1));
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), carId), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByCarByPeriod_ReturnEmptyList_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Returns([]);
|
||||
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByCarByPeriod_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByCarByPeriod_CarIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByCarByPeriod_CarIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByCarByPeriod("carId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByCarByPeriod_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByCarByPeriod_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.GetAllOrdersByCarByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOrderByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new OrderDataModel(id, Guid.NewGuid().ToString(), 10, false,
|
||||
[new OrderServiceDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_orderStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
|
||||
var element = _orderBusinessLogicContract.GetOrderByData(id);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_orderStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOrderByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_orderStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOrderByData_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.GetOrderByData("orderId"), Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOrderByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_orderStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOrderByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_orderStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertOrder_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new OrderDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false,
|
||||
[new OrderServiceDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_orderStorageContract.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
|
||||
.Callback((OrderDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.CarId == record.CarId && x.TotalCost == record.TotalCost &&
|
||||
x.IsCancel == record.IsCancel && x.Services.First().ServiceId == record.Services.First().ServiceId &&
|
||||
x.Services.First().OrderId == record.Services.First().OrderId &&
|
||||
x.Services.First().Count == record.Services.First().Count;
|
||||
});
|
||||
|
||||
_orderBusinessLogicContract.InsertOrder(record);
|
||||
|
||||
_orderStorageContract.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertOrder_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.AddElement(It.IsAny<OrderDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.InsertOrder(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
10, false, [new OrderServiceDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_orderStorageContract.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertOrder_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.InsertOrder(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_orderStorageContract.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertOrder_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.InsertOrder(new OrderDataModel("id", Guid.NewGuid().ToString(), 10, false, [])), Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertOrder_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.AddElement(It.IsAny<OrderDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.InsertOrder(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
10, false, [new OrderServiceDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_orderStorageContract.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelOrder_CorrectRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_orderStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
|
||||
_orderBusinessLogicContract.CancelOrder(id);
|
||||
|
||||
_orderStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelOrder_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_orderStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.CancelOrder(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_orderStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelOrder_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.CancelOrder(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _orderBusinessLogicContract.CancelOrder(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_orderStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelOrder_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _orderBusinessLogicContract.CancelOrder("id"), Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelOrder_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_orderStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _orderBusinessLogicContract.CancelOrder(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_orderStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PimpMyRideBusinessLogic.Implementations;
|
||||
using PimpMyRide.BusinessLogicsContracts;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
|
||||
namespace PimpMyRideTest.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ServiceBusinessLogicContractTests
|
||||
{
|
||||
private ServiceBusinessLogicContract _serviceBusinessLogicContract;
|
||||
private Mock<IServiceStorageContracts> _serviceStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_serviceStorageContract = new Mock<IServiceStorageContracts>();
|
||||
_serviceBusinessLogicContract = new ServiceBusinessLogicContract(_serviceStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_serviceStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServices_ReturnListOfRecords_Test()
|
||||
{
|
||||
var listOriginal = new List<ServiceDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Wheels, 70,
|
||||
[new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Engine, 1000, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Body, 800, []),
|
||||
};
|
||||
_serviceStorageContract.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>())).Returns(listOriginal);
|
||||
|
||||
var list = _serviceBusinessLogicContract.GetAllServices();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_serviceStorageContract.Verify(x => x.GetList(null, WorkType.None), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServices_ReturnEmptyList_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>())).Returns([]);
|
||||
|
||||
var list = _serviceBusinessLogicContract.GetAllServices();
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServices_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServices(), Throws.TypeOf<NullListException>());
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServices_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServices(), Throws.TypeOf<StorageException>());
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServicesByWork_ReturnListOfRecords_Test()
|
||||
{
|
||||
var workId = Guid.NewGuid().ToString();
|
||||
var workType = WorkType.Body;
|
||||
var listOriginal = new List<ServiceDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Wheels, 70,
|
||||
[new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Engine, 1000, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Body, 800, []),
|
||||
};
|
||||
_serviceStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<WorkType?>())).Returns(listOriginal);
|
||||
|
||||
var list = _serviceBusinessLogicContract.GetAllServicesByWork(workId, workType);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
|
||||
_serviceStorageContract.Verify(x => x.GetList(workId, workType), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServicesByWork_ReturnEmptyList_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<WorkType?>())).Returns([]);
|
||||
|
||||
var list = _serviceBusinessLogicContract.GetAllServicesByWork(Guid.NewGuid().ToString(), WorkType.Body);
|
||||
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string?>(), It.IsAny<WorkType?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServicesByWork_WorkIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServicesByWork(null, WorkType.Body), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServicesByWork(string.Empty, WorkType.Body), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServicesByWork(Guid.NewGuid().ToString(), WorkType.None), Throws.TypeOf<ArgumentNullException>());
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServicesByWork_WorkerIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServicesByWork("workerId", WorkType.Body), Throws.TypeOf<ValidationException>());
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServicesByWork_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServicesByWork(Guid.NewGuid().ToString(), WorkType.Body), Throws.TypeOf<NullListException>());
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string>(), It.IsAny<WorkType>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllServicesByWork_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.GetList(It.IsAny<string?>(), It.IsAny<WorkType?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetAllServicesByWork(Guid.NewGuid().ToString(), WorkType.Body), Throws.TypeOf<StorageException>());
|
||||
_serviceStorageContract.Verify(x => x.GetList(It.IsAny<string?>(), It.IsAny<WorkType?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetServiceByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new ServiceDataModel(id, Guid.NewGuid().ToString(), WorkType.Body, 700,
|
||||
[new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_serviceStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
|
||||
var element = _serviceBusinessLogicContract.GetServiceByData(id);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_serviceStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetServiceByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetServiceByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetServiceByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_serviceStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetServiceByData_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetServiceByData("serviceId"), Throws.TypeOf<ValidationException>());
|
||||
_serviceStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetServiceByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetServiceByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_serviceStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetServiceByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _serviceBusinessLogicContract.GetServiceByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_serviceStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertService_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new ServiceDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Body, 700,
|
||||
[new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_serviceStorageContract.Setup(x => x.AddElement(It.IsAny<ServiceDataModel>()))
|
||||
.Callback((ServiceDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.WorkerId == record.WorkerId && x.WorkType == record.WorkType &&
|
||||
x.CostWithMaterials == record.CostWithMaterials &&
|
||||
x.Materials.First().ServiceId == record.Materials.First().ServiceId &&
|
||||
x.Materials.First().MaterialId == record.Materials.First().MaterialId &&
|
||||
x.Materials.First().Count == record.Materials.First().Count;
|
||||
});
|
||||
|
||||
_serviceBusinessLogicContract.InsertService(record);
|
||||
|
||||
_serviceStorageContract.Verify(x => x.AddElement(It.IsAny<ServiceDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertService_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.AddElement(It.IsAny<ServiceDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _serviceBusinessLogicContract.InsertService(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Body, 700,
|
||||
[new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_serviceStorageContract.Verify(x => x.AddElement(It.IsAny<ServiceDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertService_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.InsertService(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_serviceStorageContract.Verify(x => x.AddElement(It.IsAny<ServiceDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertService_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.InsertService(new ServiceDataModel("id", Guid.NewGuid().ToString(), WorkType.Body, 700,[])), Throws.TypeOf<ValidationException>());
|
||||
_serviceStorageContract.Verify(x => x.AddElement(It.IsAny<ServiceDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertService_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.AddElement(It.IsAny<ServiceDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _serviceBusinessLogicContract.InsertService(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Body, 700,
|
||||
[new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
_serviceStorageContract.Verify(x => x.AddElement(It.IsAny<ServiceDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelService_CorrectRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_serviceStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
|
||||
_serviceBusinessLogicContract.CancelService(id);
|
||||
|
||||
_serviceStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelService_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_serviceStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
|
||||
Assert.That(() => _serviceBusinessLogicContract.CancelService(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_serviceStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelService_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.CancelService(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _serviceBusinessLogicContract.CancelService(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_serviceStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelService_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _serviceBusinessLogicContract.CancelService("id"), Throws.TypeOf<ValidationException>());
|
||||
_serviceStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancelService_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_serviceStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _serviceBusinessLogicContract.CancelService(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_serviceStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PimpMyRideBusinessLogic.Implementations;
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.StoragesContracts;
|
||||
|
||||
namespace PimpMyRideTest.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class WorkerBusinessLogicContractTests
|
||||
{
|
||||
private WorkerBusinessLogicContract _workerBusinessLogicContract;
|
||||
private Mock<IWorkerStorageContract> _workerStorageContract;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_workerStorageContract = new Mock<IWorkerStorageContract>();
|
||||
_workerBusinessLogicContract = new WorkerBusinessLogicContract(_workerStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_workerStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkers_ReturnListOfRecords_Test()
|
||||
{
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2", SpecialityType.BodyWorker, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3", SpecialityType.TireFitter, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, SpecialityType.None, null, null, null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, SpecialityType.None, null, null, null, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkers_ReturnEmptyList_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), SpecialityType.None, null, null, null, null), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkers_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkers_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), SpecialityType.None, null, null, null, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2", SpecialityType.BodyWorker, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3", SpecialityType.TireFitter, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, SpecialityType.None, date, date.AddDays(1), null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, SpecialityType.None, date, date.AddDays(1), null, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_ReturnEmptyList_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, SpecialityType.None, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, SpecialityType.None, It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2", SpecialityType.BodyWorker, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3", SpecialityType.TireFitter, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, SpecialityType.None, null, null, date, date.AddDays(1)), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, SpecialityType.None, null, null, date, date.AddDays(1)), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_ReturnEmptyList_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, SpecialityType.None, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, SpecialityType.None, null, null, It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
var date = DateTime.UtcNow;
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date, It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddSeconds(-1), It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersBySpeciality_ReturnListOfRecords_Test()
|
||||
{
|
||||
var specialityType = SpecialityType.Mechanic;
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2", SpecialityType.BodyWorker, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3", SpecialityType.TireFitter, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersBySpeciality(specialityType, true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersBySpeciality(specialityType, false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, specialityType, null, null, null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, specialityType, null, null, null, null), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersBySpeciality_ReturnEmptyList_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersBySpeciality(SpecialityType.Mechanic, true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersBySpeciality(SpecialityType.Mechanic, false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersBySpeciality_SpecialityIsNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersBySpeciality(SpecialityType.None, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersBySpeciality_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersBySpeciality(SpecialityType.Mechanic, It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkersBySpeciality_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.GetAllWorkersBySpeciality(SpecialityType.Mechanic, It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<SpecialityType?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWorkerByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new WorkerDataModel(id, "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false);
|
||||
_workerStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
|
||||
var element = _workerBusinessLogicContract.GetWorkerByData(id);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWorkerByData_GetByFio_ReturnRecord_Test()
|
||||
{
|
||||
var fio = "fio";
|
||||
var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio, SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false);
|
||||
_workerStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
|
||||
|
||||
var element = _workerBusinessLogicContract.GetWorkerByData(fio);
|
||||
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.FIO, Is.EqualTo(fio));
|
||||
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWorkerByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWorkerByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_workerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertWorker_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false);
|
||||
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>()))
|
||||
.Callback((WorkerDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.FIO == record.FIO && x.SpecialityType == record.SpecialityType &&
|
||||
x.BirthDate == record.BirthDate && x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
|
||||
});
|
||||
|
||||
_workerBusinessLogicContract.InsertWorker(record);
|
||||
|
||||
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertWorker_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ElementExistsException>());
|
||||
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertWorker_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.InsertWorker(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertWorker_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new WorkerDataModel("id", "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
|
||||
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertWorker_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateWorker_CorrectRecord_Test()
|
||||
{
|
||||
var flag = false;
|
||||
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false);
|
||||
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>()))
|
||||
.Callback((WorkerDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.FIO == record.FIO && x.SpecialityType == record.SpecialityType &&
|
||||
x.BirthDate == record.BirthDate && x.EmploymentDate == record.EmploymentDate && x.IsDeleted == record.IsDeleted;
|
||||
});
|
||||
|
||||
_workerBusinessLogicContract.UpdateWorker(record);
|
||||
|
||||
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ElementNotFoundException>());
|
||||
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateWorker_NullRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateWorker_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new WorkerDataModel("id", "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
|
||||
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateWorker_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", SpecialityType.Mechanic, DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteWorker_CorrectRecord_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
|
||||
_workerBusinessLogicContract.DeleteWorker(id);
|
||||
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x != id))).Throws(new ElementNotFoundException(id));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteWorker_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"), Throws.TypeOf<ValidationException>());
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteWorker_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
_workerStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
|
||||
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,14 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="NUnit" Version="4.2.2" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PimpMyRideBusinessLogic\PimpMyRideBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\PimpMyRide\PimpMyRide.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user