Added oparent to sdc main
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / item-rest / item-rest-services / src / test / java / org / openecomp / sdcrests / item / rest / services / catalog / notification / AsyncNotifierTest.java
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.junit.Assert.assertTrue;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyLong;
23 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.DONE;
24 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.RETRY;
25
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.UUID;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.ScheduledExecutorService;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.TimeoutException;
35 import java.util.function.BiFunction;
36 import org.apache.commons.lang.mutable.MutableInt;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.mockito.Mockito;
41 import org.mockito.stubbing.Answer;
42 import org.openecomp.sdcrests.item.types.ItemAction;
43
44 /**
45  * @author evitaliy
46  * @since 22 Nov 2018
47  */
48 public class AsyncNotifierTest {
49
50     private static final String NUMBER_OF_RETRIES_MESSAGE = "Number of retries must be positive";
51     private static final String DELAY_MESSAGE = "Delay must be positive";
52
53     @Rule
54     public ExpectedException exception = ExpectedException.none();
55
56     @Test(expected = NullPointerException.class)
57     public void errorWhenWorkerNull() {
58         new AsyncNotifier.RetryingTask(null, 10, 10, Mockito.mock(ScheduledExecutorService.class));
59     }
60
61     @Test(expected = NullPointerException.class)
62     public void errorWhenSchedulerServiceNull() {
63         new AsyncNotifier.RetryingTask(() -> DONE, 10, 10, null);
64     }
65
66     @Test
67     public void errorWhenNumberOfRetriesNegative() {
68         exception.expect(IllegalArgumentException.class);
69         exception.expectMessage(NUMBER_OF_RETRIES_MESSAGE);
70         new AsyncNotifier.RetryingTask(() -> DONE, -12, 10, Mockito.mock(ScheduledExecutorService.class));
71     }
72
73     @Test
74     public void errorWhenNumberOfRetriesZero() {
75         exception.expect(IllegalArgumentException.class);
76         exception.expectMessage(NUMBER_OF_RETRIES_MESSAGE);
77         new AsyncNotifier.RetryingTask(() -> DONE, 0, 10, Mockito.mock(ScheduledExecutorService.class));
78     }
79
80     @Test
81     public void errorWhenDelayNegative() {
82         exception.expect(IllegalArgumentException.class);
83         exception.expectMessage(DELAY_MESSAGE);
84         new AsyncNotifier.RetryingTask(() -> DONE, 1, -77, Mockito.mock(ScheduledExecutorService.class));
85     }
86
87     @Test
88     public void errorWhenDelayZero() {
89         exception.expect(IllegalArgumentException.class);
90         exception.expectMessage(DELAY_MESSAGE);
91         new AsyncNotifier.RetryingTask(() -> DONE, 1, 0, Mockito.mock(ScheduledExecutorService.class));
92     }
93
94     @Test
95     public void taskRunsOnceWhenSuccessful() throws Exception {
96
97         ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
98
99         MutableInt counter = new MutableInt(0);
100         Callable<AsyncNotifier.NextAction> countingTask = () -> {
101             counter.increment();
102             return DONE;
103         };
104
105         AsyncNotifier.RetryingTask retryingTask =
106                 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
107         retryingTask.call();
108         assertEquals(1, counter.intValue());
109     }
110
111     private ScheduledExecutorService createMockScheduledExecutor() {
112
113         ScheduledExecutorService executorServiceMock = Mockito.mock(ScheduledExecutorService.class);
114         Answer passThrough = invocation -> {
115             ((Callable<?>) invocation.getArgument(0)).call();
116             return null;
117         };
118
119         Mockito.doAnswer(passThrough).when(executorServiceMock).submit(any(Callable.class));
120         Mockito.doAnswer(passThrough).when(executorServiceMock)
121                 .schedule(any(Callable.class), anyLong(), any(TimeUnit.class));
122         return executorServiceMock;
123     }
124
125     @Test
126     public void taskRunsTwiceWhenFailedFirstTime() throws Exception {
127
128         ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
129
130         MutableInt counter = new MutableInt(0);
131         Callable<AsyncNotifier.NextAction> countingTask = () -> {
132             counter.increment();
133             return counter.intValue() < 2 ? RETRY : DONE;
134         };
135
136         AsyncNotifier.RetryingTask retryingTask =
137                 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
138         retryingTask.call();
139         assertEquals(2, counter.intValue());
140     }
141
142     @Test
143     public void exhaustedAttemptsWhenTaskAlwaysFails() throws Exception {
144
145         ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
146
147         MutableInt counter = new MutableInt(0);
148         Callable<AsyncNotifier.NextAction> countingTask = () -> {
149             counter.increment();
150             return RETRY;
151         };
152
153         final int numOfRetries = 10;
154         AsyncNotifier.RetryingTask retryingTask =
155                 new AsyncNotifier.RetryingTask(countingTask, numOfRetries, 10, executorServiceMock);
156         retryingTask.call();
157         assertEquals(numOfRetries, counter.intValue());
158     }
159
160     @Test
161     public void workerExecutedWithGivenItemIdsAndAction()
162             throws InterruptedException, ExecutionException, TimeoutException {
163
164         CompletableFuture<Boolean> completed = new CompletableFuture<>();
165         Callable<AsyncNotifier.NextAction> mockTask = () -> {
166             completed.complete(true);
167             return DONE;
168         };
169
170         final Collection<String> itemIds = Collections.singleton(UUID.randomUUID().toString());
171         final ItemAction action = ItemAction.RESTORE;
172
173         BiFunction<Collection<String>, ItemAction, Callable<AsyncNotifier.NextAction>> mockProducer = (i, a) -> {
174             assertEquals(itemIds, i);
175             assertEquals(action, a);
176             return mockTask;
177         };
178
179         new AsyncNotifier(mockProducer, 1, 1).execute(itemIds, action);
180         assertTrue(completed.get(5, TimeUnit.SECONDS));
181     }
182 }