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.processor;
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.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader;
33 import org.openecomp.appc.i18n.Msg;
34 import org.openecomp.appc.oam.AppcOam;
35 import org.openecomp.appc.oam.OAMCommandStatus;
36 import org.openecomp.appc.oam.util.AsyncTaskHelper;
37 import org.openecomp.appc.oam.util.BundleHelper;
38 import org.openecomp.appc.oam.util.ConfigurationHelper;
39 import org.openecomp.appc.oam.util.OperationHelper;
40 import org.openecomp.appc.oam.util.StateHelper;
41 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
42 import org.powermock.reflect.Whitebox;
44 import java.util.Date;
46 import static org.mockito.Matchers.any;
47 import static org.mockito.Matchers.anyMap;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.spy;
50 import static org.mockito.Mockito.times;
52 public class BaseActionRunnableTest {
53 private AppcOam.RPC testRpc = AppcOam.RPC.maintenance_mode;
54 private AppcOamStates targetState = AppcOamStates.MaintenanceMode;
56 private class TestProcessor extends BaseProcessor {
60 * @param eelfLogger for logging
61 * @param configurationHelperIn for property reading
62 * @param stateHelperIn for APP-C OAM state checking
63 * @param asyncTaskHelperIn for scheduling async task
64 * @param operationHelperIn for operational helper
66 TestProcessor(EELFLogger eelfLogger,
67 ConfigurationHelper configurationHelperIn,
68 StateHelper stateHelperIn,
69 AsyncTaskHelper asyncTaskHelperIn,
70 OperationHelper operationHelperIn) {
71 super(eelfLogger, configurationHelperIn, stateHelperIn, asyncTaskHelperIn, operationHelperIn);
73 // must set rpc and auditMsg
75 auditMsg = Msg.OAM_OPERATION_STARTING;
76 startTime = new Date();
80 class TestAbc extends BaseActionRunnable {
81 boolean doActionResult;
83 TestAbc(BaseProcessor parent) {
86 actionName = "testing";
87 auditMsg = Msg.OAM_OPERATION_MAINTENANCE_MODE;
88 finalState = targetState;
93 return doActionResult;
97 private TestAbc testBaseAcionRunnable;
98 private BaseProcessor testProcessor;
99 private StateHelper mockStateHelper = mock(StateHelper.class);
100 private OperationHelper mockOperHelper = mock(OperationHelper.class);
101 private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class);
102 private BundleHelper mockBundleHelper = mock(BundleHelper.class);
104 @SuppressWarnings("ResultOfMethodCallIgnored")
106 public void setUp() throws Exception {
107 // to avoid operation on logger fail, mock up the logger
108 EELFLogger mockLogger = mock(EELFLogger.class);
111 new TestProcessor(mockLogger, mockConfigHelper, mockStateHelper, null, mockOperHelper));
112 Whitebox.setInternalState(testProcessor, "bundleHelper", mockBundleHelper);
114 testBaseAcionRunnable = spy(new TestAbc(testProcessor));
115 Whitebox.setInternalState(testBaseAcionRunnable, "commonHeader", mock(CommonHeader.class));
119 public void testSetTimeoutValues() throws Exception {
120 Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", 0);
121 Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 0);
122 Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
123 long expectedTimeout = 10000L;
124 Mockito.doReturn(expectedTimeout).when(mockConfigHelper).getOAMOperationTimeoutValue(any());
125 testBaseAcionRunnable.setTimeoutValues();
126 Assert.assertEquals("Should set timeoutMs", expectedTimeout, testBaseAcionRunnable.timeoutMs);
127 Assert.assertTrue("Should set start time MS", testBaseAcionRunnable.startTimeMs != 0);
128 Assert.assertTrue("Should do check", testBaseAcionRunnable.doTimeoutChecking);
130 Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", 0);
131 Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 0);
132 Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
133 expectedTimeout = 20000L;
134 Mockito.doReturn(expectedTimeout).when(mockConfigHelper).getOAMOperationTimeoutValue(any());
135 testBaseAcionRunnable.setTimeoutValues();
136 Assert.assertEquals("Should set timeoutMs", expectedTimeout, testBaseAcionRunnable.timeoutMs);
137 Assert.assertTrue("Should set start time MS", testBaseAcionRunnable.startTimeMs != 0);
138 Assert.assertTrue("Should do check", testBaseAcionRunnable.doTimeoutChecking);
140 Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", 0);
141 Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 0);
142 Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
143 expectedTimeout = 0L;
144 Mockito.doReturn(expectedTimeout).when(mockConfigHelper).getOAMOperationTimeoutValue(any());
145 testBaseAcionRunnable.setTimeoutValues();
146 Assert.assertEquals("Should set timeoutMs", expectedTimeout, testBaseAcionRunnable.timeoutMs);
147 Assert.assertTrue("Should not set start time MS", testBaseAcionRunnable.startTimeMs == 0);
148 Assert.assertFalse("Should not do check", testBaseAcionRunnable.doTimeoutChecking);
152 public void testRun() throws Exception {
153 // test doAction failed
154 Whitebox.setInternalState(testBaseAcionRunnable, "doActionResult", false);
155 testBaseAcionRunnable.run();
156 Assert.assertFalse("isWaiting should still be false",
157 Whitebox.getInternalState(testBaseAcionRunnable, "isWaiting"));
159 // test doAction success
160 Whitebox.setInternalState(testBaseAcionRunnable, "doActionResult", true);
162 // with checkState return true
163 Mockito.doReturn(true).when(testBaseAcionRunnable).checkState();
164 testBaseAcionRunnable.run();
165 Assert.assertFalse("isWaiting should still be false",
166 Whitebox.getInternalState(testBaseAcionRunnable, "isWaiting"));
168 // with checkState return false
169 Mockito.doReturn(false).when(testBaseAcionRunnable).checkState();
170 testBaseAcionRunnable.run();
171 Assert.assertTrue("isWaiting should still be true",
172 Whitebox.getInternalState(testBaseAcionRunnable, "isWaiting"));
175 testBaseAcionRunnable.run();
176 Mockito.verify(testBaseAcionRunnable, times(1)).keepWaiting();
180 public void testSetAbortStatus() throws Exception {
181 testBaseAcionRunnable.setAbortStatus();
182 Assert.assertEquals("Should return abort code", OAMCommandStatus.ABORT.getResponseCode(),
183 testBaseAcionRunnable.status.getCode().intValue());
184 Assert.assertTrue("Should set abort due to execution error message",
185 testBaseAcionRunnable.status.getMessage().endsWith(
186 String.format(testBaseAcionRunnable.ABORT_MESSAGE_FORMAT,
187 testRpc.name(), testBaseAcionRunnable.DUE_TO_EXECUTION_ERROR)));
191 public void testCheckState() throws Exception {
192 // 1. with isTimeout true
193 Mockito.doReturn(true).when(testBaseAcionRunnable).isTimeout("checkState");
194 Assert.assertTrue("Should return true", testBaseAcionRunnable.checkState());
196 // 2. with isTimeout false and
197 Mockito.doReturn(false).when(testBaseAcionRunnable).isTimeout("checkState");
199 // 2.1 with task not all done
200 Mockito.doReturn(false).when(mockBundleHelper).isAllTaskDone(any());
201 Assert.assertFalse("Should return false", testBaseAcionRunnable.checkState());
203 // 2. 2 with task all done
204 Mockito.doReturn(true).when(mockBundleHelper).isAllTaskDone(any());
206 // 2.2.1 with has bundle failure
207 Mockito.doReturn(true).when(testBaseAcionRunnable).hasBundleOperationFailure();
208 Assert.assertTrue("Should return true", testBaseAcionRunnable.checkState());
210 // 2.2.2 with no bundle failure
211 Mockito.doReturn(false).when(testBaseAcionRunnable).hasBundleOperationFailure();
213 Mockito.doReturn(targetState).when(mockStateHelper).getBundlesState();
214 Assert.assertTrue("Should return true", testBaseAcionRunnable.checkState());
216 Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getBundlesState();
217 Assert.assertFalse("Should return false", testBaseAcionRunnable.checkState());
221 public void testPostAction() throws Exception {
222 Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState();
223 // set status to avoid NPE when using status
224 testBaseAcionRunnable.setAbortStatus();
227 testBaseAcionRunnable.postAction(null);
228 Mockito.verify(mockOperHelper, times(1)).sendNotificationMessage(any(), any(), any());
229 Mockito.verify(mockStateHelper, times(0)).setState(any());
230 Mockito.verify(testProcessor, times(1)).cancelAsyncTask();
232 // test with parameter
233 testBaseAcionRunnable.postAction(AppcOamStates.Error);
234 Mockito.verify(mockOperHelper, times(2)).sendNotificationMessage(any(), any(), any());
235 Mockito.verify(mockStateHelper, times(1)).setState(any());
236 Mockito.verify(testProcessor, times(2)).cancelAsyncTask();
240 public void testIsTimeout() throws Exception {
241 String parentName = "testing";
242 Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
243 Assert.assertFalse("Should not be timeout", testBaseAcionRunnable.isTimeout(parentName));
245 Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState();
246 Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", true);
247 Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", System.currentTimeMillis() + 100);
248 Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 2);
249 Assert.assertFalse("Should not be timeout", testBaseAcionRunnable.isTimeout(parentName));
252 Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", timeoutMs);
253 Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 2);
254 Assert.assertTrue("Should be timeout", testBaseAcionRunnable.isTimeout(parentName));
255 Mockito.verify(testBaseAcionRunnable, times(1)).postAction(any());
256 Assert.assertEquals("Should return timeout code", OAMCommandStatus.TIMEOUT.getResponseCode(),
257 testBaseAcionRunnable.status.getCode().intValue());
258 Assert.assertTrue("Should set timeout message",
259 testBaseAcionRunnable.status.getMessage().endsWith(
260 String.format(testBaseAcionRunnable.TIMEOUT_MESSAGE_FORMAT, testRpc.name(), timeoutMs)));
263 @SuppressWarnings("unchecked")
265 public void testHasBundleOperationFailure() throws Exception {
266 Mockito.when(mockBundleHelper.getFailedMetrics(anyMap())).thenReturn(Long.valueOf("0"));
267 Assert.assertFalse("should return false", testBaseAcionRunnable.hasBundleOperationFailure());
269 Mockito.when(mockStateHelper.getCurrentOamState()).thenReturn(AppcOamStates.Restarting);
270 long failedNumber = 1;
271 Mockito.doReturn(failedNumber).when(mockBundleHelper).getFailedMetrics(anyMap());
272 Assert.assertTrue("should return true", testBaseAcionRunnable.hasBundleOperationFailure());
273 Mockito.verify(testBaseAcionRunnable, times(1)).setStatus(OAMCommandStatus.UNEXPECTED_ERROR,
274 String.format(testBaseAcionRunnable.BUNDLE_OPERATION_FAILED_FORMAT, failedNumber));
275 Mockito.verify(testBaseAcionRunnable, times(1)).postAction(AppcOamStates.Error);
279 public void testAbortRunnable() throws Exception {
280 Mockito.doReturn(AppcOamStates.Restarting).when(mockStateHelper).getCurrentOamState();
281 AppcOam.RPC newRpc = AppcOam.RPC.restart;
282 testBaseAcionRunnable.abortRunnable(newRpc);
283 Assert.assertEquals("Should return abort code", OAMCommandStatus.ABORT.getResponseCode(),
284 testBaseAcionRunnable.status.getCode().intValue());
285 Assert.assertTrue("Should set abort due to new request message",
286 testBaseAcionRunnable.status.getMessage().endsWith(
287 String.format(testBaseAcionRunnable.ABORT_MESSAGE_FORMAT, testRpc.name(),
288 String.format(testBaseAcionRunnable.NEW_RPC_OPERATION_REQUEST, newRpc.name()))));
289 Mockito.verify(mockOperHelper, times(1)).sendNotificationMessage(any(), any(), any());
290 Mockito.verify(testBaseAcionRunnable, times(1)).resetLogProperties(false);
291 Mockito.verify(testBaseAcionRunnable, times(1)).resetLogProperties(true);