Skip to content

Commit

Permalink
Merge pull request #4200 from hmcts/NFDIV-4548
Browse files Browse the repository at this point in the history
NFDIV-4548 Failed SystemApplyNoticeOfChange leaves caseData invalid - Log failed notifications without failing event
  • Loading branch information
BWeeks-JusticeGovUK authored Nov 29, 2024
2 parents 713443a + c35d7b7 commit aac37c6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import uk.gov.hmcts.divorce.noticeofchange.client.AssignCaseAccessClient;
import uk.gov.hmcts.divorce.noticeofchange.service.ChangeOfRepresentativeService;
import uk.gov.hmcts.divorce.notification.NotificationDispatcher;
import uk.gov.hmcts.divorce.notification.exception.NotificationTemplateException;
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;
import uk.gov.hmcts.reform.ccd.client.model.AboutToStartOrSubmitCallbackResponse;

Expand All @@ -44,6 +45,8 @@
public class SystemApplyNoticeOfChange implements CCDConfig<CaseData, State, UserRole> {
public static final String NOTICE_OF_CHANGE_APPLIED = "notice-of-change-applied";
public static final String LETTER_TYPE_GRANT_OF_REPRESENTATION = "grant-of-representation";
public static final String NOTICE_OF_CHANGE_FAILED_ERROR = "Notice of change failed with the following error(s) for CaseID {}:";
public static final String NOTIFICATION_FAILED_ERROR = "nocCitizenToSolsNotifications failed for case Id: {} with message: {}";

private final AuthTokenGenerator authTokenGenerator;
private final ObjectMapper objectMapper;
Expand Down Expand Up @@ -88,7 +91,7 @@ public AboutToStartOrSubmitResponse<CaseData, State> aboutToStart(final CaseDeta
List<String> responseErrors = response.getErrors();

if (Objects.nonNull(responseErrors) && !responseErrors.isEmpty()) {
log.info("Notice of change failed with the following error(s) for CaseID {}:", details.getId());
log.info(NOTICE_OF_CHANGE_FAILED_ERROR, details.getId());
responseErrors.forEach(log::info);

return AboutToStartOrSubmitResponse.<CaseData, State>builder()
Expand All @@ -100,8 +103,12 @@ public AboutToStartOrSubmitResponse<CaseData, State> aboutToStart(final CaseDeta

CaseData responseData = objectMapper.convertValue(data, CaseData.class);

notificationDispatcher.sendNOC(nocCitizenToSolsNotifications, caseData,
try {
notificationDispatcher.sendNOC(nocCitizenToSolsNotifications, caseData,
beforeCaseData, details.getId(), isApplicant1, NEW_DIGITAL_SOLICITOR_NEW_ORG);
} catch (final NotificationTemplateException e) {
log.error(NOTIFICATION_FAILED_ERROR, details.getId(), e.getMessage(), e);
}

return AboutToStartOrSubmitResponse.<CaseData, State>builder()
.data(responseData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import uk.gov.hmcts.divorce.noticeofchange.model.AcaRequest;
import uk.gov.hmcts.divorce.noticeofchange.service.ChangeOfRepresentativeService;
import uk.gov.hmcts.divorce.notification.NotificationDispatcher;
import uk.gov.hmcts.divorce.notification.exception.NotificationTemplateException;
import uk.gov.hmcts.divorce.testutil.TestDataHelper;
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;
import uk.gov.hmcts.reform.ccd.client.model.AboutToStartOrSubmitCallbackResponse;
Expand All @@ -39,6 +40,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.hmcts.ccd.sdk.type.YesOrNo.NO;
Expand Down Expand Up @@ -182,6 +184,39 @@ void shouldNotApplyNoticeOfChangeWhenErrorsThrown() {
);
}

@Test
void shouldNotFailEventWhenNotificationsFail() {
setup();
Applicant applicant = TestDataHelper.applicantRepresentedBySolicitor();
final ChangeOrganisationRequest<CaseRoleID> changeOrganisationRequest = getChangeOrganisationRequestField("[APPONESOLICITOR]",
"APPLICANT_1_SOLICITOR");

CaseData caseData = CaseData.builder().applicant1(applicant).changeOrganisationRequestField(changeOrganisationRequest).build();

var details = CaseDetails.<CaseData, State>builder().id(TEST_CASE_ID).data(caseData).build();
AcaRequest acaRequest = AcaRequest.acaRequest(details);
Map<String, Object> expectedData = expectedData(caseData);
when(objectMapper.convertValue(expectedData, CaseData.class)).thenReturn(caseData);
doThrow(new NotificationTemplateException("some error"))
.when(notificationDispatcher).sendNOC(nocCitizenToSolsNotifications, caseData, null,
TEST_CASE_ID, true, NEW_DIGITAL_SOLICITOR_NEW_ORG);

AboutToStartOrSubmitCallbackResponse response = AboutToStartOrSubmitCallbackResponse
.builder().data(expectedData).build();
when(assignCaseAccessClient.applyNoticeOfChange(TEST_AUTHORIZATION_TOKEN, TEST_SERVICE_AUTH_TOKEN, acaRequest))
.thenReturn(response);

systemApplyNoticeOfChange.aboutToStart(details);

verify(assignCaseAccessClient).applyNoticeOfChange(TEST_AUTHORIZATION_TOKEN, TEST_SERVICE_AUTH_TOKEN, acaRequest);
verify(changeOfRepresentativeService).buildChangeOfRepresentative(caseData, null, SOLICITOR_NOTICE_OF_CHANGE.getValue(), true);
verify(notificationDispatcher).sendNOC(nocCitizenToSolsNotifications, caseData, null,
TEST_CASE_ID, true, NEW_DIGITAL_SOLICITOR_NEW_ORG);

assertEquals(NO, caseData.getConditionalOrder().getConditionalOrderApplicant1Questions().getIsSubmitted());
assertEquals(NO, caseData.getConditionalOrder().getConditionalOrderApplicant1Questions().getIsDrafted());
}

private Map<String, Object> expectedData(final CaseData caseData) {

ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
Expand Down

0 comments on commit aac37c6

Please sign in to comment.