OAM operations 3 - JUnit tests
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / openecomp / appc / oam / processor / BaseActionRunnableTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25
26 package org.openecomp.appc.oam.processor;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader;
34 import org.openecomp.appc.configuration.Configuration;
35 import org.openecomp.appc.i18n.Msg;
36 import org.openecomp.appc.oam.AppcOam;
37 import org.openecomp.appc.oam.OAMCommandStatus;
38 import org.openecomp.appc.oam.util.AsyncTaskHelper;
39 import org.openecomp.appc.oam.util.ConfigurationHelper;
40 import org.openecomp.appc.oam.util.OperationHelper;
41 import org.openecomp.appc.oam.util.StateHelper;
42 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
43 import org.powermock.reflect.Whitebox;
44
45 import java.util.Date;
46
47 import static org.mockito.Matchers.any;
48 import static org.mockito.Matchers.anyInt;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.spy;
51 import static org.mockito.Mockito.times;
52
53 public class BaseActionRunnableTest {
54     private AppcOam.RPC testRpc = AppcOam.RPC.maintenance_mode;
55     private AppcOamStates targetState = AppcOamStates.MaintenanceMode;
56
57     private class TestProcessor extends BaseProcessor {
58         /**
59          * Constructor
60          *
61          * @param eelfLogger            for logging
62          * @param configurationHelperIn for property reading
63          * @param stateHelperIn         for APP-C OAM state checking
64          * @param asyncTaskHelperIn     for scheduling async task
65          * @param operationHelperIn     for operational helper
66          */
67         TestProcessor(EELFLogger eelfLogger,
68                       ConfigurationHelper configurationHelperIn,
69                       StateHelper stateHelperIn,
70                       AsyncTaskHelper asyncTaskHelperIn,
71                       OperationHelper operationHelperIn) {
72             super(eelfLogger, configurationHelperIn, stateHelperIn, asyncTaskHelperIn, operationHelperIn);
73
74             // must set rpc and auditMsg
75             rpc = testRpc;
76             auditMsg = Msg.OAM_OPERATION_STARTING;
77             startTime = new Date();
78         }
79     }
80
81     class TestAbc extends BaseActionRunnable {
82         boolean doActionResult;
83
84         TestAbc(BaseProcessor parent) {
85             super(parent);
86
87             actionName = "testing";
88             auditMsg = Msg.OAM_OPERATION_MAINTENANCE_MODE;
89             finalState = targetState;
90         }
91
92         @Override
93         boolean doAction() {
94             return doActionResult;
95         }
96     }
97
98     private TestAbc testBaseAcionRunnable;
99     private BaseProcessor testProcessor;
100     private StateHelper mockStateHelper = mock(StateHelper.class);
101     private OperationHelper mockOperHelper = mock(OperationHelper.class);
102     private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class);
103     private Configuration mockConfig = mock(Configuration.class);
104
105     @SuppressWarnings("ResultOfMethodCallIgnored")
106     @Before
107     public void setUp() throws Exception {
108         // to avoid operation on logger fail, mock up the logger
109         EELFLogger mockLogger = mock(EELFLogger.class);
110
111         Mockito.doReturn(mockConfig).when(mockConfigHelper).getConfig();
112         Mockito.doReturn(10).when(mockConfig).getIntegerProperty(any(), anyInt());
113
114         testProcessor = spy(
115                 new TestProcessor(mockLogger, mockConfigHelper, mockStateHelper, null, mockOperHelper));
116         testBaseAcionRunnable = spy(new TestAbc(testProcessor));
117
118         Whitebox.setInternalState(testBaseAcionRunnable, "commonHeader", mock(CommonHeader.class));
119     }
120
121     @Test
122     public void testSetTimeoutValues() throws Exception {
123         Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", 0);
124         Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 0);
125         Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
126         testBaseAcionRunnable.setTimeoutValues();
127         Assert.assertEquals("Should set timeoutMs", 10 * 1000, testBaseAcionRunnable.timeoutMs);
128         Assert.assertTrue("Should set start time MS", testBaseAcionRunnable.startTimeMs != 0);
129         Assert.assertTrue("Should do check", testBaseAcionRunnable.doTimeoutChecking);
130
131         Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", 0);
132         Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 0);
133         Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
134         int timeoutSeconds = 20;
135         Whitebox.setInternalState(testProcessor, "timeoutSeconds", timeoutSeconds);
136         testBaseAcionRunnable.setTimeoutValues();
137         Assert.assertEquals("Should set timeoutMs", timeoutSeconds * 1000, testBaseAcionRunnable.timeoutMs);
138         Assert.assertTrue("Should set start time MS", testBaseAcionRunnable.startTimeMs != 0);
139         Assert.assertTrue("Should do check", testBaseAcionRunnable.doTimeoutChecking);
140
141         Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", 0);
142         Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 0);
143         Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
144
145         timeoutSeconds = 0;
146         Whitebox.setInternalState(testProcessor, "timeoutSeconds", timeoutSeconds);
147         Mockito.doReturn(0).when(mockConfig).getIntegerProperty(
148                 testBaseAcionRunnable.OAM_OPERATION_TIMEOUT_SECOND, testBaseAcionRunnable.DEFAULT_OAM_OPERATION_TIMEOUT);
149         testBaseAcionRunnable.setTimeoutValues();
150         Assert.assertEquals("Should set timeoutMs", timeoutSeconds * 1000, testBaseAcionRunnable.timeoutMs);
151         Assert.assertTrue("Should not set start time MS", testBaseAcionRunnable.startTimeMs == 0);
152         Assert.assertFalse("Should not do check", testBaseAcionRunnable.doTimeoutChecking);
153     }
154     @Test
155     public void testRun() throws Exception {
156         // test doAction failed
157         Whitebox.setInternalState(testBaseAcionRunnable, "doActionResult", false);
158         testBaseAcionRunnable.run();
159         Assert.assertFalse("isWaiting should still be false",
160                 Whitebox.getInternalState(testBaseAcionRunnable, "isWaiting"));
161
162         // test doAction success
163         Whitebox.setInternalState(testBaseAcionRunnable, "doActionResult", true);
164
165         // with checkState return true
166         Mockito.doReturn(targetState).when(mockStateHelper).getBundlesState();
167         testBaseAcionRunnable.run();
168         Assert.assertFalse("isWaiting should still be false",
169                 Whitebox.getInternalState(testBaseAcionRunnable, "isWaiting"));
170
171         // with checkState return false
172         Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getBundlesState();
173         testBaseAcionRunnable.run();
174         Assert.assertTrue("isWaiting should still be true",
175                 Whitebox.getInternalState(testBaseAcionRunnable, "isWaiting"));
176
177         // should stay
178         testBaseAcionRunnable.run();
179         Mockito.verify(testBaseAcionRunnable, times(1)).keepWaiting();
180     }
181
182     @Test
183     public void testSetAbortStatus() throws Exception {
184         testBaseAcionRunnable.setAbortStatus();
185         Assert.assertEquals("Should return reject code", OAMCommandStatus.REJECTED.getResponseCode(),
186                 testBaseAcionRunnable.status.getCode().intValue());
187         Assert.assertTrue("Should set abort message",
188                 testBaseAcionRunnable.status.getMessage().endsWith(
189                         String.format(testBaseAcionRunnable.ABORT_MESSAGE_FORMAT, testRpc.name())));
190     }
191
192     @Test
193     public void testCheckState() throws Exception {
194         Mockito.doReturn(targetState).when(mockStateHelper).getBundlesState();
195         Assert.assertTrue("Should return true", testBaseAcionRunnable.checkState());
196
197         Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getBundlesState();
198         Assert.assertFalse("Should return false", testBaseAcionRunnable.checkState());
199     }
200
201     @Test
202     public void testPostAction() throws Exception {
203         Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState();
204         // set status to avoid NPE when using status
205         testBaseAcionRunnable.setAbortStatus();
206
207         // test no parameter
208         testBaseAcionRunnable.postAction(null);
209         Mockito.verify(mockOperHelper, times(1)).sendNotificationMessage(any(), any(), any());
210         Mockito.verify(mockStateHelper, times(0)).setState(any());
211         Mockito.verify(testProcessor, times(1)).cancelAsyncTask();
212
213         // test with parameter
214         testBaseAcionRunnable.postAction(AppcOamStates.Error);
215         Mockito.verify(mockOperHelper, times(2)).sendNotificationMessage(any(), any(), any());
216         Mockito.verify(mockStateHelper, times(1)).setState(any());
217         Mockito.verify(testProcessor, times(2)).cancelAsyncTask();
218     }
219
220     @Test
221     public void testIsTimeout() throws Exception {
222         String parentName = "testing";
223         Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", false);
224         Assert.assertFalse("Should not be timeout", testBaseAcionRunnable.isTimeout(parentName));
225
226         Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState();
227         Whitebox.setInternalState(testBaseAcionRunnable, "doTimeoutChecking", true);
228         Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", System.currentTimeMillis() + 100);
229         Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 2);
230         Assert.assertFalse("Should not be timeout", testBaseAcionRunnable.isTimeout(parentName));
231
232         long timeoutMs = 1;
233         Whitebox.setInternalState(testBaseAcionRunnable, "timeoutMs", timeoutMs);
234         Whitebox.setInternalState(testBaseAcionRunnable, "startTimeMs", 2);
235         Assert.assertTrue("Should be timeout", testBaseAcionRunnable.isTimeout(parentName));
236         Mockito.verify(testBaseAcionRunnable, times(1)).postAction(any());
237         Assert.assertEquals("Should return timeout code", OAMCommandStatus.TIMEOUT.getResponseCode(),
238                 testBaseAcionRunnable.status.getCode().intValue());
239         Assert.assertTrue("Should set timeout message",
240                 testBaseAcionRunnable.status.getMessage().endsWith(
241                         String.format(testBaseAcionRunnable.TIMEOUT_MESSAGE_FORMAT, testRpc.name(), timeoutMs)));
242     }
243 }