476 lines
19 KiB
C#
476 lines
19 KiB
C#
|
using Moq;
|
||
|
using NUnit.Framework;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using CandyHouseBase.DataModels;
|
||
|
using CandyHouseBase.Enums;
|
||
|
using CandyHouseBase.Exceptions;
|
||
|
using CandyHouseBase.Implementations;
|
||
|
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
|
||
|
using CandyHouseBase.Interfaces.StoragesContracts;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
|
||
|
namespace CandyHouseTests.BusinessLogicsContractsTests
|
||
|
{
|
||
|
[TestFixture]
|
||
|
internal class OrderBusinessLogicContractTests
|
||
|
{
|
||
|
private OrderBusinessLogicContract _orderBusinessLogicContract;
|
||
|
private Mock<IOrderStorageContact> _orderStorageContact;
|
||
|
private Mock<IPekarStorageContact> _pekarStorageContact;
|
||
|
private Mock<IProductStorageContact> _productStorageContact;
|
||
|
|
||
|
[OneTimeSetUp]
|
||
|
public void OneTimeSetUp()
|
||
|
{
|
||
|
_orderStorageContact = new Mock<IOrderStorageContact>();
|
||
|
_pekarStorageContact = new Mock<IPekarStorageContact>();
|
||
|
_productStorageContact = new Mock<IProductStorageContact>();
|
||
|
_orderBusinessLogicContract = new OrderBusinessLogicContract(
|
||
|
_orderStorageContact.Object,
|
||
|
_pekarStorageContact.Object,
|
||
|
_productStorageContact.Object,
|
||
|
new Mock<ILogger>().Object
|
||
|
);
|
||
|
}
|
||
|
|
||
|
[SetUp]
|
||
|
public void SetUp()
|
||
|
{
|
||
|
_orderStorageContact.Reset();
|
||
|
_pekarStorageContact.Reset();
|
||
|
_productStorageContact.Reset();
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void GetAllOrders_ReturnsListOfRecords_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var pekarId = Guid.NewGuid().ToString();
|
||
|
var productId = Guid.NewGuid().ToString();
|
||
|
var orders = new List<OrderDataModel>
|
||
|
{
|
||
|
new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"John Doe",
|
||
|
DateTime.Now.AddDays(-1),
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
productId,
|
||
|
pekarId,
|
||
|
StatusType.Pending
|
||
|
),
|
||
|
new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"Jane Smith",
|
||
|
DateTime.Now,
|
||
|
200.00m,
|
||
|
20.00m,
|
||
|
productId,
|
||
|
pekarId,
|
||
|
StatusType.Completed
|
||
|
)
|
||
|
};
|
||
|
_orderStorageContact.Setup(x => x.GetOrders()).Returns(orders);
|
||
|
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
|
||
|
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
|
||
|
_productStorageContact.Setup(x => x.GetElementById(productId))
|
||
|
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
|
||
|
|
||
|
// Act
|
||
|
var list = _orderBusinessLogicContract.GetAllOrders();
|
||
|
|
||
|
// Assert
|
||
|
Assert.That(list, Is.Not.Null);
|
||
|
Assert.That(list, Is.EquivalentTo(orders));
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void GetAllOrders_ReturnsEmptyList_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
_orderStorageContact.Setup(x => x.GetOrders()).Returns(new List<OrderDataModel>());
|
||
|
|
||
|
// Act
|
||
|
var list = _orderBusinessLogicContract.GetAllOrders();
|
||
|
|
||
|
// Assert
|
||
|
Assert.That(list, Is.Not.Null);
|
||
|
Assert.That(list, Has.Count.EqualTo(0));
|
||
|
_orderStorageContact.Verify(x => x.GetOrders(), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void GetAllOrders_ReturnsNull_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
_orderStorageContact.Setup(x => x.GetOrders()).Returns((List<OrderDataModel>)null);
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.GetAllOrders(), Throws.TypeOf<NullListException>());
|
||
|
_orderStorageContact.Verify(x => x.GetOrders(), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void GetAllOrders_StorageThrowError_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
_orderStorageContact.Setup(x => x.GetOrders())
|
||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.GetAllOrders(), Throws.TypeOf<StorageException>());
|
||
|
_orderStorageContact.Verify(x => x.GetOrders(), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void GetOrderByData_ReturnsOrderById_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var id = Guid.NewGuid().ToString();
|
||
|
var order = new OrderDataModel(
|
||
|
id,
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
Guid.NewGuid().ToString(),
|
||
|
Guid.NewGuid().ToString(),
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
_orderStorageContact.Setup(x => x.GetElementById(It.Is<int>(i => i.ToString() == id))).Returns(order);
|
||
|
|
||
|
// Act
|
||
|
var element = _orderBusinessLogicContract.GetOrderByData(id);
|
||
|
|
||
|
// Assert
|
||
|
Assert.That(element, Is.Not.Null);
|
||
|
Assert.That(element.Id, Is.EqualTo(id));
|
||
|
_orderStorageContact.Verify(x => x.GetElementById(It.Is<int>(i => i.ToString() == id)), 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>());
|
||
|
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<int>()), Times.Never);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void GetOrderByData_NotFoundRecord_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var id = "999";
|
||
|
_orderStorageContact.Setup(x => x.GetElementById(It.Is<int>(i => i.ToString() == id)))
|
||
|
.Returns((OrderDataModel)null);
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(id),
|
||
|
Throws.TypeOf<ElementNotFoundException>());
|
||
|
_orderStorageContact.Verify(x => x.GetElementById(It.Is<int>(i => i.ToString() == id)), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void GetOrderByData_StorageThrowError_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
_orderStorageContact.Setup(x => x.GetElementById(It.IsAny<int>()))
|
||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.GetOrderByData("1"), Throws.TypeOf<StorageException>());
|
||
|
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<int>()), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void InsertOrder_CorrectRecord_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var pekarId = Guid.NewGuid().ToString();
|
||
|
var productId = Guid.NewGuid().ToString();
|
||
|
var flag = false;
|
||
|
var order = new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
productId,
|
||
|
pekarId,
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
_orderStorageContact.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
|
||
|
.Callback((OrderDataModel x) =>
|
||
|
{
|
||
|
flag = x.Id == order.Id && x.CustomerName == order.CustomerName && x.OrderDate == order.OrderDate &&
|
||
|
x.TotalAmount == order.TotalAmount && x.DiscountAmount == order.DiscountAmount &&
|
||
|
x.ProductId == order.ProductId && x.PekarId == order.PekarId &&
|
||
|
x.StatusType == order.StatusType;
|
||
|
});
|
||
|
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
|
||
|
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
|
||
|
_productStorageContact.Setup(x => x.GetElementById(productId))
|
||
|
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
|
||
|
|
||
|
// Act
|
||
|
_orderBusinessLogicContract.InsertOrder(order);
|
||
|
|
||
|
// Assert
|
||
|
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||
|
Assert.That(flag);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void InsertOrder_RecordWithExistsData_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var order = new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
Guid.NewGuid().ToString(),
|
||
|
Guid.NewGuid().ToString(),
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
_orderStorageContact.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
|
||
|
.Throws(new ElementExistsException("ID", order.Id));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<ElementExistsException>());
|
||
|
_orderStorageContact.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>());
|
||
|
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void InsertOrder_InvalidRecord_ThrowException_Test()
|
||
|
{
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.InsertOrder(new OrderDataModel(
|
||
|
"",
|
||
|
"",
|
||
|
DateTime.Now,
|
||
|
-100.50m,
|
||
|
-10.50m,
|
||
|
"",
|
||
|
"",
|
||
|
StatusType.Pending
|
||
|
)), Throws.TypeOf<ValidationException>());
|
||
|
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void InsertOrder_StorageThrowError_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var order = new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
Guid.NewGuid().ToString(),
|
||
|
Guid.NewGuid().ToString(),
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
_orderStorageContact.Setup(x => x.AddElement(It.IsAny<OrderDataModel>()))
|
||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<StorageException>());
|
||
|
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void UpdateOrder_CorrectRecord_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var pekarId = Guid.NewGuid().ToString();
|
||
|
var productId = Guid.NewGuid().ToString();
|
||
|
var flag = false;
|
||
|
var order = new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
productId,
|
||
|
pekarId,
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>()))
|
||
|
.Callback((OrderDataModel x) =>
|
||
|
{
|
||
|
flag = x.Id == order.Id && x.CustomerName == order.CustomerName && x.OrderDate == order.OrderDate &&
|
||
|
x.TotalAmount == order.TotalAmount && x.DiscountAmount == order.DiscountAmount &&
|
||
|
x.ProductId == order.ProductId && x.PekarId == order.PekarId &&
|
||
|
x.StatusType == order.StatusType;
|
||
|
});
|
||
|
_pekarStorageContact.Setup(x => x.GetElementById(pekarId))
|
||
|
.Returns(new PekarDataModel(pekarId, "Ivan Ivanov", "Baker", 1.5m, new List<ProductDataModel>()));
|
||
|
_productStorageContact.Setup(x => x.GetElementById(productId))
|
||
|
.Returns(new ProductDataModel(productId, "Cake", "Super soft cake", new List<IngredientDataModel>()));
|
||
|
|
||
|
// Act
|
||
|
_orderBusinessLogicContract.UpdateOrder(order);
|
||
|
|
||
|
// Assert
|
||
|
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||
|
Assert.That(flag);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void UpdateOrder_RecordNotFound_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var order = new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
Guid.NewGuid().ToString(),
|
||
|
Guid.NewGuid().ToString(),
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>()))
|
||
|
.Throws(new ElementNotFoundException("Order not found"));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order),
|
||
|
Throws.TypeOf<ElementNotFoundException>());
|
||
|
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void UpdateOrder_NullRecord_ThrowException_Test()
|
||
|
{
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(null), Throws.TypeOf<ArgumentNullException>());
|
||
|
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void UpdateOrder_InvalidRecord_ThrowException_Test()
|
||
|
{
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(new OrderDataModel(
|
||
|
"",
|
||
|
"",
|
||
|
DateTime.Now,
|
||
|
-100.50m,
|
||
|
-10.50m,
|
||
|
"",
|
||
|
"",
|
||
|
StatusType.Pending
|
||
|
)), Throws.TypeOf<ValidationException>());
|
||
|
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void UpdateOrder_StorageThrowError_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var order = new OrderDataModel(
|
||
|
Guid.NewGuid().ToString(),
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
Guid.NewGuid().ToString(),
|
||
|
Guid.NewGuid().ToString(),
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
_orderStorageContact.Setup(x => x.UpdateElement(It.IsAny<OrderDataModel>()))
|
||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order), Throws.TypeOf<StorageException>());
|
||
|
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void DeleteOrder_CorrectId_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var id = Guid.NewGuid().ToString();
|
||
|
var order = new OrderDataModel(
|
||
|
id,
|
||
|
"John Doe",
|
||
|
DateTime.Now,
|
||
|
100.50m,
|
||
|
10.50m,
|
||
|
Guid.NewGuid().ToString(),
|
||
|
Guid.NewGuid().ToString(),
|
||
|
StatusType.Pending
|
||
|
);
|
||
|
var flag = false;
|
||
|
_orderStorageContact.Setup(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)))
|
||
|
.Callback(() => { flag = true; });
|
||
|
_orderStorageContact.Setup(x => x.GetOrders()).Returns(new List<OrderDataModel> { order });
|
||
|
|
||
|
// Act
|
||
|
_orderBusinessLogicContract.DeleteOrder(id);
|
||
|
|
||
|
// Assert
|
||
|
_orderStorageContact.Verify(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)), Times.Once);
|
||
|
Assert.That(flag);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void DeleteOrder_RecordNotFound_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
var id = "nonexistent";
|
||
|
_orderStorageContact.Setup(x => x.GetOrders()).Returns(new List<OrderDataModel>());
|
||
|
_orderStorageContact.Setup(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)))
|
||
|
.Throws(new ElementNotFoundException("Order not found"));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(id), Throws.TypeOf<ElementNotFoundException>());
|
||
|
_orderStorageContact.Verify(x => x.DeleteElement(It.Is<OrderDataModel>(o => o.Id == id)), Times.Once);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void DeleteOrder_NullOrEmptyId_ThrowException_Test()
|
||
|
{
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(null), Throws.TypeOf<ArgumentNullException>());
|
||
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(string.Empty),
|
||
|
Throws.TypeOf<ArgumentNullException>());
|
||
|
_orderStorageContact.Verify(x => x.DeleteElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void DeleteOrder_InvalidId_ThrowException_Test()
|
||
|
{
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder("invalid"), Throws.TypeOf<ValidationException>());
|
||
|
_orderStorageContact.Verify(x => x.DeleteElement(It.IsAny<OrderDataModel>()), Times.Never);
|
||
|
}
|
||
|
|
||
|
[Test]
|
||
|
public void DeleteOrder_StorageThrowError_ThrowException_Test()
|
||
|
{
|
||
|
// Arrange
|
||
|
_orderStorageContact.Setup(x => x.DeleteElement(It.IsAny<OrderDataModel>()))
|
||
|
.Throws(new StorageException(new InvalidOperationException()));
|
||
|
|
||
|
// Act & Assert
|
||
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(Guid.NewGuid().ToString()),
|
||
|
Throws.TypeOf<StorageException>());
|
||
|
_orderStorageContact.Verify(x => x.DeleteElement(It.IsAny<OrderDataModel>()), Times.Once);
|
||
|
}
|
||
|
}
|
||
|
}
|