From 5a92471f7ec0193ce935e9bb3e1b91812230062b Mon Sep 17 00:00:00 2001 From: alexsilva Date: Wed, 5 Apr 2023 16:56:40 +0100 Subject: [PATCH 01/12] feat(gate): Added Logic for site entity, added flyway migration file for entities --- .../bpdm/gate/service/ResponseMappings.kt | 4 ++ .../tractusx/bpdm/gate/service/SiteService.kt | 2 + .../V0_0_2_0__create_site_tables.sql | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt index 1375d2edc..86847c317 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt @@ -20,6 +20,7 @@ package org.eclipse.tractusx.bpdm.gate.service import org.eclipse.tractusx.bpdm.common.dto.* +import org.eclipse.tractusx.bpdm.common.dto.SiteStateDto import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputResponse @@ -31,6 +32,9 @@ import org.eclipse.tractusx.bpdm.gate.entity.* import org.springframework.data.domain.Page import java.time.Instant import java.time.temporal.ChronoUnit +import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry +import org.eclipse.tractusx.bpdm.gate.entity.Site +import org.eclipse.tractusx.bpdm.gate.entity.SiteState fun AddressGateInputRequest.toAddressGate(legalEntity: LegalEntity?, site: Site?): LogisticAddress { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt index d9969a7e9..b6164053e 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt @@ -140,6 +140,8 @@ class SiteService( val sitesSaas = toSaasModels(sites) saasClient.upsertSites(sitesSaas) + sitePersistenceService.persistSitesBP(sites) + // create changelog entry if all goes well from saasClient sites.forEach { site -> changelogRepository.save(ChangelogEntry(site.externalId, LsaType.Site)) diff --git a/bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql b/bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql new file mode 100644 index 000000000..d95e9e893 --- /dev/null +++ b/bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql @@ -0,0 +1,37 @@ +create table site_states ( + id bigint not null, + created_at timestamp with time zone not null, + updated_at timestamp with time zone not null, + uuid uuid not null, + description varchar(255), + type varchar(255) not null, + valid_from timestamp, + valid_to timestamp, + site_id bigint not null, + primary key (id) +); + +create table sites ( + id bigint not null, + created_at timestamp with time zone not null, + updated_at timestamp with time zone not null, + uuid uuid not null, + bpn varchar(255) not null, + external_id varchar(255) not null, + legal_entity_external_id varchar(255), + name varchar(255) not null, + primary key (id) +); + +create index IDX4wafjs5sojc79luhn5l29bb79 on site_states (site_id); + +alter table if exists site_states add constraint UK_qr49l0kw056r00i88edmvjwqa unique (uuid); + +alter table if exists sites add constraint UK_b2t72lxjqja93ids61sgvo4hg unique (uuid); + +alter table if exists sites add constraint UK_27n4pihn8c6rh8v202wevppyu unique (bpn); + +alter table if exists sites add constraint UK_1vrdeiex4x7p93r5svtvb5b4x unique (external_id); + +alter table if exists site_states add constraint FK7t400j8drx0gxk0davixv7n54 foreign key (site_id) references sites; + From 5cff88a0c93e30b7cf02286328763f440db431ec Mon Sep 17 00:00:00 2001 From: alexsilva Date: Thu, 4 May 2023 12:10:59 +0100 Subject: [PATCH 02/12] feat(gate): junction of site persistence to LogisticAddress and Legal Entities --- .../bpdm/gate/controller/SiteController.kt | 7 ++-- .../V0_0_2_0__create_site_tables.sql | 37 ------------------- 2 files changed, 3 insertions(+), 41 deletions(-) delete mode 100644 bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt index 4a05591db..9e299c540 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt @@ -28,7 +28,6 @@ import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties -import org.eclipse.tractusx.bpdm.gate.containsDuplicates import org.eclipse.tractusx.bpdm.gate.service.SiteService import org.eclipse.tractusx.bpdm.gate.service.ValidationService import org.springframework.http.HttpStatus @@ -43,9 +42,9 @@ class SiteController( ) : GateSiteApi { override fun upsertSites(sites: Collection): ResponseEntity { - if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { - return ResponseEntity(HttpStatus.BAD_REQUEST) - } +// if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { +// return ResponseEntity(HttpStatus.BAD_REQUEST) +// } siteService.upsertSites(sites) return ResponseEntity(HttpStatus.OK) } diff --git a/bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql b/bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql deleted file mode 100644 index d95e9e893..000000000 --- a/bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_site_tables.sql +++ /dev/null @@ -1,37 +0,0 @@ -create table site_states ( - id bigint not null, - created_at timestamp with time zone not null, - updated_at timestamp with time zone not null, - uuid uuid not null, - description varchar(255), - type varchar(255) not null, - valid_from timestamp, - valid_to timestamp, - site_id bigint not null, - primary key (id) -); - -create table sites ( - id bigint not null, - created_at timestamp with time zone not null, - updated_at timestamp with time zone not null, - uuid uuid not null, - bpn varchar(255) not null, - external_id varchar(255) not null, - legal_entity_external_id varchar(255), - name varchar(255) not null, - primary key (id) -); - -create index IDX4wafjs5sojc79luhn5l29bb79 on site_states (site_id); - -alter table if exists site_states add constraint UK_qr49l0kw056r00i88edmvjwqa unique (uuid); - -alter table if exists sites add constraint UK_b2t72lxjqja93ids61sgvo4hg unique (uuid); - -alter table if exists sites add constraint UK_27n4pihn8c6rh8v202wevppyu unique (bpn); - -alter table if exists sites add constraint UK_1vrdeiex4x7p93r5svtvb5b4x unique (external_id); - -alter table if exists site_states add constraint FK7t400j8drx0gxk0davixv7n54 foreign key (site_id) references sites; - From 06f913e68dc015aafb39f06ff7596d0a86504e29 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Mon, 8 May 2023 17:37:21 +0100 Subject: [PATCH 03/12] fix(gate): added new changes to upsert unit tests to check for persisted data, fixed logic on update of some entities --- .../tractusx/bpdm/gate/controller/SiteController.kt | 7 ++++--- .../tractusx/bpdm/gate/service/SitePersistenceService.kt | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt index 9e299c540..4a05591db 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt @@ -28,6 +28,7 @@ import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties +import org.eclipse.tractusx.bpdm.gate.containsDuplicates import org.eclipse.tractusx.bpdm.gate.service.SiteService import org.eclipse.tractusx.bpdm.gate.service.ValidationService import org.springframework.http.HttpStatus @@ -42,9 +43,9 @@ class SiteController( ) : GateSiteApi { override fun upsertSites(sites: Collection): ResponseEntity { -// if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { -// return ResponseEntity(HttpStatus.BAD_REQUEST) -// } + if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { + return ResponseEntity(HttpStatus.BAD_REQUEST) + } siteService.upsertSites(sites) return ResponseEntity(HttpStatus.OK) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt index 48ab746a6..a4cdf83a2 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt @@ -70,6 +70,7 @@ class SitePersistenceService( siteRepository.save(fullSite) } } + //} } private fun updateSite(site: Site, updatedSite: SiteGateInputRequest, legalEntityRecord: LegalEntity) { From 3a959a473ebb60e17f5628ed90e537f3e74dd074 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Tue, 23 May 2023 15:22:58 +0100 Subject: [PATCH 04/12] fix(gate): Bridge Dummy Pagination changes --- .../eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt index a4cdf83a2..48ab746a6 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt @@ -70,7 +70,6 @@ class SitePersistenceService( siteRepository.save(fullSite) } } - //} } private fun updateSite(site: Site, updatedSite: SiteGateInputRequest, legalEntityRecord: LegalEntity) { From 2766958f1fed3c66e356bc8f4be91b7832b0db9b Mon Sep 17 00:00:00 2001 From: alexsilva Date: Wed, 24 May 2023 17:31:48 +0100 Subject: [PATCH 05/12] fix(gate): Fix removal of administrativeArea and New invalid entries field added --- .../gate/api/model/response/PageLogisticAddressResponse.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt index 0f0f889fd..beaba9aa2 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt @@ -33,5 +33,8 @@ data class PageLogisticAddressResponse( val contentSize: Int, @Schema(description = "Collection of results in the page") val content: Collection, + @Schema(description = "Number of entries in the page that have been omitted due to being invalid") + val invalidEntries: Int + ) From 5812e84d9602f8706cc54edb632b1e25e86ebb20 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Thu, 25 May 2023 16:43:11 +0100 Subject: [PATCH 06/12] fix(gate): Unit tests changes according to new address service fetch logic --- .../kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt index f9ebbb864..afcbc39d6 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt @@ -335,6 +335,7 @@ object ResponseValues { bpn = CommonValues.bpnAddress1, processStartedAt = SaasValues.modificationTime1, ) + val addressGateInputResponse2 = AddressGateInputResponse( address = RequestValues.address2 .copy( From 49aa2ba90abdb49a78d6e7ab6a10ef73358c88e6 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Thu, 4 May 2023 12:10:59 +0100 Subject: [PATCH 07/12] feat(gate): junction of site persistence to LogisticAddress and Legal Entities --- .../tractusx/bpdm/gate/controller/SiteController.kt | 7 +++---- .../eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt | 1 - 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt index 4a05591db..9e299c540 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt @@ -28,7 +28,6 @@ import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties -import org.eclipse.tractusx.bpdm.gate.containsDuplicates import org.eclipse.tractusx.bpdm.gate.service.SiteService import org.eclipse.tractusx.bpdm.gate.service.ValidationService import org.springframework.http.HttpStatus @@ -43,9 +42,9 @@ class SiteController( ) : GateSiteApi { override fun upsertSites(sites: Collection): ResponseEntity { - if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { - return ResponseEntity(HttpStatus.BAD_REQUEST) - } +// if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { +// return ResponseEntity(HttpStatus.BAD_REQUEST) +// } siteService.upsertSites(sites) return ResponseEntity(HttpStatus.OK) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt index 86847c317..ac322350e 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt @@ -164,7 +164,6 @@ fun LegalEntityGateInputRequest.toLegalEntity(): LegalEntity { legalForm = legalEntity.legalForm, legalName = legalEntity.legalName.toName() ) - legalEntity.identifiers.addAll( this.legalEntity.identifiers.map {toEntityIdentifier(it,legalEntity)}) legalEntity.states.addAll(this.legalEntity.states.map { toEntityState(it,legalEntity) }) legalEntity.classifications.addAll(this.legalEntity.classifications.map { toEntityClassification(it,legalEntity) }) From 65db03159565231075b921f9035c81459fec88fa Mon Sep 17 00:00:00 2001 From: cezaralexandremorais Date: Thu, 18 May 2023 14:51:14 +0100 Subject: [PATCH 08/12] feat(gate): Replaced SaaS provider calls in some legalEntity services to fetch from database instead, and changed pagination to use JPA Pagination --- .../model/response/PageLegalEntityResponse.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt new file mode 100644 index 000000000..5cb502f62 --- /dev/null +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt @@ -0,0 +1,36 @@ +/******************************************************************************* + * 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.gate.api.model.response + +import io.swagger.v3.oas.annotations.media.Schema + +@Schema(description = "Paginated collection of Legal Entities") +data class PageLegalEntityResponse( + @Schema(description = "Total number of all results in all pages") + val totalElements: Long, + @Schema(description = "Total number pages") + val totalPages: Int, + @Schema(description = "Current page number") + val page: Int, + @Schema(description = "Number of results in the page") + val contentSize: Int, + @Schema(description = "Collection of results in the page") + val content: Collection, +) \ No newline at end of file From 7adc202a347081ee3e3bbed2d77b7520c4221a35 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Wed, 24 May 2023 11:23:14 +0100 Subject: [PATCH 09/12] feat(gate): Altered service logic for sites with new pagination and fetch from persistence db --- .../tractusx/bpdm/gate/api/GateSiteApi.kt | 9 +-- .../api/model/response/PageSiteResponse.kt | 38 ++++++++++ .../bpdm/gate/controller/SiteController.kt | 13 ++-- .../bpdm/gate/repository/SiteRepository.kt | 4 ++ .../bpdm/gate/service/ResponseMappings.kt | 15 +++- .../tractusx/bpdm/gate/service/SiteService.kt | 71 ++++++++++++++----- .../gate/controller/SiteControllerInputIT.kt | 12 ++-- 7 files changed, 129 insertions(+), 33 deletions(-) create mode 100644 bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt index aeebdd32c..62e3e2748 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt @@ -25,12 +25,13 @@ import io.swagger.v3.oas.annotations.media.Content import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.responses.ApiResponses import jakarta.validation.Valid +import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse +import org.eclipse.tractusx.bpdm.gate.api.model.response.PageSiteResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.springdoc.core.annotations.ParameterObject import org.springframework.http.MediaType @@ -89,9 +90,9 @@ interface GateSiteApi { @PostMapping("/input/sites/search") @PostExchange("/input/sites/search") fun getSitesByExternalIds( - @ParameterObject @Valid paginationRequest: PaginationStartAfterRequest, + @ParameterObject @Valid paginationRequest: PaginationRequest, @RequestBody externalIds: Collection - ): PageStartAfterResponse + ): PageSiteResponse @Operation( summary = "Get page of sites", @@ -105,7 +106,7 @@ interface GateSiteApi { ) @GetMapping("/input/sites") @GetExchange("/input/sites") - fun getSites(@ParameterObject @Valid paginationRequest: PaginationStartAfterRequest): PageStartAfterResponse + fun getSites(@ParameterObject @Valid paginationRequest: PaginationRequest): PageSiteResponse @Operation( summary = "Get page of sites", diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt new file mode 100644 index 000000000..169740f64 --- /dev/null +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt @@ -0,0 +1,38 @@ +/******************************************************************************* + * 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.gate.api.model.response + +import io.swagger.v3.oas.annotations.media.Schema + +@Schema(description = "Paginated collection of Logistic Addresses") +data class PageSiteResponse( + @Schema(description = "Total number of all results in all pages") + val totalElements: Long, + @Schema(description = "Total number pages") + val totalPages: Int, + @Schema(description = "Current page number") + val page: Int, + @Schema(description = "Number of results in the page") + val contentSize: Int, + @Schema(description = "Collection of results in the page") + val content: Collection, + @Schema(description = "Number of entries in the page that have been omitted due to being invalid") + val invalidEntries: Int +) \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt index 9e299c540..168c561dd 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt @@ -19,13 +19,14 @@ package org.eclipse.tractusx.bpdm.gate.controller +import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest import org.eclipse.tractusx.bpdm.gate.api.GateSiteApi import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse +import org.eclipse.tractusx.bpdm.gate.api.model.response.PageSiteResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties import org.eclipse.tractusx.bpdm.gate.service.SiteService @@ -54,14 +55,14 @@ class SiteController( } override fun getSitesByExternalIds( - paginationRequest: PaginationStartAfterRequest, + paginationRequest: PaginationRequest, externalIds: Collection - ): PageStartAfterResponse { - return siteService.getSites(paginationRequest.limit, paginationRequest.startAfter, externalIds) + ): PageSiteResponse { + return siteService.getSites(page = paginationRequest.page, size = paginationRequest.size, externalIds = externalIds) } - override fun getSites(paginationRequest: PaginationStartAfterRequest): PageStartAfterResponse { - return siteService.getSites(paginationRequest.limit, paginationRequest.startAfter) + override fun getSites(paginationRequest: PaginationRequest): PageSiteResponse { + return siteService.getSites(page = paginationRequest.page, size = paginationRequest.size) } override fun getSitesOutput(paginationRequest: PaginationStartAfterRequest, externalIds: Collection?): PageOutputResponse { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/SiteRepository.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/SiteRepository.kt index 2c4427c32..ffd7b34ba 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/SiteRepository.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/SiteRepository.kt @@ -20,6 +20,8 @@ package org.eclipse.tractusx.bpdm.gate.repository import org.eclipse.tractusx.bpdm.gate.entity.Site +import org.springframework.data.domain.Page +import org.springframework.data.domain.Pageable import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.repository.CrudRepository @@ -29,4 +31,6 @@ interface SiteRepository : JpaRepository, CrudRepository fun findByExternalId(externalId: String): Site? + fun findByExternalIdIn(externalId: Collection?, pageable: Pageable): Page + } \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt index ac322350e..ba90638ca 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt @@ -157,7 +157,7 @@ fun LegalEntityGateInputRequest.toLegalEntity(): LegalEntity { legalEntityExternalId = externalId ) - val legalEntity = LegalEntity( + val legalEntity= LegalEntity( bpn = bpn, externalId = externalId, currentness = createCurrentnessTimestamp(), @@ -338,4 +338,17 @@ fun LegalEntity.LegalEntityGateInputResponse(legalEntity: LegalEntity): LegalEnt bpn = legalEntity.bpn, processStartedAt = null ) +} + +fun Site.toSiteDto(): SiteDto { + + return SiteDto( + name = name, + states = mapToDtoSitesStates(states), + mainAddress = mainAddress.toLogisticAddressDto() + ) +} + +fun mapToDtoSitesStates(states: MutableSet): Collection { + return states.map { SiteStateDto(it.description, it.validFrom, it.validTo, it.type) } } \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt index b6164053e..23d47f1af 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt @@ -20,22 +20,26 @@ package org.eclipse.tractusx.bpdm.gate.service import mu.KotlinLogging +import org.eclipse.tractusx.bpdm.common.dto.SiteDto import org.eclipse.tractusx.bpdm.common.dto.response.LogisticAddressResponse import org.eclipse.tractusx.bpdm.common.dto.response.SiteResponse import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas -import org.eclipse.tractusx.bpdm.common.dto.saas.FetchResponse import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse +import org.eclipse.tractusx.bpdm.gate.api.model.response.PageSiteResponse import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry +import org.eclipse.tractusx.bpdm.gate.entity.Site import org.eclipse.tractusx.bpdm.gate.exception.SaasInvalidRecordException import org.eclipse.tractusx.bpdm.gate.exception.SaasNonexistentParentException import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository +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 @@ -47,32 +51,67 @@ class SiteService( private val poolClient: PoolClient, private val bpnConfigProperties: BpnConfigProperties, private val changelogRepository: ChangelogRepository, - private val sitePersistenceService: SitePersistenceService + private val sitePersistenceService: SitePersistenceService, + private val siteRepository: SiteRepository ) { private val logger = KotlinLogging.logger { } - fun getSites(limit: Int, startAfter: String?, externalIds: Collection? = null): PageStartAfterResponse { - val sitesPage = saasClient.getSites(limit, startAfter, externalIds) + fun getSites(page: Int, size: Int, externalIds: Collection? = null): PageSiteResponse { - val validEntries = toValidSites(sitesPage.values) + val sitesPage = if (externalIds != null) { + siteRepository.findByExternalIdIn(externalIds, PageRequest.of(page, size)) + } else { + siteRepository.findAll(PageRequest.of(page, size)) + } + + val siteGateInputResponse = toValidSite(sitesPage) - return PageStartAfterResponse( - total = sitesPage.total, - nextStartAfter = sitesPage.nextStartAfter, - content = validEntries, - invalidEntries = sitesPage.values.size - validEntries.size + return PageSiteResponse( + page = page, + totalElements = sitesPage.totalElements, + totalPages = sitesPage.totalPages, + contentSize = sitesPage.content.size, + content = siteGateInputResponse, + invalidEntries = 0 ) } - fun getSiteByExternalId(externalId: String): SiteGateInputResponse { - val fetchResponse = saasClient.getBusinessPartner(externalId) + private fun toValidSite(sitePage: Page): List { - when (fetchResponse.status) { - FetchResponse.Status.OK -> return toValidSiteInput(fetchResponse.businessPartner!!) - FetchResponse.Status.NOT_FOUND -> throw BpdmNotFoundException("Site", externalId) + return sitePage.content.map { site -> + val siteMapDto = SiteDto( + name = site.name, + states = mapToDtoSitesStates(site.states), + mainAddress = site.mainAddress.toLogisticAddressDto() + ) + + SiteGateInputResponse( + site = siteMapDto, + externalId = site.externalId, + legalEntityExternalId = site.legalEntity.externalId, + bpn = site.bpn, + processStartedAt = null //TODO Remove this? + ) } } + fun getSiteByExternalId(externalId: String): SiteGateInputResponse { + val siteRecord = siteRepository.findByExternalId(externalId) ?: throw BpdmNotFoundException("Site", externalId) + + return toValidSingleSite(siteRecord) + } + + private fun toValidSingleSite(siteRecord: Site): SiteGateInputResponse { + + return SiteGateInputResponse( + site = siteRecord.toSiteDto(), + externalId = siteRecord.externalId, + legalEntityExternalId = siteRecord.legalEntity.externalId, + bpn = siteRecord.bpn, + processStartedAt = null //TODO Remove this? + ) + } + /** * Get sites by first fetching sites from "augmented business partners" in SaaS. Augmented business partners from SaaS should contain a BPN, * which is then used to fetch the data for the sites from the bpdm pool. diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt index f6b439224..451725f92 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt @@ -24,9 +24,9 @@ import com.github.tomakehurst.wiremock.client.WireMock.* import com.github.tomakehurst.wiremock.core.WireMockConfiguration 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.saas.* import org.eclipse.tractusx.bpdm.gate.api.client.GateClient -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationStatus @@ -232,7 +232,7 @@ internal class SiteControllerInputIT @Autowired constructor( ) ) - val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val paginationValue = PaginationRequest(0, limit) val pageResponse = gateClient.sites().getSites(paginationValue) assertThat(pageResponse).isEqualTo( @@ -288,7 +288,7 @@ internal class SiteControllerInputIT @Autowired constructor( ) val externalIds = sitesSaas.mapNotNull { it.externalId } - val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val paginationValue = PaginationRequest(0, limit) val pageResponse = gateClient.sites().getSitesByExternalIds(paginationValue, externalIds) assertThat(pageResponse).isEqualTo( @@ -346,7 +346,7 @@ internal class SiteControllerInputIT @Autowired constructor( ) ) - val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val paginationValue = PaginationRequest(0, limit) val pageResponse = gateClient.sites().getSites(paginationValue) assertThat(pageResponse).isEqualTo( @@ -370,7 +370,7 @@ internal class SiteControllerInputIT @Autowired constructor( .willReturn(badRequest()) ) - val paginationValue = PaginationStartAfterRequest("", 10) + val paginationValue = PaginationRequest(0, 10) try { gateClient.sites().getSites(paginationValue) @@ -389,7 +389,7 @@ internal class SiteControllerInputIT @Autowired constructor( @Test fun `get sites, pagination limit exceeded`() { - val paginationValue = PaginationStartAfterRequest("", 999999) + val paginationValue = PaginationRequest(0, 999999) try { gateClient.sites().getSites(paginationValue) From 6de3c487786a96acc56754a8f7976bf3f78185f7 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Thu, 25 May 2023 13:40:47 +0100 Subject: [PATCH 10/12] fix(gate): Removal of unused data classes, changed site service logic, removal of SaaS unused method. Creation of mapping from Sites to toSiteGateInputResponse --- .../bridge/dummy/service/GateQueryService.kt | 9 +- .../tractusx/bpdm/gate/api/GateSiteApi.kt | 6 +- .../model/response/PageLegalEntityResponse.kt | 36 ----- .../response/PageLogisticAddressResponse.kt | 3 - .../api/model/response/PageSiteResponse.kt | 38 ------ .../bpdm/gate/controller/SiteController.kt | 13 +- .../bpdm/gate/service/ResponseMappings.kt | 22 ++- .../tractusx/bpdm/gate/service/SiteService.kt | 129 +----------------- 8 files changed, 37 insertions(+), 219 deletions(-) delete mode 100644 bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt delete mode 100644 bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt diff --git a/bpdm-bridge-dummy/src/main/kotlin/com/catenax/bpdm/bridge/dummy/service/GateQueryService.kt b/bpdm-bridge-dummy/src/main/kotlin/com/catenax/bpdm/bridge/dummy/service/GateQueryService.kt index 66c2510b9..3cb53895b 100644 --- a/bpdm-bridge-dummy/src/main/kotlin/com/catenax/bpdm/bridge/dummy/service/GateQueryService.kt +++ b/bpdm-bridge-dummy/src/main/kotlin/com/catenax/bpdm/bridge/dummy/service/GateQueryService.kt @@ -30,7 +30,6 @@ import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateDto import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.ChangelogResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType import org.springframework.stereotype.Service @@ -176,11 +175,11 @@ class GateQueryService( do { val pageResponse = gateClient.sites().getSitesByExternalIds( externalIds = externalIds, - paginationRequest = PaginationStartAfterRequest(pageStartAfter, bridgeConfigProperties.queryPageSize) + paginationRequest = PaginationRequest(0, bridgeConfigProperties.queryPageSize) ) - pageStartAfter = pageResponse.nextStartAfter - validContent.addAll(pageResponse.content) - invalidEntries += pageResponse.invalidEntries + //pageStartAfter = pageResponse.nextStartAfter + validContent.addAll(pageResponse.content) //TODO Needs to be changed according to the removal of SaaS + invalidEntries += 0//pageResponse.invalidEntries } while (pageStartAfter != null) logger.info { "Gate returned ${validContent.size} valid sites, $invalidEntries were invalid" } diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt index 62e3e2748..b332209e9 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt @@ -26,12 +26,12 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.responses.ApiResponses import jakarta.validation.Valid import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest +import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageSiteResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.springdoc.core.annotations.ParameterObject import org.springframework.http.MediaType @@ -92,7 +92,7 @@ interface GateSiteApi { fun getSitesByExternalIds( @ParameterObject @Valid paginationRequest: PaginationRequest, @RequestBody externalIds: Collection - ): PageSiteResponse + ): PageResponse @Operation( summary = "Get page of sites", @@ -106,7 +106,7 @@ interface GateSiteApi { ) @GetMapping("/input/sites") @GetExchange("/input/sites") - fun getSites(@ParameterObject @Valid paginationRequest: PaginationRequest): PageSiteResponse + fun getSites(@ParameterObject @Valid paginationRequest: PaginationRequest): PageResponse @Operation( summary = "Get page of sites", diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt deleted file mode 100644 index 5cb502f62..000000000 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLegalEntityResponse.kt +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************************************* - * 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.gate.api.model.response - -import io.swagger.v3.oas.annotations.media.Schema - -@Schema(description = "Paginated collection of Legal Entities") -data class PageLegalEntityResponse( - @Schema(description = "Total number of all results in all pages") - val totalElements: Long, - @Schema(description = "Total number pages") - val totalPages: Int, - @Schema(description = "Current page number") - val page: Int, - @Schema(description = "Number of results in the page") - val contentSize: Int, - @Schema(description = "Collection of results in the page") - val content: Collection, -) \ No newline at end of file diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt index beaba9aa2..0f0f889fd 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageLogisticAddressResponse.kt @@ -33,8 +33,5 @@ data class PageLogisticAddressResponse( val contentSize: Int, @Schema(description = "Collection of results in the page") val content: Collection, - @Schema(description = "Number of entries in the page that have been omitted due to being invalid") - val invalidEntries: Int - ) diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt deleted file mode 100644 index 169740f64..000000000 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/PageSiteResponse.kt +++ /dev/null @@ -1,38 +0,0 @@ -/******************************************************************************* - * 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.gate.api.model.response - -import io.swagger.v3.oas.annotations.media.Schema - -@Schema(description = "Paginated collection of Logistic Addresses") -data class PageSiteResponse( - @Schema(description = "Total number of all results in all pages") - val totalElements: Long, - @Schema(description = "Total number pages") - val totalPages: Int, - @Schema(description = "Current page number") - val page: Int, - @Schema(description = "Number of results in the page") - val contentSize: Int, - @Schema(description = "Collection of results in the page") - val content: Collection, - @Schema(description = "Number of entries in the page that have been omitted due to being invalid") - val invalidEntries: Int -) \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt index 168c561dd..804c5b7c4 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt @@ -20,15 +20,16 @@ package org.eclipse.tractusx.bpdm.gate.controller import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest +import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.GateSiteApi import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageSiteResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties +import org.eclipse.tractusx.bpdm.gate.containsDuplicates import org.eclipse.tractusx.bpdm.gate.service.SiteService import org.eclipse.tractusx.bpdm.gate.service.ValidationService import org.springframework.http.HttpStatus @@ -43,9 +44,9 @@ class SiteController( ) : GateSiteApi { override fun upsertSites(sites: Collection): ResponseEntity { -// if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { -// return ResponseEntity(HttpStatus.BAD_REQUEST) -// } + if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) { + return ResponseEntity(HttpStatus.BAD_REQUEST) + } siteService.upsertSites(sites) return ResponseEntity(HttpStatus.OK) } @@ -57,11 +58,11 @@ class SiteController( override fun getSitesByExternalIds( paginationRequest: PaginationRequest, externalIds: Collection - ): PageSiteResponse { + ): PageResponse { return siteService.getSites(page = paginationRequest.page, size = paginationRequest.size, externalIds = externalIds) } - override fun getSites(paginationRequest: PaginationRequest): PageSiteResponse { + override fun getSites(paginationRequest: PaginationRequest): PageResponse { return siteService.getSites(page = paginationRequest.page, size = paginationRequest.size) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt index ba90638ca..5ee6e2f90 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt @@ -22,11 +22,7 @@ package org.eclipse.tractusx.bpdm.gate.service import org.eclipse.tractusx.bpdm.common.dto.* import org.eclipse.tractusx.bpdm.common.dto.SiteStateDto import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse -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.LegalEntityGateInputRequest -import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest +import org.eclipse.tractusx.bpdm.gate.api.model.* import org.eclipse.tractusx.bpdm.gate.api.model.response.ChangelogResponse import org.eclipse.tractusx.bpdm.gate.entity.* import org.springframework.data.domain.Page @@ -331,7 +327,9 @@ fun Name.toNameDto(): NameDto { return NameDto(value, shortName) } +//LegalEntity mapping to LegalEntityGateInputResponse fun LegalEntity.LegalEntityGateInputResponse(legalEntity: LegalEntity): LegalEntityGateInputResponse { + return LegalEntityGateInputResponse( legalEntity = legalEntity.toLegalEntityDto(), externalId = legalEntity.externalId, @@ -340,6 +338,7 @@ fun LegalEntity.LegalEntityGateInputResponse(legalEntity: LegalEntity): LegalEnt ) } +//Site mapping to SiteDto fun Site.toSiteDto(): SiteDto { return SiteDto( @@ -351,4 +350,17 @@ fun Site.toSiteDto(): SiteDto { fun mapToDtoSitesStates(states: MutableSet): Collection { return states.map { SiteStateDto(it.description, it.validFrom, it.validTo, it.type) } +} + +//LegalEntity mapping to LegalEntityGateInputResponse +fun Site.toSiteGateInputResponse(sitePage: Site): SiteGateInputResponse { + + return SiteGateInputResponse( + site = sitePage.toSiteDto(), + externalId = externalId, + legalEntityExternalId = legalEntity.externalId, + bpn = bpn, + processStartedAt = null //TODO Remove this? + ) + } \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt index 23d47f1af..5e6d394e8 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt @@ -20,8 +20,8 @@ package org.eclipse.tractusx.bpdm.gate.service import mu.KotlinLogging -import org.eclipse.tractusx.bpdm.common.dto.SiteDto import org.eclipse.tractusx.bpdm.common.dto.response.LogisticAddressResponse +import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.common.dto.response.SiteResponse import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException @@ -30,11 +30,9 @@ import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageSiteResponse import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry import org.eclipse.tractusx.bpdm.gate.entity.Site -import org.eclipse.tractusx.bpdm.gate.exception.SaasInvalidRecordException import org.eclipse.tractusx.bpdm.gate.exception.SaasNonexistentParentException import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository @@ -45,7 +43,6 @@ import org.springframework.stereotype.Service @Service class SiteService( private val saasRequestMappingService: SaasRequestMappingService, - private val inputSaasMappingService: InputSaasMappingService, private val outputSaasMappingService: OutputSaasMappingService, private val saasClient: SaasClient, private val poolClient: PoolClient, @@ -56,7 +53,7 @@ class SiteService( ) { private val logger = KotlinLogging.logger { } - fun getSites(page: Int, size: Int, externalIds: Collection? = null): PageSiteResponse { + fun getSites(page: Int, size: Int, externalIds: Collection? = null): PageResponse { val sitesPage = if (externalIds != null) { siteRepository.findByExternalIdIn(externalIds, PageRequest.of(page, size)) @@ -64,52 +61,25 @@ class SiteService( siteRepository.findAll(PageRequest.of(page, size)) } - val siteGateInputResponse = toValidSite(sitesPage) - - return PageSiteResponse( + return PageResponse( page = page, totalElements = sitesPage.totalElements, totalPages = sitesPage.totalPages, contentSize = sitesPage.content.size, - content = siteGateInputResponse, - invalidEntries = 0 + content = toValidSite(sitesPage) ) } private fun toValidSite(sitePage: Page): List { - return sitePage.content.map { site -> - val siteMapDto = SiteDto( - name = site.name, - states = mapToDtoSitesStates(site.states), - mainAddress = site.mainAddress.toLogisticAddressDto() - ) - - SiteGateInputResponse( - site = siteMapDto, - externalId = site.externalId, - legalEntityExternalId = site.legalEntity.externalId, - bpn = site.bpn, - processStartedAt = null //TODO Remove this? - ) + site.toSiteGateInputResponse(site) } } fun getSiteByExternalId(externalId: String): SiteGateInputResponse { val siteRecord = siteRepository.findByExternalId(externalId) ?: throw BpdmNotFoundException("Site", externalId) - return toValidSingleSite(siteRecord) - } - - private fun toValidSingleSite(siteRecord: Site): SiteGateInputResponse { - - return SiteGateInputResponse( - site = siteRecord.toSiteDto(), - externalId = siteRecord.externalId, - legalEntityExternalId = siteRecord.legalEntity.externalId, - bpn = siteRecord.bpn, - processStartedAt = null //TODO Remove this? - ) + return siteRecord.toSiteGateInputResponse(siteRecord) } /** @@ -176,21 +146,12 @@ class SiteService( */ fun upsertSites(sites: Collection) { - val sitesSaas = toSaasModels(sites) - saasClient.upsertSites(sitesSaas) - - sitePersistenceService.persistSitesBP(sites) - // create changelog entry if all goes well from saasClient sites.forEach { site -> changelogRepository.save(ChangelogEntry(site.externalId, LsaType.Site)) } sitePersistenceService.persistSitesBP(sites) - - deleteParentRelationsOfSites(sites) - - upsertParentRelations(sites) } /** @@ -201,84 +162,6 @@ class SiteService( return sites.map { toSaasModel(it, parentLegalEntitiesByExternalId[it.legalEntityExternalId]) } } - private fun upsertParentRelations(sites: Collection) { - val relations = sites.map { - SaasClient.SiteLegalEntityRelation( - siteExternalId = it.externalId, - legalEntityExternalId = it.legalEntityExternalId - ) - }.toList() - saasClient.upsertSiteRelations(relations) - } - - private fun deleteParentRelationsOfSites(sites: Collection) { - val sitesPage = saasClient.getSites(externalIds = sites.map { it.externalId }) - saasClient.deleteParentRelations(sitesPage.values) - } - - private fun toValidSites(partners: Collection): Collection { - return partners.mapNotNull { - try { - toValidSiteInput(it) - - } catch (e: RuntimeException) { - logger.warn { "${getLogDescription(it)} will be ignored: ${e.message}" } - null - } - } - } - - private fun toValidSiteInput(partner: BusinessPartnerSaas): SiteGateInputResponse { - if (!validateSiteBusinessPartner(partner)) { - throw SaasInvalidRecordException(partner.id) - } - return inputSaasMappingService.toInputSite(partner) - } - - private fun validateSiteBusinessPartner(partner: BusinessPartnerSaas): Boolean { - val logMessageStart = getLogDescription(partner) - return validateAddresses(partner, logMessageStart) - && validateLegalEntityParents(partner, logMessageStart) - && validateNames(partner, logMessageStart) - } - - private fun getLogDescription(partner: BusinessPartnerSaas) = - "SaaS business partner for site with ${if (partner.id != null) "ID " + partner.id else "external id " + partner.externalId}" - - private fun validateNames(partner: BusinessPartnerSaas, logMessageStart: String): Boolean { - if (partner.names.size > 1) { - logger.warn { "$logMessageStart has multiple names." } - } - if (partner.names.isEmpty()) { - logger.warn { "$logMessageStart does not have a name." } - return false - } - return true - } - - private fun validateAddresses(partner: BusinessPartnerSaas, logMessageStart: String): Boolean { - if (partner.addresses.size > 1) { - logger.warn { "$logMessageStart has multiple main addresses" } - } - if (partner.addresses.isEmpty()) { - logger.warn { "$logMessageStart does not have a main address" } - return false - } - return true - } - - private fun validateLegalEntityParents(partner: BusinessPartnerSaas, logMessageStart: String): Boolean { - val numLegalEntityParents = inputSaasMappingService.toParentLegalEntityExternalIds(partner).size - if (numLegalEntityParents > 1) { - logger.warn { "$logMessageStart has multiple parent legal entities." } - } - if (numLegalEntityParents == 0) { - logger.warn { "$logMessageStart does not have a parent legal entity." } - return false - } - return true - } - private fun getParentLegalEntities(sites: Collection): Map { val parentLegalEntityExternalIds = sites.map { it.legalEntityExternalId }.distinct().toList() val parentLegalEntitiesPage = saasClient.getLegalEntities(externalIds = parentLegalEntityExternalIds) From 9fbae6df89dbd0aadbc0a4cdfeefab5f54694bfe Mon Sep 17 00:00:00 2001 From: alexsilva Date: Fri, 26 May 2023 11:51:11 +0100 Subject: [PATCH 11/12] fix(gate): site unit tests fix according to fetch service changes --- .../gate/controller/SiteControllerInputIT.kt | 546 +++++------------- .../tractusx/bpdm/gate/util/RequestValues.kt | 8 +- .../tractusx/bpdm/gate/util/ResponseValues.kt | 33 ++ 3 files changed, 194 insertions(+), 393 deletions(-) diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt index 451725f92..8305fb13e 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt @@ -25,20 +25,18 @@ import com.github.tomakehurst.wiremock.core.WireMockConfiguration 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.response.PageStartAfterResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationStatus -import org.eclipse.tractusx.bpdm.gate.config.SaasConfigProperties import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository import org.eclipse.tractusx.bpdm.gate.util.* import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.SAAS_MOCK_BUSINESS_PARTNER_PATH -import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.SAAS_MOCK_DELETE_RELATIONS_PATH -import org.eclipse.tractusx.bpdm.gate.util.EndpointValues.SAAS_MOCK_RELATIONS_PATH import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.RegisterExtension import org.springframework.beans.factory.annotation.Autowired @@ -56,9 +54,9 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti @ContextConfiguration(initializers = [PostgreSQLContextInitializer::class]) internal class SiteControllerInputIT @Autowired constructor( private val objectMapper: ObjectMapper, - private val saasConfigProperties: SaasConfigProperties, val gateClient: GateClient, - private val siteRepository: SiteRepository + private val siteRepository: SiteRepository, + private val dbTestHelpers: DbTestHelpers ) { companion object { @RegisterExtension @@ -73,58 +71,43 @@ internal class SiteControllerInputIT @Autowired constructor( } } + @BeforeEach + fun beforeEach() { + dbTestHelpers.truncateDbTables() + } + /** - * Given site exists in SaaS + * Given site exists in the database * When getting site by external id - * Then site mapped to the catena data model should be returned + * Then site response should be returned */ @Test fun `get site by external id`() { - val expectedSite = ResponseValues.siteGateInputResponse1 + val expectedSite = ResponseValues.persistencesiteGateInputResponse1 - wireMockServer.stubFor( - post(urlPathMatching(EndpointValues.SAAS_MOCK_FETCH_BUSINESS_PARTNER_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - FetchResponse( - businessPartner = SaasValues.siteBusinessPartnerWithRelations1, - status = FetchResponse.Status.OK - ) - ) - ) - ) + val legalEntities = listOf( + RequestValues.legalEntityGateInputRequest1 + ) + + val sites = listOf( + RequestValues.siteGateInputRequest1 ) - val site = gateClient.sites().getSiteByExternalId(SaasValues.siteBusinessPartnerWithRelations1.externalId.toString()) + gateClient.legalEntities().upsertLegalEntities(legalEntities) + gateClient.sites().upsertSites(sites) + + val site = gateClient.sites().getSiteByExternalId(CommonValues.externalIdSite1) assertThat(site).usingRecursiveComparison().isEqualTo(expectedSite) } /** - * Given site does not exist in SaaS + * Given site does not exist in database * When getting site by external id * Then "not found" response is sent */ @Test fun `get site by external id, not found`() { - wireMockServer.stubFor( - post(urlPathMatching(EndpointValues.SAAS_MOCK_FETCH_BUSINESS_PARTNER_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - FetchResponse( - businessPartner = null, - status = FetchResponse.Status.NOT_FOUND - ) - ) - ) - ) - ) try { gateClient.sites().getSiteByExternalId("nonexistent-externalid123") @@ -134,61 +117,61 @@ internal class SiteControllerInputIT @Autowired constructor( } - /** - * When SaaS api responds with an error status code while fetching site by external id - * Then an internal server error response should be sent - */ - @Test - fun `get site by external id, SaaS error`() { - wireMockServer.stubFor( - post(urlPathMatching(EndpointValues.SAAS_MOCK_FETCH_BUSINESS_PARTNER_PATH)) - .willReturn(badRequest()) - ) - - try { - gateClient.sites().getSiteByExternalId(SaasValues.legalEntityRequest1.externalId.toString()) - } catch (e: WebClientResponseException) { - val statusCode: HttpStatusCode = e.statusCode - val statusCodeValue: Int = statusCode.value() - assertTrue(statusCodeValue in 500..599) - } - } - - /** - * Given site without main address in SaaS - * When query by its external ID - * Then server error is returned - */ - @Test - fun `get site without main address, expect error`() { - - val invalidPartner = SaasValues.siteBusinessPartnerWithRelations1.copy(addresses = emptyList()) - - wireMockServer.stubFor( - post(urlPathMatching(EndpointValues.SAAS_MOCK_FETCH_BUSINESS_PARTNER_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - FetchResponse( - businessPartner = invalidPartner, - status = FetchResponse.Status.OK - ) - ) - ) - ) - ) - - try { - gateClient.sites().getSiteByExternalId(SaasValues.siteBusinessPartnerWithRelations1.externalId.toString()) - } catch (e: WebClientResponseException) { - val statusCode: HttpStatusCode = e.statusCode - val statusCodeValue: Int = statusCode.value() - assertTrue(statusCodeValue in 500..599) - } - - } +// /** +// * When SaaS api responds with an error status code while fetching site by external id +// * Then an internal server error response should be sent +// */ +// @Test +// fun `DEPRECATED get site by external id, SaaS error`() { +// wireMockServer.stubFor( +// post(urlPathMatching(EndpointValues.SAAS_MOCK_FETCH_BUSINESS_PARTNER_PATH)) +// .willReturn(badRequest()) +// ) +// +// try { +// gateClient.sites().getSiteByExternalId(SaasValues.legalEntityRequest1.externalId.toString()) +// } catch (e: WebClientResponseException) { +// val statusCode: HttpStatusCode = e.statusCode +// val statusCodeValue: Int = statusCode.value() +// assertTrue(statusCodeValue in 500..599) +// } +// } + +// /** +// * Given site without main address in SaaS +// * When query by its external ID +// * Then server error is returned +// */ +// @Test +// fun `DEPRECATED get site without main address, expect error`() { +// +// val invalidPartner = SaasValues.siteBusinessPartnerWithRelations1.copy(addresses = emptyList()) +// +// wireMockServer.stubFor( +// post(urlPathMatching(EndpointValues.SAAS_MOCK_FETCH_BUSINESS_PARTNER_PATH)) +// .willReturn( +// aResponse() +// .withHeader("Content-Type", "application/json") +// .withBody( +// objectMapper.writeValueAsString( +// FetchResponse( +// businessPartner = invalidPartner, +// status = FetchResponse.Status.OK +// ) +// ) +// ) +// ) +// ) +// +// try { +// gateClient.sites().getSiteByExternalId(SaasValues.siteBusinessPartnerWithRelations1.externalId.toString()) +// } catch (e: WebClientResponseException) { +// val statusCode: HttpStatusCode = e.statusCode +// val statusCodeValue: Int = statusCode.value() +// assertTrue(statusCodeValue in 500..599) +// } +// +// } /** * Given sites exists in SaaS @@ -197,50 +180,42 @@ internal class SiteControllerInputIT @Autowired constructor( */ @Test fun `get sites`() { - val sitesSaas = listOf( - SaasValues.siteBusinessPartnerWithRelations1, - SaasValues.siteBusinessPartnerWithRelations2 - ) - val expectedSites = listOf( - ResponseValues.siteGateInputResponse1, - ResponseValues.siteGateInputResponse2 + ResponseValues.persistencesiteGateInputResponse1, + ResponseValues.persistenceSiteGateInputResponse2 ) - val limit = 2 - val startAfter = "Aaa111" - val nextStartAfter = "Aaa222" - val total = 10 - val invalidEntries = 0 + val page = 0 + val size = 10 - wireMockServer.stubFor( - get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - PagedResponseSaas( - limit = limit, - startAfter = startAfter, - nextStartAfter = nextStartAfter, - total = total, - values = sitesSaas - ) - ) - ) - ) + val totalElements = 2L + val totalPages = 1 + val pageValue = 0 + val contentSize = 2 + + val legalEntities = listOf( + RequestValues.legalEntityGateInputRequest1, + RequestValues.legalEntityGateInputRequest2 ) - val paginationValue = PaginationRequest(0, limit) + val sites = listOf( + RequestValues.siteGateInputRequest1, + RequestValues.siteGateInputRequest2 + ) + + gateClient.legalEntities().upsertLegalEntities(legalEntities) + gateClient.sites().upsertSites(sites) + + val paginationValue = PaginationRequest(page, size) val pageResponse = gateClient.sites().getSites(paginationValue) assertThat(pageResponse).isEqualTo( - PageStartAfterResponse( - total = total, - nextStartAfter = nextStartAfter, - content = expectedSites, - invalidEntries = invalidEntries + PageResponse( + totalElements = totalElements, + totalPages = totalPages, + page = pageValue, + contentSize = contentSize, + content = expectedSites ) ) } @@ -252,109 +227,44 @@ internal class SiteControllerInputIT @Autowired constructor( */ @Test fun `get sites by external id`() { - val sitesSaas = listOf( - SaasValues.siteBusinessPartnerWithRelations1, - SaasValues.siteBusinessPartnerWithRelations2 - ) - val expectedSites = listOf( - ResponseValues.siteGateInputResponse1, - ResponseValues.siteGateInputResponse2 - ) - - val limit = 2 - val startAfter = "Aaa111" - val nextStartAfter = "Aaa222" - val total = 10 - val invalidEntries = 0 - - wireMockServer.stubFor( - get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - PagedResponseSaas( - limit = limit, - startAfter = startAfter, - nextStartAfter = nextStartAfter, - total = total, - values = sitesSaas - ) - ) - ) - ) + ResponseValues.persistencesiteGateInputResponse1, + ResponseValues.persistenceSiteGateInputResponse2 ) - val externalIds = sitesSaas.mapNotNull { it.externalId } - val paginationValue = PaginationRequest(0, limit) - val pageResponse = gateClient.sites().getSitesByExternalIds(paginationValue, externalIds) + val page = 0 + val size = 10 - assertThat(pageResponse).isEqualTo( - PageStartAfterResponse( - total = total, - nextStartAfter = nextStartAfter, - content = expectedSites, - invalidEntries = invalidEntries - ) - ) - } + val totalElements = 2L + val totalPages = 1 + val pageValue = 0 + val contentSize = 2 - /** - * Given invalid sites in SaaS - * When getting sites page - * Then only valid sites on page returned - */ - @Test - fun `filter invalid sites`() { - val sitesSaas = listOf( - SaasValues.siteBusinessPartnerWithRelations1, - SaasValues.siteBusinessPartnerWithRelations2, - SaasValues.siteBusinessPartnerWithRelations1.copy(addresses = emptyList()), // site without address - SaasValues.siteBusinessPartnerWithRelations1.copy(names = listOf()), // site without names - SaasValues.siteBusinessPartnerWithRelations1.copy(relations = listOf()) // site without legal entity parent + val legalEntities = listOf( + RequestValues.legalEntityGateInputRequest1, + RequestValues.legalEntityGateInputRequest2 ) - val expectedSites = listOf( - ResponseValues.siteGateInputResponse1, - ResponseValues.siteGateInputResponse2 + val sites = listOf( + RequestValues.siteGateInputRequest1, + RequestValues.siteGateInputRequest2 ) - val limit = 5 - val startAfter = "Aaa111" - val nextStartAfter = "Aaa222" - val total = 10 - val invalidEntries = 3 + gateClient.legalEntities().upsertLegalEntities(legalEntities) + gateClient.sites().upsertSites(sites) - wireMockServer.stubFor( - get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - PagedResponseSaas( - limit = limit, - startAfter = startAfter, - nextStartAfter = nextStartAfter, - total = total, - values = sitesSaas - ) - ) - ) - ) - ) + val externalIds = sites.map { it.externalId } - val paginationValue = PaginationRequest(0, limit) - val pageResponse = gateClient.sites().getSites(paginationValue) + val paginationValue = PaginationRequest(page, size) + val pageResponse = gateClient.sites().getSitesByExternalIds(paginationValue, externalIds) assertThat(pageResponse).isEqualTo( - PageStartAfterResponse( - total = total, - nextStartAfter = nextStartAfter, - content = expectedSites, - invalidEntries = invalidEntries + PageResponse( + totalElements = totalElements, + totalPages = totalPages, + page = pageValue, + contentSize = contentSize, + content = expectedSites ) ) } @@ -400,9 +310,9 @@ internal class SiteControllerInputIT @Autowired constructor( } /** - * Given legal entities in SaaS + * Given legal entities in the database * When upserting sites of legal entities - * Then upsert sites and relations in SaaS api should be called with the site data mapped to the SaaS data model + * Then sites entity should be persisted in the database with associated legal entities */ @Test fun `upsert sites`() { @@ -416,137 +326,6 @@ internal class SiteControllerInputIT @Autowired constructor( RequestValues.legalEntityGateInputRequest2 ) - val parentLegalEntitiesSaas = listOf( - SaasValues.legalEntityResponse1, - SaasValues.legalEntityResponse2 - ) - - val expectedSites = listOf( - SaasValues.siteBusinessPartnerRequest1, - SaasValues.siteBusinessPartnerRequest2 - ) - - val expectedRelations = listOf( - SaasValues.relationSite1ToLegalEntity, - SaasValues.relationSite2ToLegalEntity - ) - - val expectedDeletedRelations = listOf( - DeleteRelationsRequestSaas.RelationToDeleteSaas( - startNode = DeleteRelationsRequestSaas.RelationNodeToDeleteSaas( - dataSourceId = saasConfigProperties.datasource, - externalId = SaasValues.relationSite1ToLegalEntity.startNode - ), - endNode = DeleteRelationsRequestSaas.RelationNodeToDeleteSaas( - dataSourceId = saasConfigProperties.datasource, - externalId = SaasValues.relationSite1ToLegalEntity.endNode - ), - ), - DeleteRelationsRequestSaas.RelationToDeleteSaas( - startNode = DeleteRelationsRequestSaas.RelationNodeToDeleteSaas( - dataSourceId = saasConfigProperties.datasource, - externalId = SaasValues.relationSite2ToLegalEntity.startNode - ), - endNode = DeleteRelationsRequestSaas.RelationNodeToDeleteSaas( - dataSourceId = saasConfigProperties.datasource, - externalId = SaasValues.relationSite2ToLegalEntity.endNode - ), - ), - ) - - // mock "get parent legal entities" - wireMockServer.stubFor( - get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) - .withQueryParam("externalId", equalTo(sites.map { it.legalEntityExternalId }.joinToString(","))) - .withQueryParam("dataSource", equalTo(saasConfigProperties.datasource)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - PagedResponseSaas( - limit = 50, - total = 2, - values = parentLegalEntitiesSaas - ) - ) - ) - ) - ) - val stubMappingUpsertSites = wireMockServer.stubFor( - put(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - UpsertResponse( - emptyList(), - emptyList(), - 2, - 0 - ) - ) - ) - ) - ) - // mock "get sites with relations" - // this simulates the case that the site already had some relations - wireMockServer.stubFor( - get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) - .withQueryParam("externalId", equalTo(sites.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.siteBusinessPartnerWithRelations1, - SaasValues.siteBusinessPartnerWithRelations2 - ) - ) - ) - ) - ) - ) - - val stubMappingDeleteRelations = wireMockServer.stubFor( - post(urlPathMatching(SAAS_MOCK_DELETE_RELATIONS_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - DeleteRelationsResponseSaas(2) - ) - ) - ) - ) - - val stubMappingUpsertRelations = wireMockServer.stubFor( - put(urlPathMatching(SAAS_MOCK_RELATIONS_PATH)) - .willReturn( - aResponse() - .withHeader("Content-Type", "application/json") - .withBody( - objectMapper.writeValueAsString( - UpsertRelationsResponseSaas( - failures = emptyList(), - numberOfFailed = 0, - numberOfInserts = 2, - numberOfProvidedRelations = 2, - numberOfUpdates = 0 - ) - ) - ) - ) - ) - try { gateClient.legalEntities().upsertLegalEntities(legalEntities) gateClient.sites().upsertSites(sites) @@ -561,24 +340,36 @@ internal class SiteControllerInputIT @Autowired constructor( val siteExternal2 = siteRepository.findByExternalId("site-external-2") Assertions.assertNotEquals(siteExternal2, null) - // TODO: check that "upsert sites" was called in SaaS as expected -// val upsertSitesRequest = wireMockServer.deserializeMatchedRequests(stubMappingUpsertSites, objectMapper).single() -// assertThat(upsertSitesRequest.businessPartners).containsExactlyInAnyOrderElementsOf(expectedSites) + } - // check that "delete relations" was called in SaaS as expected - val deleteRelationsRequestSaas = - wireMockServer.deserializeMatchedRequests(stubMappingDeleteRelations, objectMapper).single() - assertThat(deleteRelationsRequestSaas.relations).containsExactlyInAnyOrderElementsOf(expectedDeletedRelations) + /** + * When upserting multiple sites to the database with same externalid + * Then bad request error should appear + */ + @Test + fun `upsert sites, same externalid`() { + val sites = listOf( + RequestValues.siteGateInputRequest1, + RequestValues.siteGateInputRequest1 + ) + + val legalEntities = listOf( + RequestValues.legalEntityGateInputRequest1, + RequestValues.legalEntityGateInputRequest2 + ) + + try { + gateClient.legalEntities().upsertLegalEntities(legalEntities) + gateClient.sites().upsertSites(sites) + } catch (e: WebClientResponseException) { + assertEquals(HttpStatus.BAD_REQUEST, e.statusCode) + } - // check that "upsert relations" was called in SaaS as expected - val upsertRelationsRequest = wireMockServer.deserializeMatchedRequests(stubMappingUpsertRelations, objectMapper).single() - assertThat(upsertRelationsRequest.relations).containsExactlyInAnyOrderElementsOf(expectedRelations) } /** - * Given legal entities in SaaS * When upserting sites of legal entities using a legal entity external id that does not exist - * Then a bad request response should be sent + * Then a "not_found" response should be sent */ @Test fun `upsert sites, legal entity parent not found`() { @@ -586,34 +377,11 @@ internal class SiteControllerInputIT @Autowired constructor( RequestValues.siteGateInputRequest1, RequestValues.siteGateInputRequest2 ) - val parentLegalEntitiesSaas = listOf( - SaasValues.legalEntityResponse1 - ) - - // mock "get parent legal entities" - wireMockServer.stubFor( - get(urlPathMatching(SAAS_MOCK_BUSINESS_PARTNER_PATH)) - .withQueryParam("externalId", equalTo(sites.map { 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 - ) - ) - ) - ) - ) try { gateClient.sites().upsertSites(sites) } catch (e: WebClientResponseException) { - assertEquals(HttpStatus.BAD_REQUEST, e.statusCode) + assertEquals(HttpStatus.NOT_FOUND, e.statusCode) } } diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt index d85fdb117..ef952c7ab 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt @@ -240,7 +240,7 @@ object RequestValues { legalForm = CommonValues.legalFormTechnicalKey1, states = listOf(leBusinessStatus1), classifications = listOf(classification1, classification2), - legalAddress = address1 + legalAddress = Logisticaddress1 ) val legalEntity2 = LegalEntityDto( @@ -249,7 +249,7 @@ object RequestValues { legalForm = CommonValues.legalFormTechnicalKey2, states = listOf(leBusinessStatus2), classifications = listOf(classification3, classification4), - legalAddress = address2 + legalAddress = Logisticaddress2 ) val legalEntity3 = LegalEntityDto( @@ -282,13 +282,13 @@ object RequestValues { val site1 = SiteDto( name = CommonValues.nameSite1, states = listOf(siteBusinessStatus1), - mainAddress = address1 + mainAddress = Logisticaddress1 ) val site2 = SiteDto( name = CommonValues.nameSite2, states = listOf(siteBusinessStatus2), - mainAddress = address2 + mainAddress = Logisticaddress2 ) val siteGateInputRequest1 = SiteGateInputRequest( diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt index afcbc39d6..f4c93ec96 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/ResponseValues.kt @@ -258,6 +258,7 @@ object ResponseValues { bpn = CommonValues.bpn2, processStartedAt = SaasValues.modificationTime2, ) + val legalEntityGateInputResponse3 = LegalEntityGateInputResponse( legalEntity = RequestValues.legalEntity3, externalId = CommonValues.externalId3, @@ -265,6 +266,20 @@ object ResponseValues { processStartedAt = null, ) + //Values without processStartedAt value + val newLegalEntityGateInputResponse1 = LegalEntityGateInputResponse( + legalEntity = RequestValues.legalEntity1, + externalId = CommonValues.externalId1, + bpn = CommonValues.bpn1, + processStartedAt = null, + ) + val newLegalEntityGateInputResponse2 = LegalEntityGateInputResponse( + legalEntity = RequestValues.legalEntity2, + externalId = CommonValues.externalId2, + bpn = CommonValues.bpn2, + processStartedAt = null, + ) + val legalEntityGateOutput1 = LegalEntityGateOutput( legalEntity = legalEntityResponse1, legalAddress = logisticAddress1, @@ -310,6 +325,24 @@ object ResponseValues { processStartedAt = SaasValues.modificationTime2, ) + //New Site Response values without processStartedAt value + val persistencesiteGateInputResponse1 = SiteGateInputResponse( + site = RequestValues.site1, + externalId = CommonValues.externalIdSite1, + legalEntityExternalId = CommonValues.externalId1, + bpn = CommonValues.bpnSite1, + processStartedAt = null, + ) + + val persistenceSiteGateInputResponse2 = SiteGateInputResponse( + site = RequestValues.site2, + externalId = CommonValues.externalIdSite2, + legalEntityExternalId = CommonValues.externalId2, + bpn = CommonValues.bpnSite2, + processStartedAt = null, + ) + + val siteGateOutput1 = SiteGateOutput( site = siteResponse1, mainAddress = logisticAddress1, From c643d3d3b336f8709e1d8fc8d383cf1a6013280b Mon Sep 17 00:00:00 2001 From: alexsilva Date: Mon, 29 May 2023 11:50:13 +0100 Subject: [PATCH 12/12] fix(gate): fix sites unit test according to new response values --- .../bpdm/gate/controller/SiteControllerInputIT.kt | 7 ++++--- .../org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt index 8305fb13e..4226e15c2 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerInputIT.kt @@ -98,7 +98,7 @@ internal class SiteControllerInputIT @Autowired constructor( val site = gateClient.sites().getSiteByExternalId(CommonValues.externalIdSite1) - assertThat(site).usingRecursiveComparison().isEqualTo(expectedSite) + assertThat(site).usingRecursiveComparison().ignoringFieldsMatchingRegexes(".*processStartedAt*", ".*administrativeAreaLevel1*").isEqualTo(expectedSite) } /** @@ -209,7 +209,7 @@ internal class SiteControllerInputIT @Autowired constructor( val paginationValue = PaginationRequest(page, size) val pageResponse = gateClient.sites().getSites(paginationValue) - assertThat(pageResponse).isEqualTo( + assertThat(pageResponse).usingRecursiveComparison().ignoringFieldsMatchingRegexes(".*processStartedAt*", ".*administrativeAreaLevel1*").isEqualTo( PageResponse( totalElements = totalElements, totalPages = totalPages, @@ -218,6 +218,7 @@ internal class SiteControllerInputIT @Autowired constructor( content = expectedSites ) ) + } /** @@ -258,7 +259,7 @@ internal class SiteControllerInputIT @Autowired constructor( val paginationValue = PaginationRequest(page, size) val pageResponse = gateClient.sites().getSitesByExternalIds(paginationValue, externalIds) - assertThat(pageResponse).isEqualTo( + assertThat(pageResponse).usingRecursiveComparison().ignoringFieldsMatchingRegexes(".*processStartedAt*", ".*administrativeAreaLevel1*").isEqualTo( PageResponse( totalElements = totalElements, totalPages = totalPages, diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt index ef952c7ab..d85fdb117 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/util/RequestValues.kt @@ -240,7 +240,7 @@ object RequestValues { legalForm = CommonValues.legalFormTechnicalKey1, states = listOf(leBusinessStatus1), classifications = listOf(classification1, classification2), - legalAddress = Logisticaddress1 + legalAddress = address1 ) val legalEntity2 = LegalEntityDto( @@ -249,7 +249,7 @@ object RequestValues { legalForm = CommonValues.legalFormTechnicalKey2, states = listOf(leBusinessStatus2), classifications = listOf(classification3, classification4), - legalAddress = Logisticaddress2 + legalAddress = address2 ) val legalEntity3 = LegalEntityDto( @@ -282,13 +282,13 @@ object RequestValues { val site1 = SiteDto( name = CommonValues.nameSite1, states = listOf(siteBusinessStatus1), - mainAddress = Logisticaddress1 + mainAddress = address1 ) val site2 = SiteDto( name = CommonValues.nameSite2, states = listOf(siteBusinessStatus2), - mainAddress = Logisticaddress2 + mainAddress = address2 ) val siteGateInputRequest1 = SiteGateInputRequest(