using Diligent.WebAPI.Business.Interfaces; using Diligent.WebAPI.Data.Entities; using Diligent.WebAPI.Host.DTOs.Notification; using Diligent.WebAPI.Host.Exceptions; using Diligent.WebAPI.Host.Mediator.Notifications.Commands; using Diligent.WebAPI.Host.Mediator.Notifications.Handlers; using Moq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tests { [TestFixture] public class NotificationTests { private Mock _customerRepositoryMock; [SetUp] public void Setup() { _customerRepositoryMock = new Mock(); } [Test] public async Task DeleteNotification_CustomerIsNull_ThrowNotFoundException() { _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny())) .ReturnsAsync((Customer)null); var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO { UserId = "1", RoomId = "1" }); var deleteNotificationHandler = new DeleteNotificationHandler(_customerRepositoryMock.Object); Assert.That(async () => await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken()), Throws.Exception.TypeOf()); } [Test] public async Task DeleteNotification_UserNotificationIsNull_ThrowNotFoundException() { _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny())) .ReturnsAsync(new Customer { FirstName = "Dzenis", LastName = "Hadzifejzovic", Notifications = new List() { new Notification { RoomId = "1", Count = 1 }, new Notification { RoomId = "2", Count = 2 } } }); var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO { UserId = "1", RoomId = "3" }); var deleteNotificationHandler = new DeleteNotificationHandler(_customerRepositoryMock.Object); Assert.That(async () => await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken()), Throws.Exception.TypeOf()); } [Test] public async Task DeleteNotification_UserNotificationIsNotNull_CallsMethod() { var customer = new Customer { FirstName = "Dzenis", LastName = "Hadzifejzovic", Notifications = new List() { new Notification { RoomId = "1", Count = 1 }, new Notification { RoomId = "2", Count = 2 } } }; _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny())) .ReturnsAsync(customer); var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO { UserId = "1", RoomId = "2" }); var deleteNotificationHandler = new DeleteNotificationHandler(_customerRepositoryMock.Object); await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken()); _customerRepositoryMock.Verify(mock => mock.DeleteNotification(customer, customer.Notifications[1])); } [Test] public async Task AddNotification_ReceiverIsNull_ThrowsNotFoundException() { _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny())) .ReturnsAsync((Customer)null); var command = new AddNotificationCommand(new NotificationSaveDTO { ReceiverId = "1", RoomId = "1" }); var handler = new AddNotificationHandler(_customerRepositoryMock.Object); Assert.That(async () => await handler.Handle(command, new CancellationToken()), Throws.Exception.TypeOf()); } [Test] public async Task AddNotification_NotificationIsNull_ThrowsNotFoundException() { var customer = new Customer { FirstName = "Dzenis", LastName = "Hadzifejzovic", Notifications = new List() { new Notification { RoomId = "1", Count = 1 }, new Notification { RoomId = "2", Count = 2 } } }; _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny())) .ReturnsAsync(customer); var command = new AddNotificationCommand(new NotificationSaveDTO { ReceiverId = "1", RoomId = "3" }); var handler = new AddNotificationHandler(_customerRepositoryMock.Object); await handler.Handle(command, new CancellationToken()); _customerRepositoryMock.Verify(mock => mock.AddNotification(customer)); } [Test] public async Task AddNotification_NotificationIsNotNull_ThrowsNotFoundException() { var customer = new Customer { FirstName = "Dzenis", LastName = "Hadzifejzovic", Notifications = new List() { new Notification { RoomId = "1", Count = 1 }, new Notification { RoomId = "2", Count = 2 } } }; _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny())) .ReturnsAsync(customer); var command = new AddNotificationCommand(new NotificationSaveDTO { ReceiverId = "1", RoomId = "2" }); var handler = new AddNotificationHandler(_customerRepositoryMock.Object); await handler.Handle(command, new CancellationToken()); _customerRepositoryMock.Verify(mock => mock.AddNotification(customer)); } } }