Skip to content

Commit

Permalink
Add and amend unit tests for now patient flow
Browse files Browse the repository at this point in the history
  • Loading branch information
rcrichton committed Feb 9, 2024
1 parent d6f6533 commit a51660f
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 188 deletions.
30 changes: 21 additions & 9 deletions src/utils/kafkaFhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,29 @@ export const processBundle = async (bundle: Bundle): Promise<MpiMediatorResponse

const clientRegistryResponses = await Promise.all(promises);

clientRegistryResponses.map((clientRegistryResponse, index) => {
if (!isHttpStatusOk(clientRegistryResponse.status)) {
logger.error(
`Patient resource creation in Client Registry failed: ${JSON.stringify(
clientRegistryResponse.body
)}`
);
const failedRequests = clientRegistryResponses.filter(
(clientRegistryResponse) => !isHttpStatusOk(clientRegistryResponse.status)
);

return null;
}
if (failedRequests.length > 0) {
logger.error(
`Patient resource creation in Client Registry failed: ${JSON.stringify(failedRequests)}`
);

// combine all failed requests into a single response
const combinedResponse: ResponseObject = failedRequests.reduce(
(combined: { status: number; body: { errors: any[] } }, current) => {
combined.body.errors.push(current.body);

return combined;
},
{ status: failedRequests[0].status, body: { errors: [] } }
);

return createHandlerResponseObject('Failed', combinedResponse);
}

clientRegistryResponses.map((clientRegistryResponse, index) => {
const fullUrl = patientEntries[index]?.fullUrl;

if (fullUrl) {
Expand Down
146 changes: 143 additions & 3 deletions tests/unit/kafkaFhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getConfig } from '../../src/config/config';

import * as kafkaFhir from '../../src/utils/kafkaFhir';
import { RequestDetails } from '../../src/types/request';
import { NewPatientMap } from '../../src/types/newPatientMap';

const config = getConfig();

Expand Down Expand Up @@ -112,11 +113,12 @@ describe('Kafka Fhir interaction', (): void => {
stub.restore();
});

it('should add patient to fhir bundle before sending to kafka', async (): Promise<void> => {
it('should restore full patient data to fhir bundle before sending to kafka', async (): Promise<void> => {
const stub = sinon.stub(kafkaFhir, 'sendToKafka');
stub.callsFake(async (bundle: Bundle, topic: string): Promise<Error | null> => {
expect(topic).to.equal(config.kafkaBundleTopic);
expect(bundle.entry?.length).to.be.equal(2);
expect(bundle.entry?.[1].resource).deep.equal(restoredPatient);
return null;
});

Expand All @@ -142,11 +144,149 @@ describe('Kafka Fhir interaction', (): void => {
id: '1233',
} as FhirResource,
},
{
fullUrl: 'Patient/1234',
resource: {
resourceType: 'Patient',
link: [
{
type: 'refer',
other: {
reference: 'http://santedb-mpi:8080/fhir/Patient/xxx',
},
},
],
},
request: {
method: 'PUT',
url: 'Patient/xxx',
},
},
],
};
const patient: Patient = {

const restoredPatient: Patient = {
resourceType: 'Patient',
id: '1233',
name: [
{
given: ['John'],
family: 'Doe',
},
],
extension: [
{
url: 'http://hl7.org/fhir/StructureDefinition/patient-mothersMaidenName',
valueString: 'Jane Doe',
},
],
managingOrganization: {
reference: 'Organization/1',
},
};

const newPatientMap: NewPatientMap = {
'Patient/1234': {
restoredPatient,
},
};

nock(
`${config.fhirDatastoreProtocol}://${config.fhirDatastoreHost}:${config.fhirDatastorePort}`
)
.post(`/fhir`)
.reply(200, {
resourceType: 'Bundle',
id: '123',
});

const response = await kafkaFhir.sendToFhirAndKafka(
fhirDatastoreRequestDetailsOrg,
bundle,
newPatientMap
);

expect(response.status).to.be.equal(200);
expect(response.body.status).to.be.equal('Success');
stub.restore();
});

it('should ADD restored patient data to fhir bundle before sending to kafka if no matching patient is found in bundle', async (): Promise<void> => {
const stub = sinon.stub(kafkaFhir, 'sendToKafka');
stub.callsFake(async (bundle: Bundle, topic: string): Promise<Error | null> => {
expect(topic).to.equal(config.kafkaBundleTopic);
expect(bundle.entry?.length).to.be.equal(3);
expect(bundle.entry?.[2].resource).deep.equal(restoredPatient);
return null;
});

const fhirDatastoreRequestDetailsOrg: RequestDetails = {
protocol: config.fhirDatastoreProtocol,
host: config.fhirDatastoreHost,
port: config.fhirDatastorePort,
headers: { contentType: 'application/fhir+json' },
method: 'POST',
path: '/fhir',
data: '',
};

const bundle: Bundle = {
type: 'document',
resourceType: 'Bundle',
id: '12',
entry: [
{
fullUrl: 'Encounter/1234',
resource: {
resourceType: 'Encounter',
id: '1233',
} as FhirResource,
},
{
fullUrl: 'Patient/5555',
resource: {
resourceType: 'Patient',
link: [
{
type: 'refer',
other: {
reference: 'http://santedb-mpi:8080/fhir/Patient/xxx',
},
},
],
},
request: {
method: 'PUT',
url: 'Patient/xxx',
},
},
],
};

const restoredPatient: Patient = {
resourceType: 'Patient',
id: '1233',
name: [
{
given: ['John'],
family: 'Doe',
},
],
extension: [
{
url: 'http://hl7.org/fhir/StructureDefinition/patient-mothersMaidenName',
valueString: 'Jane Doe',
},
],
managingOrganization: {
reference: 'Organization/1',
},
};

const newPatientMap: NewPatientMap = {
'Patient/1234': {
restoredPatient,
},
};

nock(
Expand All @@ -161,7 +301,7 @@ describe('Kafka Fhir interaction', (): void => {
const response = await kafkaFhir.sendToFhirAndKafka(
fhirDatastoreRequestDetailsOrg,
bundle,
patient
newPatientMap
);

expect(response.status).to.be.equal(200);
Expand Down
114 changes: 44 additions & 70 deletions tests/unit/matchPatientSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,67 +179,11 @@ describe('Match Patient Synchronously', (): void => {

expect(handlerResponse.status).to.be.equal(500);
expect(handlerResponse.body.status).to.be.equal('Failed');
expect(handlerResponse.body.response.body).to.deep.equal(error);
expect(handlerResponse.body.response.body).to.deep.equal({ errors: [error] });
stub.restore();
});

it('should return error response when patient referenced does not exist the in Client Registry', async (): Promise<void> => {
const patientId: string = '1234';
const bundle: Bundle = {
type: 'document',
resourceType: 'Bundle',
id: '12',
entry: [
{
fullUrl: 'Encounter/1234',
resource: {
resourceType: 'Encounter',
id: '1233',
subject: {
reference: `Patient/${patientId}`,
},
status: 'planned',
},
},
],
};
const error = {
error: 'Resource not found',
};

mockSuccefullValidation();

nock(`${config.mpiProtocol}://${config.mpiHost}:${config.mpiPort}`)
.get(`/fhir/Patient/${patientId}`)
.reply(404, error);

const stub = sinon.stub(Auth, 'getMpiAuthToken');
stub.callsFake(async (): Promise<OAuth2Token> => {
const clientData = {
clientId: '',
clientSecret: 'secret',
accessTokenUri: 'test',
scopes: [],
};
const client = new ClientOAuth2(clientData);

const oauth2 = new OAuth2Token(client, {
token_type: 'bearer',
access_token: 'accessToken',
refresh_token: 'refreshToken',
expires_in: '3',
});
return oauth2;
});
const handlerResponse = await matchSyncHandler(bundle);

expect(handlerResponse.status).to.be.equal(404);
expect(handlerResponse.body.status).to.be.equal('Failed');
expect(handlerResponse.body.response.body).to.deep.equal(error);
stub.restore();
});

it('should send to the FHir store and Kafka when patient exists in the Client Registry', async (): Promise<void> => {
it('should send to the FHIR store and Kafka when patient exists in the Client Registry', async (): Promise<void> => {
const patientId: string = 'testPatient';
const bundle: Bundle = {
type: 'document',
Expand Down Expand Up @@ -271,7 +215,7 @@ describe('Match Patient Synchronously', (): void => {
resourceType: 'Encounter',
id: '1233',
subject: {
reference: `${config.mpiProtocol}://${config.mpiHost}:${config.mpiPort}/fhir/Patient/${patientId}`,
reference: `Patient/${patientId}`,
},
status: 'planned',
},
Expand Down Expand Up @@ -345,7 +289,7 @@ describe('Match Patient Synchronously', (): void => {
stub1.restore();
});

it('should send to the FHir store and Kafka when patient is created in the Client Registry', async (): Promise<void> => {
it('should send gutted patient and clinical data to the FHIR store and Kafka when patient is created in the Client Registry', async (): Promise<void> => {
const patientId: string = 'testPatient';
const bundle: Bundle = {
type: 'document',
Expand All @@ -372,7 +316,6 @@ describe('Match Patient Synchronously', (): void => {
},
],
};
const clientRegistryRef = `${config.mpiProtocol}://${config.mpiHost}:${config.mpiPort}/fhir/Patient/${patientId}`;

const modifiedBundle: Bundle = {
resourceType: 'Bundle',
Expand All @@ -385,7 +328,7 @@ describe('Match Patient Synchronously', (): void => {
resourceType: 'Encounter',
id: '1233',
subject: {
reference: clientRegistryRef,
reference: `Patient/12333`,
},
status: 'planned',
},
Expand All @@ -394,6 +337,24 @@ describe('Match Patient Synchronously', (): void => {
url: 'Encounter/1233',
},
},
{
fullUrl: 'Patient/12333',
request: {
method: 'PUT',
url: 'Patient/testPatient',
},
resource: {
link: [
{
other: {
reference: 'http://santedb-mpi:8080/fhir/Patient/testPatient',
},
type: 'refer',
},
],
resourceType: 'Patient',
},
},
],
};
const clientRegistryResponse = {
Expand Down Expand Up @@ -427,16 +388,29 @@ describe('Match Patient Synchronously', (): void => {
});
const stub1 = sinon.stub(kafkaFhir, 'sendToFhirAndKafka');
stub1.callsFake(
async (
requestDetails,
bundle,
patient,
newPatientRef
): Promise<MpiMediatorResponseObject> => {
async (requestDetails, bundle, newPatientRef): Promise<MpiMediatorResponseObject> => {
expect(requestDetails).to.deep.equal(fhirDatastoreRequestDetailsOrg);
expect(bundle).to.be.deep.equal(modifiedBundle);
expect(patient).to.be.deep.equal(clientRegistryResponse);
expect(newPatientRef).to.equal(clientRegistryRef);
expect(newPatientRef).to.deep.equal({
'Patient/12333': {
mpiResponsePatient: {
id: 'testPatient',
resourceType: 'Patient',
},
mpiTransformResult: {
extension: undefined,
managingOrganization: undefined,
patient: {
id: '12333',
resourceType: 'Patient',
},
},
restoredPatient: {
id: 'testPatient',
resourceType: 'Patient',
},
},
});

return {
status: 200,
Expand Down
Loading

0 comments on commit a51660f

Please sign in to comment.