Skip to content

Commit df99f45

Browse files
NIAD-1136 remove lenient and refactor
1 parent 9a08319 commit df99f45

34 files changed

+1377
-658
lines changed

service/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ dependencies {
8585
testImplementation 'com.squareup.okhttp3:okhttp:5.3.0'
8686
testImplementation 'com.squareup.okhttp3:mockwebserver3:5.3.0'
8787
testImplementation 'com.adobe.testing:s3mock-testcontainers:4.7.0'
88+
testImplementation 'org.mockito:mockito-inline:4.8.0'
8889

8990
spotbugs 'com.github.spotbugs:spotbugs:4.9.8'
9091
spotbugs 'com.github.spotbugs:spotbugs-annotations:4.9.8'

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

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
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;
1110
import static org.mockito.Mockito.when;
@@ -158,17 +157,16 @@ public void When_AcknowledgementReceivedOutOfOrderAcknowledgmentNotSent_Expect_E
158157
@Test
159158
public void When_DuplicateEhrRequestReceived_Expect_SkippedNoDatabaseUpdated() {
160159
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());
167160

161+
ehrExtractStatusRepository.save(ehrExtractStatus);
168162
inboundMessageHandler.handle(message);
169-
var firstEhrStatus = ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
163+
164+
var firstEhrStatus =
165+
ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
170166
inboundMessageHandler.handle(message);
171-
var secondEhrStatus = ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
167+
168+
var secondEhrStatus =
169+
ehrExtractStatusRepository.findByConversationId(ehrExtractStatus.getConversationId()).get();
172170

173171
assertThat(firstEhrStatus.getUpdatedAt()).isEqualTo(secondEhrStatus.getUpdatedAt());
174172
verify(taskDispatcher, never()).createTask(any());
@@ -220,9 +218,6 @@ public void When_DuplicateAcknowledgementSentTwice_Expect_SkippedNoDatabaseUpdat
220218
@Test
221219
@SneakyThrows
222220
public void When_UnsupportedMessageSent_Expect_ErrorThrown() {
223-
String incomingMessage = null;
224-
lenient().when(objectMapper.readValue(incomingMessage, InboundMessage.class)).thenReturn(inboundMessage);
225-
226221
assertFalse(inboundMessageHandler.handle(message));
227222

228223
verify(taskDispatcher, never()).createTask(any());
@@ -231,27 +226,27 @@ public void When_UnsupportedMessageSent_Expect_ErrorThrown() {
231226
@SneakyThrows
232227
private void mockEhrRequest(String ebxml, String payload, String interactionId, String conversationId) {
233228
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);
229+
when(objectMapper.readValue(incomingMessage, InboundMessage.class)).thenReturn(inboundMessage);
230+
when(inboundMessage.getEbXML()).thenReturn(ebxml);
231+
when(inboundMessage.getPayload()).thenReturn(payload);
237232

238233
var ebxmlDocument = SERVICE.parseDocumentFromXml(ebxml);
239-
lenient().when(xPathService.parseDocumentFromXml(inboundMessage.getEbXML())).thenReturn(ebxmlDocument);
234+
when(xPathService.parseDocumentFromXml(inboundMessage.getEbXML())).thenReturn(ebxmlDocument);
240235
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");
236+
when(xPathService.parseDocumentFromXml(inboundMessage.getPayload())).thenReturn(payloadDocument);
237+
238+
when(xPathService.getNodeValue(ebxmlDocument, ACTION_PATH)).thenReturn(interactionId);
239+
when(xPathService.getNodeValue(ebxmlDocument, CONVERSATION_ID_PATH)).thenReturn(conversationId);
240+
241+
when(xPathService.getNodeValue(payloadDocument, REQUEST_ID_PATH)).thenReturn("123");
242+
when(xPathService.getNodeValue(payloadDocument, NHS_NUMBER_PATH)).thenReturn("123");
243+
when(xPathService.getNodeValue(ebxmlDocument, FROM_PARTY_ID_PATH)).thenReturn("123");
244+
when(xPathService.getNodeValue(ebxmlDocument, TO_PARTY_ID_PATH)).thenReturn("123");
245+
when(xPathService.getNodeValue(payloadDocument, FROM_ASID_PATH)).thenReturn("123");
246+
when(xPathService.getNodeValue(payloadDocument, TO_ASID_PATH)).thenReturn("123");
247+
when(xPathService.getNodeValue(payloadDocument, FROM_ODS_CODE_PATH)).thenReturn("123");
248+
when(xPathService.getNodeValue(payloadDocument, TO_ODS_CODE_PATH)).thenReturn("123");
249+
when(xPathService.getNodeValue(ebxmlDocument, MESSAGE_ID_PATH)).thenReturn("123");
255250
}
256251

257252
private static String asString(Resource resource) {
@@ -264,20 +259,11 @@ private static String asString(Resource resource) {
264259

265260
@SneakyThrows
266261
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);
262+
SERVICE.parseDocumentFromXml(ebxml);
276263
}
277264

278265
@SneakyThrows
279266
private void mockAcknowledgementMessage(String ebxml, String payload, String interactionId, String conversationId) {
280-
when(xPathService.getNodeValue(any(), any())).thenReturn(ACK_OK_CODE);
281267
mockIncomingMessage(ebxml, payload, interactionId, conversationId);
282268
}
283269
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
55
import static org.mockito.ArgumentMatchers.any;
66
import static org.mockito.Mockito.doThrow;
7-
import static org.mockito.Mockito.lenient;
87
import static org.mockito.Mockito.verify;
98
import static org.mockito.Mockito.when;
109

@@ -83,7 +82,7 @@ public class SendAcknowledgementComponentTest {
8382
@BeforeEach
8483
public void setUp() {
8584
when(randomIdGeneratorService.createNewId()).thenReturn(GENERATED_RANDOM_ID);
86-
lenient().when(timestampService.now()).thenReturn(Instant.parse(DATE));
85+
// when(timestampService.now()).thenReturn(Instant.parse(DATE));
8786

8887
ehrExtractStatus = EhrExtractStatusTestUtils.prepareEhrExtractStatus();
8988
ehrExtractStatusRepository.save(ehrExtractStatus);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.mockito.ArgumentMatchers.any;
55
import static org.mockito.Mockito.doReturn;
66
import static org.mockito.Mockito.doThrow;
7-
import static org.mockito.Mockito.lenient;
87
import static org.mockito.Mockito.never;
98
import static org.mockito.Mockito.verify;
109
import static org.mockito.Mockito.verifyNoInteractions;
@@ -73,7 +72,7 @@ public class TaskHandlingTest {
7372

7473
@BeforeEach
7574
public void setup() {
76-
lenient().doReturn(taskExecutor).when(taskExecutorFactory).getTaskExecutor(any());
75+
doReturn(taskExecutor).when(taskExecutorFactory).getTaskExecutor(any());
7776
}
7877

7978
@Test

service/src/test/java/uk/nhs/adaptors/gp2gp/common/task/TaskErrorHandlerTest.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414

1515
import java.util.concurrent.TimeoutException;
1616

17-
import org.junit.jupiter.api.BeforeEach;
1817
import org.junit.jupiter.api.Test;
1918
import org.junit.jupiter.api.extension.ExtendWith;
2019
import org.mockito.InjectMocks;
2120
import org.mockito.Mock;
2221
import org.mockito.junit.jupiter.MockitoExtension;
23-
import org.mockito.junit.jupiter.MockitoSettings;
24-
import org.mockito.quality.Strictness;
2522

2623
import lombok.SneakyThrows;
2724
import uk.nhs.adaptors.gp2gp.common.exception.FhirValidationException;
@@ -50,13 +47,9 @@ class TaskErrorHandlerTest {
5047
@InjectMocks
5148
private TaskErrorHandler taskErrorHandler;
5249

53-
@BeforeEach
54-
void setup() {
55-
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
56-
}
57-
5850
@Test
5951
void When_HandleProcessingError_WithEhrRequestException_Expect_ProcessToBeFailedWithCorrectCode() {
52+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
6053
taskErrorHandler.handleProcessingError(new EhrRequestException(TEST_EXCEPTION_MESSAGE), taskDefinition);
6154

6255
verify(processFailureHandlingService).failProcess(
@@ -68,6 +61,7 @@ void When_HandleProcessingError_WithEhrRequestException_Expect_ProcessToBeFailed
6861

6962
@Test
7063
void When_HandleProcessingError_WithEhrValidationException_Expect_ProcessToBeFailedWithCorrectCode() {
64+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
7165
taskErrorHandler.handleProcessingError(new EhrValidationException(TEST_EXCEPTION_MESSAGE), taskDefinition);
7266

7367
verify(processFailureHandlingService).failProcess(
@@ -79,6 +73,7 @@ void When_HandleProcessingError_WithEhrValidationException_Expect_ProcessToBeFai
7973

8074
@Test
8175
void When_HandleProcessingError_WithEhrRequestException_Expect_ReturnValueOfFailService() {
76+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
8277
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
8378
.thenReturn(true, false);
8479

@@ -88,6 +83,7 @@ void When_HandleProcessingError_WithEhrRequestException_Expect_ReturnValueOfFail
8883

8984
@Test
9085
void When_HandleProcessingError_With_EhrExtractException_Expect_ProcessToBeFailedWithCorrectCode() {
86+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
9187
taskErrorHandler.handleProcessingError(new EhrExtractException("Test Exception"), taskDefinition);
9288

9389
verify(processFailureHandlingService).failProcess(
@@ -99,6 +95,7 @@ void When_HandleProcessingError_With_EhrExtractException_Expect_ProcessToBeFaile
9995

10096
@Test
10197
void When_HandleProcessingError_WithEhrExtractException_Expect_ReturnValueOfFailService() {
98+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
10299
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
103100
.thenReturn(true, false);
104101

@@ -108,6 +105,7 @@ void When_HandleProcessingError_WithEhrExtractException_Expect_ReturnValueOfFail
108105

109106
@Test
110107
void When_HandleProcessingError_WithEhrMapperException_Expect_ProcessToBeFailedWithCorrectCode() {
108+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
111109
taskErrorHandler.handleProcessingError(new EhrMapperException(TEST_EXCEPTION_MESSAGE), taskDefinition);
112110

113111
verify(processFailureHandlingService).failProcess(
@@ -119,6 +117,7 @@ void When_HandleProcessingError_WithEhrMapperException_Expect_ProcessToBeFailedW
119117

120118
@Test
121119
void When_HandleProcessingError_WithEhrMapperException_Expect_ReturnValueOfFailService() {
120+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
122121
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
123122
.thenReturn(true, false);
124123

@@ -128,6 +127,7 @@ void When_HandleProcessingError_WithEhrMapperException_Expect_ReturnValueOfFailS
128127

129128
@Test
130129
void When_HandleProcessingError_WithFhirValidationException_Expect_ProcessToBeFailedWithCorrectCode() {
130+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
131131
taskErrorHandler.handleProcessingError(new FhirValidationException(TEST_EXCEPTION_MESSAGE), taskDefinition);
132132

133133
verify(processFailureHandlingService).failProcess(
@@ -139,6 +139,7 @@ void When_HandleProcessingError_WithFhirValidationException_Expect_ProcessToBeFa
139139

140140
@Test
141141
void When_HandleProcessingError_WithFhirValidationException_Expect_ReturnValueOfFailService() {
142+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
142143
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
143144
.thenReturn(true, false);
144145

@@ -148,6 +149,7 @@ void When_HandleProcessingError_WithFhirValidationException_Expect_ReturnValueOf
148149

149150
@Test
150151
void When_HandleProcessingError_WithOtherException_Expect_ProcessToBeFailedWithCorrectCode() {
152+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
151153
taskErrorHandler.handleProcessingError(new Exception(), taskDefinition);
152154

153155
verify(processFailureHandlingService).failProcess(
@@ -159,6 +161,7 @@ void When_HandleProcessingError_WithOtherException_Expect_ProcessToBeFailedWithC
159161

160162
@Test
161163
void When_HandleProcessingError_WithOtherException_Expect_ReturnValueOfFailService() {
164+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
162165
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
163166
.thenReturn(true, false);
164167

@@ -168,6 +171,7 @@ void When_HandleProcessingError_WithOtherException_Expect_ReturnValueOfFailServi
168171

169172
@Test
170173
void When_HandleProcessingError_WithGpConnectException_Expect_ProcessToBeFailedWithCorrectCode() {
174+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
171175
taskErrorHandler.handleProcessingError(new GpConnectException(TEST_EXCEPTION_MESSAGE), taskDefinition);
172176

173177
verify(processFailureHandlingService).failProcess(
@@ -179,6 +183,7 @@ void When_HandleProcessingError_WithGpConnectException_Expect_ProcessToBeFailedW
179183

180184
@Test
181185
void When_HandleProcessingError_WithGpConnectException_Expect_ReturnValueOfFailService() {
186+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
182187
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
183188
.thenReturn(true, false);
184189

@@ -188,6 +193,7 @@ void When_HandleProcessingError_WithGpConnectException_Expect_ReturnValueOfFailS
188193

189194
@Test
190195
void When_HandleProcessingError_WithGpConnectInvalidException_Expect_ProcessToBeFailedWithCorrectCode() {
196+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
191197
taskErrorHandler.handleProcessingError(new GpConnectInvalidException(TEST_EXCEPTION_MESSAGE), taskDefinition);
192198

193199
verify(processFailureHandlingService).failProcess(
@@ -199,6 +205,7 @@ void When_HandleProcessingError_WithGpConnectInvalidException_Expect_ProcessToBe
199205

200206
@Test
201207
void When_HandleProcessingError_WithGpConnectInvalidException_Expect_ReturnValueOfFailService() {
208+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
202209
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
203210
.thenReturn(true, false);
204211

@@ -208,6 +215,7 @@ void When_HandleProcessingError_WithGpConnectInvalidException_Expect_ReturnValue
208215

209216
@Test
210217
void When_HandleProcessingError_WithGpConnectGpConnectNotFoundException_Expect_ProcessToBeFailedWithCorrectCode() {
218+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
211219
taskErrorHandler.handleProcessingError(new GpConnectNotFoundException(TEST_EXCEPTION_MESSAGE), taskDefinition);
212220

213221
verify(processFailureHandlingService).failProcess(
@@ -219,6 +227,7 @@ void When_HandleProcessingError_WithGpConnectGpConnectNotFoundException_Expect_P
219227

220228
@Test
221229
void When_HandleProcessingError_WithGpConnectNotFoundException_Expect_ReturnValueOfFailService() {
230+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
222231
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
223232
.thenReturn(true, false);
224233

@@ -228,6 +237,7 @@ void When_HandleProcessingError_WithGpConnectNotFoundException_Expect_ReturnValu
228237

229238
@Test
230239
void When_HandleProcessingError_WithMaximumExternalAttachmentsException_Expect_ProcessToBeFailedWithCorrectCode() {
240+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
231241
taskErrorHandler.handleProcessingError(new MaximumExternalAttachmentsException(TEST_EXCEPTION_MESSAGE), taskDefinition);
232242

233243
verify(processFailureHandlingService).failProcess(
@@ -239,6 +249,7 @@ void When_HandleProcessingError_WithMaximumExternalAttachmentsException_Expect_P
239249

240250
@Test
241251
void When_HandleProcessingError_WithMaximumExternalAttachmentsException_Expect_ReturnValueOfFailService() {
252+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
242253
when(processFailureHandlingService.failProcess(any(), any(), any(), any()))
243254
.thenReturn(true, false);
244255

@@ -251,7 +262,6 @@ void When_HandleProcessingError_WithMaximumExternalAttachmentsException_Expect_R
251262
}
252263

253264
@Test
254-
@MockitoSettings(strictness = Strictness.LENIENT)
255265
void When_HandleGeneralProcessingError_WithNullParameter_Expect_ProcessIsNotFailed() {
256266
taskErrorHandler.handleProcessingError(new RuntimeException(), null);
257267

@@ -260,6 +270,7 @@ void When_HandleGeneralProcessingError_WithNullParameter_Expect_ProcessIsNotFail
260270
@Test
261271
@SneakyThrows
262272
void When_FailProcessThrowsException_Expect_ExceptionToBeRethrown() {
273+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
263274
var failureHandlingException = new RuntimeException("failure handler exception");
264275
doThrow(failureHandlingException).when(processFailureHandlingService).failProcess(
265276
any(), any(), any(), any());
@@ -272,6 +283,7 @@ void When_FailProcessThrowsException_Expect_ExceptionToBeRethrown() {
272283
@Test
273284
@SneakyThrows
274285
void When_HandleProcessingError_WithGpcServerErrorExceptionAsRootCause_Expect_FailedWithCorrectCode() {
286+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
275287
Throwable testException = new RetryLimitReachedException("test", new GpcServerErrorException("exception"));
276288

277289
taskErrorHandler.handleProcessingError(testException, taskDefinition);
@@ -286,6 +298,7 @@ void When_HandleProcessingError_WithGpcServerErrorExceptionAsRootCause_Expect_Fa
286298
@Test
287299
@SneakyThrows
288300
void When_HandleProcessingError_WithTimeoutExceptionAsRootCause_Expect_FailedWithCorrectCode() {
301+
when(taskDefinition.getTaskType()).thenReturn(GET_GPC_STRUCTURED);
289302
Throwable testException = new RetryLimitReachedException("test", new TimeoutException("exception"));
290303

291304
taskErrorHandler.handleProcessingError(testException, taskDefinition);

0 commit comments

Comments
 (0)