TheSquirrel/Squirrel/SquirrelTests/BusinessLogicsContractsTests/GuestBusinessLogicContractTests.cs
2025-02-27 15:22:45 +04:00

430 lines
16 KiB
C#

using Squirrel.DataModels;
using Squirrel.StoragesContracts;
using Moq;
using Squirrel.BusinessLogicsContracts;
using SquirelBusinessLogic.Implementations;
using Squirrel.Exceptions;
using Microsoft.Extensions.Logging;
namespace SquirrelTests.BusinessLogicsContractsTests;
[TestFixture]
internal class GuestBusinessLogicContractTests
{
private GuestBusinessLogicContract _guestBusinessLogicContract;
private Mock<IGuestStorageContract> _guestStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_guestStorageContract = new Mock<IGuestStorageContract>();
_guestBusinessLogicContract = new GuestBusinessLogicContract(_guestStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_guestStorageContract.Reset();
}
[Test]
public void GetAllGuests_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<GuestDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", 0),
new(Guid.NewGuid().ToString(), "fio 2", "+7-555-444-33-23", 10),
new(Guid.NewGuid().ToString(), "fio 3", "+7-777-777-7777", 0),
};
_guestStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act
var list = _guestBusinessLogicContract.GetAllGuests();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
}
[Test]
public void GetAllGuests_ReturnEmptyList_Test()
{
//Arrange
_guestStorageContract.Setup(x => x.GetList()).Returns([]);
//Act
var list = _guestBusinessLogicContract.GetAllGuests();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_guestStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllGuests_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.GetAllGuests(), Throws.TypeOf<NullListException>());
_guestStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllGuests_StorageThrowError_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x => x.GetList()).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.GetAllGuests(), Throws.TypeOf<StorageException>());
_guestStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetGuestByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new GuestDataModel(id, "fio", "+7-111-111-11-11", 0);
_guestStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act
var element = _guestBusinessLogicContract.GetGuestByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetGuestByData_GetByFio_ReturnRecord_Test()
{
//Arrange
var fio = "fio";
var record = new GuestDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11", 0);
_guestStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(record);
//Act
var element = _guestBusinessLogicContract.GetGuestByData(fio);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.FIO, Is.EqualTo(fio));
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetGuestByData_GetByPhoneNumber_ReturnRecord_Test()
{
//Arrange
var phoneNumber = "+7-111-1111111";
var record = new GuestDataModel(Guid.NewGuid().ToString(), "fio", phoneNumber, 0);
_guestStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record);
//Act
var element = _guestBusinessLogicContract.GetGuestByData(phoneNumber);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber));
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetGuestByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.GetGuestByData(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _guestBusinessLogicContract.GetGuestByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetGuestByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.GetGuestByData(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetGuestByData_GetByFio_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("fio"), Throws.TypeOf<ElementNotFoundException>());
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetGuestByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("+7-111-1111112"), Throws.TypeOf<ElementNotFoundException>());
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never);
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never);
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetGuestByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_guestStorageContract.Setup(x => x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
_guestStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.GetGuestByData(Guid.NewGuid().ToString()), Throws.TypeOf<StorageException>());
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("fio"), Throws.TypeOf<StorageException>());
Assert.That(() => _guestBusinessLogicContract.GetGuestByData("+7-111-1111112"), Throws.TypeOf<StorageException>());
_guestStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
_guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Once);
_guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertGuest_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new GuestDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 10);
_guestStorageContract.Setup(x =>
x.AddElement(It.IsAny<GuestDataModel>()))
.Callback((GuestDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO &&
x.PhoneNumber == record.PhoneNumber &&
x.DiscountSize == record.DiscountSize;
});
//Act
_guestBusinessLogicContract.InsertGuest(record);
//Assert
_guestStorageContract.Verify(x =>
x.AddElement(It.IsAny<GuestDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertGuest_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x =>
x.AddElement(It.IsAny<GuestDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.InsertGuest(new(Guid.NewGuid().ToString(), "fio",
"+7-111-111-11-11", 0)), Throws.TypeOf<ElementExistsException>());
_guestStorageContract.Verify(x =>
x.AddElement(It.IsAny<GuestDataModel>()), Times.Once);
}
[Test]
public void InsertGuest_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.InsertGuest(null),
Throws.TypeOf<ArgumentNullException>());
_guestStorageContract.Verify(x =>
x.AddElement(It.IsAny<GuestDataModel>()), Times.Never);
}
[Test]
public void InsertGuest_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.InsertGuest(new
GuestDataModel("id", "fio", "+7-111-111-11-11", 10)),
Throws.TypeOf<ValidationException>());
_guestStorageContract.Verify(x =>
x.AddElement(It.IsAny<GuestDataModel>()), Times.Never);
}
[Test]
public void InsertGuest_StorageThrowError_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x =>
x.AddElement(It.IsAny<GuestDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.InsertGuest(new(Guid.NewGuid().ToString(), "fio",
"+7-111-111-11-11", 0)), Throws.TypeOf<StorageException>());
_guestStorageContract.Verify(x =>
x.AddElement(It.IsAny<GuestDataModel>()), Times.Once);
}
[Test]
public void UpdateGuest_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new GuestDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0);
_guestStorageContract.Setup(x => x.UpdElement(It.IsAny<GuestDataModel>()))
.Callback((GuestDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO &&
x.PhoneNumber == record.PhoneNumber &&
x.DiscountSize == record.DiscountSize;
});
//Act
_guestBusinessLogicContract.UpdateGuest(record);
//Assert
_guestStorageContract.Verify(x =>
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateGuest_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x =>
x.UpdElement(It.IsAny<GuestDataModel>())).Throws(new
ElementNotFoundException(""));
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio",
"+7-111-111-11-11", 0)), Throws.TypeOf<ElementNotFoundException>());
_guestStorageContract.Verify(x =>
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
}
[Test]
public void UpdateGuest_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x =>
x.UpdElement(It.IsAny<GuestDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio",
"+7-111-111-11-11", 0)), Throws.TypeOf<ElementExistsException>());
_guestStorageContract.Verify(x =>
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
}
[Test]
public void UpdateGuest_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.UpdateGuest(null),
Throws.TypeOf<ArgumentNullException>());
_guestStorageContract.Verify(x =>
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Never);
}
[Test]
public void UpdateGuest_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.UpdateGuest(new
GuestDataModel("id", "fio", "+7-111-111-11-11", 10)),
Throws.TypeOf<ValidationException>());
_guestStorageContract.Verify(x =>
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Never);
}
[Test]
public void UpdateGuest_StorageThrowError_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x =>
x.UpdElement(It.IsAny<GuestDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0)), Throws.TypeOf<StorageException>());
_guestStorageContract.Verify(x =>
x.UpdElement(It.IsAny<GuestDataModel>()), Times.Once);
}
[Test]
public void DeleteGuest_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_guestStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
== id))).Callback(() => { flag = true; });
//Act
_guestBusinessLogicContract.DeleteGuest(id);
//Assert
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteGuest_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(""));
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.DeleteGuest(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void DeleteGuest_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.DeleteGuest(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_guestBusinessLogicContract.DeleteGuest(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteGuest_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _guestBusinessLogicContract.DeleteGuest("id"),
Throws.TypeOf<ValidationException>());
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteGuest_StorageThrowError_ThrowException_Test()
{
//Arrange
_guestStorageContract.Setup(x => x.DelElement(It.IsAny<string>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_guestBusinessLogicContract.DeleteGuest(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_guestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
}