Skip to content

Commit 1d50b29

Browse files
committed
Add IexecHubServiceTests
1 parent 7e0e33e commit 1d50b29

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2023-2023 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.tool;
18+
19+
import com.iexec.commons.poco.contract.generated.IexecHubContract;
20+
import lombok.SneakyThrows;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.mockito.Mock;
24+
import org.mockito.MockitoAnnotations;
25+
import org.springframework.test.util.ReflectionTestUtils;
26+
import org.web3j.crypto.Credentials;
27+
import org.web3j.crypto.ECKeyPair;
28+
import org.web3j.crypto.Keys;
29+
import org.web3j.protocol.core.RemoteFunctionCall;
30+
import org.web3j.protocol.core.methods.response.TransactionReceipt;
31+
32+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
33+
import static org.mockito.ArgumentMatchers.any;
34+
import static org.mockito.Mockito.when;
35+
36+
class IexecHubServiceTests {
37+
private final ChainConfig chainConfig = ChainConfig
38+
.builder()
39+
.hubAddress("0xC129e7917b7c7DeDfAa5Fff1FB18d5D7050fE8ca")
40+
.build();
41+
@Mock
42+
private CredentialsService credentialsService;
43+
@Mock
44+
private IexecHubContract iexecHubContract;
45+
@Mock
46+
private Web3jService web3jService;
47+
@Mock
48+
private RemoteFunctionCall<TransactionReceipt> remoteFunctionCall;
49+
private IexecHubService iexecHubService;
50+
51+
@BeforeEach
52+
void init() {
53+
MockitoAnnotations.openMocks(this);
54+
Credentials credentials = createEthereumCredentials();
55+
when(credentialsService.getCredentials()).thenReturn(credentials);
56+
iexecHubService = new IexecHubService(credentialsService, web3jService, chainConfig);
57+
ReflectionTestUtils.setField(iexecHubService, "iexecHubContract", iexecHubContract);
58+
}
59+
60+
@SneakyThrows
61+
private Credentials createEthereumCredentials() {
62+
ECKeyPair ecKeyPair = Keys.createEcKeyPair();
63+
return Credentials.create(ecKeyPair);
64+
}
65+
66+
@Test
67+
void shouldNotInitializeTask() throws Exception {
68+
when(iexecHubContract.initialize(any(), any())).thenReturn(remoteFunctionCall);
69+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
70+
assertThatThrownBy(() -> iexecHubService.initializeTask("chainTaskId", 0))
71+
.isInstanceOf(Exception.class);
72+
}
73+
74+
@Test
75+
void shouldNotContribute() throws Exception {
76+
when(iexecHubContract.contribute(any(), any(), any(), any(), any(), any())).thenReturn(remoteFunctionCall);
77+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
78+
assertThatThrownBy(() -> iexecHubService.contribute("chainTaskId", "resultDigest",
79+
"workerpoolSignature", "enclaveChallenge", "enclaveSignature"))
80+
.isInstanceOf(Exception.class);
81+
}
82+
83+
@Test
84+
void shouldNotReveal() throws Exception {
85+
when(iexecHubContract.reveal(any(), any())).thenReturn(remoteFunctionCall);
86+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
87+
assertThatThrownBy(() -> iexecHubService.reveal("chainTaskId", "resultDigest"))
88+
.isInstanceOf(Exception.class);
89+
}
90+
91+
@Test
92+
void shouldNotFinalizeTask() throws Exception {
93+
when(iexecHubContract.finalize(any(), any(), any())).thenReturn(remoteFunctionCall);
94+
when(remoteFunctionCall.send()).thenThrow(Exception.class);
95+
assertThatThrownBy(() -> iexecHubService.finalizeTask("chainTaskId", "resultLink", "callbackData"))
96+
.isInstanceOf(Exception.class);
97+
}
98+
}

0 commit comments

Comments
 (0)