Sfoglia il codice sorgente

tested deleteNotification handler

feature/mongo-services-with-interfaces
meris.ahmatovic 3 anni fa
parent
commit
1375d6e822

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



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

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

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

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

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

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

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

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



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

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

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 Vedi File

namespace Tests namespace Tests
{ {
[TestFixture] [TestFixture]
public class ExceptionHandlingMiddlewareTest
public class ExceptionHandlingMiddlewareTests
{ {
private Mock<ILogger<ExceptionHandlingMiddleware>> _logger; private Mock<ILogger<ExceptionHandlingMiddleware>> _logger;



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

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 Vedi File


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


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


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


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


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


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

1616294871
671024648

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


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

4260cf941650494d42f82ac070d88a866ed26069
16c93e8d4a4c74f74ee47efac450a1cb0b2fabe7

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


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


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


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


Loading…
Annulla
Salva