Skip to content

Commit

Permalink
Merge pull request #712 from hmcts/RDCC-6697_Search_by_name_API_regex…
Browse files Browse the repository at this point in the history
…_correction

RDCC-6697 : Search by name API regex correction
  • Loading branch information
vilasshelke-hmcts authored Apr 20, 2023
2 parents a9320f1 + 7c09c2c commit 17ebf7e
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static uk.gov.hmcts.reform.cwrdapi.TestSupport.validateSearchUserProfileResponse;
import static uk.gov.hmcts.reform.cwrdapi.util.FeatureToggleConditionExtension.getToggledOffMessage;
Expand Down Expand Up @@ -65,7 +66,7 @@ void should_return_staff_user_with_status_code_200_when_default_pagination() {
.userType("1")
.role("case allocator")
.build();
assertTrue(validateSearchUserProfileResponse(new ResponseEntity<>(searchStaffUserResponse,HttpStatus.OK),
assertNotNull(validateSearchUserProfileResponse(new ResponseEntity<>(searchStaffUserResponse,HttpStatus.OK),
searchReq));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,38 @@ void should_return_staff_search_by_name_with_firstname_and_lastname_status_code_
assertThat(searchStaffUserResponse.get(0).getLastName()).contains("sbn-David");
}

@Test
void should_return_staff_search_by_name_with_firstname_and_lastname_with_phonetics_status_code_200() {

generateCaseWorkerDataPhoneticAndSpecial();
String searchString = "sbn-Æquen";
List<SearchStaffUserResponse> searchStaffUserResponse = searchApiCallAndCommonAssertions(searchString);
assertThat(searchStaffUserResponse.get(0).getFirstName()).contains("sbn-Æquen");
assertThat(searchStaffUserResponse.get(0).getLastName()).contains("sbn-Ïndîkä");
}

@Test
void should_return_staff_search_by_name_with_firstname_and_lastname_with_roman_status_code_200() {

generateCaseWorkerDataPhoneticAndSpecial();
String searchString = "sbn-IVXIIV";

List<SearchStaffUserResponse> searchStaffUserResponse = searchApiCallAndCommonAssertions(searchString);
assertThat(searchStaffUserResponse.get(0).getFirstName()).contains("sbn-IVXIIV");
assertThat(searchStaffUserResponse.get(0).getLastName()).contains("sbn-IV-XIIV’");
}

@Test
void should_return_staff_search_by_name_with_firstname_and_lastname_with_specialCharacters_status_code_200() {

generateCaseWorkerDataPhoneticAndSpecial();
String searchString = "sbn-IV-XIIV’";
List<SearchStaffUserResponse> searchStaffUserResponse = searchApiCallAndCommonAssertions(searchString);

assertThat(searchStaffUserResponse.get(0).getFirstName()).contains("sbn-IVXIIV");
assertThat(searchStaffUserResponse.get(0).getLastName()).contains("sbn-IV-XIIV’");
}

@Test
void should_return_staff_search_by_firstname_and_lastname_initial_status_code_200() {

Expand Down Expand Up @@ -292,7 +324,7 @@ void should_return_status_code_400_when_search_String_is_not_valid()

assertThat(response).containsEntry("http_status", "400");
assertThat(response.get("response_body").toString())
.contains("Invalid search string. Please input a valid string.");
.contains("The field Page Size is invalid. Please provide a valid value.");

}

Expand Down Expand Up @@ -326,6 +358,23 @@ void should_return_status_code_400_when_search_String_empty()

}

private List<SearchStaffUserResponse> searchApiCallAndCommonAssertions(String searchString) {
String path = "/profile/search-by-name";
CaseWorkerReferenceDataClient.setBearerToken(EMPTY);
ResponseEntity<SearchStaffUserResponse[]> response = caseworkerReferenceDataClient
.searchStaffUserByNameExchange(path, searchString, null, null, ROLE_STAFF_ADMIN);

assertThat(response).isNotNull();

assertThat(Integer.valueOf(response.getHeaders().get("total-records").get(0))).isEqualTo(1);

List<SearchStaffUserResponse> searchStaffUserResponse = Arrays.asList(
response.getBody());

assertThat(searchStaffUserResponse).isNotNull();
return searchStaffUserResponse;
}

private void generateCaseWorkerData() {
String emailPattern = "sbnTest1234";
String email = format(EMAIL_TEMPLATE, randomAlphanumeric(10) + emailPattern).toLowerCase();
Expand All @@ -342,6 +391,15 @@ private void generateCaseWorkerData() {
createCaseWorkerTestData("sbn-Mary", "sbn-David", email);
}

private void generateCaseWorkerDataPhoneticAndSpecial() {
String emailPattern = "sbnTest1234";
String email = format(EMAIL_TEMPLATE, randomAlphanumeric(10) + emailPattern).toLowerCase();
email = format(EMAIL_TEMPLATE, randomAlphanumeric(10) + emailPattern).toLowerCase();
createCaseWorkerTestData("sbn-Æquen", "sbn-Ïndîkä", email);
email = format(EMAIL_TEMPLATE, randomAlphanumeric(10) + emailPattern).toLowerCase();
createCaseWorkerTestData("sbn-IVXIIV", "sbn-IV-XIIV’", email);
}

private void validateSearchStaffUserResponse(List<SearchStaffUserResponse> searchStaffUserResponse) {

assertThat(searchStaffUserResponse.get(0).getFirstName()).contains("sbn-Mary");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -30,6 +33,7 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.apache.logging.log4j.util.Strings.EMPTY;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -96,7 +100,7 @@ void should_return_staff_user_with_status_code_201() {
userProfilePostUserWireMockForStaffProfile(HttpStatus.CREATED);
StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();

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

assertThat(response)
.isNotNull()
Expand All @@ -105,6 +109,64 @@ void should_return_staff_user_with_status_code_201() {
assertThat(response.get("case_worker_id")).isNotNull();
}


@ParameterizedTest(name = "{index} => firstName={0}, lastName={1}")
@MethodSource("validNameProvider")
@DisplayName("Create Staff profile with valid names status 201")
void should_return_staff_user_with_valid_name_status_code_201(String firstName, String lastName) {
CaseWorkerReferenceDataClient.setBearerToken(EMPTY);
userProfilePostUserWireMockForStaffProfile(HttpStatus.CREATED);
StaffProfileCreationRequest request =
caseWorkerReferenceDataClient.buildStaffProfileCreationRequest(firstName, lastName);

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

assertThat(response)
.isNotNull()
.containsEntry("http_status", "201 CREATED");

assertThat(response.get("case_worker_id")).isNotNull();
}


@ParameterizedTest(name = "{index} => firstName={0}, lastName={1}")
@MethodSource("inValidNameProvider")
@DisplayName("Create Staff profile with invalid names status 400")
void should_return_staff_user_with_invalid_name_status_code_400(String firstName, String lastName) {
CaseWorkerReferenceDataClient.setBearerToken(EMPTY);
userProfilePostUserWireMockForStaffProfile(HttpStatus.CREATED);
StaffProfileCreationRequest request =
caseWorkerReferenceDataClient.buildStaffProfileCreationRequest(firstName, lastName);

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

assertThat(response)
.isNotNull()
.containsEntry("http_status", "400");

assertThat(response.get("case_worker_id")).isNull();
}

private static Stream<Arguments> validNameProvider() {
return Stream.of(
Arguments.of("Vilas", "Shelke"),//normal
Arguments.of("Vilas1234", "Sh456"),//numeric
Arguments.of("IIIIVVI", "XIII"),//Roman
Arguments.of("Nando's", "Zi-n ac"),//Space, and Special characters - '
Arguments.of("Æâçdëøœoo", "Qętŷįłgå12"),//Space, and Special characters - '
Arguments.of("Æmaze", "Zìœ")//phonetic
);
}

private static Stream<Arguments> inValidNameProvider() {
return Stream.of(
Arguments.of("Vilas&", "She_lke"),//invalid special characters & _
Arguments.of("Vilas1234", "Sh+456"),//valid first name and invalid last name
Arguments.of("Nando*s", "Zi-n ac"),//valid last name and valid last name
Arguments.of("??%%/", "()*&^)")//invalid special characters
);
}

@Test
@DisplayName("Create Staff profile with status 201 with child tables entries")
void should_return_staff_user_with_status_code_201_child_tables_size() {
Expand All @@ -113,7 +175,7 @@ void should_return_staff_user_with_status_code_201_child_tables_size() {
StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();

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

assertThat(response)
.isNotNull()
Expand All @@ -135,7 +197,7 @@ void validate_staff_audit_data() throws JsonProcessingException, JSONException {
StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();

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

assertThat(response)
.isNotNull()
Expand Down Expand Up @@ -173,9 +235,9 @@ void should_return_create_staff_user_with_status_code_400_invalid_email_id() thr


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

assertThat(response).containsEntry("http_status","400");
assertThat(response).containsEntry("http_status", "400");
String responseBody = (String) response.get("response_body");
assertThat(responseBody).contains(INVALID_EMAIL);

Expand Down Expand Up @@ -222,12 +284,12 @@ void should_return_create_staff_user_with_status_code_400_invalid_primary_locati

StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();

request.setBaseLocations(List.of(caseWorkerLocationRequest,caseWorkerLocationRequest2));
request.setBaseLocations(List.of(caseWorkerLocationRequest, caseWorkerLocationRequest2));

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

assertThat(response).containsEntry("http_status","400");
assertThat(response).containsEntry("http_status", "400");
String responseBody = (String) response.get("response_body");
assertThat(responseBody).contains(NO_PRIMARY_LOCATION_PRESENT_PROFILE);

Expand Down Expand Up @@ -275,13 +337,13 @@ void should_return_create_staff_user_with_status_code_400_duplicate_service_code
.build();

StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();
request.setServices(List.of(caseWorkerServicesRequest,caseWorkerServicesRequest2));
request.setServices(List.of(caseWorkerServicesRequest, caseWorkerServicesRequest2));


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

assertThat(response).containsEntry("http_status","400");
assertThat(response).containsEntry("http_status", "400");
String responseBody = (String) response.get("response_body");
assertThat(responseBody).contains(DUPLICATE_SERVICE_CODE_IN_AREA_OF_WORK);

Expand Down Expand Up @@ -322,13 +384,13 @@ void should_return_create_staff_user_with_status_code_400_duplicate_roles() thro


StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();
request.setRoles(List.of(staffProfileRoleRequest1,staffProfileRoleRequest2));
request.setRoles(List.of(staffProfileRoleRequest1, staffProfileRoleRequest2));


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

assertThat(response).containsEntry("http_status","400");
assertThat(response).containsEntry("http_status", "400");
String responseBody = (String) response.get("response_body");
assertThat(responseBody).contains(DUPLICATE_PRIMARY_AND_SECONDARY_ROLES);

Expand Down Expand Up @@ -366,12 +428,12 @@ void should_return_create_staff_user_with_status_code_400_invalid_roles() throws
new StaffProfileRoleRequest(1, "adminRole", false);

StaffProfileCreationRequest request = caseWorkerReferenceDataClient.createStaffProfileCreationRequest();
request.setRoles(List.of(staffProfileRoleRequest1,staffProfileRoleRequest2));
request.setRoles(List.of(staffProfileRoleRequest1, staffProfileRoleRequest2));

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

assertThat(response).containsEntry("http_status","400");
assertThat(response).containsEntry("http_status", "400");
String responseBody = (String) response.get("response_body");
assertThat(responseBody).contains(NO_PRIMARY_ROLE_PRESENT_PROFILE);

Expand Down
Loading

0 comments on commit 17ebf7e

Please sign in to comment.