|
| 1 | +package uk.gov.hmcts.reform.pcs.ccd.event; |
| 2 | + |
| 3 | +import jakarta.transaction.Transactional; |
| 4 | +import org.junit.jupiter.api.AfterEach; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 12 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 13 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; |
| 14 | +import org.springframework.test.context.ActiveProfiles; |
| 15 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 16 | +import uk.gov.hmcts.ccd.sdk.api.EventPayload; |
| 17 | +import uk.gov.hmcts.ccd.sdk.api.callback.SubmitResponse; |
| 18 | +import uk.gov.hmcts.ccd.sdk.type.DynamicList; |
| 19 | +import uk.gov.hmcts.ccd.sdk.type.DynamicListElement; |
| 20 | +import uk.gov.hmcts.reform.idam.client.IdamClient; |
| 21 | +import uk.gov.hmcts.reform.pcs.ccd.domain.PCSCase; |
| 22 | +import uk.gov.hmcts.reform.pcs.ccd.domain.State; |
| 23 | +import uk.gov.hmcts.reform.pcs.ccd.testcasesupport.TestCaseSupportHelper; |
| 24 | +import uk.gov.hmcts.reform.pcs.config.AbstractPostgresContainerIT; |
| 25 | +import uk.gov.hmcts.reform.pcs.feesandpay.model.FeeDetails; |
| 26 | +import uk.gov.hmcts.reform.pcs.feesandpay.model.FeeType; |
| 27 | +import uk.gov.hmcts.reform.pcs.feesandpay.service.FeeService; |
| 28 | +import uk.gov.hmcts.reform.pcs.idam.User; |
| 29 | +import uk.gov.hmcts.reform.pcs.idam.UserInfo; |
| 30 | +import uk.gov.hmcts.reform.pcs.util.IdamHelper; |
| 31 | + |
| 32 | +import java.math.BigDecimal; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.UUID; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 39 | +import static org.mockito.ArgumentMatchers.any; |
| 40 | +import static org.mockito.ArgumentMatchers.anyString; |
| 41 | +import static org.mockito.Mockito.mock; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | +import static uk.gov.hmcts.reform.pcs.ccd.domain.State.CASE_ISSUED; |
| 44 | +import static uk.gov.hmcts.reform.pcs.ccd.domain.State.PENDING_CASE_ISSUED; |
| 45 | +import static uk.gov.hmcts.reform.pcs.ccd.event.TestCaseGeneration.ENFORCEMENT_CASE_GENERATOR; |
| 46 | +import static uk.gov.hmcts.reform.pcs.ccd.event.TestCaseGeneration.MAKE_A_CLAIM_CASE_GENERATOR; |
| 47 | +import static uk.gov.hmcts.reform.pcs.ccd.event.TestCaseGeneration.NO_NON_PROD_CASE_AVAILABLE; |
| 48 | + |
| 49 | +/** |
| 50 | + * This is based on the test json sitting in src/main/java/resources/test-case-generation |
| 51 | + * A change to the domain CAN lead to a failure here as the test json will need to be updated so to keep the test |
| 52 | + * creation event working. In order to update the json when you change the domain then a walk though the broken |
| 53 | + * journey is required and a capture of the json from the draft_case_data BEFORE submit of that event need to be made |
| 54 | + * and replace the existing json within the relevant file. |
| 55 | + */ |
| 56 | +@SpringBootTest |
| 57 | +@AutoConfigureMockMvc |
| 58 | +@ActiveProfiles({"integration"}) |
| 59 | +@DisplayName("TestCaseGenerationIT Integration Tests") |
| 60 | +public class TestCaseGenerationIT extends AbstractPostgresContainerIT { |
| 61 | + |
| 62 | + private static final String SYSTEM_USER_ID_TOKEN = "system-user-id-token"; |
| 63 | + private static final UUID USER_ID = UUID.fromString("123e4567-e89b-12d3-a456-426614174001"); |
| 64 | + private static final long CASE_REFERENCE = 1234567890123456L; |
| 65 | + |
| 66 | + @Autowired |
| 67 | + private TestCaseGeneration underTest; |
| 68 | + @Autowired |
| 69 | + private TestCaseSupportHelper testCaseSupportHelper; |
| 70 | + @Autowired |
| 71 | + private IdamHelper idamHelper; |
| 72 | + @MockitoBean |
| 73 | + private FeeService feeService; |
| 74 | + @MockitoBean |
| 75 | + private OAuth2AuthorizedClientManager authorizedClientManager; |
| 76 | + @MockitoBean |
| 77 | + private IdamClient idamClient; |
| 78 | + |
| 79 | + @BeforeEach |
| 80 | + void setUp() { |
| 81 | + setUpAuthenticatedUser(); |
| 82 | + |
| 83 | + FeeDetails feeDetails = FeeDetails.builder().code("FEE0001").feeAmount(new BigDecimal("123.45")).build(); |
| 84 | + when(feeService.getFee(any(FeeType.class))).thenReturn(feeDetails); |
| 85 | + } |
| 86 | + |
| 87 | + @AfterEach |
| 88 | + void clearSecurityContext() { |
| 89 | + SecurityContextHolder.clearContext(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @Transactional |
| 94 | + void shouldSubmitMakeAClaim() { |
| 95 | + // Given |
| 96 | + String label = selectLabelStartingWith(MAKE_A_CLAIM_CASE_GENERATOR); |
| 97 | + EventPayload<PCSCase, State> eventPayload = buildEventPayload(label); |
| 98 | + |
| 99 | + // When |
| 100 | + SubmitResponse<State> response = underTest.submit(eventPayload); |
| 101 | + |
| 102 | + // Then |
| 103 | + assertThat(response).isNotNull(); |
| 104 | + assertThat(response.getState()).isEqualTo(PENDING_CASE_ISSUED); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Transactional |
| 109 | + void shouldSubmitEnforcement() { |
| 110 | + // Given |
| 111 | + String label = selectLabelStartingWith(ENFORCEMENT_CASE_GENERATOR); |
| 112 | + EventPayload<PCSCase, State> eventPayload = buildEventPayload(label); |
| 113 | + |
| 114 | + // When |
| 115 | + SubmitResponse<State> response = underTest.submit(eventPayload); |
| 116 | + |
| 117 | + // Then |
| 118 | + assertThat(response).isNotNull(); |
| 119 | + assertThat(response.getState()).isEqualTo(CASE_ISSUED); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + @Transactional |
| 124 | + void shouldThrowWhenNoTestFilesListAvailable() { |
| 125 | + // Given |
| 126 | + PCSCase pcsCase = PCSCase.builder().build(); |
| 127 | + EventPayload<PCSCase, State> eventPayload = |
| 128 | + new EventPayload<>(CASE_REFERENCE, pcsCase, null); |
| 129 | + |
| 130 | + // When / Then |
| 131 | + assertThatThrownBy(() -> underTest.submit(eventPayload)) |
| 132 | + .isInstanceOf(IllegalArgumentException.class) |
| 133 | + .hasMessage(NO_NON_PROD_CASE_AVAILABLE); |
| 134 | + } |
| 135 | + |
| 136 | + private EventPayload<PCSCase, State> buildEventPayload(String label) { |
| 137 | + DynamicListElement selected = DynamicListElement.builder() |
| 138 | + .code(UUID.randomUUID()) |
| 139 | + .label(label) |
| 140 | + .build(); |
| 141 | + |
| 142 | + DynamicList testFilesList = DynamicList.builder() |
| 143 | + .value(selected) |
| 144 | + .listItems(List.of(selected)) |
| 145 | + .build(); |
| 146 | + |
| 147 | + PCSCase pcsCase = PCSCase.builder().testCaseSupportFileList(testFilesList).build(); |
| 148 | + return new EventPayload<>(CASE_REFERENCE, pcsCase, null); |
| 149 | + } |
| 150 | + |
| 151 | + private String selectLabelStartingWith(String prefix) { |
| 152 | + DynamicList fileList = testCaseSupportHelper.getFileList(); |
| 153 | + assertThat(fileList).isNotNull(); |
| 154 | + assertThat(fileList.getListItems()).isNotNull(); |
| 155 | + |
| 156 | + return fileList.getListItems().stream() |
| 157 | + .map(DynamicListElement::getLabel) |
| 158 | + .filter(l -> l != null && l.startsWith(prefix)) |
| 159 | + .findFirst() |
| 160 | + .orElseThrow(() -> new IllegalStateException( |
| 161 | + "No test-case-generation file found with prefix: " + prefix)); |
| 162 | + } |
| 163 | + |
| 164 | + private void setUpAuthenticatedUser() { |
| 165 | + idamHelper.stubIdamSystemUser(authorizedClientManager, SYSTEM_USER_ID_TOKEN); |
| 166 | + uk.gov.hmcts.reform.idam.client.models.UserInfo idamUserInfo = |
| 167 | + mock(uk.gov.hmcts.reform.idam.client.models.UserInfo.class); |
| 168 | + when(idamUserInfo.getUid()).thenReturn(USER_ID.toString()); |
| 169 | + when(idamClient.getUserInfo(anyString())).thenReturn(idamUserInfo); |
| 170 | + UserInfo userInfo = UserInfo.builder().uid(USER_ID.toString()).build(); |
| 171 | + User user = new User("testing", userInfo); |
| 172 | + UsernamePasswordAuthenticationToken auth = |
| 173 | + new UsernamePasswordAuthenticationToken(user, null, Collections.emptyList()); |
| 174 | + SecurityContextHolder.getContext().setAuthentication(auth); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments