|
1 | 1 | using System.Dynamic; |
| 2 | +using System.Globalization; |
2 | 3 | using Azure; |
3 | 4 | using Azure.Messaging; |
4 | 5 | using Azure.Messaging.EventGrid; |
|
7 | 8 | using Microsoft.Extensions.Logging; |
8 | 9 | using Moq; |
9 | 10 | using ServiceLayer.API.Functions; |
| 11 | +using ServiceLayer.API.Shared; |
10 | 12 | using ServiceLayer.API.Tests.Utils; |
11 | 13 |
|
12 | 14 | namespace ServiceLayer.API.Tests.Functions; |
@@ -45,14 +47,34 @@ public async Task CreateEpisodeEvent_ShouldSendEventAndReturnOk_WhenRequestIsVal |
45 | 47 | // Arrange |
46 | 48 | var request = _setupRequest.CreateMockHttpRequest(_episode); |
47 | 49 | var mockResponse = Mock.Of<Response>(r => r.IsError == false); |
48 | | - _mockEventGridPublisherClient.Setup(x => x.SendEventAsync(It.IsAny<CloudEvent>(), It.IsAny<CancellationToken>())).ReturnsAsync(mockResponse); |
| 50 | + CloudEvent? capturedEvent = null; |
| 51 | + _mockEventGridPublisherClient.Setup(x => x.SendEventAsync(It.IsAny<CloudEvent>(), It.IsAny<CancellationToken>())) |
| 52 | + .Callback<CloudEvent, CancellationToken>((ce, _) => |
| 53 | + { |
| 54 | + capturedEvent = ce; |
| 55 | + }) |
| 56 | + .ReturnsAsync(mockResponse); |
49 | 57 |
|
50 | 58 | // Act |
51 | 59 | var response = await _functions.IngressEpisode(request); |
52 | 60 |
|
53 | 61 | // Assert |
54 | 62 | Assert.IsType<OkResult>(response); |
55 | | - _mockEventGridPublisherClient.Verify(x => x.SendEventAsync(It.IsAny<CloudEvent>(), default), Times.Once()); |
| 63 | + _mockEventGridPublisherClient.Verify(x => |
| 64 | + x.SendEventAsync(It.Is<CloudEvent>(ce => |
| 65 | + ce.Type == "EpisodeEvent" && |
| 66 | + ce.Source == "ServiceLayer" && |
| 67 | + ce.Data != null |
| 68 | + ), default), Times.Once()); |
| 69 | + |
| 70 | + var data = capturedEvent!.Data!.ToObjectFromJson<CreatePathwayParticipantDto>(); |
| 71 | + Assert.NotNull(data); |
| 72 | + Assert.Equal(new Guid("11111111-1111-1111-1111-111111111113"), data.PathwayTypeId); |
| 73 | + Assert.Equal("Breast Screening Routine", data.PathwayTypeName); |
| 74 | + Assert.Equal("Breast Screening", data.ScreeningName); |
| 75 | + Assert.Equal(_episode.nhs_number, data.NhsNumber); |
| 76 | + Assert.Equal(DateOnly.Parse(_episode.date_of_birth, CultureInfo.CurrentCulture), data.DOB); |
| 77 | + Assert.Equal($"{_episode.first_given_name} {_episode.family_name}", data.Name); |
56 | 78 | } |
57 | 79 |
|
58 | 80 | [Fact] |
|
0 commit comments