From 98c316d540351e97d05dcc9bdbf46eeb727e5af7 Mon Sep 17 00:00:00 2001 From: Andy Flint Date: Wed, 5 Jun 2024 16:59:32 +0100 Subject: [PATCH] [PRMT-4840] Added TransferService unit tests for new retry logic --- .../database/TransferServiceTest.java | 86 +++++++++++++------ 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/src/test/java/uk/nhs/prm/repo/ehrtransferservice/database/TransferServiceTest.java b/src/test/java/uk/nhs/prm/repo/ehrtransferservice/database/TransferServiceTest.java index 6fa11494..03a03ebd 100644 --- a/src/test/java/uk/nhs/prm/repo/ehrtransferservice/database/TransferServiceTest.java +++ b/src/test/java/uk/nhs/prm/repo/ehrtransferservice/database/TransferServiceTest.java @@ -8,26 +8,27 @@ import uk.nhs.prm.repo.ehrtransferservice.builders.ConversationRecordBuilder; import uk.nhs.prm.repo.ehrtransferservice.database.enumeration.ConversationTransferStatus; import uk.nhs.prm.repo.ehrtransferservice.database.model.ConversationRecord; -import uk.nhs.prm.repo.ehrtransferservice.exceptions.FailedToPersistException; -import uk.nhs.prm.repo.ehrtransferservice.exceptions.base.DatabaseException; +import uk.nhs.prm.repo.ehrtransferservice.exceptions.ConversationIneligibleForRetryException; import uk.nhs.prm.repo.ehrtransferservice.repo_incoming.RepoIncomingEvent; import uk.nhs.prm.repo.ehrtransferservice.services.ConversationActivityService; import java.util.UUID; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static uk.nhs.prm.repo.ehrtransferservice.database.enumeration.ConversationTransferStatus.*; @ExtendWith(MockitoExtension.class) class TransferServiceTest { - @Mock private TransferRepository transferRepository; @Mock - private ConversationActivityService conversationActivityService; + private ConversationActivityService activityService; @InjectMocks private TransferService transferService; @@ -41,29 +42,66 @@ class TransferServiceTest { private static final String NEMS_EVENT_LAST_UPDATED = "2023-10-09T15:38:03.291499328Z"; @Test - void createOrRetryConversation_ValidRepoIncomingEvent_DoesNotThrow() { + void createOrRetryConversation_ValidNewConversationRequest_Ok() { + // given + final UUID inboundConversationId = UUID.randomUUID(); + final RepoIncomingEvent repoIncomingEvent = createRepoIncomingEvent(inboundConversationId); + + // when + when(transferRepository.isInboundConversationPresent(inboundConversationId)).thenReturn(false); + + // then + assertDoesNotThrow(() -> transferService.createOrRetryConversation(repoIncomingEvent)); + + verify(transferRepository).isInboundConversationPresent(inboundConversationId); + verify(activityService).captureConversationActivity(inboundConversationId); + verify(transferRepository).createConversation(repoIncomingEvent); + + verify(transferRepository, never()).updateConversationStatus(any(), any()); + } + + @Test + void createOrRetryConversation_ValidRetriedConversationRequest_Ok() { // given final UUID inboundConversationId = UUID.randomUUID(); final RepoIncomingEvent repoIncomingEvent = createRepoIncomingEvent(inboundConversationId); + final ConversationRecord conversationRecord = createConversationRecord(inboundConversationId, INBOUND_REQUEST_SENT); + + // when + when(transferRepository.isInboundConversationPresent(inboundConversationId)).thenReturn(true); + when(transferRepository.findConversationByInboundConversationId(inboundConversationId)).thenReturn(conversationRecord); // then assertDoesNotThrow(() -> transferService.createOrRetryConversation(repoIncomingEvent)); + + verify(transferRepository).isInboundConversationPresent(inboundConversationId); + verify(transferRepository).findConversationByInboundConversationId(inboundConversationId); + verify(activityService).captureConversationActivity(inboundConversationId); + verify(transferRepository).updateConversationStatus(inboundConversationId, INBOUND_STARTED); + + verify(transferRepository, never()).createConversation(any()); } @Test - void createConversation_TransferRepositoryThrowsDatabaseException_CreateOrRetryConversationThrows() { + void createOrRetryConversation_RetriedConversationRequestNotRetryable_ThrowsConversationIneligibleForRetryException() { // given final UUID inboundConversationId = UUID.randomUUID(); final RepoIncomingEvent repoIncomingEvent = createRepoIncomingEvent(inboundConversationId); + final ConversationRecord conversationRecord = createConversationRecord(inboundConversationId, INBOUND_FAILED); // when - doThrow(FailedToPersistException.class) - .when(transferRepository) - .createConversation(repoIncomingEvent); + when(transferRepository.isInboundConversationPresent(inboundConversationId)).thenReturn(true); + when(transferRepository.findConversationByInboundConversationId(inboundConversationId)).thenReturn(conversationRecord); // then - assertThrows(DatabaseException.class, - () -> transferService.createOrRetryConversation(repoIncomingEvent)); + assertThrows(ConversationIneligibleForRetryException.class, () -> transferService.createOrRetryConversation(repoIncomingEvent)); + + verify(transferRepository).isInboundConversationPresent(inboundConversationId); + verify(transferRepository).findConversationByInboundConversationId(inboundConversationId); + + verify(activityService, never()).captureConversationActivity(any()); + verify(transferRepository, never()).updateConversationStatus(any(), any()); + verify(transferRepository, never()).createConversation(any()); } @Test @@ -72,7 +110,7 @@ void updateConversationTransferStatus_ConversationIsTerminated_DoNotUpdateTransf final UUID inboundConversationId = UUID.randomUUID(); final ConversationTransferStatus currentTransferStatus = INBOUND_COMPLETE; final ConversationTransferStatus newTransferStatus = INBOUND_TIMEOUT; - final ConversationRecord conversationRecord = createOrRetryConversationRecord(inboundConversationId, currentTransferStatus); + final ConversationRecord conversationRecord = createConversationRecord(inboundConversationId, currentTransferStatus); // when @@ -83,7 +121,7 @@ void updateConversationTransferStatus_ConversationIsTerminated_DoNotUpdateTransf // then verify(transferRepository, never()).updateConversationStatus(any(), any()); - verify(conversationActivityService).concludeConversationActivity(inboundConversationId); + verify(activityService).concludeConversationActivity(inboundConversationId); } @Test @@ -92,7 +130,7 @@ void updateConversationTransferStatus_ConversationIsPendingAndNewStatusIsPending final UUID inboundConversationId = UUID.randomUUID(); final ConversationTransferStatus currentTransferStatus = INBOUND_STARTED; final ConversationTransferStatus newTransferStatus = INBOUND_REQUEST_SENT; - final ConversationRecord conversationRecord = createOrRetryConversationRecord(inboundConversationId, currentTransferStatus); + final ConversationRecord conversationRecord = createConversationRecord(inboundConversationId, currentTransferStatus); // when when(transferRepository.findConversationByInboundConversationId(inboundConversationId)) @@ -102,7 +140,7 @@ void updateConversationTransferStatus_ConversationIsPendingAndNewStatusIsPending // then verify(transferRepository).updateConversationStatus(inboundConversationId, newTransferStatus); - verify(conversationActivityService, never()).concludeConversationActivity(any()); + verify(activityService, never()).concludeConversationActivity(any()); } @Test @@ -111,7 +149,7 @@ void updateConversationTransferStatus_ConversationIsPendingAndNewStatusIsTermina final UUID inboundConversationId = UUID.randomUUID(); final ConversationTransferStatus currentTransferStatus = INBOUND_REQUEST_SENT; final ConversationTransferStatus newTransferStatus = INBOUND_COMPLETE; - final ConversationRecord conversationRecord = createOrRetryConversationRecord(inboundConversationId, currentTransferStatus); + final ConversationRecord conversationRecord = createConversationRecord(inboundConversationId, currentTransferStatus); // when when(transferRepository.findConversationByInboundConversationId(inboundConversationId)) @@ -121,7 +159,7 @@ void updateConversationTransferStatus_ConversationIsPendingAndNewStatusIsTermina // then verify(transferRepository).updateConversationStatus(inboundConversationId, newTransferStatus); - verify(conversationActivityService).concludeConversationActivity(inboundConversationId); + verify(activityService).concludeConversationActivity(inboundConversationId); } @Test @@ -130,7 +168,7 @@ void updateConversationTransferStatusWithFailure_ConversationIsTerminated_DoNotU final UUID inboundConversationId = UUID.randomUUID(); final ConversationTransferStatus currentTransferStatus = INBOUND_COMPLETE; final String failureCode = "06"; - final ConversationRecord conversationRecord = createOrRetryConversationRecord(inboundConversationId, currentTransferStatus); + final ConversationRecord conversationRecord = createConversationRecord(inboundConversationId, currentTransferStatus); // when when(transferRepository.findConversationByInboundConversationId(inboundConversationId)) @@ -140,7 +178,7 @@ void updateConversationTransferStatusWithFailure_ConversationIsTerminated_DoNotU // then verify(transferRepository, never()).updateConversationStatusWithFailure(any(), any()); - verify(conversationActivityService).concludeConversationActivity(inboundConversationId); + verify(activityService).concludeConversationActivity(inboundConversationId); } @Test @@ -149,7 +187,7 @@ void updateConversationTransferStatusWithFailure_ConversationIsPending_UpdateTra final UUID inboundConversationId = UUID.randomUUID(); final ConversationTransferStatus currentTransferStatus = INBOUND_REQUEST_SENT; final String failureCode = "06"; - final ConversationRecord conversationRecord = createOrRetryConversationRecord(inboundConversationId, currentTransferStatus); + final ConversationRecord conversationRecord = createConversationRecord(inboundConversationId, currentTransferStatus); // when when(transferRepository.findConversationByInboundConversationId(inboundConversationId)) @@ -159,12 +197,12 @@ void updateConversationTransferStatusWithFailure_ConversationIsPending_UpdateTra // then verify(transferRepository).updateConversationStatusWithFailure(inboundConversationId, failureCode); - verify(conversationActivityService).concludeConversationActivity(inboundConversationId); + verify(activityService).concludeConversationActivity(inboundConversationId); } // Helper Methods - private ConversationRecord createOrRetryConversationRecord( + private ConversationRecord createConversationRecord( UUID inboundConversationId, ConversationTransferStatus currentTransferStatus ) {