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 44aa23bb8..a3feb92ef 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 @@ -30,8 +30,8 @@ import org.eclipse.tractusx.bpdm.pool.api.model.response.* import org.eclipse.tractusx.bpdm.pool.dto.LegalEntityMetadataDto import org.eclipse.tractusx.bpdm.pool.repository.* import org.eclipse.tractusx.orchestrator.api.model.BpnReferenceType +import org.eclipse.tractusx.orchestrator.api.model.BusinessPartnerFullDto import org.eclipse.tractusx.orchestrator.api.model.TaskStepReservationEntryDto -import org.eclipse.tractusx.orchestrator.api.model.TaskStepResultEntryDto import org.springframework.stereotype.Service @Service @@ -48,48 +48,38 @@ class RequestValidationService( fun validateTasks( taskEntries: List, requestMappings: TaskEntryBpnMapping ): Map>> { - val legalEntitiesToCreate = taskEntries - .filter { - it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier - && !(requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference)) - } - val legalEntitiesToUpdate = taskEntries - .filter { - it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.Bpn - || (it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier - && requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference)) - } - val sitesToCreate = taskEntries - .filter { - it.businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier - && !(requestMappings.hasBpnFor(it.businessPartner.site?.bpnSReference)) - } - val sitesToUpdate = taskEntries - .filter { - it.businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.Bpn - || (it.businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier - && requestMappings.hasBpnFor(it.businessPartner.site?.bpnSReference)) - } - return validateLegalEntitiesFromOrchestrator(taskEntries, requestMappings) + - validateLegalEntitiesToCreateFromOrchestrator(legalEntitiesToCreate) + - validateLegalEntitiesToUpdateFromOrchestrator(legalEntitiesToUpdate, requestMappings) + - validateSitesToCreateFromOrchestrator(sitesToCreate) + - validateSitesToUpdateFromOrchestrator(sitesToUpdate, requestMappings) + validateSitesFromOrchestrator(taskEntries, requestMappings) + + validateAddressesFromOrchestrator(taskEntries, requestMappings) } - fun validateLegalEntitiesFromOrchestrator(taskEntries: List, requestMappings: TaskEntryBpnMapping): Map>> { + private fun isSiteUpdate( businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping) + = (businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.Bpn + || (businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier + && requestMappings.hasBpnFor(businessPartner.site?.bpnSReference))) + + private fun isSiteCreate( businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping) + = (businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier + && !(requestMappings.hasBpnFor(businessPartner.site?.bpnSReference))) + + private fun isLegalEntityUpdate(businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping ) + = (businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.Bpn + || (businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier + && requestMappings.hasBpnFor(businessPartner.legalEntity?.bpnLReference))) + + private fun isLegalEntityCreate(businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping) + = (businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier + && !(requestMappings.hasBpnFor(businessPartner.legalEntity?.bpnLReference))) + + private fun validateLegalEntitiesFromOrchestrator(taskEntries: List, requestMappings: TaskEntryBpnMapping): Map>> { val legalEntitiesToCreate = taskEntries .filter { - it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier - && !(requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference)) + isLegalEntityCreate(it.businessPartner, requestMappings) } val legalEntitiesToUpdate = taskEntries .filter { - it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.Bpn - || (it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier - && requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference)) + isLegalEntityUpdate(it.businessPartner, requestMappings) } val (requestsWithLegalAddressNull, requestsWithLegalAddressNotNull) = legalEntitiesToCreate.partition { it.businessPartner.legalEntity?.legalAddress == null } @@ -109,118 +99,130 @@ class RequestValidationService( val leCreateErrorsByRequest = validateLegalEntitiesToCreate(createBridges) val leUpdateErrorsByRequest = validateLegalEntitiesToUpdate(updateBridges) val duplicateValidations = validateLegalEntityDuplicates(createBridges+updateBridges) - return mergeMapsWithCollectionInValue(leCreateErrorsByRequest, leUpdateErrorsByRequest, duplicateValidations, legalAddressEmptyErrorsByRequest) + val result = mergeMapsWithCollectionInValue(leCreateErrorsByRequest, leUpdateErrorsByRequest, duplicateValidations, legalAddressEmptyErrorsByRequest) + + return result.map { + it.key to it.value.distinct() + }.toMap() } - fun validateLegalEntityDuplicates(requestBridges: List): Map>> { + private fun validateSitesFromOrchestrator(taskEntries: List, requestMappings: TaskEntryBpnMapping): Map>> { - val legalEntityDtos = requestBridges.map { it.legalEntity } - val duplicatesValidator = ValidateLegalEntityIdentifiersDuplicated(legalEntityDtos, LegalEntityCreateError.LegalEntityDuplicateIdentifier) + val sitesToUpdate = taskEntries + .filter { + isSiteUpdate(it.businessPartner, requestMappings) + } - return requestBridges.associate { - it.request to duplicatesValidator.validate(it.legalEntity, it.request, it.bpnL) - }.filterValues { it.isNotEmpty() } - } + val siteErrorsByRequest = validateSitesToUpdate(sitesToUpdate.map { + SiteUpdateBridge( request = it, bpnS = requestMappings.getBpn(it.businessPartner.site?.bpnSReference!!)!! ) + }) - fun validateLegalEntitiesToCreateFromOrchestrator(taskReservationRequests: List): Map>> { + return siteErrorsByRequest + } - val (requestsWithLegalAddressNull, requestsWithLegalAddressNotNull) = taskReservationRequests.partition { it.businessPartner.legalEntity?.legalAddress == null } + private fun validateAddressesFromOrchestrator(taskEntries: List, requestMappings: TaskEntryBpnMapping): Map>> { - val legalAddressBridges = requestsWithLegalAddressNotNull - .map { AddressBridge(address = it.businessPartner.legalEntity?.legalAddress!!, request = it, bpnA = null) } - val additionalAddressBridges = requestsWithLegalAddressNotNull.filter { it.businessPartner.site == null && it.businessPartner.address != null } - .map { AddressBridge(address = it.businessPartner.address!!, request = it, bpnA = null) } + val taskWithLegalAddress = taskEntries + .filter{ it.businessPartner.legalEntity?.legalAddress != null} - val legalAddressEmptyErrorsByRequest = requestsWithLegalAddressNull.associateWith { - listOf(ErrorInfo(LegalEntityCreateError.LegalAddressIdentifierNotFound, "legal address of legal entity is Empty", it.getRequestKey())) - } + val leCreateAddresses = taskWithLegalAddress.flatMap{getAddressCreateBridgesFromLegalEntity(it, requestMappings)} + val leUpdateAddresses = taskWithLegalAddress.flatMap{getAddressUpdateBridgesFromLegalEntity(it, requestMappings)} + val siteCreateAddresses = taskWithLegalAddress.flatMap{getAddressCreateBridgesFromSite(it, requestMappings)} + val siteUpdateAddresses = taskWithLegalAddress.flatMap{getAddressUpdateBridgesFromSite(it, requestMappings)} - val addressErrorsByRequest = validateAddresses(legalAddressBridges + additionalAddressBridges, leCreateMessages) - val leErrorsByRequest = validateLegalEntitiesToCreate(requestsWithLegalAddressNotNull.map { - LegalEntityBridge(legalEntity = it.businessPartner.legalEntity!!, request = it, bpnL = null) - }) - return mergeMapsWithCollectionInValue(leErrorsByRequest, addressErrorsByRequest, legalAddressEmptyErrorsByRequest) + return mergeMapsWithCollectionInValue (validateAddresses(leCreateAddresses, leCreateMessages), + validateAddresses(leUpdateAddresses, leUpdateMessages), + validateAddresses(siteCreateAddresses, siteCreateMessages), + validateAddresses(siteUpdateAddresses, siteUpdateMessages), + ) } - fun validateLegalEntitiesToUpdateFromOrchestrator(taskReservationRequests: List, - requestMappings: TaskEntryBpnMapping): Map>> { + fun getAddressCreateBridgesFromLegalEntity(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List { - val legalAddressBridges = taskReservationRequests - .map { - AddressBridge( - address = it.businessPartner.legalEntity?.legalAddress!!, - request = it, - bpnA = requestMappings.getBpn(it.businessPartner.legalEntity?.legalAddress?.bpnAReference!!) - ) + val allAddresses: MutableList = mutableListOf() + val businessPartner = task.businessPartner + if ( isLegalEntityCreate(businessPartner, requestMappings)) { + allAddresses.add(AddressBridge(address = businessPartner.legalEntity?.legalAddress!!, request = task, bpnA = null)) + if ( businessPartner.site == null && businessPartner.address != null) { + allAddresses.add( AddressBridge(address = businessPartner.address!!, request = task, bpnA = null)) } + } + return allAddresses + } - val additionalAddressBridges = taskReservationRequests.filter { it.businessPartner.site == null && it.businessPartner.address != null } - .map { - AddressBridge( - address = it.businessPartner.address!!, - request = it, - bpnA = requestMappings.getBpn(it.businessPartner.address?.bpnAReference!!), - ) + fun getAddressUpdateBridgesFromLegalEntity(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List { + + val allAddresses: MutableList = mutableListOf() + val businessPartner = task.businessPartner + if ( isLegalEntityUpdate(businessPartner, requestMappings)) { + allAddresses.add(AddressBridge( + address = businessPartner.legalEntity?.legalAddress!!, request = task, + bpnA = requestMappings.getBpn(businessPartner.legalEntity?.legalAddress?.bpnAReference!!) + )) + if ( businessPartner.site == null && businessPartner.address != null) { + allAddresses.add( AddressBridge( + address = businessPartner.address!!, + request = task, + bpnA = requestMappings.getBpn(businessPartner.address?.bpnAReference!!), + )) } - - val leErrorsByRequest = validateLegalEntitiesToUpdate(taskReservationRequests.map { - LegalEntityBridge( - legalEntity = it.businessPartner.legalEntity!!, - request = it, - bpnL = requestMappings.getBpn(it.businessPartner.legalEntity?.bpnLReference!!)!! - ) - }) - val addressErrorsByRequest = validateAddresses(legalAddressBridges + additionalAddressBridges, leUpdateMessages) - return mergeMapsWithCollectionInValue(leErrorsByRequest, addressErrorsByRequest) + } + return allAddresses } - fun validateSitesToCreateFromOrchestrator(taskReservationRequests: List): Map>> { - - // parents have to be checked in the legal entity validation - val (requestsWithMainAddressNull, requestsWithMainAddressNotNull) = taskReservationRequests.partition { it.businessPartner.site?.mainAddress == null } + fun getAddressCreateBridgesFromSite(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List { - val siteMainAddressBridges = requestsWithMainAddressNotNull - .map { - AddressBridge(address = it.businessPartner.site?.mainAddress!!, request = it, bpnA = null) - } - val additionalAddressBridges = requestsWithMainAddressNotNull - .filter { it.businessPartner.site != null && it.businessPartner.address != null } - .map { - AddressBridge(address = it.businessPartner.address!!, request = it, bpnA = null) + val allAddresses: MutableList = mutableListOf() + val businessPartner = task.businessPartner + if ( isSiteCreate(businessPartner, requestMappings)) { + allAddresses.add(AddressBridge(address = businessPartner.site?.mainAddress!!, request = task, bpnA = null)) + if ( businessPartner.site != null && businessPartner.address != null) { + allAddresses.add( AddressBridge(address = businessPartner.address!!, request = task, bpnA = null)) } + } + return allAddresses + } - val mainAddressEmptyErrorsByRequest = requestsWithMainAddressNull.associateWith { - listOf(ErrorInfo(SiteCreateError.MainAddressIdentifierNotFound, "Site main address is Empty", it.getRequestKey())) + fun getAddressUpdateBridgesFromSite(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List { + + val allAddresses: MutableList = mutableListOf() + val businessPartner = task.businessPartner + if (isSiteUpdate(businessPartner, requestMappings)) { + allAddresses.add(AddressBridge( + address = businessPartner.site?.mainAddress!!, + request = task, + bpnA = requestMappings.getBpn(businessPartner.site?.mainAddress?.bpnAReference!!) + )) + if ( businessPartner.site != null && businessPartner.address != null) { + allAddresses.add( AddressBridge( + address = businessPartner.address!!, + request = task, + bpnA = requestMappings.getBpn(businessPartner.address?.bpnAReference!!), + )) + } } - val addressErrorsByRequest = validateAddresses(siteMainAddressBridges + additionalAddressBridges, siteCreateMessages) - return mergeMapsWithCollectionInValue(addressErrorsByRequest, mainAddressEmptyErrorsByRequest) + return allAddresses } - fun validateSitesToUpdateFromOrchestrator(taskReservationRequests: List - , requestMappings: TaskEntryBpnMapping): Map>> { - val siteMainAddressBridges = taskReservationRequests.map { - AddressBridge( - address = it.businessPartner.site?.mainAddress!!, - request = it, - bpnA = requestMappings.getBpn(it.businessPartner.site?.mainAddress?.bpnAReference!!) - ) - } - val additionalAddressBridges = taskReservationRequests.filter { it.businessPartner.site != null && it.businessPartner.address != null } - .map { - AddressBridge( - address = it.businessPartner.address!!, - request = it, - bpnA = requestMappings.getBpn(it.businessPartner.address?.bpnAReference!!), - ) - } + fun validateLegalEntityDuplicates(requestBridges: List): Map>> { - val siteErrorsByRequest = validateSitesToUpdate(taskReservationRequests.map { - SiteUpdateBridge( request = it, bpnS = requestMappings.getBpn(it.businessPartner.site?.bpnSReference!!)!! ) - }) - val addressErrorsByRequest = validateAddresses(siteMainAddressBridges + additionalAddressBridges, siteUpdateMessages) + val legalEntityDtos = requestBridges.map { it.legalEntity } + val duplicatesValidator = ValidateLegalEntityIdentifiersDuplicated(legalEntityDtos, LegalEntityCreateError.LegalEntityDuplicateIdentifier) - return mergeMapsWithCollectionInValue(siteErrorsByRequest, addressErrorsByRequest) + return requestBridges.associate { + it.request to duplicatesValidator.validate(it.legalEntity, it.request, it.bpnL) + }.filterValues { it.isNotEmpty() } + } + + fun validateAddressDuplicates(requestBridges: List): Map>> { + + val addressDtos = requestBridges.map { it.address } + val duplicatesValidator = ValidateAddressIdentifiersDuplicated(addressDtos, LegalEntityCreateError.LegalAddressDuplicateIdentifier) + + return requestBridges.associate { + it.request to duplicatesValidator.validate(it.address, it.request, it.bpnA) + }.filterValues { it.isNotEmpty() } } fun validateLegalEntitiesToCreateFromController(leCreateRequests: Collection): Map>> { @@ -316,7 +318,7 @@ class RequestValidationService( identifiersDuplicateValidator.validate(legalAddressDto, request, bridge.bpnA) if(validationErrors.isNotEmpty()) { - val existing = result[request]; + val existing = result[request] if (existing == null) { result[request] = validationErrors } else { @@ -324,7 +326,7 @@ class RequestValidationService( } } } - return result; + return result } fun validateSitesToCreateFromController( 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 cc5a8224d..95eb0bca1 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 @@ -27,9 +27,7 @@ import org.eclipse.tractusx.bpdm.pool.util.TestHelpers import org.eclipse.tractusx.orchestrator.api.model.* import org.eclipse.tractusx.orchestrator.api.model.BpnReferenceType.Bpn import org.eclipse.tractusx.orchestrator.api.model.BpnReferenceType.BpnRequestIdentifier -import org.junit.Ignore import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest @@ -448,7 +446,7 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor( ) ) ) - val updateResult = upsertGoldenRecordIntoPool(taskId = "TASK_2", businessPartner = updatedFullBpLegalEntity!!) + val updateResult = upsertGoldenRecordIntoPool(taskId = "TASK_2", businessPartner = updatedFullBpLegalEntity) assertThat(updateResult[0].taskId).isEqualTo("TASK_2") assertThat(updateResult[0].errors.size).isEqualTo(0) @@ -1054,7 +1052,8 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor( @Test fun `create multiple legal entity `() { - val referenceIds = (1..10000).toList() + val countToTest = 1000 + val referenceIds = (1..countToTest).toList() val fullBpWithLegalEntity = referenceIds.map { minFullBusinessPartner().copy( legalEntity = fullValidLegalEntity( @@ -1066,14 +1065,14 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor( val taskSteps = multipleTaskStep(fullBpWithLegalEntity) val createResults = cleaningStepService.upsertGoldenRecordIntoPool(taskSteps) - assertThat(createResults).hasSize(10000) + assertThat(createResults).hasSize(countToTest) assertThat(createResults.filter { it.errors.isNotEmpty() }).hasSize(0) val updateResults = cleaningStepService.upsertGoldenRecordIntoPool(taskSteps) - assertThat(updateResults).hasSize(10000) + assertThat(updateResults).hasSize(countToTest) assertThat(updateResults.filter { it.errors.isNotEmpty() }).hasSize(0) - val referenceIds2 = (10001..20000).toList() + val referenceIds2 = ((countToTest+1)..(2*countToTest)).toList() val fullBpWithLegalEntity2 = referenceIds2.map { minFullBusinessPartner().copy( legalEntity = fullValidLegalEntity( @@ -1084,7 +1083,7 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor( } val taskSteps2 = multipleTaskStep(fullBpWithLegalEntity2) val createResults2 = cleaningStepService.upsertGoldenRecordIntoPool(taskSteps2) - assertThat(createResults2).hasSize(10000) + assertThat(createResults2).hasSize(countToTest) assertThat(createResults2.filter { it.errors.isNotEmpty() }).hasSize(0) }