Skip to content

Commit b417275

Browse files
committed
converting all Ehr resend integration tests into unit tests
1 parent 1346972 commit b417275

File tree

2 files changed

+81
-102
lines changed

2 files changed

+81
-102
lines changed

service/src/intTest/java/uk/nhs/adaptors/gp2gp/ehr/EhrResendControllerIT.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/EhrResendControllerTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
import uk.nhs.adaptors.gp2gp.common.service.TimestampService;
1616
import uk.nhs.adaptors.gp2gp.common.task.TaskDispatcher;
1717
import uk.nhs.adaptors.gp2gp.ehr.model.EhrExtractStatus;
18+
import uk.nhs.adaptors.gp2gp.gpc.GetGpcStructuredTaskDefinition;
1819

1920
import java.time.Duration;
2021
import java.time.Instant;
22+
import java.time.temporal.ChronoUnit;
2123
import java.util.Collections;
2224
import java.util.List;
2325
import java.util.Optional;
2426
import java.util.UUID;
2527

2628
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
2730
import static org.junit.jupiter.api.Assertions.assertNull;
2831
import static org.mockito.Mockito.doReturn;
32+
import static org.mockito.Mockito.times;
33+
import static org.mockito.Mockito.verify;
2934

3035
@ExtendWith(MockitoExtension.class)
3136
public class EhrResendControllerTest {
@@ -55,6 +60,46 @@ public class EhrResendControllerTest {
5560
@InjectMocks
5661
private EhrResendController ehrResendController;
5762

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+
58103
@Test
59104
public void When_AnEhrExtractHasNotFailedAndAnotherResendRequestArrives_Expect_FailedOperationOutcome() {
60105

@@ -114,6 +159,42 @@ public void When_AnEhrExtractHasFailed_Expect_RespondsWith202() {
114159
assertNull(response.getBody());
115160
}
116161

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+
117198
@Test
118199
public void When_AnEhrExtractDoesNotExist_Expect_RespondsWith404() {
119200

0 commit comments

Comments
 (0)