Skip to content

Commit

Permalink
refactor(pool): eclipse-tractusx#658 move function to create and comp…
Browse files Browse the repository at this point in the history
…are business partner in own files
  • Loading branch information
rainer-exxcellent committed Dec 12, 2023
1 parent a235fe5 commit 1a3710f
Show file tree
Hide file tree
Showing 3 changed files with 398 additions and 356 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*******************************************************************************
* 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.assertj.core.api.Assertions
import org.eclipse.tractusx.bpdm.common.dto.ILegalEntityStateDto
import org.eclipse.tractusx.bpdm.common.dto.ISiteStateDto
import org.eclipse.tractusx.bpdm.pool.api.model.*
import org.eclipse.tractusx.bpdm.pool.api.model.response.LegalEntityWithLegalAddressVerboseDto
import org.eclipse.tractusx.bpdm.pool.api.model.response.SiteWithMainAddressVerboseDto
import org.eclipse.tractusx.orchestrator.api.model.AddressIdentifierDto
import org.eclipse.tractusx.orchestrator.api.model.AddressStateDto
import org.eclipse.tractusx.orchestrator.api.model.LegalEntityClassificationDto
import org.eclipse.tractusx.orchestrator.api.model.LegalEntityDto
import org.eclipse.tractusx.orchestrator.api.model.LegalEntityIdentifierDto
import org.eclipse.tractusx.orchestrator.api.model.LogisticAddressDto
import org.eclipse.tractusx.orchestrator.api.model.SiteDto
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import java.util.function.BiPredicate

fun compareLegalEntity(verboseRequest: LegalEntityWithLegalAddressVerboseDto, legalEntity: LegalEntityDto?) {

val verboseLegalEntity = verboseRequest.legalEntity

Assertions.assertThat(verboseLegalEntity.legalShortName).isEqualTo(legalEntity?.legalShortName)
Assertions.assertThat(verboseLegalEntity.legalFormVerbose?.technicalKey).isEqualTo(legalEntity?.legalForm)
compareStates(verboseLegalEntity.states, legalEntity?.states)
compareClassifications(verboseLegalEntity.classifications, legalEntity?.classifications)
compareIdentifiers(verboseLegalEntity.identifiers, legalEntity?.identifiers)

val verboseLegalAddress = verboseRequest.legalAddress
Assertions.assertThat(verboseLegalAddress.bpnLegalEntity).isEqualTo(legalEntity?.bpnLReference?.referenceValue)
Assertions.assertThat(verboseLegalAddress.isLegalAddress).isTrue()
compareLogisticAddress(verboseLegalAddress, legalEntity?.legalAddress)
}

fun compareSite(verboseRequest: SiteWithMainAddressVerboseDto, site: SiteDto?) {

val verboseSite = verboseRequest.site

Assertions.assertThat(verboseSite.name).isEqualTo(site?.name)
Assertions.assertThat(verboseSite.bpns).isEqualTo(site?.bpnSReference?.referenceValue)
compareSiteStates(verboseSite.states, site?.states)

val verboseMainAddress = verboseRequest.mainAddress
Assertions.assertThat(verboseMainAddress.bpnSite).isEqualTo(site?.bpnSReference?.referenceValue)
val mainAddress = site?.mainAddress
Assertions.assertThat(verboseMainAddress.isMainAddress).isTrue()
compareLogisticAddress(verboseMainAddress, mainAddress)
}

fun compareLogisticAddress(verboseAddress: LogisticAddressVerboseDto, address: LogisticAddressDto?) {

Assertions.assertThat(verboseAddress.name).isEqualTo(address?.name)
compareAddressStates(verboseAddress.states, address?.states)
compareAddressIdentifiers(verboseAddress.identifiers, address?.identifiers)


val verbosePhysicalAddress = verboseAddress.physicalPostalAddress
val physicalAddress = address?.physicalPostalAddress
Assertions.assertThat(verbosePhysicalAddress).usingRecursiveComparison()
.ignoringFields(PhysicalPostalAddressVerboseDto::countryVerbose.name, PhysicalPostalAddressVerboseDto::administrativeAreaLevel1Verbose.name)
.isEqualTo(physicalAddress)
Assertions.assertThat(verbosePhysicalAddress.country.name).isEqualTo(physicalAddress?.country?.name)
Assertions.assertThat(verbosePhysicalAddress.administrativeAreaLevel1).isEqualTo(physicalAddress?.administrativeAreaLevel1)
val verboseAlternAddress = verboseAddress.alternativePostalAddress
val alternAddress = address?.alternativePostalAddress
Assertions.assertThat(verboseAlternAddress).usingRecursiveComparison()
.ignoringFields(AlternativePostalAddressVerboseDto::countryVerbose.name, AlternativePostalAddressVerboseDto::administrativeAreaLevel1Verbose.name)
.isEqualTo(alternAddress)
Assertions.assertThat(verboseAlternAddress?.country?.name).isEqualTo(alternAddress?.country?.name)
Assertions.assertThat(verboseAlternAddress?.administrativeAreaLevel1).isEqualTo(alternAddress?.administrativeAreaLevel1)
}

fun compareAddressStates(statesVerbose: Collection<AddressStateVerboseDto>, states: Collection<AddressStateDto>?) {

Assertions.assertThat(statesVerbose.size).isEqualTo(states?.size ?: 0)
val sortedVerboseStates = statesVerbose.sortedBy { it.description }
val sortedStates = states?.sortedBy { it.description }
sortedVerboseStates.indices.forEach {
Assertions.assertThat(sortedVerboseStates[it].typeVerbose.technicalKey.name).isEqualTo(sortedStates!![it].type.name)
Assertions.assertThat(sortedVerboseStates[it]).usingRecursiveComparison()
.withEqualsForFields(isEqualToIgnoringMilliseconds(), AddressStateVerboseDto::validTo.name)
.withEqualsForFields(isEqualToIgnoringMilliseconds(), AddressStateVerboseDto::validFrom.name)
.ignoringFields(AddressStateVerboseDto::typeVerbose.name).isEqualTo(sortedStates[it])
}
}

fun compareAddressIdentifiers(identifiersVerbose: Collection<AddressIdentifierVerboseDto>, identifiers: Collection<AddressIdentifierDto>?) {

Assertions.assertThat(identifiersVerbose.size).isEqualTo(identifiers?.size ?: 0)
val sortedVerboseIdentifiers = identifiersVerbose.sortedBy { it.typeVerbose.name }
val sortedIdentifiers = identifiers!!.sortedBy { it.type }
sortedVerboseIdentifiers.indices.forEach {
Assertions.assertThat(sortedVerboseIdentifiers[it].typeVerbose.technicalKey).isEqualTo(sortedIdentifiers[it].type)
Assertions.assertThat(sortedVerboseIdentifiers[it]).usingRecursiveComparison()
.ignoringFields(AddressIdentifierVerboseDto::typeVerbose.name)
.isEqualTo(sortedIdentifiers[it])
}
}

fun compareStates(statesVerbose: Collection<LegalEntityStateVerboseDto>, states: Collection<ILegalEntityStateDto>?) {

Assertions.assertThat(statesVerbose.size).isEqualTo(states?.size ?: 0)
val sortedVerboseStates = statesVerbose.sortedBy { it.description }
val sortedStates = states!!.sortedBy { it.description }
sortedVerboseStates.indices.forEach {
Assertions.assertThat(sortedVerboseStates[it].typeVerbose.technicalKey.name).isEqualTo(sortedStates[it].type.name)
Assertions.assertThat(sortedVerboseStates[it]).usingRecursiveComparison()
.withEqualsForFields(isEqualToIgnoringMilliseconds(), LegalEntityStateVerboseDto::validTo.name )
.withEqualsForFields(isEqualToIgnoringMilliseconds(), LegalEntityStateVerboseDto::validFrom.name)
.ignoringFields(LegalEntityStateVerboseDto::typeVerbose.name)
.isEqualTo(sortedStates[it])
}
}

fun compareSiteStates(statesVerbose: Collection<SiteStateVerboseDto>, states: Collection<ISiteStateDto>?) {

Assertions.assertThat(statesVerbose.size).isEqualTo(states?.size ?: 0)
val sortedVerboseStates = statesVerbose.sortedBy { it.description }
val sortedStates = states!!.sortedBy { it.description }
sortedVerboseStates.indices.forEach {
Assertions.assertThat(sortedVerboseStates[it].typeVerbose.technicalKey.name).isEqualTo(sortedStates[it].type.name)
Assertions.assertThat(sortedVerboseStates[it]).usingRecursiveComparison()
.withEqualsForFields(isEqualToIgnoringMilliseconds(), SiteStateVerboseDto::validTo.name)
.withEqualsForFields(isEqualToIgnoringMilliseconds(), SiteStateVerboseDto::validFrom.name)
.ignoringFields(SiteStateVerboseDto::typeVerbose.name)
.isEqualTo(sortedStates[it])
}
}

fun isEqualToIgnoringMilliseconds(): BiPredicate<LocalDateTime?, LocalDateTime?> {
return BiPredicate<LocalDateTime?, LocalDateTime?> { d1, d2 ->
(d1 == null && d2 == null)
|| d1.truncatedTo(ChronoUnit.SECONDS).equals(d2.truncatedTo(ChronoUnit.SECONDS))
}
}

fun compareClassifications(
classificationsVerbose: Collection<LegalEntityClassificationVerboseDto>,
classifications: Collection<LegalEntityClassificationDto>?
) {

Assertions.assertThat(classificationsVerbose.size).isEqualTo(classifications?.size ?: 0)
val sortedVerboseClassifications = classificationsVerbose.sortedBy { it.typeVerbose.name }
val sortedClassifications = classifications!!.sortedBy { it.type.name }
sortedVerboseClassifications.indices.forEach {
Assertions.assertThat(sortedVerboseClassifications[it].typeVerbose.technicalKey.name).isEqualTo(sortedClassifications[it].type.name)
Assertions.assertThat(sortedVerboseClassifications[it]).usingRecursiveComparison()
.ignoringFields(LegalEntityClassificationVerboseDto::typeVerbose.name)
.isEqualTo(sortedClassifications[it])
}
}

fun compareIdentifiers(identifiersVerbose: Collection<LegalEntityIdentifierVerboseDto>, identifiers: Collection<LegalEntityIdentifierDto>?) {

Assertions.assertThat(identifiersVerbose.size).isEqualTo(identifiers?.size ?: 0)
val sortedVerboseIdentifiers = identifiersVerbose.sortedBy { it.typeVerbose.name }
val sortedIdentifiers = identifiers!!.sortedBy { it.type }
sortedVerboseIdentifiers.indices.forEach {
Assertions.assertThat(sortedVerboseIdentifiers[it].typeVerbose.technicalKey).isEqualTo(sortedIdentifiers[it].type)
Assertions.assertThat(sortedVerboseIdentifiers[it]).usingRecursiveComparison()
.ignoringFields(LegalEntityIdentifierVerboseDto::typeVerbose.name).isEqualTo(sortedIdentifiers[it])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*******************************************************************************
* 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 com.neovisionaries.i18n.CountryCode
import org.eclipse.tractusx.bpdm.common.dto.GeoCoordinateDto
import org.eclipse.tractusx.bpdm.common.dto.TypeKeyNameVerboseDto
import org.eclipse.tractusx.bpdm.common.model.BusinessStateType
import org.eclipse.tractusx.bpdm.common.model.ClassificationType
import org.eclipse.tractusx.bpdm.common.model.DeliveryServiceType
import org.eclipse.tractusx.bpdm.pool.util.BusinessPartnerNonVerboseValues
import org.eclipse.tractusx.bpdm.pool.util.BusinessPartnerVerboseValues
import org.eclipse.tractusx.orchestrator.api.model.*
import java.time.LocalDateTime

fun minFullBusinessPartner(): BusinessPartnerFullDto {

return BusinessPartnerFullDto(generic = BusinessPartnerGenericDto())
}

fun emptyLegalEntity(): LegalEntityDto {

return LegalEntityDto()
}

fun minValidLegalEntity(bpnLReference: BpnReferenceDto, bpnAReference: BpnReferenceDto): LegalEntityDto {

return LegalEntityDto(
bpnLReference = bpnLReference,
legalName = "legalName_" + bpnLReference.referenceValue,
legalAddress = minLogisticAddress(bpnAReference = bpnAReference)
)
}

fun fullValidLegalEntity(bpnLReference: BpnReferenceDto, bpnAReference: BpnReferenceDto): LegalEntityDto {

return LegalEntityDto(
bpnLReference = bpnLReference,
legalName = "legalName_" + bpnLReference.referenceValue,
legalShortName = "shortName_" + bpnLReference.referenceValue,
legalForm = BusinessPartnerVerboseValues.legalForm1.technicalKey,
identifiers = listOf(
legalEntityIdentifierDto(bpnLReference.referenceValue, 1L, BusinessPartnerVerboseValues.identifierType1),
legalEntityIdentifierDto(bpnLReference.referenceValue, 2L, BusinessPartnerVerboseValues.identifierType2)
),
states = listOf(
legalEntityState(bpnLReference.referenceValue, 1L, BusinessStateType.ACTIVE),
legalEntityState(bpnLReference.referenceValue, 2L, BusinessStateType.INACTIVE)
),
classifications = listOf(
classificationDto(bpnLReference.referenceValue, 1L, ClassificationType.NACE),
classificationDto(bpnLReference.referenceValue, 2L, ClassificationType.NAICS)
),
legalAddress = fullLogisticAddressDto(bpnAReference)
)
}

fun legalEntityIdentifierDto(name: String, id: Long, type: TypeKeyNameVerboseDto<String>): LegalEntityIdentifierDto {

return LegalEntityIdentifierDto(
value = "value_" + name + "_" + id,
issuingBody = "issuingBody_" + name + "_" + id,
type = type.technicalKey
)
}

fun addressIdentifierDto(name: String, id: Long, type: TypeKeyNameVerboseDto<String>): AddressIdentifierDto {

return AddressIdentifierDto(
value = "value_" + name + "_" + id,
type = type.technicalKey
)
}

fun legalEntityState(name: String, id: Long, type: BusinessStateType): LegalEntityStateDto {

return LegalEntityStateDto(
description = "description_" + name + "_" + id,
validFrom = LocalDateTime.now().plusDays(id),
validTo = LocalDateTime.now().plusDays(id + 2),
type = type
)
}

fun siteState(name: String, id: Long, type: BusinessStateType): SiteStateDto {

return SiteStateDto(
description = "description_" + name + "_" + id,
validFrom = LocalDateTime.now().plusDays(id),
validTo = LocalDateTime.now().plusDays(id + 2),
type = type
)
}

fun addressState(name: String, id: Long, type: BusinessStateType): AddressStateDto {

return AddressStateDto(
description = "description_" + name + "_" + id,
validFrom = LocalDateTime.now().plusDays(id),
validTo = LocalDateTime.now().plusDays(id + 2),
type = type
)
}


fun classificationDto(name: String, id: Long, type: ClassificationType): LegalEntityClassificationDto {

return LegalEntityClassificationDto(
code = "code_" + name + "_" + id,
value = "value_" + name + "_" + id,
type = type
)
}

fun minLogisticAddress(bpnAReference: BpnReferenceDto): LogisticAddressDto {

return LogisticAddressDto(
bpnAReference = bpnAReference,
physicalPostalAddress = minPhysicalPostalAddressDto(bpnAReference)
)
}

fun minPhysicalPostalAddressDto(bpnAReference: BpnReferenceDto) = PhysicalPostalAddressDto(
country = CountryCode.DE,
city = "City_" + bpnAReference.referenceValue
)

fun fullLogisticAddressDto(bpnAReference: BpnReferenceDto): LogisticAddressDto {

return LogisticAddressDto(
bpnAReference = bpnAReference,
name = "name_" + bpnAReference.referenceValue,
identifiers = listOf(
addressIdentifierDto(bpnAReference.referenceValue, 1L, TypeKeyNameVerboseDto(BusinessPartnerNonVerboseValues.addressIdentifierTypeDto1.technicalKey, "")),
addressIdentifierDto(bpnAReference.referenceValue, 2L, TypeKeyNameVerboseDto(BusinessPartnerNonVerboseValues.addressIdentifierTypeDto2.technicalKey, ""))
),
states = listOf(
addressState(bpnAReference.referenceValue, 1L, BusinessStateType.ACTIVE),
addressState(bpnAReference.referenceValue, 2L, BusinessStateType.INACTIVE)
),
physicalPostalAddress = PhysicalPostalAddressDto(
geographicCoordinates = GeoCoordinateDto(longitude = 1.1f, latitude = 2.2f, altitude = 3.3f),
country = CountryCode.DE,
administrativeAreaLevel1 = "AD-07",
administrativeAreaLevel2 = "adminArea2_" + bpnAReference.referenceValue,
administrativeAreaLevel3 = "adminArea3_" + bpnAReference.referenceValue,
postalCode = "postalCode_" + bpnAReference.referenceValue,
city = "city_" + bpnAReference.referenceValue,
street = StreetDto(
name = "name_" + bpnAReference.referenceValue,
houseNumber = "houseNumber_" + bpnAReference.referenceValue,
milestone = "milestone_" + bpnAReference.referenceValue,
direction = "direction_" + bpnAReference.referenceValue,
namePrefix = "namePrefix_" + bpnAReference.referenceValue,
additionalNamePrefix = "additionalNamePrefix_" + bpnAReference.referenceValue,
nameSuffix = "nameSuffix_" + bpnAReference.referenceValue,
additionalNameSuffix = "additionalNameSuffix_" + bpnAReference.referenceValue,
),
district = "district_" + bpnAReference.referenceValue,
companyPostalCode = "companyPostalCode_" + bpnAReference.referenceValue,
industrialZone = "industrialZone_" + bpnAReference.referenceValue,
building = "building_" + bpnAReference.referenceValue,
floor = "floor_" + bpnAReference.referenceValue,
door = "door_" + bpnAReference.referenceValue,
),
alternativePostalAddress = AlternativePostalAddressDto(
geographicCoordinates = GeoCoordinateDto(longitude = 12.3f, latitude = 4.56f, altitude = 7.89f),
country = CountryCode.DE,
administrativeAreaLevel1 = "DE-BW",
postalCode = "alternate_postalCode_" + bpnAReference.referenceValue,
city = "alternate_city_" + bpnAReference.referenceValue,
deliveryServiceType = DeliveryServiceType.PO_BOX,
deliveryServiceQualifier = "deliveryServiceQualifier_" + bpnAReference.referenceValue,
deliveryServiceNumber = "deliveryServiceNumber_" + bpnAReference.referenceValue,
)
)
}

fun minValidSite(bpnSReference: BpnReferenceDto, bpnAReference: BpnReferenceDto): SiteDto {

return SiteDto(
bpnSReference = bpnSReference,
name = "siteName_" + bpnSReference.referenceValue,
mainAddress = minLogisticAddress(bpnAReference = bpnAReference)
)
}

fun fullValidSite(bpnSReference: BpnReferenceDto, bpnAReference: BpnReferenceDto): SiteDto {

return SiteDto(
bpnSReference = bpnSReference,
name = "siteName_" + bpnSReference.referenceValue,
states = listOf(
siteState(bpnSReference.referenceValue, 1L, BusinessStateType.ACTIVE), siteState(bpnSReference.referenceValue, 2L, BusinessStateType.INACTIVE)
),
mainAddress = fullLogisticAddressDto(bpnAReference)
)
}
Loading

0 comments on commit 1a3710f

Please sign in to comment.