Skip to content

Commit

Permalink
MODINREACH-424: Patron validation endpoint should strip commas and le…
Browse files Browse the repository at this point in the history
…ading and trailing spaces from the patron name field (#390)

* Removes spaces in front, spaces behind, spaces in between

* Removing wildcard import
  • Loading branch information
steveellis authored Mar 19, 2024
1 parent 18355bc commit cc1b66f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -226,7 +228,16 @@ private String getPatronAgencyCode(UUID centralServerId, List<LocalAgencyDTO> ag

private static boolean matchName(User user, String patronName) {
var personal = user.getPersonal();
String[] patronNameTokens = patronName.replace(",", "").split("\\s");
String[] patronNameTokens = nonNullStrip(patronName).replace(",", "").split("\\s");

for (int i = 0; i < patronNameTokens.length; i++) {
patronNameTokens[i] = patronNameTokens[i].strip();
}

// Because split can return empty strings on sequences of spaces.
patronNameTokens = Arrays.stream(patronNameTokens)
.filter(str -> !str.isEmpty())
.toArray(String[]::new);

if (patronNameTokens.length < 2) {
return false;
Expand All @@ -241,21 +252,30 @@ private static boolean matchName(User user, String patronName) {
|| checkFirstNameMiddleNameLastNameWithPosition(personal, patronNameTokens,1,2,0));
}


private static boolean checkFirstNameMiddleNameLastNameWithPosition(User.Personal personal, String[] patronNameTokens,
int firstNamePosition,int middleNamePosition,int lastNamePosition) {
int firstNamePosition, int middleNamePosition,
int lastNamePosition) {

boolean checkFirstName = equalsAnyIgnoreCase(patronNameTokens[firstNamePosition], personal.getFirstName(), personal.getPreferredFirstName());
boolean checkMiddleName = patronNameTokens[middleNamePosition].equalsIgnoreCase(personal.getMiddleName());
boolean checkLastName = patronNameTokens[lastNamePosition].equalsIgnoreCase(personal.getLastName());
boolean checkFirstName = equalsAnyIgnoreCase(patronNameTokens[firstNamePosition],
nonNullStrip(personal.getFirstName()), nonNullStrip(personal.getPreferredFirstName()));
boolean checkMiddleName = patronNameTokens[middleNamePosition].equalsIgnoreCase(nonNullStrip(personal.getMiddleName()));
boolean checkLastName = patronNameTokens[lastNamePosition].equalsIgnoreCase(nonNullStrip(personal.getLastName()));
return checkFirstName && checkMiddleName && checkLastName;
}

private static boolean checkFirstNameLastNameWithPosition(User.Personal personal, String[] patronNameTokens,int firstNamePos,int lastNamePos) {
boolean checkFirstName = equalsAnyIgnoreCase(patronNameTokens[firstNamePos], personal.getFirstName(), personal.getPreferredFirstName(), personal.getMiddleName());
boolean checkLastName = patronNameTokens[lastNamePos].equalsIgnoreCase(personal.getLastName());
private static boolean checkFirstNameLastNameWithPosition(User.Personal personal, String[] patronNameTokens,
int firstNamePos, int lastNamePos) {
boolean checkFirstName = equalsAnyIgnoreCase(patronNameTokens[firstNamePos], nonNullStrip(personal.getFirstName()),
nonNullStrip(personal.getPreferredFirstName()), nonNullStrip(personal.getMiddleName()));
boolean checkLastName = patronNameTokens[lastNamePos].equalsIgnoreCase(nonNullStrip(personal.getLastName()));
return (checkFirstName && checkLastName);
}

private static String nonNullStrip(String s) {
return Objects.requireNonNullElse(s, "").strip();
}

private static String getPatronName(User user) {
var personal = user.getPersonal();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ void return200HttpCode_and_patronInfoResponseWithPatronInfo_hasIgnoreCaseCorrect
}

@ParameterizedTest
@ValueSource(strings = {"john paul doe","doe john paul","doe John pauL","doe, John pauL","doe John, pauL","doe John pauL,",
"doe, John, pauL", "doe John, pauL,", ",doe, John, pauL,"})
@ValueSource(strings = {"john paul doe","doe john paul","doe John pauL","doe, John pauL","doe John, pauL",
"doe John pauL,", "doe, John, pauL", "doe John, pauL,", ",doe, John, pauL,", "John, Doe "," John, Doe ",
" Doe, John ", " John paul doe "})
@Sql(scripts = {
"classpath:db/central-server/pre-populate-central-server.sql",
"classpath:db/patron-type-mapping/pre-populate-patron-type-mapping.sql",
Expand Down

0 comments on commit cc1b66f

Please sign in to comment.