339 lines
17 KiB
C#

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<ICuttingStorageContract> _cuttingStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_cuttingStorageContract = new Mock<ICuttingStorageContract>();
_cuttingBusinessLogicContract = new CuttingBusinessLogicContract(_cuttingStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_cuttingStorageContract.Reset();
}
[Test]
public void GetAllCuttingsByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<CuttingDataModel>()
{
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<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).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<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).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<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllCuttingsByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllCuttingsByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllCuttingsByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_cuttingStorageContract.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(() => _cuttingBusinessLogicContract.GetAllCuttingsByPeriod(DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllCuttingsByWorkerByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<CuttingDataModel>()
{
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<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).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<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).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<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), 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<IncorrectDatesException>());
Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllCuttingsByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(null, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllCuttingsByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod("workerId", DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ValidationException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), 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<NullListException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllCuttingsByWorkerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_cuttingStorageContract.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(() => _cuttingBusinessLogicContract.GetAllCuttingsByWorkerByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_cuttingStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), 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<string>()), Times.Once);
}
[Test]
public void GetCuttingByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetCuttingByData_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData("cuttingId"), Throws.TypeOf<ValidationException>());
_cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetCuttingByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetCuttingByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_cuttingStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.GetCuttingByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_cuttingStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), 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<CuttingDataModel>()))
.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<CuttingDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertCutting_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_cuttingStorageContract.Setup(x => x.AddElement(It.IsAny<CuttingDataModel>())).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<ElementExistsException>());
_cuttingStorageContract.Verify(x => x.AddElement(It.IsAny<CuttingDataModel>()), Times.Once);
}
[Test]
public void InsertCutting_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.InsertCutting(null), Throws.TypeOf<ArgumentNullException>());
_cuttingStorageContract.Verify(x => x.AddElement(It.IsAny<CuttingDataModel>()), 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<ValidationException>());
_cuttingStorageContract.Verify(x => x.AddElement(It.IsAny<CuttingDataModel>()), Times.Never);
}
[Test]
public void InsertCutting_StorageThrowError_ThrowException_Test()
{
//Arrange
_cuttingStorageContract.Setup(x => x.AddElement(It.IsAny<CuttingDataModel>())).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<StorageException>());
_cuttingStorageContract.Verify(x => x.AddElement(It.IsAny<CuttingDataModel>()), 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<string>()), 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<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_cuttingStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void CancelCutting_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(string.Empty), Throws.TypeOf<ArgumentNullException>());
_cuttingStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelCutting_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.CancelCutting("id"), Throws.TypeOf<ValidationException>());
_cuttingStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void CancelCutting_StorageThrowError_ThrowException_Test()
{
//Arrange
_cuttingStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _cuttingBusinessLogicContract.CancelCutting(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_cuttingStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}