[APPC-144] OAM operation abort messages
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / openecomp / appc / oam / processor / BaseProcessorTest.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.StartInput;
34 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader;
35 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.status.Status;
36 import org.openecomp.appc.exceptions.APPCException;
37 import org.openecomp.appc.exceptions.InvalidInputException;
38 import org.openecomp.appc.exceptions.InvalidStateException;
39 import org.openecomp.appc.i18n.Msg;
40 import org.openecomp.appc.oam.AppcOam;
41 import org.openecomp.appc.oam.OAMCommandStatus;
42 import org.openecomp.appc.oam.util.AsyncTaskHelper;
43 import org.openecomp.appc.oam.util.ConfigurationHelper;
44 import org.openecomp.appc.oam.util.OperationHelper;
45 import org.openecomp.appc.oam.util.StateHelper;
46 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
47 import org.powermock.reflect.Whitebox;
48
49 import java.util.concurrent.Future;
50
51 import static org.mockito.Matchers.any;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.spy;
54 import static org.mockito.Mockito.times;
55
56 @SuppressWarnings("ResultOfMethodCallIgnored")
57 public class BaseProcessorTest {
58     private AppcOam.RPC testRpc = AppcOam.RPC.start;
59     private AppcOamStates currentState = AppcOamStates.Stopped;
60
61     private class TestAbc extends BaseProcessor {
62
63         /**
64          * Constructor
65          *
66          * @param eelfLogger            for logging
67          * @param configurationHelperIn for property reading
68          * @param stateHelperIn         for APP-C OAM state checking
69          * @param asyncTaskHelperIn     for scheduling async task
70          * @param operationHelperIn     for operational helper
71          */
72         TestAbc(EELFLogger eelfLogger,
73                 ConfigurationHelper configurationHelperIn,
74                 StateHelper stateHelperIn,
75                 AsyncTaskHelper asyncTaskHelperIn,
76                 OperationHelper operationHelperIn) {
77             super(eelfLogger, configurationHelperIn, stateHelperIn, asyncTaskHelperIn, operationHelperIn);
78
79             // must set rpc and auditMsg
80             rpc = testRpc;
81             auditMsg = Msg.OAM_OPERATION_STARTING;
82         }
83     }
84
85     private TestAbc testBaseProcessor;
86     private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class);
87     private StateHelper mockStateHelper = mock(StateHelper.class);
88     private AsyncTaskHelper mockTaskHelper = mock(AsyncTaskHelper.class);
89     private OperationHelper mockOperHelper = mock(OperationHelper.class);
90
91     private StartInput mockInput = mock(StartInput.class);
92     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
93
94     @Before
95     public void setUp() throws Exception {
96         Mockito.doReturn(mockCommonHeader).when(mockOperHelper).getCommonHeader(mockInput);
97         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
98
99         testBaseProcessor = spy(
100                 new TestAbc(null, mockConfigHelper, mockStateHelper, mockTaskHelper, mockOperHelper));
101
102         Whitebox.setInternalState(testBaseProcessor, "commonHeader", mockCommonHeader);
103
104         // to avoid operation on logger fail, mock up the logger
105         EELFLogger mockLogger = mock(EELFLogger.class);
106         Whitebox.setInternalState(testBaseProcessor, "logger", mockLogger);
107     }
108
109     @Test
110     public void testProcessRequestError() throws Exception {
111         Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
112         Mockito.doThrow(new InvalidInputException("test")).when(mockOperHelper).isInputValid(mockInput);
113         Status status = testBaseProcessor.processRequest(mockInput);
114         Assert.assertEquals("Should return reject",
115                 OAMCommandStatus.INVALID_PARAMETER.getResponseCode(), status.getCode().intValue());
116     }
117
118     @Test
119     public void testProcessRequest() throws Exception {
120         Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
121         Mockito.doReturn(AppcOamStates.Starting).when(mockOperHelper).getNextState(any(), any());
122         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
123         Status status = testBaseProcessor.processRequest(mockInput);
124         Assert.assertEquals("Should return success",
125                 OAMCommandStatus.ACCEPTED.getResponseCode(), status.getCode().intValue());
126     }
127
128     @Test(expected = InvalidInputException.class)
129     public void testPreProcessWithInvalidInput() throws Exception {
130         Mockito.doThrow(new InvalidInputException("test")).when(mockOperHelper).isInputValid(mockInput);
131         testBaseProcessor.preProcess(mockInput);
132     }
133
134     @Test(expected = InvalidStateException.class)
135     public void testPreProcessWithInvalidState() throws Exception {
136         Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
137         Mockito.doThrow(new InvalidStateException("test"))
138                 .when(mockOperHelper).getNextState(testRpc.getAppcOperation(), currentState);
139         testBaseProcessor.preProcess(mockInput);
140     }
141
142     @Test(expected = APPCException.class)
143     public void testPreProcessWithAppcException() throws Exception {
144         Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
145         Mockito.doThrow(new APPCException("test"))
146                 .when(mockOperHelper).getNextState(testRpc.getAppcOperation(), currentState);
147         testBaseProcessor.preProcess(mockInput);
148     }
149
150     @Test
151     public void testPreProcess() throws Exception {
152         Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
153         AppcOamStates nextState = AppcOamStates.Starting;
154         Mockito.doReturn(nextState)
155                 .when(mockOperHelper).getNextState(testRpc.getAppcOperation(), currentState);
156         testBaseProcessor.preProcess(mockInput);
157         Mockito.verify(mockOperHelper, times(1)).isInputValid(mockInput);
158         Mockito.verify(mockOperHelper, times(1)).getNextState(testRpc.getAppcOperation(), currentState);
159         Mockito.verify(mockStateHelper, times(1)).setState(nextState);
160     }
161
162     @Test
163     public void testScheduleAsyncTask() throws Exception {
164         // test no runnable
165         testBaseProcessor.scheduleAsyncTask();
166         Mockito.verify(mockTaskHelper, times(0)).scheduleAsyncTask(any(), any());
167
168         BaseActionRunnable mockRunnable = mock(BaseActionRunnable.class);
169         Whitebox.setInternalState(testBaseProcessor, "runnable", mockRunnable);
170         testBaseProcessor.scheduleAsyncTask();
171         Mockito.verify(mockTaskHelper, times(1)).scheduleAsyncTask(testRpc, mockRunnable);
172     }
173
174     @Test
175     public void isSameAsyncTask() throws Exception {
176         Future<?> mockTask1 = mock(Future.class);
177         Whitebox.setInternalState(testBaseProcessor, "scheduledRunnable", mockTask1);
178         Mockito.doReturn(mockTask1).when(mockTaskHelper).getCurrentAsyncTask();
179         Assert.assertTrue("Shoudl be the same", testBaseProcessor.isSameAsyncTask());
180
181         Future<?> mockTask2 = mock(Future.class);
182         Mockito.doReturn(mockTask2).when(mockTaskHelper).getCurrentAsyncTask();
183         Assert.assertFalse("Shoudl not be the same", testBaseProcessor.isSameAsyncTask());
184     }
185
186     @Test
187     public void cancleAsyncTask() throws Exception {
188         Future<?> mockTask = mock(Future.class);
189         Whitebox.setInternalState(testBaseProcessor, "scheduledRunnable", mockTask);
190         testBaseProcessor.cancelAsyncTask();
191         Mockito.verify(mockTaskHelper, times(1)).cancelAsyncTask(mockTask);
192         Assert.assertTrue(Whitebox.getInternalState(testBaseProcessor, "scheduledRunnable") == null);
193     }
194
195 }