@@ -40,15 +40,18 @@ describe('SQS Handler', () => {
4040
4141 const response = await handler ( recordEvent ( [ pdmResourceSubmittedEvent ] ) ) ;
4242
43- expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith ( [
44- {
45- ...pdmResourceSubmittedEvent ,
46- id : '550e8400-e29b-41d4-a716-446655440001' ,
47- time : '2023-06-20T12:00:00.250Z' ,
48- recordedtime : '2023-06-20T12:00:00.250Z' ,
49- type : 'uk.nhs.notify.digital.letters.pdm.resource.available.v1' ,
50- } ,
51- ] ) ;
43+ expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith (
44+ [
45+ {
46+ ...pdmResourceSubmittedEvent ,
47+ id : '550e8400-e29b-41d4-a716-446655440001' ,
48+ time : '2023-06-20T12:00:00.250Z' ,
49+ recordedtime : '2023-06-20T12:00:00.250Z' ,
50+ type : 'uk.nhs.notify.digital.letters.pdm.resource.available.v1' ,
51+ } ,
52+ ] ,
53+ expect . any ( Function ) ,
54+ ) ;
5255 expect ( logger . info ) . toHaveBeenCalledWith (
5356 'Received SQS Event of 1 record(s)' ,
5457 ) ;
@@ -63,19 +66,22 @@ describe('SQS Handler', () => {
6366
6467 const response = await handler ( recordEvent ( [ pdmResourceSubmittedEvent ] ) ) ;
6568
66- expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith ( [
67- {
68- ...pdmResourceSubmittedEvent ,
69- id : '550e8400-e29b-41d4-a716-446655440001' ,
70- time : '2023-06-20T12:00:00.250Z' ,
71- recordedtime : '2023-06-20T12:00:00.250Z' ,
72- type : 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1' ,
73- data : {
74- ...pdmResourceSubmittedEvent . data ,
75- retryCount : 0 ,
69+ expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith (
70+ [
71+ {
72+ ...pdmResourceSubmittedEvent ,
73+ id : '550e8400-e29b-41d4-a716-446655440001' ,
74+ time : '2023-06-20T12:00:00.250Z' ,
75+ recordedtime : '2023-06-20T12:00:00.250Z' ,
76+ type : 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1' ,
77+ data : {
78+ ...pdmResourceSubmittedEvent . data ,
79+ retryCount : 0 ,
80+ } ,
7681 } ,
77- } ,
78- ] ) ;
82+ ] ,
83+ expect . any ( Function ) ,
84+ ) ;
7985 expect ( logger . info ) . toHaveBeenCalledWith (
8086 'Received SQS Event of 1 record(s)' ,
8187 ) ;
@@ -94,15 +100,18 @@ describe('SQS Handler', () => {
94100 recordEvent ( [ pdmResourceUnavailableEvent ] ) ,
95101 ) ;
96102
97- expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith ( [
98- {
99- ...pdmResourceUnavailableEvent ,
100- id : '550e8400-e29b-41d4-a716-446655440001' ,
101- time : '2023-06-20T12:00:00.250Z' ,
102- recordedtime : '2023-06-20T12:00:00.250Z' ,
103- type : 'uk.nhs.notify.digital.letters.pdm.resource.available.v1' ,
104- } ,
105- ] ) ;
103+ expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith (
104+ [
105+ {
106+ ...pdmResourceUnavailableEvent ,
107+ id : '550e8400-e29b-41d4-a716-446655440001' ,
108+ time : '2023-06-20T12:00:00.250Z' ,
109+ recordedtime : '2023-06-20T12:00:00.250Z' ,
110+ type : 'uk.nhs.notify.digital.letters.pdm.resource.available.v1' ,
111+ } ,
112+ ] ,
113+ expect . any ( Function ) ,
114+ ) ;
106115 expect ( logger . info ) . toHaveBeenCalledWith (
107116 'Received SQS Event of 1 record(s)' ,
108117 ) ;
@@ -119,19 +128,22 @@ describe('SQS Handler', () => {
119128 recordEvent ( [ pdmResourceUnavailableEvent ] ) ,
120129 ) ;
121130
122- expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith ( [
123- {
124- ...pdmResourceUnavailableEvent ,
125- id : '550e8400-e29b-41d4-a716-446655440001' ,
126- time : '2023-06-20T12:00:00.250Z' ,
127- recordedtime : '2023-06-20T12:00:00.250Z' ,
128- type : 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1' ,
129- data : {
130- ...pdmResourceSubmittedEvent . data ,
131- retryCount : 2 ,
131+ expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith (
132+ [
133+ {
134+ ...pdmResourceUnavailableEvent ,
135+ id : '550e8400-e29b-41d4-a716-446655440001' ,
136+ time : '2023-06-20T12:00:00.250Z' ,
137+ recordedtime : '2023-06-20T12:00:00.250Z' ,
138+ type : 'uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1' ,
139+ data : {
140+ ...pdmResourceSubmittedEvent . data ,
141+ retryCount : 2 ,
142+ } ,
132143 } ,
133- } ,
134- ] ) ;
144+ ] ,
145+ expect . any ( Function ) ,
146+ ) ;
135147 expect ( logger . info ) . toHaveBeenCalledWith (
136148 'Received SQS Event of 1 record(s)' ,
137149 ) ;
@@ -154,19 +166,22 @@ describe('SQS Handler', () => {
154166
155167 const response = await handler ( recordEvent ( [ testEvent ] ) ) ;
156168
157- expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith ( [
158- {
159- ...pdmResourceUnavailableEvent ,
160- id : '550e8400-e29b-41d4-a716-446655440001' ,
161- time : '2023-06-20T12:00:00.250Z' ,
162- recordedtime : '2023-06-20T12:00:00.250Z' ,
163- type : 'uk.nhs.notify.digital.letters.pdm.resource.retries.exceeded.v1' ,
164- data : {
165- ...pdmResourceSubmittedEvent . data ,
166- retryCount : 10 ,
169+ expect ( eventPublisher . sendEvents ) . toHaveBeenCalledWith (
170+ [
171+ {
172+ ...pdmResourceUnavailableEvent ,
173+ id : '550e8400-e29b-41d4-a716-446655440001' ,
174+ time : '2023-06-20T12:00:00.250Z' ,
175+ recordedtime : '2023-06-20T12:00:00.250Z' ,
176+ type : 'uk.nhs.notify.digital.letters.pdm.resource.retries.exceeded.v1' ,
177+ data : {
178+ ...pdmResourceSubmittedEvent . data ,
179+ retryCount : 10 ,
180+ } ,
167181 } ,
168- } ,
169- ] ) ;
182+ ] ,
183+ expect . any ( Function ) ,
184+ ) ;
170185 expect ( logger . info ) . toHaveBeenCalledWith (
171186 'Received SQS Event of 1 record(s)' ,
172187 ) ;
@@ -178,16 +193,91 @@ describe('SQS Handler', () => {
178193 } ) ;
179194
180195 describe ( 'errors' , ( ) => {
181- it ( 'should return failed items to the queue if an error occurs while processing them' , async ( ) => {
196+ it ( 'should return failed SQS records to the queue if an error occurs while calling PDM' , async ( ) => {
197+ pdm . poll . mockRejectedValueOnce ( new Error ( 'PDM error' ) ) ;
182198 const event = recordEvent ( [ pdmResourceSubmittedEvent ] ) ;
183- event . Records [ 0 ] . body = 'not-json' ;
184199
185200 const result = await handler ( event ) ;
186201
187202 expect ( logger . warn ) . toHaveBeenCalledWith ( {
188- error : `Unexpected token 'o', "not-json" is not valid JSON` ,
203+ err : 'PDM error' ,
189204 description : 'Failed processing message' ,
190- messageId : '1' ,
205+ } ) ;
206+
207+ expect ( logger . info ) . toHaveBeenCalledWith (
208+ '0 of 1 records processed successfully' ,
209+ ) ;
210+
211+ expect ( result ) . toEqual ( {
212+ batchItemFailures : [ { itemIdentifier : '1' } ] ,
213+ } ) ;
214+ } ) ;
215+
216+ it ( 'should return failed SQS records to the queue if an error occurs while processing them' , async ( ) => {
217+ const event = recordEvent ( [ pdmResourceSubmittedEvent ] ) ;
218+ event . Records [ 0 ] . body = 'not-json' ;
219+
220+ const result = await handler ( event ) ;
221+
222+ expect ( logger . warn ) . toHaveBeenCalledWith ( {
223+ err : new SyntaxError (
224+ `Unexpected token 'o', "not-json" is not valid JSON` ,
225+ ) ,
226+ description : 'Error parsing SQS record' ,
227+ } ) ;
228+
229+ expect ( logger . info ) . toHaveBeenCalledWith (
230+ '0 of 1 records processed successfully' ,
231+ ) ;
232+
233+ expect ( result ) . toEqual ( {
234+ batchItemFailures : [ { itemIdentifier : '1' } ] ,
235+ } ) ;
236+ } ) ;
237+
238+ it ( 'should return failed items to the queue if an invalid pdm.resource.submitted event is received' , async ( ) => {
239+ const invalidSubmittedEvent = {
240+ ...pdmResourceSubmittedEvent ,
241+ source : 'invalid pdm.resource.submitted source' ,
242+ } ;
243+ const event = recordEvent ( [ invalidSubmittedEvent ] ) ;
244+
245+ const result = await handler ( event ) ;
246+
247+ expect ( logger . warn ) . toHaveBeenCalledWith ( {
248+ err : expect . arrayContaining ( [
249+ expect . objectContaining ( {
250+ instancePath : '/source' ,
251+ } ) ,
252+ ] ) ,
253+ description : 'Error parsing queue entry' ,
254+ } ) ;
255+
256+ expect ( logger . info ) . toHaveBeenCalledWith (
257+ '0 of 1 records processed successfully' ,
258+ ) ;
259+
260+ expect ( result ) . toEqual ( {
261+ batchItemFailures : [ { itemIdentifier : '1' } ] ,
262+ } ) ;
263+ } ) ;
264+
265+ it ( 'should return failed items to the queue if an invalid pdm.resource.unavailable event is received' , async ( ) => {
266+ const invalidSubmittedEvent = {
267+ ...pdmResourceUnavailableEvent ,
268+ source : 'invalid pdm.resource.unavailable source' ,
269+ } ;
270+ const event = recordEvent ( [ invalidSubmittedEvent ] ) ;
271+
272+ const result = await handler ( event ) ;
273+
274+ expect ( logger . warn ) . toHaveBeenCalledWith ( {
275+ err : expect . arrayContaining ( [
276+ expect . objectContaining ( {
277+ instancePath : '/source' ,
278+ } ) ,
279+ ] ) ,
280+ description : 'Error parsing queue entry' ,
191281 } ) ;
192282
193283 expect ( logger . info ) . toHaveBeenCalledWith (
0 commit comments