Skip to content

Commit

Permalink
fix(Pool): Fix Duplicate Identifier Validation on New Business Partners
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiodmota committed Aug 24, 2023
1 parent 21424f5 commit dc52a0c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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
Expand All @@ -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<LegalEntityIdentifierDto>, entityKey: String?) : Collection<ErrorInfo<LegalEntityCreateError>> {
val errorList = mutableListOf<ErrorInfo<LegalEntityCreateError>>()
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<LegalEntityPartnerUpdateRequest>
): Map<LegalEntityPartnerUpdateRequest, Collection<ErrorInfo<LegalEntityUpdateError>>> {
Expand Down Expand Up @@ -225,6 +240,19 @@ class RequestValidationService(
}.groupBy({ it.first }, { it.second })
}


fun findDuplicateLegalEntityIdentifiers(
legalEntityRequests: List<LegalEntityDto>
): Set<LegalEntityIdentifierDto> {
val allIdentifiers = legalEntityRequests.flatMap { it.identifiers }

return allIdentifiers.groupBy { it }
.filter { it.value.size > 1 }
.keys
.toSet()
}


private fun <ERROR : ErrorCode> validateIdentifierTypesExists(request: LegalEntityDto, existingTypes: Set<String>, error: ERROR, entityKey: String?)
: Collection<ErrorInfo<ERROR>> {
val requestedTypes = request.identifiers.map { it.type }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit dc52a0c

Please sign in to comment.