Skip to content

Commit

Permalink
CIRC-1931 - Display country name instead of country code in slips and…
Browse files Browse the repository at this point in the history
… notices. (#1350)

* [CIRC-1931] - Display country name instead of country code in slips and notices.

* [CIRC-1931] - Updated the code.

* [CIRC-1931] - Fixing code smells.

* [CIRC-1931] - Fixing review comments.

* [CIRC-1931] - Fixing review comments.

* [CIRC-1931] - Fixing review comments.

* [CIRC-1931] - Fixing review comments.

* [CIRC-1931] - Fixing review comments.

* [CIRC-1931] - Fixing review comments.

* [CIRC-1931] - Fixing review comments.

* [CIRC-1931] - Fixing review comments.
  • Loading branch information
gurleenkaurbp authored Oct 27, 2023
1 parent c246822 commit 14a61ce
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Stream;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.Account;
Expand Down Expand Up @@ -412,13 +415,26 @@ public UserContext withAddressProperties(JsonObject address) {
.with(UserContext.CITY, address.getString("city", null))
.with(UserContext.REGION, address.getString("region", null))
.with(UserContext.POSTAL_CODE, address.getString("postalCode", null))
.with(UserContext.COUNTRY_ID, address.getString("countryId", null))
.with(UserContext.COUNTRY_ID, getCountryNameByCodeIgnoreCase(address.getString(COUNTRY_ID, null)))
.with(UserContext.ADDRESS_TYPE_NAME, address.getString("addressTypeName", null));
} else {
return this;
}
}

public String getCountryNameByCodeIgnoreCase(String code) {
if (StringUtils.isEmpty(code)) {
return null;
}

if (!Stream.of(Locale.getISOCountries()).toList().contains(code)) {
log.error("getCountryNameByCodeIgnoreCase:: Invalid country code {}", code);
throw new IllegalArgumentException("Not a valid country code to determine the country name.");
}

return new Locale("",code).getDisplayName();
}

public UserContext withPrimaryAddressProperties(JsonObject address) {
if (address != null) {
return this
Expand All @@ -427,7 +443,7 @@ public UserContext withPrimaryAddressProperties(JsonObject address) {
.with(UserContext.PRIMARY_ADDRESS_CITY, address.getString("city", null))
.with(UserContext.PRIMARY_ADDRESS_REGION, address.getString("region", null))
.with(UserContext.PRIMARY_ADDRESS_POSTAL_CODE, address.getString("postalCode", null))
.with(UserContext.PRIMARY_ADDRESS_COUNTRY_ID, address.getString("countryId", null))
.with(UserContext.PRIMARY_ADDRESS_COUNTRY_ID, getCountryNameByCodeIgnoreCase(address.getString(COUNTRY_ID, null)))
.with(UserContext.PRIMARY_ADDRESS_ADDRESS_TYPE_NAME, address.getString("addressTypeName", null));
} else {
return this;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/api/loans/CheckInByBarcodeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void canCreateStaffSlipContextOnCheckInByBarcode() {
assertThat(userContext.getString("city"), is(address.getCity()));
assertThat(userContext.getString("region"), is(address.getRegion()));
assertThat(userContext.getString("postalCode"), is(address.getPostalCode()));
assertThat(userContext.getString("countryId"), is(address.getCountryId()));
assertThat(userContext.getString("countryId"), is("United Kingdom"));
assertThat(requestContext.getString("deliveryAddressType"), is(addressTypesFixture.home().getJson().getString("addressType")));
assertThat(requestContext.getString("requestExpirationDate"), isEquivalentTo(
ZonedDateTime.of(requestExpiration.atTime(23, 59, 59), ZoneOffset.UTC)));
Expand Down
178 changes: 97 additions & 81 deletions src/test/java/api/requests/PickSlipsTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api.requests;

import static api.support.matchers.TextDateTimeMatcher.isEquivalentTo;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.time.ZoneOffset.UTC;
import static java.util.stream.Collectors.joining;
Expand All @@ -15,7 +16,6 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;


import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand All @@ -25,7 +25,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.vertx.core.json.JsonArray;
import org.folio.circulation.domain.CallNumberComponents;
import org.folio.circulation.domain.Item;
import org.folio.circulation.domain.ItemStatus;
Expand All @@ -38,6 +37,9 @@
import org.folio.circulation.support.json.JsonObjectArrayPropertyFetcher;
import org.folio.circulation.support.utils.ClockUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.testcontainers.shaded.org.apache.commons.lang3.StringUtils;

import api.support.APITests;
import api.support.builders.Address;
Expand All @@ -48,6 +50,7 @@
import api.support.http.ResourceClient;
import api.support.http.UserResource;
import api.support.matchers.UUIDMatcher;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import lombok.val;

Expand Down Expand Up @@ -122,13 +125,18 @@ void responseContainsNoPickSlipsWhenItemHasOpenPageRequestWithWrongStatus() {
assertResponseHasItems(response, 0);
}

@Test
void responseContainsPickSlipWithAllAvailableTokens() {
@ParameterizedTest
@ValueSource(strings = {
"US",
StringUtils.EMPTY,
"XX"
})
void responseContainsPickSlipWithAllAvailableTokens(String countryCode) {
IndividualResource servicePoint = servicePointsFixture.cd1();
UUID servicePointId = servicePoint.getId();
IndividualResource locationResource = locationsFixture.thirdFloor();
IndividualResource addressTypeResource = addressTypesFixture.home();
Address address = AddressExamples.mainStreet();
Address address = AddressExamples.mainStreet(countryCode);
var departmentId1 = UUID.randomUUID().toString();
var departmentId2 = UUID.randomUUID().toString();
IndividualResource requesterResource =
Expand All @@ -154,8 +162,8 @@ void responseContainsPickSlipWithAllAvailableTokens() {
.withPermanentLoanType(loanTypeResource.getId()));

ZonedDateTime now = ClockUtil.getZonedDateTime();
checkOutFixture.checkOutByBarcode(itemResource, requesterResource);
checkInFixture.checkInByBarcode(itemResource, now, servicePointId);
var checkoutResource = checkOutFixture.checkOutByBarcode(itemResource, requesterResource);
var checkinResource = checkInFixture.checkInByBarcode(itemResource, now, servicePointId);
JsonObject lastCheckIn = itemsClient.get(itemResource.getId())
.getJson().getJsonObject("lastCheckIn");
ZonedDateTime actualCheckinDateTime = getDateTimeProperty(lastCheckIn, "dateTime");
Expand All @@ -174,80 +182,88 @@ void responseContainsPickSlipWithAllAvailableTokens() {

Response response = ResourceClient.forPickSlips().getById(servicePointId);

assertThat(response.getStatusCode(), is(HTTP_OK));
assertResponseHasItems(response, 1);

JsonObject pickSlip = getPickSlipsList(response).get(0);
JsonObject itemContext = pickSlip.getJsonObject(ITEM_KEY);
assertNotNull(pickSlip.getString(CURRENT_DATE_TIME));

ZonedDateTime requestCheckinDateTime = getDateTimeProperty(itemContext, "lastCheckedInDateTime");

Item item = Item.from(itemResource.getJson())
.withInstance(new InstanceMapper().toDomain(itemResource.getInstance().getJson()));

String contributorNames = item.getContributorNames().collect(joining("; "));

String yearCaptionsToken = String.join("; ", item.getYearCaption());
String copyNumber = item.getCopyNumber() != null ? item.getCopyNumber() : "";
String materialTypeName = getName(materialTypeResource.getJson());
String loanTypeName = getName(loanTypeResource.getJson());
Location location = new LocationMapper().toDomain(locationResource.getJson());

assertEquals(item.getTitle(), itemContext.getString("title"));
assertEquals(item.getBarcode(), itemContext.getString("barcode"));
assertEquals(ItemStatus.PAGED.getValue(), itemContext.getString("status"));
assertEquals(item.getPrimaryContributorName(), itemContext.getString("primaryContributor"));
assertEquals(contributorNames, itemContext.getString("allContributors"));
assertEquals(item.getEnumeration(), itemContext.getString("enumeration"));
assertEquals(item.getVolume(), itemContext.getString("volume"));
assertEquals(item.getChronology(), itemContext.getString("chronology"));
assertEquals(yearCaptionsToken, itemContext.getString("yearCaption"));
assertEquals(materialTypeName, itemContext.getString("materialType"));
assertEquals(loanTypeName, itemContext.getString("loanType"));
assertEquals(copyNumber, itemContext.getString("copy"));
assertEquals(item.getNumberOfPieces(), itemContext.getString("numberOfPieces"));
assertEquals(item.getDescriptionOfPieces(), itemContext.getString("descriptionOfPieces"));
assertDatetimeEquivalent(actualCheckinDateTime, requestCheckinDateTime);
assertEquals(location.getName(), itemContext.getString("effectiveLocationSpecific"));
assertEquals(location.getPrimaryServicePoint().getName(), itemContext.getString("effectiveLocationPrimaryServicePointName"));
CallNumberComponents callNumberComponents = item.getCallNumberComponents();
assertEquals(callNumberComponents.getCallNumber(), itemContext.getString("callNumber"));
assertEquals(callNumberComponents.getPrefix(), itemContext.getString("callNumberPrefix"));
assertEquals(callNumberComponents.getSuffix(), itemContext.getString("callNumberSuffix"));

User requester = new User(requesterResource.getJson());
JsonObject requesterContext = pickSlip.getJsonObject("requester");

assertThat(requesterContext.getString("firstName"), is(requester.getFirstName()));
assertThat(requesterContext.getString("lastName"), is(requester.getLastName()));
assertThat(requesterContext.getString("middleName"), is(requester.getMiddleName()));
assertThat(requesterContext.getString("barcode"), is(requester.getBarcode()));
assertThat(requesterContext.getString("addressLine1"), is(address.getAddressLineOne()));
assertThat(requesterContext.getString("addressLine2"), is(address.getAddressLineTwo()));
assertThat(requesterContext.getString("city"), is(address.getCity()));
assertThat(requesterContext.getString("region"), is(address.getRegion()));
assertThat(requesterContext.getString("postalCode"), is(address.getPostalCode()));
assertThat(requesterContext.getString("countryId"), is(address.getCountryId()));
assertThat(requesterContext.getString("patronGroup"), is("Regular Group"));
assertThat(requesterContext.getString("departments").split("; "),
arrayContainingInAnyOrder(equalTo("test department1"),equalTo("test department2")));

JsonObject requestContext = pickSlip.getJsonObject("request");

assertThat(requestContext.getString("deliveryAddressType"),
is(addressTypeResource.getJson().getString("addressType")));
assertThat(requestContext.getString("requestExpirationDate"),
isEquivalentTo(requestExpiration.atTime(23, 59, 59).atZone(UTC)));
assertThat(requestContext.getString("holdShelfExpirationDate"),
isEquivalentTo(ZonedDateTime.of(
holdShelfExpiration.atStartOfDay(), ZoneOffset.UTC)));
assertThat(requestContext.getString("requestID"),
UUIDMatcher.is(requestResource.getId()));
assertThat(requestContext.getString("servicePointPickup"),
is(servicePoint.getJson().getString("name")));
assertThat(requestContext.getString("patronComments"), is("I need the book"));
assertThat(requestContext.getString("requestDate"), isEquivalentTo(requestDate));
if (StringUtils.isNoneEmpty(countryCode) && !countryCode.equalsIgnoreCase("US")) {
assertThat(response.getStatusCode(), is(HTTP_INTERNAL_ERROR));
assertEquals(true, response.getBody().
contains("IllegalArgumentException: Not a valid country code to determine the country name."));
}
else {
assertThat(response.getStatusCode(), is(HTTP_OK));
assertResponseHasItems(response, 1);

JsonObject pickSlip = getPickSlipsList(response).get(0);
JsonObject itemContext = pickSlip.getJsonObject(ITEM_KEY);
assertNotNull(pickSlip.getString(CURRENT_DATE_TIME));

ZonedDateTime requestCheckinDateTime = getDateTimeProperty(itemContext, "lastCheckedInDateTime");

Item item = Item.from(itemResource.getJson())
.withInstance(new InstanceMapper().toDomain(itemResource.getInstance().getJson()));

String contributorNames = item.getContributorNames().collect(joining("; "));

String yearCaptionsToken = String.join("; ", item.getYearCaption());
String copyNumber = item.getCopyNumber() != null ? item.getCopyNumber() : "";
String materialTypeName = getName(materialTypeResource.getJson());
String loanTypeName = getName(loanTypeResource.getJson());
Location location = new LocationMapper().toDomain(locationResource.getJson());

assertEquals(item.getTitle(), itemContext.getString("title"));
assertEquals(item.getBarcode(), itemContext.getString("barcode"));
assertEquals(ItemStatus.PAGED.getValue(), itemContext.getString("status"));
assertEquals(item.getPrimaryContributorName(), itemContext.getString("primaryContributor"));
assertEquals(contributorNames, itemContext.getString("allContributors"));
assertEquals(item.getEnumeration(), itemContext.getString("enumeration"));
assertEquals(item.getVolume(), itemContext.getString("volume"));
assertEquals(item.getChronology(), itemContext.getString("chronology"));
assertEquals(yearCaptionsToken, itemContext.getString("yearCaption"));
assertEquals(materialTypeName, itemContext.getString("materialType"));
assertEquals(loanTypeName, itemContext.getString("loanType"));
assertEquals(copyNumber, itemContext.getString("copy"));
assertEquals(item.getNumberOfPieces(), itemContext.getString("numberOfPieces"));
assertEquals(item.getDescriptionOfPieces(), itemContext.getString("descriptionOfPieces"));
assertDatetimeEquivalent(actualCheckinDateTime, requestCheckinDateTime);
assertEquals(location.getName(), itemContext.getString("effectiveLocationSpecific"));
assertEquals(location.getPrimaryServicePoint().getName(), itemContext.getString("effectiveLocationPrimaryServicePointName"));
CallNumberComponents callNumberComponents = item.getCallNumberComponents();
assertEquals(callNumberComponents.getCallNumber(), itemContext.getString("callNumber"));
assertEquals(callNumberComponents.getPrefix(), itemContext.getString("callNumberPrefix"));
assertEquals(callNumberComponents.getSuffix(), itemContext.getString("callNumberSuffix"));

User requester = new User(requesterResource.getJson());
JsonObject requesterContext = pickSlip.getJsonObject("requester");

assertThat(requesterContext.getString("firstName"), is(requester.getFirstName()));
assertThat(requesterContext.getString("lastName"), is(requester.getLastName()));
assertThat(requesterContext.getString("middleName"), is(requester.getMiddleName()));
assertThat(requesterContext.getString("barcode"), is(requester.getBarcode()));
assertThat(requesterContext.getString("addressLine1"), is(address.getAddressLineOne()));
assertThat(requesterContext.getString("addressLine2"), is(address.getAddressLineTwo()));
assertThat(requesterContext.getString("city"), is(address.getCity()));
assertThat(requesterContext.getString("region"), is(address.getRegion()));
assertThat(requesterContext.getString("postalCode"), is(address.getPostalCode()));
assertThat(requesterContext.getString("countryId"), is(address.getCountryId().isEmpty() ?
null : ("United States")));
assertThat(requesterContext.getString("patronGroup"), is("Regular Group"));
assertThat(requesterContext.getString("departments").split("; "),
arrayContainingInAnyOrder(equalTo("test department1"), equalTo("test department2")));

JsonObject requestContext = pickSlip.getJsonObject("request");

assertThat(requestContext.getString("deliveryAddressType"),
is(addressTypeResource.getJson().getString("addressType")));
assertThat(requestContext.getString("requestExpirationDate"),
isEquivalentTo(requestExpiration.atTime(23, 59, 59).atZone(UTC)));
assertThat(requestContext.getString("holdShelfExpirationDate"),
isEquivalentTo(ZonedDateTime.of(
holdShelfExpiration.atStartOfDay(), ZoneOffset.UTC)));
assertThat(requestContext.getString("requestID"),
UUIDMatcher.is(requestResource.getId()));
assertThat(requestContext.getString("servicePointPickup"),
is(servicePoint.getJson().getString("name")));
assertThat(requestContext.getString("patronComments"), is("I need the book"));
assertThat(requestContext.getString("requestDate"), isEquivalentTo(requestDate));
}
}

@Test
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/api/support/fixtures/AddressExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public static Address RamkinResidence() {

public static Address SiriusBlack() {
return new Address(HOME_ADDRESS_TYPE, "12 Grimmauld Place",
null, "London", "London region", "123456", "UK");
null, "London", "London region", "123456", "GB");
}

public static Address mainStreet() {
public static Address mainStreet(String countryCode) {
return new Address(HOME_ADDRESS_TYPE, "16 Main St",
"Apt 3a", "Northampton", "MA", "01060", "US");
"Apt 3a", "Northampton", "MA", "01060", countryCode);
}
}

0 comments on commit 14a61ce

Please sign in to comment.