|
| 1 | +package uk.gov.hmcts.reform.iacaseapi.domain.handlers.presubmit; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | +import static org.mockito.Mockito.reset; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.FTPA_APPLICANT_TYPE; |
| 12 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.IS_FTPA_APPELLANT_DECIDED; |
| 13 | +import static uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCaseFieldDefinition.IS_FTPA_RESPONDENT_DECIDED; |
| 14 | + |
| 15 | +import java.util.Optional; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 22 | +import org.mockito.quality.Strictness; |
| 23 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.AsylumCase; |
| 24 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.CaseDetails; |
| 25 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.Event; |
| 26 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.Callback; |
| 27 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackResponse; |
| 28 | +import uk.gov.hmcts.reform.iacaseapi.domain.entities.ccd.callback.PreSubmitCallbackStage; |
| 29 | + |
| 30 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 31 | +@ExtendWith(MockitoExtension.class) |
| 32 | +@SuppressWarnings("unchecked") |
| 33 | +class ForceFtpaDecidedStateHandlerTest { |
| 34 | + |
| 35 | + @Mock |
| 36 | + private Callback<AsylumCase> callback; |
| 37 | + @Mock |
| 38 | + private CaseDetails<AsylumCase> caseDetails; |
| 39 | + @Mock |
| 40 | + private AsylumCase asylumCase; |
| 41 | + |
| 42 | + private ForceFtpaDecidedStateHandler forceFtpaDecidedStateHandler; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setUp() { |
| 46 | + forceFtpaDecidedStateHandler = new ForceFtpaDecidedStateHandler(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void should_set_appellant_decided_flag_when_applicant_type_is_appellant() { |
| 51 | + |
| 52 | + when(callback.getCaseDetails()).thenReturn(caseDetails); |
| 53 | + when(callback.getEvent()).thenReturn(Event.FORCE_FTPA_DECIDED_STATE); |
| 54 | + when(caseDetails.getCaseData()).thenReturn(asylumCase); |
| 55 | + when(asylumCase.read(FTPA_APPLICANT_TYPE, String.class)).thenReturn(Optional.of("appellant")); |
| 56 | + |
| 57 | + PreSubmitCallbackResponse<AsylumCase> returnedCallbackResponse = |
| 58 | + forceFtpaDecidedStateHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback); |
| 59 | + |
| 60 | + assertNotNull(returnedCallbackResponse); |
| 61 | + assertEquals(asylumCase, returnedCallbackResponse.getData()); |
| 62 | + verify(asylumCase).write(IS_FTPA_APPELLANT_DECIDED, "Yes"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void should_set_respondent_decided_flag_when_applicant_type_is_respondent() { |
| 67 | + |
| 68 | + when(callback.getCaseDetails()).thenReturn(caseDetails); |
| 69 | + when(callback.getEvent()).thenReturn(Event.FORCE_FTPA_DECIDED_STATE); |
| 70 | + when(caseDetails.getCaseData()).thenReturn(asylumCase); |
| 71 | + when(asylumCase.read(FTPA_APPLICANT_TYPE, String.class)).thenReturn(Optional.of("respondent")); |
| 72 | + |
| 73 | + PreSubmitCallbackResponse<AsylumCase> returnedCallbackResponse = |
| 74 | + forceFtpaDecidedStateHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback); |
| 75 | + |
| 76 | + assertNotNull(returnedCallbackResponse); |
| 77 | + assertEquals(asylumCase, returnedCallbackResponse.getData()); |
| 78 | + verify(asylumCase).write(IS_FTPA_RESPONDENT_DECIDED, "Yes"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void should_throw_exception_when_applicant_type_is_missing() { |
| 83 | + |
| 84 | + when(callback.getCaseDetails()).thenReturn(caseDetails); |
| 85 | + when(callback.getEvent()).thenReturn(Event.FORCE_FTPA_DECIDED_STATE); |
| 86 | + when(caseDetails.getCaseData()).thenReturn(asylumCase); |
| 87 | + when(asylumCase.read(FTPA_APPLICANT_TYPE, String.class)).thenReturn(Optional.empty()); |
| 88 | + |
| 89 | + assertThatThrownBy(() -> forceFtpaDecidedStateHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback)) |
| 90 | + .hasMessage("appellantType is missing") |
| 91 | + .isExactlyInstanceOf(IllegalStateException.class); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void should_throw_exception_when_applicant_type_is_invalid() { |
| 96 | + |
| 97 | + when(callback.getCaseDetails()).thenReturn(caseDetails); |
| 98 | + when(callback.getEvent()).thenReturn(Event.FORCE_FTPA_DECIDED_STATE); |
| 99 | + when(caseDetails.getCaseData()).thenReturn(asylumCase); |
| 100 | + when(asylumCase.read(FTPA_APPLICANT_TYPE, String.class)).thenReturn(Optional.of("invalid")); |
| 101 | + |
| 102 | + assertThatThrownBy(() -> forceFtpaDecidedStateHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback)) |
| 103 | + .hasMessage("appellantType is is new and needs handling") |
| 104 | + .isExactlyInstanceOf(IllegalStateException.class); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void handling_should_throw_if_cannot_actually_handle() { |
| 109 | + |
| 110 | + assertThatThrownBy( |
| 111 | + () -> forceFtpaDecidedStateHandler.handle(PreSubmitCallbackStage.ABOUT_TO_START, callback)) |
| 112 | + .hasMessage("Cannot handle callback") |
| 113 | + .isExactlyInstanceOf(IllegalStateException.class); |
| 114 | + |
| 115 | + when(callback.getEvent()).thenReturn(Event.SEND_DIRECTION); |
| 116 | + assertThatThrownBy( |
| 117 | + () -> forceFtpaDecidedStateHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, callback)) |
| 118 | + .hasMessage("Cannot handle callback") |
| 119 | + .isExactlyInstanceOf(IllegalStateException.class); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void it_can_handle_callback() { |
| 124 | + |
| 125 | + for (Event event : Event.values()) { |
| 126 | + |
| 127 | + when(callback.getEvent()).thenReturn(event); |
| 128 | + |
| 129 | + for (PreSubmitCallbackStage callbackStage : PreSubmitCallbackStage.values()) { |
| 130 | + |
| 131 | + boolean canHandle = forceFtpaDecidedStateHandler.canHandle(callbackStage, callback); |
| 132 | + |
| 133 | + if (event == Event.FORCE_FTPA_DECIDED_STATE |
| 134 | + && callbackStage == PreSubmitCallbackStage.ABOUT_TO_SUBMIT) { |
| 135 | + |
| 136 | + assertTrue(canHandle); |
| 137 | + } else { |
| 138 | + assertFalse(canHandle); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + reset(callback); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void should_not_allow_null_arguments() { |
| 148 | + |
| 149 | + assertThatThrownBy(() -> forceFtpaDecidedStateHandler.canHandle(null, callback)) |
| 150 | + .hasMessage("callbackStage must not be null") |
| 151 | + .isExactlyInstanceOf(NullPointerException.class); |
| 152 | + |
| 153 | + assertThatThrownBy(() -> forceFtpaDecidedStateHandler.canHandle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, null)) |
| 154 | + .hasMessage("callback must not be null") |
| 155 | + .isExactlyInstanceOf(NullPointerException.class); |
| 156 | + |
| 157 | + assertThatThrownBy(() -> forceFtpaDecidedStateHandler.handle(null, callback)) |
| 158 | + .hasMessage("callbackStage must not be null") |
| 159 | + .isExactlyInstanceOf(NullPointerException.class); |
| 160 | + |
| 161 | + assertThatThrownBy(() -> forceFtpaDecidedStateHandler.handle(PreSubmitCallbackStage.ABOUT_TO_SUBMIT, null)) |
| 162 | + .hasMessage("callback must not be null") |
| 163 | + .isExactlyInstanceOf(NullPointerException.class); |
| 164 | + } |
| 165 | +} |
0 commit comments