-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationControllerTests.cs
More file actions
117 lines (102 loc) · 4.26 KB
/
Copy pathNotificationControllerTests.cs
File metadata and controls
117 lines (102 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using Notification.API.Controllers;
using Notification.API.Services;
using Notification.Domain.Entities;
using Notification.Domain.Interfaces;
using Shared.Contracts.Events;
namespace Notification.Tests;
public class NotificationControllerTests
{
[Fact]
public async Task GetById_ReturnsNotFoundWhenNotificationIsMissing()
{
var repository = new Mock<INotificationRepository>();
repository
.Setup(x => x.GetByIdAsync(It.IsAny<Guid>()))
.ReturnsAsync((OrderNotification?)null);
var controller = CreateController(repository.Object);
var result = await controller.GetById(Guid.NewGuid());
result.Should().BeOfType<NotFoundResult>();
}
[Fact]
public async Task GetById_ReturnsOkWhenNotificationExists()
{
var notification = CreateNotification();
var repository = new Mock<INotificationRepository>();
repository
.Setup(x => x.GetByIdAsync(notification.Id))
.ReturnsAsync(notification);
var controller = CreateController(repository.Object);
var result = await controller.GetById(notification.Id);
result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetPreview_ReturnsNotFoundWhenRenderedBodyIsEmpty()
{
var notification = CreateNotification();
notification.RenderedBody = string.Empty;
var repository = new Mock<INotificationRepository>();
repository
.Setup(x => x.GetByIdAsync(notification.Id))
.ReturnsAsync(notification);
var controller = CreateController(repository.Object);
var result = await controller.GetPreview(notification.Id);
result.Should().BeOfType<NotFoundObjectResult>();
}
[Fact]
public async Task ReceiveOrderPlacedEvent_MapsDtoAndReturnsCreatedAtAction()
{
var repository = new Mock<INotificationRepository>();
OrderNotification? addedNotification = null;
repository
.Setup(x => x.AddAsync(It.IsAny<OrderNotification>()))
.Callback<OrderNotification>(notification => addedNotification = notification)
.ReturnsAsync((OrderNotification notification) => notification);
var controller = CreateController(repository.Object);
var orderId = Guid.NewGuid();
var customerId = Guid.NewGuid();
var placedAt = new DateTime(2025, 4, 5, 6, 7, 8, DateTimeKind.Utc);
var dto = new OrderPlacedEventDto(orderId, customerId, 12345m, placedAt);
var result = await controller.ReceiveOrderPlacedEvent(dto);
var created = result.Should().BeOfType<CreatedAtActionResult>().Subject;
created.ActionName.Should().Be(nameof(NotificationController.GetPreview));
created.RouteValues.Should().ContainKey("id");
addedNotification.Should().NotBeNull();
addedNotification!.OrderId.Should().Be(orderId);
addedNotification.CustomerId.Should().Be(customerId);
addedNotification.OrderTotal.Should().Be(12345m);
addedNotification.Status.Should().Be(NotificationStatus.Rendered);
repository.Verify(x => x.AddAsync(It.IsAny<OrderNotification>()), Times.Once);
}
private static NotificationController CreateController(INotificationRepository repository)
{
var consumer = new OrderEventConsumer(
repository,
new NotificationRenderer(),
Mock.Of<ILogger<OrderEventConsumer>>());
return new NotificationController(
repository,
consumer,
Mock.Of<ILogger<NotificationController>>());
}
private static OrderNotification CreateNotification()
{
return new OrderNotification
{
Id = Guid.NewGuid(),
OrderId = Guid.NewGuid(),
CustomerId = Guid.NewGuid(),
OrderTotal = 12345m,
CustomerEmail = "customer@example.com",
CustomerName = "Valued Customer",
Type = NotificationType.OrderConfirmation,
Status = NotificationStatus.Rendered,
RenderedSubject = "Order Confirmed",
RenderedBody = "<p>Rendered</p>",
CreatedAt = DateTime.UtcNow
};
}
}