diff --git a/bpdm-cleaning-service-dummy/src/main/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceDummy.kt b/bpdm-cleaning-service-dummy/src/main/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceDummy.kt index 93d9a34fa..4e1ab2f60 100644 --- a/bpdm-cleaning-service-dummy/src/main/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceDummy.kt +++ b/bpdm-cleaning-service-dummy/src/main/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceDummy.kt @@ -67,9 +67,9 @@ class CleaningServiceDummy( logger.info { "${cleaningTasks.size} tasks found for cleaning. Proceeding with cleaning..." } if (cleaningTasks.isNotEmpty()) { - val cleaningResults = cleaningTasks.map { reservedTask -> - processCleaningTask(reservedTask) - } + val cleaningResults = cleaningTasks + .sortedBy { reservedTask-> reservedTask.priority } + .map { reservedTask -> processCleaningTask(reservedTask) } orchestrationApiClient.goldenRecordTasks.resolveStepResults(TaskStepResultRequest(step, cleaningResults)) logger.info { "Cleaning tasks processing completed for this iteration." } diff --git a/bpdm-cleaning-service-dummy/src/test/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceApiCallsTest.kt b/bpdm-cleaning-service-dummy/src/test/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceApiCallsTest.kt index c2f1cb976..37d01cc9d 100644 --- a/bpdm-cleaning-service-dummy/src/test/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceApiCallsTest.kt +++ b/bpdm-cleaning-service-dummy/src/test/kotlin/org/eclipse/tractusx/bpdm/cleaning/service/CleaningServiceApiCallsTest.kt @@ -440,7 +440,7 @@ class CleaningServiceApiCallsTest @Autowired constructor( // Helper method to create a sample TaskStepReservationResponse private fun createSampleTaskStepReservationResponse(businessPartner: BusinessPartner): TaskStepReservationResponse { - return TaskStepReservationResponse(listOf(TaskStepReservationEntryDto(fixedTaskId, UUID.randomUUID().toString(), businessPartner)), Instant.MIN) + return TaskStepReservationResponse(listOf(TaskStepReservationEntryDto(fixedTaskId, UUID.randomUUID().toString(), businessPartner, PriorityEnum.High)), Instant.MIN) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/TaskCreationServices.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/TaskCreationServices.kt index 30c041848..e762d1c53 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/TaskCreationServices.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/TaskCreationServices.kt @@ -31,6 +31,7 @@ import org.eclipse.tractusx.orchestrator.api.model.TaskClientStateDto import org.eclipse.tractusx.orchestrator.api.model.TaskCreateRequest import org.eclipse.tractusx.orchestrator.api.model.TaskCreateRequestEntry import org.eclipse.tractusx.orchestrator.api.model.TaskMode +import org.springframework.beans.factory.annotation.Value import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -67,7 +68,9 @@ class TaskCreationChunkService( private val businessPartnerRepository: BusinessPartnerRepository, private val orchestratorMappings: OrchestratorMappings, private val orchestrationApiClient: OrchestrationApiClient, - private val properties: GoldenRecordTaskConfigProperties + private val properties: GoldenRecordTaskConfigProperties, + @Value("\${bpdm.origin-id}") + private val originId: String ) { private val logger = KotlinLogging.logger { } @@ -96,6 +99,6 @@ class TaskCreationChunkService( if (orchestratorBusinessPartnersDto.isEmpty()) return emptyList() - return orchestrationApiClient.goldenRecordTasks.createTasks(TaskCreateRequest(mode, orchestratorBusinessPartnersDto)).createdTasks + return orchestrationApiClient.goldenRecordTasks.createTasks(TaskCreateRequest(mode, orchestratorBusinessPartnersDto, originId)).createdTasks } } \ No newline at end of file diff --git a/bpdm-gate/src/main/resources/application.yml b/bpdm-gate/src/main/resources/application.yml index 5f2211ec6..6ead08997 100644 --- a/bpdm-gate/src/main/resources/application.yml +++ b/bpdm-gate/src/main/resources/application.yml @@ -150,6 +150,7 @@ bpdm: host: localhost # The database schema to use for this application schema: bpdmgate + origin-id: default-origin # # From here on are framework and dependency configuration # More information about those properties can be taken from the respective documentation of Spring or the dependency diff --git a/bpdm-gate/src/test/resources/application-test.yml b/bpdm-gate/src/test/resources/application-test.yml index 8670819f2..780aa0d1b 100644 --- a/bpdm-gate/src/test/resources/application-test.yml +++ b/bpdm-gate/src/test/resources/application-test.yml @@ -18,6 +18,7 @@ ################################################################################ bpdm: + origin-id: test-origin bpn: owner-bpn-l: BPNL00000003CRHK tasks: diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/OriginatorRegistrarApi.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/OriginatorRegistrarApi.kt new file mode 100644 index 000000000..19a004085 --- /dev/null +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/OriginatorRegistrarApi.kt @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.api + +import io.swagger.v3.oas.annotations.Operation +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 io.swagger.v3.oas.annotations.tags.Tag +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginRequest +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginResponse +import org.springframework.http.MediaType +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.PutMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping + +const val TagOrigin = "Origin Registrar" + +@RequestMapping(OriginatorRegistrarApi.PRIORITY_INDICATOR_PATH, produces = [MediaType.APPLICATION_JSON_VALUE]) +interface OriginatorRegistrarApi { + companion object{ + const val PRIORITY_INDICATOR_PATH = "${ApiCommons.BASE_PATH}/register/origin" + } + + @Operation( + summary = "Register Gate components along with their priority levels", + description = "This endpoint allows you to register Gate components, specifying their priority and threshold values." + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "Return the registered gate information." + ), + ApiResponse(responseCode = "400", description = "On malformed requests", content = [Content()]), + ] + ) + @Tag(name = TagOrigin) + @PostMapping + fun registerOrigin(@RequestBody request: UpsertOriginRequest): UpsertOriginResponse + + @Operation( + summary = "Retrieve registered Gate components using the originId", + description = "This endpoint enables fetching details of registered Gate components." + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "Returns the details of the registered Gate components." + ), + ApiResponse(responseCode = "404", description = "Not found") + ] + ) + @Tag(name = TagOrigin) + @GetMapping("/{originId}") + fun fetchOrigin(@PathVariable("originId") originId: String): UpsertOriginResponse + + @Operation( + summary = "Retrieve registered Gate components using the originId", + description = "This endpoint enables fetching details of registered Gate components." + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "Returns the details of the registered Gate components." + ), + ] + ) + @Tag(name = TagOrigin) + @PutMapping("/{originId}") + fun updateOrigin(@PathVariable("originId") originId: String, + @RequestBody request: UpsertOriginRequest): UpsertOriginResponse +} \ No newline at end of file diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClient.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClient.kt index 1e55d9995..dc8a40414 100644 --- a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClient.kt +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClient.kt @@ -24,4 +24,6 @@ interface OrchestrationApiClient { val goldenRecordTasks: GoldenRecordTaskApiClient val finishedTaskEvents: FinishedTaskEventApiClient + + val originRegistrar: OriginatorRegistrarApiClient } diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClientImpl.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClientImpl.kt index aabd9ef5e..df107db11 100644 --- a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClientImpl.kt +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OrchestrationApiClientImpl.kt @@ -45,6 +45,8 @@ class OrchestrationApiClientImpl( override val finishedTaskEvents by lazy { createClient() } + override val originRegistrar by lazy { createClient() } + private inline fun createClient() = httpServiceProxyFactory.createClient(T::class.java) } diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OriginatorRegistrarApiClient.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OriginatorRegistrarApiClient.kt new file mode 100644 index 000000000..309f790c1 --- /dev/null +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/client/OriginatorRegistrarApiClient.kt @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.api.client + +import org.eclipse.tractusx.orchestrator.api.OriginatorRegistrarApi +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginRequest +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginResponse +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.service.annotation.GetExchange +import org.springframework.web.service.annotation.HttpExchange +import org.springframework.web.service.annotation.PostExchange +import org.springframework.web.service.annotation.PutExchange + +@HttpExchange(OriginatorRegistrarApi.PRIORITY_INDICATOR_PATH) +interface OriginatorRegistrarApiClient: OriginatorRegistrarApi{ + + @PostExchange + override fun registerOrigin(@RequestBody request: UpsertOriginRequest): UpsertOriginResponse + + @GetExchange("/{originId}") + override fun fetchOrigin(@PathVariable("originId") originId: String): UpsertOriginResponse + + @PutExchange("/{originId}") + override fun updateOrigin(@PathVariable("originId") originId: String, + @RequestBody request: UpsertOriginRequest): UpsertOriginResponse +} \ No newline at end of file diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/PriorityEnum.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/PriorityEnum.kt new file mode 100644 index 000000000..8421f1969 --- /dev/null +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/PriorityEnum.kt @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.api.model + +enum class PriorityEnum{ + High, + Medium, + Low +} \ No newline at end of file diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskCreateRequest.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskCreateRequest.kt index 18570cc3c..906718934 100644 --- a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskCreateRequest.kt +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskCreateRequest.kt @@ -29,5 +29,8 @@ data class TaskCreateRequest( val mode: TaskMode, @get:ArraySchema(arraySchema = Schema(description = "The list of tasks to create")) - val requests: List + val requests: List, + + @get:Schema(required = true, description = "Indicates the originator of the record") + val originId: String, ) diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskStepReservationEntryDto.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskStepReservationEntryDto.kt index 491d4ad5e..6c8babd81 100644 --- a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskStepReservationEntryDto.kt +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/TaskStepReservationEntryDto.kt @@ -32,7 +32,10 @@ data class TaskStepReservationEntryDto( val recordId: String, @get:Schema(description = "The business partner data to process") - val businessPartner: BusinessPartner + val businessPartner: BusinessPartner, + + @get:Schema(description = "The priority for the record") + val priority: PriorityEnum ) : RequestWithKey { override fun getRequestKey(): String { return taskId diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/UpsertOriginRequest.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/UpsertOriginRequest.kt new file mode 100644 index 000000000..a1d585ced --- /dev/null +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/UpsertOriginRequest.kt @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.api.model + +import io.swagger.v3.oas.annotations.media.Schema + +@Schema(description = "Request object to register priority for the gates.") +data class UpsertOriginRequest( + @get:Schema(required = true, description = "Indicates the threshold for the gate records") + val threshold: Long, + + @get: Schema(required = true, description = "Indicates the name of the originator") + val name: String, + + @get: Schema(required = true, description = "Indicates the priority level for the registered origin.") + val priority: PriorityEnum +) \ No newline at end of file diff --git a/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/UpsertOriginResponse.kt b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/UpsertOriginResponse.kt new file mode 100644 index 000000000..fc6c7d095 --- /dev/null +++ b/bpdm-orchestrator-api/src/main/kotlin/org/eclipse/tractusx/orchestrator/api/model/UpsertOriginResponse.kt @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.api.model + +import io.swagger.v3.oas.annotations.media.Schema + +@Schema(description = "Response object for register priority of the gates.") +data class UpsertOriginResponse( + val originId: String, + val name: String, + val priority: PriorityEnum, + val threshold: Long, +){ +} \ No newline at end of file diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/OriginRegistrarController.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/OriginRegistrarController.kt new file mode 100644 index 000000000..803c7a912 --- /dev/null +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/OriginRegistrarController.kt @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.controller + +import org.eclipse.tractusx.bpdm.orchestrator.config.PermissionConfigProperties +import org.eclipse.tractusx.bpdm.orchestrator.service.OriginRegistrarService +import org.eclipse.tractusx.orchestrator.api.OriginatorRegistrarApi +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginRequest +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginResponse +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.RestController + +@RestController +class OriginRegistrarController( + private val originRegistrarService: OriginRegistrarService +): OriginatorRegistrarApi { + + @PreAuthorize("hasAuthority(${PermissionConfigProperties.CREATE_TASK})") + override fun registerOrigin(request: UpsertOriginRequest): UpsertOriginResponse { + return originRegistrarService.registerOrigin(request) + } + + @PreAuthorize("hasAuthority(${PermissionConfigProperties.VIEW_TASK})") + override fun fetchOrigin(originId: String): UpsertOriginResponse { + return originRegistrarService.fetchOrigin(originId) + } + + @PreAuthorize("hasAuthority(${PermissionConfigProperties.CREATE_TASK})") + override fun updateOrigin(originId: String, request: UpsertOriginRequest): UpsertOriginResponse { + return originRegistrarService.updateOrigin(originId,request) + } +} \ No newline at end of file diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/entity/GoldenRecordTaskDb.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/entity/GoldenRecordTaskDb.kt index 64a17ec74..5d9f0fdfc 100644 --- a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/entity/GoldenRecordTaskDb.kt +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/entity/GoldenRecordTaskDb.kt @@ -30,6 +30,7 @@ import java.util.* name = "golden_record_tasks", indexes = [ Index(name = "index_tasks_uuid", columnList = "uuid"), + Index(name = "index_origin_id", columnList = "origin_id"), Index(name = "index_tasks_step_step_state", columnList = "task_step,task_step_state"), Index(name = "index_tasks_pending_timeout", columnList = "task_pending_timeout"), Index(name = "index_tasks_retention_timeout", columnList = "task_retention_timeout"), @@ -60,7 +61,14 @@ class GoldenRecordTaskDb( @Embedded val processingState: ProcessingState, @Embedded - val businessPartner: BusinessPartner + val businessPartner: BusinessPartner, + + @Column(name = "origin_id") + val originId: String, + + @Column(name = "priority") + @Enumerated(EnumType.ORDINAL) + var priority: PriorityEnum ) { fun updateBusinessPartner(newBusinessPartnerData: BusinessPartner){ with(newBusinessPartnerData){ diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/entity/OriginRegistrarDb.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/entity/OriginRegistrarDb.kt new file mode 100644 index 000000000..cd78c2d27 --- /dev/null +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/entity/OriginRegistrarDb.kt @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.entity + +import jakarta.persistence.* +import jakarta.validation.constraints.NotNull +import org.eclipse.tractusx.orchestrator.api.model.PriorityEnum +import org.hibernate.annotations.CreationTimestamp +import org.hibernate.annotations.UpdateTimestamp +import java.time.Instant + +@Entity +@Table( + name = "origin_register", + indexes = [ + Index(name = "index_priority_indicator_origin_id", columnList = "origin_id") + ] +) +class OriginRegistrarDb ( + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "bpdm_sequence") + @SequenceGenerator(name = "bpdm_sequence", sequenceName = "bpdm_sequence", allocationSize = 1) + @Column(name = "id", nullable = false, updatable = false, insertable = false) + val id: Long = 0, + + @Column(name = "name") + var name: String, + + @Column(updatable = false, unique = true, nullable = false, name = "origin_id") + @NotNull + var originId: String, + + @Column(nullable = false, name = "threshold") + @NotNull + var threshold: Long, + + @Column(nullable = false, name = "priority") + @Enumerated(EnumType.ORDINAL) + var priority: PriorityEnum = PriorityEnum.Low, + + @Column(updatable = false, nullable = false, name = "CREATED_AT") + @CreationTimestamp + var createdAt: Instant = Instant.now(), + + @Column(nullable = false, name = "UPDATED_AT") + @UpdateTimestamp + var updatedAt: Instant = Instant.now(), +) \ No newline at end of file diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/repository/GoldenRecordTaskRepository.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/repository/GoldenRecordTaskRepository.kt index d712b7e24..5acbeecfc 100644 --- a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/repository/GoldenRecordTaskRepository.kt +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/repository/GoldenRecordTaskRepository.kt @@ -22,6 +22,7 @@ package org.eclipse.tractusx.bpdm.orchestrator.repository import org.eclipse.tractusx.bpdm.orchestrator.entity.DbTimestamp import org.eclipse.tractusx.bpdm.orchestrator.entity.GateRecordDb import org.eclipse.tractusx.bpdm.orchestrator.entity.GoldenRecordTaskDb +import org.eclipse.tractusx.orchestrator.api.model.PriorityEnum import org.eclipse.tractusx.orchestrator.api.model.TaskStep import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable @@ -34,6 +35,8 @@ import java.util.* @Repository interface GoldenRecordTaskRepository : CrudRepository, PagingAndSortingRepository { + fun countByOriginIdAndProcessingStateResultStateAndPriorityAndCreatedAtAfter(originId: String, resultState: GoldenRecordTaskDb.ResultState, priority: PriorityEnum, currentDate: DbTimestamp): Long + fun findByUuidIn(uuids: Set): Set @Query("SELECT DISTINCT task FROM GoldenRecordTaskDb task LEFT JOIN FETCH task.gateRecord WHERE task IN :tasks") @@ -60,7 +63,7 @@ interface GoldenRecordTaskRepository : CrudRepository, @Query("SELECT DISTINCT task FROM GoldenRecordTaskDb task LEFT JOIN FETCH task.businessPartner.bpnReferences WHERE task IN :tasks") fun fetchBpnReferences(tasks: Set): Set - @Query("SELECT task from GoldenRecordTaskDb task WHERE task.processingState.step = :step AND task.processingState.stepState = :stepState") + @Query("SELECT task from GoldenRecordTaskDb task WHERE task.processingState.step = :step AND task.processingState.stepState = :stepState ORDER BY task.priority ASC") fun findByStepAndStepState(step: TaskStep, stepState: GoldenRecordTaskDb.StepState, pageable: Pageable): Page fun findByProcessingStatePendingTimeoutBefore(time: DbTimestamp, pageable: Pageable): Page diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/repository/OriginRegistrarRepository.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/repository/OriginRegistrarRepository.kt new file mode 100644 index 000000000..9108d4418 --- /dev/null +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/repository/OriginRegistrarRepository.kt @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.repository + +import org.eclipse.tractusx.bpdm.orchestrator.entity.OriginRegistrarDb +import org.springframework.data.repository.CrudRepository + +interface OriginRegistrarRepository: CrudRepository { + + fun findByOriginId(originId: String): OriginRegistrarDb? +} \ No newline at end of file diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskService.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskService.kt index c1a693924..98f6399fe 100644 --- a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskService.kt +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskService.kt @@ -57,7 +57,7 @@ class GoldenRecordTaskService( abortOutdatedTasks(gateRecords.toSet()) return createRequest.requests.zip(gateRecords) - .map { (request, record) -> goldenRecordTaskStateMachine.initTask(createRequest.mode, request.businessPartner, record) } + .map { (request, record) -> goldenRecordTaskStateMachine.initTask(createRequest.mode, request.businessPartner, record, createRequest.originId) } .map { task -> responseMapper.toClientState(task, calculateTaskRetentionTimeout(task)) } .let { TaskCreateResponse(createdTasks = it) } } @@ -102,7 +102,8 @@ class GoldenRecordTaskService( TaskStepReservationEntryDto( task.uuid.toString(), task.gateRecord.publicId.toString(), - responseMapper.toBusinessPartnerResult(task.businessPartner) + responseMapper.toBusinessPartnerResult(task.businessPartner), + task.priority ) } .let { reservations -> TaskStepReservationResponse(reservations, pendingTimeout) } diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachine.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachine.kt index 7f3cce67c..13c2a62e9 100644 --- a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachine.kt +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachine.kt @@ -34,13 +34,14 @@ import java.time.Instant class GoldenRecordTaskStateMachine( private val taskConfigProperties: TaskConfigProperties, private val taskRepository: GoldenRecordTaskRepository, + private val originRegistrarService: OriginRegistrarService, private val requestMapper: RequestMapper, private val stateMachineConfigProperties: StateMachineConfigProperties ) { private val logger = KotlinLogging.logger { } - fun initTask(mode: TaskMode, initBusinessPartner: BusinessPartner, record: GateRecordDb): GoldenRecordTaskDb { + fun initTask(mode: TaskMode, initBusinessPartner: BusinessPartner, record: GateRecordDb, originId: String): GoldenRecordTaskDb { logger.debug { "Executing initProcessingState() with parameters mode: $mode and business partner data: $initBusinessPartner" } val initialStep = getInitialStep(mode) @@ -60,10 +61,11 @@ class GoldenRecordTaskStateMachine( processingState = initProcessingState, businessPartner = requestMapper.toBusinessPartner(initBusinessPartner), createdAt = nowTime, - updatedAt = nowTime + updatedAt = nowTime, + originId = originId, + priority = originRegistrarService.getPriority(originId) ) } - return taskRepository.save(initialTask) } diff --git a/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/OriginRegistrarService.kt b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/OriginRegistrarService.kt new file mode 100644 index 000000000..7c134c844 --- /dev/null +++ b/bpdm-orchestrator/src/main/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/OriginRegistrarService.kt @@ -0,0 +1,89 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.service + +import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException +import org.eclipse.tractusx.bpdm.orchestrator.entity.DbTimestamp +import org.eclipse.tractusx.bpdm.orchestrator.entity.GoldenRecordTaskDb +import org.eclipse.tractusx.bpdm.orchestrator.entity.OriginRegistrarDb +import org.eclipse.tractusx.bpdm.orchestrator.repository.GoldenRecordTaskRepository +import org.eclipse.tractusx.bpdm.orchestrator.repository.OriginRegistrarRepository +import org.eclipse.tractusx.orchestrator.api.model.PriorityEnum +import org.eclipse.tractusx.orchestrator.api.model.TaskStep +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginRequest +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginResponse +import org.springframework.stereotype.Service +import java.time.LocalDate +import java.time.ZoneId +import java.util.UUID + +@Service +class OriginRegistrarService( + private val originRegistrarRepository: OriginRegistrarRepository, + private val taskRepository: GoldenRecordTaskRepository, +) { + + fun getPriority(originId:String):PriorityEnum{ + return if (originId != null) { + originRegistrarRepository.findByOriginId(originId)?.let { priorityIndicator -> + taskRepository.countByOriginIdAndProcessingStateResultStateAndPriorityAndCreatedAtAfter(originId, GoldenRecordTaskDb.ResultState.Pending, PriorityEnum.High, DbTimestamp(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant())).takeIf { it >= priorityIndicator.threshold } + ?.let { PriorityEnum.Low } + ?: priorityIndicator.priority + } ?: PriorityEnum.Low + } else PriorityEnum.Low + } + + fun registerOrigin(request: UpsertOriginRequest): UpsertOriginResponse { + var originDb = originRegistrarRepository.save( + OriginRegistrarDb( + originId = UUID.randomUUID().toString(), + name = request.name, + threshold = request.threshold, + priority = request.priority + ) + ) + return UpsertOriginResponse(originId = originDb.originId, name = originDb.name, + priority = originDb.priority, threshold = originDb.threshold) + } + + fun fetchOrigin(originId: String): UpsertOriginResponse { + val originDb = originRegistrarRepository.findByOriginId(originId) + return if (originDb == null){ + throw BpdmNotFoundException("Origin Value", originId) + }else { + UpsertOriginResponse( + originId = originDb.originId, name = originDb.name, + priority = originDb.priority, threshold = originDb.threshold + ) + } + } + + fun updateOrigin(originId: String, request: UpsertOriginRequest): UpsertOriginResponse { + var originDb = originRegistrarRepository.findByOriginId(originId) ?: throw BpdmNotFoundException("Origin Value", originId) + originDb.name = request.name + originDb.priority = request.priority + originDb.threshold = request.threshold + originDb = originRegistrarRepository.save(originDb) + return UpsertOriginResponse( + originId = originDb.originId, name = originDb.name, + priority = originDb.priority, threshold = originDb.threshold + ) + } +} \ No newline at end of file diff --git a/bpdm-orchestrator/src/main/resources/db/migration/V6_3_0_1__add_priority_indicator_tables.sql b/bpdm-orchestrator/src/main/resources/db/migration/V6_3_0_1__add_priority_indicator_tables.sql new file mode 100644 index 000000000..b4686b68a --- /dev/null +++ b/bpdm-orchestrator/src/main/resources/db/migration/V6_3_0_1__add_priority_indicator_tables.sql @@ -0,0 +1,17 @@ +CREATE TABLE origin_register ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY, + name VARCHAR(255) NOT NULL, + origin_id VARCHAR(50) NOT NULL, + priority INT NOT NULL DEFAULT '2', + threshold BIGINT NOT NULL, + CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (id), + CONSTRAINT index_priority_indicator_origin_id UNIQUE (origin_id) +); + +ALTER TABLE golden_record_tasks ADD COLUMN origin_id VARCHAR(50) DEFAULT NULL; + +CREATE INDEX index_origin_id ON golden_record_tasks (origin_id); + +ALTER TABLE golden_record_tasks ADD COLUMN priority INT NOT NULL DEFAULT '2'; diff --git a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/auth/AuthTestBase.kt b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/auth/AuthTestBase.kt index 03053e8d7..6c07cf15a 100644 --- a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/auth/AuthTestBase.kt +++ b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/auth/AuthTestBase.kt @@ -31,9 +31,11 @@ abstract class AuthTestBase( private val authAssertions: AuthAssertionHelper, private val orchAuthExpectations: OrchestratorAuthExpectations ) { + + private val originId = "test-origin" @Test fun `POST Golden Record Task`() { - val payload = TaskCreateRequest(TaskMode.entries.first(), listOf()) + val payload = TaskCreateRequest(TaskMode.entries.first(), listOf(), originId) authAssertions.assert(orchAuthExpectations.tasks.postTask) { orchestratorClient.goldenRecordTasks.createTasks(payload) } } diff --git a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskControllerIT.kt b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskControllerIT.kt index f3a9c3b3c..23de2e3cb 100644 --- a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskControllerIT.kt +++ b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskControllerIT.kt @@ -24,11 +24,15 @@ import org.assertj.core.api.ThrowableAssert import org.assertj.core.data.TemporalUnitOffset import org.eclipse.tractusx.bpdm.orchestrator.config.StateMachineConfigProperties import org.eclipse.tractusx.bpdm.orchestrator.config.TaskConfigProperties +import org.eclipse.tractusx.bpdm.orchestrator.entity.OriginRegistrarDb +import org.eclipse.tractusx.bpdm.orchestrator.repository.GoldenRecordTaskRepository +import org.eclipse.tractusx.bpdm.orchestrator.repository.OriginRegistrarRepository import org.eclipse.tractusx.bpdm.test.containers.PostgreSQLContextInitializer import org.eclipse.tractusx.bpdm.test.testdata.orchestrator.BusinessPartnerTestDataFactory import org.eclipse.tractusx.bpdm.test.util.DbTestHelpers import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient import org.eclipse.tractusx.orchestrator.api.model.* +import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.params.ParameterizedTest @@ -41,6 +45,7 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti import java.time.Instant import java.time.temporal.ChronoUnit import java.util.* +import java.util.stream.Collectors val WITHIN_ALLOWED_TIME_OFFSET: TemporalUnitOffset = within(1, ChronoUnit.SECONDS) @@ -59,16 +64,21 @@ class GoldenRecordTaskControllerIT @Autowired constructor( private val orchestratorClient: OrchestrationApiClient, private val taskConfigProperties: TaskConfigProperties, private val dbTestHelpers: DbTestHelpers, - private val stateMachineConfigProperties: StateMachineConfigProperties + private val stateMachineConfigProperties: StateMachineConfigProperties, + private val originRegistrarRepository: OriginRegistrarRepository, + private val goldenRecordTaskRepository: GoldenRecordTaskRepository ) { private val testDataFactory = BusinessPartnerTestDataFactory() private val defaultBusinessPartner1 = testDataFactory.createFullBusinessPartner("BP1") private val defaultBusinessPartner2 = testDataFactory.createFullBusinessPartner("BP2") + private val originId = "test-origin" @BeforeEach fun cleanUp() { dbTestHelpers.truncateDbTables() + originRegistrarRepository.deleteAll() + originRegistrarRepository.save(OriginRegistrarDb(originId = originId, name = "test", priority = PriorityEnum.High, threshold = 1)) } /** @@ -511,9 +521,13 @@ class GoldenRecordTaskControllerIT @Autowired constructor( requestsWithRecords.forEach { assertThat(it.recordId).isNotNull() } - val tasksWithRecords = orchestratorClient.goldenRecordTasks.createTasks(TaskCreateRequest(taskMode, requestsWithRecords)).createdTasks + val tasksWithRecords = orchestratorClient.goldenRecordTasks.createTasks(TaskCreateRequest(taskMode, requestsWithRecords, originId)).createdTasks tasksWithRecords.zip(existingRecordIds).forEach { (actualTask, expectedRecordId) -> assertThat(actualTask.recordId).isEqualTo(expectedRecordId) } + + var goldenRecordTaskDb = goldenRecordTaskRepository.findByUuidIn(tasksWithRecords.filter { it.processingState.resultState==ResultState.Pending }.map { UUID.fromString(it.taskId) }.toSet()) + + Assertions.assertSame(1, goldenRecordTaskDb.filter { it.priority == PriorityEnum.High }.count()) } @ParameterizedTest @@ -622,7 +636,7 @@ class GoldenRecordTaskControllerIT @Autowired constructor( entries: List? = null ): TaskCreateResponse{ val resolvedEntries = entries ?: listOf(defaultBusinessPartner1, defaultBusinessPartner2).map { bp -> TaskCreateRequestEntry(null, bp) } - return orchestratorClient.goldenRecordTasks.createTasks(TaskCreateRequest(mode = mode, requests = resolvedEntries)) + return orchestratorClient.goldenRecordTasks.createTasks(TaskCreateRequest(mode = mode, requests = resolvedEntries, originId = originId)) } private fun createTasksWithoutRecordId(mode: TaskMode, businessPartners: List? = null): TaskCreateResponse = diff --git a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskEventControllerIT.kt b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskEventControllerIT.kt index 9fdbaf1ee..349ded0d2 100644 --- a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskEventControllerIT.kt +++ b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/GoldenRecordTaskEventControllerIT.kt @@ -22,6 +22,8 @@ package org.eclipse.tractusx.bpdm.orchestrator.controller import org.assertj.core.api.Assertions.assertThat import org.eclipse.tractusx.bpdm.common.dto.PaginationRequest import org.eclipse.tractusx.bpdm.orchestrator.config.StateMachineConfigProperties +import org.eclipse.tractusx.bpdm.orchestrator.entity.OriginRegistrarDb +import org.eclipse.tractusx.bpdm.orchestrator.repository.OriginRegistrarRepository import org.eclipse.tractusx.bpdm.test.containers.PostgreSQLContextInitializer import org.eclipse.tractusx.bpdm.test.testdata.orchestrator.BusinessPartnerTestDataFactory import org.eclipse.tractusx.bpdm.test.util.DbTestHelpers @@ -46,17 +48,20 @@ import java.time.Instant class GoldenRecordTaskEventControllerIT @Autowired constructor( private val orchestratorClient: OrchestrationApiClient, private val dbTestHelpers: DbTestHelpers, - private val stateMachineConfigProperties: StateMachineConfigProperties + private val stateMachineConfigProperties: StateMachineConfigProperties, + private val originRegistrarRepository: OriginRegistrarRepository ){ private val testDataFactory = BusinessPartnerTestDataFactory() private val defaultBusinessPartner1 = testDataFactory.createFullBusinessPartner("BP1") private val defaultBusinessPartner2 = testDataFactory.createFullBusinessPartner("BP2") + private val originId = "test-origin" @BeforeEach fun cleanUp() { dbTestHelpers.truncateDbTables() + originRegistrarRepository.save(OriginRegistrarDb(originId = originId, name = "test", priority = PriorityEnum.Low, threshold = 20)) } /* @@ -83,7 +88,8 @@ class GoldenRecordTaskEventControllerIT @Autowired constructor( TaskCreateRequestEntry(null, defaultBusinessPartner1), TaskCreateRequestEntry(null, defaultBusinessPartner2) - ) + ), + originId )).createdTasks @@ -182,7 +188,7 @@ class GoldenRecordTaskEventControllerIT @Autowired constructor( fun createFinishedTasks(count: Int, taskMode: TaskMode): List{ val createdTasks = orchestratorClient.goldenRecordTasks.createTasks(TaskCreateRequest(taskMode, - (1 .. count).map { TaskCreateRequestEntry(null, testDataFactory.createFullBusinessPartner(it.toString())) }) + (1 .. count).map { TaskCreateRequestEntry(null, testDataFactory.createFullBusinessPartner(it.toString())) }, originId) ).createdTasks val allSteps = stateMachineConfigProperties.modeSteps[taskMode]!! diff --git a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/OriginRegistrarControllerIT.kt b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/OriginRegistrarControllerIT.kt new file mode 100644 index 000000000..b7426b642 --- /dev/null +++ b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/controller/OriginRegistrarControllerIT.kt @@ -0,0 +1,90 @@ +/******************************************************************************* + * Copyright (c) 2021,2024 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.orchestrator.controller + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.tractusx.bpdm.orchestrator.repository.OriginRegistrarRepository +import org.eclipse.tractusx.bpdm.test.containers.PostgreSQLContextInitializer +import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient +import org.eclipse.tractusx.orchestrator.api.model.PriorityEnum +import org.eclipse.tractusx.orchestrator.api.model.UpsertOriginRequest +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration + +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = [ + "bpdm.security.enabled=false" + ] +) +@ContextConfiguration(initializers = [PostgreSQLContextInitializer::class]) +class OriginRegistrarControllerIT @Autowired constructor( + private val orchestratorClient: OrchestrationApiClient, + private val originRegistrarRepository: OriginRegistrarRepository, +) { + + @BeforeEach + fun cleanUp() { + originRegistrarRepository.deleteAll() + } + + @Test + fun `register new gate with threshold`(){ + registerAndValidatingGateInformation() + } + + @Test + fun `fetch registered gate information`(){ + val originId = registerAndValidatingGateInformation() + var response = orchestratorClient.originRegistrar.fetchOrigin(originId) + assertThat(response).isNotNull() + assertThat(response.originId).isNotNull().isNotBlank() + Assertions.assertEquals(originId, response.originId) + } + + @Test + fun `update registered gate information`(){ + val originId = registerAndValidatingGateInformation() + val request = UpsertOriginRequest(5, "Update test", PriorityEnum.Low) + val response = orchestratorClient.originRegistrar.updateOrigin(originId, request) + assertThat(response).isNotNull() + assertThat(response.originId).isNotNull().isNotBlank() + Assertions.assertEquals(request.priority, response.priority) + Assertions.assertEquals(request.name, response.name) + Assertions.assertEquals(1L, originRegistrarRepository.count()) + } + + private fun registerAndValidatingGateInformation(): String { + Assertions.assertEquals(0L, originRegistrarRepository.count()) + val request = UpsertOriginRequest(20, "Test", PriorityEnum.High) + val response = orchestratorClient.originRegistrar.registerOrigin(request) + assertThat(response).isNotNull() + assertThat(response.originId).isNotNull().isNotBlank() + Assertions.assertEquals(request.priority, response.priority) + Assertions.assertEquals(request.name, response.name) + Assertions.assertEquals(1L, originRegistrarRepository.count()) + assertThat(originRegistrarRepository.findByOriginId(response.originId)).isNotNull + return response.originId + } +} \ No newline at end of file diff --git a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/performance/OrchestratorPerformanceIT.kt b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/performance/OrchestratorPerformanceIT.kt index 6f4b5c333..7379d46f0 100644 --- a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/performance/OrchestratorPerformanceIT.kt +++ b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/performance/OrchestratorPerformanceIT.kt @@ -49,6 +49,7 @@ class OrchestratorPerformanceIT@Autowired constructor( private val testDataFactory = BusinessPartnerTestDataFactory() private val taskMode = stateMachineConfigProperties.modeSteps.keys.first() private val step = stateMachineConfigProperties.modeSteps[taskMode]!!.first() + private val originId = "test-origin" @ParameterizedTest @ValueSource(ints = [100]) @@ -107,7 +108,7 @@ class OrchestratorPerformanceIT@Autowired constructor( private fun createNewRecordTasks(size: Int): List{ return (1 .. size) .map { TaskCreateRequestEntry(null, testDataFactory.createFullBusinessPartner("BP$it")) } - .let { TaskCreateRequest(mode = taskMode, requests = it) } + .let { TaskCreateRequest(mode = taskMode, requests = it, originId = originId) } .let { orchestratorClient.goldenRecordTasks.createTasks(it).createdTasks } .map { TaskStateRequest.Entry(it.taskId, it.recordId) } } @@ -115,7 +116,7 @@ class OrchestratorPerformanceIT@Autowired constructor( private fun updateRecords(recordIds: List): List{ return recordIds .map { TaskCreateRequestEntry(it, testDataFactory.createFullBusinessPartner(it)) } - .let { TaskCreateRequest(mode = taskMode, requests = it) } + .let { TaskCreateRequest(mode = taskMode, requests = it, originId = originId) } .let { orchestratorClient.goldenRecordTasks.createTasks(it).createdTasks } .map { TaskStateRequest.Entry(it.taskId, it.recordId) } } diff --git a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachineIT.kt b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachineIT.kt index 19b635ff8..f2e463603 100644 --- a/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachineIT.kt +++ b/bpdm-orchestrator/src/test/kotlin/org/eclipse/tractusx/bpdm/orchestrator/service/GoldenRecordTaskStateMachineIT.kt @@ -27,15 +27,14 @@ import org.eclipse.tractusx.bpdm.orchestrator.config.StateMachineConfigPropertie import org.eclipse.tractusx.bpdm.orchestrator.config.TaskConfigProperties import org.eclipse.tractusx.bpdm.orchestrator.entity.GateRecordDb import org.eclipse.tractusx.bpdm.orchestrator.entity.GoldenRecordTaskDb +import org.eclipse.tractusx.bpdm.orchestrator.entity.OriginRegistrarDb import org.eclipse.tractusx.bpdm.orchestrator.exception.BpdmIllegalStateException import org.eclipse.tractusx.bpdm.orchestrator.repository.GateRecordRepository +import org.eclipse.tractusx.bpdm.orchestrator.repository.OriginRegistrarRepository import org.eclipse.tractusx.bpdm.test.containers.PostgreSQLContextInitializer import org.eclipse.tractusx.bpdm.test.testdata.orchestrator.BusinessPartnerTestDataFactory import org.eclipse.tractusx.bpdm.test.util.DbTestHelpers -import org.eclipse.tractusx.orchestrator.api.model.TaskErrorDto -import org.eclipse.tractusx.orchestrator.api.model.TaskErrorType -import org.eclipse.tractusx.orchestrator.api.model.TaskMode -import org.eclipse.tractusx.orchestrator.api.model.TaskStep +import org.eclipse.tractusx.orchestrator.api.model.* import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.EnumSource @@ -63,7 +62,8 @@ class GoldenRecordTaskStateMachineIT @Autowired constructor( private val taskConfigProperties: TaskConfigProperties, private val gateRecordRepository: GateRecordRepository, private val dbTestHelpers: DbTestHelpers, - private val stateMachineConfigProperties: StateMachineConfigProperties + private val stateMachineConfigProperties: StateMachineConfigProperties, + private val originRegistrarRepository: OriginRegistrarRepository ) { private val testDataFactory = BusinessPartnerTestDataFactory() @@ -72,10 +72,13 @@ class GoldenRecordTaskStateMachineIT @Autowired constructor( private lateinit var gateRecord: GateRecordDb + private val originId = "test-origin" + @BeforeEach fun cleanUp() { dbTestHelpers.truncateDbTables() gateRecord = gateRecordRepository.save(GateRecordDb(publicId = UUID.randomUUID(), privateId = UUID.randomUUID())) + originRegistrarRepository.save(OriginRegistrarDb(originId = originId, name = "test", priority = PriorityEnum.Low, threshold = 20)) } @@ -88,7 +91,7 @@ class GoldenRecordTaskStateMachineIT @Autowired constructor( @Transactional fun `initial state`(taskMode: TaskMode) { val now = Instant.now() - val task = goldenRecordTaskStateMachine.initTask(taskMode, businessPartnerFull, gateRecord) + val task = goldenRecordTaskStateMachine.initTask(taskMode, businessPartnerFull, gateRecord, originId) val expectedStep = stateMachineConfigProperties.modeSteps[taskMode]!!.first() val state = task.processingState @@ -111,7 +114,7 @@ class GoldenRecordTaskStateMachineIT @Autowired constructor( @Transactional fun `walk through all UpdateFromSharingMember steps`(taskMode: TaskMode) { // new task - val task = goldenRecordTaskStateMachine.initTask(taskMode, businessPartnerFull, gateRecord) + val task = goldenRecordTaskStateMachine.initTask(taskMode, businessPartnerFull, gateRecord, originId) val allSteps = stateMachineConfigProperties.modeSteps[taskMode]!! allSteps.forEach { step -> @@ -165,7 +168,7 @@ class GoldenRecordTaskStateMachineIT @Autowired constructor( @Transactional fun `walk through steps and resolve with error`(taskMode: TaskMode) { // new task - val task = goldenRecordTaskStateMachine.initTask(taskMode, businessPartnerFull, gateRecord) + val task = goldenRecordTaskStateMachine.initTask(taskMode, businessPartnerFull, gateRecord, originId) val expectedStep = stateMachineConfigProperties.modeSteps[taskMode]!!.first() assertProcessingState(task.processingState, GoldenRecordTaskDb.ResultState.Pending, expectedStep, GoldenRecordTaskDb.StepState.Queued) // taskPendingTimeout has been set diff --git a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskResolutionServiceTest.kt b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskResolutionServiceTest.kt index f0baa3362..9516d0518 100644 --- a/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskResolutionServiceTest.kt +++ b/bpdm-pool/src/test/kotlin/org/eclipse/tractusx/bpdm/pool/service/TaskResolutionServiceTest.kt @@ -898,7 +898,8 @@ class TaskResolutionServiceTest @Autowired constructor( TaskStepReservationEntryDto( taskId = taskId, recordId = UUID.randomUUID().toString(), - businessPartner = businessPartner + businessPartner = businessPartner, + priority = PriorityEnum.High ) ) } @@ -909,7 +910,8 @@ class TaskResolutionServiceTest @Autowired constructor( TaskStepReservationEntryDto( taskId = it.legalEntity.bpnReference.referenceValue!!, recordId = UUID.randomUUID().toString(), - businessPartner = it + businessPartner = it, + priority = PriorityEnum.High ) }