Skip to content

Commit

Permalink
Rdcc 6212 (#671)
Browse files Browse the repository at this point in the history
* Implementation of resend invite as true

* checkstyle fix

* checkstyle fix

* unit test coverage

* unit test coverage

* unit test coverage

* unit test coverage

* Integration test cases

* Integration test cases

* Integration test cases

* Integration test cases

* Integration test cases

* Integration test cases

* Integration test cases

* Integration test cases

* Integration test cases

* adding json property annotation

* Helm chart upgrade

---------

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 9, 2023
1 parent c819e7e commit 770a30d
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 10 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,6 @@ dependencies {
pactTestImplementation sourceSets.test.runtimeClasspath
}



dependencyManagement {

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.SkillsRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.StaffProfileCreationRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.request.StaffProfileRoleRequest;
import uk.gov.hmcts.reform.cwrdapi.controllers.response.SearchStaffUserResponse;
import uk.gov.hmcts.reform.cwrdapi.domain.StaffAudit;
import uk.gov.hmcts.reform.cwrdapi.repository.CaseWorkerLocationRepository;
import uk.gov.hmcts.reform.cwrdapi.repository.CaseWorkerProfileRepository;
Expand All @@ -31,9 +33,12 @@

import static org.apache.logging.log4j.util.Strings.EMPTY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants.DUPLICATE_PRIMARY_AND_SECONDARY_ROLES;
import static uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants.DUPLICATE_SERVICE_CODE_IN_AREA_OF_WORK;
import static uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants.INVALID_EMAIL;
import static uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants.INVALID_PROFILE;
import static uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants.NO_PRIMARY_LOCATION_PRESENT_PROFILE;
import static uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants.NO_PRIMARY_ROLE_PRESENT_PROFILE;

Expand Down Expand Up @@ -112,7 +117,7 @@ void should_return_update_staff_user_with_status_code_200_child_tables_size() th

request.setFirstName("StaffProfilefirstNameCN");
request.setLastName("StaffProfilelastNameCN");

request.setResendInvite(false);
Map<String, Object> response = caseworkerReferenceDataClient
.updateStaffProfile(request,ROLE_STAFF_ADMIN);

Expand Down Expand Up @@ -403,6 +408,63 @@ void should_return_update_staff_user_with_status_code_400_invalid_roles() throws

}

@Test
void should_return_reinvite_staff_user_with_status_code_404_profile_doesnot_exist() throws Exception {

StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();
request.setEmailId("test@test.com");
request.setResendInvite(true);

Map<String, Object> response = caseworkerReferenceDataClient
.updateStaffProfile(request,ROLE_STAFF_ADMIN);

assertThat(response.get("http_status")).isEqualTo("404");
String responseBody = (String) response.get("response_body");
assertThat(responseBody.contains(INVALID_PROFILE)).isTrue();

}

@Test
void should_return_reinvite_staff_user_with_status_code_200_profile() throws Exception {

StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();
userProfilePostUserWireMockForStaffProfile(HttpStatus.CREATED);
request.setResendInvite(true);

Map<String, Object> createResponse = caseworkerReferenceDataClient.createStaffProfile(request,ROLE_STAFF_ADMIN);
Map createBody = (Map)createResponse.get("body");
Map<String, Object> resendResponse = caseworkerReferenceDataClient.updateStaffProfile(request,ROLE_STAFF_ADMIN);

assertThat(resendResponse).isNotNull();
assertThat(resendResponse.get("http_status")).isEqualTo("200 OK");
Map resendResponseBody = (Map) resendResponse.get("body");
assertEquals(createBody.get("case_worker_id"), resendResponseBody.get("case_worker_id"));
}

@Test
void should_update_IdamId_when_reinvite_staff_user_true_in_crd() throws Exception {

StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();
userProfilePostUserWireMockForStaffProfile(false);
userProfilePostUserWireMockForStaffProfile(true);

Map<String, Object> createResponse = caseworkerReferenceDataClient.createStaffProfile(request,ROLE_STAFF_ADMIN);
request.setResendInvite(true);
Map createBody = (Map)createResponse.get("body");
Map<String, Object> resendResponse = caseworkerReferenceDataClient.updateStaffProfile(request,ROLE_STAFF_ADMIN);
assertThat(resendResponse).isNotNull();
assertThat(resendResponse.get("http_status")).isEqualTo("200 OK");
Map resendResponseBody = (Map) resendResponse.get("body");
assertNotEquals(createBody.get("case_worker_id"), resendResponseBody.get("case_worker_id"));

String path = "/profile/search-by-name";
ResponseEntity<SearchStaffUserResponse[]> fetchStaff = caseworkerReferenceDataClient
.searchStaffUserByNameExchange(path, request.getFirstName(), "1", "1",
ROLE_STAFF_ADMIN);
assertEquals(resendResponseBody.get("case_worker_id"), fetchStaff.getBody()[0].getCaseWorkerId());
assertNotEquals(fetchStaff.getBody()[0].getCaseWorkerId(), createBody.get("case_worker_id"));
}

public StaffProfileCreationRequest getStaffProfileCreationRequest() {

StaffProfileRoleRequest staffProfileRoleRequest1 =
Expand Down Expand Up @@ -470,4 +532,7 @@ public StaffProfileCreationRequest getStaffProfileCreationRequest() {

}




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

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.put;
Expand Down Expand Up @@ -253,6 +254,19 @@ public void userProfilePostUserWireMockForStaffProfile(HttpStatus status) {
+ "}")));
}

public void userProfilePostUserWireMockForStaffProfile(boolean resend) {
userProfileService.stubFor(post(urlPathMatching("/v1/userprofile"))
.withRequestBody(equalToJson("{ \"resendInvite\": " + resend + "}", true,
true))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(201)
.withBody("{"
+ " \"idamId\":\"" + UUID.randomUUID() + "\","
+ " \"idamRegistrationResponse\":\"" + 201 + "\""
+ "}")));
}

public static class CaseWorkerTransformer extends ResponseTransformer {
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,17 @@ public ResponseEntity<List<SearchStaffUserResponse>> searchStaffProfile(
public ResponseEntity<StaffProfileCreationResponse> updateStaffUserProfile(@RequestBody StaffProfileCreationRequest
staffProfileCreationRequest) {
log.info("Inside updateStaffUserProfile Controller");
StaffProfileCreationResponse staffProfileCreationResponse = null;
StaffProfileCreationResponse staffProfileCreationResponse;

staffProfileCreationResponse = staffRefDataService.updateStaffProfile(staffProfileCreationRequest);
if (staffProfileCreationRequest.isResendInvite()) {
staffProfileCreationResponse = staffRefDataService.reinviteStaffProfile(staffProfileCreationRequest);
} else {
staffProfileCreationResponse = staffRefDataService.updateStaffProfile(staffProfileCreationRequest);
}
if (isNotEmpty(staffProfileCreationResponse)) {

staffRefDataService.publishStaffProfileToTopic(staffProfileCreationResponse);
}
return ResponseEntity.status(HttpStatus.OK).body(staffProfileCreationResponse);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ public class StaffProfileCreationRequest {

@JsonProperty("skills")
private List<SkillsRequest> skills;

@JsonProperty("is_resend_invite")
private boolean resendInvite;

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface StaffRefDataService {
ResponseEntity<List<SearchStaffUserResponse>> retrieveStaffProfile(SearchRequest searchRequest,
PageRequest pageRequest);


StaffProfileCreationResponse reinviteStaffProfile(StaffProfileCreationRequest profileRequest);

StaffProfileCreationResponse updateStaffProfile(StaffProfileCreationRequest staffProfileRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public UserProfileCreationRequest createUserProfileRequest(StaffProfileCreationR
UserCategory.CASEWORKER,
UserTypeRequest.INTERNAL,
userRoles,
false);
profileRequest.isResendInvite());
}

/**
Expand Down Expand Up @@ -669,6 +669,36 @@ public StaffProfileCreationResponse updateStaffProfile(StaffProfileCreationReque
return response;
}

@Override
public StaffProfileCreationResponse reinviteStaffProfile(StaffProfileCreationRequest profileRequest) {

CaseWorkerProfile caseWorkerProfile = caseWorkerProfileRepo
.findByEmailId(profileRequest.getEmailId().toLowerCase());
//if caseworker profile does not have the input emailid throw error
if (caseWorkerProfile == null) {
staffProfileAuditService.saveStaffAudit(AuditStatus.FAILURE, PROFILE_NOT_PRESENT_IN_SRD,
StringUtils.EMPTY, profileRequest, STAFF_PROFILE_UPDATE);
throw new StaffReferenceException(HttpStatus.NOT_FOUND, PROFILE_NOT_PRESENT_IN_SRD,
PROFILE_NOT_PRESENT_IN_SRD);
}
ResponseEntity<Object> responseEntity = createUserProfileInIdamUP(profileRequest);
UserProfileCreationResponse upResponse = (UserProfileCreationResponse) (responseEntity.getBody());

// update idamid in case its different in idam
if (upResponse != null && !upResponse.getIdamId().equals(caseWorkerProfile.getCaseWorkerId())) {
caseWorkerProfileRepo.delete(caseWorkerProfile);
cwrCommonRepository.flush();
caseWorkerProfile.setCaseWorkerId(upResponse.getIdamId());
caseWorkerProfile.getCaseWorkerLocations().forEach(e -> e.setCaseWorkerId(upResponse.getIdamId()));
caseWorkerProfile.getCaseWorkerRoles().forEach(e -> e.setCaseWorkerId(upResponse.getIdamId()));
caseWorkerProfile.getCaseWorkerWorkAreas().forEach(e -> e.setCaseWorkerId(upResponse.getIdamId()));
caseWorkerProfile.getCaseWorkerSkills().forEach(e -> e.setCaseWorkerId(upResponse.getIdamId()));
caseWorkerProfileRepo.save(caseWorkerProfile);
}
return new StaffProfileCreationResponse(caseWorkerProfile.getCaseWorkerId());
}


private CaseWorkerProfile validateStaffProfileForUpdate(StaffProfileCreationRequest profileRequest) {

// get all existing profile from db (used IN clause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private CaseWorkerConstants() {
public static final String USER_NAME_PATTERN = "^[A-Za-z0-9]+[\\w!#$%&'’.*+/=?`{|}~^-]+"
+ "(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*";
public static final String INVALID_EMAIL = "You must add a valid email address";
public static final String INVALID_PROFILE = "User does not exist in SRD";

public static final String FILE_NO_DATA_ERROR_MESSAGE = "There is no data in the file uploaded."
+ " Upload a valid file in xlsx or xls format";
Expand Down Expand Up @@ -217,6 +218,9 @@ private CaseWorkerConstants() {
public static final String PROFILE_NOT_PRESENT_IN_UP_OR_IDAM = "User does not exists in UP/IDAM";

public static final String IDAM_STATUS_NOT_ACTIVE = "User is in pending status or does not exist in IDAM";

public static final String IDAM_STATUS_ACTIVE = "User is already ACTIVE in IDAM";

public static final String IDAM_STATUS_USER_PROFILE = "User does not exists in IDAM";
public static final String NO_PRIMARY_ROLE_PRESENT_PROFILE = "You must add Primary Role"
+ TRY_AGAIN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ private boolean verifyAllUserTypes(List<StaffRefDataUserType> actualResultUserTy

@Test
void should_return_staffCreateResponse_with_status_code_200() {

ResponseEntity<StaffProfileCreationResponse> actual = staffRefDataController
.createStaffUserProfile(request);
assertThat(actual.getStatusCodeValue()).isEqualTo(201);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ void shouldUpdateStaffUserProfileEmptyResponseTest() {
verify(staffRefDataService,times(1))
.updateStaffProfile(staffProfileCreationRequest);
}

@Test
void shouldUpdateStaffUserProfileResendInviteTest() {
StaffProfileCreationRequest staffProfileCreationRequest = StaffProfileCreationRequest
.staffProfileCreationRequest()
.build();

staffProfileCreationRequest.setResendInvite(true);
staffRefDataController.updateStaffUserProfile(staffProfileCreationRequest);

verify(staffRefDataService,times(1))
.reinviteStaffProfile(staffProfileCreationRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
Expand Down Expand Up @@ -1386,4 +1387,49 @@ private StaffProfileCreationRequest getStaffProfileUpdateRequest() {
return staffProfileCreationRequest;

}

@Test
void test_reInviteStaffProfile_when_no_emailId_found() throws JsonProcessingException {
when(caseWorkerProfileRepository.findByEmailId(any())).thenReturn(null);
Exception ex = assertThrows(StaffReferenceException.class, () -> staffRefDataServiceImpl.reinviteStaffProfile(
staffProfileCreationRequest));
assertNotNull(ex);
assertEquals("User does not exist in SRD", ex.getMessage());
//verify(staffRefDataServiceImpl, times(0))
// .createUserProfileInIdamUP(staffProfileCreationRequest);
}

@Test
void test_reInviteStaffProfile_success() throws JsonProcessingException {
when(caseWorkerProfileRepository.findByEmailId(any())).thenReturn(caseWorkerProfile);
UserProfileCreationResponse userProfileCreationResponse = new UserProfileCreationResponse();
userProfileCreationResponse.setIdamId("12345678");
userProfileCreationResponse.setIdamRegistrationResponse(201);

String body = mapper.writeValueAsString(userProfileCreationResponse);
when(userProfileFeignClient.createUserProfile(any(),any())).thenReturn(Response.builder()
.request(mock(Request.class)).body(body, defaultCharset()).status(201).build());
staffRefDataServiceImpl.reinviteStaffProfile(staffProfileCreationRequest);
verify(staffAuditRepository, times(0)).save(any());
verify(caseWorkerProfileRepository, times(1))
.findByEmailId(any());
}

@Test
void test_reInviteStaffProfile_Bad_Request() throws JsonProcessingException {
when(caseWorkerProfileRepository.findByEmailId(any())).thenReturn(caseWorkerProfile);
UserProfileCreationResponse userProfileCreationResponse = new UserProfileCreationResponse();
userProfileCreationResponse.setIdamId("12345678");
userProfileCreationResponse.setIdamRegistrationResponse(1);

String body = mapper.writeValueAsString(userProfileCreationResponse);
when(userProfileFeignClient.createUserProfile(any(),any())).thenReturn(Response.builder()
.request(mock(Request.class)).body(body, defaultCharset()).status(400).build());
StaffReferenceException ex = assertThrows(StaffReferenceException.class, () -> staffRefDataServiceImpl
.reinviteStaffProfile(
staffProfileCreationRequest));
assertNotNull(ex);
assertEquals(HttpStatus.BAD_REQUEST, ex.getStatus());
}

}

0 comments on commit 770a30d

Please sign in to comment.