@@ -5,13 +5,16 @@ import { makeApiGwEvent } from './utils/test-utils';
55import * as letterService from '../../services/letter-operations' ;
66import { LetterApiDocument , LetterApiStatus } from '../../contracts/letter-api' ;
77import { mapErrorToResponse } from '../../mappers/error-mapper' ;
8+ import { ValidationError } from '../../errors' ;
9+ import * as errors from '../../contracts/errors' ;
810
911jest . mock ( '../../services/letter-operations' ) ;
1012jest . mock ( '../../mappers/error-mapper' ) ;
1113
1214jest . mock ( "../../config/lambda-config" , ( ) => ( {
1315 lambdaConfig : {
14- SUPPLIER_ID_HEADER : "nhsd-supplier-id"
16+ SUPPLIER_ID_HEADER : "nhsd-supplier-id" ,
17+ APIM_CORRELATION_HEADER : 'nhsd-correlation-id'
1518 }
1619} ) ) ;
1720
@@ -53,7 +56,8 @@ describe('patchLetters API Handler', () => {
5356 path : '/letters/id1' ,
5457 body : requestBody ,
5558 pathParameters : { id : "id1" } ,
56- headers : { 'nhsd-supplier-id' : 'supplier1' } } ) ;
59+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
60+ } ) ;
5761 const context = mockDeep < Context > ( ) ;
5862 const callback = jest . fn ( ) ;
5963
@@ -71,54 +75,63 @@ describe('patchLetters API Handler', () => {
7175 const event = makeApiGwEvent ( {
7276 path : '/letters/id1' ,
7377 pathParameters : { id : "id1" } ,
74- headers : { 'nhsd-supplier-id' : 'supplier1' } } ) ;
78+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
79+ } ) ;
7580 const context = mockDeep < Context > ( ) ;
7681 const callback = jest . fn ( ) ;
7782
7883 const result = await patchLetters ( event , context , callback ) ;
7984
85+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new ValidationError ( errors . ApiErrorDetail . InvalidRequestMissingBody ) , 'correlationId' ) ;
8086 expect ( result ) . toEqual ( expectedErrorResponse ) ;
8187 } ) ;
8288
8389 it ( 'returns error response when path parameter letterId is not found' , async ( ) => {
8490 const event = makeApiGwEvent ( {
8591 path : '/letters/' ,
8692 body : requestBody ,
87- headers : { 'nhsd-supplier-id' : 'supplier1' } } ) ;
93+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
94+ } ) ;
8895 const context = mockDeep < Context > ( ) ;
8996 const callback = jest . fn ( ) ;
9097 const result = await patchLetters ( event , context , callback ) ;
9198
99+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new ValidationError ( errors . ApiErrorDetail . InvalidRequestMissingLetterIdPathParameter ) , 'correlationId' ) ;
92100 expect ( result ) . toEqual ( expectedErrorResponse ) ;
93101 } ) ;
94102
95103 it ( 'returns error response when error is thrown by service' , async ( ) => {
96- mockedPatchLetterStatus . mockRejectedValue ( new Error ( 'Service error' ) ) ;
104+ const error = new Error ( 'Service error' ) ;
105+ mockedPatchLetterStatus . mockRejectedValue ( error ) ;
97106
98107 const event = makeApiGwEvent ( {
99108 path : '/letters/id1' ,
100109 body : requestBody ,
101110 pathParameters : { id : "id1" } ,
102- headers : { 'nhsd-supplier-id' : 'supplier1' }
111+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
103112 } ) ;
104113 const context = mockDeep < Context > ( ) ;
105114 const callback = jest . fn ( ) ;
106115
107116 const result = await patchLetters ( event , context , callback ) ;
108117
118+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( error , 'correlationId' ) ;
109119 expect ( result ) . toEqual ( expectedErrorResponse ) ;
110120 } ) ;
111121
112- it ( 'returns error when nhsd- supplier- id is missing' , async ( ) => {
122+ it ( 'returns error when supplier id is missing' , async ( ) => {
113123 const event = makeApiGwEvent ( {
114124 path : '/letters/id1' ,
115125 body : requestBody ,
116- pathParameters : { id : "id1" } } ) ;
126+ pathParameters : { id : "id1" } ,
127+ headers : { 'nhsd-correlation-id' : 'correlationId' }
128+ } ) ;
117129 const context = mockDeep < Context > ( ) ;
118130 const callback = jest . fn ( ) ;
119131
120132 const result = await patchLetters ( event , context , callback ) ;
121133
134+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new ValidationError ( errors . ApiErrorDetail . InvalidRequestMissingSupplierId ) , 'correlationId' ) ;
122135 expect ( result ) . toEqual ( expectedErrorResponse ) ;
123136 } ) ;
124137
@@ -127,12 +140,14 @@ describe('patchLetters API Handler', () => {
127140 path : '/letters/id1' ,
128141 body : '{test: "test"}' ,
129142 pathParameters : { id : "id1" } ,
130- headers : { 'nhsd-supplier-id' : 'supplier1' } } ) ;
143+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
144+ } ) ;
131145 const context = mockDeep < Context > ( ) ;
132146 const callback = jest . fn ( ) ;
133147
134148 const result = await patchLetters ( event , context , callback ) ;
135149
150+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new ValidationError ( errors . ApiErrorDetail . InvalidRequestBody ) , 'correlationId' ) ;
136151 expect ( result ) . toEqual ( expectedErrorResponse ) ;
137152 } ) ;
138153
@@ -141,32 +156,69 @@ describe('patchLetters API Handler', () => {
141156 path : '/letters/id1' ,
142157 body : '{#invalidJSON' ,
143158 pathParameters : { id : "id1" } ,
144- headers : { 'nhsd-supplier-id' : 'supplier1' } } ) ;
159+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
160+ } ) ;
145161 const context = mockDeep < Context > ( ) ;
146162 const callback = jest . fn ( ) ;
147163
148164 const result = await patchLetters ( event , context , callback ) ;
149165
166+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new ValidationError ( errors . ApiErrorDetail . InvalidRequestBody ) , 'correlationId' ) ;
150167 expect ( result ) . toEqual ( expectedErrorResponse ) ;
151168 } ) ;
152169
153170 it ( 'returns error if unexpected error is thrown' , async ( ) => {
154171 const event = makeApiGwEvent ( {
155172 path : '/letters/id1' ,
156- body : '{#invalidJSON ' ,
173+ body : 'somebody ' ,
157174 pathParameters : { id : "id1" } ,
158- headers : { 'nhsd-supplier-id' : 'supplier1' } } ) ;
175+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
176+ } ) ;
159177 const context = mockDeep < Context > ( ) ;
160178 const callback = jest . fn ( ) ;
161179
180+ const error = "Unexpected error" ;
162181 const spy = jest . spyOn ( JSON , "parse" ) . mockImplementation ( ( ) => {
163- throw "Unexpected error" ;
182+ throw error ;
164183 } ) ;
165184
166185 const result = await patchLetters ( event , context , callback ) ;
167186
187+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( error , 'correlationId' ) ;
168188 expect ( result ) . toEqual ( expectedErrorResponse ) ;
169189
170190 spy . mockRestore ( ) ;
171191 } ) ;
192+
193+ it ( "returns error if correlation id not provided in request" , async ( ) => {
194+ const event = makeApiGwEvent ( {
195+ path : '/letters/id1' ,
196+ body : requestBody ,
197+ pathParameters : { id : "id1" } ,
198+ headers : { 'nhsd-supplier-id' : 'supplier1' }
199+ } ) ;
200+ const context = mockDeep < Context > ( ) ;
201+ const callback = jest . fn ( ) ;
202+
203+ const result = await patchLetters ( event , context , callback ) ;
204+
205+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new Error ( "The request headers don't contain the APIM correlation id" ) , undefined ) ;
206+ expect ( result ) . toEqual ( expectedErrorResponse ) ;
207+ } ) ;
208+
209+ it ( 'returns 400 for missing supplier ID (empty headers)' , async ( ) => {
210+ const event = makeApiGwEvent ( {
211+ path : '/letters/id1' ,
212+ body : requestBody ,
213+ pathParameters : { id : "id1" } ,
214+ headers : { }
215+ } ) ;
216+ const context = mockDeep < Context > ( ) ;
217+ const callback = jest . fn ( ) ;
218+
219+ const result = await patchLetters ( event , context , callback ) ;
220+
221+ expect ( mockedMapErrorToResponse ) . toHaveBeenCalledWith ( new Error ( 'The request headers are empty' ) , undefined ) ;
222+ expect ( result ) . toEqual ( expectedErrorResponse ) ;
223+ } ) ;
172224} ) ;
0 commit comments