Browse Source

tested deleteNotification handler

feature/mongo-services-with-interfaces
meris.ahmatovic 3 years ago
parent
commit
1375d6e822

+ 2
- 2
Backend/Diligent.WebAPI.Business/Interfaces/ICustomerService.cs View File

@@ -13,10 +13,10 @@ namespace Diligent.WebAPI.Business.Interfaces

Task<Customer> GetCustomerById(string id);

Task<bool> AddNotification(string receiverId, string roomId);
Task AddNotification(Customer c);

Task<List<Notification>> ReadNotifications(string userId);

Task<bool> DeleteNotification(string userId, string roomId);
Task DeleteNotification(Customer c, Notification n);
}
}

+ 2
- 44
Backend/Diligent.WebAPI.Business/Services/CustomerService.cs View File

@@ -30,35 +30,9 @@ namespace Diligent.WebAPI.Business.Services
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);

return true;
}

public async Task<List<Notification>> ReadNotifications(string userId)
@@ -68,26 +42,10 @@ namespace Diligent.WebAPI.Business.Services
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);
await _customerManager.UpdateAsync(user);

return true;
}
}
}

+ 21
- 5
Backend/Diligent.WebAPI.Host/Mediator/Notifications/Handlers/AddNotificationHandler.cs View File

@@ -1,4 +1,6 @@
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
using MediatR;

@@ -15,18 +17,32 @@ namespace Diligent.WebAPI.Host.Mediator.Notifications.Handlers

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();
}
}

+ 11
- 6
Backend/Diligent.WebAPI.Host/Mediator/Notifications/Handlers/DeleteNotificationHandler.cs View File

@@ -1,4 +1,5 @@
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
using MediatR;

@@ -15,18 +16,22 @@ namespace Diligent.WebAPI.Host.Mediator.Notifications.Handlers

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();
}
}

+ 0
- 5
Backend/Diligent.WebAPI.Host/Mediator/Notifications/Handlers/GetNotificationsHandler.cs View File

@@ -19,11 +19,6 @@ namespace Diligent.WebAPI.Host.Mediator.Notifications.Handlers

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));
}
}

+ 0
- 64
Backend/Tests/CustomerServiceTest.cs View File

@@ -1,64 +0,0 @@
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();
}
}
}

Backend/Tests/ExceptionHandlingMiddlewareTest.cs → Backend/Tests/ExceptionHandlingMiddlewareTests.cs View File

@@ -8,7 +8,7 @@ using System.Net;
namespace Tests
{
[TestFixture]
public class ExceptionHandlingMiddlewareTest
public class ExceptionHandlingMiddlewareTests
{
private Mock<ILogger<ExceptionHandlingMiddleware>> _logger;


+ 116
- 0
Backend/Tests/NotificationTests.cs View File

@@ -0,0 +1,116 @@
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]));
}
}
}

BIN
Backend/Tests/bin/Debug/net6.0/Diligent.WebAPI.Business.dll View File


BIN
Backend/Tests/bin/Debug/net6.0/Diligent.WebAPI.Business.pdb View File


BIN
Backend/Tests/bin/Debug/net6.0/Diligent.WebAPI.Host.dll View File


BIN
Backend/Tests/bin/Debug/net6.0/Diligent.WebAPI.Host.pdb View File


BIN
Backend/Tests/bin/Debug/net6.0/Tests.dll View File


BIN
Backend/Tests/bin/Debug/net6.0/Tests.pdb View File


+ 1
- 1
Backend/Tests/bin/Debug/net6.0/nunit_random_seed.tmp View File

@@ -1 +1 @@
1616294871
671024648

BIN
Backend/Tests/obj/Debug/net6.0/Tests.csproj.AssemblyReference.cache View File


+ 1
- 1
Backend/Tests/obj/Debug/net6.0/Tests.csproj.CoreCompileInputs.cache View File

@@ -1 +1 @@
4260cf941650494d42f82ac070d88a866ed26069
16c93e8d4a4c74f74ee47efac450a1cb0b2fabe7

BIN
Backend/Tests/obj/Debug/net6.0/Tests.dll View File


BIN
Backend/Tests/obj/Debug/net6.0/Tests.pdb View File


BIN
Backend/Tests/obj/Debug/net6.0/ref/Tests.dll View File


BIN
Backend/Tests/obj/Debug/net6.0/refint/Tests.dll View File


Loading…
Cancel
Save