11/*
2- * Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
2+ * Copyright 2020-2025 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.
1414 * limitations under the License.
1515 */
1616
17- package com .iexec .core .replicate ;
17+ package com .iexec .core .replicate . listener ;
1818
1919import com .iexec .common .replicate .ReplicateStatus ;
2020import com .iexec .common .replicate .ReplicateStatusUpdate ;
2121import com .iexec .core .detector .replicate .ContributionUnnotifiedDetector ;
22- import com .iexec .core .task .listener .ReplicateListeners ;
22+ import com .iexec .core .replicate .ReplicateUpdatedEvent ;
23+ import com .iexec .core .replicate .ReplicatesService ;
2324import com .iexec .core .task .update .TaskUpdateRequestManager ;
2425import com .iexec .core .worker .WorkerService ;
25- import org .junit .jupiter .api .BeforeEach ;
2626import org .junit .jupiter .api .Test ;
27+ import org .junit .jupiter .api .extension .ExtendWith ;
2728import org .junit .jupiter .params .ParameterizedTest ;
2829import org .junit .jupiter .params .provider .MethodSource ;
29- import org .mockito .*;
30+ import org .mockito .ArgumentCaptor ;
31+ import org .mockito .InjectMocks ;
32+ import org .mockito .Mock ;
33+ import org .mockito .junit .jupiter .MockitoExtension ;
3034
3135import java .util .Arrays ;
3236import java .util .List ;
3640import static com .iexec .common .replicate .ReplicateStatus .*;
3741import static com .iexec .common .replicate .ReplicateStatusCause .TASK_NOT_ACTIVE ;
3842import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
39- import static org .mockito .ArgumentMatchers .*;
43+ import static org .mockito .ArgumentMatchers .any ;
44+ import static org .mockito .ArgumentMatchers .eq ;
45+ import static org .mockito .Mockito .*;
4046
47+ @ ExtendWith (MockitoExtension .class )
4148class ReplicateListenersTests {
4249
4350 private static final String CHAIN_TASK_ID = "chainTaskId" ;
@@ -55,22 +62,15 @@ class ReplicateListenersTests {
5562 @ InjectMocks
5663 private ReplicateListeners replicateListeners ;
5764
58- @ BeforeEach
59- void init () {
60- MockitoAnnotations .openMocks (this );
61- }
62-
6365 @ Test
6466 void shouldUpdateTaskOnReplicateUpdate () {
65- List <ReplicateStatus > someStatuses = ReplicateStatus .getSuccessStatuses (); //not exhaustive
67+ final List <ReplicateStatus > someStatuses = ReplicateStatus .getSuccessStatuses (); //not exhaustive
6668
67- for (ReplicateStatus randomStatus : someStatuses ) {
68- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (randomStatus );
69+ someStatuses .stream ()
70+ .map (this ::getMockReplicate )
71+ .forEach (replicateListeners ::onReplicateUpdatedEvent );
6972
70- replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
71- }
72-
73- Mockito .verify (taskUpdateRequestManager , Mockito .times (someStatuses .size ())).publishRequest (any ());
73+ verify (taskUpdateRequestManager , times (someStatuses .size ())).publishRequest (any ());
7474 }
7575
7676 @ Test
@@ -98,51 +98,46 @@ void shouldRemoveFromComputedTasksSinceComputeFailed() {
9898 assertIsRemovedFromComputedTasks (COMPUTE_FAILED );
9999 }
100100
101- private void assertIsRemovedFromComputedTasks (ReplicateStatus computed ) {
102- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (computed );
101+ private void assertIsRemovedFromComputedTasks (final ReplicateStatus computed ) {
102+ final ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (computed );
103103 replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
104- Mockito .verify (workerService , Mockito .times (1 ))
105- .removeComputedChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
104+ verify (workerService ).removeComputedChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
106105 }
107106
108107 @ Test
109108 void shouldTriggerDetectOnchainContributedSinceTaskNotActive () {
110- ReplicateUpdatedEvent replicateUpdatedEvent = ReplicateUpdatedEvent .builder ()
109+ final ReplicateUpdatedEvent replicateUpdatedEvent = ReplicateUpdatedEvent .builder ()
111110 .chainTaskId (CHAIN_TASK_ID )
112111 .walletAddress (WORKER_WALLET )
113112 .replicateStatusUpdate (new ReplicateStatusUpdate (CONTRIBUTING , TASK_NOT_ACTIVE ))
114113 .build ();
115114
116115 replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
117116
118- Mockito . verify (contributionUnnotifiedDetector , Mockito . times ( 1 ) ).detectOnchainDone ();
117+ verify (contributionUnnotifiedDetector ).detectOnchainDone ();
119118 }
120119
121120 @ Test
122121 void shouldNotTriggerDetectOnchain () {
123- List <ReplicateStatus > someStatuses = ReplicateStatus .getSuccessStatuses (); //not exhaustive
124- someStatuses .remove (CONTRIBUTING );
125-
126- for (ReplicateStatus randomStatus : someStatuses ) {
127- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (randomStatus );
122+ ReplicateStatus .getSuccessStatuses ().stream ()
123+ .filter (status -> status != CONTRIBUTING )
124+ .map (this ::getMockReplicate )
125+ .forEach (replicateListeners ::onReplicateUpdatedEvent );
128126
129- replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
130- }
131-
132- Mockito .verify (contributionUnnotifiedDetector , Mockito .times (0 )).detectOnchainDone ();
127+ verifyNoInteractions (contributionUnnotifiedDetector );
133128 }
134129
135130 @ Test
136131 void shouldNotTriggerDetectOnchainContributedSinceCauseIsNull () {
137- ReplicateUpdatedEvent replicateUpdatedEvent = ReplicateUpdatedEvent .builder ()
132+ final ReplicateUpdatedEvent replicateUpdatedEvent = ReplicateUpdatedEvent .builder ()
138133 .chainTaskId (CHAIN_TASK_ID )
139134 .walletAddress (WORKER_WALLET )
140135 .replicateStatusUpdate (new ReplicateStatusUpdate (CONTRIBUTING ))
141136 .build ();
142137
143138 replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
144139
145- Mockito . verify (contributionUnnotifiedDetector , Mockito . times ( 0 )). detectOnchainDone ( );
140+ verifyNoInteractions (contributionUnnotifiedDetector );
146141 }
147142
148143 static Stream <ReplicateStatus > getUncompletableStatuses () {
@@ -151,13 +146,12 @@ static Stream<ReplicateStatus> getUncompletableStatuses() {
151146
152147 @ ParameterizedTest
153148 @ MethodSource ("getUncompletableStatuses" )
154- void shouldAddFailedStatusSinceUncompletableReplicateStatus (ReplicateStatus uncompletableStatus ) {
155- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (uncompletableStatus );
149+ void shouldAddFailedStatusSinceUncompletableReplicateStatus (final ReplicateStatus uncompletableStatus ) {
150+ final ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (uncompletableStatus );
156151 replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
157152
158153 final ArgumentCaptor <ReplicateStatusUpdate > statusUpdate = ArgumentCaptor .forClass (ReplicateStatusUpdate .class );
159- Mockito .verify (replicatesService , Mockito .times (1 ))
160- .updateReplicateStatus (eq (CHAIN_TASK_ID ), eq (WORKER_WALLET ), statusUpdate .capture ());
154+ verify (replicatesService ).updateReplicateStatus (eq (CHAIN_TASK_ID ), eq (WORKER_WALLET ), statusUpdate .capture ());
161155 assertThat (statusUpdate .getValue ().getStatus ()).isEqualTo (FAILED );
162156 }
163157
@@ -168,51 +162,43 @@ static Stream<ReplicateStatus> getCompletableStatuses() {
168162
169163 @ ParameterizedTest
170164 @ MethodSource ("getCompletableStatuses" )
171- void shouldNotAddFailedStatusSinceCompletableReplicateStatus (ReplicateStatus completableStatus ) {
172- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (completableStatus );
165+ void shouldNotAddFailedStatusSinceCompletableReplicateStatus (final ReplicateStatus completableStatus ) {
166+ final ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (completableStatus );
173167 replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
174168
175- Mockito .verify (replicatesService , Mockito .times (0 ))
176- .updateReplicateStatus (anyString (), anyString (), any (ReplicateStatusUpdate .class ));
169+ verifyNoInteractions (replicatesService );
177170 }
178171
179172 @ Test
180173 void shouldRemoveChainTaskIdFromWorkerSinceCompleted () {
181- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (COMPLETED );
174+ final ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (COMPLETED );
182175
183176 replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
184177
185- Mockito .verify (workerService , Mockito .times (1 ))
186- .removeChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
178+ verify (workerService ).removeChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
187179 }
188180
189181 @ Test
190182 void shouldRemoveChainTaskIdFromWorkerSinceFailed () {
191- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (FAILED );
183+ final ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (FAILED );
192184
193185 replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
194186
195- Mockito .verify (workerService , Mockito .times (1 ))
196- .removeChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
187+ verify (workerService ).removeChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
197188 }
198189
199190 @ Test
200191 void shouldNotRemoveChainTaskIdFromWorker () {
201- List <ReplicateStatus > someStatuses = ReplicateStatus .getSuccessStatuses (); //not exhaustive
202- someStatuses .remove (COMPLETED );
203- someStatuses .remove (FAILED );
204-
205- for (ReplicateStatus randomStatus : someStatuses ) {
206- ReplicateUpdatedEvent replicateUpdatedEvent = getMockReplicate (randomStatus );
207-
208- replicateListeners .onReplicateUpdatedEvent (replicateUpdatedEvent );
209- }
192+ ReplicateStatus .getSuccessStatuses ().stream ()
193+ .filter (status -> status != COMPLETED && status != FAILED )
194+ .map (this ::getMockReplicate )
195+ .forEach (replicateListeners ::onReplicateUpdatedEvent );
210196
211- Mockito . verify (workerService , Mockito . times ( 0 ))
212- .removeChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
197+ verify (workerService ). removeComputedChainTaskIdFromWorker ( CHAIN_TASK_ID , WORKER_WALLET );
198+ verify ( workerService , never ()) .removeChainTaskIdFromWorker (CHAIN_TASK_ID , WORKER_WALLET );
213199 }
214200
215- private ReplicateUpdatedEvent getMockReplicate (ReplicateStatus computed ) {
201+ private ReplicateUpdatedEvent getMockReplicate (final ReplicateStatus computed ) {
216202 return ReplicateUpdatedEvent .builder ()
217203 .chainTaskId (CHAIN_TASK_ID )
218204 .walletAddress (WORKER_WALLET )
0 commit comments