Skip to content

Commit 5b8c22b

Browse files
authored
Merge pull request #262 from hmcts/TA-234_PactProviderTest
Ta 234 pact provider test
2 parents 39e87da + 411ebe3 commit 5b8c22b

File tree

6 files changed

+344
-12
lines changed

6 files changed

+344
-12
lines changed

Jenkinsfile_CNP

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@Library("Infrastructure")
44

55
import uk.gov.hmcts.contino.GradleBuilder
6+
import uk.gov.hmcts.contino.AppPipelineDsl
67

78
def product = "aac"
89
def component = "manage-case-assignment"
@@ -70,6 +71,7 @@ def vaultOverrides = [
7071
// Assume a feature build branched off 'develop', with dependencies develop-to-develop.
7172
env.DEFINITION_STORE_HOST = "http://ccd-definition-store-api-${definitionStoreDevelopPr}.service.core-compute-preview.internal".toLowerCase()
7273
env.CCD_DATA_STORE_API_BASE_URL = "http://ccd-data-store-api-${dataStoreApiDevelopPr}.service.core-compute-preview.internal".toLowerCase()
74+
env.MCA_API_BASE_URL = "http://aac-manage-case-assignment-${env.BRANCH_NAME}.service.core-compute-preview.internal".toLowerCase()
7375

7476
// Other env variables needed for BEFTA.
7577
env.S2S_URL = "http://rpe-service-auth-provider-aat.service.core-compute-aat.internal"
@@ -103,13 +105,15 @@ withPipeline("java", product, component) {
103105
}
104106
onMaster {
105107
enableSlackNotifications('#ccd-master-builds')
108+
enablePactAs([AppPipelineDsl.PactRoles.PROVIDER])
106109
}
107110

108111
onDemo {
109112
enableSlackNotifications('#ccd-demo-builds')
110113
}
111114
onPR {
112115
enableSlackNotifications('#ccd-pr-builds')
116+
enablePactAs([AppPipelineDsl.PactRoles.PROVIDER])
113117
}
114118

115119
// Check if the build should be wired to an environment higher than 'preview'.
@@ -121,26 +125,18 @@ withPipeline("java", product, component) {
121125
}
122126
env.DEFINITION_STORE_HOST = "http://ccd-definition-store-api-${environmentOfDependencies}.service.core-compute-${environmentOfDependencies}.internal"
123127
env.CCD_DATA_STORE_API_BASE_URL = "http://ccd-data-store-api-${environmentOfDependencies}.service.core-compute-${environmentOfDependencies}.internal"
128+
env.MCA_API_BASE_URL = "http://aac-manage-case-assignment-${environmentOfDependencies}.service.core-compute-${environmentOfDependencies}.internal"
124129
}
125130

126131
echo "FTA will use Definition Store: ${env.DEFINITION_STORE_HOST}"
132+
echo " ... published with MCA URL: ${env.MCA_API_BASE_URL}"
127133

128134
overrideVaultEnvironments(vaultOverrides)
129135
loadVaultSecrets(secrets)
130136

131137
enableAksStagingDeployment()
132138
disableLegacyDeployment()
133139

134-
before('smoketest:preview') {
135-
env.MCA_API_BASE_URL = env.TEST_URL.replace("https", "http")
136-
echo " ... published with MCA URL: ${env.MCA_API_BASE_URL}"
137-
}
138-
139-
before('smoketest:aat') {
140-
env.MCA_API_BASE_URL = env.TEST_URL.replace("https", "http")
141-
echo " ... published with MCA URL: ${env.MCA_API_BASE_URL}"
142-
}
143-
144140
after('smoketest:preview') {
145141
copyIgnore('./build/reports/tests/befta/smoke/cucumber-html-reports', './BEFTA Report for Smoke Tests/')
146142
steps.archiveArtifacts allowEmptyArchive: true, artifacts: '**/BEFTA Report for Smoke Tests/**/*'

build.gradle

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
id 'com.github.ben-manes.versions' version '0.28.0'
88
id 'com.github.spacialcircumstances.gradle-cucumber-reporting' version '0.1.23'
99
id 'org.sonarqube' version '2.8'
10+
id 'au.com.dius.pact' version '4.1.7'
1011
}
1112

1213
ext['spring-security.version'] = '5.4.5'
@@ -39,6 +40,15 @@ sourceSets {
3940
resources.srcDir file('src/integrationTest/resources')
4041
}
4142

43+
contractTest {
44+
java {
45+
compileClasspath += main.output
46+
runtimeClasspath += main.output
47+
srcDir file('src/contractTest/java')
48+
}
49+
resources.srcDir file('src/contractTest/resources')
50+
}
51+
4252
}
4353

4454
tasks.withType(JavaCompile) {
@@ -137,6 +147,41 @@ jacocoTestReport {
137147
}
138148
}
139149

150+
project.ext {
151+
pactVersion = getCheckedOutGitCommitHash()
152+
}
153+
154+
task contract(type: Test) {
155+
description = 'Runs the consumer Pact tests'
156+
useJUnitPlatform()
157+
testClassesDirs = sourceSets.contractTest.output.classesDirs
158+
classpath = sourceSets.contractTest.runtimeClasspath
159+
include "uk/gov/hmcts/reform/managecase/api/controller/provider/**"
160+
161+
}
162+
163+
task runProviderPactVerification(type: Test) {
164+
testClassesDirs = sourceSets.contractTest.output.classesDirs
165+
classpath = sourceSets.contractTest.runtimeClasspath
166+
systemProperty 'pact.verifier.publishResults', System.getProperty('pact.verifier.publishResults')
167+
systemProperty 'pact.provider.version', project.pactVersion
168+
}
169+
170+
runProviderPactVerification.finalizedBy pactVerify
171+
172+
def getCheckedOutGitCommitHash() {
173+
'git rev-parse --verify --short HEAD'.execute().text.trim()
174+
}
175+
176+
pact {
177+
publish {
178+
pactDirectory = 'pacts'
179+
pactBrokerUrl = System.getenv("PACT_BROKER_FULL_URL") ?: 'http://localhost:80'
180+
tags = [System.getenv("PACT_BRANCH_NAME") ?:'Dev']
181+
version = project.pactVersion
182+
}
183+
}
184+
140185
project.tasks['sonarqube'].dependsOn jacocoTestReport
141186

142187
sonarqube {
@@ -220,7 +265,8 @@ def versions = [
220265
springBoot : springBoot.class.package.implementationVersion,
221266
springfoxSwagger: '3.0.0',
222267
restAssured : '4.3.1',
223-
lombok : '1.18.8'
268+
lombok : '1.18.8',
269+
pact_version : '4.1.7',
224270
]
225271

226272
ext.libraries = [
@@ -248,7 +294,6 @@ dependencies {
248294
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
249295
implementation group: 'commons-io', name: 'commons-io', version: '2.8.0'
250296
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-bootstrap', version: '3.0.1'
251-
252297
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
253298
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
254299
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
@@ -318,6 +363,27 @@ dependencies {
318363
functionalTestImplementation sourceSets.main.runtimeClasspath
319364
functionalTestImplementation sourceSets.test.runtimeClasspath
320365

366+
contractTestCompile group: 'au.com.dius.pact.consumer', name: 'junit5', version: versions.pact_version
367+
contractTestCompile group: 'au.com.dius.pact.consumer', name: 'java8', version: versions.pact_version
368+
contractTestRuntime group: 'au.com.dius.pact.consumer', name: 'junit5', version: versions.pact_version
369+
contractTestRuntime group: 'au.com.dius.pact.consumer', name: 'java8', version: versions.pact_version
370+
contractTestImplementation group: 'org.modelmapper', name: 'modelmapper', version: '2.3.7'
371+
372+
contractTestCompile group: 'au.com.dius.pact.provider', name: 'junit5', version: versions.pact_version
373+
contractTestCompile group: 'au.com.dius.pact.provider', name: 'spring', version: versions.pact_version
374+
contractTestCompile group: 'au.com.dius.pact.provider', name: 'junit5spring', version: versions.pact_version
375+
contractTestCompile("org.junit.jupiter:junit-jupiter-api:5.7.0")
376+
contractTestRuntime("org.junit.jupiter:junit-jupiter-engine:5.7.0")
377+
contractTestImplementation('org.junit.jupiter:junit-jupiter-api:5.7.0')
378+
contractTestRuntimeOnly "org.junit.platform:junit-platform-commons:1.7.0"
379+
contractTestImplementation group: 'uk.gov.hmcts.reform', name: 'idam-client', version: '1.5.5'
380+
contractTestImplementation group: 'uk.gov.hmcts.reform', name: 'service-auth-provider-client', version: '3.1.2'
381+
contractTestImplementation group: 'org.projectlombok', name: 'lombok', version: versions.lombok
382+
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: versions.lombok
383+
contractTestImplementation sourceSets.main.runtimeClasspath
384+
contractTestImplementation sourceSets.test.runtimeClasspath
385+
386+
321387
}
322388

323389
mainClassName = 'uk.gov.hmcts.reform.managecase.Application'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package uk.gov.hmcts.reform.managecase.api.controller.provider;
2+
3+
import org.modelmapper.ModelMapper;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.mock.mockito.MockBean;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Primary;
9+
import uk.gov.hmcts.reform.managecase.api.controller.CaseAssignmentController;
10+
import uk.gov.hmcts.reform.managecase.repository.DataStoreRepository;
11+
import uk.gov.hmcts.reform.managecase.repository.IdamRepository;
12+
import uk.gov.hmcts.reform.managecase.repository.PrdRepository;
13+
import uk.gov.hmcts.reform.managecase.security.SecurityUtils;
14+
import uk.gov.hmcts.reform.managecase.service.CaseAssignmentService;
15+
import uk.gov.hmcts.reform.managecase.util.JacksonUtils;
16+
17+
@Configuration
18+
public class ContractConfig {
19+
20+
@MockBean
21+
PrdRepository prdRepository;
22+
@MockBean
23+
DataStoreRepository dataStoreRepository;
24+
@MockBean
25+
IdamRepository idamRepository;
26+
@MockBean
27+
JacksonUtils jacksonUtils;
28+
@MockBean
29+
SecurityUtils securityUtils;
30+
31+
@Autowired
32+
ModelMapper modelMapper;
33+
34+
@Bean
35+
@Primary
36+
public CaseAssignmentController caseAssignmentController() {
37+
return new CaseAssignmentController(caseAssignmentService(), modelMapper);
38+
}
39+
40+
public CaseAssignmentService caseAssignmentService() {
41+
return new CaseAssignmentService(prdRepository, dataStoreRepository,
42+
idamRepository, jacksonUtils, securityUtils);
43+
}
44+
}

0 commit comments

Comments
 (0)