Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/api site response should include main address #247

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.request.SiteBpnSearchRequest
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.response.SiteResponse
import org.eclipse.tractusx.bpdm.pool.api.model.request.SitePartnerCreateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.SitePartnerUpdateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.response.MainAddressResponse
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePartnerCreateResponseWrapper
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePartnerUpdateResponseWrapper
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePoolResponse
import org.springdoc.core.annotations.ParameterObject
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*
Expand Down Expand Up @@ -79,7 +79,7 @@ interface PoolSiteApi {
@GetExchange("/{bpn}")
fun getSite(
@Parameter(description = "Bpn value") @PathVariable bpn: String
): SiteResponse
): SitePoolResponse

@Operation(
summary = "Search site partners by BPNs and/or parent BPNs",
Expand All @@ -96,7 +96,7 @@ interface PoolSiteApi {
fun searchSites(
@RequestBody siteSearchRequest: SiteBpnSearchRequest,
@ParameterObject paginationRequest: PaginationRequest
): PageResponse<SiteResponse>
): PageResponse<SitePoolResponse>

@Operation(
summary = "Create new site business partners",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.pool.api.model.response

import com.fasterxml.jackson.annotation.JsonUnwrapped
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import io.swagger.v3.oas.annotations.media.Schema
import org.eclipse.tractusx.bpdm.common.dto.response.LogisticAddressResponse
import org.eclipse.tractusx.bpdm.common.dto.response.SiteResponse
import org.eclipse.tractusx.bpdm.common.service.DataClassUnwrappedJsonDeserializer

@JsonDeserialize(using = DataClassUnwrappedJsonDeserializer::class)
@Schema(name = "SitePoolResponse", description = "Site with legal entity reference.")
data class SitePoolResponse(
@field:JsonUnwrapped
val site: SiteResponse,

@Schema(description = "Main address where this site resides")
val mainAddress: LogisticAddressResponse,
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ package org.eclipse.tractusx.bpdm.pool.controller
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.request.SiteBpnSearchRequest
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.response.SiteResponse
import org.eclipse.tractusx.bpdm.pool.api.PoolSiteApi
import org.eclipse.tractusx.bpdm.pool.api.model.request.SitePartnerCreateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.SitePartnerUpdateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.response.MainAddressResponse
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePartnerCreateResponseWrapper
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePartnerUpdateResponseWrapper
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePoolResponse
import org.eclipse.tractusx.bpdm.pool.service.AddressService
import org.eclipse.tractusx.bpdm.pool.service.BusinessPartnerBuildService
import org.eclipse.tractusx.bpdm.pool.service.SiteService
Expand All @@ -49,14 +49,14 @@ class SiteController(

override fun getSite(
bpn: String
): SiteResponse {
): SitePoolResponse {
return siteService.findByBpn(bpn.uppercase())
}

override fun searchSites(
siteSearchRequest: SiteBpnSearchRequest,
paginationRequest: PaginationRequest
): PageResponse<SiteResponse> {
): PageResponse<SitePoolResponse> {
return siteService.findByPartnerBpns(siteSearchRequest, paginationRequest)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,22 @@ fun Site.toDto(): SiteResponse {
states = states.map { it.toDto() },
bpnLegalEntity = legalEntity.bpn,
createdAt = createdAt,
updatedAt = updatedAt
updatedAt = updatedAt,
)
}

fun Site.toPoolDto(): SitePoolResponse {
return SitePoolResponse(

site = SiteResponse(
bpn,
name,
states = states.map { it.toDto() },
bpnLegalEntity = legalEntity.bpn,
createdAt = createdAt,
updatedAt = updatedAt,
),
mainAddress = mainAddress.toDto()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.request.SiteBpnSearchRequest
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.response.SiteResponse
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePoolResponse
import org.eclipse.tractusx.bpdm.pool.entity.Site
import org.eclipse.tractusx.bpdm.pool.repository.LegalEntityRepository
import org.eclipse.tractusx.bpdm.pool.repository.SiteRepository
Expand All @@ -46,18 +47,18 @@ class SiteService(
return page.toDto(page.content.map { it.toDto() })
}

fun findByPartnerBpns(siteSearchRequest: SiteBpnSearchRequest, paginationRequest: PaginationRequest): PageResponse<SiteResponse> {
fun findByPartnerBpns(siteSearchRequest: SiteBpnSearchRequest, paginationRequest: PaginationRequest): PageResponse<SitePoolResponse> {
val partners =
if (siteSearchRequest.legalEntities.isNotEmpty()) legalEntityRepository.findDistinctByBpnIn(siteSearchRequest.legalEntities) else emptyList()
val sitePage =
siteRepository.findByLegalEntityInOrBpnIn(partners, siteSearchRequest.sites, PageRequest.of(paginationRequest.page, paginationRequest.size))
fetchSiteDependencies(sitePage.toSet())
return sitePage.toDto(sitePage.content.map { it.toDto() })
return sitePage.toDto(sitePage.content.map { it.toPoolDto() })
}

fun findByBpn(bpn: String): SiteResponse {
fun findByBpn(bpn: String): SitePoolResponse {
val site = siteRepository.findByBpn(bpn) ?: throw BpdmNotFoundException("Site", bpn)
return site.toDto()
return site.toPoolDto()
}

private fun fetchSiteDependencies(sites: Set<Site>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.eclipse.tractusx.bpdm.pool.Application
import org.eclipse.tractusx.bpdm.pool.api.client.PoolClientImpl
import org.eclipse.tractusx.bpdm.pool.api.model.response.SiteCreateError
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePartnerCreateResponse
import org.eclipse.tractusx.bpdm.pool.api.model.response.SitePoolResponse
import org.eclipse.tractusx.bpdm.pool.api.model.response.SiteUpdateError
import org.eclipse.tractusx.bpdm.pool.util.*
import org.junit.jupiter.api.BeforeEach
Expand Down Expand Up @@ -64,7 +65,7 @@ class SiteControllerIT @Autowired constructor(
.let { bpn -> requestSitesOfLegalEntity(bpn).content.single().bpns }
.let { bpnSite -> requestSite(bpnSite) }
.let { siteResponse ->
assertThat(siteResponse.bpnLegalEntity).isEqualTo(importedPartner.legalEntity.bpnl)
assertThat(siteResponse.site.bpnLegalEntity).isEqualTo(importedPartner.legalEntity.bpnl)
}
}

Expand Down Expand Up @@ -106,11 +107,20 @@ class SiteControllerIT @Autowired constructor(
val siteSearchRequest = SiteBpnSearchRequest(emptyList(), listOf(bpnS1, bpnS2))
val searchResult = poolClient.sites().searchSites(siteSearchRequest, PaginationRequest())

val expectedSiteWithReference1 = ResponseValues.site1.copy(bpnLegalEntity = bpnL)
val expectedSiteWithReference2 = ResponseValues.site2.copy(bpnLegalEntity = bpnL)
val expectedSiteWithReference1 = SitePoolResponse(
site = ResponseValues.site1.copy(bpnLegalEntity = bpnL),
mainAddress = ResponseValues.addressPartner1.copy(isMainAddress = true, bpnSite = CommonValues.bpnS1)
)
val expectedSiteWithReference2 = SitePoolResponse(
site = ResponseValues.site2.copy(bpnLegalEntity = bpnL),
mainAddress = ResponseValues.addressPartner2.copy(isMainAddress = true, bpnSite = CommonValues.bpnS2)
)

testHelpers.assertRecursively(searchResult.content)
.ignoringFieldsOfTypes(Instant::class.java)
.ignoringFields(
SitePoolResponse::mainAddress.name + "." + LogisticAddressResponse::bpna.name,
)
.isEqualTo(listOf(expectedSiteWithReference1, expectedSiteWithReference2))
}

Expand Down Expand Up @@ -143,12 +153,27 @@ class SiteControllerIT @Autowired constructor(
val siteSearchRequest = SiteBpnSearchRequest(listOf(bpnL1, bpnL2))
val searchResult = poolClient.sites().searchSites(siteSearchRequest, PaginationRequest())

val expectedSiteWithReference1 = ResponseValues.site1.copy(bpnLegalEntity = bpnL1)
val expectedSiteWithReference2 = ResponseValues.site2.copy(bpnLegalEntity = bpnL1)
val expectedSiteWithReference3 = ResponseValues.site3.copy(bpnLegalEntity = bpnL2)
val expectedSiteWithReference1 =
SitePoolResponse(
site = ResponseValues.site1.copy(bpnLegalEntity = bpnL1),
mainAddress = ResponseValues.addressPartner1.copy(isMainAddress = true, bpnSite = CommonValues.bpnS1)
)
val expectedSiteWithReference2 =
SitePoolResponse(
site = ResponseValues.site2.copy(bpnLegalEntity = bpnL1),
mainAddress = ResponseValues.addressPartner2.copy(isMainAddress = true, bpnSite = CommonValues.bpnS2)
)
val expectedSiteWithReference3 =
SitePoolResponse(
site = ResponseValues.site3.copy(bpnLegalEntity = bpnL2),
mainAddress = ResponseValues.addressPartner3.copy(isMainAddress = true, bpnSite = CommonValues.bpnS3)
)

testHelpers.assertRecursively(searchResult.content)
.ignoringFieldsOfTypes(Instant::class.java)
.ignoringFields(
SitePoolResponse::mainAddress.name + "." + LogisticAddressResponse::bpna.name,
)
.isEqualTo(listOf(expectedSiteWithReference1, expectedSiteWithReference2, expectedSiteWithReference3))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ object ResponseValues {
states = listOf(siteStatus1),
bpnLegalEntity = CommonValues.bpnL1,
createdAt = CommonValues.now,
updatedAt = CommonValues.now
updatedAt = CommonValues.now,
)

val site2 = SiteResponse(
Expand All @@ -209,7 +209,7 @@ object ResponseValues {
states = listOf(siteStatus2),
bpnLegalEntity = CommonValues.bpnL2,
createdAt = CommonValues.now,
updatedAt = CommonValues.now
updatedAt = CommonValues.now,
)

val site3 = SiteResponse(
Expand All @@ -218,7 +218,7 @@ object ResponseValues {
states = listOf(siteStatus3),
bpnLegalEntity = CommonValues.bpnL3,
createdAt = CommonValues.now,
updatedAt = CommonValues.now
updatedAt = CommonValues.now,
)

val siteUpsert1 = SitePartnerCreateResponse(
Expand Down