| Task<Customer> GetCustomerById(string id); | Task<Customer> GetCustomerById(string id); | ||||
| Task<bool> AddNotification(string receiverId, string roomId); | |||||
| Task AddNotification(Customer c); | |||||
| Task<List<Notification>> ReadNotifications(string userId); | Task<List<Notification>> ReadNotifications(string userId); | ||||
| Task<bool> DeleteNotification(string userId, string roomId); | |||||
| Task DeleteNotification(Customer c, Notification n); | |||||
| } | } | ||||
| } | } |
| return customer; | return customer; | ||||
| } | } | ||||
| public async Task<bool> AddNotification(string receiverId, string roomId) | |||||
| public async Task AddNotification(Customer receiver) | |||||
| { | { | ||||
| var receiver = await GetCustomerById(receiverId); | |||||
| if (receiver == null) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| var notificationExists = receiver.Notifications.Where(x => x.RoomId == roomId).FirstOrDefault(); | |||||
| if (notificationExists == null) | |||||
| { | |||||
| var notification = new Notification | |||||
| { | |||||
| Count = 1, | |||||
| RoomId = roomId | |||||
| }; | |||||
| receiver.Notifications.Add(notification); | |||||
| } | |||||
| else | |||||
| { | |||||
| notificationExists.Count++; | |||||
| } | |||||
| await _customerManager.UpdateAsync(receiver); | await _customerManager.UpdateAsync(receiver); | ||||
| return true; | |||||
| } | } | ||||
| public async Task<List<Notification>> ReadNotifications(string userId) | public async Task<List<Notification>> ReadNotifications(string userId) | ||||
| return user.Notifications; | return user.Notifications; | ||||
| } | } | ||||
| public async Task<bool> DeleteNotification(string userId, string roomId) | |||||
| public async Task DeleteNotification(Customer user, Notification notification) | |||||
| { | { | ||||
| var user = await GetCustomerById(userId); | |||||
| if (user == null) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| var notification = user.Notifications.Where(x => x.RoomId == roomId).FirstOrDefault(); | |||||
| if (notification == null) | |||||
| { | |||||
| return true; | |||||
| } | |||||
| user.Notifications.Remove(notification); | user.Notifications.Remove(notification); | ||||
| await _customerManager.UpdateAsync(user); | await _customerManager.UpdateAsync(user); | ||||
| return true; | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Business.Interfaces; | using Diligent.WebAPI.Business.Interfaces; | ||||
| using Diligent.WebAPI.Data.Entities; | |||||
| using Diligent.WebAPI.Host.Exceptions; | |||||
| using Diligent.WebAPI.Host.Mediator.Notifications.Commands; | using Diligent.WebAPI.Host.Mediator.Notifications.Commands; | ||||
| using MediatR; | using MediatR; | ||||
| public async Task<Unit> Handle(AddNotificationCommand request, CancellationToken cancellationToken) | public async Task<Unit> Handle(AddNotificationCommand request, CancellationToken cancellationToken) | ||||
| { | { | ||||
| if (request == null) | |||||
| var receiver = await _customerService.GetCustomerById(request.Notification.ReceiverId); | |||||
| if (receiver == null) | |||||
| { | { | ||||
| throw new BadHttpRequestException("Object cannot be null"); | |||||
| throw new NotFoundException(); | |||||
| } | } | ||||
| var result = await _customerService.AddNotification(request.Notification.ReceiverId, request.Notification.RoomId); | |||||
| var notificationExists = receiver.Notifications.Where(x => x.RoomId == request.Notification.RoomId).FirstOrDefault(); | |||||
| if (notificationExists == null) | |||||
| { | |||||
| var notification = new Notification | |||||
| { | |||||
| Count = 1, | |||||
| RoomId = request.Notification.RoomId | |||||
| }; | |||||
| if (!result) | |||||
| receiver.Notifications.Add(notification); | |||||
| } | |||||
| else | |||||
| { | { | ||||
| throw new Exception("Problem with saving notification in database"); | |||||
| notificationExists.Count++; | |||||
| } | } | ||||
| await _customerService.AddNotification(receiver); | |||||
| return new Unit(); | return new Unit(); | ||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Business.Interfaces; | using Diligent.WebAPI.Business.Interfaces; | ||||
| using Diligent.WebAPI.Host.Exceptions; | |||||
| using Diligent.WebAPI.Host.Mediator.Notifications.Commands; | using Diligent.WebAPI.Host.Mediator.Notifications.Commands; | ||||
| using MediatR; | using MediatR; | ||||
| public async Task<Unit> Handle(DeleteNotificationCommand request, CancellationToken cancellationToken) | public async Task<Unit> Handle(DeleteNotificationCommand request, CancellationToken cancellationToken) | ||||
| { | { | ||||
| if (request == null) | |||||
| var user = await _customerService.GetCustomerById(request.NotificationData.UserId); | |||||
| if (user == null) | |||||
| { | { | ||||
| throw new BadHttpRequestException("Object cannot be null"); | |||||
| throw new NotFoundException(); | |||||
| } | } | ||||
| var result = await _customerService.DeleteNotification(request.NotificationData.UserId, request.NotificationData.RoomId); | |||||
| if (!result) | |||||
| var notification = user.Notifications.Where(x => x.RoomId == request.NotificationData.RoomId).FirstOrDefault(); | |||||
| if (notification == null) | |||||
| { | { | ||||
| throw new Exception("Problem with deleting notification"); | |||||
| throw new NotFoundException(); | |||||
| } | } | ||||
| await _customerService.DeleteNotification(user, notification); | |||||
| return new Unit(); | return new Unit(); | ||||
| } | } | ||||
| } | } |
| public async Task<List<NotificationReadDTO>> Handle(GetNotificationsQuery request, CancellationToken cancellationToken) | public async Task<List<NotificationReadDTO>> Handle(GetNotificationsQuery request, CancellationToken cancellationToken) | ||||
| { | { | ||||
| if (request == null) | |||||
| { | |||||
| throw new BadHttpRequestException("User id cannot be null"); | |||||
| } | |||||
| return _mapper.Map<List<NotificationReadDTO>>(await _customerService.ReadNotifications(request.UserId)); | return _mapper.Map<List<NotificationReadDTO>>(await _customerService.ReadNotifications(request.UserId)); | ||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Business.Services; | |||||
| using Diligent.WebAPI.Data.Entities; | |||||
| using FluentAssertions; | |||||
| using Microsoft.AspNetCore.Identity; | |||||
| using Moq; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Tests | |||||
| { | |||||
| [TestFixture] | |||||
| public class CustomerServiceTest | |||||
| { | |||||
| private CustomerService service; | |||||
| [Test] | |||||
| public async Task DeleteNotification_WhenUserIsNull_ReturnsFalse() | |||||
| { | |||||
| // Arrange | |||||
| var userManagerMock = new Mock<UserManager<Customer>>(Mock.Of<IUserStore<Customer>>(), null, null, null, null, null, null, null, null); | |||||
| userManagerMock.Setup(x => x.FindByIdAsync(It.IsAny<string>())).ReturnsAsync((Customer)null); | |||||
| service = new CustomerService(userManagerMock.Object); | |||||
| // Act | |||||
| var result = await service.DeleteNotification("", ""); | |||||
| // Assert | |||||
| //Assert.IsFalse(result); | |||||
| result.Should().BeFalse(); | |||||
| } | |||||
| [Test] | |||||
| public async Task DeleteNotification_UserIsNotNullAndNotificationIsNull_ReturnsFalse() | |||||
| { | |||||
| // Arrange | |||||
| var userManagerMock = new Mock<UserManager<Customer>>(Mock.Of<IUserStore<Customer>>(), null, null, null, null, null, null, null, null); | |||||
| var customer = new Customer | |||||
| { | |||||
| FirstName = "User", | |||||
| LastName = "Someone", | |||||
| Notifications = new List<Notification> | |||||
| { | |||||
| new Notification | |||||
| { | |||||
| RoomId = "Room1", Count = 1 | |||||
| } | |||||
| } | |||||
| }; | |||||
| userManagerMock.Setup(x => x.FindByIdAsync(It.IsAny<string>())).ReturnsAsync(customer); | |||||
| service = new CustomerService(userManagerMock.Object); | |||||
| // Act | |||||
| var result = await service.DeleteNotification("arg", "Room1"); | |||||
| // Assert | |||||
| result.Should().BeTrue(); | |||||
| } | |||||
| } | |||||
| } |
| namespace Tests | namespace Tests | ||||
| { | { | ||||
| [TestFixture] | [TestFixture] | ||||
| public class ExceptionHandlingMiddlewareTest | |||||
| public class ExceptionHandlingMiddlewareTests | |||||
| { | { | ||||
| private Mock<ILogger<ExceptionHandlingMiddleware>> _logger; | private Mock<ILogger<ExceptionHandlingMiddleware>> _logger; | ||||
| 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<ICustomerService> _customerServiceMock; | |||||
| [SetUp] | |||||
| public void Setup() | |||||
| { | |||||
| _customerServiceMock = new Mock<ICustomerService>(); | |||||
| } | |||||
| [Test] | |||||
| public async Task DeleteNotification_CustomerIsNull_ThrowNotFoundException() | |||||
| { | |||||
| _customerServiceMock.Setup(x => x.GetCustomerById(It.IsAny<string>())) | |||||
| .ReturnsAsync((Customer)null); | |||||
| var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO | |||||
| { | |||||
| UserId = "1", | |||||
| RoomId = "1" | |||||
| }); | |||||
| var deleteNotificationHandler = new DeleteNotificationHandler(_customerServiceMock.Object); | |||||
| Assert.That(async () => await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken()), Throws.Exception.TypeOf<NotFoundException>()); | |||||
| } | |||||
| [Test] | |||||
| public async Task DeleteNotification_UserNotificationIsNull_ThrowNotFoundException() | |||||
| { | |||||
| _customerServiceMock.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(_customerServiceMock.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 | |||||
| } | |||||
| } | |||||
| }; | |||||
| _customerServiceMock.Setup(x => x.GetCustomerById(It.IsAny<string>())) | |||||
| .ReturnsAsync(customer); | |||||
| var deleteNotificationCommand = new DeleteNotificationCommand(new NotificationDeleteDTO | |||||
| { | |||||
| UserId = "1", | |||||
| RoomId = "2" | |||||
| }); | |||||
| var deleteNotificationHandler = new DeleteNotificationHandler(_customerServiceMock.Object); | |||||
| await deleteNotificationHandler.Handle(deleteNotificationCommand, new CancellationToken()); | |||||
| _customerServiceMock.Verify(mock => mock.DeleteNotification(customer, customer.Notifications[1])); | |||||
| } | |||||
| } | |||||
| } |
| 1616294871 | |||||
| 671024648 |
| 4260cf941650494d42f82ac070d88a866ed26069 | |||||
| 16c93e8d4a4c74f74ee47efac450a1cb0b2fabe7 |