|
15 | 15 | import uk.nhs.adaptors.gp2gp.common.service.TimestampService; |
16 | 16 | import uk.nhs.adaptors.gp2gp.common.task.TaskDispatcher; |
17 | 17 | import uk.nhs.adaptors.gp2gp.ehr.model.EhrExtractStatus; |
| 18 | +import uk.nhs.adaptors.gp2gp.gpc.GetGpcStructuredTaskDefinition; |
18 | 19 |
|
19 | 20 | import java.time.Duration; |
20 | 21 | import java.time.Instant; |
| 22 | +import java.time.temporal.ChronoUnit; |
21 | 23 | import java.util.Collections; |
22 | 24 | import java.util.List; |
23 | 25 | import java.util.Optional; |
24 | 26 | import java.util.UUID; |
25 | 27 |
|
26 | 28 | import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
27 | 30 | import static org.junit.jupiter.api.Assertions.assertNull; |
28 | 31 | import static org.mockito.Mockito.doReturn; |
| 32 | +import static org.mockito.Mockito.times; |
| 33 | +import static org.mockito.Mockito.verify; |
29 | 34 |
|
30 | 35 | @ExtendWith(MockitoExtension.class) |
31 | 36 | public class EhrResendControllerTest { |
@@ -55,6 +60,46 @@ public class EhrResendControllerTest { |
55 | 60 | @InjectMocks |
56 | 61 | private EhrResendController ehrResendController; |
57 | 62 |
|
| 63 | + @Test |
| 64 | + public void When_AnEhrExtractHasFailed_Expect_GetGpcStructuredTaskScheduled() { |
| 65 | + |
| 66 | + String ehrMessageRef = generateRandomUppercaseUUID(); |
| 67 | + var ehrExtractStatus = new EhrExtractStatus(); |
| 68 | + |
| 69 | + ehrExtractStatus.setConversationId(CONVERSATION_ID); |
| 70 | + ehrExtractStatus.setEhrReceivedAcknowledgement(EhrExtractStatus.EhrReceivedAcknowledgement.builder() |
| 71 | + .conversationClosed(FIVE_DAYS_AGO) |
| 72 | + .errors(List.of( |
| 73 | + EhrExtractStatus.EhrReceivedAcknowledgement.ErrorDetails.builder() |
| 74 | + .code(INCUMBENT_NACK_CODE) |
| 75 | + .display(INCUMBENT_NACK_DISPLAY) |
| 76 | + .build())) |
| 77 | + .messageRef(ehrMessageRef) |
| 78 | + .received(FIVE_DAYS_AGO) |
| 79 | + .rootId(generateRandomUppercaseUUID()) |
| 80 | + .build()); |
| 81 | + ehrExtractStatus.setEhrRequest(EhrExtractStatus.EhrRequest.builder().nhsNumber(NHS_NUMBER).build()); |
| 82 | + ehrExtractStatus.setEhrExtractCorePending(EhrExtractStatus.EhrExtractCorePending.builder().build()); |
| 83 | + ehrExtractStatus.setEhrContinue(EhrExtractStatus.EhrContinue.builder().build()); |
| 84 | + |
| 85 | + doReturn(Optional.of(ehrExtractStatus)).when(ehrExtractStatusRepository).findByConversationId(CONVERSATION_ID); |
| 86 | + |
| 87 | + Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS); |
| 88 | + doReturn(now).when(timestampService).now(); |
| 89 | + |
| 90 | + ehrResendController.scheduleEhrExtractResend(CONVERSATION_ID); |
| 91 | + |
| 92 | + var updatedEhrExtractStatus = ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()); |
| 93 | + var taskDefinition = GetGpcStructuredTaskDefinition.getGetGpcStructuredTaskDefinition(randomIdGeneratorService, ehrExtractStatus); |
| 94 | + |
| 95 | + verify(taskDispatcher, times(1)).createTask(taskDefinition); |
| 96 | + assertEquals(now, updatedEhrExtractStatus.get().getMessageTimestamp()); |
| 97 | + assertNull(updatedEhrExtractStatus.get().getEhrExtractCorePending()); |
| 98 | + assertNull(updatedEhrExtractStatus.get().getEhrContinue()); |
| 99 | + assertNull(updatedEhrExtractStatus.get().getAckPending()); |
| 100 | + assertNull(updatedEhrExtractStatus.get().getEhrReceivedAcknowledgement()); |
| 101 | + } |
| 102 | + |
58 | 103 | @Test |
59 | 104 | public void When_AnEhrExtractHasNotFailedAndAnotherResendRequestArrives_Expect_FailedOperationOutcome() { |
60 | 105 |
|
@@ -114,6 +159,42 @@ public void When_AnEhrExtractHasFailed_Expect_RespondsWith202() { |
114 | 159 | assertNull(response.getBody()); |
115 | 160 | } |
116 | 161 |
|
| 162 | + @Test |
| 163 | + public void When_AnEhrExtractHasNotFailed_Expect_RespondsWith403() { |
| 164 | + |
| 165 | + var details = new CodeableConcept(); |
| 166 | + var codeableConceptCoding = new Coding(); |
| 167 | + codeableConceptCoding.setSystem("http://fhir.nhs.net/ValueSet/gpconnect-error-or-warning-code-1"); |
| 168 | + codeableConceptCoding.setCode("INTERNAL_SERVER_ERROR"); |
| 169 | + details.setCoding(List.of(codeableConceptCoding)); |
| 170 | + var diagnostics = "The current resend operation is still in progress. Please wait for it to complete before retrying"; |
| 171 | + |
| 172 | + var operationOutcome = createOperationOutcome(OperationOutcome.IssueType.BUSINESSRULE, |
| 173 | + OperationOutcome.IssueSeverity.ERROR, |
| 174 | + details, |
| 175 | + diagnostics); |
| 176 | + |
| 177 | + String ehrMessageRef = generateRandomUppercaseUUID(); |
| 178 | + var ehrExtractStatus = new EhrExtractStatus(); |
| 179 | + |
| 180 | + ehrExtractStatus.setConversationId(CONVERSATION_ID); |
| 181 | + ehrExtractStatus.setEhrReceivedAcknowledgement(EhrExtractStatus.EhrReceivedAcknowledgement.builder() |
| 182 | + .conversationClosed(FIVE_DAYS_AGO) |
| 183 | + .errors(List.of()) |
| 184 | + .messageRef(ehrMessageRef) |
| 185 | + .received(FIVE_DAYS_AGO) |
| 186 | + .rootId(generateRandomUppercaseUUID()) |
| 187 | + .build()); |
| 188 | + ehrExtractStatus.setEhrRequest(EhrExtractStatus.EhrRequest.builder().nhsNumber(NHS_NUMBER).build()); |
| 189 | + |
| 190 | + doReturn(Optional.of(ehrExtractStatus)).when(ehrExtractStatusRepository).findByConversationId(CONVERSATION_ID); |
| 191 | + |
| 192 | + var response = ehrResendController.scheduleEhrExtractResend(CONVERSATION_ID); |
| 193 | + |
| 194 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); |
| 195 | + assertThat(response.getBody()).usingRecursiveComparison().isEqualTo(operationOutcome); |
| 196 | + } |
| 197 | + |
117 | 198 | @Test |
118 | 199 | public void When_AnEhrExtractDoesNotExist_Expect_RespondsWith404() { |
119 | 200 |
|
|
0 commit comments