Skip to content

Commit 1092760

Browse files
authored
Improve code maintainability in test classes (#615)
1 parent 5717913 commit 1092760

File tree

10 files changed

+139
-166
lines changed

10 files changed

+139
-166
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
1616
### Quality
1717

1818
- Reorder `static` and `final` keywords. (#614)
19+
- Improve code maintainability in test classes. (#615)
1920

2021
### Dependency Upgrades
2122

src/test/java/com/iexec/worker/chain/ContributionServiceTests.java

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -23,11 +23,11 @@
2323
import com.iexec.commons.poco.task.TaskDescription;
2424
import com.iexec.commons.poco.utils.BytesUtils;
2525
import com.iexec.commons.poco.utils.TestUtils;
26-
import org.junit.jupiter.api.Assertions;
2726
import org.junit.jupiter.api.BeforeEach;
2827
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
2929
import org.mockito.Mock;
30-
import org.mockito.MockitoAnnotations;
30+
import org.mockito.junit.jupiter.MockitoExtension;
3131
import org.web3j.crypto.Credentials;
3232

3333
import java.math.BigInteger;
@@ -37,9 +37,12 @@
3737

3838
import static com.iexec.common.replicate.ReplicateStatusCause.*;
3939
import 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;
4042
import static org.mockito.ArgumentMatchers.anyString;
4143
import static org.mockito.Mockito.*;
4244

45+
@ExtendWith(MockitoExtension.class)
4346
class 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

src/test/java/com/iexec/worker/chain/RevealServiceTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -24,8 +24,9 @@
2424
import org.apache.commons.lang3.time.DateUtils;
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.ExtendWith;
2728
import org.mockito.Mock;
28-
import org.mockito.MockitoAnnotations;
29+
import org.mockito.junit.jupiter.MockitoExtension;
2930
import org.web3j.crypto.Credentials;
3031
import org.web3j.crypto.Hash;
3132
import org.web3j.protocol.core.methods.response.Log;
@@ -38,6 +39,7 @@
3839
import static org.junit.jupiter.api.Assertions.assertThrows;
3940
import static org.mockito.Mockito.when;
4041

42+
@ExtendWith(MockitoExtension.class)
4143
class RevealServiceTests {
4244
private static final String WORKER_WALLET_ADDRESS = "0x2D29bfBEc903479fe4Ba991918bAB99B494f2bEf";
4345

@@ -50,8 +52,6 @@ class RevealServiceTests {
5052

5153
@BeforeEach
5254
void beforeEach() {
53-
MockitoAnnotations.openMocks(this);
54-
5555
revealService = new RevealService(iexecHubService, web3jService, WORKER_WALLET_ADDRESS);
5656
}
5757

@@ -77,9 +77,7 @@ void canRevealAllValid() {
7777
.resultHash(contributionValue)
7878
.resultSeal(contributionSeal)
7979
.build());
80-
when(iexecHubService.hasEnoughGas()).thenReturn(true);
8180
when(iexecHubService.getChainContribution(chainTaskId)).thenReturn(optionalChainContribution);
82-
// when(resultService.getDeterministHashForTask(chainTaskId)).thenReturn(determinismHash);
8381
when(iexecHubService.isChainTaskRevealing(chainTaskId)).thenReturn(true);
8482

8583
assertThat(revealService.canReveal(chainTaskId, determinismHash)).isTrue();

src/test/java/com/iexec/worker/chain/WorkerpoolAuthorizationServiceTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ void shouldPurgeAllTasksData() {
126126
// endregion
127127

128128
private WorkerpoolAuthorization getWorkerpoolAuthorization() {
129-
// PRIVATE_KEY_STRING: "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6";
130-
// PUBLIC_KEY_STRING: "0x506bc1dc099358e5137292f4efdd57e400f29ba5132aa5d12b18dac1c1f6aaba645c0b7b58158babbfa6c6cd5a48aa7340a8749176b120e8516216787a13dc76";
131-
132129
final String workerWallet = "0x748e091bf16048cb5103E0E10F9D5a8b7fBDd860";
133130
final String chainTaskId = "0xd94b63fc2d3ec4b96daf84b403bbafdc8c8517e8e2addd51fec0fa4e67801be8";
134131
final String enclaveWallet = "0x9a43BB008b7A657e1936ebf5d8e28e5c5E021596";

src/test/java/com/iexec/worker/compute/ComputeManagerServiceTests.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
import com.iexec.worker.docker.DockerRegistryConfiguration;
3535
import com.iexec.worker.docker.DockerService;
3636
import com.iexec.worker.result.ResultService;
37-
import org.junit.jupiter.api.BeforeEach;
3837
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.extension.ExtendWith;
3939
import org.junit.jupiter.api.io.TempDir;
4040
import org.junit.jupiter.params.ParameterizedTest;
4141
import org.junit.jupiter.params.provider.Arguments;
4242
import org.junit.jupiter.params.provider.EnumSource;
4343
import org.junit.jupiter.params.provider.MethodSource;
4444
import org.mockito.InjectMocks;
4545
import org.mockito.Mock;
46-
import org.mockito.MockitoAnnotations;
46+
import org.mockito.junit.jupiter.MockitoExtension;
4747

4848
import java.io.File;
4949
import java.time.Duration;
@@ -56,13 +56,13 @@
5656
import static org.mockito.ArgumentMatchers.anyString;
5757
import static org.mockito.Mockito.*;
5858

59+
@ExtendWith(MockitoExtension.class)
5960
class ComputeManagerServiceTests {
6061

6162
private static final String CHAIN_TASK_ID = "CHAIN_TASK_ID";
6263
private static final String DATASET_URI = "DATASET_URI";
6364
private static final String DIGEST = "digest";
6465
private static final String APP_URI = "APP_URI";
65-
private static final String TEE_POST_COMPUTE_IMAGE = "TEE_POST_COMPUTE_IMAGE";
6666
private static final TeeSessionGenerationResponse SECURE_SESSION = mock(TeeSessionGenerationResponse.class);
6767
private static final long MAX_EXECUTION_TIME = 1000;
6868

@@ -95,11 +95,6 @@ class ComputeManagerServiceTests {
9595
@Mock
9696
private ResultService resultService;
9797

98-
@BeforeEach
99-
void beforeEach() {
100-
MockitoAnnotations.openMocks(this);
101-
}
102-
10398
private TaskDescription.TaskDescriptionBuilder createTaskDescriptionBuilder(boolean isTeeTask) {
10499
return TaskDescription.builder()
105100
.chainTaskId(CHAIN_TASK_ID)
@@ -127,7 +122,7 @@ void shouldDownloadApp() {
127122
void shouldNotDownloadAppSincePullImageFailed() {
128123
final TaskDescription taskDescription = createTaskDescriptionBuilder(true).build();
129124
when(dockerService.getClient(taskDescription.getAppUri())).thenReturn(dockerClient);
130-
when(dockerClient.pullImage(taskDescription.getAppUri())).thenReturn(false);
125+
when(dockerClient.pullImage(taskDescription.getAppUri(), Duration.ofMinutes(0))).thenReturn(false);
131126
assertThat(computeManagerService.downloadApp(taskDescription)).isFalse();
132127
}
133128

0 commit comments

Comments
 (0)