|
| 1 | +namespace NHS.CohortManager.Tests.UnitTests.AddCohortDistributionDataTests; |
| 2 | + |
| 3 | +using System.Net; |
| 4 | +using System.Text.Json; |
| 5 | +using Common; |
| 6 | +using DataServices.Client; |
| 7 | +using Model; |
| 8 | +using Moq; |
| 9 | +using NHS.CohortManager.CohortDistributionDataServices; |
| 10 | +using NHS.CohortManager.Tests.TestUtils; |
| 11 | + |
| 12 | +[TestClass] |
| 13 | +public class AddCohortDistributionDataTests : DatabaseTestBaseSetup<AddCohortDistributionDataFunction> |
| 14 | +{ |
| 15 | + private static readonly Mock<IExceptionHandler> _handleException = new(); |
| 16 | + private static readonly Mock<IDataServiceClient<CohortDistribution>> _cohortDistributionDataService = new(); |
| 17 | + private CohortDistributionParticipant _participantCsvRecord = new(); |
| 18 | + |
| 19 | + public AddCohortDistributionDataTests() : base((conn, logger, transaction, command, response) => |
| 20 | + new AddCohortDistributionDataFunction( |
| 21 | + logger, |
| 22 | + response, |
| 23 | + _handleException.Object, |
| 24 | + _cohortDistributionDataService.Object)) |
| 25 | + { |
| 26 | + CreateHttpResponseMock(); |
| 27 | + } |
| 28 | + |
| 29 | + [TestInitialize] |
| 30 | + public void TestInitialize() |
| 31 | + { |
| 32 | + _cohortDistributionDataService.Reset(); |
| 33 | + _service = new AddCohortDistributionDataFunction( |
| 34 | + _loggerMock.Object, |
| 35 | + _createResponseMock.Object, |
| 36 | + _handleException.Object, |
| 37 | + _cohortDistributionDataService.Object); |
| 38 | + _participantCsvRecord = new CohortDistributionParticipant() { NhsNumber = "1234567890" }; |
| 39 | + } |
| 40 | + |
| 41 | + [DataRow("")] |
| 42 | + [DataRow("Invalid request body")] |
| 43 | + [TestMethod] |
| 44 | + public async Task Run_InvalidRequest_ReturnsInternalServerError(string badRequest) |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + _request = SetupRequest(badRequest); |
| 48 | + |
| 49 | + // Act |
| 50 | + var result = await _service.RunAsync(_request.Object); |
| 51 | + |
| 52 | + // Assert |
| 53 | + Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode); |
| 54 | + } |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + public async Task Run_InsertCohortDistributionDataSucceeds_ReturnsOk() |
| 58 | + { |
| 59 | + // Arrange |
| 60 | + var participantRecord = JsonSerializer.Serialize(_participantCsvRecord); |
| 61 | + _request = SetupRequest(participantRecord); |
| 62 | + |
| 63 | + _cohortDistributionDataService.Setup(x => x.Add(It.IsAny<CohortDistribution>())).Returns(Task.FromResult(true)); |
| 64 | + |
| 65 | + // Act |
| 66 | + var result = await _service.RunAsync(_request.Object); |
| 67 | + |
| 68 | + // Assert |
| 69 | + Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); |
| 70 | + } |
| 71 | + |
| 72 | + [TestMethod] |
| 73 | + public async Task Run_InsertCohortDistributionDataFails_ReturnsInternalServerError() |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + var participantRecord = JsonSerializer.Serialize(_participantCsvRecord); |
| 77 | + _request = SetupRequest(participantRecord); |
| 78 | + |
| 79 | + _cohortDistributionDataService.Setup(x => x.Add(It.IsAny<CohortDistribution>())).Returns(Task.FromResult(false)); |
| 80 | + |
| 81 | + // Act |
| 82 | + var result = await _service.RunAsync(_request.Object); |
| 83 | + |
| 84 | + // Assert |
| 85 | + Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode); |
| 86 | + } |
| 87 | +} |
0 commit comments