From 12d7893d036b9308b38bd623f5fa0dc5547b17d8 Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Tue, 20 Feb 2024 12:20:32 +0100 Subject: [PATCH 1/4] Label REST API with `v1` version --- CHANGELOG.md | 4 + .../api/BlockchainAdapterApiClient.java | 8 +- .../command/task/TaskControllerV1.java | 105 +++++++++++++++ .../command/task/TaskControllerV1Tests.java | 120 ++++++++++++++++++ 4 files changed, 233 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java create mode 100644 src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 4033707..e9e2b33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. ## [[NEXT]](https://github.com/iExecBlockchainComputing/iexec-blockchain-adapter-api/releases/tag/vNEXT) 2024 +### New Features + +- Label REST API with `v1` version. (#132) + ### Quality - Remove `/tasks/{chainTaskId}` endpoint, the adapter must only call `initialize` and `finalize` **PoCo** methods. (#130) diff --git a/iexec-blockchain-adapter-api-library/src/main/java/com/iexec/blockchain/api/BlockchainAdapterApiClient.java b/iexec-blockchain-adapter-api-library/src/main/java/com/iexec/blockchain/api/BlockchainAdapterApiClient.java index 28fa9cd..508c9f6 100644 --- a/iexec-blockchain-adapter-api-library/src/main/java/com/iexec/blockchain/api/BlockchainAdapterApiClient.java +++ b/iexec-blockchain-adapter-api-library/src/main/java/com/iexec/blockchain/api/BlockchainAdapterApiClient.java @@ -35,17 +35,17 @@ public interface BlockchainAdapterApiClient { @RequestLine("GET /metrics") String getMetrics(); - @RequestLine("POST /tasks/initialize?chainDealId={chainDealId}&taskIndex={taskIndex}") + @RequestLine("POST /v1/tasks/initialize?chainDealId={chainDealId}&taskIndex={taskIndex}") String requestInitializeTask(@Param("chainDealId") String chainDealId, @Param("taskIndex") int taskIndex); - @RequestLine("GET /tasks/initialize/{chainTaskId}/status") + @RequestLine("GET /v1/tasks/initialize/{chainTaskId}/status") CommandStatus getStatusForInitializeTaskRequest(@Param("chainTaskId") String chainTaskId); - @RequestLine("POST /tasks/finalize/{chainTaskId}") + @RequestLine("POST /v1/tasks/finalize/{chainTaskId}") String requestFinalizeTask(@Param("chainTaskId") String chainTaskId, TaskFinalizeArgs taskFinalizeArgs); - @RequestLine("GET /tasks/finalize/{chainTaskId}/status") + @RequestLine("GET /v1/tasks/finalize/{chainTaskId}/status") CommandStatus getStatusForFinalizeTaskRequest(@Param("chainTaskId") String chainTaskId); // endregion diff --git a/src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java b/src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java new file mode 100644 index 0000000..7410c94 --- /dev/null +++ b/src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java @@ -0,0 +1,105 @@ +/* + * Copyright 2024-2024 IEXEC BLOCKCHAIN TECH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://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. + */ + +package com.iexec.blockchain.command.task; + +import com.iexec.blockchain.api.CommandStatus; +import com.iexec.blockchain.command.task.finalize.TaskFinalizeService; +import com.iexec.blockchain.command.task.initialize.TaskInitializeService; +import com.iexec.common.chain.adapter.args.TaskFinalizeArgs; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import static com.iexec.blockchain.swagger.OpenApiConfig.SWAGGER_BASIC_AUTH; + +@RestController +@RequestMapping("/v1/tasks") +public class TaskControllerV1 { + + private final TaskInitializeService taskInitializeService; + private final TaskFinalizeService taskFinalizeService; + + public TaskControllerV1(TaskInitializeService taskInitializeService, TaskFinalizeService taskFinalizeService) { + this.taskInitializeService = taskInitializeService; + this.taskFinalizeService = taskFinalizeService; + } + + /** + * Start the asynchronous `initialize task` blockchain remote call. + * + * @param chainDealId blockchain deal ID + * @param taskIndex index of the task int the bag + * @return blockchain task ID if successful + */ + @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) + @PostMapping("/initialize") + public ResponseEntity requestInitializeTask(@RequestParam String chainDealId, + @RequestParam int taskIndex) { + String chainTaskId = taskInitializeService.start(chainDealId, taskIndex); + if (!chainTaskId.isEmpty()) { + return ResponseEntity.ok(chainTaskId); + } + return ResponseEntity.badRequest().build(); + } + + /** + * Read status for the asynchronous `initialize task` blockchain remote call. + * + * @param chainTaskId blockchain ID of the task + * @return status + */ + @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) + @GetMapping("/initialize/{chainTaskId}/status") + public ResponseEntity getStatusForInitializeTaskRequest(@PathVariable String chainTaskId) { + return taskInitializeService.getStatusForCommand(chainTaskId) + .map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build()); + } + + /** + * Start the asynchronous `finalize task` blockchain remote call. + * + * @param chainTaskId blockchain task ID + * @param args input arguments for `finalize task` + * @return blockchain task ID if successful + */ + @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) + @PostMapping("/finalize/{chainTaskId}") + public ResponseEntity requestFinalizeTask(@PathVariable String chainTaskId, + @RequestBody TaskFinalizeArgs args) { + if (!taskFinalizeService.start(chainTaskId, args).isEmpty()) { + return ResponseEntity.ok(chainTaskId); + } + return ResponseEntity.badRequest().build(); + } + + /** + * Read status for the asynchronous `finalize task` blockchain remote call. + * + * @param chainTaskId blockchain ID of the task + * @return status + */ + @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) + @GetMapping("/finalize/{chainTaskId}/status") + public ResponseEntity getStatusForFinalizeTaskRequest(@PathVariable String chainTaskId) { + return taskFinalizeService.getStatusForCommand(chainTaskId) + .map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build()); + } + +} diff --git a/src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java b/src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java new file mode 100644 index 0000000..2bfc9bc --- /dev/null +++ b/src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java @@ -0,0 +1,120 @@ +/* + * Copyright 2024-2024 IEXEC BLOCKCHAIN TECH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://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. + */ + +package com.iexec.blockchain.command.task; + +import com.iexec.blockchain.api.CommandStatus; +import com.iexec.blockchain.command.task.finalize.TaskFinalizeService; +import com.iexec.blockchain.command.task.initialize.TaskInitializeService; +import com.iexec.common.chain.adapter.args.TaskFinalizeArgs; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.ResponseEntity; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +class TaskControllerV1Tests { + + private static final String CHAIN_DEAL_ID = "0x1"; + private static final int TASK_INDEX = 0; + private static final String CHAIN_TASK_ID = "0x2"; + + @Mock + private TaskInitializeService taskInitializeService; + @Mock + private TaskFinalizeService taskFinalizeService; + @InjectMocks + private TaskControllerV1 taskController; + + @BeforeEach + void init() { + MockitoAnnotations.openMocks(this); + } + + // region requestInitializeTask + @Test + void shouldNotifyInitializeCommandSubmissionFailure() { + when(taskInitializeService.start(CHAIN_DEAL_ID, TASK_INDEX)).thenReturn(""); + assertThat(taskController.requestInitializeTask(CHAIN_DEAL_ID, TASK_INDEX)) + .isEqualTo(ResponseEntity.badRequest().build()); + } + + @Test + void shouldNotifyInitializeCommandSubmissionSuccess() { + when(taskInitializeService.start(CHAIN_DEAL_ID, TASK_INDEX)).thenReturn(CHAIN_TASK_ID); + assertThat(taskController.requestInitializeTask(CHAIN_DEAL_ID, TASK_INDEX)) + .isEqualTo(ResponseEntity.ok(CHAIN_TASK_ID)); + } + // endregion + + // region getStatusForInitializeTaskRequest + @ParameterizedTest + @EnumSource(value = CommandStatus.class) + void shouldReturnInitializeCommandStatusWhenAvailable(CommandStatus status) { + when(taskInitializeService.getStatusForCommand(CHAIN_TASK_ID)).thenReturn(Optional.of(status)); + assertThat(taskController.getStatusForInitializeTaskRequest(CHAIN_TASK_ID)) + .isEqualTo(ResponseEntity.ok(status)); + } + + @Test + void shouldNotReturnInitializeCommandStatusWhenEmpty() { + when(taskInitializeService.getStatusForCommand(CHAIN_TASK_ID)).thenReturn(Optional.empty()); + assertThat(taskController.getStatusForInitializeTaskRequest(CHAIN_TASK_ID)) + .isEqualTo(ResponseEntity.notFound().build()); + } + // endregion + + // region requestFinalizeTask + @Test + void shouldNotifyFinalizeCommandSubmissionFailure() { + when(taskFinalizeService.start(CHAIN_TASK_ID, TaskFinalizeArgs.builder().build())).thenReturn(""); + assertThat(taskController.requestFinalizeTask(CHAIN_TASK_ID, TaskFinalizeArgs.builder().build())) + .isEqualTo(ResponseEntity.badRequest().build()); + } + + @Test + void shouldNotifyFinalizeCommandSubmissionSuccess() { + when(taskFinalizeService.start(CHAIN_TASK_ID, TaskFinalizeArgs.builder().build())).thenReturn(CHAIN_TASK_ID); + assertThat(taskController.requestFinalizeTask(CHAIN_TASK_ID, TaskFinalizeArgs.builder().build())) + .isEqualTo(ResponseEntity.ok(CHAIN_TASK_ID)); + } + // endregion + + // region getStatusForFinalizeTaskRequest + @ParameterizedTest + @EnumSource(value = CommandStatus.class) + void shouldReturnFinalizeCommandStatusWhenAvailable(CommandStatus status) { + when(taskFinalizeService.getStatusForCommand(CHAIN_TASK_ID)).thenReturn(Optional.of(status)); + assertThat(taskController.getStatusForFinalizeTaskRequest(CHAIN_TASK_ID)) + .isEqualTo(ResponseEntity.ok(status)); + } + + @Test + void shouldNotReturnFinalizeCommandStatusWhenEmpty() { + when(taskFinalizeService.getStatusForCommand(CHAIN_TASK_ID)).thenReturn(Optional.empty()); + assertThat(taskController.getStatusForFinalizeTaskRequest(CHAIN_TASK_ID)) + .isEqualTo(ResponseEntity.notFound().build()); + } + // endregion +} From 041d606082b4b9edaf42d5da71c93933fe6c7010 Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Tue, 20 Feb 2024 14:30:44 +0100 Subject: [PATCH 2/4] Rename controller and handle duplications and depreciation correctly --- .../command/task/TaskController.java | 23 ++++---- ...ollerV1.java => TaskControllerLegacy.java} | 53 ++++++++----------- ...rV1Tests.java => TaskControllerTests.java} | 4 +- 3 files changed, 34 insertions(+), 46 deletions(-) rename src/main/java/com/iexec/blockchain/command/task/{TaskControllerV1.java => TaskControllerLegacy.java} (61%) rename src/test/java/com/iexec/blockchain/command/task/{TaskControllerV1Tests.java => TaskControllerTests.java} (98%) diff --git a/src/main/java/com/iexec/blockchain/command/task/TaskController.java b/src/main/java/com/iexec/blockchain/command/task/TaskController.java index 23a2509..f70dc36 100644 --- a/src/main/java/com/iexec/blockchain/command/task/TaskController.java +++ b/src/main/java/com/iexec/blockchain/command/task/TaskController.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2024 IEXEC BLOCKCHAIN TECH + * Copyright 2024-2024 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ import static com.iexec.blockchain.swagger.OpenApiConfig.SWAGGER_BASIC_AUTH; @RestController -@RequestMapping("/tasks") +@RequestMapping("/v1/tasks") public class TaskController { private final TaskInitializeService taskInitializeService; @@ -48,11 +48,9 @@ public TaskController(TaskInitializeService taskInitializeService, TaskFinalizeS */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/initialize") - public ResponseEntity requestInitializeTask( - @RequestParam String chainDealId, - @RequestParam int taskIndex) { - String chainTaskId = - taskInitializeService.start(chainDealId, taskIndex); + public ResponseEntity requestInitializeTask(@RequestParam String chainDealId, + @RequestParam int taskIndex) { + String chainTaskId = taskInitializeService.start(chainDealId, taskIndex); if (!chainTaskId.isEmpty()) { return ResponseEntity.ok(chainTaskId); } @@ -67,8 +65,7 @@ public ResponseEntity requestInitializeTask( */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/initialize/{chainTaskId}/status") - public ResponseEntity getStatusForInitializeTaskRequest( - @PathVariable String chainTaskId) { + public ResponseEntity getStatusForInitializeTaskRequest(@PathVariable String chainTaskId) { return taskInitializeService.getStatusForCommand(chainTaskId) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); @@ -83,9 +80,8 @@ public ResponseEntity getStatusForInitializeTaskRequest( */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/finalize/{chainTaskId}") - public ResponseEntity requestFinalizeTask( - @PathVariable String chainTaskId, - @RequestBody TaskFinalizeArgs args) { + public ResponseEntity requestFinalizeTask(@PathVariable String chainTaskId, + @RequestBody TaskFinalizeArgs args) { if (!taskFinalizeService.start(chainTaskId, args).isEmpty()) { return ResponseEntity.ok(chainTaskId); } @@ -100,8 +96,7 @@ public ResponseEntity requestFinalizeTask( */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/finalize/{chainTaskId}/status") - public ResponseEntity getStatusForFinalizeTaskRequest( - @PathVariable String chainTaskId) { + public ResponseEntity getStatusForFinalizeTaskRequest(@PathVariable String chainTaskId) { return taskFinalizeService.getStatusForCommand(chainTaskId) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); diff --git a/src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java b/src/main/java/com/iexec/blockchain/command/task/TaskControllerLegacy.java similarity index 61% rename from src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java rename to src/main/java/com/iexec/blockchain/command/task/TaskControllerLegacy.java index 7410c94..2cf8444 100644 --- a/src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java +++ b/src/main/java/com/iexec/blockchain/command/task/TaskControllerLegacy.java @@ -1,5 +1,5 @@ /* - * Copyright 2024-2024 IEXEC BLOCKCHAIN TECH + * Copyright 2021-2024 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,16 +27,16 @@ import static com.iexec.blockchain.swagger.OpenApiConfig.SWAGGER_BASIC_AUTH; +/** + * Call /v1/tasks endpoints in {@code TaskControllerV1} + */ +@Deprecated(forRemoval = true) @RestController -@RequestMapping("/v1/tasks") -public class TaskControllerV1 { - - private final TaskInitializeService taskInitializeService; - private final TaskFinalizeService taskFinalizeService; +@RequestMapping("/tasks") +public class TaskControllerLegacy extends TaskController { - public TaskControllerV1(TaskInitializeService taskInitializeService, TaskFinalizeService taskFinalizeService) { - this.taskInitializeService = taskInitializeService; - this.taskFinalizeService = taskFinalizeService; + public TaskControllerLegacy(TaskInitializeService taskInitializeService, TaskFinalizeService taskFinalizeService) { + super(taskInitializeService, taskFinalizeService); } /** @@ -48,13 +48,10 @@ public TaskControllerV1(TaskInitializeService taskInitializeService, TaskFinaliz */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/initialize") - public ResponseEntity requestInitializeTask(@RequestParam String chainDealId, - @RequestParam int taskIndex) { - String chainTaskId = taskInitializeService.start(chainDealId, taskIndex); - if (!chainTaskId.isEmpty()) { - return ResponseEntity.ok(chainTaskId); - } - return ResponseEntity.badRequest().build(); + public ResponseEntity requestInitializeTask( + @RequestParam String chainDealId, + @RequestParam int taskIndex) { + return super.requestInitializeTask(chainDealId, taskIndex); } /** @@ -65,10 +62,9 @@ public ResponseEntity requestInitializeTask(@RequestParam String chainDe */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/initialize/{chainTaskId}/status") - public ResponseEntity getStatusForInitializeTaskRequest(@PathVariable String chainTaskId) { - return taskInitializeService.getStatusForCommand(chainTaskId) - .map(ResponseEntity::ok) - .orElse(ResponseEntity.notFound().build()); + public ResponseEntity getStatusForInitializeTaskRequest( + @PathVariable String chainTaskId) { + return super.getStatusForInitializeTaskRequest(chainTaskId); } /** @@ -80,12 +76,10 @@ public ResponseEntity getStatusForInitializeTaskRequest(@PathVari */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/finalize/{chainTaskId}") - public ResponseEntity requestFinalizeTask(@PathVariable String chainTaskId, - @RequestBody TaskFinalizeArgs args) { - if (!taskFinalizeService.start(chainTaskId, args).isEmpty()) { - return ResponseEntity.ok(chainTaskId); - } - return ResponseEntity.badRequest().build(); + public ResponseEntity requestFinalizeTask( + @PathVariable String chainTaskId, + @RequestBody TaskFinalizeArgs args) { + return super.requestFinalizeTask(chainTaskId, args); } /** @@ -96,10 +90,9 @@ public ResponseEntity requestFinalizeTask(@PathVariable String chainTask */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/finalize/{chainTaskId}/status") - public ResponseEntity getStatusForFinalizeTaskRequest(@PathVariable String chainTaskId) { - return taskFinalizeService.getStatusForCommand(chainTaskId) - .map(ResponseEntity::ok) - .orElse(ResponseEntity.notFound().build()); + public ResponseEntity getStatusForFinalizeTaskRequest( + @PathVariable String chainTaskId) { + return super.getStatusForFinalizeTaskRequest(chainTaskId); } } diff --git a/src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java b/src/test/java/com/iexec/blockchain/command/task/TaskControllerTests.java similarity index 98% rename from src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java rename to src/test/java/com/iexec/blockchain/command/task/TaskControllerTests.java index 2bfc9bc..aa367fb 100644 --- a/src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java +++ b/src/test/java/com/iexec/blockchain/command/task/TaskControllerTests.java @@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; -class TaskControllerV1Tests { +class TaskControllerTests { private static final String CHAIN_DEAL_ID = "0x1"; private static final int TASK_INDEX = 0; @@ -45,7 +45,7 @@ class TaskControllerV1Tests { @Mock private TaskFinalizeService taskFinalizeService; @InjectMocks - private TaskControllerV1 taskController; + private TaskController taskController; @BeforeEach void init() { From 20838b3263c9664914874592b0e204fbd5b19a31 Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Tue, 20 Feb 2024 15:28:16 +0100 Subject: [PATCH 3/4] Rename controllers to pass quality --- .../command/task/TaskController.java | 51 ++++++++---------- ...ollerLegacy.java => TaskControllerV1.java} | 53 +++++++++++-------- ...rTests.java => TaskControllerV1Tests.java} | 4 +- 3 files changed, 54 insertions(+), 54 deletions(-) rename src/main/java/com/iexec/blockchain/command/task/{TaskControllerLegacy.java => TaskControllerV1.java} (61%) rename src/test/java/com/iexec/blockchain/command/task/{TaskControllerTests.java => TaskControllerV1Tests.java} (98%) diff --git a/src/main/java/com/iexec/blockchain/command/task/TaskController.java b/src/main/java/com/iexec/blockchain/command/task/TaskController.java index f70dc36..1d20035 100644 --- a/src/main/java/com/iexec/blockchain/command/task/TaskController.java +++ b/src/main/java/com/iexec/blockchain/command/task/TaskController.java @@ -1,5 +1,5 @@ /* - * Copyright 2024-2024 IEXEC BLOCKCHAIN TECH + * Copyright 2021-2024 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,16 +27,16 @@ import static com.iexec.blockchain.swagger.OpenApiConfig.SWAGGER_BASIC_AUTH; +/** + * Call /v1/tasks endpoints in {@code TaskControllerV1} + */ +@Deprecated(forRemoval = true) @RestController -@RequestMapping("/v1/tasks") -public class TaskController { - - private final TaskInitializeService taskInitializeService; - private final TaskFinalizeService taskFinalizeService; +@RequestMapping("/tasks") +public class TaskController extends TaskControllerV1 { public TaskController(TaskInitializeService taskInitializeService, TaskFinalizeService taskFinalizeService) { - this.taskInitializeService = taskInitializeService; - this.taskFinalizeService = taskFinalizeService; + super(taskInitializeService, taskFinalizeService); } /** @@ -48,13 +48,10 @@ public TaskController(TaskInitializeService taskInitializeService, TaskFinalizeS */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/initialize") - public ResponseEntity requestInitializeTask(@RequestParam String chainDealId, - @RequestParam int taskIndex) { - String chainTaskId = taskInitializeService.start(chainDealId, taskIndex); - if (!chainTaskId.isEmpty()) { - return ResponseEntity.ok(chainTaskId); - } - return ResponseEntity.badRequest().build(); + public ResponseEntity requestInitializeTask( + @RequestParam String chainDealId, + @RequestParam int taskIndex) { + return super.requestInitializeTask(chainDealId, taskIndex); } /** @@ -65,10 +62,9 @@ public ResponseEntity requestInitializeTask(@RequestParam String chainDe */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/initialize/{chainTaskId}/status") - public ResponseEntity getStatusForInitializeTaskRequest(@PathVariable String chainTaskId) { - return taskInitializeService.getStatusForCommand(chainTaskId) - .map(ResponseEntity::ok) - .orElse(ResponseEntity.notFound().build()); + public ResponseEntity getStatusForInitializeTaskRequest( + @PathVariable String chainTaskId) { + return super.getStatusForInitializeTaskRequest(chainTaskId); } /** @@ -80,12 +76,10 @@ public ResponseEntity getStatusForInitializeTaskRequest(@PathVari */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/finalize/{chainTaskId}") - public ResponseEntity requestFinalizeTask(@PathVariable String chainTaskId, - @RequestBody TaskFinalizeArgs args) { - if (!taskFinalizeService.start(chainTaskId, args).isEmpty()) { - return ResponseEntity.ok(chainTaskId); - } - return ResponseEntity.badRequest().build(); + public ResponseEntity requestFinalizeTask( + @PathVariable String chainTaskId, + @RequestBody TaskFinalizeArgs args) { + return super.requestFinalizeTask(chainTaskId, args); } /** @@ -96,10 +90,9 @@ public ResponseEntity requestFinalizeTask(@PathVariable String chainTask */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/finalize/{chainTaskId}/status") - public ResponseEntity getStatusForFinalizeTaskRequest(@PathVariable String chainTaskId) { - return taskFinalizeService.getStatusForCommand(chainTaskId) - .map(ResponseEntity::ok) - .orElse(ResponseEntity.notFound().build()); + public ResponseEntity getStatusForFinalizeTaskRequest( + @PathVariable String chainTaskId) { + return super.getStatusForFinalizeTaskRequest(chainTaskId); } } diff --git a/src/main/java/com/iexec/blockchain/command/task/TaskControllerLegacy.java b/src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java similarity index 61% rename from src/main/java/com/iexec/blockchain/command/task/TaskControllerLegacy.java rename to src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java index 2cf8444..7410c94 100644 --- a/src/main/java/com/iexec/blockchain/command/task/TaskControllerLegacy.java +++ b/src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2024 IEXEC BLOCKCHAIN TECH + * Copyright 2024-2024 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,16 +27,16 @@ import static com.iexec.blockchain.swagger.OpenApiConfig.SWAGGER_BASIC_AUTH; -/** - * Call /v1/tasks endpoints in {@code TaskControllerV1} - */ -@Deprecated(forRemoval = true) @RestController -@RequestMapping("/tasks") -public class TaskControllerLegacy extends TaskController { +@RequestMapping("/v1/tasks") +public class TaskControllerV1 { + + private final TaskInitializeService taskInitializeService; + private final TaskFinalizeService taskFinalizeService; - public TaskControllerLegacy(TaskInitializeService taskInitializeService, TaskFinalizeService taskFinalizeService) { - super(taskInitializeService, taskFinalizeService); + public TaskControllerV1(TaskInitializeService taskInitializeService, TaskFinalizeService taskFinalizeService) { + this.taskInitializeService = taskInitializeService; + this.taskFinalizeService = taskFinalizeService; } /** @@ -48,10 +48,13 @@ public TaskControllerLegacy(TaskInitializeService taskInitializeService, TaskFin */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/initialize") - public ResponseEntity requestInitializeTask( - @RequestParam String chainDealId, - @RequestParam int taskIndex) { - return super.requestInitializeTask(chainDealId, taskIndex); + public ResponseEntity requestInitializeTask(@RequestParam String chainDealId, + @RequestParam int taskIndex) { + String chainTaskId = taskInitializeService.start(chainDealId, taskIndex); + if (!chainTaskId.isEmpty()) { + return ResponseEntity.ok(chainTaskId); + } + return ResponseEntity.badRequest().build(); } /** @@ -62,9 +65,10 @@ public ResponseEntity requestInitializeTask( */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/initialize/{chainTaskId}/status") - public ResponseEntity getStatusForInitializeTaskRequest( - @PathVariable String chainTaskId) { - return super.getStatusForInitializeTaskRequest(chainTaskId); + public ResponseEntity getStatusForInitializeTaskRequest(@PathVariable String chainTaskId) { + return taskInitializeService.getStatusForCommand(chainTaskId) + .map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build()); } /** @@ -76,10 +80,12 @@ public ResponseEntity getStatusForInitializeTaskRequest( */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/finalize/{chainTaskId}") - public ResponseEntity requestFinalizeTask( - @PathVariable String chainTaskId, - @RequestBody TaskFinalizeArgs args) { - return super.requestFinalizeTask(chainTaskId, args); + public ResponseEntity requestFinalizeTask(@PathVariable String chainTaskId, + @RequestBody TaskFinalizeArgs args) { + if (!taskFinalizeService.start(chainTaskId, args).isEmpty()) { + return ResponseEntity.ok(chainTaskId); + } + return ResponseEntity.badRequest().build(); } /** @@ -90,9 +96,10 @@ public ResponseEntity requestFinalizeTask( */ @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/finalize/{chainTaskId}/status") - public ResponseEntity getStatusForFinalizeTaskRequest( - @PathVariable String chainTaskId) { - return super.getStatusForFinalizeTaskRequest(chainTaskId); + public ResponseEntity getStatusForFinalizeTaskRequest(@PathVariable String chainTaskId) { + return taskFinalizeService.getStatusForCommand(chainTaskId) + .map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build()); } } diff --git a/src/test/java/com/iexec/blockchain/command/task/TaskControllerTests.java b/src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java similarity index 98% rename from src/test/java/com/iexec/blockchain/command/task/TaskControllerTests.java rename to src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java index aa367fb..2bfc9bc 100644 --- a/src/test/java/com/iexec/blockchain/command/task/TaskControllerTests.java +++ b/src/test/java/com/iexec/blockchain/command/task/TaskControllerV1Tests.java @@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; -class TaskControllerTests { +class TaskControllerV1Tests { private static final String CHAIN_DEAL_ID = "0x1"; private static final int TASK_INDEX = 0; @@ -45,7 +45,7 @@ class TaskControllerTests { @Mock private TaskFinalizeService taskFinalizeService; @InjectMocks - private TaskController taskController; + private TaskControllerV1 taskController; @BeforeEach void init() { From 1874a4369afa8624e4175927ec5edc2584624059 Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Tue, 20 Feb 2024 15:37:26 +0100 Subject: [PATCH 4/4] Add missing annotations and @deprecated tag --- .../com/iexec/blockchain/command/task/TaskController.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/iexec/blockchain/command/task/TaskController.java b/src/main/java/com/iexec/blockchain/command/task/TaskController.java index 1d20035..c4d4ca5 100644 --- a/src/main/java/com/iexec/blockchain/command/task/TaskController.java +++ b/src/main/java/com/iexec/blockchain/command/task/TaskController.java @@ -28,7 +28,7 @@ import static com.iexec.blockchain.swagger.OpenApiConfig.SWAGGER_BASIC_AUTH; /** - * Call /v1/tasks endpoints in {@code TaskControllerV1} + * @deprecated Call /v1/tasks endpoints in {@code TaskControllerV1} */ @Deprecated(forRemoval = true) @RestController @@ -46,6 +46,7 @@ public TaskController(TaskInitializeService taskInitializeService, TaskFinalizeS * @param taskIndex index of the task int the bag * @return blockchain task ID if successful */ + @Override @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/initialize") public ResponseEntity requestInitializeTask( @@ -60,6 +61,7 @@ public ResponseEntity requestInitializeTask( * @param chainTaskId blockchain ID of the task * @return status */ + @Override @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/initialize/{chainTaskId}/status") public ResponseEntity getStatusForInitializeTaskRequest( @@ -74,6 +76,7 @@ public ResponseEntity getStatusForInitializeTaskRequest( * @param args input arguments for `finalize task` * @return blockchain task ID if successful */ + @Override @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @PostMapping("/finalize/{chainTaskId}") public ResponseEntity requestFinalizeTask( @@ -88,6 +91,7 @@ public ResponseEntity requestFinalizeTask( * @param chainTaskId blockchain ID of the task * @return status */ + @Override @Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH)) @GetMapping("/finalize/{chainTaskId}/status") public ResponseEntity getStatusForFinalizeTaskRequest(