using Microsoft.Extensions.Logging; using Moq; using PapaCarloBusinessLogic.Implementations; using PapaCarloContracts.DataModels; using PapaCarloContracts.Exceptions; using PapaCarloContracts.StoragesContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PapaCarloTests.BusinessLogicsContracts; [TestFixture] internal class CuttingBusinessLogicContractTests { private CuttingBusinessLogicContract _cuttingBusinessLogicContract; private Mock _cuttingStorageContract; [OneTimeSetUp] public void OneTimeSetUp() { _cuttingStorageContract = new Mock(); _cuttingBusinessLogicContract = new CuttingBusinessLogicContract(_cuttingStorageContract.Object, new Mock().Object); } [SetUp] public void SetUp() { _cuttingStorageContract.Reset(); } [Test] public void GetAllCuttingsByPeriod_ReturnListOfRecords_Test() { //Arrange var date = DateTime.UtcNow; var listOriginal = new List() { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, []), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, []), }; _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); //Act var list = _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date.AddDays(1)); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Is.EquivalentTo(listOriginal)); _cuttingStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null, null), Times.Once); } [Test] public void GetAllCuttingsByPeriod_ReturnEmptyList_Test() { //Arrange _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); //Act var list = _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Has.Count.EqualTo(0)); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllCuttingsByPeriod_IncorrectDates_ThrowException_Test() { //Arrange var date = DateTime.UtcNow; //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date), Throws.TypeOf()); Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllCuttingsByPeriod_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllCuttingsByPeriod_StorageThrowError_ThrowException_Test() { //Arrange _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllCuttingsByWorkerByPeriod_ReturnListOfRecords_Test() { //Arrange var date = DateTime.UtcNow; var workerId = Guid.NewGuid().ToString(); var listOriginal = new List() { new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, []), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, []), }; _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(listOriginal); //Act var list = _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(workerId, date, date.AddDays(1)); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Is.EquivalentTo(listOriginal)); _cuttingStorageContract.Verify(x => x.GetList(date, date.AddDays(1), workerId, null, null), Times.Once); } [Test] public void GetAllCuttingsByWorkerByPeriod_ReturnEmptyList_Test() { //Arrange _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns([]); //Act var list = _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Has.Count.EqualTo(0)); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllCuttingsByWorkerByPeriod_IncorrectDates_ThrowException_Test() { //Arrange var date = DateTime.UtcNow; //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), date, date), Throws.TypeOf()); Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllCuttingsByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllCuttingsByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllCuttingsByWorkerByPeriod_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllCuttingsByWorkerByPeriod_StorageThrowError_ThrowException_Test() { //Arrange _cuttingStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetCuttingByData_GetById_ReturnRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var record = new CuttingDataModel(id, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); _cuttingStorageContract.Setup(x => x.GetElementById(id)).Returns(record); //Act var element = _cuttingBusinessLogicContract.GetCuttingByData(id); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.Id, Is.EqualTo(id)); _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetCuttingByData_EmptyData_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(null), Throws.TypeOf()); Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(string.Empty), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); } [Test] public void GetCuttingByData_IdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData("cuttingId"), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); } [Test] public void GetCuttingByData_GetById_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetCuttingByData_StorageThrowError_ThrowException_Test() { //Arrange _cuttingStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void InsertCutting_CorrectRecord_Test() { //Arrange var flag = false; var record = new CuttingDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); _cuttingStorageContract.Setup(x => x.AddElement(It.IsAny())) .Callback((CuttingDataModel x) => { flag = x.Id == record.Id && x.WorkerId == record.WorkerId && x.BlankId == record.BlankId && x.CutDate == record.CutDate && x.IsCancel == record.IsCancel; }); //Act _cuttingBusinessLogicContract.InsertCutting(record); //Assert _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void InsertCutting_RecordWithExistsData_ThrowException_Test() { //Arrange _cuttingStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void InsertCutting_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(null), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertCutting_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(new CuttingDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, [])), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertCutting_StorageThrowError_ThrowException_Test() { //Arrange _cuttingStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),false, [new CuttingProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void CancelCutting_CorrectRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var flag = false; _cuttingStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); //Act _cuttingBusinessLogicContract.CancelCutting(id); //Assert _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void CancelCutting_RecordWithIncorrectId_ThrowException_Test() { //Arrange var id = Guid.NewGuid().ToString(); _cuttingStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(Guid.NewGuid().ToString()), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } [Test] public void CancelCutting_IdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(null), Throws.TypeOf()); Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(string.Empty), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void CancelCutting_IdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.CancelCutting("id"), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void CancelCutting_StorageThrowError_ThrowException_Test() { //Arrange _cuttingStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(Guid.NewGuid().ToString()), Throws.TypeOf()); _cuttingStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } }