Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Label REST API with v1 version #132

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 14 additions & 22 deletions src/main/java/com/iexec/blockchain/command/task/TaskController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@

import static com.iexec.blockchain.swagger.OpenApiConfig.SWAGGER_BASIC_AUTH;

/**
* @deprecated Call /v1/tasks endpoints in {@code TaskControllerV1}
*/
@Deprecated(forRemoval = true)
@RestController
@RequestMapping("/tasks")
public class TaskController {

private final TaskInitializeService taskInitializeService;
private final TaskFinalizeService taskFinalizeService;
public class TaskController extends TaskControllerV1 {

public TaskController(TaskInitializeService taskInitializeService, TaskFinalizeService taskFinalizeService) {
this.taskInitializeService = taskInitializeService;
this.taskFinalizeService = taskFinalizeService;
super(taskInitializeService, taskFinalizeService);
}

/**
Expand All @@ -46,17 +46,13 @@ 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<String> requestInitializeTask(
@RequestParam String chainDealId,
@RequestParam int taskIndex) {
String chainTaskId =
taskInitializeService.start(chainDealId, taskIndex);
if (!chainTaskId.isEmpty()) {
return ResponseEntity.ok(chainTaskId);
}
return ResponseEntity.badRequest().build();
return super.requestInitializeTask(chainDealId, taskIndex);
}

/**
Expand All @@ -65,13 +61,12 @@ public ResponseEntity<String> requestInitializeTask(
* @param chainTaskId blockchain ID of the task
* @return status
*/
@Override
@Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH))
@GetMapping("/initialize/{chainTaskId}/status")
public ResponseEntity<CommandStatus> getStatusForInitializeTaskRequest(
@PathVariable String chainTaskId) {
return taskInitializeService.getStatusForCommand(chainTaskId)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
return super.getStatusForInitializeTaskRequest(chainTaskId);
}

/**
Expand All @@ -81,15 +76,13 @@ public ResponseEntity<CommandStatus> 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<String> requestFinalizeTask(
@PathVariable String chainTaskId,
@RequestBody TaskFinalizeArgs args) {
if (!taskFinalizeService.start(chainTaskId, args).isEmpty()) {
return ResponseEntity.ok(chainTaskId);
}
return ResponseEntity.badRequest().build();
return super.requestFinalizeTask(chainTaskId, args);
}

/**
Expand All @@ -98,13 +91,12 @@ public ResponseEntity<String> requestFinalizeTask(
* @param chainTaskId blockchain ID of the task
* @return status
*/
@Override
@Operation(security = @SecurityRequirement(name = SWAGGER_BASIC_AUTH))
@GetMapping("/finalize/{chainTaskId}/status")
public ResponseEntity<CommandStatus> getStatusForFinalizeTaskRequest(
@PathVariable String chainTaskId) {
return taskFinalizeService.getStatusForCommand(chainTaskId)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
return super.getStatusForFinalizeTaskRequest(chainTaskId);
}

}
105 changes: 105 additions & 0 deletions src/main/java/com/iexec/blockchain/command/task/TaskControllerV1.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<CommandStatus> 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<String> 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<CommandStatus> getStatusForFinalizeTaskRequest(@PathVariable String chainTaskId) {
return taskFinalizeService.getStatusForCommand(chainTaskId)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

}
Original file line number Diff line number Diff line change
@@ -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
}