в процессе
This commit is contained in:
parent
e6042c4137
commit
c809d02e7b
@ -11,7 +11,7 @@ public interface IPostStorageContract
|
||||
{
|
||||
List<PostDataModel> GetList(bool onlyActual = true);
|
||||
List<PostDataModel> GetPostWithHistory(string postId);
|
||||
PostDataModel? GetElementById(int id);
|
||||
PostDataModel? GetElementById(string id);
|
||||
PostDataModel? GetElementByName(string name);
|
||||
void AddElement(PostDataModel postDataModel);
|
||||
void UpdElement(PostDataModel postDataModel);
|
||||
|
@ -15,4 +15,6 @@ public interface IProductStorageContract
|
||||
ProductDataModel? GetElementByName(string name);
|
||||
void AddElement(ProductDataModel productDataModel);
|
||||
void UpdElement(ProductDataModel productDataModel);
|
||||
void DelElement(string id);
|
||||
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ public interface IWorkerStorageContract
|
||||
List<WorkerDataModel> GetList(bool onlyActive = true, string? postId = null, DateTime? fromBirtDate = null, DateTime? toBirtDate = null, DateTime? fromEmploymentDate = null, DateTime? toEmploymentDate = null);
|
||||
WorkerDataModel? GetElementById(string id);
|
||||
WorkerDataModel? GetElementByFIO(string fio);
|
||||
WorkerDataModel? GetElementByEmail(string email);
|
||||
void AddElement(WorkerDataModel workerDataModel);
|
||||
void UpdElement(WorkerDataModel woworkerDataModel);
|
||||
void DelElement(string id);
|
||||
|
@ -2,47 +2,99 @@
|
||||
using PipingHotContrast.BusinessLogicsContracts;
|
||||
using PipingHotContrast.DataModels;
|
||||
using PipingHotContrast.Enums;
|
||||
using PipingHotContrast.Exceptions;
|
||||
using PipingHotContrast.Extensions;
|
||||
using PipingHotContrast.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotBusinessLogic.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)
|
||||
{
|
||||
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("New data: {json}",JsonSerializer.Serialize(postDataModel));
|
||||
ArgumentNullException.ThrowIfNull(postDataModel);
|
||||
postDataModel.Validate();
|
||||
_postStorageContract.AddElement(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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -13,6 +13,8 @@ namespace PipingHotBusinessLogic.Implementations;
|
||||
|
||||
internal class ProductBusinessLogicContract(IProductStorageContract productStorageContract, ILogger logger) : IProductBusinessLogicContract
|
||||
{
|
||||
private readonly ILogger _logger = logger;
|
||||
private readonly IProductStorageContract _productStorageContract = productStorageContract;
|
||||
public List<ProductDataModel> GetAllProducts(bool onlyActive = true)
|
||||
{
|
||||
return [];
|
||||
|
@ -2,11 +2,14 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PipingHotContrast.BusinessLogicsContracts;
|
||||
using PipingHotContrast.DataModels;
|
||||
using PipingHotContrast.Exceptions;
|
||||
using PipingHotContrast.Extensions;
|
||||
using PipingHotContrast.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotBusinessLogic.Implementations;
|
||||
@ -18,25 +21,54 @@ internal class RestaurantBusinessLogicContract(IRestaurantStorageContract resta
|
||||
|
||||
public List<RestaurantDataModel> GetAllRestaurants()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("GetAllRestaurants");
|
||||
return _restaurantStorageContract.GetList() ?? throw new NullListException();
|
||||
|
||||
}
|
||||
|
||||
public RestaurantDataModel GetRestaurantByData(string data)
|
||||
{
|
||||
return new("", "", null, null);
|
||||
_logger.LogInformation("Get element by data: {data}", data);
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
if (data.IsGuid())
|
||||
{
|
||||
return _restaurantStorageContract.GetElementById(data) ??
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
return _restaurantStorageContract.GetElementByName(data) ??
|
||||
_restaurantStorageContract.GetElementByOldName(data) ??
|
||||
throw new ElementNotFoundException(data);
|
||||
}
|
||||
|
||||
public void InsertRestaurant(RestaurantDataModel restaurantDataModel)
|
||||
{
|
||||
|
||||
logger.LogInformation("New data: {json}", JsonSerializer.Serialize(restaurantDataModel));
|
||||
ArgumentNullException.ThrowIfNull(restaurantDataModel);
|
||||
restaurantDataModel.Validate();
|
||||
_restaurantStorageContract.AddElement(restaurantDataModel);
|
||||
}
|
||||
|
||||
public void UpdateRestaurant(RestaurantDataModel restaurantDataModel)
|
||||
{
|
||||
|
||||
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(restaurantDataModel));
|
||||
ArgumentNullException.ThrowIfNull(restaurantDataModel);
|
||||
restaurantDataModel.Validate();
|
||||
_restaurantStorageContract.UpdElement(restaurantDataModel);
|
||||
}
|
||||
public void DeleteRestaurant(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");
|
||||
}
|
||||
_restaurantStorageContract.DelElement(id);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,636 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PipingHotBusinessLogic.Implementations;
|
||||
using PipingHotContrast.BusinessLogicsContracts;
|
||||
using PipingHotContrast.DataModels;
|
||||
using PipingHotContrast.Exceptions;
|
||||
using PipingHotContrast.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotTests.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()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
var listOriginal = new List<OrderDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false,
|
||||
[new OrderProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false, []),
|
||||
};
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByPeriod(date, date.AddDays(1));
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||
//Assert
|
||||
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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByPeriod_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllOrdersByWorkerByPeriod_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<OrderDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false,
|
||||
[new OrderProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false, []),
|
||||
};
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
|
||||
var list =_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(workerId, date, date.AddDays(1));
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), workerId, null, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByWorkerByPeriod_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||
//Assert
|
||||
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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByWorkerByPeriod_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByWorkerByPeriod_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByWorkerByPeriod_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByWorkerByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByRestaurantByPeriod_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
var restaurantId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<OrderDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false,
|
||||
[new OrderProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false, []),
|
||||
};
|
||||
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(restaurantId, date,date.AddDays(1));
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, restaurantId, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByRestaurantByPeriod_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list =
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||
//Assert
|
||||
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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByRestaurantByPeriod_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(Guid.NewGuid().ToString(),date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByRestaurantByPeriod_RestaurantIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByRestaurantByPeriod_RestaurantIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod("restaurantId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByRestaurantByPeriod_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByRestaurantByPeriod_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByRestaurantByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByProductByPeriod_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<OrderDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false,
|
||||
[new OrderProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10,false, []),
|
||||
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
|
||||
null, 10, false, []),
|
||||
};
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list =
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod(productId, date,
|
||||
date.AddDays(1));
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_orderStorageContract.Verify(x => x.GetList(date, date.AddDays(1),
|
||||
null, null, productId), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByProductByPeriod_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString()
|
||||
, DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
|
||||
//Assert
|
||||
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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByProductByPeriod_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByProductByPeriod_ProductIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod(null, DateTime.UtcNow,DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByProductByPeriod_ProductIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod("productId",DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByProductByPeriod_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllOrdersByProductByPeriod_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.GetAllOrdersByProductByPeriod(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>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetOrderByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new OrderDataModel(id, Guid.NewGuid().ToString(), null,
|
||||
10, false,
|
||||
[new OrderProductDataModel(Guid.NewGuid().ToString(),Guid.NewGuid().ToString(), 5)]);
|
||||
_orderStorageContract.Setup(x =>x.GetElementById(id)).Returns(record);
|
||||
|
||||
//Act
|
||||
var element = _orderBusinessLogicContract.GetOrderByData(id);
|
||||
//Assert
|
||||
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()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _orderBusinessLogicContract.GetOrderByData("saleId"),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_orderStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetOrderByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x =>
|
||||
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
|
||||
var record = new OrderDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10,
|
||||
false, [new OrderProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
|
||||
_orderStorageContract.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
|
||||
.Callback((OrderDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.WorkerId ==
|
||||
record.WorkerId && x.RestaurantId == record.RestaurantId &&
|
||||
x.OrderDate == record.OrderDate && x.Sum ==
|
||||
record.Sum &&
|
||||
x.IsCancel == record.IsCancel && x.Products.Count == record.Products.Count &&
|
||||
x.Products.First().ProductId ==
|
||||
record.Products.First().ProductId &&
|
||||
x.Products.First().OrderId ==
|
||||
record.Products.First().OrderId &&
|
||||
x.Products.First().Count ==
|
||||
record.Products.First().Count;
|
||||
});
|
||||
//Act
|
||||
_orderBusinessLogicContract.InsertOrder(record);
|
||||
//Assert
|
||||
_orderStorageContract.Verify(x =>x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void InsertOrder_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.AddElement(It.IsAny<OrderDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.InsertOrder(new(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 10, false,
|
||||
[new OrderProductDataModel(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()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _orderBusinessLogicContract.InsertOrder(new
|
||||
OrderDataModel("id", Guid.NewGuid().ToString(), 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()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.AddElement(It.IsAny<OrderDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.InsertOrder(new(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 10, false,
|
||||
[new OrderProductDataModel(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()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_orderStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_orderBusinessLogicContract.CancelOrder(id);
|
||||
//Assert
|
||||
_orderStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void CancelOrder_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_orderStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Arrange
|
||||
_orderStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_orderBusinessLogicContract.CancelOrder(Guid.NewGuid().ToString()),Throws.TypeOf<StorageException>());
|
||||
_orderStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,462 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PipingHotBusinessLogic.Implementations;
|
||||
using PipingHotContrast.BusinessLogicsContracts;
|
||||
using PipingHotContrast.DataModels;
|
||||
using PipingHotContrast.Enums;
|
||||
using PipingHotContrast.Exceptions;
|
||||
using PipingHotContrast.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotTests.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class PostBusinessLogicContractTests
|
||||
{
|
||||
private PostBusinessLogicContract _postBusinessLogicContract;
|
||||
private Mock<IPostStorageContract> _postStorageContract;
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_postStorageContract = new Mock<IPostStorageContract>();
|
||||
_postBusinessLogicContract = new PostBusinessLogicContract(_postStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_postStorageContract.Reset();
|
||||
}
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(),"name 1", PostType.Operator, 10, true, DateTime.UtcNow),
|
||||
new(Guid.NewGuid().ToString(), "name 2", PostType.Operator, 10, false, DateTime.UtcNow),
|
||||
new(Guid.NewGuid().ToString(), "name 3", PostType.Operator, 10, true, DateTime.UtcNow),
|
||||
};
|
||||
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
|
||||
var listAll = _postBusinessLogicContract.GetAllPosts(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(listAll, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(listAll, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_postStorageContract.Verify(x => x.GetList(true), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetList(false), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
|
||||
//Act
|
||||
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
|
||||
var listAll = _postBusinessLogicContract.GetAllPosts(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(listAll, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(listAll, Has.Count.EqualTo(0));
|
||||
});
|
||||
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllPosts_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()),Throws.TypeOf<NullListException>());
|
||||
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllPosts_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(postId, "name 1", PostType.Operator, 10, true,
|
||||
DateTime.UtcNow),
|
||||
new(postId, "name 2", PostType.Operator, 10, false,
|
||||
DateTime.UtcNow)
|
||||
};
|
||||
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllDataOfPost(postId);
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(postId), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString());
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null),Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllDataOfPost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
|
||||
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPostByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new PostDataModel(id, "name", PostType.Operator, 10,true, DateTime.UtcNow);
|
||||
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _postBusinessLogicContract.GetPostByData(id);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
|
||||
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_GetByName_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postName = "name";
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Operator, 10, true, DateTime.UtcNow);
|
||||
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
|
||||
//Act
|
||||
var element = _postBusinessLogicContract.GetPostByData(postName);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.PostName, Is.EqualTo(postName));
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetPostByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_postStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_postStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Deliveryman, 10, true, DateTime.UtcNow.AddDays(-1));
|
||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
|
||||
.Callback((PostDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary && x.ChangeDate == record.ChangeDate;
|
||||
});
|
||||
//Act
|
||||
_postBusinessLogicContract.InsertPost(record);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertPost_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Deliveryman, 10, true, DateTime.UtcNow)),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(null),Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Deliveryman, 10, true, DateTime.UtcNow)),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void InsertPost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Deliveryman, 10, true, DateTime.UtcNow)),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x =>x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_CorrectRecord_Test()
|
||||
{
|
||||
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Deliveryman, 10, true, DateTime.UtcNow.AddDays(-1));
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
|
||||
|
||||
.Callback((PostDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.PostName ==
|
||||
record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
|
||||
x.ChangeDate == record.ChangeDate;
|
||||
});
|
||||
//Act
|
||||
_postBusinessLogicContract.UpdatePost(record);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Deliveryman, 10, true, DateTime.UtcNow)),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Deliveryman, 10, true, DateTime.UtcNow)),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void UpdatePost_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Deliveryman, 10, true, DateTime.UtcNow)),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdatePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Deliveryman, 10, true, DateTime.UtcNow)),Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_postBusinessLogicContract.DeletePost(id);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeletePost_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void DeletePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_postBusinessLogicContract.RestorePost(id);
|
||||
//Assert
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(null),
|
||||
Throws.TypeOf<ArgumentNullException>());
|
||||
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()),Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RestorePost_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost("id"), Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void RestorePost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_postStorageContract.Setup(x => x.ResElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,518 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PipingHotBusinessLogic.Implementations;
|
||||
using PipingHotContrast.DataModels;
|
||||
using PipingHotContrast.Enums;
|
||||
using PipingHotContrast.Exceptions;
|
||||
using PipingHotContrast.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotTests.BusinessLogicsContractsTests;
|
||||
[TestFixture]
|
||||
internal class ProductBusinessLogicContractTests
|
||||
{
|
||||
private ProductBusinessLogicContract _productBusinessLogicContract;
|
||||
private Mock<IProductStorageContract> _productStorageContract;
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_productStorageContract = new Mock<IProductStorageContract>();
|
||||
_productBusinessLogicContract = new ProductBusinessLogicContract(_productStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_productStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllProducts_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var listOriginal = new List<ProductDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "name 1",
|
||||
ProductType.Pizza, Guid.NewGuid().ToString(), 10, false),
|
||||
new(Guid.NewGuid().ToString(), "name 2",
|
||||
ProductType.Pizza, Guid.NewGuid().ToString(), 10, true),
|
||||
new(Guid.NewGuid().ToString(), "name 3",
|
||||
ProductType.Pizza, Guid.NewGuid().ToString(), 10, false),
|
||||
};
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
|
||||
var list = _productBusinessLogicContract.GetAllProducts(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_productStorageContract.Verify(x => x.GetList(true, null), Times.Once);
|
||||
_productStorageContract.Verify(x => x.GetList(false, null),Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProducts_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
|
||||
var list = _productBusinessLogicContract.GetAllProducts(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllProducts_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllProducts_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProductsByRestaurant_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var restaurantId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<ProductDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "name 1",ProductType.Pizza,
|
||||
Guid.NewGuid().ToString(), 10, false),
|
||||
new(Guid.NewGuid().ToString(), "name 2",
|
||||
ProductType.Pizza, Guid.NewGuid().ToString(), 10, true),
|
||||
new(Guid.NewGuid().ToString(), "name 3",
|
||||
ProductType.Pizza, Guid.NewGuid().ToString(), 10, false),
|
||||
};
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, true);
|
||||
var list = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_productStorageContract.Verify(x => x.GetList(true, restaurantId), Times.Once);
|
||||
_productStorageContract.Verify(x => x.GetList(false, restaurantId), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllProductsByRestaurant_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
var restaurantId = Guid.NewGuid().ToString();
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
|
||||
var listOnlyActive = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, true);
|
||||
var list = _productBusinessLogicContract.GetAllProductsByRestaurant(restaurantId, false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),restaurantId), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllProductsByRestaurant_RestaurantIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProductsByRestaurant(null, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProductsByRestaurant(string.Empty,It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProductsByRestaurant_RestaurantIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProductsByRestaurant("manufacturerId",It.IsAny<bool>()), Throws.TypeOf<ValidationException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProductsByRestaurant_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProductsByRestaurant(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProductsByRestaurant_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProductsByRestaurant(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetProductHistoryByProduct_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<ProductHistoryDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), 10),
|
||||
new(Guid.NewGuid().ToString(), 15),
|
||||
new(Guid.NewGuid().ToString(), 10),
|
||||
};
|
||||
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list =
|
||||
_productBusinessLogicContract.GetProductHistoryByProduct(productId);
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_productStorageContract.Verify(x => x.GetHistoryByProductId(productId), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductHistoryByProduct_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString());
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductHistoryByProduct_ProductIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductHistoryByProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductHistoryByProduct(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductHistoryByProduct("productId"), Throws.TypeOf<ValidationException>());
|
||||
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
||||
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductHistoryByProduct_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new ProductDataModel(id, "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
|
||||
_productStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _productBusinessLogicContract.GetProductByData(id);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductByData_GetByName_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var name = "name";
|
||||
var record = new ProductDataModel(Guid.NewGuid().ToString(), name, ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
|
||||
|
||||
_productStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
|
||||
//Act
|
||||
var element = _productBusinessLogicContract.GetProductByData(name);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.ProductName, Is.EqualTo(name));
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetProductByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductByData(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductByData_GetByName_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductByData("name"),Throws.TypeOf<ElementNotFoundException>());
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetProductByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_productStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetProductByData("name"), Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertProduct_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
|
||||
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
|
||||
.Callback((ProductDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.ProductName ==
|
||||
record.ProductName && x.ProductType == record.ProductType &&
|
||||
x.RestaurantId == record.RestaurantId && x.Price ==
|
||||
record.Price && x.IsDeleted == record.IsDeleted;
|
||||
});
|
||||
//Act
|
||||
_productBusinessLogicContract.InsertProduct(record);
|
||||
//Assert
|
||||
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void InsertProduct_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void InsertProduct_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _productBusinessLogicContract.InsertProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void InsertProduct_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
|
||||
Assert.That(() => _productBusinessLogicContract.InsertProduct(new ProductDataModel("id", "name", ProductType.Pizza, Guid.NewGuid().ToString(),10, false)), Throws.TypeOf<ValidationException>());
|
||||
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertProduct_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.AddElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateProduct_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new ProductDataModel(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false);
|
||||
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>()))
|
||||
.Callback((ProductDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.ProductName ==
|
||||
record.ProductName && x.ProductType == record.ProductType &&
|
||||
x.RestaurantId == record.RestaurantId && x.Price ==
|
||||
record.Price && x.IsDeleted == record.IsDeleted;
|
||||
});
|
||||
//Act
|
||||
_productBusinessLogicContract.UpdateProduct(record);
|
||||
//Assert
|
||||
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(),"name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateProduct_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateProduct_NullRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _productBusinessLogicContract.UpdateProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateProduct_InvalidRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _productBusinessLogicContract.UpdateProduct(new ProductDataModel("id", "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ValidationException>());
|
||||
_productStorageContract.Verify(x =>x.UpdElement(It.IsAny<ProductDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateProduct_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", ProductType.Pizza, Guid.NewGuid().ToString(), 10, false)),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.UpdElement(It.IsAny<ProductDataModel>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteProduct_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_productStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_productBusinessLogicContract.DeleteProduct(id);
|
||||
//Assert
|
||||
|
||||
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void DeleteProduct_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _productBusinessLogicContract.DeleteProduct(null), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.DeleteProduct(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
||||
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void DeleteProduct_IdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _productBusinessLogicContract.DeleteProduct("id"), Throws.TypeOf<ValidationException>());
|
||||
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void DeleteProduct_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -14,6 +14,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotTests.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class RestaurantBusinessLogicsContractsTests
|
||||
{
|
||||
private RestaurantBusinessLogicContract _restaurantBusinessLogicContract;
|
||||
|
@ -0,0 +1,423 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PipingHotBusinessLogic.Implementations;
|
||||
using PipingHotContrast.DataModels;
|
||||
using PipingHotContrast.Enums;
|
||||
using PipingHotContrast.Exceptions;
|
||||
using PipingHotContrast.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotTests.BusinessLogicsContractsTests;
|
||||
|
||||
[TestFixture]
|
||||
internal class SalaryBusinessLogicContractTests
|
||||
{
|
||||
private SalaryBusinessLogicContract _salaryBusinessLogicContract;
|
||||
private Mock<ISalaryStorageContract> _salaryStorageContract;
|
||||
private Mock<IOrderStorageContract> _orderStorageContract;
|
||||
private Mock<IPostStorageContract> _postStorageContract;
|
||||
private Mock<IWorkerStorageContract> _workerStorageContract;
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
_salaryStorageContract = new Mock<ISalaryStorageContract>();
|
||||
_orderStorageContract = new Mock<IOrderStorageContract>();
|
||||
_postStorageContract = new Mock<IPostStorageContract>();
|
||||
_workerStorageContract = new Mock<IWorkerStorageContract>();
|
||||
_salaryBusinessLogicContract = new
|
||||
SalaryBusinessLogicContract(_salaryStorageContract.Object,
|
||||
_orderStorageContract.Object, _postStorageContract.Object,
|
||||
_workerStorageContract.Object, new Mock<ILogger>().Object);
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_salaryStorageContract.Reset();
|
||||
_orderStorageContract.Reset();
|
||||
_postStorageContract.Reset();
|
||||
_workerStorageContract.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSalaries_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var startDate = DateTime.UtcNow;
|
||||
var endDate = DateTime.UtcNow.AddDays(1);
|
||||
var listOriginal = new List<SalaryDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
|
||||
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1),
|
||||
14),
|
||||
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1),
|
||||
30),
|
||||
};
|
||||
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriod(startDate, endDate);
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalaries_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list =
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow,
|
||||
DateTime.UtcNow.AddDays(1));
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalaries_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var dateTime = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalaries_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalaries_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllSalariesByWorker_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var startDate = DateTime.UtcNow;
|
||||
var endDate = DateTime.UtcNow.AddDays(1);
|
||||
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<SalaryDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
|
||||
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1),
|
||||
14),
|
||||
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1),
|
||||
30),
|
||||
};
|
||||
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
//Act
|
||||
var list =
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(startDate, endDate, workerId);
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate, workerId), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalariesByWorker_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var list = _salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString());
|
||||
//Assert
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalariesByWorker_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var dateTime = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime, Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime.AddSeconds(-1), Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<IncorrectDatesException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalariesByWorker_WorkerIdIsNUllOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), null), Throws.TypeOf<ArgumentNullException>());
|
||||
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), string.Empty),
|
||||
Throws.TypeOf<ArgumentNullException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalariesByWorker_WorkerIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow,DateTime.UtcNow.AddDays(1), "workerId"), Throws.TypeOf<ValidationException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalariesByWorker_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<NullListException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllSalariesByWorker_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_CalculateSalary_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
var orderSum = 200.0;
|
||||
var postSalary = 2000.0;
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, orderSum, false, [])]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
|
||||
PostType.Operator, postSalary, true, DateTime.UtcNow));
|
||||
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||
var sum = 0.0;
|
||||
var expectedSum = postSalary + orderSum * 0.1;
|
||||
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
|
||||
.Callback((SalaryDataModel x) =>
|
||||
{
|
||||
sum = x.Salary;
|
||||
});
|
||||
//Act
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||
//Assert
|
||||
Assert.That(sum, Is.EqualTo(expectedSum));
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_WithSeveralWorkers_Test()
|
||||
{
|
||||
//Arrange
|
||||
var worker1Id = Guid.NewGuid().ToString();
|
||||
var worker2Id = Guid.NewGuid().ToString();
|
||||
var worker3Id = Guid.NewGuid().ToString();
|
||||
var list = new List<WorkerDataModel>() {
|
||||
new(worker1Id, "Test","Test", Guid.NewGuid().ToString(),
|
||||
DateTime.UtcNow, DateTime.UtcNow, false),
|
||||
new(worker2Id, "Test","Test", Guid.NewGuid().ToString(),
|
||||
DateTime.UtcNow, DateTime.UtcNow, false),
|
||||
new(worker3Id, "Test","Test", Guid.NewGuid().ToString(),
|
||||
DateTime.UtcNow, DateTime.UtcNow, false)
|
||||
};
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns([new OrderDataModel(Guid.NewGuid().ToString(),
|
||||
worker1Id, null, 1, false, []),
|
||||
new OrderDataModel(Guid.NewGuid().ToString(), worker1Id, null,
|
||||
1, false, []),
|
||||
new OrderDataModel(Guid.NewGuid().ToString(), worker2Id, null,
|
||||
1, false, []),
|
||||
new OrderDataModel(Guid.NewGuid().ToString(), worker3Id, null,
|
||||
1, false, []),
|
||||
new OrderDataModel(Guid.NewGuid().ToString(), worker3Id, null,
|
||||
1, false, [])]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",PostType.Operator, 2000, true, DateTime.UtcNow));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns(list);
|
||||
//Act
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||
//Assert
|
||||
_salaryStorageContract.Verify(x => x.AddElement(It.IsAny<SalaryDataModel>()), Times.Exactly(list.Count));
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_WithoitOrdersByWorker_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postSalary = 2000.0;
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns([]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
|
||||
PostType.Operator, postSalary, true, DateTime.UtcNow));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test","Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||
var sum = 0.0;
|
||||
var expectedSum = postSalary;
|
||||
_salaryStorageContract.Setup(x =>x.AddElement(It.IsAny<SalaryDataModel>()))
|
||||
.Callback((SalaryDataModel x) =>
|
||||
{
|
||||
sum = x.Salary;
|
||||
});
|
||||
//Act
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
|
||||
//Assert
|
||||
Assert.That(sum, Is.EqualTo(expectedSum));
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_OrderStorageReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
|
||||
PostType.Operator, 2000, true, DateTime.UtcNow));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf<NullListException>());
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_PostStorageReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, 200, false, [])]);
|
||||
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test","Test", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_WorkerStorageReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, 200, false, [])]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Operator, 2000, true, DateTime.UtcNow));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf<NullListException>());
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_OrderStorageThrowException_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Throws(new StorageException(new InvalidOperationException()));
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
|
||||
PostType.Operator, 2000, true, DateTime.UtcNow));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf<StorageException>());
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_PostStorageThrowException_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns([new OrderDataModel(Guid.NewGuid().ToString(), workerId, null, 200,false, [])]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Throws(new StorageException(new InvalidOperationException()));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Returns([new WorkerDataModel(workerId, "Test","Test",Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf<StorageException>());
|
||||
}
|
||||
[Test]
|
||||
public void CalculateSalaryByMounth_WorkerStorageThrowException_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
_orderStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
|
||||
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns([new OrderDataModel(Guid.NewGuid().ToString(),workerId, null, 200, false, [])]);
|
||||
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
|
||||
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
|
||||
PostType.Operator, 2000, true, DateTime.UtcNow));
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
||||
.Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow),Throws.TypeOf<StorageException>());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,746 @@
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PipingHotBusinessLogic.Implementations;
|
||||
using PipingHotContrast.DataModels;
|
||||
using PipingHotContrast.Exceptions;
|
||||
using PipingHotContrast.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PipingHotTests.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()
|
||||
{
|
||||
//Arrange
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1","email1",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2","email2",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3","email3",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, null, null, null,
|
||||
null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, null, null, null,
|
||||
null, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkers_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
//Act
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkers(true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null,
|
||||
null, null, null, null), Times.Exactly(2));
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkers_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()),
|
||||
Throws.TypeOf<NullListException>());
|
||||
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAllWorkers_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null,
|
||||
null, null, null, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByPost_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1","email 1",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2","email 2",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3","email 3",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive =
|
||||
_workerBusinessLogicContract.GetAllWorkersByPost(postId, true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByPost(postId,
|
||||
false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, postId, null,
|
||||
null, null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, postId, null,
|
||||
null, null, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByPost_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
//Act
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Exactly(2));
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByPost_PostIdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkersByPost(null, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkersByPost(string.Empty, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByPost_PostIdIsNotGuid_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkersByPost("postId", It.IsAny<bool>()),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByPost_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(),
|
||||
It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByPost_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(),It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1","email 1",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2","email 2",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3","email 3",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1), false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, null, date,
|
||||
date.AddDays(1), null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, null, date,
|
||||
date.AddDays(1), null, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
//Act
|
||||
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow,DateTime.UtcNow.AddDays(1), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkers(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, null,
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, null,
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
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<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
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<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
var listOriginal = new List<WorkerDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "fio 1","email 1",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
new(Guid.NewGuid().ToString(), "fio 2","email 2",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
true),
|
||||
new(Guid.NewGuid().ToString(), "fio 3","email 3",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false),
|
||||
};
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1),true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1), false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, null, null, null,
|
||||
date, date.AddDays(1)), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, null, null, null,
|
||||
date, date.AddDays(1)), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
|
||||
//Act
|
||||
var listOnlyActive = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true);
|
||||
var list = _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_workerStorageContract.Verify(x => x.GetList(true, null, null, null,
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetList(false, null, null, null,
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var date = DateTime.UtcNow;
|
||||
//Act&Assert
|
||||
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<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
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<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny<bool>()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetWorkerByData_GetById_ReturnRecord_Test()
|
||||
{
|
||||
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new WorkerDataModel(id, "fio","email",Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false);
|
||||
_workerStorageContract.Setup(x =>x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
var element = _workerBusinessLogicContract.GetWorkerByData(id);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Id, Is.EqualTo(id));
|
||||
_workerStorageContract.Verify(x =>
|
||||
x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetWorkerByData_GetByFio_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var fio = "fio";
|
||||
var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio,"email",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false);
|
||||
_workerStorageContract.Setup(x =>
|
||||
x.GetElementByFIO(fio)).Returns(record);
|
||||
//Act
|
||||
var element = _workerBusinessLogicContract.GetWorkerByData(fio);
|
||||
//Assert
|
||||
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_GetByEmail_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var email = "user@example.com";
|
||||
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio",
|
||||
email, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false);
|
||||
_workerStorageContract.Setup(x => x.GetElementByEmail(email)).Returns(record);
|
||||
|
||||
//Act
|
||||
|
||||
var element = _workerBusinessLogicContract.GetWorkerByData(email);
|
||||
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.Email, Is.EqualTo(email));
|
||||
_workerStorageContract.Verify(x =>x.GetElementByEmail(It.IsAny<string>()), Times.Once);
|
||||
|
||||
}
|
||||
[Test]
|
||||
public void GetWorkerByData_EmptyData_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
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);
|
||||
_workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
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);
|
||||
_workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
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);
|
||||
_workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
public void GetWorkerByData_GetByEmail_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetWorkerByData("email"), Throws.TypeOf<ElementNotFoundException>());
|
||||
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
||||
_workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
|
||||
_workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetWorkerByData_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_workerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
_workerStorageContract.Setup(x => x.GetElementByEmail(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf<StorageException>());
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.GetWorkerByData("email"), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
||||
_workerStorageContract.Verify(x =>x.GetElementByFIO(It.IsAny<string>()), Times.Once);
|
||||
_workerStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void InsertWorker_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio","email", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).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.Email == record.Email &&
|
||||
x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
|
||||
x.EmploymentDate == record.EmploymentDate &&
|
||||
x.IsDeleted == record.IsDeleted;
|
||||
});
|
||||
//Act
|
||||
_workerBusinessLogicContract.InsertWorker(record);
|
||||
//Assert
|
||||
_workerStorageContract.Verify(x =>
|
||||
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void InsertWorker_RecordWithExistsData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
|
||||
_workerStorageContract.Setup(x => x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio","email",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).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()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new WorkerDataModel("id", "fio","email", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-
|
||||
16).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()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x =>
|
||||
x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio","email",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false)), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateWorker_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio","email",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).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.Email == record.Email &&
|
||||
x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
|
||||
|
||||
x.EmploymentDate == record.EmploymentDate &&
|
||||
x.IsDeleted == record.IsDeleted;
|
||||
});
|
||||
//Act
|
||||
_workerBusinessLogicContract.UpdateWorker(record);
|
||||
//Assert
|
||||
_workerStorageContract.Verify(x =>x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x => x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new ElementNotFoundException(""));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio","email",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).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()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new
|
||||
WorkerDataModel("id", "fio","email", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-
|
||||
16).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()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x =>
|
||||
x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio","email",
|
||||
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
|
||||
false)), Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void DeleteWorker_CorrectRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var flag = false;
|
||||
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
|
||||
== id))).Callback(() => { flag = true; });
|
||||
//Act
|
||||
_workerBusinessLogicContract.DeleteWorker(id);
|
||||
//Assert
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),Times.Once);
|
||||
Assert.That(flag);
|
||||
}
|
||||
[Test]
|
||||
public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
|
||||
!= id))).Throws(new ElementNotFoundException(id));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
|
||||
Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _workerBusinessLogicContract.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()
|
||||
{
|
||||
//Act&Assert
|
||||
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()
|
||||
{
|
||||
//Arrange
|
||||
_workerStorageContract.Setup(x =>
|
||||
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
||||
//Act&Assert
|
||||
|
||||
Assert.That(() =>
|
||||
_workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()),Throws.TypeOf<StorageException>());
|
||||
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user