You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationTests.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Diligent.WebAPI.Business.Interfaces;
  2. using Diligent.WebAPI.Data.Entities;
  3. using Diligent.WebAPI.Host.DTOs.Notification;
  4. using Diligent.WebAPI.Host.Exceptions;
  5. using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
  6. using Diligent.WebAPI.Host.Mediator.Notifications.Handlers;
  7. using Moq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace Tests
  14. {
  15. [TestFixture]
  16. public class NotificationTests
  17. {
  18. private Mock<ICustomerRepository> _customerRepositoryMock;
  19. [SetUp]
  20. public void Setup()
  21. {
  22. _customerRepositoryMock = new Mock<ICustomerRepository>();
  23. }
  24. [Test]
  25. public async Task DeleteNotification_CustomerIsNull_ThrowNotFoundException()
  26. {
  27. _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
  28. .ReturnsAsync((Customer)null);
  29. var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO
  30. {
  31. UserId = "1",
  32. RoomId = "1"
  33. });
  34. var deleteNotificationHandler = new DeleteNotificationHandler(_customerRepositoryMock.Object);
  35. Assert.That(async () => await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken()), Throws.Exception.TypeOf<NotFoundException>());
  36. }
  37. [Test]
  38. public async Task DeleteNotification_UserNotificationIsNull_ThrowNotFoundException()
  39. {
  40. _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
  41. .ReturnsAsync(new Customer
  42. {
  43. FirstName = "Dzenis",
  44. LastName = "Hadzifejzovic",
  45. Notifications = new List<Notification>()
  46. {
  47. new Notification
  48. {
  49. RoomId = "1",
  50. Count = 1
  51. },
  52. new Notification
  53. {
  54. RoomId = "2",
  55. Count = 2
  56. }
  57. }
  58. });
  59. var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO
  60. {
  61. UserId = "1",
  62. RoomId = "3"
  63. });
  64. var deleteNotificationHandler = new DeleteNotificationHandler(_customerRepositoryMock.Object);
  65. Assert.That(async () => await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken()), Throws.Exception.TypeOf<NotFoundException>());
  66. }
  67. [Test]
  68. public async Task DeleteNotification_UserNotificationIsNotNull_CallsMethod()
  69. {
  70. var customer = new Customer
  71. {
  72. FirstName = "Dzenis",
  73. LastName = "Hadzifejzovic",
  74. Notifications = new List<Notification>()
  75. {
  76. new Notification
  77. {
  78. RoomId = "1",
  79. Count = 1
  80. },
  81. new Notification
  82. {
  83. RoomId = "2",
  84. Count = 2
  85. }
  86. }
  87. };
  88. _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
  89. .ReturnsAsync(customer);
  90. var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO
  91. {
  92. UserId = "1",
  93. RoomId = "2"
  94. });
  95. var deleteNotificationHandler = new DeleteNotificationHandler(_customerRepositoryMock.Object);
  96. await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken());
  97. _customerRepositoryMock.Verify(mock => mock.DeleteNotification(customer, customer.Notifications[1]));
  98. }
  99. [Test]
  100. public async Task AddNotification_ReceiverIsNull_ThrowsNotFoundException()
  101. {
  102. _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
  103. .ReturnsAsync((Customer)null);
  104. var command = new AddNotificationCommand(new NotificationSaveDTO
  105. {
  106. ReceiverId = "1",
  107. RoomId = "1"
  108. });
  109. var handler = new AddNotificationHandler(_customerRepositoryMock.Object);
  110. Assert.That(async () => await handler.Handle(command, new CancellationToken()), Throws.Exception.TypeOf<NotFoundException>());
  111. }
  112. [Test]
  113. public async Task AddNotification_NotificationIsNull_ThrowsNotFoundException()
  114. {
  115. var customer = new Customer
  116. {
  117. FirstName = "Dzenis",
  118. LastName = "Hadzifejzovic",
  119. Notifications = new List<Notification>()
  120. {
  121. new Notification
  122. {
  123. RoomId = "1",
  124. Count = 1
  125. },
  126. new Notification
  127. {
  128. RoomId = "2",
  129. Count = 2
  130. }
  131. }
  132. };
  133. _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
  134. .ReturnsAsync(customer);
  135. var command = new AddNotificationCommand(new NotificationSaveDTO
  136. {
  137. ReceiverId = "1",
  138. RoomId = "3"
  139. });
  140. var handler = new AddNotificationHandler(_customerRepositoryMock.Object);
  141. await handler.Handle(command, new CancellationToken());
  142. _customerRepositoryMock.Verify(mock => mock.AddNotification(customer));
  143. }
  144. [Test]
  145. public async Task AddNotification_NotificationIsNotNull_ThrowsNotFoundException()
  146. {
  147. var customer = new Customer
  148. {
  149. FirstName = "Dzenis",
  150. LastName = "Hadzifejzovic",
  151. Notifications = new List<Notification>()
  152. {
  153. new Notification
  154. {
  155. RoomId = "1",
  156. Count = 1
  157. },
  158. new Notification
  159. {
  160. RoomId = "2",
  161. Count = 2
  162. }
  163. }
  164. };
  165. _customerRepositoryMock.Setup(x => x.GetCustomerById(It.IsAny<string>()))
  166. .ReturnsAsync(customer);
  167. var command = new AddNotificationCommand(new NotificationSaveDTO
  168. {
  169. ReceiverId = "1",
  170. RoomId = "2"
  171. });
  172. var handler = new AddNotificationHandler(_customerRepositoryMock.Object);
  173. await handler.Handle(command, new CancellationToken());
  174. _customerRepositoryMock.Verify(mock => mock.AddNotification(customer));
  175. }
  176. }
  177. }