Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gate):removed saas test classes from gate, removed some SaaS Logic from Gate that are not necessary anymore #307

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ package org.eclipse.tractusx.bpdm.gate.service

import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum
import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.LsaType
import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateOutputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.*
import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties
import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
import org.eclipse.tractusx.bpdm.gate.entity.LogisticAddress
import org.eclipse.tractusx.bpdm.gate.exception.SaasNonexistentParentException
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.springframework.data.domain.Page
Expand All @@ -41,9 +35,6 @@ import org.springframework.stereotype.Service

@Service
class AddressService(
private val saasRequestMappingService: SaasRequestMappingService,
private val outputSaasMappingService: OutputSaasMappingService,
private val saasClient: SaasClient,
private val bpnConfigProperties: BpnConfigProperties,
private val changelogRepository: ChangelogRepository,
private val addressPersistenceService: AddressPersistenceService,
Expand Down Expand Up @@ -132,53 +123,4 @@ class AddressService(

}

/**
* Fetches parent information and converts the given [addresses] to their corresponding SaaS models
*/
fun toSaasModels(addresses: Collection<AddressGateInputRequest>): Collection<BusinessPartnerSaas> {
val parentLegalEntitiesByExternalId: Map<String, BusinessPartnerSaas> = getParentLegalEntities(addresses)
val parentSitesByExternalId: Map<String, BusinessPartnerSaas> = getParentSites(addresses)

return addresses.map { toSaasModel(it, parentLegalEntitiesByExternalId[it.legalEntityExternalId], parentSitesByExternalId[it.siteExternalId]) }
}

private fun getParentSites(addresses: Collection<AddressGateInputRequest>): Map<String, BusinessPartnerSaas> {
val parentSiteExternalIds = addresses.mapNotNull { it.siteExternalId }.distinct().toList()
var parentSitesByExternalId: Map<String, BusinessPartnerSaas> = HashMap()
if (parentSiteExternalIds.isNotEmpty()) {
val parentSitesPage = saasClient.getSites(externalIds = parentSiteExternalIds)
if (parentSitesPage.limit < parentSiteExternalIds.size) {
// should not happen as long as configured upsert limit is lower than SaaS's limit
throw IllegalStateException("Could not fetch all parent sites in single request.")
}
parentSitesByExternalId = parentSitesPage.values.associateBy { it.externalId!! }
}
return parentSitesByExternalId
}

private fun getParentLegalEntities(addresses: Collection<AddressGateInputRequest>): Map<String, BusinessPartnerSaas> {
val parentLegalEntityExternalIds = addresses.mapNotNull { it.legalEntityExternalId }.distinct().toList()
var parentLegalEntitiesByExternalId: Map<String, BusinessPartnerSaas> = HashMap()
if (parentLegalEntityExternalIds.isNotEmpty()) {
val parentLegalEntitiesPage = saasClient.getLegalEntities(externalIds = parentLegalEntityExternalIds)
if (parentLegalEntitiesPage.limit < parentLegalEntityExternalIds.size) {
// should not happen as long as configured upsert limit is lower than SaaS's limit
throw IllegalStateException("Could not fetch all parent legal entities in single request.")
}
parentLegalEntitiesByExternalId = parentLegalEntitiesPage.values.associateBy { it.externalId!! }
}
return parentLegalEntitiesByExternalId
}

private fun toSaasModel(address: AddressGateInputRequest, parentLegalEntity: BusinessPartnerSaas?, parentSite: BusinessPartnerSaas?): BusinessPartnerSaas {
if (parentLegalEntity == null && parentSite == null) {
throw SaasNonexistentParentException(address.legalEntityExternalId ?: address.siteExternalId!!)
}
val addressSaas = saasRequestMappingService.toSaasModel(address)
val parentNames = (parentLegalEntity ?: parentSite!!).names
val parentIdentifiersWithoutBpn = (parentLegalEntity ?: parentSite!!).identifiers.filter { it.type?.technicalKey != bpnConfigProperties.id }
// TODO Is this still okay? Address has its own name and identifiers with different valid types from LEs!
return addressSaas.copy(identifiers = addressSaas.identifiers.plus(parentIdentifiersWithoutBpn), names = parentNames)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package org.eclipse.tractusx.bpdm.gate.service

import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest
Expand All @@ -30,16 +29,13 @@ import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputResponse
import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties
import org.eclipse.tractusx.bpdm.gate.entity.Site
import org.eclipse.tractusx.bpdm.gate.exception.SaasNonexistentParentException
import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service

@Service
class SiteService(
private val saasRequestMappingService: SaasRequestMappingService,
private val saasClient: SaasClient,
private val bpnConfigProperties: BpnConfigProperties,
private val sitePersistenceService: SitePersistenceService,
private val siteRepository: SiteRepository
Expand Down Expand Up @@ -119,30 +115,4 @@ class SiteService(
sitePersistenceService.persistSitesOutputBP(sites, OutputInputEnum.Output)
}

/**
* Fetches parent information and converts the given [sites] to their corresponding SaaS models
*/
fun toSaasModels(sites: Collection<SiteGateInputRequest>): Collection<BusinessPartnerSaas> {
val parentLegalEntitiesByExternalId = getParentLegalEntities(sites)
return sites.map { toSaasModel(it, parentLegalEntitiesByExternalId[it.legalEntityExternalId]) }
}

private fun getParentLegalEntities(sites: Collection<SiteGateInputRequest>): Map<String, BusinessPartnerSaas> {
val parentLegalEntityExternalIds = sites.map { it.legalEntityExternalId }.distinct().toList()
val parentLegalEntitiesPage = saasClient.getLegalEntities(externalIds = parentLegalEntityExternalIds)
if (parentLegalEntitiesPage.limit < parentLegalEntityExternalIds.size) {
// should not happen as long as configured upsert limit is lower than SaaS's limit
throw IllegalStateException("Could not fetch all parent legal entities in single request.")
}
return parentLegalEntitiesPage.values.associateBy { it.externalId!! }
}

private fun toSaasModel(site: SiteGateInputRequest, parentLegalEntity: BusinessPartnerSaas?): BusinessPartnerSaas {
if (parentLegalEntity == null) {
throw SaasNonexistentParentException(site.legalEntityExternalId)
}
val siteSaas = saasRequestMappingService.toSaasModel(site)
val parentIdentifiersWithoutBpn = parentLegalEntity.identifiers.filter { it.type?.technicalKey != bpnConfigProperties.id }
return siteSaas.copy(identifiers = siteSaas.identifiers.plus(parentIdentifiersWithoutBpn))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import org.assertj.core.api.Assertions.assertThat
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.saas.*
import org.eclipse.tractusx.bpdm.gate.api.client.GateClient
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.util.*
Expand All @@ -58,8 +57,6 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.web.reactive.function.client.WebClientResponseException

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand All @@ -76,11 +73,6 @@ internal class AddressControllerInputIT @Autowired constructor(
.options(WireMockConfiguration.wireMockConfig().dynamicPort())
.build()

@JvmStatic
@DynamicPropertySource
fun properties(registry: DynamicPropertyRegistry) {
registry.add("bpdm.saas.host") { wireMockServer.baseUrl() }
}
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ internal class AddressControllerOutputIT @Autowired constructor(
val testHelpers: DbTestHelpers
) {
companion object {
@RegisterExtension
private val wireMockServerSaas: WireMockExtension = WireMockExtension.newInstance()
.options(WireMockConfiguration.wireMockConfig().dynamicPort())
.build()

@RegisterExtension
private val wireMockServerBpdmPool: WireMockExtension = WireMockExtension.newInstance()
Expand All @@ -83,7 +79,6 @@ internal class AddressControllerOutputIT @Autowired constructor(
@JvmStatic
@DynamicPropertySource
fun properties(registry: DynamicPropertyRegistry) {
registry.add("bpdm.saas.host") { wireMockServerSaas.baseUrl() }
registry.add("bpdm.pool.base-url") { wireMockServerBpdmPool.baseUrl() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.RecursiveComparisonAssert
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.saas.*
import org.eclipse.tractusx.bpdm.gate.api.client.GateClient
import org.eclipse.tractusx.bpdm.gate.api.exception.ChangeLogOutputError
import org.eclipse.tractusx.bpdm.gate.api.model.request.ChangeLogSearchRequest
import org.eclipse.tractusx.bpdm.gate.api.model.response.ChangelogResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.ErrorInfo
import org.eclipse.tractusx.bpdm.gate.config.SaasConfigProperties
import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
import org.eclipse.tractusx.bpdm.gate.util.*
import org.eclipse.tractusx.bpdm.gate.util.CommonValues.lsaTypeParam
Expand All @@ -63,8 +61,6 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import java.time.Instant

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand All @@ -73,7 +69,6 @@ import java.time.Instant
internal class ChangeLogControllerIT @Autowired constructor(
val gateClient: GateClient,
private val objectMapper: ObjectMapper,
val saasConfigProperties: SaasConfigProperties,
private val testHelpers: DbTestHelpers,
) {
companion object {
Expand All @@ -82,11 +77,6 @@ internal class ChangeLogControllerIT @Autowired constructor(
.options(WireMockConfiguration.wireMockConfig().dynamicPort())
.build()

@JvmStatic
@DynamicPropertySource
fun properties(registry: DynamicPropertyRegistry) {
registry.add("bpdm.saas.host") { wireMockServer.baseUrl() }
}
}


Expand All @@ -96,7 +86,6 @@ internal class ChangeLogControllerIT @Autowired constructor(
fun beforeEach() {
testHelpers.truncateDbTables()
wireMockServer.resetAll()
mockSaas()
createChangeLogs()
}

Expand Down Expand Up @@ -233,86 +222,6 @@ internal class ChangeLogControllerIT @Autowired constructor(
.isEqualTo(listOf(ChangelogResponse(CommonValues.externalIdAddress1, lsaTypeParam, instant)))
}

fun mockSaas() {
val addresses = listOf(
RequestValues.addressGateInputRequest1
)

val parentLegalEntitiesSaas = listOf(
SaasValues.legalEntityResponse1
)

val parentSitesSaas = listOf(
SaasValues.siteBusinessPartner1
)


// mock "get parent legal entities"
wireMockServer.stubFor(
get(urlPathMatching(EndpointValues.SAAS_MOCK_BUSINESS_PARTNER_PATH))
.withQueryParam("externalId", equalTo(addresses.mapNotNull { it.legalEntityExternalId }.joinToString(",")))
.withQueryParam("dataSource", equalTo(saasConfigProperties.datasource))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(
objectMapper.writeValueAsString(
PagedResponseSaas(
limit = 50,
total = 1,
values = parentLegalEntitiesSaas
)
)
)
)
)
// mock "get parent sites"
wireMockServer.stubFor(
get(urlPathMatching(EndpointValues.SAAS_MOCK_BUSINESS_PARTNER_PATH))
.withQueryParam("externalId", equalTo(addresses.mapNotNull { it.siteExternalId }.joinToString(",")))
.withQueryParam("dataSource", equalTo(saasConfigProperties.datasource))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(
objectMapper.writeValueAsString(
PagedResponseSaas(
limit = 50,
total = 1,
values = parentSitesSaas
)
)
)
)
)

// mock "get addresses with relations"
// this simulates the case that the address already had some relations
wireMockServer.stubFor(
get(urlPathMatching(EndpointValues.SAAS_MOCK_BUSINESS_PARTNER_PATH))
.withQueryParam("externalId", equalTo(addresses.map { it.externalId }.joinToString(",")))
.withQueryParam("dataSource", equalTo(saasConfigProperties.datasource))
.withQueryParam("featuresOn", containing("FETCH_RELATIONS"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(
objectMapper.writeValueAsString(
PagedResponseSaas(
limit = 50,
total = 2,
values = listOf(
SaasValues.addressBusinessPartnerWithRelations1,
SaasValues.addressBusinessPartnerWithRelations2
)
)
)
)
)
)
}


fun <T> assertRecursively(actual: T): RecursiveComparisonAssert<*> {
return assertThat(actual)
.usingRecursiveComparison()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import org.assertj.core.api.Assertions.assertThat
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.saas.*
import org.eclipse.tractusx.bpdm.gate.api.client.GateClient
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.eclipse.tractusx.bpdm.gate.util.*
import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.GATE_API_INPUT_LEGAL_ENTITIES_PATH
import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.SAAS_MOCK_BUSINESS_PARTNER_PATH
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -46,8 +44,6 @@ import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.web.reactive.function.client.WebClientResponseException

Expand All @@ -67,11 +63,6 @@ internal class LegalEntityControllerInputIT @Autowired constructor(
.options(WireMockConfiguration.wireMockConfig().dynamicPort())
.build()

@JvmStatic
@DynamicPropertySource
fun properties(registry: DynamicPropertyRegistry) {
registry.add("bpdm.saas.host") { wireMockServer.baseUrl() }
}
}

@BeforeEach
Expand Down Expand Up @@ -316,24 +307,6 @@ internal class LegalEntityControllerInputIT @Autowired constructor(
RequestValues.legalEntityGateInputRequest2,
)

wireMockServer.stubFor(
put(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(
objectMapper.writeValueAsString(
UpsertResponse(
emptyList(),
emptyList(),
2,
0
)
)
)
)
)

try {
gateClient.legalEntities().upsertLegalEntities(legalEntities)
} catch (e: WebClientResponseException) {
Expand Down
Loading