Skip to content

Commit

Permalink
feat(Gate): Add endpoint to get bpns with filter on external ids from…
Browse files Browse the repository at this point in the history
… Saas
  • Loading branch information
fabiodmota committed Apr 18, 2023
1 parent 115373f commit 554fea0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
): PageStartAfterResponse<AddressGateInputResponse>


@Operation(
summary = "Get page of addresses",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,6 +61,13 @@ class AddressController(
return addressService.getAddressByExternalId(externalId)
}

override fun getAddressesByExternalIds(
paginationRequest: PaginationStartAfterRequest,
externalIds: Collection<String>
): PageStartAfterResponse<AddressGateInputResponse> {
return addressService.getAddresses(limit = paginationRequest.limit, startAfter = paginationRequest.startAfter, externalIds = externalIds)
}

override fun getAddresses(paginationRequest: PaginationStartAfterRequest): PageStartAfterResponse<AddressGateInputResponse> {
return addressService.getAddresses(paginationRequest.limit, paginationRequest.startAfter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class AddressService(
) {
private val logger = KotlinLogging.logger { }

fun getAddresses(limit: Int, startAfter: String?): PageStartAfterResponse<AddressGateInputResponse> {
val addressesPage = saasClient.getAddresses(limit, startAfter)
fun getAddresses(limit: Int, startAfter: String?, externalIds: Collection<String>? = null): PageStartAfterResponse<AddressGateInputResponse> {
val addressesPage = saasClient.getAddresses(limit, startAfter, externalIds)
val validEntries = addressesPage.values.filter { validateAddressBusinessPartner(it) }

val addressesWithParent = validEntries.map { Pair(it, inputSaasMappingService.toParentLegalEntityExternalId(it)!!) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 554fea0

Please sign in to comment.