Skip to content

Commit b677c11

Browse files
chor: adding unit tests for pds functions features
1 parent a75af90 commit b677c11

8 files changed

Lines changed: 449 additions & 98 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace NHS.CohortManager.DemographicServices;
2+
3+
using Model;
4+
5+
public interface IPdsProcessor
6+
{
7+
Task ProcessPdsNotFoundResponse(HttpResponseMessage pdsResponse, string nhsNumber);
8+
Task ProcessRecord(Participant participant);
9+
Task<bool> UpsertDemographicRecordFromPDS(ParticipantDemographic participantDemographic);
10+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
namespace NHS.CohortManager.DemographicServices;
2+
3+
using System.Collections.Concurrent;
4+
using System.Net.Http.Json;
5+
using Common;
6+
using DataServices.Client;
7+
using Microsoft.Extensions.Logging;
8+
using Microsoft.Extensions.Options;
9+
using Model;
10+
11+
public class PdsProcessor : IPdsProcessor
12+
{
13+
private readonly ILogger<PdsProcessor> _logger;
14+
15+
private readonly IDataServiceClient<ParticipantDemographic> _participantDemographicClient;
16+
private readonly ICreateBasicParticipantData _createBasicParticipantData;
17+
private readonly RetrievePDSDemographicConfig _config;
18+
private readonly IAddBatchToQueue _addBatchToQueue;
19+
20+
21+
public PdsProcessor(
22+
ILogger<PdsProcessor> logger,
23+
ICreateBasicParticipantData createBasicParticipantData,
24+
IDataServiceClient<ParticipantDemographic> participantDemographicClient,
25+
IAddBatchToQueue addBatchToQueue,
26+
IOptions<RetrievePDSDemographicConfig> retrievePDSDemographicConfig)
27+
{
28+
_logger = logger;
29+
_participantDemographicClient = participantDemographicClient;
30+
_createBasicParticipantData = createBasicParticipantData;
31+
_addBatchToQueue = addBatchToQueue;
32+
_config = retrievePDSDemographicConfig.Value;
33+
}
34+
35+
public async Task ProcessPdsNotFoundResponse(HttpResponseMessage pdsResponse, string nhsNumber)
36+
{
37+
var errorResponse = await pdsResponse!.Content.ReadFromJsonAsync<PdsErrorResponse>();
38+
// we now create a record as an update record and send to the manage participant function. Reason for removal for date should be today and the reason for remove of ORR
39+
if (errorResponse!.issue!.FirstOrDefault()!.details!.coding!.FirstOrDefault()!.code == PdsConstants.InvalidatedResourceCode)
40+
{
41+
var pdsDemographic = new PdsDemographic()
42+
{
43+
NhsNumber = nhsNumber,
44+
PrimaryCareProvider = null,
45+
ReasonForRemoval = PdsConstants.OrrRemovalReason,
46+
RemovalEffectiveFromDate = DateTime.UtcNow.Date.ToString("yyyyMMdd")
47+
};
48+
var participant = new Participant(pdsDemographic);
49+
participant.RecordType = Actions.Removed;
50+
//sends record for an update
51+
await ProcessRecord(participant);
52+
return;
53+
}
54+
_logger.LogError("the PDS function has returned a 404 error. function now stopping processing");
55+
}
56+
57+
58+
public async Task ProcessRecord(Participant participant)
59+
{
60+
var updateRecord = new ConcurrentQueue<BasicParticipantCsvRecord>();
61+
participant.RecordType = participant.RecordType = Actions.Removed;
62+
63+
var basicParticipantCsvRecord = new BasicParticipantCsvRecord
64+
{
65+
BasicParticipantData = _createBasicParticipantData.BasicParticipantData(participant),
66+
FileName = PdsConstants.DefaultFileName,
67+
Participant = participant
68+
};
69+
70+
updateRecord.Enqueue(basicParticipantCsvRecord);
71+
72+
_logger.LogInformation("Sending record to the update queue.");
73+
await _addBatchToQueue.ProcessBatch(updateRecord, _config.ParticipantManagementTopic);
74+
}
75+
76+
77+
public async Task<bool> UpsertDemographicRecordFromPDS(ParticipantDemographic participantDemographic)
78+
{
79+
ParticipantDemographic oldParticipantDemographic = await _participantDemographicClient.GetSingleByFilter(i => i.NhsNumber == participantDemographic.NhsNumber);
80+
81+
if (oldParticipantDemographic == null)
82+
{
83+
_logger.LogInformation("Participant Demographic record not found, attemping to add Participant Demographic.");
84+
bool addSuccess = await _participantDemographicClient.Add(participantDemographic);
85+
86+
if (addSuccess)
87+
{
88+
_logger.LogInformation("Successfully added Participant Demographic.");
89+
return true;
90+
}
91+
92+
_logger.LogError("Failed to add Participant Demographic.");
93+
return false;
94+
}
95+
96+
_logger.LogInformation("Participant Demographic record found, attempting to update Participant Demographic.");
97+
participantDemographic.ParticipantId = oldParticipantDemographic.ParticipantId;
98+
bool updateSuccess = await _participantDemographicClient.Update(participantDemographic);
99+
100+
if (updateSuccess)
101+
{
102+
_logger.LogInformation("Successfully updated Participant Demographic.");
103+
return true;
104+
}
105+
106+
_logger.LogError("Failed to update Participant Demographic.");
107+
return false;
108+
}
109+
110+
}

application/CohortManager/src/Functions/DemographicServices/RetrievePDSDemographic/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
services.AddSingleton<IHttpParserHelper, HttpParserHelper>();
2020
services.AddSingleton<IFhirPatientDemographicMapper, FhirPatientDemographicMapper>();
2121
services.AddSingleton<IAddBatchToQueue, AddBatchToQueue>();
22+
services.AddSingleton<IPdsProcessor, PdsProcessor>();
2223
services.AddSingleton<ICreateBasicParticipantData, CreateBasicParticipantData>();
2324
// Register health checks
2425
services.AddBasicHealthCheck("RetrievePdsDemographic");

application/CohortManager/src/Functions/DemographicServices/RetrievePDSDemographic/RetrievePDSDemographic.cs

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ namespace NHS.CohortManager.DemographicServices;
1212
using Microsoft.Extensions.Options;
1313
using System.Threading.Tasks;
1414
using Model;
15-
using System.Net.Http.Json;
16-
using System.Collections.Concurrent;
1715

1816
public class RetrievePdsDemographic
1917
{
@@ -22,10 +20,8 @@ public class RetrievePdsDemographic
2220
private readonly IHttpClientFunction _httpClientFunction;
2321
private readonly RetrievePDSDemographicConfig _config;
2422
private readonly IFhirPatientDemographicMapper _fhirPatientDemographicMapper;
25-
private readonly IDataServiceClient<ParticipantDemographic> _participantDemographicClient;
2623
private readonly IBearerTokenService _bearerTokenService;
27-
private readonly ICreateBasicParticipantData _createBasicParticipantData;
28-
private readonly IAddBatchToQueue _addBatchToQueue;
24+
private readonly IPdsProcessor _pdsProcessor;
2925
private const string PdsParticipantUrlFormat = "{0}/{1}";
3026

3127

@@ -35,21 +31,17 @@ public RetrievePdsDemographic(
3531
IHttpClientFunction httpClientFunction,
3632
IFhirPatientDemographicMapper fhirPatientDemographicMapper,
3733
IOptions<RetrievePDSDemographicConfig> retrievePDSDemographicConfig,
38-
IDataServiceClient<ParticipantDemographic> participantDemographicClient,
39-
ICreateBasicParticipantData createBasicParticipantData,
40-
IAddBatchToQueue addBatchToQueue,
41-
IBearerTokenService bearerTokenService
34+
IBearerTokenService bearerTokenService,
35+
IPdsProcessor pdsProcessor
4236
)
4337
{
4438
_logger = logger;
4539
_createResponse = createResponse;
4640
_httpClientFunction = httpClientFunction;
4741
_fhirPatientDemographicMapper = fhirPatientDemographicMapper;
4842
_config = retrievePDSDemographicConfig.Value;
49-
_participantDemographicClient = participantDemographicClient;
50-
_createBasicParticipantData = createBasicParticipantData;
5143
_bearerTokenService = bearerTokenService;
52-
_addBatchToQueue = addBatchToQueue;
44+
_pdsProcessor = pdsProcessor;
5345
}
5446

5547
// TODO: Need to send an exception to the EXCEPTION_MANAGEMENT table whenever this function returns a non OK status.
@@ -79,7 +71,7 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
7971

8072
if (response.StatusCode == HttpStatusCode.NotFound)
8173
{
82-
await ProcessPdsNotFoundResponse(response, nhsNumber);
74+
await _pdsProcessor.ProcessPdsNotFoundResponse(response, nhsNumber);
8375
return _createResponse.CreateHttpResponse(HttpStatusCode.NotFound, req, "PDS returned a 404 please database for details");
8476
}
8577

@@ -88,7 +80,7 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
8880
jsonResponse = await _httpClientFunction.GetResponseText(response);
8981
var pdsDemographic = _fhirPatientDemographicMapper.ParseFhirJson(jsonResponse);
9082
var participantDemographic = pdsDemographic.ToParticipantDemographic();
91-
var upsertResult = await UpsertDemographicRecordFromPDS(participantDemographic);
83+
var upsertResult = await _pdsProcessor.UpsertDemographicRecordFromPDS(participantDemographic);
9284

9385
return upsertResult ?
9486
_createResponse.CreateHttpResponse(HttpStatusCode.OK, req, JsonSerializer.Serialize(participantDemographic)) :
@@ -100,79 +92,4 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
10092
return _createResponse.CreateHttpResponse(HttpStatusCode.InternalServerError, req);
10193
}
10294
}
103-
104-
private async Task ProcessPdsNotFoundResponse(HttpResponseMessage pdsResponse, string nhsNumber)
105-
{
106-
var errorResponse = await pdsResponse!.Content.ReadFromJsonAsync<PdsErrorResponse>();
107-
// we now create a record as an update record and send to the manage participant function. Reason for removal for date should be today and the reason for remove of ORR
108-
if (errorResponse!.issue!.FirstOrDefault()!.details!.coding!.FirstOrDefault()!.code == PdsConstants.InvalidatedResourceCode)
109-
{
110-
var pdsDemographic = new PdsDemographic()
111-
{
112-
NhsNumber = nhsNumber,
113-
PrimaryCareProvider = null,
114-
ReasonForRemoval = PdsConstants.OrrRemovalReason,
115-
RemovalEffectiveFromDate = DateTime.UtcNow.Date.ToString("yyyyMMdd")
116-
};
117-
var participant = new Participant(pdsDemographic);
118-
participant.RecordType = Actions.Removed;
119-
//sends record for an update
120-
await ProcessRecord(participant);
121-
return;
122-
}
123-
_logger.LogError("the PDS function has returned a 404 error. function now stopping processing");
124-
}
125-
126-
127-
private async Task ProcessRecord(Participant participant)
128-
{
129-
var updateRecord = new ConcurrentQueue<BasicParticipantCsvRecord>();
130-
participant.RecordType = participant.RecordType = Actions.Removed;
131-
132-
var basicParticipantCsvRecord = new BasicParticipantCsvRecord
133-
{
134-
BasicParticipantData = _createBasicParticipantData.BasicParticipantData(participant),
135-
FileName = PdsConstants.DefaultFileName,
136-
Participant = participant
137-
};
138-
139-
updateRecord.Enqueue(basicParticipantCsvRecord);
140-
141-
_logger.LogInformation("Sending record to the update queue.");
142-
await _addBatchToQueue.ProcessBatch(updateRecord, _config.ParticipantManagementTopic);
143-
}
144-
145-
146-
private async Task<bool> UpsertDemographicRecordFromPDS(ParticipantDemographic participantDemographic)
147-
{
148-
ParticipantDemographic oldParticipantDemographic = await _participantDemographicClient.GetSingleByFilter(i => i.NhsNumber == participantDemographic.NhsNumber);
149-
150-
if (oldParticipantDemographic == null)
151-
{
152-
_logger.LogInformation("Participant Demographic record not found, attemping to add Participant Demographic.");
153-
bool addSuccess = await _participantDemographicClient.Add(participantDemographic);
154-
155-
if (addSuccess)
156-
{
157-
_logger.LogInformation("Successfully added Participant Demographic.");
158-
return true;
159-
}
160-
161-
_logger.LogError("Failed to add Participant Demographic.");
162-
return false;
163-
}
164-
165-
_logger.LogInformation("Participant Demographic record found, attempting to update Participant Demographic.");
166-
participantDemographic.ParticipantId = oldParticipantDemographic.ParticipantId;
167-
bool updateSuccess = await _participantDemographicClient.Update(participantDemographic);
168-
169-
if (updateSuccess)
170-
{
171-
_logger.LogInformation("Successfully updated Participant Demographic.");
172-
return true;
173-
}
174-
175-
_logger.LogError("Failed to update Participant Demographic.");
176-
return false;
177-
}
17895
}

application/CohortManager/src/Functions/Functions.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWTTokenServiceTests", "..\
229229
EndProject
230230
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthClientCredentialsTests", "..\..\..\..\tests\UnitTests\AuthClientCredentialsTests\AuthClientCredentialsTests.csproj", "{59CBDBE5-29BE-F38C-80E6-40843F2F8AF6}"
231231
EndProject
232+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PdsProcesserTests", "PdsProcesserTests", "{5555D2A1-8C8F-5B64-9F84-08EFE0FC7CD8}"
233+
EndProject
234+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdsProcessorTests", "..\..\..\..\tests\PdsProcessorTests\PdsProcessorTests.csproj", "{FC22C311-57DD-B069-4041-AD2AC8F80B5D}"
235+
EndProject
232236
Global
233237
GlobalSection(SolutionConfigurationPlatforms) = preSolution
234238
Debug|Any CPU = Debug|Any CPU
@@ -1367,6 +1371,18 @@ Global
13671371
{59CBDBE5-29BE-F38C-80E6-40843F2F8AF6}.Release|x64.Build.0 = Release|Any CPU
13681372
{59CBDBE5-29BE-F38C-80E6-40843F2F8AF6}.Release|x86.ActiveCfg = Release|Any CPU
13691373
{59CBDBE5-29BE-F38C-80E6-40843F2F8AF6}.Release|x86.Build.0 = Release|Any CPU
1374+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1375+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
1376+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Debug|x64.ActiveCfg = Debug|Any CPU
1377+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Debug|x64.Build.0 = Debug|Any CPU
1378+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Debug|x86.ActiveCfg = Debug|Any CPU
1379+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Debug|x86.Build.0 = Debug|Any CPU
1380+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
1381+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Release|Any CPU.Build.0 = Release|Any CPU
1382+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Release|x64.ActiveCfg = Release|Any CPU
1383+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Release|x64.Build.0 = Release|Any CPU
1384+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Release|x86.ActiveCfg = Release|Any CPU
1385+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D}.Release|x86.Build.0 = Release|Any CPU
13701386
EndGlobalSection
13711387
GlobalSection(SolutionProperties) = preSolution
13721388
HideSolutionNode = FALSE
@@ -1428,5 +1444,6 @@ Global
14281444
{52C72C2E-9A76-4ECF-A210-C3F7C584C193} = {19500E0D-AAAB-6F02-E24F-82619ACA2290}
14291445
{4BD680A2-1ACB-7D6B-B2FD-8EBE9AEB5050} = {E8E33C5F-F9FB-3ACA-2B58-298ED48517C1}
14301446
{59CBDBE5-29BE-F38C-80E6-40843F2F8AF6} = {E8E33C5F-F9FB-3ACA-2B58-298ED48517C1}
1447+
{FC22C311-57DD-B069-4041-AD2AC8F80B5D} = {5555D2A1-8C8F-5B64-9F84-08EFE0FC7CD8}
14311448
EndGlobalSection
14321449
EndGlobal

0 commit comments

Comments
 (0)