| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 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<ICustomerRepository> _customerRepositoryMock;
-
- [SetUp]
- public void Setup()
- {
- _customerRepositoryMock = new Mock<ICustomerRepository>();
- }
-
- [Test]
- public async Task DeleteNotification_CustomerIsNull_ThrowNotFoundException()
- {
- _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
- .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<NotFoundException>());
- }
-
- [Test]
- public async Task DeleteNotification_UserNotificationIsNull_ThrowNotFoundException()
- {
- _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
- .ReturnsAsync(new Customer
- {
- FirstName = "Dzenis",
- LastName = "Hadzifejzovic",
- Notifications = new List<Notification>()
- {
- 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<NotFoundException>());
- }
-
- [Test]
- public async Task DeleteNotification_UserNotificationIsNotNull_CallsMethod()
- {
- var customer = new Customer
- {
- FirstName = "Dzenis",
- LastName = "Hadzifejzovic",
- Notifications = new List<Notification>()
- {
- new Notification
- {
- RoomId = "1",
- Count = 1
- },
- new Notification
- {
- RoomId = "2",
- Count = 2
- }
- }
- };
-
- _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
- .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<string>()))
- .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<NotFoundException>());
- }
-
- [Test]
- public async Task AddNotification_NotificationIsNull_ThrowsNotFoundException()
- {
- var customer = new Customer
- {
- FirstName = "Dzenis",
- LastName = "Hadzifejzovic",
- Notifications = new List<Notification>()
- {
- new Notification
- {
- RoomId = "1",
- Count = 1
- },
- new Notification
- {
- RoomId = "2",
- Count = 2
- }
- }
- };
-
- _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
- .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<Notification>()
- {
- new Notification
- {
- RoomId = "1",
- Count = 1
- },
- new Notification
- {
- RoomId = "2",
- Count = 2
- }
- }
- };
-
- _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
- .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));
- }
- }
- }
|