Compare commits
2 Commits
main
...
Task2_Busi
| Author | SHA1 | Date | |
|---|---|---|---|
| cfbc287b8a | |||
| cf8fa96d80 |
@@ -1,10 +1,14 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.12.35527.113 d17.12
|
||||
VisualStudioVersion = 17.12.35527.113
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PimpMyRide", "PimpMyRide\PimpMyRide.csproj", "{0DF3EFC4-9A6D-4325-9C25-16F971E4BC53}"
|
||||
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
|
||||
@@ -15,6 +19,14 @@ Global
|
||||
{0DF3EFC4-9A6D-4325-9C25-16F971E4BC53}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0DF3EFC4-9A6D-4325-9C25-16F971E4BC53}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0DF3EFC4-9A6D-4325-9C25-16F971E4BC53}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B9E83569-89AA-4293-BC57-84FD67CE584E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{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);
|
||||
}
|
||||
42
PimpMyRide/PimpMyRide/DataModels/CarDataModel.cs
Normal file
42
PimpMyRide/PimpMyRide/DataModels/CarDataModel.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class CarDataModel(string id, string clientId, string make, string model, string stateNumber) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public string ClientId { get; private set; } = clientId;
|
||||
|
||||
public string Make { get; private set; } = make;
|
||||
|
||||
public string Model { get; private set; } = model;
|
||||
|
||||
public string StateNumber { get; private set; } = stateNumber;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
|
||||
if (ClientId.IsEmpty())
|
||||
throw new ValidationException("Field CustomerId is empty");
|
||||
|
||||
if (!ClientId.IsGuid())
|
||||
throw new ValidationException("The value in the field CustomerId is not a unique identifier");
|
||||
|
||||
if (Make.IsEmpty())
|
||||
throw new ValidationException("Field Make is empty");
|
||||
|
||||
if (Model.IsEmpty())
|
||||
throw new ValidationException("Field Model is empty");
|
||||
|
||||
if (StateNumber.IsEmpty())
|
||||
throw new ValidationException("Field StateNumber is empty");
|
||||
}
|
||||
}
|
||||
33
PimpMyRide/PimpMyRide/DataModels/ClientDataModel.cs
Normal file
33
PimpMyRide/PimpMyRide/DataModels/ClientDataModel.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class ClientDataModel(string id, string fio, string phoneNumber) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public string FIO { get; private set; } = fio;
|
||||
|
||||
public string PhoneNumber { get; private set; } = phoneNumber;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not an unique identifier");
|
||||
|
||||
if (FIO.IsEmpty())
|
||||
throw new ValidationException("Field FIO is empty");
|
||||
|
||||
if (PhoneNumber.IsEmpty())
|
||||
throw new ValidationException("Field PhoneNumber is empty");
|
||||
|
||||
if (!Regex.IsMatch(PhoneNumber, @"^((\+7|7|8)+([0-9]){10})$"))
|
||||
throw new ValidationException("Field PhoneNumber is not a phone number");
|
||||
}
|
||||
}
|
||||
35
PimpMyRide/PimpMyRide/DataModels/MaterialDataModel.cs
Normal file
35
PimpMyRide/PimpMyRide/DataModels/MaterialDataModel.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class MaterialDataModel(string id, MaterialType materialType, string description, double unitCost) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public MaterialType MaterialType { get; private set; } = materialType;
|
||||
|
||||
public string Description { get; private set; } = description;
|
||||
|
||||
public double UnitCost { get; private set; } = unitCost;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not an unique identifier");
|
||||
|
||||
if (MaterialType == MaterialType.None)
|
||||
throw new ValidationException("Field MaterialType is empty");
|
||||
|
||||
if (Description.IsEmpty())
|
||||
throw new ValidationException("Field Description is empty");
|
||||
|
||||
if (UnitCost < 0)
|
||||
throw new ValidationException("Cost cannot be zero or less");
|
||||
}
|
||||
}
|
||||
26
PimpMyRide/PimpMyRide/DataModels/MaterialHistoryDataModel.cs
Normal file
26
PimpMyRide/PimpMyRide/DataModels/MaterialHistoryDataModel.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class MaterialHistoryDataModel(string materialId, double oldPrice) : IValidation
|
||||
{
|
||||
public string MaterialId { get; private set; } = materialId;
|
||||
|
||||
public double OldPrice { get; private set; } = oldPrice;
|
||||
|
||||
public DateTime ChangeDate { get; private set;} = DateTime.UtcNow;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (MaterialId.IsEmpty())
|
||||
throw new ValidationException("Field MaterialId is empty");
|
||||
|
||||
if (!MaterialId.IsGuid())
|
||||
throw new ValidationException("The value in field MaterialId is not an unique identifier");
|
||||
|
||||
if (OldPrice <= 0)
|
||||
throw new ValidationException("OldPrice value cannnot be less or equal to '0'");
|
||||
}
|
||||
}
|
||||
41
PimpMyRide/PimpMyRide/DataModels/OrderDataModel.cs
Normal file
41
PimpMyRide/PimpMyRide/DataModels/OrderDataModel.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class OrderDataModel(string id, string carId, double totalCost, bool isCancel, List<OrderServiceDataModel> services) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public string CarId { get; private set; } = carId;
|
||||
|
||||
public DateTime OrderDate { get; private set; } = DateTime.UtcNow;
|
||||
|
||||
public double TotalCost { get; private set; } = totalCost;
|
||||
|
||||
public bool IsCancel { get; private set; } = isCancel;
|
||||
|
||||
public List<OrderServiceDataModel> Services { get; private set; } = services;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not an unique identifier");
|
||||
|
||||
if (CarId.IsEmpty())
|
||||
throw new ValidationException("Field CarId is empty");
|
||||
|
||||
if (!CarId.IsGuid())
|
||||
throw new ValidationException("The value in the field CarId is not an unique identifier");
|
||||
|
||||
if (TotalCost <= 0)
|
||||
throw new ValidationException("TotalCost cannot be 0 or less");
|
||||
|
||||
if ((Services?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The order must include Services");
|
||||
}
|
||||
}
|
||||
32
PimpMyRide/PimpMyRide/DataModels/OrderServiceDataModel.cs
Normal file
32
PimpMyRide/PimpMyRide/DataModels/OrderServiceDataModel.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class OrderServiceDataModel(string orderId, string serviceId, int count) : IValidation
|
||||
{
|
||||
public string OrderId { get; private set; } = orderId;
|
||||
|
||||
public string ServiceId { get; private set; } = serviceId;
|
||||
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (OrderId.IsEmpty())
|
||||
throw new ValidationException("Field OrderId is empty");
|
||||
|
||||
if (!OrderId.IsGuid())
|
||||
throw new ValidationException("The value in the field OrderId is not a unique identifier");
|
||||
|
||||
if (ServiceId.IsEmpty())
|
||||
throw new ValidationException("Field ServiceId is empty");
|
||||
|
||||
if (!ServiceId.IsGuid())
|
||||
throw new ValidationException("The value in the field ServiceId is not a unique identifier");
|
||||
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
43
PimpMyRide/PimpMyRide/DataModels/ServiceDataModel.cs
Normal file
43
PimpMyRide/PimpMyRide/DataModels/ServiceDataModel.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class ServiceDataModel(string id, string workerId, WorkType workType, double cost, List<ServiceMaterialDataModel> materials) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public string WorkerId { get; private set; } = workerId;
|
||||
|
||||
public WorkType WorkType { get; private set; } = workType;
|
||||
|
||||
public double CostWithMaterials { get; private set; } = cost;
|
||||
|
||||
public List<ServiceMaterialDataModel> Materials { get; private set; } = materials;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not an unique identifier");
|
||||
|
||||
if (WorkerId.IsEmpty())
|
||||
throw new ValidationException("Field WorkerId is empty");
|
||||
|
||||
if (!WorkerId.IsGuid())
|
||||
throw new ValidationException("The value in the field WorkerId is not an unique identifier");
|
||||
|
||||
if (WorkType == WorkType.None)
|
||||
throw new ValidationException("Field WorkType is empty");
|
||||
|
||||
if (CostWithMaterials <= 0)
|
||||
throw new ValidationException("CostWithMaterials cannot be 0 or less");
|
||||
|
||||
if ((Materials?.Count ?? 0) == 0)
|
||||
throw new ValidationException("The service must include Materials");
|
||||
}
|
||||
}
|
||||
32
PimpMyRide/PimpMyRide/DataModels/ServiceMaterialDataModel.cs
Normal file
32
PimpMyRide/PimpMyRide/DataModels/ServiceMaterialDataModel.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class ServiceMaterialDataModel(string serviceId, string materialId, int count) : IValidation
|
||||
{
|
||||
public string ServiceId { get; private set; } = serviceId;
|
||||
|
||||
public string MaterialId { get; private set; } = materialId;
|
||||
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (ServiceId.IsEmpty())
|
||||
throw new ValidationException("Field ServiceId is empty");
|
||||
|
||||
if (!ServiceId.IsGuid())
|
||||
throw new ValidationException("The value in the field ServiceId is not a unique identifier");
|
||||
|
||||
if (MaterialId.IsEmpty())
|
||||
throw new ValidationException("Field MaterialId is empty");
|
||||
|
||||
if (!MaterialId.IsGuid())
|
||||
throw new ValidationException("The value in the field MaterialId is not a unique identifier");
|
||||
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Field Count is less than or equal to 0");
|
||||
}
|
||||
}
|
||||
45
PimpMyRide/PimpMyRide/DataModels/WorkerDataModel.cs
Normal file
45
PimpMyRide/PimpMyRide/DataModels/WorkerDataModel.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
using PimpMyRide.Extensions;
|
||||
using PimpMyRide.Infrastructure;
|
||||
|
||||
namespace PimpMyRide.DataModels;
|
||||
|
||||
public class WorkerDataModel(string id, string fio, SpecialityType specialityType, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
public string FIO { get; private set; } = fio;
|
||||
|
||||
public SpecialityType SpecialityType { get; private set; } = specialityType;
|
||||
|
||||
public DateTime BirthDate { get; private set; } = birthDate;
|
||||
|
||||
public DateTime EmploymentDate { get; private set; } = employmentDate;
|
||||
|
||||
public bool IsDeleted { get; private set; } = isDeleted;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
throw new ValidationException("Field Id is empty");
|
||||
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("The value in the field Id is not an unique identifier");
|
||||
|
||||
if (FIO.IsEmpty())
|
||||
throw new ValidationException("Field FIO is empty");
|
||||
|
||||
if (SpecialityType == SpecialityType.None)
|
||||
throw new ValidationException("Field SpecialityType is empty");
|
||||
|
||||
if (BirthDate.Date > DateTime.Now.AddYears(-18).Date)
|
||||
throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})");
|
||||
|
||||
if (EmploymentDate.Date < BirthDate.Date)
|
||||
throw new ValidationException("The date of employment cannot be less than the date of birth");
|
||||
|
||||
if ((EmploymentDate - BirthDate).TotalDays / 365 < 16)
|
||||
throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
|
||||
}
|
||||
}
|
||||
10
PimpMyRide/PimpMyRide/Enums/MaterialType.cs
Normal file
10
PimpMyRide/PimpMyRide/Enums/MaterialType.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PimpMyRide.Enums;
|
||||
|
||||
public enum MaterialType
|
||||
{
|
||||
None = 0,
|
||||
SmallConsumables = 1,
|
||||
BodyParts = 2,
|
||||
EngineParts = 3,
|
||||
Wheels = 4
|
||||
}
|
||||
10
PimpMyRide/PimpMyRide/Enums/SpecialityType.cs
Normal file
10
PimpMyRide/PimpMyRide/Enums/SpecialityType.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PimpMyRide.Enums;
|
||||
|
||||
public enum SpecialityType
|
||||
{
|
||||
None = 0,
|
||||
TireFitter = 1,
|
||||
BodyWorker = 2,
|
||||
MaintenanceWorker = 3,
|
||||
Mechanic = 4
|
||||
}
|
||||
10
PimpMyRide/PimpMyRide/Enums/WorkType.cs
Normal file
10
PimpMyRide/PimpMyRide/Enums/WorkType.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PimpMyRide.Enums;
|
||||
|
||||
public enum WorkType
|
||||
{
|
||||
None = 0,
|
||||
Wheels = 1,
|
||||
Body = 2,
|
||||
Maintenance = 3,
|
||||
Engine = 4
|
||||
}
|
||||
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) { }
|
||||
}
|
||||
5
PimpMyRide/PimpMyRide/Exceptions/ValidationException.cs
Normal file
5
PimpMyRide/PimpMyRide/Exceptions/ValidationException.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace PimpMyRide.Exceptions;
|
||||
|
||||
public class ValidationException(string message) : Exception(message)
|
||||
{
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
14
PimpMyRide/PimpMyRide/Extensions/StringExtensions.cs
Normal file
14
PimpMyRide/PimpMyRide/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace PimpMyRide.Extensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool IsEmpty(this string str)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(str);
|
||||
}
|
||||
|
||||
public static bool IsGuid(this string str)
|
||||
{
|
||||
return Guid.TryParse(str, out _);
|
||||
}
|
||||
}
|
||||
6
PimpMyRide/PimpMyRide/Infrastructure/IValidation.cs
Normal file
6
PimpMyRide/PimpMyRide/Infrastructure/IValidation.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PimpMyRide.Infrastructure;
|
||||
|
||||
public interface IValidation
|
||||
{
|
||||
void Validate();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]//Указывает, что класс является контейнером для тестовых методов.
|
||||
internal class CarDataModelTests
|
||||
{
|
||||
[Test]//Указывает, что метод — это метода теста.
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var car = CreateDataModel(null, Guid.NewGuid().ToString(), "Porsche", "911", "A991MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
car = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), "Porsche", "911", "A991MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var car = CreateDataModel("id", Guid.NewGuid().ToString(), "Porsche", "911", "A991MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClientIdIsNullOrEmptyTest()
|
||||
{
|
||||
var car = CreateDataModel(Guid.NewGuid().ToString(), null, "Porsche", "911", "A991MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
car = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "Porsche", "911", "A991MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClientIdIsNotGuidTest()
|
||||
{
|
||||
var car = CreateDataModel(Guid.NewGuid().ToString(), "id", "Porsche", "911", "A991MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MakeIsNullOrEmptyTest()
|
||||
{
|
||||
var car = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, "911", "A911MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
car = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), string.Empty, "911", "A911MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModelIsNullOrEmptyTest()
|
||||
{
|
||||
var car = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", null, "A911MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
car = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", string.Empty, "A911MP");
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StateNumberIsNullOrEmptyTest()
|
||||
{
|
||||
var car = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", null);
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
car = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "Porsche", "911", string.Empty);
|
||||
Assert.That(() => car.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var clientId = Guid.NewGuid().ToString();
|
||||
var make = "Porsche";
|
||||
var model = "911";
|
||||
var stateNumber = "A911MP";
|
||||
var car = CreateDataModel(id, clientId, make, model, stateNumber);
|
||||
Assert.That(() => car.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(car.Id, Is.EqualTo(id));
|
||||
Assert.That(car.ClientId, Is.EqualTo(clientId));
|
||||
Assert.That(car.Make, Is.EqualTo(make));
|
||||
Assert.That(car.Model, Is.EqualTo(model));
|
||||
Assert.That(car.StateNumber, Is.EqualTo(stateNumber));
|
||||
});
|
||||
}
|
||||
|
||||
private static CarDataModel CreateDataModel(string? id, string? clientId, string? make, string? model, string? stateNumber) =>
|
||||
new(id, clientId, make, model, stateNumber);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ClientDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var client = CreateDataModel(null, "fio", "88005553535");
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
client = CreateDataModel(string.Empty, "fio", "88005553535");
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var client = CreateDataModel("id", "fio", "88005553535");
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FIOIsNullOrEmptyTest()
|
||||
{
|
||||
var client = CreateDataModel(Guid.NewGuid().ToString(), null, "88005553535");
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
client = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, "88005553535");
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PhoneNumberIsNullOrEmptyTest()
|
||||
{
|
||||
var client = CreateDataModel(Guid.NewGuid().ToString(), "fio", null);
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
client = CreateDataModel(Guid.NewGuid().ToString(), "fio", string.Empty);
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PhoneNumberIsIncorrectTest()
|
||||
{
|
||||
var client = CreateDataModel(Guid.NewGuid().ToString(), "fio", "777");
|
||||
Assert.That(() => client.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var clientId = Guid.NewGuid().ToString();
|
||||
var fio = "Fio";
|
||||
var phoneNumber = "88005553535";
|
||||
var client = CreateDataModel(clientId, fio, phoneNumber);
|
||||
Assert.That(() => client.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(client.Id, Is.EqualTo(clientId));
|
||||
Assert.That(client.FIO, Is.EqualTo(fio));
|
||||
Assert.That(client.PhoneNumber, Is.EqualTo(phoneNumber));
|
||||
});
|
||||
}
|
||||
|
||||
private static ClientDataModel CreateDataModel(string? id, string? fio, string? phoneNumber) =>
|
||||
new(id, fio, phoneNumber);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class MaterialDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var material = CreateDataModel(null, MaterialType.Wheels, "Bridgestone tires", 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
material = CreateDataModel(string.Empty, MaterialType.Wheels, "Bridgestone tires", 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var material = CreateDataModel("id", MaterialType.Wheels, "Bridgestone tires", 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MaterialTypeIsNoneTest()
|
||||
{
|
||||
var material = CreateDataModel(Guid.NewGuid().ToString(), MaterialType.None, "Bridgestone tires", 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DescriptionIsNullOrEmptyTest()
|
||||
{
|
||||
var material = CreateDataModel(Guid.NewGuid().ToString(), MaterialType.Wheels, null, 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
material = CreateDataModel(Guid.NewGuid().ToString(), MaterialType.Wheels, string.Empty, 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnitCostIsNegativeTest()
|
||||
{
|
||||
var material = CreateDataModel(Guid.NewGuid().ToString(), MaterialType.Wheels, "Bridgestone tires", -1.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var materialType = MaterialType.Wheels;
|
||||
var description = "Bridgestone tires";
|
||||
var unitCost = 100.0;
|
||||
var material = CreateDataModel(id, materialType, description, unitCost);
|
||||
Assert.That(() => material.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(material.Id, Is.EqualTo(id));
|
||||
Assert.That(material.MaterialType, Is.EqualTo(materialType));
|
||||
Assert.That(material.Description, Is.EqualTo(description));
|
||||
Assert.That(material.UnitCost, Is.EqualTo(unitCost));
|
||||
});
|
||||
}
|
||||
|
||||
private static MaterialDataModel CreateDataModel(string? id, MaterialType materialType, string? description, double unitCost) =>
|
||||
new(id, materialType, description, unitCost);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class MaterialHistoryDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void MaterialIdIsNullOrEmptyTest()
|
||||
{
|
||||
var material = CreateDataModel(null, 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
material = CreateDataModel(string.Empty, 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MaterialIdIsNotGuidTest()
|
||||
{
|
||||
var material = CreateDataModel("id", 100.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OldPriceIsZeroOrNegative()
|
||||
{
|
||||
var material = CreateDataModel(Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
material = CreateDataModel(Guid.NewGuid().ToString(), -1.0);
|
||||
Assert.That(() => material.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var materialId = Guid.NewGuid().ToString();
|
||||
var oldPrice = 100.0;
|
||||
var material = CreateDataModel(materialId, oldPrice);
|
||||
Assert.That(() => material.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(material.MaterialId, Is.EqualTo(materialId));
|
||||
Assert.That(material.OldPrice, Is.EqualTo(oldPrice));
|
||||
});
|
||||
}
|
||||
|
||||
private static MaterialHistoryDataModel CreateDataModel(string? materialId, double oldPrice) =>
|
||||
new(materialId, oldPrice);
|
||||
}
|
||||
101
PimpMyRide/PimpMyRideTest/DataModelTests/OrderDataModelTests.cs
Normal file
101
PimpMyRide/PimpMyRideTest/DataModelTests/OrderDataModelTests.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class OrderDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var order = CreateDataModel(null, Guid.NewGuid().ToString(), 500.0, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
order = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 500.0, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var order = CreateDataModel("id", Guid.NewGuid().ToString(), 500.0, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CarIdIsNullOrEmptyTest()
|
||||
{
|
||||
var order = CreateDataModel(Guid.NewGuid().ToString(), null, 500.0, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
order = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 500.0, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CarIdIsNotGuidTest()
|
||||
{
|
||||
var order = CreateDataModel(Guid.NewGuid().ToString(), "id", 500.0, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TotalCostIsZeroOrNegativeTest()
|
||||
{
|
||||
var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
|
||||
order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1, false,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ServicesAreEmptyTest()
|
||||
{
|
||||
var order = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 500.0, false, []);
|
||||
Assert.That(() => order.Validate(), Throws.TypeOf<ValidationException>());
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var carId = Guid.NewGuid().ToString();
|
||||
var totalCost = 5000.0;
|
||||
var isCancel = false;
|
||||
var services = new List<OrderServiceDataModel> { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1) };
|
||||
|
||||
var order = CreateDataModel(id, carId, totalCost, isCancel, services);
|
||||
|
||||
|
||||
Assert.That(() => order.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(order.Id, Is.EqualTo(id));
|
||||
Assert.That(order.CarId, Is.EqualTo(carId));
|
||||
Assert.That(order.TotalCost, Is.EqualTo(totalCost));
|
||||
Assert.That(order.IsCancel, Is.EqualTo(isCancel));
|
||||
Assert.That(order.Services, Is.EquivalentTo(services));
|
||||
});
|
||||
}
|
||||
|
||||
private static OrderDataModel CreateDataModel(string? id, string? carId, double totalCost, bool isCancel,
|
||||
List<OrderServiceDataModel> services) => new(id, carId, totalCost, isCancel, services);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class OrderServiceDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void OrderIdIsNullOrEmptyTest()
|
||||
{
|
||||
var orderService = CreateDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
orderService = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OrderIdIsNotGuidTest()
|
||||
{
|
||||
var orderService = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ServiceIdIsNullOrEmptyTest()
|
||||
{
|
||||
var orderService = CreateDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
orderService = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ServiceIdIsNotGuidTest()
|
||||
{
|
||||
var orderService = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsZeroOrNegativeTest()
|
||||
{
|
||||
var orderService = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
orderService = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => orderService.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var orderId = Guid.NewGuid().ToString();
|
||||
var serviceId = Guid.NewGuid().ToString();
|
||||
var count = 2;
|
||||
var orderService = CreateDataModel(orderId, serviceId, count);
|
||||
Assert.That(() => orderService.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(orderService.OrderId, Is.EqualTo(orderId));
|
||||
Assert.That(orderService.ServiceId, Is.EqualTo(serviceId));
|
||||
Assert.That(orderService.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static OrderServiceDataModel CreateDataModel(string? orderId, string? serviceId, int count) =>
|
||||
new(orderId, serviceId, count);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ServiceDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var service = CreateDataModel(null, Guid.NewGuid().ToString(), WorkType.Engine, 200.0,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => service.Validate(), Throws.TypeOf<ValidationException>());
|
||||
service = CreateDataModel(string.Empty, Guid.NewGuid().ToString(), WorkType.Engine, 200.0,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => service.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var service = CreateDataModel("id", Guid.NewGuid().ToString(), WorkType.Engine, 200.0,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => service.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WorkerIdIsNotGuidTest()
|
||||
{
|
||||
var service = CreateDataModel(Guid.NewGuid().ToString(), "worker", WorkType.Engine, 200.0,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => service.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WorkTypeIsNoneTest()
|
||||
{
|
||||
var service = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.None, 200.0,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => service.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CostIsZeroOrNegativeTest()
|
||||
{
|
||||
var service = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Engine, 0.0,
|
||||
[new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)]);
|
||||
Assert.That(() => service.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MaterialsAreEmptyTest()
|
||||
{
|
||||
var service = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), WorkType.Engine, 200.0, []);
|
||||
Assert.That(() => service.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
var workType = WorkType.Engine;
|
||||
var cost = 200.0;
|
||||
var materials = new List<ServiceMaterialDataModel> {new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)};
|
||||
|
||||
var service = CreateDataModel(id, workerId, workType, cost, materials);
|
||||
|
||||
Assert.That(() => service.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(service.Id, Is.EqualTo(id));
|
||||
Assert.That(service.WorkerId, Is.EqualTo(workerId));
|
||||
Assert.That(service.WorkType, Is.EqualTo(workType));
|
||||
Assert.That(service.CostWithMaterials, Is.EqualTo(cost));
|
||||
Assert.That(service.Materials, Is.EquivalentTo(materials));
|
||||
});
|
||||
}
|
||||
|
||||
private static ServiceDataModel CreateDataModel(string? id, string? workerId, WorkType workType, double cost,
|
||||
List<ServiceMaterialDataModel> materials) => new(id, workerId, workType, cost, materials);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class ServiceMaterialDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void ServiceIdIsNullOrEmptyTest()
|
||||
{
|
||||
var serviceMaterial = new ServiceMaterialDataModel(null, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
serviceMaterial = new ServiceMaterialDataModel(string.Empty, Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ServiceIdIsNotGuidTest()
|
||||
{
|
||||
var serviceMaterial = CreateDataModel("id", Guid.NewGuid().ToString(), 1);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MaterialIdIsNullOrEmptyTest()
|
||||
{
|
||||
var serviceMaterial = new ServiceMaterialDataModel(Guid.NewGuid().ToString(), null, 1);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
serviceMaterial = new ServiceMaterialDataModel(Guid.NewGuid().ToString(), string.Empty, 1);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MaterialIdIsNotGuidTest()
|
||||
{
|
||||
var serviceMaterial = CreateDataModel(Guid.NewGuid().ToString(), "id", 1);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountIsZeroOrNegativeTest()
|
||||
{
|
||||
var serviceMaterial = new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
serviceMaterial = new ServiceMaterialDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -1);
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var serviceId = Guid.NewGuid().ToString();
|
||||
var materialId = Guid.NewGuid().ToString();
|
||||
var count = 2;
|
||||
|
||||
var serviceMaterial = CreateDataModel(serviceId, materialId, count);
|
||||
|
||||
Assert.That(() => serviceMaterial.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(serviceMaterial.ServiceId, Is.EqualTo(serviceId));
|
||||
Assert.That(serviceMaterial.MaterialId, Is.EqualTo(materialId));
|
||||
Assert.That(serviceMaterial.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
|
||||
private static ServiceMaterialDataModel CreateDataModel(string? serviceId, string? materialId, int count) =>
|
||||
new(serviceId, materialId, count);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using PimpMyRide.DataModels;
|
||||
using PimpMyRide.Enums;
|
||||
using PimpMyRide.Exceptions;
|
||||
|
||||
namespace PimpMyRideTest.DataModelTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class WorkerDataModelTests
|
||||
{
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var worker = CreateDataModel(null, "John Doe", SpecialityType.Mechanic, DateTime.Now.AddYears(-20),
|
||||
DateTime.Now.AddYears(-2), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
worker = CreateDataModel(string.Empty, "John Doe", SpecialityType.Mechanic, DateTime.Now.AddYears(-20),
|
||||
DateTime.Now.AddYears(-2), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var worker = CreateDataModel("id", "John Doe", SpecialityType.Mechanic, DateTime.Now.AddYears(-20),
|
||||
DateTime.Now.AddYears(-2), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FioIsNullOrEmptyTest()
|
||||
{
|
||||
var worker = CreateDataModel(Guid.NewGuid().ToString(), null, SpecialityType.Mechanic, DateTime.Now.AddYears(-20),
|
||||
DateTime.Now.AddYears(-2), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
worker = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, SpecialityType.Mechanic, DateTime.Now.AddYears(-20),
|
||||
DateTime.Now.AddYears(-2), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SpecialityTypeIsNoneTest()
|
||||
{
|
||||
var worker = CreateDataModel(Guid.NewGuid().ToString(), "John Doe", SpecialityType.None, DateTime.Now.AddYears(-20),
|
||||
DateTime.Now.AddYears(-2), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BirthDateIsUnderageTest()
|
||||
{
|
||||
var worker = CreateDataModel(Guid.NewGuid().ToString(), "John Doe", SpecialityType.Mechanic, DateTime.Now.AddYears(-17),
|
||||
DateTime.Now.AddYears(-1), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmploymentDateBeforeBirthDateTest()
|
||||
{
|
||||
var worker = CreateDataModel(Guid.NewGuid().ToString(), "John Doe", SpecialityType.Mechanic, DateTime.Now.AddYears(-30),
|
||||
DateTime.Now.AddYears(-31), false);
|
||||
Assert.That(() => worker.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllFieldsAreCorrectTest()
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var fio = "John Doe";
|
||||
var specialityType = SpecialityType.Mechanic;
|
||||
var birthDate = DateTime.Now.AddYears(-25);
|
||||
var employmentDate = DateTime.Now.AddYears(-5);
|
||||
var isDeleted = false;
|
||||
|
||||
var worker = CreateDataModel(id, fio, specialityType, birthDate, employmentDate, isDeleted);
|
||||
Assert.That(() => worker.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(worker.Id, Is.EqualTo(id));
|
||||
Assert.That(worker.FIO, Is.EqualTo(fio));
|
||||
Assert.That(worker.SpecialityType, Is.EqualTo(specialityType));
|
||||
Assert.That(worker.BirthDate, Is.EqualTo(birthDate));
|
||||
Assert.That(worker.EmploymentDate, Is.EqualTo(employmentDate));
|
||||
Assert.That(worker.IsDeleted, Is.EqualTo(isDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
private static WorkerDataModel CreateDataModel(string? id, string? fio, SpecialityType specialityType, DateTime birthDate,
|
||||
DateTime employmentDate, bool isDeleted) => new(id, fio, specialityType, birthDate, employmentDate, isDeleted);
|
||||
}
|
||||
29
PimpMyRide/PimpMyRideTest/PimpMyRideTest.csproj
Normal file
29
PimpMyRide/PimpMyRideTest/PimpMyRideTest.csproj
Normal file
@@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.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>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="NUnit.Framework" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user