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.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;
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;
48 public class AsyncNotifierTest {
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";
54 public ExpectedException exception = ExpectedException.none();
56 @Test(expected = NullPointerException.class)
57 public void errorWhenWorkerNull() {
58 new AsyncNotifier.RetryingTask(null, 10, 10, Mockito.mock(ScheduledExecutorService.class));
61 @Test(expected = NullPointerException.class)
62 public void errorWhenSchedulerServiceNull() {
63 new AsyncNotifier.RetryingTask(() -> DONE, 10, 10, null);
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));
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));
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));
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));
95 public void taskRunsOnceWhenSuccessful() throws Exception {
97 ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
99 MutableInt counter = new MutableInt(0);
100 Callable<AsyncNotifier.NextAction> countingTask = () -> {
105 AsyncNotifier.RetryingTask retryingTask =
106 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
108 assertEquals(1, counter.intValue());
111 private ScheduledExecutorService createMockScheduledExecutor() {
113 ScheduledExecutorService executorServiceMock = Mockito.mock(ScheduledExecutorService.class);
114 Answer passThrough = invocation -> {
115 ((Callable<?>) invocation.getArgument(0)).call();
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;
126 public void taskRunsTwiceWhenFailedFirstTime() throws Exception {
128 ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
130 MutableInt counter = new MutableInt(0);
131 Callable<AsyncNotifier.NextAction> countingTask = () -> {
133 return counter.intValue() < 2 ? RETRY : DONE;
136 AsyncNotifier.RetryingTask retryingTask =
137 new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock);
139 assertEquals(2, counter.intValue());
143 public void exhaustedAttemptsWhenTaskAlwaysFails() throws Exception {
145 ScheduledExecutorService executorServiceMock = createMockScheduledExecutor();
147 MutableInt counter = new MutableInt(0);
148 Callable<AsyncNotifier.NextAction> countingTask = () -> {
153 final int numOfRetries = 10;
154 AsyncNotifier.RetryingTask retryingTask =
155 new AsyncNotifier.RetryingTask(countingTask, numOfRetries, 10, executorServiceMock);
157 assertEquals(numOfRetries, counter.intValue());
161 public void workerExecutedWithGivenItemIdsAndAction()
162 throws InterruptedException, ExecutionException, TimeoutException {
164 CompletableFuture<Boolean> completed = new CompletableFuture<>();
165 Callable<AsyncNotifier.NextAction> mockTask = () -> {
166 completed.complete(true);
170 final Collection<String> itemIds = Collections.singleton(UUID.randomUUID().toString());
171 final ItemAction action = ItemAction.RESTORE;
173 BiFunction<Collection<String>, ItemAction, Callable<AsyncNotifier.NextAction>> mockProducer = (i, a) -> {
174 assertEquals(itemIds, i);
175 assertEquals(action, a);
179 new AsyncNotifier(mockProducer, 1, 1).execute(itemIds, action);
180 assertTrue(completed.get(5, TimeUnit.SECONDS));