diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateAddressApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateAddressApi.kt index 5dc3d83f7..e10585d2f 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateAddressApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateAddressApi.kt @@ -78,6 +78,23 @@ interface GateAddressApi { @GetExchange("/input/addresses/{externalId}") fun getAddressByExternalId(@Parameter(description = "External identifier") @PathVariable externalId: String): AddressGateInputResponse + @Operation( + summary = "Get page of addresses filtered by a collection of externalIds", + description = "Get page of addresses filtered by a collection of externalIds." + ) + @ApiResponses( + value = [ + ApiResponse(responseCode = "200", description = "The requested page of addresses"), + ApiResponse(responseCode = "400", description = "On malformed pagination request", content = [Content()]), + ] + ) + @PostMapping("/input/addresses/search") + @PostExchange("/input/addresses/search") + fun getAddressesByExternalIds( + @ParameterObject @Valid paginationRequest: PaginationStartAfterRequest, + @RequestBody externalIds: Collection + ): PageStartAfterResponse + @Operation( summary = "Get page of addresses", diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressController.kt index 4f8516547..befa2a993 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressController.kt @@ -19,6 +19,8 @@ package org.eclipse.tractusx.bpdm.gate.controller +import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest +import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.GateAddressApi import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputResponse @@ -59,6 +61,13 @@ class AddressController( return addressService.getAddressByExternalId(externalId) } + override fun getAddressesByExternalIds( + paginationRequest: PaginationStartAfterRequest, + externalIds: Collection + ): PageStartAfterResponse { + return addressService.getAddresses(limit = paginationRequest.limit, startAfter = paginationRequest.startAfter, externalIds = externalIds) + } + override fun getAddresses(paginationRequest: PaginationStartAfterRequest): PageStartAfterResponse { return addressService.getAddresses(paginationRequest.limit, paginationRequest.startAfter) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt index f269c4df6..3e6b67032 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt @@ -51,8 +51,8 @@ class AddressService( ) { private val logger = KotlinLogging.logger { } - fun getAddresses(limit: Int, startAfter: String?): PageStartAfterResponse { - val addressesPage = saasClient.getAddresses(limit, startAfter) + fun getAddresses(limit: Int, startAfter: String?, externalIds: Collection? = null): PageStartAfterResponse { + val addressesPage = saasClient.getAddresses(limit, startAfter, externalIds) val validEntries = addressesPage.values.filter { validateAddressBusinessPartner(it) } val addressesWithParent = validEntries.map { Pair(it, inputSaasMappingService.toParentLegalEntityExternalId(it)!!) } diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressControllerInputIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressControllerInputIT.kt index 005a46e76..eeb18267c 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressControllerInputIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/AddressControllerInputIT.kt @@ -323,6 +323,90 @@ internal class AddressControllerInputIT @Autowired constructor( ) } + + /** + * Given addresses exists in SaaS + * When getting addresses page based on external id list + * Then addresses page mapped to the catena data model should be returned + */ + @Test + fun `get addresses filter by external ids`() { + val addressesSaas = listOf( + SaasValues.addressBusinessPartnerWithRelations1, + SaasValues.addressBusinessPartnerWithRelations2 + ) + + val parentsSaas = listOf( + SaasValues.legalEntityResponse1, + SaasValues.siteBusinessPartner1 + ) + + val expectedAddresses = listOf( + ResponseValues.addressGateInputResponse1, + ResponseValues.addressGateInputResponse2, + ) + + val limit = 2 + val startAfter = "Aaa111" + val nextStartAfter = "Aaa222" + val total = 10 + val invalidEntries = 0 + + wireMockServer.stubFor( + get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) + .withQueryParam("externalId", absent()) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBody( + objectMapper.writeValueAsString( + PagedResponseSaas( + limit = limit, + startAfter = startAfter, + nextStartAfter = nextStartAfter, + total = total, + values = addressesSaas + ) + ) + ) + ) + ) + + wireMockServer.stubFor( + get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) + .withQueryParam("externalId", matching(".*")) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBody( + objectMapper.writeValueAsString( + PagedResponseSaas( + limit = parentsSaas.size, + startAfter = null, + nextStartAfter = null, + total = parentsSaas.size, + values = parentsSaas + ) + ) + ) + ) + ) + + val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val pageResponse = gateClient.addresses().getAddresses(paginationValue) + val listExternalIds = addressesSaas.mapNotNull { it.externalId } + gateClient.addresses().getAddressesByExternalIds(paginationValue, listExternalIds) + + assertThat(pageResponse).isEqualTo( + PageStartAfterResponse( + total = total, + nextStartAfter = nextStartAfter, + content = expectedAddresses, + invalidEntries = invalidEntries + ) + ) + } + /** * Given invalid addresses in SaaS * When getting addresses page