2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.oam.util;
27 import com.att.eelf.configuration.EELFLogger;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.openecomp.appc.oam.AppcOam;
33 import org.openecomp.appc.oam.processor.BaseActionRunnable;
34 import org.powermock.reflect.Whitebox;
36 import java.util.Arrays;
37 import java.util.concurrent.Future;
38 import java.util.concurrent.ScheduledExecutorService;
39 import java.util.concurrent.ScheduledFuture;
40 import java.util.concurrent.TimeUnit;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.times;
45 public class AsyncTaskHelperTest {
46 private AsyncTaskHelper asyncTaskHelper;
47 private ScheduledExecutorService mockScheduler = mock(ScheduledExecutorService.class);
48 private BaseActionRunnable mockRunnable = mock(BaseActionRunnable.class);
51 public void setUp() throws Exception {
52 asyncTaskHelper = new AsyncTaskHelper(null);
54 Whitebox.setInternalState(asyncTaskHelper, "scheduledExecutorService", mockScheduler);
55 // to avoid operation on logger fail, mock up the logger
56 EELFLogger mockLogger = mock(EELFLogger.class);
57 Whitebox.setInternalState(asyncTaskHelper, "logger", mockLogger);
61 public void testClose() throws Exception {
62 asyncTaskHelper.close();
63 Mockito.verify(mockScheduler, times(1)).shutdown();
67 public void testGetCurrentAsyncTask() throws Exception {
68 Future<?> mockTask = mock(Future.class);
69 Whitebox.setInternalState(asyncTaskHelper, "backgroundOamTask", mockTask);
70 Assert.assertEquals("Should return mock task", mockTask, asyncTaskHelper.getCurrentAsyncTask());
74 public void testScheduleAsyncTaskWithMmod() throws Exception {
75 // test maintenance mode
76 ScheduledFuture<?> mockTask0 = mock(ScheduledFuture.class);
77 Whitebox.setInternalState(asyncTaskHelper, "backgroundOamTask", mockTask0);
79 ScheduledFuture<?> mockTask1 = mock(ScheduledFuture.class);
80 Mockito.doReturn(mockTask1).when(mockScheduler).scheduleWithFixedDelay(
81 mockRunnable, asyncTaskHelper.MMODE_TASK_DELAY,
82 asyncTaskHelper.MMODE_TASK_DELAY, TimeUnit.MILLISECONDS);
83 asyncTaskHelper.scheduleAsyncTask(AppcOam.RPC.maintenance_mode, mockRunnable);
84 Mockito.verify(mockTask0, times(1)).cancel(true);
85 Assert.assertEquals(mockTask1, asyncTaskHelper.scheduleAsyncTask(AppcOam.RPC.maintenance_mode, mockRunnable));
86 Assert.assertEquals("Should set backgroundOamTask", mockTask1, asyncTaskHelper.getCurrentAsyncTask());
90 public void testScheduleAsyncTaskWithStart() throws Exception {
91 for (AppcOam.RPC rpc : Arrays.asList(AppcOam.RPC.start, AppcOam.RPC.stop, AppcOam.RPC.restart)) {
96 private void runTest(AppcOam.RPC rpc) {
97 ScheduledFuture<?> mockTask0 = mock(ScheduledFuture.class);
98 Whitebox.setInternalState(asyncTaskHelper, "backgroundOamTask", mockTask0);
99 BaseActionRunnable mockRunnable0 = mock(BaseActionRunnable.class);
100 Whitebox.setInternalState(asyncTaskHelper, "taskRunnable", mockRunnable0);
102 ScheduledFuture<?> mockTask2 = mock(ScheduledFuture.class);
103 Mockito.doReturn(mockTask2).when(mockScheduler).scheduleWithFixedDelay(
104 mockRunnable, asyncTaskHelper.COMMON_INITIAL_DELAY,
105 asyncTaskHelper.COMMON_INTERVAL, TimeUnit.MILLISECONDS);
106 asyncTaskHelper.scheduleAsyncTask(rpc, mockRunnable);
107 Mockito.verify(mockTask0, times(1)).cancel(true);
108 Mockito.verify(mockRunnable0, times(1)).abortRunnable(rpc);
109 Assert.assertEquals(mockTask2, asyncTaskHelper.scheduleAsyncTask(rpc, mockRunnable));
110 Assert.assertEquals("Should set backgroundOamTask", mockTask2, asyncTaskHelper.getCurrentAsyncTask());
114 public void testCancelAsyncTask() throws Exception {
115 Future<?> mockTask = mock(Future.class);
116 Whitebox.setInternalState(asyncTaskHelper, "backgroundOamTask", mockTask);
117 asyncTaskHelper.cancelAsyncTask(mockTask);
118 Mockito.verify(mockTask, times(1)).cancel(false);
119 Assert.assertTrue("Should have reset backgroundOamTask",
120 asyncTaskHelper.getCurrentAsyncTask() == null);
123 Whitebox.setInternalState(asyncTaskHelper, "backgroundOamTask", mockTask);
124 Future<?> mockTask2 = mock(Future.class);
125 asyncTaskHelper.cancelAsyncTask(mockTask2);
126 Mockito.verify(mockTask2, times(1)).cancel(false);
127 Assert.assertEquals("Should not reset backgroundOamTask",
128 mockTask, asyncTaskHelper.getCurrentAsyncTask());