Skip to content

Commit d3eb36b

Browse files
NIAD-1136 remove lenient and refactor (#1401)
1 parent 8800055 commit d3eb36b

34 files changed

+1396
-786
lines changed

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

Lines changed: 18 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import static org.assertj.core.api.Assertions.assertThat;
66
import static org.junit.jupiter.api.Assertions.assertFalse;
77
import static org.mockito.ArgumentMatchers.any;
8-
import static org.mockito.Mockito.lenient;
98
import static org.mockito.Mockito.never;
109
import static org.mockito.Mockito.verify;
11-
import static org.mockito.Mockito.when;
1210

1311
import java.io.IOException;
1412
import java.io.InputStreamReader;
@@ -32,14 +30,11 @@
3230
import org.springframework.test.context.junit4.SpringRunner;
3331
import org.springframework.util.FileCopyUtils;
3432

35-
import com.fasterxml.jackson.databind.ObjectMapper;
36-
3733
import lombok.SneakyThrows;
3834
import uk.nhs.adaptors.gp2gp.common.service.RandomIdGeneratorService;
3935
import uk.nhs.adaptors.gp2gp.common.service.XPathService;
4036
import uk.nhs.adaptors.gp2gp.common.task.TaskDispatcher;
4137
import uk.nhs.adaptors.gp2gp.ehr.model.EhrExtractStatus;
42-
import uk.nhs.adaptors.gp2gp.mhs.InboundMessage;
4338
import uk.nhs.adaptors.gp2gp.mhs.InboundMessageHandler;
4439
import uk.nhs.adaptors.gp2gp.testcontainers.ActiveMQExtension;
4540
import uk.nhs.adaptors.gp2gp.testcontainers.MongoDBExtension;
@@ -49,31 +44,8 @@
4944
@SpringBootTest
5045
@DirtiesContext
5146
public class IllogicalMessageComponentTest {
52-
private static final String INTERACTION_ID_PATH = "/RCMR_IN010000UK05";
53-
private static final String SUBJECT_PATH = INTERACTION_ID_PATH + "/ControlActEvent/subject";
54-
private static final String MESSAGE_HEADER_PATH = "/Envelope/Header/MessageHeader";
55-
private static final String REQUEST_ID_PATH = SUBJECT_PATH + "/EhrRequest/id/@root";
56-
private static final String NHS_NUMBER_PATH = SUBJECT_PATH + "/EhrRequest/recordTarget/patient/id/@extension";
57-
private static final String FROM_PARTY_ID_PATH = MESSAGE_HEADER_PATH + "/From/PartyId";
58-
private static final String TO_PARTY_ID_PATH = MESSAGE_HEADER_PATH + "/To/PartyId";
59-
private static final String FROM_ASID_PATH = INTERACTION_ID_PATH + "/communicationFunctionSnd/device/id/@extension";
60-
private static final String TO_ASID_PATH = INTERACTION_ID_PATH + "/communicationFunctionRcv/device/id/@extension";
61-
private static final String FROM_ODS_CODE_PATH = SUBJECT_PATH + "/EhrRequest/author/AgentOrgSDS/agentOrganizationSDS/id/@extension";
62-
private static final String TO_ODS_CODE_PATH = SUBJECT_PATH + "/EhrRequest/destination/AgentOrgSDS/agentOrganizationSDS/id/@extension";
63-
private static final String MESSAGE_ID_PATH = MESSAGE_HEADER_PATH + "/MessageData/MessageId";
64-
private static final String EHR_EXTRACT_REQUEST = "RCMR_IN010000UK05";
65-
private static final String CONTINUE_REQUEST = "COPC_IN000001UK01";
66-
private static final String ACKNOWLEDGMENT_REQUEST = "MCCI_IN010000UK13";
67-
private static final String ACTION_PATH = "/Envelope/Header/MessageHeader/Action";
68-
private static final String CONVERSATION_ID_PATH = "/Envelope/Header/MessageHeader/ConversationId";
69-
private static final String NON_EXISTING_CONVERSATION_ID = "d3746650-096e-414b-92a4-146ceaf74f0e";
70-
private static final String ACK_OK_CODE = "AA";
7147
private static final XPathService SERVICE = new XPathService();
7248

73-
@Mock
74-
private XPathService xPathService;
75-
@Mock
76-
private ObjectMapper objectMapper;
7749
@Mock
7850
private TaskDispatcher taskDispatcher;
7951
@InjectMocks
@@ -84,28 +56,18 @@ public class IllogicalMessageComponentTest {
8456
private RandomIdGeneratorService randomIdGeneratorService;
8557
@Mock
8658
private Message message;
87-
@Mock
88-
private InboundMessage inboundMessage;
8959

90-
@Value("classpath:illogicalmessage/RCMR_IN010000UK05_ebxml.txt")
91-
private Resource requestResponseEbxml;
92-
@Value("classpath:illogicalmessage/RCMR_IN010000UK05_payload.txt")
93-
private Resource requestResponsePayload;
9460
@Value("classpath:illogicalmessage/COPC_IN000001UK01_ebxml.txt")
9561
private Resource continueResponseEbxml;
96-
@Value("classpath:illogicalmessage/COPC_IN000001UK01_payload.txt")
97-
private Resource continueResponsePayload;
9862
@Value("classpath:illogicalmessage/MCCI_IN010000UK13_ebxml.txt")
9963
private Resource acknowledgementResponseEbxml;
100-
@Value("classpath:illogicalmessage/MCCI_IN010000UK13_payload.txt")
101-
private Resource acknowledgementResponsePayload;
64+
10265

10366
@Test
10467
public void When_ContinueReceivedToNonExistingEhrExtractStatus_Expect_ErrorThrown() {
105-
String continuePayload = asString(continueResponsePayload);
10668
String continueEbxml = asString(continueResponseEbxml);
10769

108-
mockIncomingMessage(continueEbxml, continuePayload, CONTINUE_REQUEST, NON_EXISTING_CONVERSATION_ID);
70+
mockIncomingMessage(continueEbxml);
10971

11072
assertFalse(inboundMessageHandler.handle(message));
11173

@@ -114,10 +76,9 @@ public void When_ContinueReceivedToNonExistingEhrExtractStatus_Expect_ErrorThrow
11476

11577
@Test
11678
public void When_AcknowledgementReceivedToNonExistingEhrExtractStatus_Expect_ErrorThrown() {
117-
String acknowledgementPayload = asString(acknowledgementResponsePayload);
11879
String acknowledgementEbxml = asString(acknowledgementResponseEbxml);
11980

120-
mockAcknowledgementMessage(acknowledgementEbxml, acknowledgementPayload, ACKNOWLEDGMENT_REQUEST, NON_EXISTING_CONVERSATION_ID);
81+
mockAcknowledgementMessage(acknowledgementEbxml);
12182

12283
assertFalse(inboundMessageHandler.handle(message));
12384

@@ -129,10 +90,9 @@ public void When_ContinueReceivedOutOfOrderExtractCoreNotSent_Expect_ErrorThrown
12990
var ehrExtractStatus = EhrExtractStatusTestUtils.prepareEhrExtractStatus(randomIdGeneratorService.createNewId());
13091
ehrExtractStatusRepository.save(ehrExtractStatus);
13192

132-
String continuePayload = asString(continueResponsePayload);
13393
String continueEbxml = asString(continueResponseEbxml);
13494

135-
mockIncomingMessage(continueEbxml, continuePayload, CONTINUE_REQUEST, ehrExtractStatus.getConversationId());
95+
mockIncomingMessage(continueEbxml);
13696

13797
assertFalse(inboundMessageHandler.handle(message));
13898

@@ -144,11 +104,9 @@ public void When_AcknowledgementReceivedOutOfOrderAcknowledgmentNotSent_Expect_E
144104
var ehrExtractStatus = EhrExtractStatusTestUtils.prepareEhrExtractStatus(randomIdGeneratorService.createNewId());
145105
ehrExtractStatusRepository.save(ehrExtractStatus);
146106

147-
String acknowledgementPayload = asString(acknowledgementResponsePayload);
148107
String acknowledgementEbxml = asString(acknowledgementResponseEbxml);
149108

150-
mockAcknowledgementMessage(acknowledgementEbxml, acknowledgementPayload, ACKNOWLEDGMENT_REQUEST,
151-
ehrExtractStatus.getConversationId());
109+
mockAcknowledgementMessage(acknowledgementEbxml);
152110

153111
assertFalse(inboundMessageHandler.handle(message));
154112

@@ -158,17 +116,16 @@ public void When_AcknowledgementReceivedOutOfOrderAcknowledgmentNotSent_Expect_E
158116
@Test
159117
public void When_DuplicateEhrRequestReceived_Expect_SkippedNoDatabaseUpdated() {
160118
var ehrExtractStatus = EhrExtractStatusTestUtils.prepareEhrExtractStatus();
161-
ehrExtractStatusRepository.save(ehrExtractStatus);
162-
163-
String requestPayload = asString(requestResponsePayload);
164-
String requestEbxml = asString(requestResponseEbxml);
165-
166-
mockEhrRequest(requestEbxml, requestPayload, EHR_EXTRACT_REQUEST, ehrExtractStatus.getConversationId());
167119

120+
ehrExtractStatusRepository.save(ehrExtractStatus);
168121
inboundMessageHandler.handle(message);
169-
var firstEhrStatus = ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
122+
123+
var firstEhrStatus =
124+
ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
170125
inboundMessageHandler.handle(message);
171-
var secondEhrStatus = ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
126+
127+
var secondEhrStatus =
128+
ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
172129

173130
assertThat(firstEhrStatus.getUpdatedAt()).isEqualTo(secondEhrStatus.getUpdatedAt());
174131
verify(taskDispatcher, never()).createTask(any());
@@ -181,10 +138,9 @@ public void When_DuplicateContinueReceived_Expect_SkippedNoDatabaseUpdated() {
181138
ehrExtractStatus.setEhrContinue(EhrExtractStatus.EhrContinue.builder().build());
182139
ehrExtractStatusRepository.save(ehrExtractStatus);
183140

184-
String continuePayload = asString(continueResponsePayload);
185141
String continueEbxml = asString(continueResponseEbxml);
186142

187-
mockIncomingMessage(continueEbxml, continuePayload, CONTINUE_REQUEST, ehrExtractStatus.getConversationId());
143+
mockIncomingMessage(continueEbxml);
188144

189145
inboundMessageHandler.handle(message);
190146
var firstEhrStatus = ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
@@ -202,11 +158,9 @@ public void When_DuplicateAcknowledgementSentTwice_Expect_SkippedNoDatabaseUpdat
202158
ehrExtractStatus.setEhrReceivedAcknowledgement(EhrExtractStatus.EhrReceivedAcknowledgement.builder().build());
203159
ehrExtractStatusRepository.save(ehrExtractStatus);
204160

205-
String acknowledgementPayload = asString(acknowledgementResponsePayload);
206161
String acknowledgementEbxml = asString(acknowledgementResponseEbxml);
207162

208-
mockAcknowledgementMessage(acknowledgementEbxml, acknowledgementPayload, ACKNOWLEDGMENT_REQUEST,
209-
ehrExtractStatus.getConversationId());
163+
mockAcknowledgementMessage(acknowledgementEbxml);
210164

211165
inboundMessageHandler.handle(message);
212166
var firstEhrStatus = ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
@@ -220,40 +174,11 @@ public void When_DuplicateAcknowledgementSentTwice_Expect_SkippedNoDatabaseUpdat
220174
@Test
221175
@SneakyThrows
222176
public void When_UnsupportedMessageSent_Expect_ErrorThrown() {
223-
String incomingMessage = null;
224-
lenient().when(objectMapper.readValue(incomingMessage, InboundMessage.class)).thenReturn(inboundMessage);
225-
226177
assertFalse(inboundMessageHandler.handle(message));
227178

228179
verify(taskDispatcher, never()).createTask(any());
229180
}
230181

231-
@SneakyThrows
232-
private void mockEhrRequest(String ebxml, String payload, String interactionId, String conversationId) {
233-
String incomingMessage = null;
234-
lenient().when(objectMapper.readValue(incomingMessage, InboundMessage.class)).thenReturn(inboundMessage);
235-
lenient().when(inboundMessage.getEbXML()).thenReturn(ebxml);
236-
lenient().when(inboundMessage.getPayload()).thenReturn(payload);
237-
238-
var ebxmlDocument = SERVICE.parseDocumentFromXml(ebxml);
239-
lenient().when(xPathService.parseDocumentFromXml(inboundMessage.getEbXML())).thenReturn(ebxmlDocument);
240-
var payloadDocument = SERVICE.parseDocumentFromXml(payload);
241-
lenient().when(xPathService.parseDocumentFromXml(inboundMessage.getPayload())).thenReturn(payloadDocument);
242-
243-
lenient().when(xPathService.getNodeValue(ebxmlDocument, ACTION_PATH)).thenReturn(interactionId);
244-
lenient().when(xPathService.getNodeValue(ebxmlDocument, CONVERSATION_ID_PATH)).thenReturn(conversationId);
245-
246-
lenient().when(xPathService.getNodeValue(payloadDocument, REQUEST_ID_PATH)).thenReturn("123");
247-
lenient().when(xPathService.getNodeValue(payloadDocument, NHS_NUMBER_PATH)).thenReturn("123");
248-
lenient().when(xPathService.getNodeValue(ebxmlDocument, FROM_PARTY_ID_PATH)).thenReturn("123");
249-
lenient().when(xPathService.getNodeValue(ebxmlDocument, TO_PARTY_ID_PATH)).thenReturn("123");
250-
lenient().when(xPathService.getNodeValue(payloadDocument, FROM_ASID_PATH)).thenReturn("123");
251-
lenient().when(xPathService.getNodeValue(payloadDocument, TO_ASID_PATH)).thenReturn("123");
252-
lenient().when(xPathService.getNodeValue(payloadDocument, FROM_ODS_CODE_PATH)).thenReturn("123");
253-
lenient().when(xPathService.getNodeValue(payloadDocument, TO_ODS_CODE_PATH)).thenReturn("123");
254-
lenient().when(xPathService.getNodeValue(ebxmlDocument, MESSAGE_ID_PATH)).thenReturn("123");
255-
}
256-
257182
private static String asString(Resource resource) {
258183
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
259184
return FileCopyUtils.copyToString(reader);
@@ -263,21 +188,12 @@ private static String asString(Resource resource) {
263188
}
264189

265190
@SneakyThrows
266-
private void mockIncomingMessage(String ebxml, String payload, String interactionId, String conversationId) {
267-
String incomingMessage = null;
268-
lenient().when(objectMapper.readValue(incomingMessage, InboundMessage.class)).thenReturn(inboundMessage);
269-
lenient().when(inboundMessage.getEbXML()).thenReturn(ebxml);
270-
lenient().when(inboundMessage.getPayload()).thenReturn(payload);
271-
272-
var ebxmlDocument = SERVICE.parseDocumentFromXml(ebxml);
273-
lenient().when(xPathService.parseDocumentFromXml(inboundMessage.getEbXML())).thenReturn(ebxmlDocument);
274-
lenient().when(xPathService.getNodeValue(ebxmlDocument, ACTION_PATH)).thenReturn(interactionId);
275-
lenient().when(xPathService.getNodeValue(ebxmlDocument, CONVERSATION_ID_PATH)).thenReturn(conversationId);
191+
private void mockIncomingMessage(String ebxml) {
192+
SERVICE.parseDocumentFromXml(ebxml);
276193
}
277194

278195
@SneakyThrows
279-
private void mockAcknowledgementMessage(String ebxml, String payload, String interactionId, String conversationId) {
280-
when(xPathService.getNodeValue(any(), any())).thenReturn(ACK_OK_CODE);
281-
mockIncomingMessage(ebxml, payload, interactionId, conversationId);
196+
private void mockAcknowledgementMessage(String ebxml) {
197+
mockIncomingMessage(ebxml);
282198
}
283199
}

0 commit comments

Comments
 (0)