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