Skip to content

Commit

Permalink
add some tests for iexecHubService and remove unused import
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhitewizard committed May 9, 2023
1 parent d7bfb93 commit f34adb8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
75 changes: 75 additions & 0 deletions src/test/java/com/iexec/core/chain/IexecHubServiceTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.iexec.core.chain;

import com.iexec.commons.poco.chain.ChainTask;
import com.iexec.commons.poco.chain.ChainTaskStatus;
import com.iexec.commons.poco.contract.generated.IexecHubContract;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.Keys;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.tx.TransactionManager;

import java.math.BigInteger;
import java.util.Optional;

import static com.iexec.commons.poco.utils.TestUtils.CHAIN_TASK_ID;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class IexecHubServiceTests {

@Mock
private CredentialsService credentialsService;

@Mock
private Web3jService web3jService;

@Mock
private ChainConfig chainConfig;

private IexecHubService iexecHubService;

@BeforeEach
void init() throws Exception {
MockitoAnnotations.openMocks(this);

final Credentials credentials = Credentials.create(Keys.createEcKeyPair());

when(credentialsService.getCredentials()).thenReturn(credentials);
when(web3jService.hasEnoughGas(any())).thenReturn(true);
when(chainConfig.getHubAddress()).thenReturn("0x748e091bf16048cb5103E0E10F9D5a8b7fBDd860");

try (MockedStatic<IexecHubContract> iexecHubContract = Mockito.mockStatic(IexecHubContract.class)) {
final IexecHubContract mockIexecContract = mock(IexecHubContract.class);
final RemoteFunctionCall<BigInteger> mockRemoteFunctionCall = mock(RemoteFunctionCall.class);
iexecHubContract.when(() -> IexecHubContract.load(any(), any(), (TransactionManager) any(), any()))
.thenReturn(mockIexecContract);
when(mockIexecContract.contribution_deadline_ratio()).thenReturn(mockRemoteFunctionCall);
when(mockRemoteFunctionCall.send()).thenReturn(BigInteger.ONE);
iexecHubService = spy(new IexecHubService(credentialsService, web3jService, chainConfig));
}
}


@Test
void shouldTaskBeInCompletedStatusOnChain() {
final ChainTask task = ChainTask.builder().status(ChainTaskStatus.COMPLETED).build();
doReturn(Optional.of(task)).when(iexecHubService).getChainTask(CHAIN_TASK_ID);

assertThat(iexecHubService.isTaskInCompletedStatusOnChain(CHAIN_TASK_ID)).isTrue();
}

@Test
void shouldTaskNotBeInCompletedStatusOnChain() {
final ChainTask task = ChainTask.builder().status(ChainTaskStatus.REVEALING).build();
doReturn(Optional.of(task)).when(iexecHubService).getChainTask(CHAIN_TASK_ID);

assertThat(iexecHubService.isTaskInCompletedStatusOnChain(CHAIN_TASK_ID)).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import com.iexec.common.replicate.*;
import com.iexec.commons.poco.chain.ChainContribution;
import com.iexec.commons.poco.chain.ChainContributionStatus;
import com.iexec.commons.poco.chain.ChainTask;
import com.iexec.commons.poco.chain.ChainTaskStatus;
import com.iexec.commons.poco.notification.TaskNotificationType;
import com.iexec.commons.poco.task.TaskDescription;
import com.iexec.commons.poco.utils.BytesUtils;
Expand Down

0 comments on commit f34adb8

Please sign in to comment.