Skip to content

Commit

Permalink
fix(gate): added new changes to upsert unit tests to check for persis…
Browse files Browse the repository at this point in the history
…ted data, fixed logic on update of some entities
  • Loading branch information
alexsilva-CGI committed May 23, 2023
1 parent 37ca32f commit a64014d
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse
import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties
import org.eclipse.tractusx.bpdm.gate.containsDuplicates
import org.eclipse.tractusx.bpdm.gate.service.SiteService
import org.eclipse.tractusx.bpdm.gate.service.ValidationService
import org.springframework.http.HttpStatus
Expand All @@ -42,9 +43,9 @@ class SiteController(
) : GateSiteApi {

override fun upsertSites(sites: Collection<SiteGateInputRequest>): ResponseEntity<Unit> {
// if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) {
// return ResponseEntity(HttpStatus.BAD_REQUEST)
// }
if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
}
siteService.upsertSites(sites)
return ResponseEntity(HttpStatus.OK)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.springframework.data.repository.CrudRepository

interface LegalEntityRepository : JpaRepository<LegalEntity, Long>, CrudRepository<LegalEntity, Long> {

fun findDistinctByBpnIn(externalId: Collection<String>): Set<LegalEntity>
fun findDistinctByExternalIdIn(externalId: Collection<String>): Set<LegalEntity>

fun findByExternalId(externalId: String): LegalEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package org.eclipse.tractusx.bpdm.gate.service

import jakarta.transaction.Transactional
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.util.replace
import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputRequest
import org.eclipse.tractusx.bpdm.gate.entity.AddressIdentifier
import org.eclipse.tractusx.bpdm.gate.entity.AddressState
import org.eclipse.tractusx.bpdm.gate.entity.LogisticAddress
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
Expand All @@ -46,8 +47,12 @@ class AddressPersistenceService(

addresses.forEach { address ->
val legalEntityRecord =
address.legalEntityExternalId?.let { legalEntityRepository.findByExternalId(it) ?: throw BpdmNotFoundException("Business Partner", it) }
val siteRecord = address.siteExternalId?.let { siteEntityRepository.findByExternalId(it) ?: throw BpdmNotFoundException("Business Partner", it) }
address.legalEntityExternalId?.let { legalEntityRepository.findByExternalId(it) }
val siteRecord = address.siteExternalId?.let { siteEntityRepository.findByExternalId(it) }

// if(legalEntityRecord == null && siteRecord == null) {
// throw BpdmNotFoundException("Business Partner", "Error")
// }

val fullAddress = address.toAddressGate(legalEntityRecord, siteRecord)
addressRecord.find { it.externalId == address.externalId }?.let { existingAddress ->
Expand All @@ -64,14 +69,22 @@ class AddressPersistenceService(
address.name = changeAddress.name
address.bpn = changeAddress.bpn
address.externalId = changeAddress.externalId
address.legalEntity= changeAddress.legalEntity
address.legalEntity = changeAddress.legalEntity
address.siteExternalId = changeAddress.siteExternalId
address.physicalPostalAddress = changeAddress.physicalPostalAddress
address.alternativePostalAddress = changeAddress.alternativePostalAddress

address.identifiers.replace(changeAddress.identifiers)
address.states.replace(changeAddress.states)
address.identifiers.replace(changeAddress.identifiers.map { toEntityIdentifier(it, address) })
address.states.replace(changeAddress.states.map { toEntityAddress(it, address) })

}

fun toEntityAddress(dto: AddressState, address: LogisticAddress): AddressState {
return AddressState(dto.description, dto.validFrom, dto.validTo, dto.type, address)
}

fun toEntityIdentifier(dto: AddressIdentifier, address: LogisticAddress): AddressIdentifier {
return AddressIdentifier(dto.value, dto.type, address)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,40 @@
package org.eclipse.tractusx.bpdm.gate.service


import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.util.replace
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity
import org.eclipse.tractusx.bpdm.gate.entity.*
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class LegalEntityPersistenceService(
private val gateLegalEntityRepository: LegalEntityRepository
private val gateLegalEntityRepository: LegalEntityRepository,
private val gateAddressRepository: GateAddressRepository
) {

@Transactional
fun persistLegalEntitiesBP(legalEntities: Collection<LegalEntityGateInputRequest>) {

//finds Legal Entity by External ID
val legalEntityRecord = gateLegalEntityRepository.findDistinctByBpnIn(legalEntities.map { it.externalId })
val legalEntityRecord = gateLegalEntityRepository.findDistinctByExternalIdIn(legalEntities.map { it.externalId })

//Business Partner persist
legalEntities.forEach { legalEntity ->
val fullLegalEntity = legalEntity.toLegalEntity()
legalEntityRecord.find { it.externalId == legalEntity.externalId }?.let { existingLegalEntity ->

updateLegalEntity(existingLegalEntity, fullLegalEntity)
val logisticAddressRecord = gateAddressRepository.findByExternalId(existingLegalEntity.externalId + "_legalAddress")
?: throw BpdmNotFoundException("Business Partner", "Error")

updateAddress(logisticAddressRecord, fullLegalEntity.legalAddress)

updateLegalEntity(existingLegalEntity, fullLegalEntity, logisticAddressRecord)
gateLegalEntityRepository.save(existingLegalEntity)

} ?: run {

gateLegalEntityRepository.save(fullLegalEntity)
Expand All @@ -53,18 +62,53 @@ class LegalEntityPersistenceService(
}
}


private fun updateLegalEntity(legalEntity: LegalEntity, legalEntityRequest:LegalEntity): LegalEntity {
private fun updateLegalEntity(legalEntity: LegalEntity, legalEntityRequest: LegalEntity, logisticAddressRecord: LogisticAddress): LegalEntity {
legalEntity.bpn = legalEntityRequest.bpn
legalEntity.externalId = legalEntityRequest.externalId
legalEntity.legalForm = legalEntityRequest.legalForm
legalEntity.legalName= legalEntityRequest.legalName
legalEntity.identifiers.replace(legalEntityRequest.identifiers)
legalEntity.states.replace(legalEntityRequest.states)
legalEntity.classifications.replace(legalEntityRequest.classifications)
legalEntity.legalAddress = legalEntityRequest.legalAddress
legalEntity.legalAddress.legalEntity= legalEntity
legalEntity.legalName = legalEntityRequest.legalName
legalEntity.identifiers.replace(legalEntityRequest.identifiers.map { toEntityIdentifier(it, legalEntity) })
legalEntity.states.replace(legalEntityRequest.states.map { toEntityState(it, legalEntity) })
legalEntity.classifications.replace(legalEntityRequest.classifications.map { toEntityClassification(it, legalEntity) })
legalEntity.legalAddress = logisticAddressRecord
legalEntity.legalAddress.legalEntity = legalEntity

return legalEntity
}

fun toEntityIdentifier(dto: LegalEntityIdentifier, legalEntity: LegalEntity): LegalEntityIdentifier {
return LegalEntityIdentifier(dto.value, dto.type, dto.issuingBody, legalEntity)
}

fun toEntityClassification(dto: Classification, legalEntity: LegalEntity): Classification {
return Classification(dto.value, dto.code, dto.type, legalEntity)
}

fun toEntityState(dto: LegalEntityState, legalEntity: LegalEntity): LegalEntityState {
return LegalEntityState(dto.officialDenotation, dto.validFrom, dto.validTo, dto.type, legalEntity)
}

private fun updateAddress(address: LogisticAddress, changeAddress: LogisticAddress) {

address.name = changeAddress.name
address.bpn = changeAddress.bpn
address.externalId = changeAddress.externalId
address.legalEntity = changeAddress.legalEntity
address.siteExternalId = changeAddress.siteExternalId
address.physicalPostalAddress = changeAddress.physicalPostalAddress
address.alternativePostalAddress = changeAddress.alternativePostalAddress

address.identifiers.replace(changeAddress.identifiers.map { toEntityIdentifier(it, address) })
address.states.replace(changeAddress.states.map { toEntityAddress(it, address) })

}

fun toEntityAddress(dto: AddressState, address: LogisticAddress): AddressState {
return AddressState(dto.description, dto.validFrom, dto.validTo, dto.type, address)
}

fun toEntityIdentifier(dto: AddressIdentifier, address: LogisticAddress): AddressIdentifier {
return AddressIdentifier(dto.value, dto.type, address)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fun SiteGateInputRequest.toSiteGate(legalEntity: LegalEntity): Site {

val addressInputRequest = AddressGateInputRequest(
address = site.mainAddress,
externalId = externalId + "_mainAddress",
externalId = externalId + "_site",
legalEntityExternalId = externalId
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ package org.eclipse.tractusx.bpdm.gate.service
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.util.replace
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest
import org.eclipse.tractusx.bpdm.gate.entity.Site
import org.eclipse.tractusx.bpdm.gate.entity.*
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository
import org.springframework.stereotype.Service
Expand All @@ -31,38 +32,40 @@ import org.springframework.transaction.annotation.Transactional
@Service
class SitePersistenceService(
private val siteRepository: SiteRepository,
private val legalEntityRepository: LegalEntityRepository
private val legalEntityRepository: LegalEntityRepository,
private val addressRepository: GateAddressRepository
) {

@Transactional
fun persistSitesBP(sites: Collection<SiteGateInputRequest>) {

//finds Legal Entity by External ID
//val legalEntities = gateLegalEntityRepository.findDistinctByBpnIn(sites.map { it.legalEntityExternalId })

//Finds Site in DB
val externalIdColl: MutableCollection<String> = mutableListOf()
sites.forEach { externalIdColl.add(it.externalId) }
val siteRecord = siteRepository.findByExternalIdIn(externalIdColl)

// for (legalEntity in legalEntities) {
sites.forEach { site ->

val legalEntityRecord =
site.legalEntityExternalId.let {
legalEntityRepository.findByExternalId(site.legalEntityExternalId) ?: throw BpdmNotFoundException("Business Partner", it)
}

val fullSite = site.toSiteGate(legalEntityRecord) //TODO (Needs to receive an Legal Entity)
val fullSite = site.toSiteGate(legalEntityRecord)

siteRecord.find { it.externalId == site.externalId }?.let { existingSite ->

val logisticAddressRecord =
addressRepository.findByExternalId(site.externalId + "_site") ?: throw BpdmNotFoundException("Business Partner", "Error")

updateAddress(logisticAddressRecord, fullSite.mainAddress)

updateSite(existingSite, fullSite)
siteRepository.save(existingSite)
} ?: run {
siteRepository.save(fullSite)
}
//}
}
//}
}


Expand All @@ -72,8 +75,35 @@ class SitePersistenceService(
site.name = updatedSite.name
site.externalId = updatedSite.externalId
site.legalEntity = updatedSite.legalEntity
site.states.replace(updatedSite.states)
site.states.replace(updatedSite.states.map { toEntityAddress(it, site) })

}

fun toEntityAddress(dto: SiteState, site: Site): SiteState {
return SiteState(dto.description, dto.validFrom, dto.validTo, dto.type, site)
}

private fun updateAddress(address: LogisticAddress, changeAddress: LogisticAddress) {

address.name = changeAddress.name
address.bpn = changeAddress.bpn
address.externalId = changeAddress.externalId
address.legalEntity = changeAddress.legalEntity
address.siteExternalId = changeAddress.siteExternalId
address.physicalPostalAddress = changeAddress.physicalPostalAddress
address.alternativePostalAddress = changeAddress.alternativePostalAddress

address.identifiers.replace(changeAddress.identifiers.map { toEntityIdentifier(it, address) })
address.states.replace(changeAddress.states.map { toEntityAddress(it, address) })

}

fun toEntityAddress(dto: AddressState, address: LogisticAddress): AddressState {
return AddressState(dto.description, dto.validFrom, dto.validTo, dto.type, address)
}

fun toEntityIdentifier(dto: AddressIdentifier, address: LogisticAddress): AddressIdentifier {
return AddressIdentifier(dto.value, dto.type, address)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,14 @@ internal class AddressControllerInputIT @Autowired constructor(
RequestValues.addressGateInputRequest2
)

val legalEntity = listOf(
RequestValues.legalEntityGateInputRequest1,
)

val sites = listOf(
RequestValues.siteGateInputRequest1,
)

val parentLegalEntitiesSaas = listOf(
SaasValues.legalEntityResponse1
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequ
import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationStatus
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.eclipse.tractusx.bpdm.gate.util.*
import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.GATE_API_INPUT_LEGAL_ENTITIES_PATH
import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.SAAS_MOCK_BUSINESS_PARTNER_PATH
import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.SAAS_MOCK_FETCH_BUSINESS_PARTNER_PATH
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -60,7 +60,8 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti
internal class LegalEntityControllerInputIT @Autowired constructor(
private val webTestClient: WebTestClient,
private val objectMapper: ObjectMapper,
val gateClient: GateClient
val gateClient: GateClient,
private val legalEntityRepository: LegalEntityRepository
) {
companion object {
@RegisterExtension
Expand Down Expand Up @@ -595,4 +596,52 @@ internal class LegalEntityControllerInputIT @Autowired constructor(
assertThat(actualResponse).isEqualTo(expectedResponse)

}

/**
* When upserting legal entities
* Then SaaS upsert api should be called with the legal entity data mapped to the SaaS data model
*/
@Test
fun `upsert and persist legal entities`() {
val legalEntities = listOf(
RequestValues.legalEntityGateInputRequest1,
RequestValues.legalEntityGateInputRequest2,
)

val expectedLegalEntities = listOf(
SaasValues.legalEntityRequest1,
SaasValues.legalEntityRequest2,
)

wireMockServer.stubFor(
put(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(
objectMapper.writeValueAsString(
UpsertResponse(
emptyList(),
emptyList(),
2,
0
)
)
)
)
)

try {
gateClient.legalEntities().upsertLegalEntities(legalEntities)
} catch (e: WebClientResponseException) {
assertEquals(HttpStatus.OK, e.statusCode)
}

val legalEntityRecordExternal1 = legalEntityRepository.findByExternalId("external-1")
assertNotEquals(legalEntityRecordExternal1, null)

val legalEntityRecordExternal2 = legalEntityRepository.findByExternalId("external-2")
assertNotEquals(legalEntityRecordExternal2, null)

}
}
Loading

0 comments on commit a64014d

Please sign in to comment.