2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdcrests.item.rest.services.catalog.notification;
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;
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;
37 public class AsyncNotifierTest {
40 public void taskRunsOnceWhenSuccessful() throws Exception {
42 ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
44 MutableInt counter = new MutableInt(0);
45 Callable<AsyncNotifier.NextAction> countingTask = () -> {
50 AsyncNotifier.RetryingTask retryingTask =
51 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
53 assertEquals(1, counter.intValue());
57 public void taskRunsTwiceWhenFailedFirstTime() throws Exception {
59 ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
61 MutableInt counter = new MutableInt(0);
62 Callable<AsyncNotifier.NextAction> countingTask = () -> {
64 return counter.intValue() < 2 ? RETRY : DONE;
67 AsyncNotifier.RetryingTask retryingTask =
68 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
70 assertEquals(2, counter.intValue());
74 public void exhaustedAttemptsWhenTaskAlwaysFails() throws Exception {
76 ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
78 MutableInt counter = new MutableInt(0);
79 Callable<AsyncNotifier.NextAction> countingTask = () -> {
84 final int numOfRetries = 10;
85 AsyncNotifier.RetryingTask retryingTask =
86 new AsyncNotifier.RetryingTask(countingTask, numOfRetries, 10, executorServiceMock);
88 assertEquals(numOfRetries, counter.intValue());
91 private ScheduledExecutorService createMockScheduledExecutor() {
93 ScheduledExecutorService executorServiceMock = Mockito.mock(ScheduledExecutorService.class);
94 Answer passThrough = invocation -> {
95 ((Callable<?>) invocation.getArgument(0)).call();
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;