From dc52a0c74e43b3c46aeddeddc5d25715a49a9f21 Mon Sep 17 00:00:00 2001 From: "fabio.d.mota" Date: Tue, 22 Aug 2023 13:24:21 +0100 Subject: [PATCH] fix(Pool): Fix Duplicate Identifier Validation on New Business Partners --- .../pool/service/RequestValidationService.kt | 36 +++++++++++-- .../controller/LegalEntityControllerIT.kt | 52 +++++++++++++++++-- .../tractusx/bpdm/pool/util/CommonValues.kt | 1 + 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/RequestValidationService.kt b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/RequestValidationService.kt index 9fafbc850..3a3a3ac60 100644 --- a/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/RequestValidationService.kt +++ b/bpdm-pool/src/main/kotlin/org/eclipse/tractusx/bpdm/pool/service/RequestValidationService.kt @@ -21,6 +21,7 @@ package org.eclipse.tractusx.bpdm.pool.service import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerType import org.eclipse.tractusx.bpdm.common.dto.LegalEntityDto +import org.eclipse.tractusx.bpdm.common.dto.LegalEntityIdentifierDto import org.eclipse.tractusx.bpdm.common.dto.LogisticAddressDto import org.eclipse.tractusx.bpdm.pool.api.model.request.* import org.eclipse.tractusx.bpdm.pool.api.model.response.* @@ -50,9 +51,8 @@ class RequestValidationService( val legalEntityMetadata = metadataService.getMetadata(legalEntityRequests).toKeys() val addressMetadata = metadataService.getMetadata(legalAddressRequests).toKeys() - val duplicateIdentifierCandidates = getDuplicateLegalEntityCandidates(legalEntityRequests) - + val duplicateIdentifiers = findDuplicateLegalEntityIdentifiers(legalEntityRequests) return requests.flatMap { request -> val legalEntity = request.legalEntity val legalAddress = request.legalAddress @@ -78,13 +78,28 @@ class RequestValidationService( null, LegalEntityCreateError.LegalEntityDuplicateIdentifier, request.index - ) - + ) + + validateDuplicates(legalEntity,duplicateIdentifiers,request.index) validationErrors.map { Pair(request, it) } }.groupBy({ it.first }, { it.second }) } + fun validateDuplicates(legalEntity: LegalEntityDto,duplicateIdentifiers: Set, entityKey: String?) : Collection> { + val errorList = mutableListOf>() + duplicateIdentifiers.forEach { duplicate -> + if (legalEntity.identifiers.contains(duplicate)) { + val error = ErrorInfo( + LegalEntityCreateError.LegalEntityDuplicateIdentifier, + "Identifier $duplicate is duplicated among legal entities in the request", + entityKey + ) + errorList.add(error) + } + } + + return errorList + } fun validateLegalEntityUpdates( requests: Collection ): Map>> { @@ -225,6 +240,19 @@ class RequestValidationService( }.groupBy({ it.first }, { it.second }) } + + fun findDuplicateLegalEntityIdentifiers( + legalEntityRequests: List + ): Set { + val allIdentifiers = legalEntityRequests.flatMap { it.identifiers } + + return allIdentifiers.groupBy { it } + .filter { it.value.size > 1 } + .keys + .toSet() + } + + private fun validateIdentifierTypesExists(request: LegalEntityDto, existingTypes: Set, error: ERROR, entityKey: String?) : Collection> { val requestedTypes = request.identifiers.map { it.type } diff --git a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/controller/LegalEntityControllerIT.kt b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/controller/LegalEntityControllerIT.kt index 5a578189f..8c96ceee1 100644 --- a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/controller/LegalEntityControllerIT.kt +++ b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/controller/LegalEntityControllerIT.kt @@ -25,10 +25,7 @@ import org.eclipse.tractusx.bpdm.common.dto.response.LogisticAddressVerboseDto import org.eclipse.tractusx.bpdm.common.dto.response.PoolLegalEntityVerboseDto 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.LegalAddressVerboseDto -import org.eclipse.tractusx.bpdm.pool.api.model.response.LegalEntityCreateError -import org.eclipse.tractusx.bpdm.pool.api.model.response.LegalEntityPartnerCreateVerboseDto -import org.eclipse.tractusx.bpdm.pool.api.model.response.LegalEntityUpdateError +import org.eclipse.tractusx.bpdm.pool.api.model.response.* import org.eclipse.tractusx.bpdm.pool.util.* import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Disabled @@ -87,6 +84,53 @@ class LegalEntityControllerIT @Autowired constructor( assertThat(response.errorCount).isEqualTo(0) } + /** + * Given no legal entities + * When creating new legal entity with duplicate identifiers on legal entity and address + * Then new legal entity is returned with error + */ + @Test + fun `create new legal entity and get duplicate error`() { + + val expectedBpn = CommonValues.bpnL1 + val expected = with(ResponseValues.legalEntityUpsert1) { + copy( + legalEntity = legalEntity.copy( + bpnl = expectedBpn + ) + ) + } + + + val toCreate = RequestValues.legalEntityCreate1 + val secondRequest = toCreate.copy(index = CommonValues.index4) + val toCreate2 = RequestValues.legalEntityCreate3 + + // Create a new instance of LogisticAddressDto with the modified identifiers list + val response = poolClient.legalEntities().createBusinessPartners(listOf(toCreate,secondRequest,toCreate2)) + + val identifier = toCreate.legalEntity.identifiers.toList().get(0) + + + + val expectedErrors = listOf( + ErrorInfo( + LegalEntityCreateError.LegalEntityDuplicateIdentifier, + "Identifier $identifier is duplicated among legal entities in the request", + toCreate.index + ) , + ErrorInfo( + LegalEntityCreateError.LegalEntityDuplicateIdentifier, + "Identifier $identifier is duplicated among legal entities in the request", + secondRequest.index + ) + ) + + assertThat(response.errorCount).isEqualTo(2) + testHelpers.assertRecursively(response.errors) + .isEqualTo(expectedErrors) + } + /** * Given no legal entities * When creating new legal entities diff --git a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/util/CommonValues.kt b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/util/CommonValues.kt index 5bc9e1086..ebb80f259 100644 --- a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/util/CommonValues.kt +++ b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/util/CommonValues.kt @@ -52,6 +52,7 @@ object CommonValues { val index1 = "1" val index2 = "2" val index3 = "3" + val index4 = "4" val uuid1 = UUID.fromString("e9975a48-b190-4bf1-a7e6-73c6a1744de8")