Skip to content

Commit

Permalink
Merge pull request #717 from catenax-ng/feat/cleaning-service/bpn-ref…
Browse files Browse the repository at this point in the history
…erence-names

feat(Cleaning Service): create BPN references based on name
  • Loading branch information
nicoprow authored Jan 22, 2024
2 parents 19749db + 1bf6927 commit 413fa04
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ package org.eclipse.tractusx.bpdm.cleaning.service


import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.cleaning.util.md5
import org.eclipse.tractusx.bpdm.common.dto.AddressType
import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient
import org.eclipse.tractusx.orchestrator.api.model.*
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.util.*

@Service
class CleaningServiceDummy(
Expand Down Expand Up @@ -101,12 +101,7 @@ class CleaningServiceDummy(
fun createSiteDtoIfNeeded(businessPartner: BusinessPartnerGenericDto, addressPartner: LogisticAddressDto): SiteDto? {
if (!shouldCreateSite(businessPartner)) return null

val siteAddressReference = when (businessPartner.address.addressType) {
AddressType.SiteMainAddress, AddressType.LegalAndSiteMainAddress -> addressPartner.bpnAReference
else -> generateNewBpnRequestIdentifier()
}

val siteMainAddress = addressPartner.copy(bpnAReference = siteAddressReference)
val siteMainAddress = addressPartner.copy(bpnAReference = generateBpnRequestIdentifier(businessPartner.createSiteMainAddressReferenceValue()))
return createSiteRepresentation(businessPartner, siteMainAddress)
}

Expand All @@ -115,45 +110,28 @@ class CleaningServiceDummy(
addressType: AddressType,
genericPartner: BusinessPartnerGenericDto
): LegalEntityDto {
val legalAddressBpnReference = if (addressType == AddressType.LegalAddress || addressType == AddressType.LegalAndSiteMainAddress) {
addressPartner.bpnAReference
} else {
generateNewBpnRequestIdentifier()
}

val legalAddressBpnReference = generateBpnRequestIdentifier(genericPartner.createLegalAddressReferenceValue())
val legalAddress = addressPartner.copy(bpnAReference = legalAddressBpnReference)

val bpnReferenceDto = createBpnReference(genericPartner.legalEntity.legalEntityBpn)

return genericPartner.toLegalEntityDto(bpnReferenceDto, legalAddress)
val legalEntityBpnReference = generateBpnRequestIdentifier(genericPartner.createLegalEntityReferenceValue())
return genericPartner.toLegalEntityDto(legalEntityBpnReference, legalAddress)

}

fun createAddressRepresentation(genericPartner: BusinessPartnerGenericDto): LogisticAddressDto {
val bpnReferenceDto = createBpnReference(genericPartner.address.addressBpn)
val bpnReferenceDto = generateNewBpnRequestIdentifier(genericPartner.createAdditionalAddressReferenceValue())
return genericPartner.toLogisticAddressDto(bpnReferenceDto)
}

fun createSiteRepresentation(genericPartner: BusinessPartnerGenericDto, siteAddressReference: LogisticAddressDto): SiteDto {
val bpnReferenceDto = createBpnReference(genericPartner.site.siteBpn)
val bpnReferenceDto = generateBpnRequestIdentifier(genericPartner.createSiteReferenceValue())
return genericPartner.toSiteDto(bpnReferenceDto, siteAddressReference)
}

fun createBpnReference(bpn: String?): BpnReferenceDto {
return if (bpn != null) {
BpnReferenceDto(bpn, BpnReferenceType.Bpn)
} else {
// Generate a new UUID and create a BpnReferenceDto object if bpnL/bpnS/bpnA is null
generateNewBpnRequestIdentifier()
}
}

private fun generateNewBpnRequestIdentifier() = BpnReferenceDto(UUID.randomUUID().toString(), BpnReferenceType.BpnRequestIdentifier)
private fun generateNewBpnRequestIdentifier(fromString: String) = BpnReferenceDto(fromString.md5(), BpnReferenceType.BpnRequestIdentifier)

fun shouldCreateSite(genericPartner: BusinessPartnerGenericDto): Boolean {
return genericPartner.address.addressType == AddressType.SiteMainAddress ||
genericPartner.address.addressType == AddressType.LegalAndSiteMainAddress ||
genericPartner.site.siteBpn != null
return genericPartner.ownerBpnL != null && genericPartner.site.name != null
}

private fun BusinessPartnerGenericDto.update(
Expand All @@ -176,5 +154,25 @@ class CleaningServiceDummy(
)
}

private fun BusinessPartnerGenericDto.createLegalEntityReferenceValue() =
"LEGAL_ENTITY" + (legalEntity.legalName ?: nameParts.joinToString { " " })

private fun BusinessPartnerGenericDto.createLegalAddressReferenceValue() =
"LEGAL_ADDRESS" + createLegalEntityReferenceValue()

private fun BusinessPartnerGenericDto.createSiteReferenceValue() =
"SITE" + createLegalEntityReferenceValue() + site.name

private fun BusinessPartnerGenericDto.createSiteMainAddressReferenceValue() =
if (address.addressType == AddressType.LegalAndSiteMainAddress)
createLegalAddressReferenceValue()
else
"SITE_MAIN_ADDRESS" + createSiteReferenceValue()

private fun BusinessPartnerGenericDto.createAdditionalAddressReferenceValue() =
"ADDITIONAL_ADDRESS" + createSiteReferenceValue()

private fun generateBpnRequestIdentifier(fromString: String) = BpnReferenceDto(fromString.md5(), BpnReferenceType.BpnRequestIdentifier)


}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ val dummyConfidenceCriteria = ConfidenceCriteriaDto(
)

fun BusinessPartnerGenericDto.toLegalEntityDto(bpnReferenceDto: BpnReferenceDto, legalAddress: LogisticAddressDto): LegalEntityDto {

return LegalEntityDto(
bpnLReference = bpnReferenceDto,
hasChanged = address.addressType in setOf(AddressType.LegalAddress, AddressType.LegalAndSiteMainAddress),
Expand Down Expand Up @@ -92,7 +91,6 @@ fun BusinessPartnerGenericDto.toLogisticAddressDto(bpnReferenceDto: BpnReference

fun BusinessPartnerGenericDto.toSiteDto(bpnReferenceDto: BpnReferenceDto, siteAddressReference: LogisticAddressDto): SiteDto {


return SiteDto(
bpnSReference = bpnReferenceDto,
hasChanged = address.addressType in setOf(AddressType.SiteMainAddress, AddressType.LegalAndSiteMainAddress),
Expand Down
Original file line number Diff line number Diff line change
@@ -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.cleaning.util

import java.math.BigInteger
import java.security.MessageDigest


fun String.md5(): String {
val md = MessageDigest.getInstance("MD5")
val digest = md.digest(this.toByteArray())
return BigInteger(1, digest).toString(16).padStart(32, '0')
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.eclipse.tractusx.bpdm.cleaning.testdata.CommonValues.expectedLegalEnt
import org.eclipse.tractusx.bpdm.cleaning.testdata.CommonValues.expectedLogisticAddressDto
import org.eclipse.tractusx.bpdm.cleaning.testdata.CommonValues.expectedSiteDto
import org.eclipse.tractusx.orchestrator.api.model.*
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -46,34 +45,19 @@ class CleaningServiceDummyTest @Autowired constructor(


@Test
fun `test processCleaningTask with BpnA present and additional address type`() {
fun `test processCleaningTask with additional address type`() {
val taskStepReservationEntryDto = createSampleTaskStepReservationResponse(businessPartnerWithBpnA).reservedTasks[0]

val result = cleaningServiceDummy.processCleaningTask(taskStepReservationEntryDto)

val expectedBpnA = taskStepReservationEntryDto.businessPartner.generic.address.addressBpn

val resultedAddress = result.businessPartner?.address

val resultedLegalEntity = result.businessPartner?.legalEntity

// legalEntity should be Generated with new bpnL and legalAddress bpnA
// addressPartner should use passed bpnA, and it will be different from legalAddress since type is additional address type

assertEquals(expectedBpnA, resultedAddress?.bpnAReference?.referenceValue)

assertEquals(BpnReferenceType.BpnRequestIdentifier, resultedLegalEntity?.bpnLReference?.referenceType)

val expectedAddress = expectedLogisticAddressDto.copy(bpnAReference = BpnReferenceDto(expectedBpnA.toString(), BpnReferenceType.Bpn))

val expectedLegalEntity = expectedLegalEntityDto.copy(legalAddress = expectedLogisticAddressDto, hasChanged = false)

assertRecursively(resultedAddress).isEqualTo(expectedAddress)

// ignoring bpnLReference and bpnAReference since they are generated
assertRecursively(resultedLegalEntity).ignoringFields("bpnLReference", "legalAddress.bpnAReference").isEqualTo(expectedLegalEntity)

val expectedAddress = expectedLogisticAddressDto
val expectedLegalEntity = expectedLegalEntityDto.copy(hasChanged = false, legalAddress = expectedAddress)

assertLegalEntitiesEqual(resultedLegalEntity, expectedLegalEntity)
assertAddressesEqual(resultedAddress, expectedAddress)
}

@Test
Expand All @@ -83,160 +67,73 @@ class CleaningServiceDummyTest @Autowired constructor(
val result = cleaningServiceDummy.processCleaningTask(taskStepReservationEntryDto)

val resultedAddress = result.businessPartner?.address

val resultedLegalEntity = result.businessPartner?.legalEntity

// legalEntity should be Generated with new bpnL and legalAddress bpnA
// addressPartner should be Generated bpnA, and it will be different from legalAddress since type is additional address type

assertEquals(BpnReferenceType.BpnRequestIdentifier, resultedAddress?.bpnAReference?.referenceType)

assertEquals(BpnReferenceType.BpnRequestIdentifier, resultedLegalEntity?.bpnLReference?.referenceType)

assertNotEquals(resultedAddress?.bpnAReference?.referenceValue, resultedLegalEntity?.legalAddress?.bpnAReference?.referenceValue)

val expectedAddress = expectedLogisticAddressDto.copy()

val expectedLegalEntity = expectedLegalEntityDto.copy(legalAddress = expectedLogisticAddressDto, hasChanged = false)

// bpnAReference since they are generated
assertRecursively(resultedAddress).ignoringFields("bpnAReference").isEqualTo(expectedAddress)

// ignoring bpnLReference and bpnAReference since they are generated
assertRecursively(resultedLegalEntity).ignoringFields("bpnAReference", "bpnLReference", "legalAddress.bpnAReference").isEqualTo(expectedLegalEntity)
assertLegalEntitiesEqual(resultedLegalEntity, expectedLegalEntity)
assertAddressesEqual(resultedAddress, expectedAddress)
}

@Test
fun `test processCleaningTask with BpnL and BpnA present and legal address type`() {
fun `test processCleaningTask with legal address type`() {
val taskStepReservationEntryDto = createSampleTaskStepReservationResponse(businessPartnerWithBpnLAndBpnAAndLegalAddressType).reservedTasks[0]

val result = cleaningServiceDummy.processCleaningTask(taskStepReservationEntryDto)

val expectedBpnA = taskStepReservationEntryDto.businessPartner.generic.address.addressBpn

val expectedBpnL = taskStepReservationEntryDto.businessPartner.generic.legalEntity.legalEntityBpn

val resultedAddress = result.businessPartner?.address

val resultedLegalEntity = result.businessPartner?.legalEntity

// legalEntity should use passed bpnL and legalAddress should use passed bpnA since address type is LegalAddressType
// addressPartner should use null, since it does not create when type is LegalAddressType

assertNull(resultedAddress?.bpnAReference?.referenceValue)
assertEquals(expectedBpnL, resultedLegalEntity?.bpnLReference?.referenceValue)
assertEquals(expectedBpnA, resultedLegalEntity?.legalAddress?.bpnAReference?.referenceValue)


val expectedLegalEntity = expectedLegalEntityDto.copy(
legalAddress = expectedLogisticAddressDto.copy(
hasChanged = false,
bpnAReference = BpnReferenceDto(
expectedBpnA.toString(),
BpnReferenceType.Bpn
)
), bpnLReference = BpnReferenceDto(expectedBpnL.toString(), BpnReferenceType.Bpn)
)
)

assertRecursively(resultedLegalEntity).isEqualTo(expectedLegalEntity)
assertLegalEntitiesEqual(resultedLegalEntity, expectedLegalEntity)
}

@Test
fun `test processCleaningTask with BpnS and BpnA present and legal and site main address type`() {
fun `test processCleaningTask with legal and site main address type given`() {
val taskStepReservationResponse = createSampleTaskStepReservationResponse(businessPartnerWithBpnSAndBpnAAndLegalAndSiteMainAddressType).reservedTasks[0]

val result = cleaningServiceDummy.processCleaningTask(taskStepReservationResponse)

val resultedAddress = result.businessPartner?.address

val resultedLegalEntity = result.businessPartner?.legalEntity

val resultedSite = result.businessPartner?.site

val expectedBpnA = taskStepReservationResponse.businessPartner.generic.address.addressBpn

val expectedBpnS = taskStepReservationResponse.businessPartner.generic.site.siteBpn


// legalEntity should Generate new bpnL and legalAddress should use passed bpnA since address type is LegalAndSiteMainAddress
// addressPartner should use null, since it does not create when type is LegalAndSiteMainAddress
// Site should use passed bpnS, and it will be the same MainAddress as legalAddress and addressPartner since address type is LegalAndSiteMainAddress


assertNull(resultedAddress?.bpnAReference?.referenceValue)

assertEquals(expectedBpnA, resultedLegalEntity?.legalAddress?.bpnAReference?.referenceValue)

assertEquals(expectedBpnA, resultedSite?.mainAddress?.bpnAReference?.referenceValue)

assertEquals(BpnReferenceType.BpnRequestIdentifier, resultedLegalEntity?.bpnLReference?.referenceType)

assertEquals(expectedBpnS, resultedSite?.bpnSReference?.referenceValue)


val expectedLegalEntity = expectedLegalEntityDto.copy(
hasChanged = true,
legalAddress = expectedLogisticAddressDto.copy(
hasChanged = false,
bpnAReference = BpnReferenceDto(
expectedBpnA.toString(),
BpnReferenceType.Bpn
)
hasChanged = false
)
)

val expectedSite = expectedSiteDto.copy(
hasChanged = true,
mainAddress = expectedLogisticAddressDto.copy(bpnAReference = BpnReferenceDto(expectedBpnA.toString(), BpnReferenceType.Bpn), hasChanged = false),
bpnSReference = BpnReferenceDto(expectedBpnS.toString(), BpnReferenceType.Bpn)
mainAddress = expectedLogisticAddressDto.copy(hasChanged = false)
)


// ignoring bpnLReference since they are generated
assertRecursively(resultedLegalEntity).ignoringFields("bpnLReference").isEqualTo(expectedLegalEntity)

assertRecursively(resultedSite).isEqualTo(expectedSite)


assertLegalEntitiesEqual(resultedLegalEntity, expectedLegalEntity)
assertSitesEqual(resultedSite, expectedSite)
}
@Test
fun `test processCleaningTask with empty Bpn and site main address type`() {
val taskStepReservationResponse = createSampleTaskStepReservationResponse(businessPartnerWithEmptyBpnAndSiteMainAddressType).reservedTasks[0]

val result = cleaningServiceDummy.processCleaningTask(taskStepReservationResponse)

val resultedAddress = result.businessPartner?.address

val resultedLegalEntity = result.businessPartner?.legalEntity

val resultedSite = result.businessPartner?.site


// legalEntity should Generate new bpnL and legalAddress should Generate new bpnA since address type is SiteMainAddress
// addressPartner should use null, since it does not create when type is SiteMainAddress
// Site should Generate new bpnS


assertEquals(BpnReferenceType.BpnRequestIdentifier, resultedLegalEntity?.bpnLReference?.referenceType)

assertNull(resultedAddress?.bpnAReference?.referenceType)

assertEquals(BpnReferenceType.BpnRequestIdentifier, resultedSite?.bpnSReference?.referenceType)

assertNotEquals(resultedAddress?.bpnAReference?.referenceValue, resultedLegalEntity?.legalAddress?.bpnAReference?.referenceValue)
assertNotEquals(resultedAddress?.bpnAReference?.referenceValue, resultedSite?.mainAddress?.bpnAReference?.referenceValue)

val expectedLegalEntity = expectedLegalEntityDto.copy(legalAddress = expectedLogisticAddressDto.copy(hasChanged = false), hasChanged = false)

val expectedSite = expectedSiteDto.copy(mainAddress = expectedLogisticAddressDto.copy(hasChanged = false), hasChanged = true)


// ignoring bpnLReference and legalAddress.bpnAReference since they are generated
assertRecursively(resultedLegalEntity).ignoringFields("bpnLReference", "legalAddress.bpnAReference").isEqualTo(expectedLegalEntity)

// ignoring bpnSReference and mainAddress.bpnAReference since they are generated
assertRecursively(resultedSite).ignoringFields("bpnSReference", "mainAddress.bpnAReference").isEqualTo(expectedSite)

assertLegalEntitiesEqual(resultedLegalEntity, expectedLegalEntity)
assertSitesEqual(resultedSite, expectedSite)
}

// Helper method to create a sample TaskStepReservationResponse
Expand All @@ -253,4 +150,20 @@ class CleaningServiceDummyTest @Autowired constructor(
.ignoringFieldsOfTypes(Instant::class.java)
}

private fun assertLegalEntitiesEqual(actual: LegalEntityDto?, expected: LegalEntityDto) =
assertRecursively(actual)
.ignoringFields(LegalEntityDto::bpnLReference.name)
.ignoringFields("${LegalEntityDto::legalAddress.name}.${LogisticAddressDto::bpnAReference.name}")
.isEqualTo(expected)

private fun assertSitesEqual(actual: SiteDto?, expected: SiteDto) =
assertRecursively(actual)
.ignoringFields(SiteDto::bpnSReference.name)
.ignoringFields("${SiteDto::mainAddress.name}.${LogisticAddressDto::bpnAReference.name}")
.isEqualTo(expected)

private fun assertAddressesEqual(actual: LogisticAddressDto?, expected: LogisticAddressDto) =
assertRecursively(actual)
.ignoringFields(LogisticAddressDto::bpnAReference.name)
.isEqualTo(expected)
}

0 comments on commit 413fa04

Please sign in to comment.