2025-02-26 23:49:16 +04:00
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using CandyHouseBase.DataModels;
|
|
|
|
using CandyHouseBase.Enums;
|
|
|
|
using CandyHouseBase.Exceptions;
|
2025-02-27 02:25:39 +04:00
|
|
|
using CandyHouseBase.Extensions;
|
2025-02-26 23:49:16 +04:00
|
|
|
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
|
|
|
|
);
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns(order);
|
2025-02-26 23:49:16 +04:00
|
|
|
|
|
|
|
// Act
|
|
|
|
var element = _orderBusinessLogicContract.GetOrderByData(id);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
Assert.That(element, Is.Not.Null);
|
|
|
|
Assert.That(element.Id, Is.EqualTo(id));
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetOrderByData_EmptyData_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Act & Assert
|
|
|
|
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(null), Throws.TypeOf<ArgumentNullException>());
|
2025-02-27 02:25:39 +04:00
|
|
|
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetOrderByData_NotFoundRecord_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Arrange
|
2025-02-27 02:25:39 +04:00
|
|
|
var id = Guid.NewGuid().ToString();
|
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns((OrderDataModel)null);
|
2025-02-26 23:49:16 +04:00
|
|
|
|
|
|
|
// Act & Assert
|
2025-02-27 02:25:39 +04:00
|
|
|
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(id), Throws.TypeOf<ElementNotFoundException>());
|
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetOrderByData_StorageThrowError_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Arrange
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
|
2025-02-26 23:49:16 +04:00
|
|
|
.Throws(new StorageException(new InvalidOperationException()));
|
|
|
|
|
|
|
|
// Act & Assert
|
2025-02-27 02:25:39 +04:00
|
|
|
Assert.That(() => _orderBusinessLogicContract.GetOrderByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[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
|
|
|
|
);
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns((OrderDataModel)null); // No existing order
|
|
|
|
_orderStorageContact.Setup(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
|
2025-02-26 23:49:16 +04:00
|
|
|
.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;
|
2025-02-27 02:25:39 +04:00
|
|
|
})
|
|
|
|
.Verifiable();
|
2025-02-26 23:49:16 +04:00
|
|
|
_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
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
Assert.That(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void InsertOrder_RecordWithExistsData_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Arrange
|
2025-02-27 02:25:39 +04:00
|
|
|
var pekarId = Guid.NewGuid().ToString();
|
|
|
|
var productId = Guid.NewGuid().ToString();
|
2025-02-26 23:49:16 +04:00
|
|
|
var order = new OrderDataModel(
|
|
|
|
Guid.NewGuid().ToString(),
|
|
|
|
"John Doe",
|
|
|
|
DateTime.Now,
|
|
|
|
100.50m,
|
|
|
|
10.50m,
|
2025-02-27 02:25:39 +04:00
|
|
|
productId,
|
|
|
|
pekarId,
|
2025-02-26 23:49:16 +04:00
|
|
|
StatusType.Pending
|
|
|
|
);
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns(order); // Existing order
|
|
|
|
_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>()));
|
2025-02-26 23:49:16 +04:00
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<ElementExistsException>());
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(order.Id), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[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,
|
2025-02-27 02:25:39 +04:00
|
|
|
-100.50m,
|
|
|
|
-10.50m,
|
|
|
|
"",
|
|
|
|
"",
|
2025-02-26 23:49:16 +04:00
|
|
|
StatusType.Pending
|
|
|
|
)), Throws.TypeOf<ValidationException>());
|
|
|
|
_orderStorageContact.Verify(x => x.AddElement(It.IsAny<OrderDataModel>()), Times.Never);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void InsertOrder_StorageThrowError_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Arrange
|
2025-02-27 02:25:39 +04:00
|
|
|
var pekarId = Guid.NewGuid().ToString();
|
|
|
|
var productId = Guid.NewGuid().ToString();
|
2025-02-26 23:49:16 +04:00
|
|
|
var order = new OrderDataModel(
|
|
|
|
Guid.NewGuid().ToString(),
|
|
|
|
"John Doe",
|
|
|
|
DateTime.Now,
|
|
|
|
100.50m,
|
|
|
|
10.50m,
|
2025-02-27 02:25:39 +04:00
|
|
|
productId,
|
|
|
|
pekarId,
|
2025-02-26 23:49:16 +04:00
|
|
|
StatusType.Pending
|
|
|
|
);
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(order.Id)).Returns((OrderDataModel)null); // No existing order
|
|
|
|
_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>()));
|
|
|
|
_orderStorageContact.Setup(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
|
2025-02-26 23:49:16 +04:00
|
|
|
.Throws(new StorageException(new InvalidOperationException()));
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
Assert.That(() => _orderBusinessLogicContract.InsertOrder(order), Throws.TypeOf<StorageException>());
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(order.Id), Times.Once);
|
|
|
|
_orderStorageContact.Verify(x => x.AddElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[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
|
|
|
|
);
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
|
2025-02-26 23:49:16 +04:00
|
|
|
.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;
|
2025-02-27 02:25:39 +04:00
|
|
|
})
|
|
|
|
.Verifiable();
|
2025-02-26 23:49:16 +04:00
|
|
|
_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
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
Assert.That(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateOrder_RecordNotFound_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Arrange
|
2025-02-27 02:25:39 +04:00
|
|
|
var pekarId = Guid.NewGuid().ToString();
|
|
|
|
var productId = Guid.NewGuid().ToString();
|
2025-02-26 23:49:16 +04:00
|
|
|
var order = new OrderDataModel(
|
|
|
|
Guid.NewGuid().ToString(),
|
|
|
|
"John Doe",
|
|
|
|
DateTime.Now,
|
|
|
|
100.50m,
|
|
|
|
10.50m,
|
2025-02-27 02:25:39 +04:00
|
|
|
productId,
|
|
|
|
pekarId,
|
2025-02-26 23:49:16 +04:00
|
|
|
StatusType.Pending
|
|
|
|
);
|
2025-02-27 02:25:39 +04:00
|
|
|
_pekarStorageContact.Setup(x => x.GetElementById(pekarId)).Returns((PekarDataModel)null); // Trigger not found
|
|
|
|
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
|
|
|
|
.Verifiable();
|
2025-02-26 23:49:16 +04:00
|
|
|
|
|
|
|
// Act & Assert
|
2025-02-27 02:25:39 +04:00
|
|
|
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order), Throws.TypeOf<ElementNotFoundException>());
|
|
|
|
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Never);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[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(
|
2025-02-27 02:25:39 +04:00
|
|
|
"",
|
2025-02-26 23:49:16 +04:00
|
|
|
"",
|
|
|
|
DateTime.Now,
|
|
|
|
-100.50m,
|
2025-02-27 02:25:39 +04:00
|
|
|
-10.50m,
|
|
|
|
"",
|
|
|
|
"",
|
2025-02-26 23:49:16 +04:00
|
|
|
StatusType.Pending
|
|
|
|
)), Throws.TypeOf<ValidationException>());
|
|
|
|
_orderStorageContact.Verify(x => x.UpdateElement(It.IsAny<OrderDataModel>()), Times.Never);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateOrder_StorageThrowError_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Arrange
|
2025-02-27 02:25:39 +04:00
|
|
|
var pekarId = Guid.NewGuid().ToString();
|
|
|
|
var productId = Guid.NewGuid().ToString();
|
2025-02-26 23:49:16 +04:00
|
|
|
var order = new OrderDataModel(
|
|
|
|
Guid.NewGuid().ToString(),
|
|
|
|
"John Doe",
|
|
|
|
DateTime.Now,
|
|
|
|
100.50m,
|
|
|
|
10.50m,
|
2025-02-27 02:25:39 +04:00
|
|
|
productId,
|
|
|
|
pekarId,
|
2025-02-26 23:49:16 +04:00
|
|
|
StatusType.Pending
|
|
|
|
);
|
2025-02-27 02:25:39 +04:00
|
|
|
_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>()));
|
|
|
|
_orderStorageContact.Setup(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)))
|
2025-02-26 23:49:16 +04:00
|
|
|
.Throws(new StorageException(new InvalidOperationException()));
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
Assert.That(() => _orderBusinessLogicContract.UpdateOrder(order), Throws.TypeOf<StorageException>());
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.UpdateElement(It.Is<OrderDataModel>(o => o.Id == order.Id)), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[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;
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns(order);
|
|
|
|
_orderStorageContact.Setup(x => x.DeleteElement(order)).Callback(() => { flag = true; });
|
2025-02-26 23:49:16 +04:00
|
|
|
|
|
|
|
// Act
|
|
|
|
_orderBusinessLogicContract.DeleteOrder(id);
|
|
|
|
|
|
|
|
// Assert
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.DeleteElement(order), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
Assert.That(flag);
|
2025-02-27 02:25:39 +04:00
|
|
|
Assert.That(Guid.TryParse(order.Id, out _), Is.True);
|
|
|
|
Assert.That(!order.CustomerName.IsEmpty());
|
|
|
|
Assert.That(order.TotalAmount >= 0);
|
|
|
|
Assert.That(order.DiscountAmount >= 0);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void DeleteOrder_RecordNotFound_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Arrange
|
2025-02-27 02:25:39 +04:00
|
|
|
var id = Guid.NewGuid().ToString();
|
|
|
|
_orderStorageContact.Setup(x => x.GetElementById(id)).Returns((OrderDataModel)null);
|
2025-02-26 23:49:16 +04:00
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(id), Throws.TypeOf<ElementNotFoundException>());
|
2025-02-27 02:25:39 +04:00
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void DeleteOrder_NullOrEmptyId_ThrowException_Test()
|
|
|
|
{
|
|
|
|
// Act & Assert
|
|
|
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(null), Throws.TypeOf<ArgumentNullException>());
|
2025-02-27 02:25:39 +04:00
|
|
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
2025-02-26 23:49:16 +04:00
|
|
|
_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
|
2025-02-27 02:25:39 +04:00
|
|
|
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(id)).Returns(order);
|
|
|
|
_orderStorageContact.Setup(x => x.DeleteElement(order)).Throws(new StorageException(new InvalidOperationException()));
|
2025-02-26 23:49:16 +04:00
|
|
|
|
|
|
|
// Act & Assert
|
2025-02-27 02:25:39 +04:00
|
|
|
Assert.That(() => _orderBusinessLogicContract.DeleteOrder(id), Throws.TypeOf<StorageException>());
|
|
|
|
_orderStorageContact.Verify(x => x.GetElementById(id), Times.Once);
|
|
|
|
_orderStorageContact.Verify(x => x.DeleteElement(order), Times.Once);
|
2025-02-26 23:49:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|