From e4b1a36ef28d50334125ae0b8ef45b43cb58a906 Mon Sep 17 00:00:00 2001 From: rschneider <97682836+rainer-exxcellent@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:15:38 +0200 Subject: [PATCH] feat(pool): #432 Upsert Business Partners from Cleaning Result - add BpnRequestIdentifierMapping entity - map RequestIdentifier to bpn --- .../entity/BpnRequestIdentifierMapping.kt | 41 ++++++++++ .../BpnRequestIdentifierRepository.kt | 30 +++++++ .../bpdm/pool/service/TaskEntryBpnMapping.kt | 75 +++++++++++++++++ .../bpdm/pool/service/TaskStepBuildService.kt | 81 ++++++++++--------- .../service/TaskStepFetchAndReserveService.kt | 13 ++- .../db/migration/V4_0_0_9__bpn_mapping.sql | 20 +++++ .../TaskStepFetchAndReserveServiceTest.kt | 56 ++++++++++++- 7 files changed, 275 insertions(+), 41 deletions(-) create mode 100644 bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/entity/BpnRequestIdentifierMapping.kt create mode 100644 bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/repository/BpnRequestIdentifierRepository.kt create mode 100644 bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskEntryBpnMapping.kt create mode 100644 bpdm-pool/src/main/resources/db/migration/V4_0_0_9__bpn_mapping.sql diff --git a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/entity/BpnRequestIdentifierMapping.kt b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/entity/BpnRequestIdentifierMapping.kt new file mode 100644 index 000000000..b684dbf83 --- /dev/null +++ b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/entity/BpnRequestIdentifierMapping.kt @@ -0,0 +1,41 @@ +/******************************************************************************* + * 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.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.Index +import jakarta.persistence.Table +import org.eclipse.tractusx.bpdm.common.model.BaseEntity + +@Entity +@Table( + name = "bpn_requestidentifier_mapping", + indexes = [ + Index(columnList = "request_identifier"), + ] +) +class BpnRequestIdentifierMapping( + + @Column(name = "request_identifier", unique = true, nullable = false) + var requestIdentifier: String, + @Column(name = "bpn", nullable = false) + var bpn: String +) : BaseEntity() \ No newline at end of file diff --git a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/repository/BpnRequestIdentifierRepository.kt b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/repository/BpnRequestIdentifierRepository.kt new file mode 100644 index 000000000..7ff7f5034 --- /dev/null +++ b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/repository/BpnRequestIdentifierRepository.kt @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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.repository + +import org.eclipse.tractusx.bpdm.pool.entity.BpnRequestIdentifierMapping +import org.springframework.data.repository.CrudRepository +import org.springframework.data.repository.PagingAndSortingRepository + +interface BpnRequestIdentifierRepository : PagingAndSortingRepository, CrudRepository { + + fun findDistinctByRequestIdentifierIn(requestIdentifiers: Collection): Set + +} \ No newline at end of file diff --git a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskEntryBpnMapping.kt b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskEntryBpnMapping.kt new file mode 100644 index 000000000..1d85f26ec --- /dev/null +++ b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskEntryBpnMapping.kt @@ -0,0 +1,75 @@ +/******************************************************************************* + * 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.service + +import org.eclipse.tractusx.bpdm.pool.entity.BpnRequestIdentifierMapping +import org.eclipse.tractusx.bpdm.pool.repository.BpnRequestIdentifierRepository +import org.eclipse.tractusx.orchestrator.api.model.BpnReferenceDto +import org.eclipse.tractusx.orchestrator.api.model.BpnReferenceType +import org.eclipse.tractusx.orchestrator.api.model.TaskStepReservationEntryDto + +class TaskEntryBpnMapping(taskEntries: List, bpnRequestIdentifierRepository: BpnRequestIdentifierRepository) { + + private val bpnByRequestIdentifier: MutableMap + private val createdBpnByRequestIdentifier: MutableMap = mutableMapOf() + init{ + this.bpnByRequestIdentifier = readRequestMappings(taskEntries, bpnRequestIdentifierRepository) + + } + + private fun readRequestMappings(taskEntries: List, bpnRequestIdentifierRepository: BpnRequestIdentifierRepository ): MutableMap { + + val references = taskEntries.mapNotNull { it.businessPartner.legalEntity?.bpnLReference } + + taskEntries.mapNotNull { it.businessPartner.site?.bpnSReference } + + taskEntries.mapNotNull { it.businessPartner.address?.bpnAReference } + + val usedRequestIdentifiers: Collection = references.map { it.referenceValue } + + val mappings = bpnRequestIdentifierRepository.findDistinctByRequestIdentifierIn(usedRequestIdentifiers) + val bpnByRequestIdentifier = mappings + .map { it.requestIdentifier to it.bpn } + .toMap() + return bpnByRequestIdentifier.toMutableMap() + } + + fun getBpn(bpnLReference: BpnReferenceDto): String? { + + return if (bpnLReference.referenceType == BpnReferenceType.BpnRequestIdentifier) { + bpnByRequestIdentifier.get(bpnLReference.referenceValue) + } else { + bpnLReference.referenceValue + } + } + + fun addMapping(bpnLReference: BpnReferenceDto, bpn: String) { + + createdBpnByRequestIdentifier[bpnLReference.referenceValue] = bpn + bpnByRequestIdentifier[bpnLReference.referenceValue] = bpn + } + + fun writeCreatedMappingsToDb(bpnRequestIdentifierRepository: BpnRequestIdentifierRepository) { + + val mappingsToCreate = createdBpnByRequestIdentifier.map{ + BpnRequestIdentifierMapping(requestIdentifier=it.key, bpn =it.value) + } + bpnRequestIdentifierRepository.saveAll(mappingsToCreate) + } + +} \ No newline at end of file diff --git a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepBuildService.kt b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepBuildService.kt index 9f4f7fc8d..43ab66f13 100644 --- a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepBuildService.kt +++ b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepBuildService.kt @@ -65,23 +65,24 @@ class TaskStepBuildService( } @Transactional - fun upsertBusinessPartner(taskEntry: TaskStepReservationEntryDto): TaskStepResultEntryDto { + fun upsertBusinessPartner(taskEntry: TaskStepReservationEntryDto, taskEntryBpnMapping: TaskEntryBpnMapping): TaskStepResultEntryDto { // TODO associate generated BPN with BPN request identifier val businessPartnerDto = taskEntry.businessPartner var siteResult: SiteDto? = null var addressResult: LogisticAddressDto? = null - val legalEntity = upsertLegalEntity(businessPartnerDto.legalEntity) + val legalEntity = upsertLegalEntity(businessPartnerDto.legalEntity, taskEntryBpnMapping) var siteEntity: Site? = null if (businessPartnerDto.site != null) { - siteEntity = upsertSite(businessPartnerDto.site, legalEntity) + siteEntity = upsertSite(businessPartnerDto.site, legalEntity, taskEntryBpnMapping) siteResult = businessPartnerDto.site!!.copy( bpnSReference = BpnReferenceDto(referenceValue = siteEntity.bpn, referenceType = BpnReferenceType.Bpn) ) } + var addressEntity: LogisticAddress? = null if (businessPartnerDto.address != null) { - val addressEntity = upsertLogisticAddress(businessPartnerDto.address, legalEntity, siteEntity) + addressEntity = upsertLogisticAddress(businessPartnerDto.address, legalEntity, siteEntity, taskEntryBpnMapping) addressResult = businessPartnerDto.address!!.copy( bpnAReference = BpnReferenceDto(referenceValue = addressEntity.bpn, referenceType = BpnReferenceType.Bpn) ) @@ -90,7 +91,11 @@ class TaskStepBuildService( return TaskStepResultEntryDto( taskId = taskEntry.taskId, businessPartner = BusinessPartnerFullDto( - generic = businessPartnerDto.generic, + generic = businessPartnerDto.generic.copy( + bpnL = legalEntity.bpn, + bpnS = siteEntity?.bpn, + bpnA = addressEntity?.bpn + ), legalEntity = businessPartnerDto.legalEntity!!.copy( bpnLReference = BpnReferenceDto(referenceValue = legalEntity.bpn, referenceType = BpnReferenceType.Bpn) ), @@ -103,21 +108,20 @@ class TaskStepBuildService( private fun upsertLogisticAddress( addressDto: LogisticAddressDto?, legalEntity: LegalEntity, - siteEntity: Site? + siteEntity: Site?, + taskEntryBpnMapping: TaskEntryBpnMapping ): LogisticAddress { val bpnAReference = addressDto?.bpnAReference ?: throw BpdmValidationException(CleaningError.BPNA_IS_NULL.message) - val isCreate = bpnAReference.referenceType == BpnReferenceType.BpnRequestIdentifier - val changelogType = - if (isCreate) ChangelogType.CREATE else ChangelogType.UPDATE + val bpn = taskEntryBpnMapping.getBpn(bpnAReference) - val upsertAddress = if (isCreate) { - val bpnLA = bpnIssuingService.issueAddressBpns(1).single() - createLogisticAddressInternal(addressDto, bpnLA) + val upsertAddress = if (bpn == null) { + val bpnA = bpnIssuingService.issueAddressBpns(1).single() + taskEntryBpnMapping.addMapping(bpnAReference, bpnA) + createLogisticAddressInternal(addressDto, bpnA) } else { val addressMetadataMap = metadataService.getMetadata(listOf(addressDto)).toMapping() - val updateAddress = logisticAddressRepository.findByBpn(bpnAReference.referenceValue) if (updateAddress != null) { updateLogisticAddress(updateAddress, addressDto, addressMetadataMap) @@ -132,6 +136,9 @@ class TaskStepBuildService( upsertAddress.legalEntity = legalEntity } logisticAddressRepository.save(upsertAddress) + + val changelogType = + if (bpn == null) ChangelogType.CREATE else ChangelogType.UPDATE changelogService.createChangelogEntries( listOf( ChangelogEntryCreateRequest(upsertAddress.bpn, changelogType, BusinessPartnerType.ADDRESS) @@ -141,12 +148,15 @@ class TaskStepBuildService( return upsertAddress } - private fun createLogisticAddress( - addressDto: LogisticAddressDto? - ): LogisticAddress { + private fun createLogisticAddress(addressDto: LogisticAddressDto?, taskEntryBpnMapping: TaskEntryBpnMapping): LogisticAddress { - val bpnLA = bpnIssuingService.issueAddressBpns(1) - val newAddress = createLogisticAddressInternal(addressDto, bpnLA[0]) + val bpnAReference = addressDto?.bpnAReference + val bpnL = bpnIssuingService.issueAddressBpns(1).single() + + if (bpnAReference != null && bpnAReference.referenceType == BpnReferenceType.BpnRequestIdentifier) { + taskEntryBpnMapping.addMapping(bpnAReference, bpnL) + } + val newAddress = createLogisticAddressInternal(addressDto, bpnL) changelogService.createChangelogEntries( listOf( ChangelogEntryCreateRequest(newAddress.bpn, ChangelogType.CREATE, BusinessPartnerType.ADDRESS) @@ -323,28 +333,26 @@ class TaskStepBuildService( fun upsertLegalEntity( - legalEntityDto: LegalEntityDto? + legalEntityDto: LegalEntityDto?, taskEntryBpnMapping: TaskEntryBpnMapping ): LegalEntity { val bpnLReference = legalEntityDto?.bpnLReference ?: throw BpdmValidationException(CleaningError.LEGAL_ENTITY_IS_NULL.message) val legalAddress = legalEntityDto.legalAddress ?: throw BpdmValidationException(CleaningError.LEGAL_ADDRESS_IS_NULL.message) - - val isCreate = bpnLReference.referenceType == BpnReferenceType.BpnRequestIdentifier - val changelogType = - if (isCreate) ChangelogType.CREATE else ChangelogType.UPDATE - val legalEntityMetadataMap = metadataService.getMetadata(listOf(legalEntityDto)).toMapping() - val upsertLe = if (isCreate) { + val bpn = taskEntryBpnMapping.getBpn(bpnLReference) + + val upsertLe = if (bpn == null) { val bpnL = bpnIssuingService.issueLegalEntityBpns(1).single() - val createdLe = createLegalEntity(legalEntityDto, bpnL, legalEntityMetadataMap) - val address = createLogisticAddress(legalAddress) + taskEntryBpnMapping.addMapping(bpnLReference, bpnL) + val createdLe = createLegalEntity(legalEntityDto, bpnL, legalEntityMetadataMap) + val address = createLogisticAddress(legalAddress, taskEntryBpnMapping) createdLe.legalAddress = address address.legalEntity = createdLe createdLe } else { - val updateLe = legalEntityRepository.findByBpn(bpnLReference.referenceValue) + val updateLe = legalEntityRepository.findByBpn(bpn) if (updateLe != null) { if (legalEntityDto.hasChanged == false) { updateLegalEntity(updateLe, legalEntityDto, @@ -358,6 +366,8 @@ class TaskStepBuildService( updateLe } legalEntityRepository.save(upsertLe) + val changelogType = + if (bpn == null) ChangelogType.CREATE else ChangelogType.UPDATE changelogService.createChangelogEntries( listOf( ChangelogEntryCreateRequest(upsertLe.bpn, changelogType, BusinessPartnerType.LEGAL_ENTITY) @@ -367,20 +377,19 @@ class TaskStepBuildService( return upsertLe } - private fun upsertSite( - siteDto: SiteDto?, - legalEntity: LegalEntity - ): Site { + private fun upsertSite(siteDto: SiteDto?, legalEntity: LegalEntity, taskEntryBpnMapping: TaskEntryBpnMapping): Site { + val bpnSReference = siteDto?.bpnSReference ?: throw BpdmValidationException(CleaningError.BPNS_IS_NULL.message) val mainAddress = siteDto.mainAddress ?: throw BpdmValidationException(CleaningError.MAINE_ADDRESS_IS_NULL.message) - val isCreate = bpnSReference.referenceType == BpnReferenceType.BpnRequestIdentifier - val changelogType = if (isCreate) ChangelogType.CREATE else ChangelogType.UPDATE + val bpn = taskEntryBpnMapping.getBpn(bpnSReference) + val changelogType = if (bpn == null) ChangelogType.CREATE else ChangelogType.UPDATE - val upsertSite = if (isCreate) { + val upsertSite = if (bpn == null) { val bpnS = bpnIssuingService.issueSiteBpns(1).single() val createSite = createSite(siteDto, bpnS, legalEntity) - val address = createLogisticAddress(mainAddress) + taskEntryBpnMapping.addMapping(bpnSReference, bpnS) + val address = createLogisticAddress(mainAddress, taskEntryBpnMapping) createSite.mainAddress = address address.site = createSite createSite diff --git a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveService.kt b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveService.kt index 37373c65d..a9a1a47f1 100644 --- a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveService.kt +++ b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveService.kt @@ -25,6 +25,7 @@ import org.eclipse.tractusx.bpdm.common.dto.IBaseLogisticAddressDto import org.eclipse.tractusx.bpdm.pool.api.model.response.ErrorInfo import org.eclipse.tractusx.bpdm.pool.api.model.response.LegalEntityCreateError import org.eclipse.tractusx.bpdm.pool.exception.BpdmValidationException +import org.eclipse.tractusx.bpdm.pool.repository.BpnRequestIdentifierRepository import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient import org.eclipse.tractusx.orchestrator.api.model.* import org.springframework.scheduling.annotation.Scheduled @@ -35,6 +36,7 @@ class TaskStepFetchAndReserveService( private val orchestrationClient: OrchestrationApiClient, private val taskStepBuildService: TaskStepBuildService, private val requestValidationService: RequestValidationService, + private val bpnRequestIdentifierRepository: BpnRequestIdentifierRepository ) { private val logger = KotlinLogging.logger { } @@ -60,20 +62,23 @@ class TaskStepFetchAndReserveService( fun upsertGoldenRecordIntoPool(taskEntries: List): List { + val taskEntryBpnMapping = TaskEntryBpnMapping(taskEntries, bpnRequestIdentifierRepository) //TODO Implement validation for sites, ... val validationStepErrorsByEntry = validateLegalEntityCreateTasks(taskEntries) - return taskEntries.map { + val taksResults = taskEntries.map { val existingEntryError = validationStepErrorsByEntry.get(it) - existingEntryError ?: businessPartnerTaskResult(it) + existingEntryError ?: businessPartnerTaskResult(it, taskEntryBpnMapping) } + taskEntryBpnMapping.writeCreatedMappingsToDb(bpnRequestIdentifierRepository) + return taksResults } - fun businessPartnerTaskResult(taskStep: TaskStepReservationEntryDto): TaskStepResultEntryDto { + private fun businessPartnerTaskResult(taskStep: TaskStepReservationEntryDto, taskEntryBpnMapping: TaskEntryBpnMapping): TaskStepResultEntryDto { return try { - taskStepBuildService.upsertBusinessPartner(taskStep) + taskStepBuildService.upsertBusinessPartner(taskStep, taskEntryBpnMapping) } catch (ex: BpdmValidationException) { TaskStepResultEntryDto( taskId = taskStep.taskId, diff --git a/bpdm-pool/src/main/resources/db/migration/V4_0_0_9__bpn_mapping.sql b/bpdm-pool/src/main/resources/db/migration/V4_0_0_9__bpn_mapping.sql new file mode 100644 index 000000000..abef88d71 --- /dev/null +++ b/bpdm-pool/src/main/resources/db/migration/V4_0_0_9__bpn_mapping.sql @@ -0,0 +1,20 @@ +-- field Rules +create table bpn_requestidentifier_mapping +( + id bigint not null, + uuid UUID not null, + created_at timestamp without time zone not null, + updated_at timestamp without time zone not null, + request_identifier varchar(255) not null, + bpn varchar(255) not null, + primary key (id) +); + +alter table bpn_requestidentifier_mapping + add constraint uc_field_request_identifier unique (uuid); + +create index idx_request_identifier_on_bpn_requestidentifier_mapping + on bpn_requestidentifier_mapping (request_identifier); + +commit; + diff --git a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveServiceTest.kt b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveServiceTest.kt index 4ea3cccde..ea13a333f 100644 --- a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveServiceTest.kt +++ b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskStepFetchAndReserveServiceTest.kt @@ -4,6 +4,7 @@ import com.neovisionaries.i18n.CountryCode import org.assertj.core.api.Assertions.assertThat import org.eclipse.tractusx.bpdm.pool.Application import org.eclipse.tractusx.bpdm.pool.api.client.PoolClientImpl +import org.eclipse.tractusx.bpdm.pool.repository.BpnRequestIdentifierRepository import org.eclipse.tractusx.bpdm.pool.service.TaskStepBuildService.CleaningError import org.eclipse.tractusx.bpdm.pool.util.OpenSearchContextInitializer import org.eclipse.tractusx.bpdm.pool.util.PostgreSQLContextInitializer @@ -24,6 +25,7 @@ import org.springframework.test.context.ContextConfiguration @ContextConfiguration(initializers = [PostgreSQLContextInitializer::class, OpenSearchContextInitializer::class]) class TaskStepFetchAndReserveServiceTest @Autowired constructor( val cleaningStepService: TaskStepFetchAndReserveService, + val bpnRequestIdentifierRepository: BpnRequestIdentifierRepository, val testHelpers: TestHelpers, val poolClient: PoolClientImpl ) { @@ -73,13 +75,65 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor( assertThat(resultSteps[0].taskId).isEqualTo("TASK_1") assertThat(resultSteps[0].errors.size).isEqualTo(0) + val bpnMappings = bpnRequestIdentifierRepository.findDistinctByRequestIdentifierIn(listOf("123","222" )) val createdLegalEntity = poolClient.legalEntities.getLegalEntity(resultSteps[0].businessPartner?.legalEntity?.bpnLReference?.referenceValue!!) assertThat(createdLegalEntity.legalAddress.bpnLegalEntity).isNotNull() + assertThat(bpnMappings.size).isEqualTo(2) + assertThat(resultSteps[0].businessPartner?.generic?.bpnL).isEqualTo(createdLegalEntity.legalEntity.bpnl) + } + + @Test + fun `upsert Golden Record into pool with same legal entity referenceValue to create`() { + + val fullBpWithLegalEntity = minFullBusinessPartner().copy( + legalEntity = minValidLegalEntity( + bpnLReference = BpnReferenceDto(referenceValue = "123", referenceType = BpnRequestIdentifier), + bpnAReference = BpnReferenceDto(referenceValue = "222", referenceType = BpnRequestIdentifier) + ) + ) + val resultSteps1 = cleanStep(taskId = "TASK_1", businessPartner = fullBpWithLegalEntity) + assertThat(resultSteps1[0].taskId).isEqualTo("TASK_1") + assertThat(resultSteps1[0].errors.size).isEqualTo(0) + val createdLegalEntity1 = poolClient.legalEntities.getLegalEntity(resultSteps1[0].businessPartner?.legalEntity?.bpnLReference?.referenceValue!!) + + val resultSteps2 = cleanStep(taskId = "TASK_2", businessPartner = fullBpWithLegalEntity) + assertThat(resultSteps2[0].taskId).isEqualTo("TASK_2") + assertThat(resultSteps2[0].errors.size).isEqualTo(0) + assertThat(createdLegalEntity1.legalEntity.bpnl).isEqualTo(resultSteps2[0].businessPartner?.legalEntity?.bpnLReference?.referenceValue!!) + } + + @Test + fun `upsert Golden Record into pool with differrent legal entity referenceValue to create`() { + + val fullBpWithLegalEntity = minFullBusinessPartner().copy( + legalEntity = minValidLegalEntity( + bpnLReference = BpnReferenceDto(referenceValue = "123", referenceType = BpnRequestIdentifier), + bpnAReference = BpnReferenceDto(referenceValue = "222", referenceType = BpnRequestIdentifier) + ) + ) + val resultSteps1 = cleanStep(taskId = "TASK_1", businessPartner = fullBpWithLegalEntity) + assertThat(resultSteps1[0].taskId).isEqualTo("TASK_1") + assertThat(resultSteps1[0].errors.size).isEqualTo(0) + val createdLegalEntity1 = poolClient.legalEntities.getLegalEntity(resultSteps1[0].businessPartner?.legalEntity?.bpnLReference?.referenceValue!!) + + val fullBpWithLegalEntity2 = minFullBusinessPartner().copy( + legalEntity = minValidLegalEntity( + bpnLReference = BpnReferenceDto(referenceValue = "diffenrentBpnL", referenceType = BpnRequestIdentifier), + bpnAReference = BpnReferenceDto(referenceValue = "diffenrentBpnA", referenceType = BpnRequestIdentifier) + ) + ) + val resultSteps2 = cleanStep(taskId = "TASK_2", businessPartner = fullBpWithLegalEntity2) + assertThat(resultSteps2[0].taskId).isEqualTo("TASK_2") + assertThat(resultSteps2[0].errors.size).isEqualTo(0) + 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) + } fun cleanStep(taskId: String, businessPartner: BusinessPartnerFullDto): List { - val steps = singleTaskStep(taskId = "TASK_1", businessPartner = businessPartner) + val steps = singleTaskStep(taskId = taskId, businessPartner = businessPartner) return cleaningStepService.upsertGoldenRecordIntoPool(steps) }