Добавление интерфейсов и логики для пополнения и агенства

This commit is contained in:
2025-03-03 18:09:18 +04:00
parent 43dc07e662
commit da93722d9f
11 changed files with 760 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
using MagicCarpetContracts.BusinessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Extensions;
using MagicCarpetContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace MagicCarpetBusinessLogic.Implementations;
public class AgencyBusinessLogicContract(IAgencyStorageContract agencyStorageContract, ILogger logger) : IAgencyBusinessLogicContract
{
private readonly IAgencyStorageContract _AgencyStorageContract = agencyStorageContract;
private readonly ILogger _logger = logger;
public List<AgencyDataModel> GetAllComponents()
{
_logger.LogInformation("GetAllComponents");
return _AgencyStorageContract.GetList() ?? throw new NullListException();
return [];
}
public AgencyDataModel GetTourByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ElementNotFoundException(data);
}
return _AgencyStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return new("", TourType.None, 0, []);
}
public void InsertTour(AgencyDataModel AgencyDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(AgencyDataModel));
ArgumentNullException.ThrowIfNull(AgencyDataModel);
AgencyDataModel.Validate();
_AgencyStorageContract.AddElement(AgencyDataModel);
}
public void UpdateTour(AgencyDataModel AgencyDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(AgencyDataModel));
ArgumentNullException.ThrowIfNull(AgencyDataModel);
AgencyDataModel.Validate();
_AgencyStorageContract.UpdElement(AgencyDataModel);
}
public void DeleteTour(string id)
{
logger.LogInformation("Delete by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_AgencyStorageContract.DelElement(id);
}
}

View File

@@ -0,0 +1,62 @@
using MagicCarpetContracts.BusinessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Extensions;
using MagicCarpetContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace MagicCarpetBusinessLogic.Implementations;
public class SuppliesBusinessLogicContract(ISuppliesStorageContract suppliesStorageContract, ILogger logger) : ISuppliesBusinessLogicContract
{
private readonly ISuppliesStorageContract _suppliesStorageContract = suppliesStorageContract;
private readonly ILogger _logger = logger;
public List<SuppliesDataModel> GetAllTours()
{
_logger.LogInformation("GetAllTours");
return _suppliesStorageContract.GetList() ?? throw new NullListException();
return [];
}
public SuppliesDataModel GetTourByData(string data)
{
_logger.LogInformation("Get element by data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ElementNotFoundException(data);
}
return _suppliesStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
return new("", TourType.None, DateTime.UtcNow, 0, []);
}
public void InsertTour(SuppliesDataModel suppliesDataModel)
{
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(suppliesDataModel));
ArgumentNullException.ThrowIfNull(suppliesDataModel);
suppliesDataModel.Validate();
_suppliesStorageContract.AddElement(suppliesDataModel);
}
public void UpdateTour(SuppliesDataModel suppliesDataModel)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(suppliesDataModel));
ArgumentNullException.ThrowIfNull(suppliesDataModel);
suppliesDataModel.Validate();
_suppliesStorageContract.UpdElement(suppliesDataModel);
}
}

View File

@@ -56,6 +56,10 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(tourDataModel));
ArgumentNullException.ThrowIfNull(tourDataModel);
tourDataModel.Validate();
if (_tourStorageContract.CheckComponents(tourDataModel))
{
throw new InsufficientException("Dont have tour in agency");
}
_tourStorageContract.AddElement(tourDataModel);
}
@@ -64,6 +68,10 @@ internal class TourBusinessLogicContract(ITourStorageContract tourStorageContrac
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(tourDataModel));
ArgumentNullException.ThrowIfNull(tourDataModel);
tourDataModel.Validate();
if (_tourStorageContract.CheckComponents(tourDataModel))
{
throw new InsufficientException("Dont have component in warehouse");
}
_tourStorageContract.UpdElement(tourDataModel);
}

View File

@@ -0,0 +1,17 @@
using MagicCarpetContracts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.BusinessLogicContracts;
public interface IAgencyBusinessLogicContract
{
List<AgencyDataModel> GetAllTours();
AgencyDataModel GetTourByData(string data);
void InsertTour(AgencyDataModel agencyDataModel);
void UpdateTour(AgencyDataModel agencyDataModel);
void DeleteTour(string id);
}

View File

@@ -0,0 +1,16 @@
using MagicCarpetContracts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.BusinessLogicContracts;
public interface ISuppliesBusinessLogicContract
{
List<SuppliesDataModel> GetAllTours();
SuppliesDataModel GetTourByData(string data);
void InsertTour(SuppliesDataModel componentDataModel);
void UpdateTour(SuppliesDataModel componentDataModel);
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.Exceptions;
public class InsufficientException(string message) : Exception(message)
{
}

View File

@@ -0,0 +1,17 @@
using MagicCarpetContracts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.StoragesContracts;
public interface IAgencyStorageContract
{
List<AgencyDataModel> GetList();
AgencyDataModel GetElementById(string id);
void AddElement(AgencyDataModel agencyDataModel);
void UpdElement(AgencyDataModel agencyDataModel);
void DelElement(string id);
}

View File

@@ -0,0 +1,16 @@
using MagicCarpetContracts.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.StoragesContracts;
public interface ISuppliesStorageContract
{
List<SuppliesDataModel> GetList(DateTime? startDate = null);
SuppliesDataModel GetElementById(string id);
void AddElement(SuppliesDataModel suppliesDataModel);
void UpdElement(SuppliesDataModel suppliesDataModel);
}

View File

@@ -16,4 +16,5 @@ public interface ITourStorageContract
void AddElement(TourDataModel tourDataModel);
void UpdElement(TourDataModel tourDataModel);
void DelElement(string id);
bool CheckComponents(TourDataModel tourDataModel);
}

View File

@@ -0,0 +1,296 @@
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 AgnecyBusinessLogicContractTests
{
private IAgencyBusinessLogicContract _agencyBusinessLogicContract;
private Mock<IAgencyStorageContract> _agencyStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_agencyStorageContract = new Mock<IAgencyStorageContract>();
_agencyBusinessLogicContract = new AgencyBusinessLogicContract(_agencyStorageContract.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_agencyStorageContract.Reset();
}
public void GetAllSupplies_ReturnListOfRecords_Test()
{
// Arrange
var listOriginal = new List<AgencyDataModel>()
{
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
new(Guid.NewGuid().ToString(), TourType.Beach, 1, []),
};
_agencyStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
// Act
var list = _agencyBusinessLogicContract.GetAllTours();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
}
[Test]
public void GetAllSupplies_ReturnEmptyList_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.GetList()).Returns([]);
// Act
var list = _agencyBusinessLogicContract.GetAllTours();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_agencyStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllSupplies_ReturnNull_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.GetList()).Returns((List<AgencyDataModel>)null);
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.GetAllTours(), Throws.TypeOf<NullListException>());
_agencyStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.GetAllTours(), Throws.TypeOf<StorageException>());
_agencyStorageContract.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, []);
_agencyStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
// Act
var element = _agencyBusinessLogicContract.GetTourByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_agencyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetToursByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _agencyBusinessLogicContract.GetTourByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _agencyBusinessLogicContract.GetTourByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_agencyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.GetTourByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_agencyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.GetTourByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_agencyStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), 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)]);
_agencyStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>()));
// Act
_agencyBusinessLogicContract.InsertTour(record);
// Assert
_agencyStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
}
[Test]
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_agencyStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Once);
}
[Test]
public void InsertSupplies_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.InsertTour(null), Throws.TypeOf<ArgumentNullException>());
_agencyStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Never);
}
[Test]
public void InsertTours_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _agencyBusinessLogicContract.InsertTour(new AgencyDataModel("id", TourType.Beach, 1, [])), Throws.TypeOf<ValidationException>());
_agencyStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), Times.Never);
}
[Test]
public void InsertSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.AddElement(It.IsAny<AgencyDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_agencyStorageContract.Verify(x => x.AddElement(It.IsAny<AgencyDataModel>()), 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)]);
_agencyStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>()));
// Act
_agencyBusinessLogicContract.UpdateTour(record);
// Assert
_agencyStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
}
[Test]
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
_agencyStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
}
[Test]
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_agencyStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
}
[Test]
public void UpdateSupplies_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.UpdateTour(null), Throws.TypeOf<ArgumentNullException>());
_agencyStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Never);
}
[Test]
public void UpdateTours_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _agencyBusinessLogicContract.UpdateTour(new AgencyDataModel(Guid.NewGuid().ToString(), TourType.Beach, 1, [])), Throws.TypeOf<ValidationException>());
_agencyStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Never);
}
[Test]
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_agencyStorageContract.Setup(x => x.UpdElement(It.IsAny<AgencyDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _agencyBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), TourType.Beach, 1,
[new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_agencyStorageContract.Verify(x => x.UpdElement(It.IsAny<AgencyDataModel>()), Times.Once);
}
[Test]
public void DeleteTours_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_agencyStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; });
//Act
_agencyBusinessLogicContract.DeleteTour(id);
//Assert
_agencyStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once); Assert.That(flag);
}
[Test]
public void DeleteTours_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_agencyStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() => _agencyBusinessLogicContract.DeleteTour(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_agencyStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteTours_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _agencyBusinessLogicContract.DeleteTour(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _agencyBusinessLogicContract.DeleteTour(string.Empty), Throws.TypeOf<ArgumentNullException>());
_agencyStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteTours_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _agencyBusinessLogicContract.DeleteTour("id"),
Throws.TypeOf<ValidationException>());
_agencyStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteTours_StorageThrowError_ThrowException_Test()
{
//Arrange
_agencyStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _agencyBusinessLogicContract.DeleteTour(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_agencyStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,242 @@
using MagicCarpetBusinessLogic.Implementations;
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 SuppliesBusinessLogicContractTests
{
private SuppliesBusinessLogicContract _suppliesBusinessLogicContract;
private Mock<ISuppliesStorageContract> _suppliesStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_suppliesStorageContract = new Mock<ISuppliesStorageContract>();
_suppliesBusinessLogicContract = new SuppliesBusinessLogicContract(_suppliesStorageContract.Object, new Mock<ILogger>().Object);
}
[TearDown]
public void TearDown()
{
_suppliesStorageContract.Reset();
}
[Test]
public void GetAllSupplies_ReturnListOfRecords_Test()
{
// Arrange
var listOriginal = new List<SuppliesDataModel>()
{
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, []),
};
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns(listOriginal);
// Act
var list = _suppliesBusinessLogicContract.GetAllTours();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
}
[Test]
public void GetAllSupplies_ReturnEmptyList_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns([]);
// Act
var list = _suppliesBusinessLogicContract.GetAllTours();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllSupplies_ReturnNull_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Returns((List<SuppliesDataModel>)null);
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.GetAllTours(), Throws.TypeOf<NullListException>());
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.GetAllTours(), Throws.TypeOf<StorageException>());
_suppliesStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetSuppliesByData_GetById_ReturnRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var record = new SuppliesDataModel(id, TourType.Beach, DateTime.Now, 1, []);
_suppliesStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
// Act
var element = _suppliesBusinessLogicContract.GetTourByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetToursByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _suppliesBusinessLogicContract.GetTourByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _suppliesBusinessLogicContract.GetTourByData(string.Empty), Throws.TypeOf<ArgumentNullException>());
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSuppliesByData_GetById_NotFoundRecord_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.GetTourByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSuppliesByData_StorageThrowError_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.GetTourByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
_suppliesStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertSupplies_CorrectRecord_Test()
{
// Arrange
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>()));
// Act
_suppliesBusinessLogicContract.InsertTour(record);
// Assert
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
}
[Test]
public void InsertSupplies_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
}
[Test]
public void InsertSupplies_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.InsertTour(null), Throws.TypeOf<ArgumentNullException>());
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
}
[Test]
public void InsertTours_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _suppliesBusinessLogicContract.InsertTour(new SuppliesDataModel("id", TourType.Beach, DateTime.UtcNow, 1, [])), Throws.TypeOf<ValidationException>());
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Never);
}
[Test]
public void InsertSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.AddElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_suppliesStorageContract.Verify(x => x.AddElement(It.IsAny<SuppliesDataModel>()), Times.Once);
}
[Test]
public void UpdateSupplies_CorrectRecord_Test()
{
// Arrange
var record = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>()));
// Act
_suppliesBusinessLogicContract.UpdateTour(record);
// Assert
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
}
[Test]
public void UpdateSupplies_RecordWithIncorrectData_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementNotFoundException>());
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
}
[Test]
public void UpdateSupplies_RecordWithExistsData_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
}
[Test]
public void UpdateSupplies_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.UpdateTour(null), Throws.TypeOf<ArgumentNullException>());
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
}
[Test]
public void UpdateTours_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _suppliesBusinessLogicContract.UpdateTour(new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1, [])), Throws.TypeOf<ValidationException>());
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Never);
}
[Test]
public void UpdateSupplies_StorageThrowError_ThrowException_Test()
{
// Arrange
_suppliesStorageContract.Setup(x => x.UpdElement(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _suppliesBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), TourType.Beach, DateTime.Now, 1,
[new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_suppliesStorageContract.Verify(x => x.UpdElement(It.IsAny<SuppliesDataModel>()), Times.Once);
}
}