Skip to content

Commit

Permalink
feat: new external endpoint for demographic data management (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
will-larkin-nhs authored Jan 14, 2025
2 parents 2d14460 + 721ff1b commit ac5f775
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ public DemographicDataFunction(ILogger<DemographicDataFunction> logger, ICreateR

[Function("DemographicDataFunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
return await Main(req, false);
}


/// <summary>
/// Gets filtered demographic data from the demographic data service,
/// this endpoint is used by the external BI product
/// </summary>
/// <param name="Id">The NHS number to get the demographic data for.</param>
/// <returns>JSON response containing the Primary Care Provider & Preferred Language</returns>
[Function("DemographicDataFunctionExternal")]
public async Task<HttpResponseData> RunExternal([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
{
return await Main(req, true);
}

private async Task<HttpResponseData> Main(HttpRequestData req, bool externalRequest)
{
var participantData = new Participant();
try
Expand Down Expand Up @@ -56,6 +74,14 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
_logger.LogInformation("demographic function failed");
return _createResponse.CreateHttpResponse(HttpStatusCode.NotFound, req);
}

// Filters out unnsecessry data for use in the BI prdoduct
if (externalRequest)
{
var filterdData = JsonSerializer.Deserialize<FilteredDemographicData>(data);
data = JsonSerializer.Serialize(filterdData);
}

return _createResponse.CreateHttpResponse(HttpStatusCode.OK, req, data);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <summary>Demographic fields required in requests from the BI product.</summary>

namespace NHS.CohortManager.DemographicServices;

public class FilteredDemographicData
{
public string? PrimaryCareProvider { get; set; }
public string? PreferredLanguage { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DemographicDataFunctionTests
{
private readonly Mock<ILogger<DemographicDataFunction>> _logger = new();
private readonly Mock<ICreateResponse> _createResponse = new();
private readonly Mock<ICallFunction> _callFunction = new();
private readonly Mock<ICallFunction> _callFunctionMock = new();
private readonly Mock<FunctionContext> _context = new();
private Mock<HttpRequestData> _request;
private readonly Mock<HttpWebResponse> _webResponse = new();
Expand Down Expand Up @@ -59,20 +59,20 @@ public DemographicDataFunctionTests()


_webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
_callFunction.Setup(call => call.SendPost(It.IsAny<string>(), It.IsAny<string>()))
_callFunctionMock.Setup(call => call.SendPost(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.FromResult<HttpWebResponse>(_webResponse.Object));

_webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
_callFunction.Setup(call => call.SendGet(It.IsAny<string>()))
_callFunctionMock.Setup(call => call.SendGet(It.IsAny<string>()))
.Returns(Task.FromResult<string>(""));
}

[TestMethod]
public async Task Run_return_DemographicDataSavedPostRequest_OK()
public async Task RunPost_ValidRequest_ReturnOk()
{
// Arrange
var json = JsonSerializer.Serialize(_participant);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object);

_request = _setupRequest.Setup(json);

Expand All @@ -85,15 +85,15 @@ public async Task Run_return_DemographicDataSavedPostRequest_OK()
}

[TestMethod]
public async Task Run_return_DemographicDataSavedPostRequest_InternalServerEver()
public async Task RunPost_DataServiceReturns500_ReturnInternalServerError()
{
// Arrange
var json = JsonSerializer.Serialize(_participant);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object);

_request = _setupRequest.Setup(json);
_webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.InternalServerError);
_callFunction.Setup(call => call.SendPost(It.IsAny<string>(), It.IsAny<string>()))
_callFunctionMock.Setup(call => call.SendPost(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.FromResult<HttpWebResponse>(_webResponse.Object));

// Act
Expand All @@ -105,18 +105,18 @@ public async Task Run_return_DemographicDataSavedPostRequest_InternalServerEver(
}

[TestMethod]
public async Task Run_return_DemographicDataGetRequest_OK()
public async Task RunGet_ValidRequest_ReturnOk()
{
// Arrange
var json = JsonSerializer.Serialize(_participant);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object);

_request = _setupRequest.Setup(json);

// Act
_request.Setup(x => x.Query).Returns(new System.Collections.Specialized.NameValueCollection() { { "Id", "1" } });

_callFunction.Setup(call => call.SendGet(It.IsAny<string>()))
_callFunctionMock.Setup(call => call.SendGet(It.IsAny<string>()))
.Returns(Task.FromResult<string>("data"));


Expand All @@ -132,7 +132,7 @@ public async Task Run_return_DemographicDataNotSaved_InternalServerError()
{
// Arrange
var json = JsonSerializer.Serialize(_participant);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object);

_request = _setupRequest.Setup(json);

Expand All @@ -146,7 +146,7 @@ public async Task Run_return_DemographicDataNotSaved_InternalServerError()


_webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.InternalServerError);
_callFunction.Setup(call => call.SendPost(It.Is<string>(s => s.Contains("DemographicDataFunctionURI")), It.IsAny<string>()))
_callFunctionMock.Setup(call => call.SendPost(It.Is<string>(s => s.Contains("DemographicDataFunctionURI")), It.IsAny<string>()))
.Returns(Task.FromResult<HttpWebResponse>(_webResponse.Object));

// Act
Expand All @@ -157,11 +157,11 @@ public async Task Run_return_DemographicDataNotSaved_InternalServerError()
}

[TestMethod]
public async Task Run_Return_DemographicFunctionThrows_InternalServerError()
public async Task RunPost_CallFunctionThrowsError_ReturnInternalServerError()
{
// Arrange
var json = JsonSerializer.Serialize(_participant);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunction.Object);
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object);

_request = _setupRequest.Setup(json);

Expand All @@ -175,7 +175,7 @@ public async Task Run_Return_DemographicFunctionThrows_InternalServerError()


_webResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.InternalServerError);
_callFunction.Setup(call => call.SendPost(It.Is<string>(s => s.Contains("DemographicDataFunctionURI")), It.IsAny<string>()))
_callFunctionMock.Setup(call => call.SendPost(It.Is<string>(s => s.Contains("DemographicDataFunctionURI")), It.IsAny<string>()))
.ThrowsAsync(new Exception("there was an error"));

// Act
Expand All @@ -193,4 +193,33 @@ public async Task Run_Return_DemographicFunctionThrows_InternalServerError()
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
), Times.AtLeastOnce(), "There has been an error saving demographic data:");
}

[TestMethod]
public async Task RunExternal_ValidRequest_ReturnOk()
{
// Arrange
var json = JsonSerializer.Serialize(_participant);

_request = _setupRequest.Setup(json);

Demographic DataServiceResponse = new()
{
PrimaryCareProvider = "Blerg",
PreferredLanguage = "Francais"
};

_request.Setup(x => x.Query).Returns(new System.Collections.Specialized.NameValueCollection() { { "Id", "1" } });

_callFunctionMock.Setup(call => call.SendGet(It.IsAny<string>()))
.ReturnsAsync(JsonSerializer.Serialize(DataServiceResponse));

_request.Setup(r => r.Method).Returns("GET");
var sut = new DemographicDataFunction(_logger.Object, _createResponse.Object, _callFunctionMock.Object);

// Act
var result = await sut.RunExternal(_request.Object);

// Assert
Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
}
}

0 comments on commit ac5f775

Please sign in to comment.