Тесты бизнес логики в зелёной зоне

This commit is contained in:
145Game 2025-02-17 13:19:13 +04:00
parent 5af0fcdb14
commit 0c79a9475d
8 changed files with 307 additions and 56 deletions

View File

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

View File

@ -3,6 +3,9 @@ using North_Bridge_Contract.DataModels;
using North_Bridge_Contract.Enums;
using North_Bridge_Contract.StoragesContracts;
using Microsoft.Extensions.Logging;
using North_Bridge_Contract.Exceptions;
using North_Bridge_Contract.Extentions;
using System.Text.Json;
namespace North_Bridge_BusinessLogics.Implementations;
@ -14,28 +17,71 @@ internal class ProductBusinessLogicContract(IProductStorageContract productStora
public List<ProductDataModel> GetAllProducts(bool onlyActive)
{
return [];
_logger.LogInformation("GetAllProducts params: {onlyActive}", onlyActive);
return _productStorageContract.GetList(onlyActive) ?? throw new
NullListException();
}
public List<ProductHistoryDataModel> GetProductHistoryByProduct(string productId)
{
return [];
_logger.LogInformation("GetProductHistoryByProduct for {productId}", productId);
if (productId.IsEmpty())
{
throw new ArgumentNullException(nameof(productId));
}
if (!productId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
}
return _productStorageContract.GetHistoryByProductId(productId) ?? throw new NullListException();
}
public ProductDataModel GetProductByData(string data)
{
return new("", "", ProductType.None, 0, true);
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (data.IsGuid())
{
return _productStorageContract.GetElementById(data) ?? throw
new ElementNotFoundException(data);
}
return _productStorageContract.GetElementByName(data) ?? throw new
ElementNotFoundException(data);
}
public void InsertProduct(ProductDataModel productDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
_productStorageContract.AddElement(productDataModel);
}
public void UpdateProduct(ProductDataModel productDataModel)
{
_logger.LogInformation("Update data: {json}",
JsonSerializer.Serialize(productDataModel));
ArgumentNullException.ThrowIfNull(productDataModel);
productDataModel.Validate();
_productStorageContract.UpdElement(productDataModel);
}
public void DeleteProduct(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");
}
_productStorageContract.DelElement(id);
}
}

View File

@ -2,6 +2,8 @@
using North_Bridge_Contract.DataModels;
using North_Bridge_Contract.StoragesContracts;
using Microsoft.Extensions.Logging;
using North_Bridge_Contract.Exceptions;
using North_Bridge_Contract.Extentions;
namespace North_Bridge_BusinessLogics.Implementations;
@ -17,19 +19,53 @@ internal class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageC
private readonly IWorkerStorageContract _workerStorageContract = workerStorageContract;
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate,
DateTime toDate)
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{
return [];
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _salaryStorageContract.GetList(fromDate, toDate) ?? throw new
NullListException();
}
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime
fromDate, DateTime toDate, string workerId)
public List<SalaryDataModel> GetAllSalariesByPeriodByWorker(DateTime fromDate, DateTime toDate, string workerId)
{
return [];
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (workerId.IsEmpty())
{
throw new ArgumentNullException(nameof(workerId));
}
if (!workerId.IsGuid())
{
throw new ValidationException("The value in the field workerId is not a unique identifier.");
}
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}, { workerId}", fromDate, toDate, workerId);
return _salaryStorageContract.GetList(fromDate, toDate, workerId) ??
throw new NullListException();
}
public void CalculateSalaryByMounth(DateTime date)
{
_logger.LogInformation("CalculateSalaryByMounth: {date}", date);
var startDate = new DateTime(date.Year, date.Month, 1);
var finishDate = new DateTime(date.Year, date.Month,
DateTime.DaysInMonth(date.Year, date.Month));
var workers = _workerStorageContract.GetList() ?? throw new NullListException();
foreach (var worker in workers)
{
var sales = _saleStorageContract.GetList(startDate,
finishDate, workerId: worker.Id)?.Sum(x => x.Sum) ??
throw new NullListException();
var post = _postStorageContract.GetElementById(worker.PostId)
?? throw new NullListException();
var salary = post.Salary + sales * 0.1;
_logger.LogDebug("The employee {workerId} was paid a salary of { salary}", worker.Id, salary);
_salaryStorageContract.AddElement(new SalaryDataModel(worker.Id, finishDate, salary));
}
}
}

View File

@ -1,7 +1,10 @@
using Microsoft.Extensions.Logging;
using North_Bridge_Contract.BusinessLogicsContracts;
using North_Bridge_Contract.DataModels;
using North_Bridge_Contract.Exceptions;
using North_Bridge_Contract.Extentions;
using North_Bridge_Contract.StoragesContracts;
using System.Text.Json;
namespace North_Bridge_BusinessLogics.Implementations;
@ -11,33 +14,89 @@ internal class SaleBusinessLogicContract(ISaleStorageContract saleStorageContrac
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime
toDate)
public List<SaleDataModel> GetAllSalesByPeriod(DateTime fromDate, DateTime toDate)
{
return [];
_logger.LogInformation("GetAllSales params: {fromDate}, {toDate}", fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
return _saleStorageContract.GetList(fromDate, toDate) ?? throw new
NullListException();
}
public List<SaleDataModel> GetAllSalesByWorkerByPeriod(string workerId,
DateTime fromDate, DateTime toDate)
{
return [];
_logger.LogInformation("GetAllSales params: {workerId}, {fromDate}, { toDate}", workerId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (workerId.IsEmpty())
{
throw new ArgumentNullException(nameof(workerId));
}
if (!workerId.IsGuid())
{
throw new ValidationException("The value in the field workerId is not a unique identifier.");
}
return _saleStorageContract.GetList(fromDate, toDate, workerId: workerId) ?? throw new NullListException();
}
public List<SaleDataModel> GetAllSalesByProductByPeriod(string productId, DateTime fromDate, DateTime toDate)
{
return [];
_logger.LogInformation("GetAllSales params: {productId}, {fromDate}, { toDate}", productId, fromDate, toDate);
if (fromDate.IsDateNotOlder(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
if (productId.IsEmpty())
{
throw new ArgumentNullException(nameof(productId));
}
if (!productId.IsGuid())
{
throw new ValidationException("The value in the field productId is not a unique identifier.");
}
return _saleStorageContract.GetList(fromDate, toDate, productId: productId) ?? throw new NullListException();
}
public SaleDataModel GetSaleByData(string data)
{
return new("", "", 0, false, []);
_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 _saleStorageContract.GetElementById(data) ?? throw new
ElementNotFoundException(data);
}
public void InsertSale(SaleDataModel saleDataModel)
{
_logger.LogInformation("New data: {json}",
JsonSerializer.Serialize(saleDataModel));
ArgumentNullException.ThrowIfNull(saleDataModel);
saleDataModel.Validate();
_saleStorageContract.AddElement(saleDataModel);
}
public void CancelSale(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");
}
_saleStorageContract.DelElement(id);
}
}

View File

@ -1,7 +1,10 @@
using Microsoft.Extensions.Logging;
using North_Bridge_Contract.BusinessLogicsContracts;
using North_Bridge_Contract.DataModels;
using North_Bridge_Contract.Exceptions;
using North_Bridge_Contract.Extentions;
using North_Bridge_Contract.StoragesContracts;
using System.Text.Json;
namespace North_Bridge_BusinessLogics.Implementations;
@ -13,39 +16,92 @@ internal class WorkerBusinessLogicContract(IWorkerStorageContract workerStorageC
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
{
return [];
_logger.LogInformation("GetAllWorkers params: {onlyActive}", onlyActive);
return _workerStorageContract.GetList(onlyActive) ?? throw new NullListException();
}
public List<WorkerDataModel> GetAllWorkersByPost(string postId, bool
onlyActive = true)
{
return [];
_logger.LogInformation("GetAllWorkers params: {postId}, { onlyActive},", postId, onlyActive);
if (postId.IsEmpty())
{
throw new ArgumentNullException(nameof(postId));
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier.");
}
return _workerStorageContract.GetList(onlyActive, postId) ?? throw new NullListException();
}
public List<WorkerDataModel> GetAllWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
return [];
_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)
{
return [];
_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 WorkerDataModel GetWorkerByData(string data)
{
return new("", "", "", "", DateTime.Now, DateTime.Now, true);
_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);
}
}

View File

@ -20,7 +20,7 @@ public class WorkerDataModel : IValidation
public bool IsDeleted { get; private set; }
public WorkerDataModel(string id, string email, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted)
public WorkerDataModel(string id, string fio, string email, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted)
{
Id = id;
FIO = fio;
@ -42,22 +42,25 @@ public class WorkerDataModel : IValidation
if (FIO.IsEmpty())
throw new ValidationException("Field FIO is empty");
if (Email.IsEmpty())
throw new ValidationException("Field Email is empty");
if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
throw new ValidationException("Field Email is not a valid email address");
if (PostId.IsEmpty())
throw new ValidationException("Field PostId is empty");
if (!PostId.IsGuid())
throw new ValidationException("The value in the field PostId is not a unique identifier");
if (BirthDate.Date > DateTime.Now.AddYears(-16).Date)
throw new ValidationException($"Minors cannot be hired (BirthDate = { BirthDate.ToShortDateString() })");
if (BirthDate.Date > DateTime.Now.AddYears(-18).Date)
throw new ValidationException($"Only adults can 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) // EmploymentDate.Year - BirthDate.Year
throw new ValidationException($"Minors cannot be hired (EmploymentDate - { EmploymentDate.ToShortDateString() }, BirthDate - { BirthDate.ToShortDateString()})");
if (!Regex.IsMatch(Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")) // example@example.com
throw new ValidationException("Field Email is not a email");
if ((EmploymentDate - BirthDate).TotalDays / 365 < 18)
throw new ValidationException($"Only adults can be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})");
}
}

View File

@ -583,7 +583,7 @@ internal class WorkerBusinessLogicContractTests
//Arrange
var flag = false;
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "example@example.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now,
false);
_workerStorageContract.Setup(x =>
x.AddElement(It.IsAny<WorkerDataModel>()))
@ -612,7 +612,7 @@ internal class WorkerBusinessLogicContractTests
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<ElementExistsException>());
_workerStorageContract.Verify(x =>
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
@ -649,7 +649,7 @@ internal class WorkerBusinessLogicContractTests
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x =>
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
@ -661,7 +661,7 @@ internal class WorkerBusinessLogicContractTests
//Arrange
var flag = false;
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "example@example.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now,
false);
_workerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()))
@ -690,7 +690,7 @@ internal class WorkerBusinessLogicContractTests
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
@ -727,7 +727,7 @@ internal class WorkerBusinessLogicContractTests
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "example@example.com",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-18).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);

View File

@ -99,26 +99,26 @@ internal class WorkerDataModelTests
{
var workerId = Guid.NewGuid().ToString();
var fio = "fio";
var email = "example@example.com";
var Email = "example@example.com";
var postId = Guid.NewGuid().ToString();
var birthDate = DateTime.Now.AddYears(-16).AddDays(-1);
var birthDate = DateTime.Now.AddYears(-18).AddDays(-1);
var employmentDate = DateTime.Now;
var isDelete = false;
var worker = CreateDataModel(workerId, fio, email, postId, birthDate, employmentDate, isDelete);
var worker = CreateDataModel(workerId, fio, Email, postId, birthDate, employmentDate, isDelete);
Assert.That(() => worker.Validate(), Throws.Nothing);
Assert.Multiple(() =>
{
Assert.That(worker.Id, Is.EqualTo(workerId));
Assert.That(worker.FIO, Is.EqualTo(fio));
Assert.That(worker.Email, Is.EqualTo(email));
Assert.That(worker.Email, Is.EqualTo(Email));
Assert.That(worker.PostId, Is.EqualTo(postId));
Assert.That(worker.BirthDate, Is.EqualTo(birthDate));
Assert.That(worker.EmploymentDate,Is.EqualTo(employmentDate));
Assert.That(worker.EmploymentDate, Is.EqualTo(employmentDate));
Assert.That(worker.IsDeleted, Is.EqualTo(isDelete));
});
}
private static WorkerDataModel CreateDataModel(string? id, string? fio, string email,
private static WorkerDataModel CreateDataModel(string? id, string? fio, string? email,
string? postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) =>
new(id, fio, email, postId, birthDate, employmentDate, isDeleted);
}