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.
3333import org .assertj .core .api .Assertions ;
3434import org .junit .jupiter .api .AfterEach ;
3535import org .junit .jupiter .api .BeforeAll ;
36- import org .junit .jupiter .api .BeforeEach ;
3736import org .junit .jupiter .api .Test ;
37+ import org .junit .jupiter .api .extension .ExtendWith ;
3838import org .junit .jupiter .params .ParameterizedTest ;
3939import org .junit .jupiter .params .provider .Arguments ;
4040import org .junit .jupiter .params .provider .MethodSource ;
4141import org .mockito .ArgumentCaptor ;
4242import org .mockito .InjectMocks ;
4343import org .mockito .Mock ;
44- import org .mockito .MockitoAnnotations ;
44+ import org .mockito .junit . jupiter . MockitoExtension ;
4545import org .springframework .context .ApplicationEventPublisher ;
4646import org .springframework .test .util .ReflectionTestUtils ;
4747import org .web3j .protocol .core .methods .response .Log ;
5656import static org .assertj .core .api .Assertions .assertThat ;
5757import static org .mockito .Mockito .*;
5858
59+ @ ExtendWith (MockitoExtension .class )
5960class DealWatcherServiceTests {
6061
6162 private static final String OUT_OF_SERVICE_FIELD_NAME = "outOfService" ;
@@ -91,9 +92,7 @@ static void initRegistry() {
9192 Metrics .globalRegistry .add (new SimpleMeterRegistry ());
9293 }
9394
94- @ BeforeEach
95- void init () {
96- MockitoAnnotations .openMocks (this );
95+ void initMocks () {
9796 when (chainConfig .getHubAddress ()).thenReturn ("hubAddress" );
9897 when (chainConfig .getPoolAddress ()).thenReturn ("0x1" );
9998 }
@@ -124,6 +123,7 @@ void shouldReturnZeroForAllCountersWhereNothingHasAppended() {
124123 @ Test
125124 void shouldRunAndStop () {
126125 BigInteger blockNumber = BigInteger .TEN ;
126+ initMocks ();
127127 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (blockNumber );
128128 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .empty ());
129129 dealWatcherService .run ();
@@ -142,6 +142,7 @@ void shouldUpdateLastSeenBlockWhenOneDeal() {
142142 BigInteger blockOfDeal = BigInteger .valueOf (3 );
143143 IexecHubContract .SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice (blockOfDeal );
144144
145+ initMocks ();
145146 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (from );
146147 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice ));
147148
@@ -182,8 +183,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndCreateTask() {
182183
183184 Task task = new Task ();
184185
186+ initMocks ();
185187 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice ));
186- when (iexecHubService .getChainDeal (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
188+ when (iexecHubService .getChainDealWithDetails (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
187189 when (iexecHubService .isBeforeContributionDeadline (chainDeal )).thenReturn (true );
188190 when (taskService .addTask (any (), anyInt (), anyLong (), any (), any (), anyInt (), anyLong (), any (), any (), any ()))
189191 .thenReturn (Optional .of (task ));
@@ -214,8 +216,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndNotCreateTaskSinceDealIsExpired() {
214216 BigInteger blockOfDeal = BigInteger .valueOf (3 );
215217 IexecHubContract .SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice (blockOfDeal );
216218
219+ initMocks ();
217220 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice ));
218- when (iexecHubService .getChainDeal (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
221+ when (iexecHubService .getChainDealWithDetails (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
219222 when (iexecHubService .isBeforeContributionDeadline (chainDeal )).thenReturn (false );
220223 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (from );
221224
@@ -236,8 +239,9 @@ void shouldUpdateLastSeenBlockWhenOneDealAndNotCreateTaskSinceBotSizeIsZero() {
236239 .botSize (BigInteger .valueOf (0 ))
237240 .build ();
238241
242+ initMocks ();
239243 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice ));
240- when (iexecHubService .getChainDeal (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
244+ when (iexecHubService .getChainDealWithDetails (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
241245 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (from );
242246
243247 dealWatcherService .subscribeToDealEventFromOneBlockToLatest (from );
@@ -257,8 +261,9 @@ void shouldUpdateLastSeenBlockWhenOneDealButNotCreateTaskSinceExceptionThrown()
257261 .botSize (BigInteger .valueOf (1 ))
258262 .build ();
259263
264+ initMocks ();
260265 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice ));
261- when (iexecHubService .getChainDeal (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
266+ when (iexecHubService .getChainDealWithDetails (BytesUtils .bytesToString (schedulerNotice .dealid ))).thenReturn (Optional .of (chainDeal ));
262267 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (from );
263268
264269 dealWatcherService .subscribeToDealEventFromOneBlockToLatest (from );
@@ -275,6 +280,7 @@ void shouldUpdateLastSeenBlockTwiceWhenTwoDeals() {
275280 IexecHubContract .SchedulerNoticeEventResponse schedulerNotice1 = createSchedulerNotice (blockOfDeal1 );
276281 IexecHubContract .SchedulerNoticeEventResponse schedulerNotice2 = createSchedulerNotice (blockOfDeal2 );
277282
283+ initMocks ();
278284 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (from );
279285 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice1 , schedulerNotice2 ));
280286
@@ -290,6 +296,7 @@ void shouldNotUpdateLastSeenBlockWhenReceivingOldMissedDeal() {
290296 BigInteger blockOfDeal = BigInteger .valueOf (3 );
291297 IexecHubContract .SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice (blockOfDeal );
292298
299+ initMocks ();
293300 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (from );
294301 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice ));
295302
@@ -306,13 +313,14 @@ void shouldReplayAllEventInRange() {
306313 BigInteger blockOfDeal = BigInteger .valueOf (3 );
307314 IexecHubContract .SchedulerNoticeEventResponse schedulerNotice = createSchedulerNotice (blockOfDeal );
308315
316+ initMocks ();
309317 when (configurationService .getLastSeenBlockWithDeal ()).thenReturn (BigInteger .TEN );
310318 when (configurationService .getFromReplay ()).thenReturn (BigInteger .ZERO );
311319 when (iexecHubService .getDealEventObservable (any ())).thenReturn (Flowable .just (schedulerNotice ));
312320
313321 dealWatcherService .replayDealEvent ();
314322
315- verify (iexecHubService ).getChainDeal (any ());
323+ verify (iexecHubService ).getChainDealWithDetails (any ());
316324 Counter lastBlockCounter = Metrics .globalRegistry .find (DealWatcherService .METRIC_DEALS_LAST_BLOCK ).counter ();
317325 Counter dealsReplayCounter = Metrics .globalRegistry .find (DealWatcherService .METRIC_DEALS_REPLAY_COUNT ).counter ();
318326 Assertions .assertThat (lastBlockCounter ).isNotNull ();
0 commit comments