900fc940eedbf7f7003a514108f8c16df9b776c0
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
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 org.openecomp.sdcrests.item.rest.services.catalog.notification;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.anyLong;
22 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.DONE;
23 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.RETRY;
24
25 import java.util.concurrent.Callable;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.TimeUnit;
28 import org.apache.commons.lang.mutable.MutableInt;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.mockito.stubbing.Answer;
32
33 /**
34  * @author evitaliy
35  * @since 22 Nov 2018
36  */
37 public class AsyncNotifierTest {
38
39     @Test
40     public void taskRunsOnceWhenSuccessful() throws Exception {
41
42         ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
43
44         MutableInt counter = new MutableInt(0);
45         Callable<AsyncNotifier.NextAction> countingTask = () -> {
46             counter.increment();
47             return DONE;
48         };
49
50         AsyncNotifier.RetryingTask retryingTask =
51                 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
52         retryingTask.call();
53         assertEquals(1, counter.intValue());
54     }
55
56     @Test
57     public void taskRunsTwiceWhenFailedFirstTime() throws Exception {
58
59         ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
60
61         MutableInt counter = new MutableInt(0);
62         Callable<AsyncNotifier.NextAction> countingTask = () -> {
63             counter.increment();
64             return counter.intValue() < 2 ? RETRY : DONE;
65         };
66
67         AsyncNotifier.RetryingTask retryingTask =
68                 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
69         retryingTask.call();
70         assertEquals(2, counter.intValue());
71     }
72
73     @Test
74     public void exhaustedAttemptsWhenTaskAlwaysFails() throws Exception {
75
76         ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
77
78         MutableInt counter = new MutableInt(0);
79         Callable<AsyncNotifier.NextAction> countingTask = () -> {
80             counter.increment();
81             return RETRY;
82         };
83
84         final int numOfRetries = 10;
85         AsyncNotifier.RetryingTask retryingTask =
86                 new AsyncNotifier.RetryingTask(countingTask, numOfRetries, 10, executorServiceMock);
87         retryingTask.call();
88         assertEquals(numOfRetries, counter.intValue());
89     }
90
91     private ScheduledExecutorService createMockScheduledExecutor() {
92
93         ScheduledExecutorService executorServiceMock = Mockito.mock(ScheduledExecutorService.class);
94         Answer passThrough = invocation -> {
95             ((Callable<?>) invocation.getArgument(0)).call();
96             return null;
97         };
98
99         Mockito.doAnswer(passThrough).when(executorServiceMock).submit(any(Callable.class));
100         Mockito.doAnswer(passThrough).when(executorServiceMock)
101                 .schedule(any(Callable.class), anyLong(), any(TimeUnit.class));
102         return executorServiceMock;
103     }
104 }