diff --git a/src/main/java/org/folio/dew/batch/acquisitions/services/ConfigurationService.java b/src/main/java/org/folio/dew/batch/acquisitions/services/ConfigurationService.java index 9552b6748..3c6b0ac72 100644 --- a/src/main/java/org/folio/dew/batch/acquisitions/services/ConfigurationService.java +++ b/src/main/java/org/folio/dew/batch/acquisitions/services/ConfigurationService.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.JsonNode; import lombok.RequiredArgsConstructor; -import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.dew.client.TenantAddressesClient; @@ -17,8 +16,6 @@ public class ConfigurationService { private static final Logger logger = LogManager.getLogger(); - private static final String ADDRESSES = "addresses"; - private static final String ID = "id"; private static final String ADDRESS = "address"; private final TenantAddressesClient tenantAddressesClient; @@ -30,28 +27,15 @@ public String getAddressConfig(UUID shipToConfigId) { return ""; } try { - JsonNode addressesResponse = tenantAddressesClient.getTenantAddresses(); + JsonNode addressResponse = tenantAddressesClient.getById(shipToConfigId.toString()); - if (addressesResponse == null || !addressesResponse.has(ADDRESSES)) { - logger.warn("getAddressConfig:: No addresses found in tenant-addresses response"); + if (addressResponse == null) { + logger.warn("getAddressConfig:: No address found for id '{}'", shipToConfigId); return ""; } - JsonNode addressesList = addressesResponse.get(ADDRESSES); - if (!addressesList.isArray() || addressesList.isEmpty()) { - logger.warn("getAddressConfig:: Addresses list is empty"); - return ""; - } - - for (JsonNode addressEntry : addressesList) { - String addressId = addressEntry.path(ID).asText(null); - if (StringUtils.equals(shipToConfigId.toString(), addressId)) { - logger.info("getAddressConfig:: Found address with id '{}'", shipToConfigId); - return addressEntry.path(ADDRESS).asText(""); - } - } - - return ""; + logger.info("getAddressConfig:: Found address with id '{}'", shipToConfigId); + return addressResponse.path(ADDRESS).asText(""); } catch (Exception e) { logger.warn("getAddressConfig:: Cannot find address by id: '{}'", shipToConfigId, e); return ""; diff --git a/src/main/java/org/folio/dew/client/TenantAddressesClient.java b/src/main/java/org/folio/dew/client/TenantAddressesClient.java index 2e474f43f..15e808f1b 100644 --- a/src/main/java/org/folio/dew/client/TenantAddressesClient.java +++ b/src/main/java/org/folio/dew/client/TenantAddressesClient.java @@ -5,11 +5,12 @@ import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name = "tenant-addresses", configuration = FeignClientConfiguration.class) public interface TenantAddressesClient { - @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) - JsonNode getTenantAddresses(); + @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + JsonNode getById(@PathVariable("id") String id); } diff --git a/src/test/java/org/folio/dew/batch/acquisitions/services/ConfigurationServiceTest.java b/src/test/java/org/folio/dew/batch/acquisitions/services/ConfigurationServiceTest.java index 1f0e95dcd..ad752a2a4 100644 --- a/src/test/java/org/folio/dew/batch/acquisitions/services/ConfigurationServiceTest.java +++ b/src/test/java/org/folio/dew/batch/acquisitions/services/ConfigurationServiceTest.java @@ -6,7 +6,6 @@ import java.util.UUID; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import org.folio.dew.client.TenantAddressesClient; @@ -36,39 +35,22 @@ void getAddressConfig_nullId_returnsEmpty() { @Test void getAddressConfig_matchingAddress_returnsAddress() { - when(tenantAddressesClient.getTenantAddresses()) - .thenReturn(createResponse(CONFIG_ID.toString(), EXPECTED_ADDRESS)); + when(tenantAddressesClient.getById(CONFIG_ID.toString())) + .thenReturn(createAddressNode(EXPECTED_ADDRESS)); assertThat(configurationService.getAddressConfig(CONFIG_ID)).isEqualTo(EXPECTED_ADDRESS); } - @Test - void getAddressConfig_noMatchingId_returnsEmpty() { - when(tenantAddressesClient.getTenantAddresses()) - .thenReturn(createResponse("other-id", "Other Address")); - - assertThat(configurationService.getAddressConfig(CONFIG_ID)).isEmpty(); - } - @Test void getAddressConfig_nullResponse_returnsEmpty() { - when(tenantAddressesClient.getTenantAddresses()).thenReturn(null); - - assertThat(configurationService.getAddressConfig(CONFIG_ID)).isEmpty(); - } - - @Test - void getAddressConfig_emptyAddressArray_returnsEmpty() { - ObjectNode response = MAPPER.createObjectNode(); - response.putArray("addresses"); - when(tenantAddressesClient.getTenantAddresses()).thenReturn(response); + when(tenantAddressesClient.getById(CONFIG_ID.toString())).thenReturn(null); assertThat(configurationService.getAddressConfig(CONFIG_ID)).isEmpty(); } @Test void getAddressConfig_clientThrowsException_returnsEmpty() { - when(tenantAddressesClient.getTenantAddresses()) + when(tenantAddressesClient.getById(CONFIG_ID.toString())) .thenThrow(new RuntimeException("Connection error")); assertThat(configurationService.getAddressConfig(CONFIG_ID)).isEmpty(); @@ -76,12 +58,9 @@ void getAddressConfig_clientThrowsException_returnsEmpty() { // -- Helper -- - private static ObjectNode createResponse(String id, String address) { - ObjectNode response = MAPPER.createObjectNode(); - ArrayNode addresses = response.putArray("addresses"); - ObjectNode entry = addresses.addObject(); - entry.put("id", id); + private static ObjectNode createAddressNode(String address) { + ObjectNode entry = MAPPER.createObjectNode(); entry.put("address", address); - return response; + return entry; } -} \ No newline at end of file +}