88import static org .junit .jupiter .api .Assertions .assertNotNull ;
99import static org .mockito .ArgumentMatchers .any ;
1010import static org .mockito .Mockito .doThrow ;
11+ import static org .mockito .Mockito .clearInvocations ;
1112import static org .mockito .Mockito .timeout ;
1213import static org .mockito .Mockito .times ;
1314import static org .mockito .Mockito .verify ;
4142import org .springframework .http .HttpHeaders ;
4243import org .springframework .jms .core .JmsTemplate ;
4344import org .springframework .test .annotation .DirtiesContext ;
45+ import org .springframework .test .context .TestPropertySource ;
4446import org .springframework .test .context .bean .override .mockito .MockitoSpyBean ;
4547import org .springframework .test .context .junit .jupiter .SpringExtension ;
4648import org .springframework .web .reactive .function .client .WebClientRequestException ;
5254import uk .nhs .adaptors .common .enums .MigrationStatus ;
5355import uk .nhs .adaptors .common .model .TransferRequestMessage ;
5456import uk .nhs .adaptors .pss .translator .Gp2gpTranslatorApplication ;
57+ import uk .nhs .adaptors .pss .translator .config .MhsQueueProperties ;
5558import uk .nhs .adaptors .pss .translator .config .PssQueueProperties ;
5659import uk .nhs .adaptors .pss .translator .exception .MhsServerErrorException ;
5760import uk .nhs .adaptors .pss .translator .service .MhsClientService ;
6770@ ExtendWith ({SpringExtension .class , MockitoExtension .class })
6871@ DirtiesContext (classMode = DirtiesContext .ClassMode .BEFORE_EACH_TEST_METHOD )
6972@ AutoConfigureMockMvc
73+ @ TestPropertySource (properties = {
74+ "amqp.pss.queueName=pssQueueServiceFailureIT" ,
75+ "amqp.mhs.queueName=mhsQueueServiceFailureIT"
76+ })
7077public class ServiceFailureIT extends BaseEhrHandler {
7178
72- public static final int TEN_SECONDS = 10000 ;
7379 private static final String LOSING_ASID = "LOSING_ASID" ;
7480 private static final String WINNING_ASID = "WINNING_ASID" ;
7581 private static final String STUB_BODY = "test Body" ;
7682 private static final int THIRTY_SECONDS = 30000 ;
7783 private static final long TWO_MINUTES_LONG = 2L ;
78- private static final int FIVE_WANTED_NUMBER_OF_INVOCATIONS = 5 ;
7984 public static final String JSON_LARGE_MESSAGE_SCENARIO_3_UK_06_JSON = "/json/LargeMessage/Scenario_3/uk06.json" ;
8085 public static final String JSON_LARGE_MESSAGE_SCENARIO_3_COPC_JSON = "/json/LargeMessage/Scenario_3/copc.json" ;
8186 public static final String JSON_LARGE_MESSAGE_EXPECTED_BUNDLE_SCENARIO_3_JSON = "/json/LargeMessage/expectedBundleScenario3.json" ;
8287 public static final int RECEIVE_TIMEOUT_LIMIT = 50 ;
83- public static final int TWENTY = 20 ;
8488 private String conversationId ;
8589
8690 @ Autowired
8791 private ObjectMapper objectMapper ;
8892 @ Autowired
8993 private PssQueueProperties pssQueueProperties ;
94+ @ Autowired
95+ private MhsQueueProperties mhsQueueProperties ;
9096
9197 @ Autowired
9298 @ Qualifier ("jmsTemplateMhsDLQ" )
@@ -134,7 +140,8 @@ public void When_SendingInitialRequest_WithMhsOutboundServerError_Expect_Migrati
134140
135141 sendRequestToPssQueue (conversationId , patientNhsNumber );
136142
137- await ().until (() -> hasMigrationStatus (EHR_EXTRACT_REQUEST_ERROR , conversationId ));
143+ await ().atMost (Duration .ofMinutes (TWO_MINUTES_LONG ))
144+ .until (() -> hasMigrationStatus (EHR_EXTRACT_REQUEST_ERROR , conversationId ));
138145
139146 verify (mhsClientService , timeout (THIRTY_SECONDS ).times (pssQueueProperties .getMaxRedeliveries () + 1 )
140147 ).send (any ());
@@ -146,15 +153,16 @@ public void When_SendingInitialRequest_WithMhsOutboundServerError_Expect_Migrati
146153 @ Test
147154 public void When_ReceivingEhrExtract_WithMhsOutboundServerError_Expect_MigrationHasProcessingError () {
148155 doThrow (MhsServerErrorException .class )
149- .doNothing ()
150156 .when (sendContinueRequestHandler )
151157 .prepareAndSendRequest (any ());
152158
153159 sendInboundMessageToQueue (JSON_LARGE_MESSAGE_SCENARIO_3_UK_06_JSON );
154160
155- await ().until (() -> hasMigrationStatus (EHR_GENERAL_PROCESSING_ERROR , getConversationId ()));
161+ await ().atMost (Duration .ofMinutes (TWO_MINUTES_LONG ))
162+ .until (() -> hasMigrationStatus (EHR_GENERAL_PROCESSING_ERROR , getConversationId ()));
156163
157- verify (sendContinueRequestHandler , times (1 )).prepareAndSendRequest (any ());
164+ verify (sendContinueRequestHandler , timeout (THIRTY_SECONDS ).atLeast (MhsQueueConsumer .RETRY_ATTEMPTS ))
165+ .prepareAndSendRequest (any ());
158166
159167 assertThat (getCurrentMigrationStatus (getConversationId ())).isEqualTo (EHR_GENERAL_PROCESSING_ERROR );
160168 }
@@ -189,6 +197,7 @@ public void When_ReceivingCOPC_WithMhsServerErrorException_Expect_MessageSentToD
189197 sendInboundMessageToQueue (JSON_LARGE_MESSAGE_SCENARIO_3_UK_06_JSON );
190198
191199 await ().until (this ::hasContinueMessageBeenReceived );
200+ clearInvocations (mhsClientService );
192201
193202 doThrow (MhsServerErrorException .class ).when (mhsClientService ).send (any ());
194203
@@ -200,7 +209,7 @@ public void When_ReceivingCOPC_WithMhsServerErrorException_Expect_MessageSentToD
200209
201210 assertNotNull (messageSentToDlq );
202211 assertEquals (copcMessageInJsonFormat , ((JmsTextMessage ) messageSentToDlq ).getText ());
203- verify (mhsClientService , times (FIVE_WANTED_NUMBER_OF_INVOCATIONS )).send (any ());
212+ verify (mhsClientService , times (( mhsQueueProperties . getMaxRedeliveries () + 1 ) * MhsQueueConsumer . RETRY_ATTEMPTS )).send (any ());
204213 }
205214
206215 @ Test
0 commit comments