Skip to content

Commit

Permalink
feat(pool): #556 Upsert Business Partners from Cleaning Result
Browse files Browse the repository at this point in the history
- add tests to upsert site
  • Loading branch information
rainer-exxcellent committed Oct 31, 2023
1 parent 6c709db commit 38c4493
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class TaskStepBuildService(
createLogisticAddressInternal(addressDto, bpnA)
} else {
val addressMetadataMap = metadataService.getMetadata(listOf(addressDto)).toMapping()
val updateAddress = logisticAddressRepository.findByBpn(bpnAReference.referenceValue)
val updateAddress = logisticAddressRepository.findByBpn(bpn)
if (updateAddress != null) {
updateLogisticAddress(updateAddress, addressDto, addressMetadataMap)
} else {
Expand Down Expand Up @@ -151,12 +151,12 @@ class TaskStepBuildService(
private fun createLogisticAddress(addressDto: LogisticAddressDto?, taskEntryBpnMapping: TaskEntryBpnMapping): LogisticAddress {

val bpnAReference = addressDto?.bpnAReference
val bpnL = bpnIssuingService.issueAddressBpns(1).single()
val bpnA = bpnIssuingService.issueAddressBpns(1).single()

if (bpnAReference != null && bpnAReference.referenceType == BpnReferenceType.BpnRequestIdentifier) {
taskEntryBpnMapping.addMapping(bpnAReference, bpnL)
taskEntryBpnMapping.addMapping(bpnAReference, bpnA)
}
val newAddress = createLogisticAddressInternal(addressDto, bpnL)
val newAddress = createLogisticAddressInternal(addressDto, bpnA)
changelogService.createChangelogEntries(
listOf(
ChangelogEntryCreateRequest(newAddress.bpn, ChangelogType.CREATE, BusinessPartnerType.ADDRESS)
Expand Down Expand Up @@ -259,7 +259,7 @@ class TaskStepBuildService(

val updateLe = legalEntityRepository.findByBpn(bpn)
if (updateLe != null) {
if (legalEntityDto.hasChanged == false) {
if (legalEntityDto.hasChanged == true) {
BusinessPartnerBuildService.updateLegalEntity(updateLe, legalEntityDto, legalEntityDto.legalName, legalEntityMetadataMap)
val addressMetadataMap = metadataService.getMetadata(listOf(legalAddress)).toMapping()
updateLogisticAddress(updateLe.legalAddress, legalAddress, addressMetadataMap)
Expand Down Expand Up @@ -299,9 +299,9 @@ class TaskStepBuildService(
createSite
} else {

val updateSite = siteRepository.findByBpn(siteDto.bpnSReference?.referenceValue!!)
val updateSite = siteRepository.findByBpn(bpn)
if (updateSite != null) {
if (siteDto.hasChanged == false) {
if (siteDto.hasChanged == true) {
BusinessPartnerBuildService.updateSite(updateSite, siteDto)
val addressMetadataMap = metadataService.getMetadata(listOf(mainAddress)).toMapping()
updateLogisticAddress(updateSite.mainAddress, mainAddress, addressMetadataMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor(
}

@Test
fun `upsert Golden Record into pool with same legal entity referenceValue to create`() {
fun `check that requests with same referenceValue don't create a new legal entity`() {

val fullBpWithLegalEntity = minFullBusinessPartner().copy(
legalEntity = minValidLegalEntity(
Expand All @@ -103,7 +103,7 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor(
}

@Test
fun `upsert Golden Record into pool with differrent legal entity referenceValue to create`() {
fun `upsert Golden Record into pool with different legal entity referenceValue to create`() {

val fullBpWithLegalEntity = minFullBusinessPartner().copy(
legalEntity = minValidLegalEntity(
Expand All @@ -128,6 +128,52 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor(
assertThat(createdLegalEntity1.legalEntity.bpnl).isNotEqualTo(resultSteps2[0].businessPartner?.legalEntity?.bpnLReference?.referenceValue!!)
val createdLegalEntity2 = poolClient.legalEntities.getLegalEntity(resultSteps2[0].businessPartner?.legalEntity?.bpnLReference?.referenceValue!!)
assertThat(resultSteps2[0].businessPartner?.generic?.bpnL).isEqualTo(createdLegalEntity2.legalEntity.bpnl)
}


@Test
fun `insert Site from orchestrator`() {

val fullBpWithSite = minFullBusinessPartner().copy(
legalEntity = minValidLegalEntity(
bpnLReference = BpnReferenceDto(referenceValue = "123", referenceType = BpnRequestIdentifier),
bpnAReference = BpnReferenceDto(referenceValue = "222", referenceType = BpnRequestIdentifier)
),
site = minValidSite( bpnSReference = BpnReferenceDto(referenceValue = "siteRefValue", referenceType = BpnRequestIdentifier),
bpnAReference = BpnReferenceDto(referenceValue = "mainAddressRefValue", referenceType = BpnRequestIdentifier))
)

val result = cleanStep(taskId = "TASK_1", businessPartner = fullBpWithSite)
val createdSite = poolClient.sites.getSite(result[0].businessPartner?.site?.bpnSReference?.referenceValue!!)
assertThat(result[0].taskId).isEqualTo("TASK_1")
assertThat(result[0].errors).hasSize(0)
assertThat(createdSite.site.name).isEqualTo(fullBpWithSite.site?.name)
}


@Test
fun `update Site from orchestrator`() {

val fullBpWithSite = minFullBusinessPartner().copy(
legalEntity = minValidLegalEntity(
bpnLReference = BpnReferenceDto(referenceValue = "leRfValue", referenceType = BpnRequestIdentifier),
bpnAReference = BpnReferenceDto(referenceValue = "legalAddressValue", referenceType = BpnRequestIdentifier)
),
site = minValidSite( bpnSReference = BpnReferenceDto(referenceValue = "siteRefValue", referenceType = BpnRequestIdentifier),
bpnAReference = BpnReferenceDto(referenceValue = "mainAddressRefValue", referenceType = BpnRequestIdentifier))
)

val result = cleanStep(taskId = "TASK_1", businessPartner = fullBpWithSite)
val createdSite = poolClient.sites.getSite(result[0].businessPartner?.site?.bpnSReference?.referenceValue!!)
assertThat(createdSite.site.name).isEqualTo(fullBpWithSite.site?.name)

val updateCopy = fullBpWithSite.copy (
site = fullBpWithSite.site?.copy(name = "ChangedName", hasChanged = true)
)
val result2 = cleanStep(taskId = "TASK_1", businessPartner = updateCopy)
val updatedSite = poolClient.sites.getSite(result2[0].businessPartner?.site?.bpnSReference?.referenceValue!!)
assertThat(updatedSite.site.name).isEqualTo("ChangedName")


}

Expand Down Expand Up @@ -172,6 +218,21 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor(
)
}

fun minValidSite(bpnSReference: BpnReferenceDto, bpnAReference: BpnReferenceDto): SiteDto {

return SiteDto(
bpnSReference = bpnSReference,
name = "siteName_" + bpnSReference.referenceValue,
mainAddress = LogisticAddressDto(
bpnAReference = bpnAReference,
physicalPostalAddress = PhysicalPostalAddressDto(
country = CountryCode.DE,
city = "City" + bpnSReference.referenceValue
)
)
)
}

fun assertTaskError(step: TaskStepResultEntryDto, taskId: String, error: CleaningError) {

assertThat(step.taskId).isEqualTo(taskId)
Expand Down

0 comments on commit 38c4493

Please sign in to comment.