|
| 1 | +namespace NHS.CohortManager.Tests.UnitTests.DemographicServicesTests; |
| 2 | + |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Common; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using Moq; |
| 8 | +using NHS.MESH.Client.Contracts.Services; |
| 9 | +using NHS.MESH.Client.Models; |
| 10 | + |
| 11 | +[TestClass] |
| 12 | +public class MeshSendCaasSubscribeTests |
| 13 | +{ |
| 14 | + private readonly Mock<ILogger<MeshSendCaasSubscribe>> _logger = new(); |
| 15 | + private readonly Mock<IMeshOutboxService> _meshOutbox = new(); |
| 16 | + |
| 17 | + private MeshSendCaasSubscribe CreateSut(string workflowId = "WF-CAAS-SUB") |
| 18 | + { |
| 19 | + var cfg = Options.Create(new MeshSendCaasSubscribeConfig { SendCaasWorkflowId = workflowId }); |
| 20 | + return new MeshSendCaasSubscribe(_logger.Object, _meshOutbox.Object, cfg); |
| 21 | + } |
| 22 | + |
| 23 | + [TestMethod] |
| 24 | + public async Task SendSubscriptionRequest_Success_SendsExpectedAttachment_AndReturnsMessageId() |
| 25 | + { |
| 26 | + // Arrange |
| 27 | + string? capturedFrom = null, capturedTo = null, capturedWorkflow = null; |
| 28 | + FileAttachment? capturedFile = null; |
| 29 | + _meshOutbox |
| 30 | + .Setup(m => m.SendCompressedMessageAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<FileAttachment>(), null, null, false)) |
| 31 | + .Callback((string from, string to, string workflow, FileAttachment file, string? _, string? __, bool ___) => |
| 32 | + { |
| 33 | + capturedFrom = from; capturedTo = to; capturedWorkflow = workflow; capturedFile = file; |
| 34 | + }) |
| 35 | + .ReturnsAsync(new MeshResponse<SendMessageResponse> |
| 36 | + { |
| 37 | + IsSuccessful = true, |
| 38 | + Response = new SendMessageResponse { MessageId = "MSG123" } |
| 39 | + }); |
| 40 | + |
| 41 | + var sut = CreateSut(); |
| 42 | + |
| 43 | + // Act |
| 44 | + var result = await sut.SendSubscriptionRequest(9000000009L, "TO_BOX", "FROM_BOX"); |
| 45 | + |
| 46 | + // Assert |
| 47 | + Assert.AreEqual("MSG123", result); |
| 48 | + Assert.AreEqual("FROM_BOX", capturedFrom); |
| 49 | + Assert.AreEqual("TO_BOX", capturedTo); |
| 50 | + Assert.AreEqual("WF-CAAS-SUB", capturedWorkflow); |
| 51 | + Assert.IsNotNull(capturedFile); |
| 52 | + Assert.AreEqual("CaaSSubscribe.parquet", capturedFile!.FileName); |
| 53 | + Assert.AreEqual("application/octet-stream", capturedFile.ContentType); |
| 54 | + Assert.IsNotNull(capturedFile.Content); |
| 55 | + Assert.IsTrue(capturedFile.Content.Length > 0); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public async Task SendSubscriptionRequest_Failure_ReturnsNull() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + _meshOutbox |
| 63 | + .Setup(m => m.SendCompressedMessageAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<FileAttachment>(), null, null, false)) |
| 64 | + .ReturnsAsync(new MeshResponse<SendMessageResponse> |
| 65 | + { |
| 66 | + IsSuccessful = false, |
| 67 | + Error = new APIErrorResponse { ErrorCode = "500", ErrorDescription = "boom" } |
| 68 | + }); |
| 69 | + |
| 70 | + var sut = CreateSut(); |
| 71 | + |
| 72 | + // Act |
| 73 | + var result = await sut.SendSubscriptionRequest(9000000009L, "TO_BOX", "FROM_BOX"); |
| 74 | + |
| 75 | + // Assert |
| 76 | + Assert.IsNull(result); |
| 77 | + } |
| 78 | +} |
| 79 | + |
0 commit comments