From 14a61ce0cb41435e5bb515689538ef639dc2e75c Mon Sep 17 00:00:00 2001 From: Gurleen Kaur <34331959+gurleenkaurbp@users.noreply.github.com> Date: Fri, 27 Oct 2023 22:08:59 +0530 Subject: [PATCH] CIRC-1931 - Display country name instead of country code in slips and 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. --- .../domain/notice/TemplateContextUtil.java | 20 +- .../java/api/loans/CheckInByBarcodeTests.java | 2 +- .../java/api/requests/PickSlipsTests.java | 178 ++++++++++-------- .../api/support/fixtures/AddressExamples.java | 6 +- 4 files changed, 119 insertions(+), 87 deletions(-) diff --git a/src/main/java/org/folio/circulation/domain/notice/TemplateContextUtil.java b/src/main/java/org/folio/circulation/domain/notice/TemplateContextUtil.java index f0e3f7f83d..985a542640 100644 --- a/src/main/java/org/folio/circulation/domain/notice/TemplateContextUtil.java +++ b/src/main/java/org/folio/circulation/domain/notice/TemplateContextUtil.java @@ -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; @@ -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 @@ -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; diff --git a/src/test/java/api/loans/CheckInByBarcodeTests.java b/src/test/java/api/loans/CheckInByBarcodeTests.java index 7d53c84f7a..0c38ff2452 100644 --- a/src/test/java/api/loans/CheckInByBarcodeTests.java +++ b/src/test/java/api/loans/CheckInByBarcodeTests.java @@ -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))); diff --git a/src/test/java/api/requests/PickSlipsTests.java b/src/test/java/api/requests/PickSlipsTests.java index bdb321a28e..dc1e8a3cf4 100644 --- a/src/test/java/api/requests/PickSlipsTests.java +++ b/src/test/java/api/requests/PickSlipsTests.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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 = @@ -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"); @@ -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 diff --git a/src/test/java/api/support/fixtures/AddressExamples.java b/src/test/java/api/support/fixtures/AddressExamples.java index 867d61b10a..cb3d574cb9 100644 --- a/src/test/java/api/support/fixtures/AddressExamples.java +++ b/src/test/java/api/support/fixtures/AddressExamples.java @@ -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); } }