Skip to content

Commit

Permalink
[RDCC-7094]Added more testcases for V1 and new test cases for V2
Browse files Browse the repository at this point in the history
  • Loading branch information
manukundloo-hmcts committed Oct 23, 2023
1 parent 476a6a7 commit 2d44e50
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.core.model.RequestResponsePact;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import groovy.util.logging.Slf4j;
import io.restassured.http.ContentType;
import net.serenitybdd.rest.SerenityRest;
Expand All @@ -23,12 +23,11 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.gov.hmcts.reform.cwrdapi.client.domain.JrdUserRequest;

import java.util.Map;
import java.util.Set;

import static io.pactfoundation.consumer.dsl.LambdaDsl.newJsonArray;
import static io.pactfoundation.consumer.dsl.LambdaDsl.newJsonBody;

@Slf4j
@ExtendWith(PactConsumerTestExt.class)
Expand All @@ -53,6 +52,79 @@ void teardown() {
Executor.closeIdleConnections();
}


//To run the Pact tests please uncommnet all Pact Test annotations
//@Pact(provider = "referenceData_judicial", consumer = "JRD_API_V1_Search_ConsumerTest")
public RequestResponsePact searchJrdProfiles(PactDslWithProvider builder) throws JsonProcessingException {

return builder
.given("User profile details exist for the search request provided")
.uponReceiving("the api returns judicial user profiles "
+ "based on the provided list of user ids")
.path(JRD_GET_PROFILES_URL + "/search")
.body(createJrdProfileSearchRequest())
.method(HttpMethod.POST.toString())
.willRespondWith()
.status(HttpStatus.OK.value())
.headers(getResponseHeaders())
.body(searchJrdProfilesResponse())
.toPact();
}

//@Test
//@PactTestFor(pactMethod = "searchJrdProfiles")
void searchJrdProfiles(MockServer mockServer)
throws JSONException {
var actualResponseBody =
SerenityRest
.given()
.headers(getHttpHeaders())
.body(createJrdProfileSearchRequest().toString())
.contentType(ContentType.JSON)
.post(mockServer.getUrl() + JRD_GET_PROFILES_URL + "/search")
.then()
.log().all().extract().asString();

JSONArray response = new JSONArray(actualResponseBody);
Assertions.assertThat(response).isNotNull();

}

//@Pact(provider = "referenceData_judicial", consumer = "JRD_Fetch_API_ConsumerTest")
public RequestResponsePact getJudicialUserProfiles(PactDslWithProvider builder) throws JsonProcessingException {

return builder
.given("User profile details exist")
.uponReceiving("the api returns judicial user profiles ")
.path(JRD_GET_PROFILES_URL + "/fetch")
.body(createJrdProfileUserRequest())
.method(HttpMethod.POST.toString())
.willRespondWith()
.status(HttpStatus.OK.value())
.headers(getResponseHeaders())
.body(createJrdFetchProfilesResponse())
.toPact();
}

//@Test
//@PactTestFor(pactMethod = "getJudicialUserProfiles")
void executeGetJudicialUserProfiles(MockServer mockServer)
throws JSONException {
var actualResponseBody =
SerenityRest
.given()
.headers(getHttpHeaders())
.body(createJrdProfileUserRequest().toString())
.contentType(ContentType.JSON)
.post(mockServer.getUrl() + JRD_GET_PROFILES_URL + "/fetch")
.then()
.log().all().extract().asString();

JSONArray response = new JSONArray(actualResponseBody);
Assertions.assertThat(response).isNotNull();

}

//@Pact(provider = "referenceData_judicial", consumer = "JRD_API_ConsumerTest")
public RequestResponsePact getJrdProfilesListOfIds(PactDslWithProvider builder) throws JsonProcessingException {

Expand All @@ -61,8 +133,7 @@ public RequestResponsePact getJrdProfilesListOfIds(PactDslWithProvider builder)
.uponReceiving("the api returns judicial user profiles "
+ "based on the provided list of user ids")
.path(JRD_GET_PROFILES_URL)
.body(new ObjectMapper().writeValueAsString(
JrdUserRequest.builder().sidamIds(Set.of(SIDAM_ID)).build()))
.body(createJrdProfileUpdateRequest())
.method(HttpMethod.POST.toString())
.willRespondWith()
.status(HttpStatus.OK.value())
Expand All @@ -79,8 +150,7 @@ void executeGetJrdProfilesListOfIds(MockServer mockServer)
SerenityRest
.given()
.headers(getHttpHeaders())
.body(JrdUserRequest.builder().sidamIds(
Set.of(SIDAM_ID)).build())
.body(createJrdProfileUpdateRequest().toString())
.contentType(ContentType.JSON)
.post(mockServer.getUrl() + JRD_GET_PROFILES_URL)
.then()
Expand All @@ -99,8 +169,7 @@ public RequestResponsePact getJrdProfilesServiceName(PactDslWithProvider builder
.uponReceiving("the api returns judicial user profiles "
+ "based on the provided service name")
.path(JRD_GET_PROFILES_URL)
.body(new ObjectMapper().writeValueAsString(
JrdUserRequest.builder().ccdServiceNames("CMC").build()))
.body(createJrdProfileForServiceNameRequest())
.method(HttpMethod.POST.toString())
.willRespondWith()
.status(HttpStatus.OK.value())
Expand All @@ -117,7 +186,7 @@ void executeGetJrdProfilesServiceName(MockServer mockServer)
SerenityRest
.given()
.headers(getHttpHeaders())
.body(JrdUserRequest.builder().ccdServiceNames("CMC").build())
.body(createJrdProfileForServiceNameRequest().toString())
.contentType(ContentType.JSON)
.post(mockServer.getUrl() + JRD_GET_PROFILES_URL)
.then()
Expand All @@ -142,6 +211,46 @@ private DslPart createJrdProfilesResponse() {
)).build();
}

private DslPart createJrdFetchProfilesResponse() {
return newJsonArray(o -> o.object(ob -> ob
.stringType("idamId", SIDAM_ID)
.minArrayLike("appointments", 1, r -> r
.stringType("appointmentId", "1")
)
.minArrayLike("authorisations", 1, r -> r

.stringType("authorisationId", "1234")
)
)).build();
}


private DslPart createJrdProfileUpdateRequest() {
return newJsonBody(o -> o
.stringType("ccdServiceName", null)
.stringType("object_ids", null)
.minArrayLike("sidam_ids", 1,
PactDslJsonRootValue.stringType("44362987-4b00-f2e7-4ff8-761b87f16bf9"),1)
).build();
}


private DslPart createJrdProfileUserRequest() {
return newJsonBody(o -> o
.minArrayLike("userIds", 1,
PactDslJsonRootValue.stringType("44362987-4b00-f2e7-4ff8-761b87f16bf9"),1)
).build();
}

private DslPart createJrdProfileForServiceNameRequest() {
return newJsonBody(o -> o
.stringType("ccdServiceName", "CMC")
.stringType("object_ids", null)
.minArrayLike("sidam_ids", 1,
PactDslJsonRootValue.stringType("44362987-4b00-f2e7-4ff8-761b87f16bf9"),1)
).build();
}

@NotNull
private Map<String, String> getResponseHeaders() {
Map<String, String> responseHeaders = Map.of("Content-Type", "application/json");
Expand All @@ -154,4 +263,24 @@ private HttpHeaders getHttpHeaders() {
headers.add("Authorization", "Bearer " + "2345");
return headers;
}

private DslPart createJrdProfileSearchRequest() {
return newJsonBody(o -> o
.stringType("searchString", "testFullName")
.stringType("serviceCode", "CMC")
.stringType("location", "location")
).build();
}

private DslPart searchJrdProfilesResponse() {
return newJsonArray(o -> o.object(ob -> ob
.stringType("idamId", SIDAM_ID)
.stringType("fullName", "testFullName")
.stringType("knownAs", "testKnownAs")
.stringType("surname", "surname")
.stringType("emailId", "test@test.com")
.stringType("title", "Family Judge")
.stringType("personalCode", "1234")))
.build();
}
}
Loading

0 comments on commit 2d44e50

Please sign in to comment.