Skip to content

Commit

Permalink
RDCC-2963: PACT and Functional test to complete for of JRD POST API (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ishratt authored Jul 27, 2021
1 parent 12d6061 commit 6575dd7
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 58 deletions.
25 changes: 25 additions & 0 deletions Jenkinsfile_CNP
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!groovy
import uk.gov.hmcts.contino.AppPipelineDsl

@Library("Infrastructure")

def type = "java"
def product = "rd"
def component = "judicial-api"

def appVault = "rd-aat"
def secrets = [
's2s-${env}': [
secret('microservicekey-rd-judicial-api', 'S2S_SECRET')
Expand All @@ -15,6 +17,13 @@ def secrets = [
secret('OAUTH2-CLIENT-AUTH', 'OAUTH2_CLIENT_AUTH'),
secret('OAUTH2-CLIENT-ID', 'OAUTH2_CLIENT_ID'),
secret('LD-SDK-KEY', 'LD_SDK_KEY')
],
(appVault): [
secret('judicial-api-POSTGRES-HOST', 'DATABASE_HOST'),
secret('judicial-api-POSTGRES-PORT', 'DATABASE_PORT'),
secret('judicial-api-POSTGRES-DATABASE', 'DATABASE_NAME'),
secret('judicial-api-POSTGRES-USER', 'DATABASE_USER'),
secret('judicial-api-POSTGRES-PASS', 'DATABASE_PASS')
]
]

Expand All @@ -35,6 +44,12 @@ def vaultOverrides = [
// Configure branches to sync with master branch
def branchesToSync = ['demo', 'ithc', 'perftest']

// Vars for Kubernetes PACT
env.PACT_BROKER_FULL_URL = 'https://pact-broker.platform.hmcts.net'
env.PACT_BROKER_URL = "pact-broker.platform.hmcts.net"
env.PACT_BROKER_PORT = "443"
env.PACT_BROKER_SCHEME = "https"

withPipeline(type, product, component) {

overrideVaultEnvironments(vaultOverrides)
Expand Down Expand Up @@ -66,4 +81,14 @@ withPipeline(type, product, component) {
after('functionalTest:aat') {
steps.archiveArtifacts allowEmptyArchive: true, artifacts: '**/site/serenity/**/*'
}

after('pact-provider-verification') {
steps.archiveArtifacts allowEmptyArchive: true, artifacts: 'build/reports/tests/**/*'
}


enablePactAs([
AppPipelineDsl.PactRoles.PROVIDER
])

}
83 changes: 78 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def versions = [
springfoxSwagger : '2.9.2',
restAssured : '4.3.3',
jackson : '2.12.1',
launchDarklySdk : '5.2.2'
launchDarklySdk : '5.2.2',
pact_version : '4.1.7'
]

mainClassName = 'uk.gov.hmcts.reform.judicialapi.JudicialApplication'
Expand Down Expand Up @@ -86,6 +87,24 @@ sourceSets {
}
resources.srcDir file('src/smokeTest/resources')
}

contractTest {
java {
compileClasspath += main.output
runtimeClasspath += main.output
srcDir file('src/contractTest/java')
}
resources.srcDir file('src/contractTest/resources')
}

pactTest {
java {
compileClasspath += main.output
runtimeClasspath += main.output
srcDir file('src/pactTest/java')
}
resources.srcDir file('src/pactTest/resources')
}
}

idea {
Expand All @@ -99,6 +118,44 @@ idea {
}
}

project.ext {
pactVersion = getCheckedOutGitCommitHash()
}

def getCheckedOutGitCommitHash() {
'git rev-parse --verify --short HEAD'.execute().text.trim()
}

task contract(type: Test) {
description = 'Runs the consumer Pact tests'
useJUnitPlatform()
testClassesDirs = sourceSets.contractTest.output.classesDirs
classpath = sourceSets.contractTest.runtimeClasspath
include "uk/gov/hmcts/reform/judicialapi/**"
}

task runProviderPactVerification(type: Test) {
useJUnitPlatform()
logger.lifecycle("Runs pact Tests")
testClassesDirs = sourceSets.contractTest.output.classesDirs
classpath = sourceSets.contractTest.runtimeClasspath
systemProperty 'pact.verifier.publishResults', System.getProperty('pact.verifier.publishResults')
systemProperty 'pact.provider.version', project.pactVersion
}

runProviderPactVerification.dependsOn contract

runProviderPactVerification.finalizedBy pactVerify

pact {
publish {
pactDirectory = 'pacts'
pactBrokerUrl = System.getenv("PACT_BROKER_FULL_URL") ?: 'http://localhost:80'
tags = [System.getenv("PACT_BRANCH_NAME") ?:'Dev']
version = project.pactVersion
}
}

configurations {
integrationTestImplementation.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
Expand Down Expand Up @@ -251,7 +308,7 @@ dependencies {
compile group: 'com.launchdarkly', name: 'launchdarkly-java-server-sdk', version: versions.launchDarklySdk
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
compile group: 'io.jsonwebtoken', name: 'jjwt', version:'0.9.1'
compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
compile group: 'com.nimbusds', name: 'nimbus-jose-jwt', version: '8.20'
implementation "io.github.openfeign:feign-httpclient:11.0"
implementation 'org.springframework.boot:spring-boot-starter-validation'
Expand All @@ -267,7 +324,7 @@ dependencies {
exclude group: "org.hamcrest", module: "hamcrest-core"
exclude group: "org.hamcrest", module: "hamcrest-library"
}

testImplementation group: 'com.h2database', name: 'h2'
testImplementation "com.github.tomakehurst:wiremock:2.21.0"
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.4.6'
Expand All @@ -289,13 +346,13 @@ dependencies {


testImplementation(group: 'org.yaml', name: 'snakeyaml') {
version{
version {
strictly '1.25'
}
}

integrationTestImplementation(group: 'org.yaml', name: 'snakeyaml') {
version{
version {
strictly '1.23'
}
}
Expand All @@ -306,6 +363,16 @@ dependencies {
}
}

contractTestCompile group: 'au.com.dius.pact.consumer', name: 'junit5', version: versions.pact_version
contractTestRuntime group: 'au.com.dius.pact.consumer', name: 'junit5', version: versions.pact_version

contractTestCompile("org.junit.jupiter:junit-jupiter-api:5.7.1")
contractTestRuntime("org.junit.jupiter:junit-jupiter-engine:5.7.1")
contractTestImplementation('org.junit.jupiter:junit-jupiter-api:5.7.1')
contractTestImplementation group: 'au.com.dius.pact.provider', name: 'junit5', version: versions.pact_version
contractTestImplementation group: 'au.com.dius.pact.provider', name: 'spring', version: versions.pact_version
contractTestImplementation group: 'au.com.dius.pact.provider', name: 'junit5spring', version: versions.pact_version

integrationTestImplementation sourceSets.main.runtimeClasspath
integrationTestImplementation sourceSets.test.runtimeClasspath

Expand All @@ -314,6 +381,12 @@ dependencies {

smokeTestImplementation sourceSets.main.runtimeClasspath
smokeTestImplementation sourceSets.test.runtimeClasspath

contractTestCompile sourceSets.main.runtimeClasspath
contractTestCompile sourceSets.test.runtimeClasspath

pactTestCompile sourceSets.main.runtimeClasspath
pactTestCompile sourceSets.test.runtimeClasspath
}

dependencyManagement {
Expand Down
2 changes: 1 addition & 1 deletion charts/rd-judicial-api/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
appVersion: "0.1"
description: Reference data service for judiciary
name: rd-judicial-api
version: 0.0.12
version: 0.0.13
maintainers:
- name: Reference Data Team
dependencies:
Expand Down
1 change: 1 addition & 0 deletions charts/rd-judicial-api/values.preview.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ java:
POSTGRES_NAME: "{{ .Values.postgresql.postgresqlDatabase}}"
POSTGRES_USERNAME: "{{ .Values.postgresql.postgresqlUsername}}"
POSTGRES_PASSWORD: "{{ .Values.postgresql.postgresqlPassword}}"
JUDICIAL_DB: classpath:db/migration,classpath:db/migrationAks
LAUNCH_DARKLY_ENV: "preview"
ENVIRONMENT_NAME: "preview"
postgresql:
Expand Down
1 change: 1 addition & 0 deletions charts/rd-judicial-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ java:
IDAM_URL: https://idam-api.{{ .Values.global.environment }}.platform.hmcts.net
OIDC_ISSUER_URL: https://forgerock-am.service.core-compute-idam-{{ .Values.global.environment }}.internal:8443/openam/oauth2/hmcts
OPEN_ID_API_BASE_URI: https://idam-web-public.{{ .Values.global.environment }}.platform.hmcts.net/o
JUDICIAL_DB: classpath:db/migration
LAUNCH_DARKLY_ENV: "{{ .Values.global.environment }}"
ENVIRONMENT_NAME: "{{ .Values.global.environment }}"
image: 'hmctspublic.azurecr.io/rd/judicial-api:latest'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package uk.gov.hmcts.reform.judicialapi.provider;

import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify;
import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.State;
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
import au.com.dius.pact.provider.junitsupport.loader.VersionSelector;
import au.com.dius.pact.provider.spring.junit5.MockMvcTestTarget;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.judicialapi.controller.JrdUsersController;
import uk.gov.hmcts.reform.judicialapi.domain.Appointment;
import uk.gov.hmcts.reform.judicialapi.domain.Authorisation;
import uk.gov.hmcts.reform.judicialapi.domain.BaseLocationType;
import uk.gov.hmcts.reform.judicialapi.domain.RegionType;
import uk.gov.hmcts.reform.judicialapi.domain.UserProfile;
import uk.gov.hmcts.reform.judicialapi.repository.UserProfileRepository;
import uk.gov.hmcts.reform.judicialapi.service.impl.JudicialUserServiceImpl;


import java.util.Collections;
import java.util.List;

import static java.util.Objects.nonNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
@Provider("referenceData_judicial")
@PactBroker(scheme = "${PACT_BROKER_SCHEME:http}",
host = "${PACT_BROKER_URL:localhost}",
port = "${PACT_BROKER_PORT:80}", consumerVersionSelectors = {
@VersionSelector(tag = "master")})
@ContextConfiguration(classes = {JrdUsersController.class, JudicialUserServiceImpl.class})
@TestPropertySource(properties = {"defaultPageSize=10"})
@IgnoreNoPactsToVerify
public class JrdApiProviderTest {

@Autowired
JrdUsersController jrdUsersController;

@MockBean
UserProfileRepository userProfileRepository;

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
if (context != null) {
context.verifyInteraction();
}
}

@BeforeEach
void before(PactVerificationContext context) {
MockMvcTestTarget testTarget = new MockMvcTestTarget();
testTarget.setControllers(jrdUsersController);
if (nonNull(context)) {
context.setTarget(testTarget);
}

}

@State({"User profile details exist"})
public void toReturnUserProfilesDetails() {
BaseLocationType baseLocationType = new BaseLocationType();
baseLocationType.setBaseLocationId("1");

RegionType regionType = new RegionType();
regionType.setRegionId("1");
regionType.setRegionDescEn("default");

Appointment appointment = new Appointment();
appointment.setOfficeAppointmentId(12L);
appointment.setBaseLocationType(baseLocationType);
appointment.setRegionType(regionType);
appointment.setIsPrincipleAppointment(Boolean.TRUE);

Authorisation authorisation = new Authorisation();
authorisation.setOfficeAuthId(1234L);
authorisation.setJurisdiction("Languages");

List<Authorisation> authorisations = Collections.singletonList(authorisation);
List<Appointment> appointments = Collections.singletonList(appointment);

UserProfile userProfile = new UserProfile();
userProfile.setSidamId("44362987-4b00-f2e7-4ff8-761b87f16bf9");
userProfile.setAppointments(appointments);
userProfile.setAuthorisations(authorisations);

List<UserProfile> userProfiles = Collections.singletonList(userProfile);

Page<UserProfile> pagedUserProfiles = new PageImpl<>(userProfiles);

when(userProfileRepository.findBySidamIdIn(anyList(),any())).thenReturn(pagedUserProfiles);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -18,16 +17,11 @@
import uk.gov.hmcts.reform.judicialapi.client.S2sClient;
import uk.gov.hmcts.reform.judicialapi.config.Oauth2;
import uk.gov.hmcts.reform.judicialapi.config.TestConfigProperties;
import uk.gov.hmcts.reform.judicialapi.controller.request.UserRequest;
import uk.gov.hmcts.reform.judicialapi.controller.response.OrmResponse;
import uk.gov.hmcts.reform.judicialapi.idam.IdamOpenIdClient;


import java.util.List;

import static java.util.Objects.isNull;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.codehaus.groovy.runtime.InvokerHelper.asList;

@ContextConfiguration(classes = {TestConfigProperties.class, Oauth2.class})
@ComponentScan("uk.gov.hmcts.reform.judicialapi")
Expand Down Expand Up @@ -97,17 +91,4 @@ public static String getS2sToken() {
}


public List fetchUserProfiles(UserRequest userRequest, int pageSize, int pageNumber, int expectedResponse) {
Response fetchResponse = judicialApiClient.getMultipleAuthHeadersInternal(ROLE_JRD_SYSTEM_USER)
.body(userRequest).log().body(true)
.post("/refdata/judicial/users/fetch?page_size=" + pageSize + "&page_number=" + pageNumber)
.andReturn();

log.info("JRD get users response: {}", fetchResponse.getStatusCode());

fetchResponse.then()
.assertThat()
.statusCode(expectedResponse);
return asList(fetchResponse.getBody().as(OrmResponse.class));
}
}
Loading

0 comments on commit 6575dd7

Please sign in to comment.