|
| 1 | +/* |
| 2 | + * Copyright 2024 IEXEC BLOCKCHAIN TECH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.iexec.blockchain.command.task.finalize; |
| 18 | + |
| 19 | +import com.iexec.blockchain.api.CommandStatus; |
| 20 | +import com.iexec.blockchain.chain.QueueService; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.Arguments; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
| 26 | +import org.mockito.InjectMocks; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 29 | +import org.web3j.protocol.core.methods.response.TransactionReceipt; |
| 30 | + |
| 31 | +import java.util.Optional; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import static com.iexec.commons.poco.utils.BytesUtils.EMPTY_ADDRESS; |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.mockito.ArgumentMatchers.any; |
| 37 | +import static org.mockito.ArgumentMatchers.eq; |
| 38 | +import static org.mockito.Mockito.*; |
| 39 | + |
| 40 | +@ExtendWith(MockitoExtension.class) |
| 41 | +class TaskFinalizeServiceTests { |
| 42 | + |
| 43 | + private static final String CHAIN_TASK_ID = |
| 44 | + "0xe90fc4654b5ea32ad8689091e7610cad7ee5c8b9b1a6e39401b57d90343bfcaa"; |
| 45 | + private static final String RESULT_LINK = "/ipfs/QmeQHGKFAkEkA5tm3kuXqBM9zz9JorkvCsAJ2bzAAh6NX4"; |
| 46 | + |
| 47 | + @InjectMocks |
| 48 | + private TaskFinalizeService taskFinalizeService; |
| 49 | + @Mock |
| 50 | + private TaskFinalizeBlockchainService blockchainService; |
| 51 | + @Mock |
| 52 | + private TaskFinalizeStorageService updaterService; |
| 53 | + @Mock |
| 54 | + private QueueService queueService; |
| 55 | + |
| 56 | + private final TaskFinalizeArgs args = new TaskFinalizeArgs(CHAIN_TASK_ID, RESULT_LINK, EMPTY_ADDRESS); |
| 57 | + |
| 58 | + // region start |
| 59 | + @Test |
| 60 | + void shouldFinalizeTask() { |
| 61 | + when(blockchainService.canSendBlockchainCommand(args)).thenReturn(true); |
| 62 | + when(updaterService.updateToReceived(args)).thenReturn(true); |
| 63 | + final String chainTaskId = taskFinalizeService.start(CHAIN_TASK_ID, RESULT_LINK, EMPTY_ADDRESS); |
| 64 | + assertThat(chainTaskId).isEqualTo(CHAIN_TASK_ID); |
| 65 | + verify(queueService).addExecutionToQueue(any(Runnable.class), eq(true)); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("provideTaskFinalizeBadParameters") |
| 70 | + void shouldNotFinalizeTaskWithBadParameters(final String chainTaskId, final String resultLink, final String callbackData) { |
| 71 | + assertThat(taskFinalizeService.start(chainTaskId, resultLink, callbackData)).isEmpty(); |
| 72 | + } |
| 73 | + |
| 74 | + private static Stream<Arguments> provideTaskFinalizeBadParameters() { |
| 75 | + return Stream.of( |
| 76 | + Arguments.of("not-a-task", RESULT_LINK, EMPTY_ADDRESS), |
| 77 | + Arguments.of(CHAIN_TASK_ID, null, EMPTY_ADDRESS), |
| 78 | + Arguments.of(CHAIN_TASK_ID, RESULT_LINK, null) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void shouldNotFinalizeTaskSinceCannotOnChain() { |
| 84 | + when(blockchainService.canSendBlockchainCommand(args)).thenReturn(false); |
| 85 | + |
| 86 | + final String chainTaskId = taskFinalizeService.start(CHAIN_TASK_ID, RESULT_LINK, EMPTY_ADDRESS); |
| 87 | + |
| 88 | + assertThat(chainTaskId).isEmpty(); |
| 89 | + verifyNoInteractions(queueService); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void shouldNotFinalizeTaskSinceCannotUpdate() { |
| 94 | + when(blockchainService.canSendBlockchainCommand(args)).thenReturn(true); |
| 95 | + when(updaterService.updateToReceived(args)).thenReturn(false); |
| 96 | + |
| 97 | + final String chainTaskId = taskFinalizeService.start(CHAIN_TASK_ID, RESULT_LINK, EMPTY_ADDRESS); |
| 98 | + |
| 99 | + assertThat(chainTaskId).isEmpty(); |
| 100 | + verifyNoInteractions(queueService); |
| 101 | + } |
| 102 | + // endregion |
| 103 | + |
| 104 | + // region triggerBlockchainCommand |
| 105 | + @Test |
| 106 | + void triggerFinalizeTask() throws Exception { |
| 107 | + final TransactionReceipt receipt = mock(TransactionReceipt.class); |
| 108 | + when(updaterService.updateToProcessing(CHAIN_TASK_ID)).thenReturn(true); |
| 109 | + when(blockchainService.sendBlockchainCommand(args)).thenReturn(receipt); |
| 110 | + |
| 111 | + taskFinalizeService.triggerBlockchainCommand(args); |
| 112 | + verify(updaterService).updateToFinal(CHAIN_TASK_ID, receipt); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void shouldNotTriggerFinalizeTaskSinceCannotUpdate() { |
| 117 | + final TransactionReceipt receipt = mock(TransactionReceipt.class); |
| 118 | + when(updaterService.updateToProcessing(CHAIN_TASK_ID)).thenReturn(false); |
| 119 | + |
| 120 | + taskFinalizeService.triggerBlockchainCommand(args); |
| 121 | + verify(updaterService, never()).updateToFinal(CHAIN_TASK_ID, receipt); |
| 122 | + verifyNoInteractions(blockchainService); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void shouldNotTriggerFinalizeTaskSinceReceiptIsNull() throws Exception { |
| 127 | + when(updaterService.updateToProcessing(CHAIN_TASK_ID)).thenReturn(true); |
| 128 | + when(blockchainService.sendBlockchainCommand(args)).thenReturn(null); |
| 129 | + |
| 130 | + taskFinalizeService.triggerBlockchainCommand(args); |
| 131 | + verify(updaterService).updateToFinal(CHAIN_TASK_ID, null); |
| 132 | + } |
| 133 | + // endregion |
| 134 | + |
| 135 | + // region getStatusForCommand |
| 136 | + @Test |
| 137 | + void shouldGetStatusForFinalizeTaskRequest() { |
| 138 | + when(updaterService.getStatusForCommand(CHAIN_TASK_ID)).thenReturn(Optional.of(CommandStatus.PROCESSING)); |
| 139 | + assertThat(taskFinalizeService.getStatusForCommand(CHAIN_TASK_ID)).contains(CommandStatus.PROCESSING); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void shouldNotGetStatusForFinalizeTaskRequestSinceNoRequest() { |
| 144 | + when(updaterService.getStatusForCommand(CHAIN_TASK_ID)).thenReturn(Optional.empty()); |
| 145 | + assertThat(taskFinalizeService.getStatusForCommand(CHAIN_TASK_ID)).isEmpty(); |
| 146 | + } |
| 147 | + // endregion |
| 148 | + |
| 149 | +} |
0 commit comments