Skip to content

Commit

Permalink
Fetch Staff Profile By Id (#665)
Browse files Browse the repository at this point in the history
* Fetch Staff Profile By Id

* Fetch Staff Profile By Id

* Fetch Staff Profile By Id

* Fetch Staff Profile By Id

* checkstyle fixes

* unit test

* unit test

* unit test

* unit test

* unit test

* sonar fix

* Integration Test

* Dependency check

* Dependency check

* checkstyle fix

* Integration test cases

* Integration test cases

* swagger change response

* checkstyle fix

* assert statement in integration

* check style fix

* Functional Test

* Functional Test

* Functional Test

* Functional Test

* Functional Test

* LaunchDarkly configuration

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
sahitya-desireddy and github-actions[bot] authored Mar 13, 2023
1 parent 770a30d commit cd2ae4b
Show file tree
Hide file tree
Showing 14 changed files with 553 additions and 52 deletions.
11 changes: 7 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def versions = [
pact_version : '4.1.7',
launchDarklySdk : "5.10.2",
restAssured : '4.3.3',
jackson : '2.14.0-rc1',
jackson : '2.14.2',
log4j : '2.17.1',
springVersion : '5.3.20',
poi : '4.1.2',
Expand Down Expand Up @@ -370,7 +370,7 @@ dependencies {

implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: versions.jackson
implementation group: 'io.jsonwebtoken', name: 'jjwt', version:'0.9.1'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: versions.jackson
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: versions.jackson
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: versions.jackson
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: versions.jackson
implementation group: 'com.fasterxml.jackson', name: 'jackson-bom', version: '2.13.2.20220324', ext: 'pom'
Expand Down Expand Up @@ -412,7 +412,9 @@ dependencies {
implementation group: 'com.vladmihalcea', name: 'hibernate-types-52', version: '2.19.2'
implementation group: 'com.fasterxml.jackson.module', name: 'jackson-module-jaxb-annotations', version: '2.13.4'
implementation group: 'org.yaml', name: 'snakeyaml', version: '1.33'

// https://mvnrepository.com/artifact/net.minidev/json-smart
implementation group: 'net.minidev', name: 'json-smart', version: '2.4.8'


testImplementation ('com.github.hmcts:rd-commons-lib:v0.0.13'){
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-web'
Expand Down Expand Up @@ -479,7 +481,8 @@ dependencies {
testImplementation('com.opentable.components:otj-pg-embedded:0.13.4')
testImplementation group: 'org.codehaus.groovy', name: 'groovy', version: '3.0.10'
testImplementation group: 'org.codehaus.groovy', name: 'groovy-xml', version: '2.5.14'
testImplementation group: 'org.codehaus.groovy', name: 'groovy-json', version: '2.5.14'
// https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-json
implementation group: 'org.codehaus.groovy', name: 'groovy-json', version: '3.0.15'
testImplementation group: 'org.springframework', name: 'spring-test', version: '5.3.17'
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.10'
testImplementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.10'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package uk.gov.hmcts.reform.cwrdapi;

import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;
import net.thucydides.core.annotations.WithTag;
import net.thucydides.core.annotations.WithTags;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ActiveProfiles;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.SkillsRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.StaffProfileCreationRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.SearchStaffUserByIdResponse;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.StaffProfileCreationResponse;
import uk.gov.hmcts.reform.cwrdapi.idam.IdamOpenIdClient;
import uk.gov.hmcts.reform.cwrdapi.util.FeatureToggleConditionExtension;
import uk.gov.hmcts.reform.cwrdapi.util.ToggleEnable;
import uk.gov.hmcts.reform.lib.util.serenity5.SerenityTest;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@ComponentScan("uk.gov.hmcts.reform.cwrdapi")
@WithTags({@WithTag("testType:Functional")})
@ActiveProfiles("functional")
@SerenityTest
@SpringBootTest
@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FetchStaffProfileByIdFunctionalTest extends AuthorizationFunctionalTest {

public static final String FETCH_STAFF_PROFILE_BY_ID = "StaffRefDataController.fetchStaffProfileById";
public static final String STAFF_PROFILE_URL = "/refdata/case-worker";

@Test
@ToggleEnable(mapKey = FETCH_STAFF_PROFILE_BY_ID, withFeature = true)
@ExtendWith(FeatureToggleConditionExtension.class)
void should_fetchStaffProfile_By_ID_200() {
SkillsRequest skillsRequest = SkillsRequest
.skillsRequest()
.skillId(9)
.description("testskill1")
.skillCode("SKILL:AAA7:TEST1")
.build();

StaffProfileCreationRequest staffProfileCreationRequest = caseWorkerApiClient
.createStaffProfileCreationRequest();
staffProfileCreationRequest.setSkills(List.of(skillsRequest));

Response response = caseWorkerApiClient.createStaffUserProfile(staffProfileCreationRequest);

assertNotNull(response);
StaffProfileCreationResponse staffProfileCreationResponse =
response.getBody().as(StaffProfileCreationResponse.class);
assertNotNull(staffProfileCreationResponse);
assertNotNull(staffProfileCreationResponse.getCaseWorkerId());

String firstCaseworkerId = staffProfileCreationResponse.getCaseWorkerId();

IdamOpenIdClient.cwdStaffAdminUserToken = null;
Response fetchResponse = caseWorkerApiClient.getMultipleAuthHeadersInternal(ROLE_STAFF_ADMIN)
.get(STAFF_PROFILE_URL + "/profile/search-by-id?id=" + firstCaseworkerId)
.andReturn();
fetchResponse.then()
.assertThat()
.statusCode(200);

SearchStaffUserByIdResponse caseWorkerProfile =
fetchResponse.getBody().as(SearchStaffUserByIdResponse.class);

assertThat(caseWorkerProfile).isNotNull();

assertEquals(firstCaseworkerId, caseWorkerProfile.getCaseWorkerId());
assertEquals(staffProfileCreationRequest.getFirstName(), caseWorkerProfile.getFirstName());
assertEquals(staffProfileCreationRequest.getLastName(), caseWorkerProfile.getLastName());
assertEquals(staffProfileCreationRequest.getEmailId(), caseWorkerProfile.getEmailId());
assertEquals(staffProfileCreationRequest.isStaffAdmin(), caseWorkerProfile.isStaffAdmin());
assertEquals(staffProfileCreationRequest.getBaseLocations().size(),
caseWorkerProfile.getBaseLocations().size());
assertEquals(staffProfileCreationRequest.getBaseLocations().get(0).getLocation(),
caseWorkerProfile.getBaseLocations().get(0).getLocationName());
assertEquals(staffProfileCreationRequest.getServices().size(), caseWorkerProfile.getServices().size());
assertEquals(staffProfileCreationRequest.getServices().get(0).getService(),
caseWorkerProfile.getServices().get(0).getService());
assertEquals(staffProfileCreationRequest.getRoles().size(), caseWorkerProfile.getRoles().size());
assertEquals(staffProfileCreationRequest.getRoles().get(0).getRole(),
caseWorkerProfile.getRoles().get(0).getRoleName());
assertThat(caseWorkerProfile.getSkills().size()).isGreaterThanOrEqualTo(1);
assertEquals(staffProfileCreationRequest.getSkills().get(0).getSkillId(),
caseWorkerProfile.getSkills().get(0).getSkillId());
assertEquals(staffProfileCreationRequest.getSkills().get(0).getDescription(),
caseWorkerProfile.getSkills().get(0).getDescription());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package uk.gov.hmcts.reform.cwrdapi;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
import org.testcontainers.shaded.com.google.common.collect.ImmutableSet;
import uk.gov.hmcts.reform.cwrdapi.controllers.advice.ErrorResponse;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.CaseWorkerLocationRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.CaseWorkerRoleRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.CaseWorkerWorkAreaRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.CaseWorkersProfileCreationRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.SearchStaffUserByIdResponse;
import uk.gov.hmcts.reform.cwrdapi.util.AuthorizationEnabledIntegrationTest;
import uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants;
import uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerReferenceDataClient;

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

import static org.apache.logging.log4j.util.Strings.EMPTY;
import static org.assertj.core.api.Assertions.assertThat;
import static uk.gov.hmcts.reform.cwrdapi.CreateStaffReferenceProfileBasicSearchTest.ROLE_CWD_ADMIN;
import static uk.gov.hmcts.reform.cwrdapi.UpdateStaffReferenceProfileTest.ROLE_STAFF_ADMIN;

public class FetchStaffProfileByIdIntegrationTest extends AuthorizationEnabledIntegrationTest {

List<CaseWorkersProfileCreationRequest> caseWorkersProfileCreationRequests;


@BeforeEach
public void setUpClient() {
super.setUpClient();
Set<String> roles = ImmutableSet.of(" tribunal_case_worker ");
List<CaseWorkerRoleRequest> caseWorkerRoleRequests =
ImmutableList.of(CaseWorkerRoleRequest.caseWorkerRoleRequest().role(" role ").isPrimaryFlag(true)
.build());

List<CaseWorkerLocationRequest> caseWorkerLocationRequests = ImmutableList.of(CaseWorkerLocationRequest
.caseWorkersLocationRequest()
.isPrimaryFlag(true).locationId(1)
.location(" location ").build());

List<CaseWorkerWorkAreaRequest> caseWorkerAreaRequests = ImmutableList.of(CaseWorkerWorkAreaRequest
.caseWorkerWorkAreaRequest()
.areaOfWork(" areaOfWork ").serviceCode(" serviceCode ")
.build());

caseWorkersProfileCreationRequests = ImmutableList.of(CaseWorkersProfileCreationRequest
.caseWorkersProfileCreationRequest()
.firstName(" firstName ")
.lastName(" lastName ")
.emailId("[email protected]")
.regionId(1).userType("CTSC")
.region("region")
.suspended(false)
.taskSupervisor(true)
.caseAllocator(false)
.roles(caseWorkerRoleRequests)
.idamRoles(roles)
.baseLocations(caseWorkerLocationRequests)
.workerWorkAreaRequests(caseWorkerAreaRequests)
.build());
}

@BeforeAll
public static void setup() {
CaseWorkerReferenceDataClient.setBearerToken(EMPTY);
}

@AfterAll
public static void tearDown() {
CaseWorkerReferenceDataClient.setBearerToken(EMPTY);
}

@Test
void should_return_status_code_404_when_profile_not_found_in_cw() throws JsonProcessingException {
String searchString = "?id=123";
String path = "/profile/search-by-id";

Map<String, Object> response = (Map<String, Object>) caseworkerReferenceDataClient
.fetchStaffUserById(SearchStaffUserByIdResponse.class, path + searchString, ROLE_STAFF_ADMIN);
assertThat(response).containsEntry("http_status", HttpStatus.NOT_FOUND);
assertThat(((ErrorResponse)response.get("response_body")).getErrorDescription())
.contains(CaseWorkerConstants.NO_DATA_FOUND);
}


@Test
void should_return_status_code_404_when_profile_not_found_in_up() throws JsonProcessingException {
caseworkerReferenceDataClient.setBearerToken(EMPTY);
Map<String, Object> createResponse = caseworkerReferenceDataClient
.createCaseWorkerProfile(caseWorkersProfileCreationRequests, ROLE_CWD_ADMIN);
assertThat(createResponse).containsEntry("http_status", "201 CREATED");
String caseWorkerId = ((List)((Map)createResponse.get("body")).get("case_worker_ids")).get(0).toString();
Assertions.assertNotNull(caseWorkerId);

String path = "/profile/search-by-id";
String searchString = "?id=" + caseWorkerId;
caseworkerReferenceDataClient.setBearerToken(null);
userProfileGetUserByIdWireMock(caseWorkerId, 404);
Map<String, Object> getResponse = (Map<String, Object>) caseworkerReferenceDataClient
.fetchStaffUserById(SearchStaffUserByIdResponse.class, path + searchString, ROLE_STAFF_ADMIN);
assertThat(getResponse).containsEntry("http_status", HttpStatus.NOT_FOUND);
assertThat(((ErrorResponse)getResponse.get("response_body")).getErrorDescription())
.contains(CaseWorkerConstants.NO_DATA_FOUND);
}

@Test
void should_return_status_code_200_when_profile_found() throws JsonProcessingException {
Map<String, Object> createResponse = caseworkerReferenceDataClient
.createCaseWorkerProfile(caseWorkersProfileCreationRequests, ROLE_CWD_ADMIN);
assertThat(createResponse).containsEntry("http_status", "201 CREATED");
String caseWorkerId = ((List)((Map)createResponse.get("body")).get("case_worker_ids")).get(0).toString();
Assertions.assertNotNull(caseWorkerId);
String path = "/profile/search-by-id";
String searchString = "?id=" + caseWorkerId;
caseworkerReferenceDataClient.setBearerToken(null);

userProfileGetUserByIdWireMock(caseWorkerId, 200);
SearchStaffUserByIdResponse getResponse = (SearchStaffUserByIdResponse)caseworkerReferenceDataClient
.fetchStaffUserById(SearchStaffUserByIdResponse.class, path + searchString, ROLE_STAFF_ADMIN);
Assertions.assertNotNull(getResponse);
Assertions.assertEquals(caseWorkerId, getResponse.getCaseWorkerId());
Assertions.assertNotNull(getResponse.getIdamStatus());
Assertions.assertEquals("pending", getResponse.getIdamStatus());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ public void userProfileGetUserWireMock(String idamStatus, String roles) {
+ "}")));
}

public void userProfileGetUserByIdWireMock(String idamId, Integer status) {
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\""
+ " ]"
+ "}")));
}


public void userProfileDeleteUserWireMock() {
userProfileService.stubFor(delete(urlPathMatching("/v1/userprofile/users.*"))
.willReturn(aResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
import uk.gov.hmcts.reform.cwrdapi.client.domain.ServiceRoleMapping;
import uk.gov.hmcts.reform.cwrdapi.controllers.advice.ErrorResponse;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.CaseWorkerLocationRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.CaseWorkerServicesRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.CaseWorkersProfileCreationRequest;
Expand Down Expand Up @@ -121,7 +122,7 @@ private Object mapServiceSkillsIdResponse(ResponseEntity<Object> responseEntity,
Map<String, Object> errorResponseMap = new HashMap<>();
errorResponseMap.put(
"response_body",
objectMapper.readValue(responseEntity.getBody().toString(), clazz)
objectMapper.readValue(responseEntity.getBody().toString(), ErrorResponse.class)
);
errorResponseMap.put("http_status", status);
return errorResponseMap;
Expand Down Expand Up @@ -573,4 +574,10 @@ public StaffProfileCreationRequest createStaffProfileCreationRequest() {
public Map<String, Object> updateStaffProfile(StaffProfileCreationRequest request, String role) {
return putRequest(baseUrl + "/profile", request, role, null);
}

public Object fetchStaffUserById(Class<?> clazz,
String userId, String role) throws JsonProcessingException {
ResponseEntity<Object> responseEntity = getRequest(userId, clazz, role);
return mapServiceSkillsIdResponse(responseEntity, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void addInterceptors(InterceptorRegistry registry) {
"/refdata/case-worker/profile",
"/refdata/case-worker/job-title",
"/refdata/case-worker/profile/search-by-name",
"/refdata/case-worker/profile/search");
"/refdata/case-worker/profile/search",
"/refdata/case-worker/profile/search-by-id");


//Audit Interceptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.SearchRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.StaffProfileCreationRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.SearchStaffUserByIdResponse;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.SearchStaffUserResponse;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.StaffProfileCreationResponse;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.StaffRefDataJobTitle;
Expand Down Expand Up @@ -419,4 +420,45 @@ public ResponseEntity<StaffProfileCreationResponse> updateStaffUserProfile(@Requ
}
return ResponseEntity.status(HttpStatus.OK).body(staffProfileCreationResponse);
}

@ApiOperation(
value = "This API search a staff user by Id",
authorizations = {
@Authorization(value = "ServiceAuthorization"),
@Authorization(value = "Authorization")
}
)
@ApiResponses({
@ApiResponse(
code = 200,
message = "Request is successful",
response = SearchStaffUserByIdResponse.class
),
@ApiResponse(
code = 400,
message = BAD_REQUEST
),
@ApiResponse(
code = 401,
message = UNAUTHORIZED_ERROR
),
@ApiResponse(
code = 403,
message = FORBIDDEN_ERROR
),
@ApiResponse(
code = 500,
message = INTERNAL_SERVER_ERROR
)
})
@GetMapping(
path = "/profile/search-by-id",
produces = APPLICATION_JSON_VALUE
)
@Secured("staff-admin")
public ResponseEntity<SearchStaffUserByIdResponse> fetchStaffProfileById(
@RequestParam(value = "id") String caseWorkerId) {
return staffRefDataService.fetchStaffProfileById(caseWorkerId.trim());
}

}
Loading

0 comments on commit cd2ae4b

Please sign in to comment.