Skip to content

Commit

Permalink
DIAC-854 fix organisation policy validation in remove representative …
Browse files Browse the repository at this point in the history
…event (#2457)
  • Loading branch information
alivenichoppa authored Jan 24, 2025
1 parent 3e94b96 commit 2d73cb8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AsylumCase> {
Expand Down Expand Up @@ -42,10 +46,15 @@ public PreSubmitCallbackResponse<AsylumCase> handle(
AsylumCase asylumCase = callback.getCaseDetails().getCaseData();
PreSubmitCallbackResponse<AsylumCase> response = new PreSubmitCallbackResponse<>(asylumCase);

if (callback.getCaseDetails().getCaseData().read(AsylumCaseFieldDefinition.LOCAL_AUTHORITY_POLICY).isEmpty()) {
Optional<OrganisationPolicy> 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");
Expand All @@ -58,8 +67,8 @@ public PreSubmitCallbackResponse<AsylumCase> handle(
)
);

return response;
}
return response;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;


Expand Down Expand Up @@ -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<AsylumCase> callbackResponse =
Expand Down Expand Up @@ -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());

Expand All @@ -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<AsylumCase> 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<Arguments> 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() {

Expand Down

0 comments on commit 2d73cb8

Please sign in to comment.