|
| 1 | +namespace DataServiceTests; |
| 2 | + |
| 3 | +using ExcludedSMULookupDataService; |
| 4 | +using Common; |
| 5 | +using DataServices.Core; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Model; |
| 8 | +using Moq; |
| 9 | +using NHS.CohortManager.Tests.TestUtils; |
| 10 | +using System.Net; |
| 11 | +using Microsoft.Azure.Functions.Worker; |
| 12 | +using FluentAssertions; |
| 13 | + |
| 14 | +[TestClass] |
| 15 | +public class ExcludedSMULookupDataServiceTests |
| 16 | +{ |
| 17 | + private readonly Mock<ILogger<ExcludedSMULookupDataService>> _mockFunctionLogger = new(); |
| 18 | + private readonly Mock<ILogger<RequestHandler<ExcludedSMULookup>>> _mockRequestHandlerLogger = new(); |
| 19 | + private readonly CreateResponse _createResponse = new(); |
| 20 | + private readonly Mock<FunctionContext> _context = new(); |
| 21 | + private readonly MockDataServiceAccessor<ExcludedSMULookup> _dataServiceAccessor; |
| 22 | + private readonly List<ExcludedSMULookup> _mockData; |
| 23 | + private AuthenticationConfiguration _authenticationConfiguration; |
| 24 | + |
| 25 | + public ExcludedSMULookupDataServiceTests() |
| 26 | + { |
| 27 | + _mockData = new List<ExcludedSMULookup>{ |
| 28 | + new ExcludedSMULookup{ |
| 29 | + GpPracticeCode = "ABC" |
| 30 | + }, |
| 31 | + new ExcludedSMULookup{ |
| 32 | + GpPracticeCode = "DEF" |
| 33 | + } |
| 34 | + }; |
| 35 | + _dataServiceAccessor = new MockDataServiceAccessor<ExcludedSMULookup>(_mockData); |
| 36 | + _authenticationConfiguration = DataServiceTestHelper.AllowAllAccessConfig; |
| 37 | + } |
| 38 | + |
| 39 | + #region Get By Id |
| 40 | + [DataRow("ABC")] |
| 41 | + [DataRow("DEF")] |
| 42 | + [TestMethod] |
| 43 | + public async Task RunAsync_GetItemById_ReturnsCorrectItems(string gpPracticeCode) |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + var _requestHandler = new RequestHandler<ExcludedSMULookup>(_dataServiceAccessor, _mockRequestHandlerLogger.Object, _authenticationConfiguration); |
| 47 | + ExcludedSMULookupDataService function = new ExcludedSMULookupDataService(_mockFunctionLogger.Object, _requestHandler, _createResponse); |
| 48 | + var req = new MockHttpRequestData(_context.Object, "", "GET"); |
| 49 | + |
| 50 | + // Act |
| 51 | + var result = await function.Run(req, gpPracticeCode); |
| 52 | + |
| 53 | + // Assert |
| 54 | + var expectedPractice = _mockData.Single(i => i.GpPracticeCode == gpPracticeCode); |
| 55 | + var resultObject = await MockHelpers.GetResponseBodyAsObject<ExcludedSMULookup>(result); |
| 56 | + |
| 57 | + Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); |
| 58 | + resultObject.Should().BeEquivalentTo(expectedPractice); |
| 59 | + } |
| 60 | + |
| 61 | + [DataRow("ABC")] |
| 62 | + [TestMethod] |
| 63 | + public async Task RunAsync_GetItemByIdNotAllowed_Returns401(string gpPracticeCode) |
| 64 | + { |
| 65 | + // Arrange |
| 66 | + _authenticationConfiguration = DataServiceTestHelper.DenyAllAccessConfig; |
| 67 | + var _requestHandler = new RequestHandler<ExcludedSMULookup>(_dataServiceAccessor, _mockRequestHandlerLogger.Object, _authenticationConfiguration); |
| 68 | + ExcludedSMULookupDataService function = new ExcludedSMULookupDataService(_mockFunctionLogger.Object, _requestHandler, _createResponse); |
| 69 | + var req = new MockHttpRequestData(_context.Object, "", "GET"); |
| 70 | + |
| 71 | + // Act |
| 72 | + var result = await function.Run(req, gpPracticeCode); |
| 73 | + |
| 74 | + // Assert |
| 75 | + Assert.AreEqual(HttpStatusCode.Unauthorized, result.StatusCode); |
| 76 | + } |
| 77 | + |
| 78 | + [DataRow("GHI")] |
| 79 | + [TestMethod] |
| 80 | + public async Task RunAsync_GetItemByIdNonExistent_ReturnsNotFound(string gpPracticeCode) |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + var _requestHandler = new RequestHandler<ExcludedSMULookup>(_dataServiceAccessor, _mockRequestHandlerLogger.Object, _authenticationConfiguration); |
| 84 | + ExcludedSMULookupDataService function = new ExcludedSMULookupDataService(_mockFunctionLogger.Object, _requestHandler, _createResponse); |
| 85 | + var req = new MockHttpRequestData(_context.Object, "", "GET"); |
| 86 | + |
| 87 | + // Act |
| 88 | + var result = await function.Run(req, gpPracticeCode); |
| 89 | + |
| 90 | + // Assert |
| 91 | + Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode); |
| 92 | + } |
| 93 | + #endregion |
| 94 | + |
| 95 | + #region Exception |
| 96 | + [TestMethod] |
| 97 | + public async Task RunAsync_GetItemByIdWithInvalidMethod_ReturnsInternalServerError() |
| 98 | + { |
| 99 | + // Arrange |
| 100 | + var invalidMethod = string.Empty; |
| 101 | + var _requestHandler = new RequestHandler<ExcludedSMULookup>(_dataServiceAccessor, _mockRequestHandlerLogger.Object, _authenticationConfiguration); |
| 102 | + ExcludedSMULookupDataService function = new ExcludedSMULookupDataService(_mockFunctionLogger.Object, _requestHandler, _createResponse); |
| 103 | + var req = new MockHttpRequestData(_context.Object, "", invalidMethod); |
| 104 | + |
| 105 | + // Act |
| 106 | + var result = await function.Run(req, null); |
| 107 | + |
| 108 | + // Assert |
| 109 | + Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode); |
| 110 | + } |
| 111 | + #endregion |
| 112 | +} |
0 commit comments