77import static org .hamcrest .CoreMatchers .allOf ;
88import static org .hamcrest .CoreMatchers .is ;
99import static org .hamcrest .MatcherAssert .assertThat ;
10+ import static org .hamcrest .Matchers .containsString ;
1011import static org .junit .jupiter .api .Assertions .fail ;
1112import static org .mockito .ArgumentMatchers .any ;
1213import static org .mockito .ArgumentMatchers .eq ;
1819import static uk .gov .justice .services .test .utils .core .matchers .JsonEnvelopeMetadataMatcher .metadata ;
1920import static uk .gov .justice .services .test .utils .core .matchers .JsonEnvelopePayloadMatcher .payloadIsJson ;
2021import static uk .gov .justice .services .test .utils .core .messaging .MetadataBuilderFactory .metadataWithRandomUUID ;
22+ import static uk .gov .moj .cpp .results .event .processor .StagingEnforcementAcknowledgmentEventProcessor .FINE_ACCOUNT_NOT_PRESENT ;
2123
2224import uk .gov .justice .services .core .sender .Sender ;
2325import uk .gov .justice .services .messaging .Envelope ;
@@ -186,6 +188,7 @@ public void shouldCallUpdatecorrelationId() {
186188
187189 }
188190
191+
189192 @ Test
190193 void shouldProcessSendNcesMailForNewApplication () {
191194 // GIVEN
@@ -218,7 +221,8 @@ void shouldProcessSendNcesMailForNewApplication() {
218221 // THEN
219222 verify (progressionService ).getInactiveMigratedCasesByCaseIds (List .of (caseId1 , caseId2 , caseId3 ));
220223
221- verify (sender , times (4 )).sendAsAdmin (envelopeArgumentCaptor .capture ());
224+ // Updated to 5: (3 from Case1 + 1 from Case2 + 1 Final Notification)
225+ verify (sender , times (5 )).sendAsAdmin (envelopeArgumentCaptor .capture ());
222226
223227 List <Envelope <JsonObject >> allEnvelopes = envelopeArgumentCaptor .getAllValues ();
224228
@@ -248,6 +252,16 @@ void shouldProcessSendNcesMailForNewApplication() {
248252 ))));
249253
250254 assertThat (JsonEnvelope .envelopeFrom (allEnvelopes .get (2 ).metadata (), allEnvelopes .get (2 ).payload ()),
255+ jsonEnvelope (
256+ metadata ().withName ("result.command.send-migrated-inactive-nces-email-for-application" ),
257+ payloadIsJson (allOf (
258+ withJsonPath ("$.migratedMasterDefendantCourtEmailAndFineAccount.caseId" , is (caseId1 )),
259+ withJsonPath ("$.migratedMasterDefendantCourtEmailAndFineAccount.fineAccountNumber" , is (FINE_ACCOUNT_NOT_PRESENT )),
260+ withJsonPath ("$.migratedMasterDefendantCourtEmailAndFineAccount.defendantName" , is ("Jane Dare" )),
261+ withJsonPath ("$.migratedMasterDefendantCourtEmailAndFineAccount.defendantEmail" , is ("jane.dare@gmail.com" ))
262+ ))));
263+
264+ assertThat (JsonEnvelope .envelopeFrom (allEnvelopes .get (3 ).metadata (), allEnvelopes .get (3 ).payload ()),
251265 jsonEnvelope (
252266 metadata ().withName ("result.command.send-migrated-inactive-nces-email-for-application" ),
253267 payloadIsJson (allOf (
@@ -256,6 +270,59 @@ void shouldProcessSendNcesMailForNewApplication() {
256270 withJsonPath ("$.migratedMasterDefendantCourtEmailAndFineAccount.defendantName" , is ("Garfield Dare" ))
257271 ))));
258272
273+ assertThat (allEnvelopes .get (4 ).metadata ().name (), is ("result.command.send-nces-email-for-application" ));
274+ }
275+
276+ @ Test
277+ void shouldProcessSendNcesMailForNewApplicationWhenDefendantFineAccountNumbersMissing () {
278+ // GIVEN
279+ final String masterDefendantId = "1a9176f4-3adc-4ea1-a808-26c4632f38ab" ;
280+ final String caseId1 = "b00acc1c-eb69-4b3c-960e-76be9153125a" ;
281+ final String caseId2 = "7776f4-3adc-4ea1-a808-26c4632f38ab" ;
282+ final String caseId3 = "b10acc1c-eb69-4b3c-960e-76be9153125a" ;
283+ final String hearingCourtCentreId = "faa91bb2-19cb-384b-bcc1-06d31d12cc67" ;
284+
285+ final JsonObject notificationPayload = createObjectBuilder ()
286+ .add ("masterDefendantId" , masterDefendantId )
287+ .add ("caseIds" , createCaseIds (caseId1 , caseId2 , caseId3 ))
288+ .add ("hearingCourtCentreId" , hearingCourtCentreId )
289+ .build ();
290+
291+ final JsonObject progressionResponse = getPayload ("inactive-migrated-cases-without-defendant-fine-account-numbers.json" );
292+
293+ when (progressionService .getInactiveMigratedCasesByCaseIds (List .of (caseId1 , caseId2 , caseId3 )))
294+ .thenReturn (Optional .of (progressionResponse ));
295+
296+ final JsonObject payload = getPayload ("organisation-units.json" );
297+ when (referenceDataService .getOrganisationUnit (eq (hearingCourtCentreId ), any ())).thenReturn (payload );
298+
299+ final JsonEnvelope event = JsonEnvelope .envelopeFrom (
300+ metadataWithRandomUUID ("public.hearing.nces-email-notification-for-application" ), notificationPayload );
301+
302+ // WHEN
303+ stagingEnforcementAcknowledgmentEventProcessor .processSendNcesMailForNewApplication (event );
304+
305+ // THEN
306+ verify (progressionService ).getInactiveMigratedCasesByCaseIds (List .of (caseId1 , caseId2 , caseId3 ));
307+
308+ verify (sender , times (4 )).sendAsAdmin (envelopeArgumentCaptor .capture ());
309+
310+ List <Envelope <JsonObject >> allEnvelopes = envelopeArgumentCaptor .getAllValues ();
311+
312+ assertThat (allEnvelopes .get (0 ).payload ().getJsonObject ("migratedMasterDefendantCourtEmailAndFineAccount" ).getString ("fineAccountNumber" ), is (FINE_ACCOUNT_NOT_PRESENT ));
313+ assertThat (allEnvelopes .get (0 ).payload ().getJsonObject ("migratedMasterDefendantCourtEmailAndFineAccount" ).getString ("caseId" ), is (caseId1 ));
314+
315+ assertThat (allEnvelopes .get (1 ).payload ().getJsonObject ("migratedMasterDefendantCourtEmailAndFineAccount" ).getString ("fineAccountNumber" ), is (FINE_ACCOUNT_NOT_PRESENT ));
316+ assertThat (allEnvelopes .get (1 ).payload ().getJsonObject ("migratedMasterDefendantCourtEmailAndFineAccount" ).getString ("defendantName" ), containsString ("Junior" ));
317+
318+ assertThat (JsonEnvelope .envelopeFrom (allEnvelopes .get (2 ).metadata (), allEnvelopes .get (2 ).payload ()),
319+ jsonEnvelope (
320+ metadata ().withName ("result.command.send-migrated-inactive-nces-email-for-application" ),
321+ payloadIsJson (allOf (
322+ withJsonPath ("$.migratedMasterDefendantCourtEmailAndFineAccount.caseId" , is (caseId2 )),
323+ withJsonPath ("$.migratedMasterDefendantCourtEmailAndFineAccount.fineAccountNumber" , is ("67890" ))
324+ ))));
325+
259326 assertThat (allEnvelopes .get (3 ).metadata ().name (), is ("result.command.send-nces-email-for-application" ));
260327 }
261328
0 commit comments