using MagicCarpetBusinessLogic.Implementations; using MagicCarpetContracts.DataModels; using MagicCarpetContracts.Enums; using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.StoragesContracts; using MagicCarpetTests.DataModelTests; 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 TourBusinessLogicContractTests { private TourBusinessLogicContract _tourBusinessLogicContract; private Mock _tourStorageContract; [OneTimeSetUp] public void OneTimeSetUp() { _tourStorageContract = new Mock(); _tourBusinessLogicContract = new TourBusinessLogicContract(_tourStorageContract.Object, new Mock().Object); } [SetUp] public void SetUp() { _tourStorageContract.Reset(); } [Test] public void GetAllTours_ReturnListOfRecords_Test() { //Arrange var listOriginal = new List() { new(Guid.NewGuid().ToString(), "name 1", "country1", 15.5, TourType.Ski, [],[]), new(Guid.NewGuid().ToString(), "name 2", "country2", 10.1, TourType.Sightseeing,[],[]), new(Guid.NewGuid().ToString(), "name 3", "country3", 13.9, TourType.Beach,[],[]), }; _tourStorageContract.Setup(x => x.GetList()).Returns(listOriginal); //Act var list = _tourBusinessLogicContract.GetAllTours(); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Is.EquivalentTo(listOriginal)); } [Test] public void GetAllTours_ReturnEmptyList_Test() { //Arrange _tourStorageContract.Setup(x => x.GetList()).Returns([]); //Act var list = _tourBusinessLogicContract.GetAllTours(); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Has.Count.EqualTo(0)); _tourStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllTours_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetAllTours(), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetAllTours_StorageThrowError_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetAllTours(), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetList(), Times.Once); } [Test] public void GetTourHistoryByTour_ReturnListOfRecords_Test() { //Arrange var tourId = Guid.NewGuid().ToString(); var listOriginal = new List() { new(Guid.NewGuid().ToString(), 10), new(Guid.NewGuid().ToString(), 15), new(Guid.NewGuid().ToString(), 12), }; _tourStorageContract.Setup(x => x.GetHistoryByTourId(It.IsAny())).Returns(listOriginal); //Act var list = _tourBusinessLogicContract.GetTourHistoryByTour(tourId); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Is.EquivalentTo(listOriginal)); _tourStorageContract.Verify(x => x.GetHistoryByTourId(tourId), Times.Once); } [Test] public void GetTourHistoryByTour_ReturnEmptyList_Test() { //Arrange _tourStorageContract.Setup(x => x.GetHistoryByTourId(It.IsAny())).Returns([]); //Act var list = _tourBusinessLogicContract.GetTourHistoryByTour(Guid.NewGuid().ToString()); //Assert Assert.That(list, Is.Not.Null); Assert.That(list, Has.Count.EqualTo(0)); _tourStorageContract.Verify(x => x.GetHistoryByTourId(It.IsAny()), Times.Once); } [Test] public void GetTourHistoryByTour_TourIdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourHistoryByTour(null), Throws.TypeOf()); Assert.That(() => _tourBusinessLogicContract.GetTourHistoryByTour(string.Empty), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetHistoryByTourId(It.IsAny()), Times.Never); } [Test] public void GetTourHistoryByTour_TourIdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourHistoryByTour("tourId"), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetHistoryByTourId(It.IsAny()), Times.Never); } [Test] public void GetTourHistoryByTour_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourHistoryByTour(Guid.NewGuid().ToString()), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetHistoryByTourId(It.IsAny()), Times.Once); } [Test] public void GetTourHistoryByTour_StorageThrowError_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.GetHistoryByTourId(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourHistoryByTour(Guid.NewGuid().ToString()), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetHistoryByTourId(It.IsAny()), Times.Once); } [Test] public void GetTourByData_GetById_ReturnRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var record = new TourDataModel(id, "name", "country", 10, TourType.Ski, [], []); _tourStorageContract.Setup(x => x.GetElementById(id)).Returns(record); //Act var element = _tourBusinessLogicContract.GetTourByData(id); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.Id, Is.EqualTo(id)); _tourStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetTourByData_GetByName_ReturnRecord_Test() { //Arrange var name = "name"; var record = new TourDataModel(Guid.NewGuid().ToString(), name, "country", 10, TourType.Ski, [], []); _tourStorageContract.Setup(x => x.GetElementByName(name)).Returns(record); //Act var element = _tourBusinessLogicContract.GetTourByData(name); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.TourName, Is.EqualTo(name)); _tourStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void GetTourByData_GetByCountry_ReturnRecord_Test() { //Arrange var country = "country"; var record = new TourDataModel(Guid.NewGuid().ToString(), "name", country, 10, TourType.Ski, [], []); _tourStorageContract.Setup(x => x.GetElementByName(country)).Returns(record); //Act var element = _tourBusinessLogicContract.GetTourByData(country); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.TourCountry, Is.EqualTo(country)); _tourStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void GetTourByData_EmptyData_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourByData(null), Throws.TypeOf()); Assert.That(() => _tourBusinessLogicContract.GetTourByData(string.Empty), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); _tourStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Never); } [Test] public void GetTourByData_GetById_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); } [Test] public void GetTourByData_GetByName_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourByData("name"), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void GetTourByData_GetByCountry_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourByData("country"), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void GetTourByData_StorageThrowError_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); _tourStorageContract.Setup(x => x.GetElementByName(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _tourBusinessLogicContract.GetTourByData(Guid.NewGuid().ToString()), Throws.TypeOf()); Assert.That(() => _tourBusinessLogicContract.GetTourByData("name"), Throws.TypeOf()); _tourStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _tourStorageContract.Verify(x => x.GetElementByName(It.IsAny()), Times.Once); } [Test] public void InsertTour_CorrectRecord_Test() { //Arrange var flag = false; var record = new TourDataModel(Guid.NewGuid().ToString(), "name","country",10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); _agencyStorageContract.Setup(x => x.CheckComponents(It.IsAny())).Returns(true); _tourStorageContract.Setup(x => x.AddElement(It.IsAny())) .Callback((TourDataModel x) => { flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry && x.Price == record.Price && x.Type == record.Type; }); //Act _tourBusinessLogicContract.InsertTour(record); //Assert _agencyStorageContract.Verify(x => x.CheckComponents(It.IsAny()), Times.Once); _tourStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void InsertTour_RecordWithExistsData_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _tourBusinessLogicContract.InsertTour(new(Guid.NewGuid().ToString(), "name","country",10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _tourStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void InsertTour_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.InsertTour(null), Throws.TypeOf()); _tourStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertTour_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel("id", "name", "country", 10, TourType.Ski, [], [])), Throws.TypeOf()); _tourStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertTour_StorageThrowError_ThrowException_Test() { //Arrange Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name","country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); //Act&Assert _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void InsertFurniture_InsufficientError_ThrowException_Test() { //Arrange Assert.That(() => _tourBusinessLogicContract.InsertTour(new TourDataModel(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])),Throws.TypeOf()); //Act&Assert _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateTour_CorrectRecord_Test() { //Arrange var flag = false; var record = new TourDataModel(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]); _tourStorageContract.Setup(x => x.UpdElement(It.IsAny())) .Callback((TourDataModel x) => { flag = x.Id == record.Id && x.TourName == record.TourName && x.TourCountry == record.TourCountry && x.Price == record.Price && x.Type == record.Type; }); //Act _tourBusinessLogicContract.UpdateTour(record); //Assert _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void UpdateTour_RecordWithIncorrectData_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); //Act&Assert Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateTour_RecordWithExistsData_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf ()); _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateTour_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.UpdateTour(null), Throws.TypeOf()); _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateTour_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.UpdateTour(new TourDataModel("id", "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateTour_StorageThrowError_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _tourBusinessLogicContract.UpdateTour(new(Guid.NewGuid().ToString(), "name", "country", 10, TourType.Ski, [new TourSuppliesDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)], [new TourAgencyDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf()); _tourStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void DeleteTour_CorrectRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var flag = false; _tourStorageContract.Setup(x => x.DelElement(It.Is((string x) => x == id))).Callback(() => { flag = true; }); //Act _tourBusinessLogicContract.DeleteTour(id); //Assert _tourStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); Assert.That(flag); } [Test] public void DeleteTour_RecordWithIncorrectId_ThrowException_Test() { //Arrange var id = Guid.NewGuid().ToString(); _tourStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException(id)); //Act&Assert Assert.That(() => _tourBusinessLogicContract.DeleteTour(Guid.NewGuid().ToString()), Throws.TypeOf()); _tourStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } [Test] public void DeleteTour_IdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.DeleteTour(null), Throws.TypeOf()); Assert.That(() => _tourBusinessLogicContract.DeleteTour(string.Empty), Throws.TypeOf()); _tourStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteTour_IdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _tourBusinessLogicContract.DeleteTour("id"), Throws.TypeOf()); _tourStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteTour_StorageThrowError_ThrowException_Test() { //Arrange _tourStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _tourBusinessLogicContract.DeleteTour(Guid.NewGuid().ToString()), Throws.TypeOf()); _tourStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } }