diff --git a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparer.java b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparer.java index 1fc5ce2e57..a07c7c9497 100644 --- a/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparer.java +++ b/src/main/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparer.java @@ -2,8 +2,11 @@ import static com.google.common.collect.Lists.newArrayList; import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang3.StringUtils.isEmpty; import java.time.LocalDateTime; +import java.util.Optional; + import org.springframework.stereotype.Component; import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCase; import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition; @@ -15,6 +18,7 @@ import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackStage; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.ChangeOrganisationRequest; import uk.gov.hmcts.reform.iacaseapi.domain.handlers.PreSubmitCallbackHandler; +import uk.gov.hmcts.reform.iacaseapi.infrastructure.clients.model.ccd.OrganisationPolicy; @Component public class RemoveRepresentationPreparer implements PreSubmitCallbackHandler { @@ -42,10 +46,15 @@ public PreSubmitCallbackResponse handle( AsylumCase asylumCase = callback.getCaseDetails().getCaseData(); PreSubmitCallbackResponse response = new PreSubmitCallbackResponse<>(asylumCase); - if (callback.getCaseDetails().getCaseData().read(AsylumCaseFieldDefinition.LOCAL_AUTHORITY_POLICY).isEmpty()) { + Optional localAuthorityPolicy = callback.getCaseDetails().getCaseData().read(AsylumCaseFieldDefinition.LOCAL_AUTHORITY_POLICY); + if (localAuthorityPolicy.isEmpty()) { + // For appeals submitted before 10 February 2021, localAuthorityPolicy was not added to the case data response.addError("You cannot use this feature because the legal representative does not have a MyHMCTS account or the appeal was created before 10 February 2021."); response.addError("If you are a legal representative, you must contact all parties confirming you no longer represent this client."); - return response; + + } else if (localAuthorityPolicy.get().getOrganisation() == null + || isEmpty(localAuthorityPolicy.get().getOrganisation().getOrganisationID())) { + response.addError("This appellant is not currently represented so Notice of Change cannot be actioned. Please contact the Service Desk giving this error message."); } else { Value caseRole = new Value("[LEGALREPRESENTATIVE]", "Legal Representative"); @@ -58,8 +67,8 @@ public PreSubmitCallbackResponse handle( ) ); - return response; } + return response; } } diff --git a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparerTest.java b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparerTest.java index 76221cd059..d80dba0f76 100644 --- a/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/iacaseapi/domain/handlers/presubmit/RemoveRepresentationPreparerTest.java @@ -9,11 +9,15 @@ import java.time.LocalDateTime; import java.util.Optional; +import java.util.stream.Stream; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCase; @@ -26,6 +30,7 @@ import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackResponse; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackStage; import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.field.ChangeOrganisationRequest; +import uk.gov.hmcts.reform.iacaseapi.infrastructure.clients.model.ccd.Organisation; import uk.gov.hmcts.reform.iacaseapi.infrastructure.clients.model.ccd.OrganisationPolicy; @@ -70,6 +75,8 @@ void should_write_to_remove_representation_requested_flag_field(Event event) { when(callback.getCaseDetails()).thenReturn(caseDetails); when(callback.getEvent()).thenReturn(event); when(caseDetails.getCaseData()).thenReturn(asylumCase); + when(organisationPolicy.getOrganisation()).thenReturn( + Organisation.builder().organisationID("123").organisationName("test").build()); when(asylumCase.read(AsylumCaseFieldDefinition.LOCAL_AUTHORITY_POLICY)).thenReturn(Optional.of(organisationPolicy)); PreSubmitCallbackResponse callbackResponse = @@ -101,10 +108,10 @@ void handling_should_throw_if_cannot_actually_handle() { @EnumSource(value = Event.class, names = { "REMOVE_REPRESENTATION", "REMOVE_LEGAL_REPRESENTATIVE" }) - void should_respond_with_error_for_legal_rep_when_organisation_policy_not_present() { + void should_respond_with_error_for_legal_rep_when_organisation_policy_not_present(Event event) { when(callback.getCaseDetails()).thenReturn(caseDetails); - when(callback.getEvent()).thenReturn(Event.REMOVE_REPRESENTATION); + when(callback.getEvent()).thenReturn(event); when(caseDetails.getCaseData()).thenReturn(asylumCase); when(asylumCase.read(LOCAL_AUTHORITY_POLICY)).thenReturn(Optional.empty()); @@ -122,6 +129,43 @@ void should_respond_with_error_for_legal_rep_when_organisation_policy_not_presen verify(asylumCase, times(0)).write(CHANGE_ORGANISATION_REQUEST_FIELD, changeOrganisationRequest); } + @ParameterizedTest + @MethodSource("provideOrganisationPolicyValues") + void should_respond_with_error_for_legal_rep_when_organisation_id_not_present(Event event, OrganisationPolicy orgPolicy) { + + when(callback.getCaseDetails()).thenReturn(caseDetails); + when(callback.getEvent()).thenReturn(event); + when(caseDetails.getCaseData()).thenReturn(asylumCase); + when(asylumCase.read(LOCAL_AUTHORITY_POLICY)).thenReturn(Optional.of(orgPolicy)); + + PreSubmitCallbackResponse response = + removeRepresentationPreparer.handle( + PreSubmitCallbackStage.ABOUT_TO_START, + callback + ); + + assertThat(response.getData()).isInstanceOf(AsylumCase.class); + assertThat(response.getErrors()).contains("This appellant is not currently represented so Notice of Change cannot be actioned. Please contact the Service Desk giving this error message."); + + verify(asylumCase, times(1)).read(LOCAL_AUTHORITY_POLICY); + verify(asylumCase, times(0)).write(CHANGE_ORGANISATION_REQUEST_FIELD, changeOrganisationRequest); + } + + private static Stream provideOrganisationPolicyValues() { + return Stream.of( + Arguments.of(Event.REMOVE_REPRESENTATION, OrganisationPolicy.builder().build()), + Arguments.of(Event.REMOVE_LEGAL_REPRESENTATIVE, OrganisationPolicy.builder().build()), + Arguments.of(Event.REMOVE_REPRESENTATION, OrganisationPolicy.builder().organisation(Organisation.builder().build()).build()), + Arguments.of(Event.REMOVE_LEGAL_REPRESENTATIVE, OrganisationPolicy.builder().organisation(Organisation.builder().build()).build()), + Arguments.of(Event.REMOVE_REPRESENTATION, OrganisationPolicy.builder().organisation(Organisation.builder().organisationName("Org1").build()).build()), + Arguments.of(Event.REMOVE_LEGAL_REPRESENTATIVE, OrganisationPolicy.builder().organisation(Organisation.builder().organisationName("Org1").build()).build()), + Arguments.of(Event.REMOVE_REPRESENTATION, OrganisationPolicy.builder().organisation(Organisation.builder().organisationID("").build()).build()), + Arguments.of(Event.REMOVE_LEGAL_REPRESENTATIVE, OrganisationPolicy.builder().organisation(Organisation.builder().organisationID("").build()).build()), + Arguments.of(Event.REMOVE_REPRESENTATION, OrganisationPolicy.builder().organisation(Organisation.builder().organisationID(null).build()).build()), + Arguments.of(Event.REMOVE_LEGAL_REPRESENTATIVE, OrganisationPolicy.builder().organisation(Organisation.builder().organisationID(null).build()).build()) + ); + } + @Test void it_can_handle_callback() {