Skip to content

Commit

Permalink
Merge pull request #714 from hmcts/RDCC-6706-SRD_WireMock_response_en…
Browse files Browse the repository at this point in the history
…hancement

RDCC-6706 SRD:WireMock response enhancement
  • Loading branch information
manukundloo-hmcts authored Nov 13, 2023
2 parents a4cbef4 + aa1eb7e commit e9d0ed6
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.hmcts.reform.cwrdapi.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.extension.Parameters;
Expand Down Expand Up @@ -34,6 +35,7 @@
import uk.gov.hmcts.reform.lib.util.serenity5.SerenityTest;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
Expand All @@ -57,7 +59,7 @@
@SerenityTest
@WithTags({@WithTag("testType:Integration")})
@TestPropertySource(properties = {"S2S_URL=http://127.0.0.1:8990", "IDAM_URL:http://127.0.0.1:5000",
"USER_PROFILE_URL:http://127.0.0.1:8091", "spring.config.location=classpath:application-test.yml"})
"USER_PROFILE_URL:http://127.0.0.1:8091", "spring.config.location=classpath:application-test.yml"})
@ContextConfiguration(classes = {TestConfig.class, RestTemplateConfiguration.class})
public abstract class AuthorizationEnabledIntegrationTest extends SpringBootIntegrationTest {

Expand Down Expand Up @@ -122,72 +124,71 @@ public void setUpClient() {
public void setupIdamStubs() throws Exception {

s2sService.stubFor(get(urlEqualTo("/details"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withBody("rd_caseworker_ref_api")));
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withBody("rd_caseworker_ref_api")));


UserIdentifier userDetails = UserIdentifier.builder()
.id("%s")
.uid("%s")
.forename("Super")
.surname("User")
.email("[email protected]")
.accountStatus("active")
.roles(List.of("%s"))
.build();

sidamService.stubFor(get(urlPathMatching("/o/userinfo"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withBody("{"
+ " \"id\": \"%s\","
+ " \"uid\": \"%s\","
+ " \"forename\": \"Super\","
+ " \"surname\": \"User\","
+ " \"email\": \"[email protected]\","
+ " \"accountStatus\": \"active\","
+ " \"roles\": ["
+ " \"%s\""
+ " ]"
+ "}")
.withTransformers("user-token-response")));
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withBody(WireMockUtil.getObjectMapper().writeValueAsString(userDetails))
.withTransformers("user-token-response")));

mockHttpServerForOidc.stubFor(get(urlPathMatching("/jwks"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withBody(getDynamicJwksResponse())));
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withBody(getDynamicJwksResponse())));
}

public void userProfileGetUserWireMock(String idamStatus, String roles) {
public void userProfileGetUserWireMock(String idamStatus, String roles) throws JsonProcessingException {
userProfileService.stubFor(get(urlPathMatching("/v1/userprofile.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(200)
.withBody("{"
+ " \"userIdentifier\":\"" + UUID.randomUUID().toString() + "\","
+ " \"firstName\": \"prashanth\","
+ " \"lastName\": \"rao\","
+ " \"email\": \"[email protected]\","
+ " \"idamStatus\": \"" + idamStatus + "\","
+ " \"roles\": " + roles
+ "}")));
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(200)
.withBody(WireMockUtil.getObjectMapper().writeValueAsString(
getUserIdentifierData(idamStatus, roles))))
);
}

public void userProfileGetUserByIdWireMock(String idamId, Integer status) {
public void userProfileGetUserByIdWireMock(String idamId, Integer status) throws JsonProcessingException {
userProfileService.stubFor(get(urlPathMatching("/v1/userprofile.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(status)
.withBody("{"
+ " \"userIdentifier\":\"" + UUID.randomUUID().toString() + "\","
+ " \"firstName\": \"prashanth\","
+ " \"lastName\": \"rao\","
+ " \"email\": \"[email protected]\","
+ " \"idamStatus\": \"pending\","
+ " \"roles\": ["
+ " \"%s\""
+ " ]"
+ "}")));
.withBody(WireMockUtil.getObjectMapper().writeValueAsString(
getUserIdentifierData("pending", "%s"))))
);
}

private UserIdentifier getUserIdentifierData(String idamStatus, String roles) {
return UserIdentifier.builder()
.userIdentifier(UUID.randomUUID().toString())
.firstName("prashanth")
.lastName("rao")
.email("[email protected]")
.idamStatus(idamStatus)
.roles(List.of(roles))
.build();
}

public void userProfileDeleteUserWireMock() {
userProfileService.stubFor(delete(urlPathMatching("/v1/userprofile/users.*"))
Expand All @@ -209,11 +210,11 @@ public void modifyUserRoles() throws Exception {
userProfileRolesResponse.setRoleAdditionResponse(roleAdditionRes);

userProfileService.stubFor(put(urlPathMatching("/v1/userprofile.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(201)
.withBody(objectMapper.writeValueAsString(userProfileRolesResponse))));
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(201)
.withBody(objectMapper.writeValueAsString(userProfileRolesResponse))));
}

public void modifyUserStatus(int idamStatus) throws Exception {
Expand All @@ -225,26 +226,26 @@ public void modifyUserStatus(int idamStatus) throws Exception {
userProfileRolesResponse.setAttributeResponse(attributeResponse);

userProfileService.stubFor(put(urlPathMatching("/v1/userprofile.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(201)
.withBody(objectMapper.writeValueAsString(userProfileRolesResponse))));
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(201)
.withBody(objectMapper.writeValueAsString(userProfileRolesResponse))));
}

//removed UUID mock here and put in Test config,hence use this only for insert integration testing
//for update use insert response UUID in test or other mock methods
@BeforeEach
public void userProfilePostUserWireMock() {
userProfileService.stubFor(post(urlPathMatching("/v1/userprofile"))
.inScenario("")
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(201)
.withBody("{"
+ " \"idamRegistrationResponse\":\"201\""
+ "}")));
.inScenario("")
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(201)
.withBody("{"
+ " \"idamRegistrationResponse\":\"201\""
+ "}")));
}

@AfterEach
Expand All @@ -258,13 +259,13 @@ public void cleanupTestData() {
public void userProfileCreateUserWireMock(HttpStatus status) {

userProfileService.stubFor(post(urlPathMatching("/v1/userprofile"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(status.value())
.withBody("{"
+ " \"idamRegistrationResponse\":\"" + status.value() + "\""
+ "}")));
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withStatus(status.value())
.withBody("{"
+ " \"idamRegistrationResponse\":\"" + status.value() + "\""
+ "}")));
}

public void userProfilePostUserWireMockForStaffProfile(HttpStatus status) {
Expand Down Expand Up @@ -303,8 +304,8 @@ public Response transform(Request request, Response response, FileSource files,
formatResponse = format(formatResponse, tokenInfo.get(1), tokenInfo.get(1), tokenInfo.get(0));

return Response.Builder.like(response)
.but().body(formatResponse)
.build();
.but().body(formatResponse)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.web.context.ContextCleanupListener;
import uk.gov.hmcts.reform.cwrdapi.config.WireMockExtension;

import java.util.HashMap;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
Expand Down Expand Up @@ -46,16 +48,18 @@ private ClientRegistration clientRegistration() {

public void setUpMockServiceForOidc() throws JsonProcessingException, JOSEException {

HashMap<String,String> data = new HashMap<>();
data.put("issuer","http://0.0.0.0:6000/o");
data.put("jwks_uri","http://0.0.0.0:7000/jwks");

mockHttpServerForOidc.stubFor(get(urlPathMatching("/o/.well-known/openid-configuration"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("Connection", "close")
.withBody(
" {"
+ " \"issuer\": \"http://0.0.0.0:6000/o\","
+ " \"jwks_uri\": \"http://0.0.0.0:7000/jwks\" "
+ "}")));
WireMockUtil.getObjectMapper().writeValueAsString(data)
)));

if (!mockHttpServerForOidc.isRunning()) {
mockHttpServerForOidc.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.hmcts.reform.cwrdapi.util;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@Getter
@Builder
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserIdentifier {
private String id;
private String uid;
private String forename;
private String surname;
private String email;
private String accountStatus;
private List<String> roles;
private String userIdentifier;
private String firstName;
private String lastName;
private String idamStatus;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.gov.hmcts.reform.cwrdapi.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class WireMockUtil {

private WireMockUtil() {

}

public static ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;

}
}

0 comments on commit e9d0ed6

Please sign in to comment.