1515import org .springframework .data .mongodb .core .query .Update ;
1616import org .springframework .data .mongodb .core .query .UpdateDefinition ;
1717import uk .nhs .adaptors .gp2gp .common .service .TimestampService ;
18+ import uk .nhs .adaptors .gp2gp .ehr .exception .EhrExtractException ;
1819import uk .nhs .adaptors .gp2gp .ehr .model .EhrExtractStatus ;
1920import uk .nhs .adaptors .gp2gp .mhs .exception .UnrecognisedInteractionIdException ;
2021
2627import java .util .Optional ;
2728import java .util .UUID ;
2829
30+ import static java .lang .String .format ;
2931import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
3032import static org .junit .jupiter .api .Assertions .assertEquals ;
3133import static org .junit .jupiter .api .Assertions .assertFalse ;
@@ -112,7 +114,7 @@ void shouldLogWarningWhenLateAcknowledgementReceivedAfter8DaysAndEhrReceivedAckH
112114 ehrExtractStatusServiceSpy .updateEhrExtractStatusAck (conversationId , ack );
113115
114116 verify (logger , times (1 ))
115- .warn ("Received an ACK message with conversation_id : {}, "
117+ .warn ("Received an ACK message with conversationId : {}, "
116118 + "but it is being ignored because the EhrExtract has already been marked as failed "
117119 + "from not receiving an acknowledgement from the requester in time." ,
118120 conversationId );
@@ -144,9 +146,9 @@ void shouldNotLogWarningThatAckIsIgnoredWhenAcknowledgementReceivedAfter8Days()
144146 ehrExtractStatusServiceSpy .updateEhrExtractStatusAck (conversationId , ack );
145147
146148 verify (logger , never ())
147- .warn ("Received an ACK message with a conversation_id : {}, but it will be ignored" , conversationId );
149+ .warn ("Received an ACK message with a conversationId : {}, but it will be ignored" , conversationId );
148150 verify (logger , times (1 ))
149- .warn ("Received an ACK message with a conversation_id : {} that is a duplicate" , conversationId );
151+ .warn ("Received an ACK message with a conversationId : {} that is a duplicate" , conversationId );
150152 }
151153
152154 @ Test
@@ -166,7 +168,7 @@ void shouldUpdateStatusWithErrorAndSpecificErrorCodeAndMessage() {
166168 ERROR_MESSAGE );
167169
168170 verify (logger ).info ("EHR status (EHR received acknowledgement) record successfully "
169- + "updated in the database with error information conversation_id : {}" , inProgressConversationId );
171+ + "updated in the database with error information conversationId : {}" , inProgressConversationId );
170172 verify (mongoTemplate , times (1 )).findAndModify (queryCaptor .capture (),
171173 updateCaptor .capture (),
172174 any (FindAndModifyOptions .class ),
@@ -219,9 +221,32 @@ void shouldNotLogWarningThatAckIsIgnoredWhenAcknowledgementReceivedAfterWithinAn
219221 ehrExtractStatusServiceSpy .updateEhrExtractStatusAck (conversationId , ack );
220222
221223 verify (logger , never ())
222- .warn ("Received an ACK message with a conversation_id : {}, but it will be ignored" , conversationId );
224+ .warn ("Received an ACK message with a conversationId : {}, but it will be ignored" , conversationId );
223225 verify (logger , times (1 ))
224- .warn ("Received an ACK message with a conversation_id: {} that is a duplicate" , conversationId );
226+ .warn ("Received an ACK message with a conversationId: {} that is a duplicate" , conversationId );
227+ }
228+
229+ @ Test
230+ void receiveExceptionWhenEhrExtractStatusIsNull () {
231+
232+ EhrExtractStatusService ehrExtractStatusServiceSpy = spy (ehrExtractStatusService );
233+ String conversationId = generateRandomUppercaseUUID ();
234+ Instant currentInstant = Instant .now ();
235+ Optional <EhrExtractStatus > ehrExtractStatus = Optional .of (EhrExtractStatus .builder ().ehrExtractCorePending (null ).build ());
236+
237+ doReturn (true ).when (ehrExtractStatusServiceSpy ).isEhrStatusWaitingForFinalAck (conversationId );
238+ doReturn (false ).when (ehrExtractStatusServiceSpy ).hasFinalAckBeenReceived (conversationId );
239+ doReturn (ehrExtractStatus ).when (ehrExtractStatusRepository ).findByConversationId (conversationId );
240+ when (ack .getErrors ()).thenReturn (null );
241+ when (ack .getReceived ()).thenReturn (currentInstant );
242+ doReturn (null )
243+ .when (mongoTemplate ).findAndModify (any (Query .class ), any (Update .class ), any (FindAndModifyOptions .class ), any (Class .class ));
244+
245+ Exception ehrExtractException = assertThrows (EhrExtractException .class ,
246+ () -> ehrExtractStatusServiceSpy .updateEhrExtractStatusAck (conversationId , ack ));
247+
248+ assertTrue (ehrExtractException .getMessage ()
249+ .contains (format ("Received an ACK message with a conversationId %s that is not recognised" , conversationId )));
225250 }
226251
227252 @ Test
@@ -243,9 +268,9 @@ void updateEhrExtractStatusWhenEhrExtractCorePendingIsNull() {
243268
244269 ehrExtractStatusServiceSpy .updateEhrExtractStatusAck (conversationId , ack );
245270
246- verify (logger , never ()).warn ("Received an ACK message with a conversation_id ={} exceeded 8 days" , conversationId );
271+ verify (logger , never ()).warn ("Received an ACK message with a conversationId ={} exceeded 8 days" , conversationId );
247272 verify (logger , times (1 ))
248- .info ("Database successfully updated with EHRAcknowledgement, conversation_id : {}" , conversationId );
273+ .info ("Database successfully updated with EHRAcknowledgement, conversationId : {}" , conversationId );
249274 }
250275
251276 @ Test
@@ -262,7 +287,7 @@ void ehrExtractStatusThrowsExceptionWhenConversationIdNotFound() {
262287 Exception exception = assertThrows (UnrecognisedInteractionIdException .class , () ->
263288 ehrExtractStatusServiceSpy .updateEhrExtractStatusAck (conversationId , ack ));
264289
265- assertEquals ("Received an unrecognized ACK message with conversation_id : " + conversationId ,
290+ assertEquals ("Received an unrecognized ACK message with conversationId : " + conversationId ,
266291 exception .getMessage ());
267292
268293 verify (ehrExtractStatusServiceSpy , never ()).updateEhrExtractStatusError (conversationId ,
@@ -307,6 +332,31 @@ void doesNotUpdateEhrExtractStatusWhenEhrContinueIsPresent() {
307332 assertFalse (ehrExtractStatusResult .isPresent ());
308333 }
309334
335+ @ Test
336+ void throwsEhrExtractExceptionWhenEhrExtractStatusIsNotFound () {
337+ EhrExtractStatusService ehrExtractStatusServiceSpy = spy (ehrExtractStatusService );
338+ String conversationId = generateRandomUppercaseUUID ();
339+
340+ Optional <EhrExtractStatus > ehrExtractStatus = Optional .of (EhrExtractStatus .builder ()
341+ .ehrExtractCorePending (
342+ EhrExtractStatus
343+ .EhrExtractCorePending
344+ .builder ()
345+ .sentAt (Instant .now ())
346+ .taskId ("22222" ).build ())
347+ .build ());
348+ doReturn (ehrExtractStatus ).when (ehrExtractStatusRepository ).findByConversationId (conversationId );
349+
350+ doReturn (null ).when (mongoTemplate ).findAndModify (any (Query .class ), any (UpdateDefinition .class ),
351+ any (FindAndModifyOptions .class ), any ());
352+
353+ Exception exception = assertThrows (EhrExtractException .class ,
354+ () -> ehrExtractStatusServiceSpy .updateEhrExtractStatusContinue (conversationId ));
355+
356+ assertTrue (exception .getMessage ()
357+ .contains (format ("Received a Continue message with a conversationId %s that is not recognised" , conversationId )));
358+ }
359+
310360 @ Test
311361 void expectTrueWhenEhrExtractStatusWithEhrReceivedAckWithErrorsAndExceededTimeoutLimit () {
312362
@@ -404,7 +454,7 @@ void shouldUpdateStatusWithErrorWhenEhrExtractAckTimeoutOccurs() {
404454 ERROR_MESSAGE );
405455
406456 verify (logger ).info ("EHR status (EHR received acknowledgement) record successfully "
407- + "updated in the database with error information conversation_id : {}" , inProgressConversationId );
457+ + "updated in the database with error information conversationId : {}" , inProgressConversationId );
408458 }
409459
410460
@@ -521,7 +571,7 @@ void shouldLogWarningWithMsgIgnoredWhenLateAcknowledgementReceivedAfter8DaysAndE
521571 ehrExtractStatusServiceSpy .updateEhrExtractStatusAck (conversationId , ack );
522572
523573 verify (logger , times (1 ))
524- .warn ("Received an ACK message with conversation_id : {}, "
574+ .warn ("Received an ACK message with conversationId : {}, "
525575 + "but it is being ignored because the EhrExtract has already been marked as failed "
526576 + "from not receiving an acknowledgement from the requester in time." ,
527577 conversationId );
0 commit comments