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 _guestStorageContract; [OneTimeSetUp] public void OneTimeSetUp() { _guestStorageContract = new Mock(); _guestBusinessLogicContract = new GuestBusinessLogicContract(_guestStorageContract.Object, new Mock().Object); } [SetUp] public void SetUp() { _guestStorageContract.Reset(); } [Test] public void GetAllGuests_ReturnListOfRecords_Test() { //Arrange var listOriginal = new List() { 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()); _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()); _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()), 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()), 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()), Times.Once); } [Test] public void GetGuestByData_EmptyData_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.GetGuestByData(null), Throws.TypeOf()); Assert.That(() => _guestBusinessLogicContract.GetGuestByData(string.Empty), Throws.TypeOf()); _guestStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); _guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); } [Test] public void GetGuestByData_GetById_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.GetGuestByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _guestStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); _guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); } [Test] public void GetGuestByData_GetByFio_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.GetGuestByData("fio"), Throws.TypeOf()); _guestStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); _guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); } [Test] public void GetGuestByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.GetGuestByData("+7-111-1111112"), Throws.TypeOf()); _guestStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); _guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); } [Test] public void GetGuestByData_StorageThrowError_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); _guestStorageContract.Setup(x => x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); _guestStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _guestBusinessLogicContract.GetGuestByData(Guid.NewGuid().ToString()), Throws.TypeOf()); Assert.That(() => _guestBusinessLogicContract.GetGuestByData("fio"), Throws.TypeOf()); Assert.That(() => _guestBusinessLogicContract.GetGuestByData("+7-111-1111112"), Throws.TypeOf()); _guestStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _guestStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); _guestStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), 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())) .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()), Times.Once); Assert.That(flag); } [Test] public void InsertGuest_RecordWithExistsData_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _guestBusinessLogicContract.InsertGuest(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0)), Throws.TypeOf()); _guestStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void InsertGuest_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.InsertGuest(null), Throws.TypeOf()); _guestStorageContract.Verify(x => x.AddElement(It.IsAny()), 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()); _guestStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertGuest_StorageThrowError_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _guestBusinessLogicContract.InsertGuest(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0)), Throws.TypeOf()); _guestStorageContract.Verify(x => x.AddElement(It.IsAny()), 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())) .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()), Times.Once); Assert.That(flag); } [Test] public void UpdateGuest_RecordWithIncorrectData_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); //Act&Assert Assert.That(() => _guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0)), Throws.TypeOf()); _guestStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateGuest_RecordWithExistsData_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0)), Throws.TypeOf()); _guestStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateGuest_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.UpdateGuest(null), Throws.TypeOf()); _guestStorageContract.Verify(x => x.UpdElement(It.IsAny()), 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()); _guestStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateGuest_StorageThrowError_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _guestBusinessLogicContract.UpdateGuest(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", 0)), Throws.TypeOf()); _guestStorageContract.Verify(x => x.UpdElement(It.IsAny()), 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()), Times.Once); Assert.That(flag); } [Test] public void DeleteGuest_RecordWithIncorrectId_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new ElementNotFoundException("")); //Act&Assert Assert.That(() => _guestBusinessLogicContract.DeleteGuest(Guid.NewGuid().ToString()), Throws.TypeOf()); _guestStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } [Test] public void DeleteGuest_IdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.DeleteGuest(null), Throws.TypeOf()); Assert.That(() => _guestBusinessLogicContract.DeleteGuest(string.Empty), Throws.TypeOf()); _guestStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteGuest_IdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _guestBusinessLogicContract.DeleteGuest("id"), Throws.TypeOf()); _guestStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteGuest_StorageThrowError_ThrowException_Test() { //Arrange _guestStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _guestBusinessLogicContract.DeleteGuest(Guid.NewGuid().ToString()), Throws.TypeOf()); _guestStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } }