11/*
2- * Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+ * Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2323import com .iexec .commons .poco .task .TaskDescription ;
2424import com .iexec .commons .poco .utils .BytesUtils ;
2525import com .iexec .commons .poco .utils .TestUtils ;
26- import org .junit .jupiter .api .Assertions ;
2726import org .junit .jupiter .api .BeforeEach ;
2827import org .junit .jupiter .api .Test ;
28+ import org .junit .jupiter .api .extension .ExtendWith ;
2929import org .mockito .Mock ;
30- import org .mockito .MockitoAnnotations ;
30+ import org .mockito .junit . jupiter . MockitoExtension ;
3131import org .web3j .crypto .Credentials ;
3232
3333import java .math .BigInteger ;
3737
3838import static com .iexec .common .replicate .ReplicateStatusCause .*;
3939import static org .assertj .core .api .Assertions .assertThat ;
40+ import static org .junit .jupiter .api .Assertions .assertEquals ;
41+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
4042import static org .mockito .ArgumentMatchers .anyString ;
4143import static org .mockito .Mockito .*;
4244
45+ @ ExtendWith (MockitoExtension .class )
4346class ContributionServiceTests {
4447
4548 private static final String CHAIN_DEAL_ID = "0x1566a9348a284d12f7d81fa017fbc440fd501ddef5746821860ffda7113eb847" ;
@@ -63,22 +66,20 @@ class ContributionServiceTests {
6366
6467 @ BeforeEach
6568 void beforeEach () {
66- MockitoAnnotations .openMocks (this );
67-
6869 contributionService = new ContributionService (iexecHubService , workerpoolAuthorizationService , enclaveAuthorizationService , WORKER_WALLET_ADDRESS );
6970 }
7071
7172 @ Test
7273 void shouldChainTaskBeInitialized () {
73- String chainTaskId = "0xabc" ;
74+ final String chainTaskId = "0xabc" ;
7475 when (iexecHubService .getTaskDescription (chainTaskId )).thenReturn (TaskDescription .builder ().build ());
7576
7677 assertThat (contributionService .isChainTaskInitialized (chainTaskId )).isTrue ();
7778 }
7879
7980 @ Test
8081 void shouldChainTaskNotBeInitialized () {
81- String chainTaskId = "0xabc" ;
82+ final String chainTaskId = "0xabc" ;
8283 when (iexecHubService .getTaskDescription (chainTaskId )).thenReturn (null );
8384
8485 assertThat (contributionService .isChainTaskInitialized (chainTaskId )).isFalse ();
@@ -87,7 +88,7 @@ void shouldChainTaskNotBeInitialized() {
8788 //region getCannotContributeStatusCause
8889 @ Test
8990 void getCannotContributeStatusShouldReturnChainUnreachable () {
90- String chainTaskId = "chainTaskId" ;
91+ final String chainTaskId = "chainTaskId" ;
9192
9293 when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .empty ());
9394
@@ -100,7 +101,7 @@ void getCannotContributeStatusShouldReturnChainUnreachable() {
100101
101102 @ Test
102103 void getCannotContributeStatusShouldReturnStakeTooLow () {
103- String chainTaskId = chainTask .getChainTaskId ();
104+ final String chainTaskId = chainTask .getChainTaskId ();
104105
105106 when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (chainTask ));
106107 when (iexecHubService .getChainAccount ()).thenReturn (Optional .of (ChainAccount .builder ().deposit (0 ).build ()));
@@ -117,7 +118,7 @@ void getCannotContributeStatusShouldReturnStakeTooLow() {
117118
118119 @ Test
119120 void getCannotContributeStatusShouldReturnTaskNotActive () {
120- String chainTaskId = chainTask .getChainTaskId ();
121+ final String chainTaskId = chainTask .getChainTaskId ();
121122
122123 when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (chainTask ));
123124 when (iexecHubService .getChainAccount ()).thenReturn (Optional .of (ChainAccount .builder ().deposit (1000 ).build ()));
@@ -136,15 +137,15 @@ void getCannotContributeStatusShouldReturnTaskNotActive() {
136137
137138 @ Test
138139 void getCannotContributeStatusShouldReturnAfterDeadline () {
139- ChainTask chainTask = ChainTask .builder ()
140+ final ChainTask timedOutChainTask = ChainTask .builder ()
140141 .dealid (CHAIN_DEAL_ID )
141142 .idx (0 )
142143 .contributionDeadline (new Date ().getTime () - 1000 )
143144 .build ();
144145
145- String chainTaskId = chainTask .getChainTaskId ();
146+ final String chainTaskId = timedOutChainTask .getChainTaskId ();
146147
147- when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (chainTask ));
148+ when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (timedOutChainTask ));
148149 when (iexecHubService .getChainAccount ())
149150 .thenReturn (Optional .of (ChainAccount .builder ().deposit (1000 ).build ()));
150151 when (iexecHubService .getChainDeal (CHAIN_DEAL_ID ))
@@ -163,7 +164,7 @@ void getCannotContributeStatusShouldReturnAfterDeadline() {
163164
164165 @ Test
165166 void getCannotContributeStatusShouldReturnContributionAlreadySet () {
166- String chainTaskId = chainTask .getChainTaskId ();
167+ final String chainTaskId = chainTask .getChainTaskId ();
167168
168169 when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (chainTask ));
169170 when (iexecHubService .getChainAccount ())
@@ -188,7 +189,7 @@ void getCannotContributeStatusShouldReturnContributionAlreadySet() {
188189
189190 @ Test
190191 void getCannotContributeStatusCauseShouldReturnWorkerpoolAuthorizationNotFound () {
191- String chainTaskId = chainTask .getChainTaskId ();
192+ final String chainTaskId = chainTask .getChainTaskId ();
192193
193194 when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (chainTask ));
194195 when (iexecHubService .getChainAccount ())
@@ -213,7 +214,7 @@ void getCannotContributeStatusCauseShouldReturnWorkerpoolAuthorizationNotFound()
213214
214215 @ Test
215216 void getCannotContributeStatusCauseShouldReturnEmpty () {
216- String chainTaskId = chainTask .getChainTaskId ();
217+ final String chainTaskId = chainTask .getChainTaskId ();
217218
218219 when (iexecHubService .getChainTask (chainTaskId ))
219220 .thenReturn (Optional .of (chainTask ));
@@ -242,7 +243,7 @@ void getCannotContributeStatusCauseShouldReturnEmpty() {
242243 // region getCannotContributeAndFinalizeStatusCause
243244 @ Test
244245 void getCannotContributeAndFinalizeStatusCauseShouldReturnChainUnreachable () {
245- String chainTaskId = "chainTaskId" ;
246+ final String chainTaskId = "chainTaskId" ;
246247
247248 when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .empty ());
248249
@@ -254,7 +255,7 @@ void getCannotContributeAndFinalizeStatusCauseShouldReturnChainUnreachable() {
254255
255256 @ Test
256257 void getCannotContributeAndFinalizeStatusCauseShouldReturnTrustNotOne () {
257- String chainTaskId = chainTask .getChainTaskId ();
258+ final String chainTaskId = chainTask .getChainTaskId ();
258259
259260 ChainDeal chainDeal = ChainDeal .builder ()
260261 .chainDealId (CHAIN_DEAL_ID )
@@ -270,21 +271,21 @@ void getCannotContributeAndFinalizeStatusCauseShouldReturnTrustNotOne() {
270271
271272 @ Test
272273 void getCannotContributeAndFinalizeStatusCauseShouldReturnTaskAlreadyContributed () {
273- ChainTask chainTask = ChainTask .builder ()
274+ final ChainTask chainTaskWithContribution = ChainTask .builder ()
274275 .dealid (CHAIN_DEAL_ID )
275276 .idx (0 )
276277 .contributionDeadline (new Date ().getTime () + 1000 )
277278 .contributors (List .of ("CONTRIBUTED" ))
278279 .build ();
279280
280- String chainTaskId = chainTask .getChainTaskId ();
281+ final String chainTaskId = chainTaskWithContribution .getChainTaskId ();
281282
282- ChainDeal chainDeal = ChainDeal .builder ()
283+ final ChainDeal chainDeal = ChainDeal .builder ()
283284 .chainDealId (CHAIN_DEAL_ID )
284285 .trust (BigInteger .ONE )
285286 .build ();
286287
287- when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (chainTask ));
288+ when (iexecHubService .getChainTask (chainTaskId )).thenReturn (Optional .of (chainTaskWithContribution ));
288289 when (iexecHubService .getChainDeal (CHAIN_DEAL_ID )).thenReturn (Optional .of (chainDeal ));
289290
290291 assertThat (contributionService .getCannotContributeAndFinalizeStatusCause (chainTaskId ).orElse (null ))
@@ -293,9 +294,9 @@ void getCannotContributeAndFinalizeStatusCauseShouldReturnTaskAlreadyContributed
293294
294295 @ Test
295296 void getCannotContributeAndFinalizeStatusCauseShouldReturnEmpty () {
296- String chainTaskId = chainTask .getChainTaskId ();
297+ final String chainTaskId = chainTask .getChainTaskId ();
297298
298- ChainDeal chainDeal = ChainDeal .builder ()
299+ final ChainDeal chainDeal = ChainDeal .builder ()
299300 .chainDealId (CHAIN_DEAL_ID )
300301 .trust (BigInteger .ONE )
301302 .build ();
@@ -325,11 +326,9 @@ void getContribution() {
325326 .build ();
326327 Contribution contribution = contributionService .getContribution (computedFile );
327328
328- System .out .println (contribution );
329-
330- Assertions .assertNotNull (contribution );
329+ assertNotNull (contribution );
331330
332- Assertions . assertEquals (contribution ,
331+ assertEquals (contribution ,
333332 Contribution .builder ()
334333 .chainTaskId (chainTaskId )
335334 .resultDigest (resultDigest )
@@ -365,13 +364,13 @@ void getContributionWithTee() {
365364 .build ();
366365 Contribution contribution = contributionService .getContribution (computedFile );
367366
368- Assertions . assertNotNull (contribution );
369- Assertions . assertEquals (chainTaskId , contribution .getChainTaskId ());
370- Assertions . assertEquals (resultDigest , contribution .getResultDigest ());
371- Assertions . assertEquals (resultSeal , contribution .getResultSeal ());
372- Assertions . assertEquals (TestUtils .ENCLAVE_ADDRESS , contribution .getEnclaveChallenge ());
373- Assertions . assertEquals ("0xenclaveSignature" , contribution .getEnclaveSignature ());
374- Assertions . assertEquals (teeWorkerpoolAuth .getSignature ().getValue (), contribution .getWorkerPoolSignature ());
367+ assertNotNull (contribution );
368+ assertEquals (chainTaskId , contribution .getChainTaskId ());
369+ assertEquals (resultDigest , contribution .getResultDigest ());
370+ assertEquals (resultSeal , contribution .getResultSeal ());
371+ assertEquals (TestUtils .ENCLAVE_ADDRESS , contribution .getEnclaveChallenge ());
372+ assertEquals ("0xenclaveSignature" , contribution .getEnclaveSignature ());
373+ assertEquals (teeWorkerpoolAuth .getSignature ().getValue (), contribution .getWorkerPoolSignature ());
375374
376375 Contribution expectedContribution = Contribution .builder ()
377376 .chainTaskId (chainTaskId )
@@ -382,7 +381,7 @@ void getContributionWithTee() {
382381 .enclaveSignature ("0xenclaveSignature" )
383382 .workerPoolSignature (teeWorkerpoolAuth .getSignature ().getValue ())
384383 .build ();
385- Assertions . assertEquals (contribution , expectedContribution );
384+ assertEquals (contribution , expectedContribution );
386385
387386 }
388387
0 commit comments