|
| 1 | +package uk.gov.hmcts.reform.managecase.api.controller.provider; |
| 2 | + |
| 3 | +import au.com.dius.pact.provider.junit5.PactVerificationContext; |
| 4 | +import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider; |
| 5 | +import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify; |
| 6 | +import au.com.dius.pact.provider.junitsupport.Provider; |
| 7 | +import au.com.dius.pact.provider.junitsupport.State; |
| 8 | +import au.com.dius.pact.provider.junitsupport.loader.PactBroker; |
| 9 | +import au.com.dius.pact.provider.junitsupport.loader.VersionSelector; |
| 10 | +import au.com.dius.pact.provider.spring.junit5.MockMvcTestTarget; |
| 11 | +import com.fasterxml.jackson.databind.JsonNode; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.TestTemplate; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.test.context.ContextConfiguration; |
| 17 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 18 | +import uk.gov.hmcts.reform.idam.client.models.UserDetails; |
| 19 | +import uk.gov.hmcts.reform.managecase.api.controller.CaseAssignmentController; |
| 20 | +import uk.gov.hmcts.reform.managecase.client.datastore.CaseUserRole; |
| 21 | +import uk.gov.hmcts.reform.managecase.config.MapperConfig; |
| 22 | +import uk.gov.hmcts.reform.managecase.domain.OrganisationPolicy; |
| 23 | +import uk.gov.hmcts.reform.managecase.repository.DataStoreRepository; |
| 24 | +import uk.gov.hmcts.reform.managecase.repository.IdamRepository; |
| 25 | +import uk.gov.hmcts.reform.managecase.repository.PrdRepository; |
| 26 | +import uk.gov.hmcts.reform.managecase.security.SecurityUtils; |
| 27 | +import uk.gov.hmcts.reform.managecase.util.JacksonUtils; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.mockito.ArgumentMatchers.any; |
| 33 | +import static org.mockito.ArgumentMatchers.anyList; |
| 34 | +import static org.mockito.ArgumentMatchers.anyString; |
| 35 | +import static org.mockito.ArgumentMatchers.eq; |
| 36 | +import static org.mockito.BDDMockito.given; |
| 37 | +import static uk.gov.hmcts.reform.managecase.TestFixtures.CaseDetailsFixture.organisationPolicy; |
| 38 | +import static uk.gov.hmcts.reform.managecase.TestFixtures.ProfessionalUserFixture.user; |
| 39 | +import static uk.gov.hmcts.reform.managecase.TestFixtures.ProfessionalUserFixture.usersByOrganisation; |
| 40 | + |
| 41 | +@ExtendWith(SpringExtension.class) |
| 42 | +@Provider("acc_manageCaseAssignment") |
| 43 | +@PactBroker(scheme = "${PACT_BROKER_SCHEME:http}", |
| 44 | + host = "${PACT_BROKER_URL:localhost}", |
| 45 | + port = "${PACT_BROKER_PORT:80}", |
| 46 | + consumerVersionSelectors = {@VersionSelector(tag = "master")}) |
| 47 | +@ContextConfiguration(classes = {ContractConfig.class, MapperConfig.class}) |
| 48 | +@IgnoreNoPactsToVerify |
| 49 | +public class CasesAssignmentControllerProviderTest { |
| 50 | + |
| 51 | + private static final String ORG_POLICY_ROLE = "caseworker-probate"; |
| 52 | + private static final String ORG_POLICY_ROLE2 = "caseworker-probate2"; |
| 53 | + private static final String ORGANIZATION_ID = "TEST_ORG"; |
| 54 | + private static final String ASSIGNEE_ID = "0a5874a4-3f38-4bbd-ba4c"; |
| 55 | + private static final String BEAR_TOKEN = "TestBearToken"; |
| 56 | + |
| 57 | + private static final String ASSIGNEE_ID2 = "38130f09-0010-4c12-afd1-2563bb25d1d3"; |
| 58 | + private static final String CASE_ID = "12345678"; |
| 59 | + private static final String CASE_ID2 = "87654321"; |
| 60 | + private static final String CASE_ROLE = "[CR1]"; |
| 61 | + private static final String CASE_ROLE2 = "[CR2]"; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + DataStoreRepository dataStoreRepository; |
| 65 | + @Autowired |
| 66 | + PrdRepository prdRepository; |
| 67 | + @Autowired |
| 68 | + IdamRepository idamRepository; |
| 69 | + @Autowired |
| 70 | + JacksonUtils jacksonUtils; |
| 71 | + @Autowired |
| 72 | + SecurityUtils securityUtils; |
| 73 | + |
| 74 | + @Autowired |
| 75 | + CaseAssignmentController caseAssignmentController; |
| 76 | + |
| 77 | + |
| 78 | + @TestTemplate |
| 79 | + @ExtendWith(PactVerificationInvocationContextProvider.class) |
| 80 | + void pactVerificationTestTemplate(PactVerificationContext context) { |
| 81 | + if (context != null) { |
| 82 | + context.verifyInteraction(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @BeforeEach |
| 87 | + void before(PactVerificationContext context) { |
| 88 | + MockMvcTestTarget testTarget = new MockMvcTestTarget(); |
| 89 | + //System.getProperties().setProperty("pact.verifier.publishResults", "true"); |
| 90 | + testTarget.setControllers(caseAssignmentController); |
| 91 | + if (context != null) { |
| 92 | + context.setTarget(testTarget); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @State({"Assign a user to a case"}) |
| 97 | + public void toAssignUserToCase() throws IOException { |
| 98 | + |
| 99 | + given(prdRepository.findUsersByOrganisation()) |
| 100 | + .willReturn(usersByOrganisation(user(ASSIGNEE_ID))); |
| 101 | + given(dataStoreRepository.findCaseByCaseIdUsingExternalApi(anyString())) |
| 102 | + .willReturn(TestFixtures.CaseDetailsFixture.caseDetails(ORGANIZATION_ID, ORG_POLICY_ROLE)); |
| 103 | + |
| 104 | + given(securityUtils.hasSolicitorRole(anyList())).willReturn(true); |
| 105 | + given(securityUtils.hasSolicitorAndJurisdictionRoles(anyList(), anyString())).willReturn(true); |
| 106 | + |
| 107 | + UserDetails userDetails = UserDetails.builder() |
| 108 | + .id(ASSIGNEE_ID).roles(List.of("caseworker-AUTOTEST1-solicitor")).build(); |
| 109 | + given(idamRepository.getCaaSystemUserAccessToken()).willReturn(BEAR_TOKEN); |
| 110 | + given(idamRepository.getNocApproverSystemUserAccessToken()).willReturn(BEAR_TOKEN); |
| 111 | + given(idamRepository.getUserByUserId(ASSIGNEE_ID, BEAR_TOKEN)).willReturn(userDetails); |
| 112 | + |
| 113 | + given(jacksonUtils.convertValue(any(JsonNode.class), eq(OrganisationPolicy.class))) |
| 114 | + .willReturn(organisationPolicy(ORGANIZATION_ID, ORG_POLICY_ROLE)) |
| 115 | + .willReturn(organisationPolicy(ORGANIZATION_ID, ORG_POLICY_ROLE2)); |
| 116 | + } |
| 117 | + |
| 118 | + @State({"Case assignments exist for case Ids"}) |
| 119 | + public void toGetExistingCaseAssignments() throws IOException { |
| 120 | + given(prdRepository.findUsersByOrganisation()) |
| 121 | + .willReturn(usersByOrganisation(user(ASSIGNEE_ID), user(ASSIGNEE_ID2))); |
| 122 | + given(dataStoreRepository.getCaseAssignments(eq(List.of(CASE_ID, CASE_ID2)), |
| 123 | + eq(List.of(ASSIGNEE_ID, ASSIGNEE_ID2)))) |
| 124 | + .willReturn(List.of( |
| 125 | + new CaseUserRole(CASE_ID, ASSIGNEE_ID, CASE_ROLE), |
| 126 | + new CaseUserRole(CASE_ID2, ASSIGNEE_ID2, CASE_ROLE), |
| 127 | + new CaseUserRole(CASE_ID2, ASSIGNEE_ID2, CASE_ROLE2))); |
| 128 | + |
| 129 | + } |
| 130 | +} |
0 commit comments