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