using MagicCarpetBusinessLogic.Implementations; using MagicCarpetContracts.BusinessLogicContracts; using MagicCarpetContracts.DataModels; using MagicCarpetContracts.Enums; using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.StoragesContracts; using Microsoft.Extensions.Logging; using Moq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MagicCarpetTests.BusinessLogicContractsTests; [TestFixture] internal class AgencyBusinessLogicContractTests { private IAgencyBusinessLogicContract _warehouseBusinessLogicContract; private Mock _warehouseStorageContract; [OneTimeSetUp] public void OneTimeSetUp() { _warehouseStorageContract = new Mock(); _warehouseBusinessLogicContract = new AgencyBusinessLogicContract(_warehouseStorageContract.Object, new Mock().Object); } [TearDown] public void TearDown() { _warehouseStorageContract.Reset(); } public void GetAllSupplies_ReturnListOfRecords_Test() { // Arrange var listOriginal = new List() { new(Guid.NewGuid().ToString(), TourType.Beach, 1, []), new(Guid.NewGuid().ToString(), TourType.Beach, 1, []), new(Guid.NewGuid().ToString(), TourType.Beach, 1, []), }; _warehouseStorageContract.Setup(x => x.GetList()).Returns(listOriginal); // Act var list = _warehouseBusinessLogicContract.GetAllComponents(); // Assert Assert.That(list, Is.Not.Null); Assert.That(list, Is.EquivalentTo(listOriginal)); } [Test] public void GetAllSupplies_ReturnEmptyList_Test() { // Arrange _warehouseStorageContract.Setup(x => x.GetList()).Returns([]); // Act var list = _warehouseBusinessLogicContract.GetAllComponents(); // Assert Assert.That(list, Is.Not.Null); Assert.That(list, Has.Count.EqualTo(0)); _warehouseStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllSupplies_ReturnNull_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.GetList()).Returns((List)null); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllSupplies_StorageThrowError_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.GetAllComponents(), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetSuppliesByData_GetById_ReturnRecord_Test() { // Arrange var id = Guid.NewGuid().ToString(); var record = new AgencyDataModel(id, TourType.Beach, 1, []); _warehouseStorageContract.Setup(x => x.GetElementById(id)).Returns(record); // Act var element = _warehouseBusinessLogicContract.GetComponentByData(id); // Assert Assert.That(element, Is.Not.Null); Assert.That(element.Id, Is.EqualTo(id)); _warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetComponentsByData_EmptyData_ThrowException_Test() { //Act&Assert Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(null), Throws.TypeOf()); Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(string.Empty), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); } [Test] public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new ElementNotFoundException("")); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetSuppliesByData_StorageThrowError_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.GetComponentByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void InsertSupplies_CorrectRecord_Test() { // Arrange var record = new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); _warehouseStorageContract.Setup(x => x.AddElement(It.IsAny())); // Act _warehouseBusinessLogicContract.InsertComponent(record); // Assert _warehouseStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void InsertSupplies_RecordWithExistsData_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1, [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void InsertSupplies_NullRecord_ThrowException_Test() { // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(null), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertComponents_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new AgencyDataModel("id", TourType.Beach, 1, [])), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertSupplies_StorageThrowError_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.InsertComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1, [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void UpdateSupplies_CorrectRecord_Test() { // Arrange var record = new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); _warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny())); // Act _warehouseBusinessLogicContract.UpdateComponent(record); // Assert _warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1, [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateSupplies_RecordWithExistsData_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1, [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateSupplies_NullRecord_ThrowException_Test() { // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(null), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateComponents_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, [])), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateSupplies_StorageThrowError_ThrowException_Test() { // Arrange _warehouseStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); // Act & Assert Assert.That(() => _warehouseBusinessLogicContract.UpdateComponent(new(Guid.NewGuid().ToString(), TourType.Beach, 1, [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void DeleteComponents_CorrectRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var flag = false; _warehouseStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); //Act _warehouseBusinessLogicContract.DeleteComponent(id); //Assert _warehouseStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void DeleteComponents_RecordWithIncorrectId_ThrowException_Test() { //Arrange var id = Guid.NewGuid().ToString(); _warehouseStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); //Act&Assert Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } [Test] public void DeleteComponents_IdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(null), Throws.TypeOf()); Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(string.Empty), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteComponents_IdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent("id"), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteComponents_StorageThrowError_ThrowException_Test() { //Arrange _warehouseStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _warehouseBusinessLogicContract.DeleteComponent(Guid.NewGuid().ToString()), Throws.TypeOf()); _warehouseStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } }