484 lines
21 KiB
C#
484 lines
21 KiB
C#
using SnowMaidenBusinessLogic.Implementations;
|
|
using SnowMaidenContracts.DataModels;
|
|
using SnowMaidenContracts.Enums;
|
|
using SnowMaidenContracts.Exceptions;
|
|
using SnowMaidenContracts.StoragesContracts;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using SnowMaidenContracts.BusinessLogicsContracts;
|
|
|
|
namespace SnowMaidenTests.BusinessLogicsContractsTests;
|
|
|
|
[TestFixture]
|
|
internal class IceCreamBusinessLogicContractTests
|
|
{
|
|
private IceCreamBusinessLogicContract _icecreamBusinessLogicContract;
|
|
private Mock<IIceCreamStorageContract> _icecreamStorageContract;
|
|
|
|
[OneTimeSetUp]
|
|
public void OneTimeSetUp()
|
|
{
|
|
_icecreamStorageContract = new Mock<IIceCreamStorageContract>();
|
|
_icecreamBusinessLogicContract = new IceCreamBusinessLogicContract(_icecreamStorageContract.Object, new Mock<ILogger>().Object);
|
|
}
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_icecreamStorageContract.Reset();
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProducts_ReturnListOfRecords_Test()
|
|
{
|
|
//Arrange
|
|
var listOriginal = new List<IceCreamDataModel>()
|
|
{
|
|
new(Guid.NewGuid().ToString(), "name 1", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false),
|
|
new(Guid.NewGuid().ToString(), "name 2", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, true),
|
|
new(Guid.NewGuid().ToString(), "name 3", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false),
|
|
};
|
|
_icecreamStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns(listOriginal);
|
|
//Act
|
|
var listOnlyActive = _icecreamBusinessLogicContract.GetAllProducts(true);
|
|
var list = _icecreamBusinessLogicContract.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));
|
|
});
|
|
_icecreamStorageContract.Verify(x => x.GetList(true, null), Times.Once);
|
|
_icecreamStorageContract.Verify(x => x.GetList(false, null), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProducts_ReturnEmptyList_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns([]);
|
|
//Act
|
|
var listOnlyActive = _icecreamBusinessLogicContract.GetAllProducts(true);
|
|
var list = _icecreamBusinessLogicContract.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));
|
|
});
|
|
_icecreamStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null), Times.Exactly(2));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProducts_ReturnNull_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
|
_icecreamStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProducts_StorageThrowError_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
|
_icecreamStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProductsByManufacturer_ReturnListOfRecords_Test()
|
|
{
|
|
//Arrange
|
|
var manufacturerId = Guid.NewGuid().ToString();
|
|
var listOriginal = new List<IceCreamDataModel>()
|
|
{
|
|
new(Guid.NewGuid().ToString(), "name 1",IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false),
|
|
new(Guid.NewGuid().ToString(), "name 2", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, true),
|
|
new(Guid.NewGuid().ToString(), "name 3", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false),
|
|
};
|
|
_icecreamStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns(listOriginal);
|
|
//Act
|
|
var listOnlyActive = _icecreamBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, true);
|
|
var list = _icecreamBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, 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));
|
|
});
|
|
_icecreamStorageContract.Verify(x => x.GetList(true, manufacturerId), Times.Once);
|
|
_icecreamStorageContract.Verify(x => x.GetList(false, manufacturerId), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProductsByManufacturer_ReturnEmptyList_Test()
|
|
{
|
|
//Arrange
|
|
var manufacturerId = Guid.NewGuid().ToString();
|
|
_icecreamStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns([]);
|
|
//Act
|
|
var listOnlyActive = _icecreamBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, true);
|
|
var list = _icecreamBusinessLogicContract.GetAllProductsByManufacturer(manufacturerId, 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));
|
|
});
|
|
_icecreamStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), manufacturerId), Times.Exactly(2));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProductsByManufacturer_ManufacturerIdIsNullOrEmpty_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetAllProductsByManufacturer(null, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetAllProductsByManufacturer(string.Empty, It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
|
|
_icecreamStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProductsByManufacturer_ManufacturerIdIsNotGuid_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetAllProductsByManufacturer("manufacturerId", It.IsAny<bool>()), Throws.TypeOf<ValidationException>());
|
|
_icecreamStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProductsByManufacturer_ReturnNull_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetAllProductsByManufacturer(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
|
|
_icecreamStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllProductsByManufacturer_StorageThrowError_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetAllProductsByManufacturer(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
|
_icecreamStorageContract.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<IceCreamHistoryDataModel>()
|
|
{
|
|
new(Guid.NewGuid().ToString(), 10),
|
|
new(Guid.NewGuid().ToString(), 15),
|
|
new(Guid.NewGuid().ToString(), 10),
|
|
};
|
|
_icecreamStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns(listOriginal);
|
|
//Act
|
|
var list = _icecreamBusinessLogicContract.GetProductHistoryByProduct(productId);
|
|
//Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.EquivalentTo(listOriginal));
|
|
_icecreamStorageContract.Verify(x => x.GetHistoryByProductId(productId), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductHistoryByProduct_ReturnEmptyList_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Returns([]);
|
|
//Act
|
|
var list = _icecreamBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString());
|
|
//Assert
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(0));
|
|
_icecreamStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductHistoryByProduct_ProductIdIsNullOrEmpty_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductHistoryByProduct(null), Throws.TypeOf<ArgumentNullException>());
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductHistoryByProduct(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
|
_icecreamStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductHistoryByProduct("productId"), Throws.TypeOf<ValidationException>());
|
|
_icecreamStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductHistoryByProduct_ReturnNull_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<NullListException>());
|
|
_icecreamStorageContract.Verify(x => x.GetHistoryByProductId(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductHistoryByProduct_StorageThrowError_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.GetHistoryByProductId(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductHistoryByProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
|
_icecreamStorageContract.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 IceCreamDataModel(id, "name", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false);
|
|
_icecreamStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
|
|
//Act
|
|
var element = _icecreamBusinessLogicContract.GetProductByData(id);
|
|
//Assert
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.That(element.Id, Is.EqualTo(id));
|
|
_icecreamStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductByData_GetByName_ReturnRecord_Test()
|
|
{
|
|
//Arrange
|
|
var name = "name";
|
|
var record = new IceCreamDataModel(Guid.NewGuid().ToString(), name, IceCreamType.Popsicle, Guid.NewGuid().ToString(), 10, false);
|
|
_icecreamStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
|
|
//Act
|
|
var element = _icecreamBusinessLogicContract.GetProductByData(name);
|
|
//Assert
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.That(element.IceCreamName, Is.EqualTo(name));
|
|
_icecreamStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductByData_EmptyData_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductByData(null), Throws.TypeOf<ArgumentNullException>());
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
|
_icecreamStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
|
_icecreamStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductByData_GetById_NotFoundRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
_icecreamStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductByData_GetByName_NotFoundRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductByData("name"), Throws.TypeOf<ElementNotFoundException>());
|
|
_icecreamStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void GetProductByData_StorageThrowError_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
_icecreamStorageContract.Setup(x => x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
|
Assert.That(() => _icecreamBusinessLogicContract.GetProductByData("name"), Throws.TypeOf<StorageException>());
|
|
_icecreamStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
|
|
_icecreamStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertProduct_CorrectRecord_Test()
|
|
{
|
|
//Arrange
|
|
var flag = false;
|
|
var record = new IceCreamDataModel(Guid.NewGuid().ToString(), "name", IceCreamType.Popsicle, Guid.NewGuid().ToString(), 10, false);
|
|
_icecreamStorageContract.Setup(x => x.AddElement(It.IsAny<IceCreamDataModel>()))
|
|
.Callback((IceCreamDataModel x) =>
|
|
{
|
|
flag = x.Id == record.Id && x.IceCreamName == record.IceCreamName && x.IceCreamType == record.IceCreamType &&
|
|
x.ManufacturerId == record.ManufacturerId && x.Price == record.Price && x.IsDeleted == record.IsDeleted;
|
|
});
|
|
//Act
|
|
_icecreamBusinessLogicContract.InsertProduct(record);
|
|
//Assert
|
|
_icecreamStorageContract.Verify(x => x.AddElement(It.IsAny<IceCreamDataModel>()), Times.Once);
|
|
Assert.That(flag);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertProduct_RecordWithExistsData_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.AddElement(It.IsAny<IceCreamDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", IceCreamType.Popsicle, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ElementExistsException>());
|
|
_icecreamStorageContract.Verify(x => x.AddElement(It.IsAny<IceCreamDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertProduct_NullRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.InsertProduct(null), Throws.TypeOf<ArgumentNullException>());
|
|
_icecreamStorageContract.Verify(x => x.AddElement(It.IsAny<IceCreamDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertProduct_InvalidRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.InsertProduct(new IceCreamDataModel("id", "name", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ValidationException>());
|
|
_icecreamStorageContract.Verify(x => x.AddElement(It.IsAny<IceCreamDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void InsertProduct_StorageThrowError_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.AddElement(It.IsAny<IceCreamDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.InsertProduct(new(Guid.NewGuid().ToString(), "name", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<StorageException>());
|
|
_icecreamStorageContract.Verify(x => x.AddElement(It.IsAny<IceCreamDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateProduct_CorrectRecord_Test()
|
|
{
|
|
//Arrange
|
|
var flag = false;
|
|
var record = new IceCreamDataModel(Guid.NewGuid().ToString(), "name", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false);
|
|
_icecreamStorageContract.Setup(x => x.UpdElement(It.IsAny<IceCreamDataModel>()))
|
|
.Callback((IceCreamDataModel x) =>
|
|
{
|
|
flag = x.Id == record.Id && x.IceCreamName == record.IceCreamName && x.IceCreamType == record.IceCreamType &&
|
|
x.ManufacturerId == record.ManufacturerId && x.Price == record.Price && x.IsDeleted == record.IsDeleted;
|
|
});
|
|
//Act
|
|
_icecreamBusinessLogicContract.UpdateProduct(record);
|
|
//Assert
|
|
_icecreamStorageContract.Verify(x => x.UpdElement(It.IsAny<IceCreamDataModel>()), Times.Once);
|
|
Assert.That(flag);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateProduct_RecordWithIncorrectData_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.UpdElement(It.IsAny<IceCreamDataModel>())).Throws(new ElementNotFoundException(""));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ElementNotFoundException>());
|
|
_icecreamStorageContract.Verify(x => x.UpdElement(It.IsAny<IceCreamDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateProduct_RecordWithExistsData_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.UpdElement(It.IsAny<IceCreamDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "anme", IceCreamType.Popsicle, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ElementExistsException>());
|
|
_icecreamStorageContract.Verify(x => x.UpdElement(It.IsAny<IceCreamDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateProduct_NullRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.UpdateProduct(null), Throws.TypeOf<ArgumentNullException>());
|
|
_icecreamStorageContract.Verify(x => x.UpdElement(It.IsAny<IceCreamDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateProduct_InvalidRecord_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.UpdateProduct(new IceCreamDataModel("id", "name", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<ValidationException>());
|
|
_icecreamStorageContract.Verify(x => x.UpdElement(It.IsAny<IceCreamDataModel>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateProduct_StorageThrowError_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.UpdElement(It.IsAny<IceCreamDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.UpdateProduct(new(Guid.NewGuid().ToString(), "name", IceCreamType.Sherbet, Guid.NewGuid().ToString(), 10, false)), Throws.TypeOf<StorageException>());
|
|
_icecreamStorageContract.Verify(x => x.UpdElement(It.IsAny<IceCreamDataModel>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void DeleteProduct_CorrectRecord_Test()
|
|
{
|
|
//Arrange
|
|
var id = Guid.NewGuid().ToString();
|
|
var flag = false;
|
|
_icecreamStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
|
|
//Act
|
|
_icecreamBusinessLogicContract.DeleteProduct(id);
|
|
//Assert
|
|
_icecreamStorageContract.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();
|
|
_icecreamStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
_icecreamStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void DeleteProduct_IdIsNullOrEmpty_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.DeleteProduct(null), Throws.TypeOf<ArgumentNullException>());
|
|
Assert.That(() => _icecreamBusinessLogicContract.DeleteProduct(string.Empty), Throws.TypeOf<ArgumentNullException>());
|
|
_icecreamStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void DeleteProduct_IdIsNotGuid_ThrowException_Test()
|
|
{
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.DeleteProduct("id"), Throws.TypeOf<ValidationException>());
|
|
_icecreamStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void DeleteProduct_StorageThrowError_ThrowException_Test()
|
|
{
|
|
//Arrange
|
|
_icecreamStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
|
|
//Act&Assert
|
|
Assert.That(() => _icecreamBusinessLogicContract.DeleteProduct(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
|
|
_icecreamStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
|
|
}
|
|
} |