2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2018 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.oam.processor;
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.onap.appc.oam.rev170303.StartInput;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.exceptions.InvalidInputException;
38 import org.onap.appc.exceptions.InvalidStateException;
39 import org.onap.appc.i18n.Msg;
40 import org.onap.appc.oam.AppcOam;
41 import org.onap.appc.oam.OAMCommandStatus;
42 import org.onap.appc.oam.util.AsyncTaskHelper;
43 import org.onap.appc.oam.util.ConfigurationHelper;
44 import org.onap.appc.oam.util.OperationHelper;
45 import org.onap.appc.oam.util.StateHelper;
46 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
47 import org.powermock.reflect.Whitebox;
49 import static org.mockito.Matchers.any;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.times;
54 @SuppressWarnings("ResultOfMethodCallIgnored")
55 public class BaseProcessorTest {
56 private AppcOam.RPC testRpc = AppcOam.RPC.start;
57 private AppcOamStates currentState = AppcOamStates.Stopped;
59 private class TestAbc extends BaseProcessor {
64 * @param eelfLogger for logging
65 * @param configurationHelperIn for property reading
66 * @param stateHelperIn for APP-C OAM state checking
67 * @param asyncTaskHelperIn for scheduling async task
68 * @param operationHelperIn for operational helper
70 TestAbc(EELFLogger eelfLogger,
71 ConfigurationHelper configurationHelperIn,
72 StateHelper stateHelperIn,
73 AsyncTaskHelper asyncTaskHelperIn,
74 OperationHelper operationHelperIn) {
75 super(eelfLogger, configurationHelperIn, stateHelperIn, asyncTaskHelperIn, operationHelperIn);
77 // must set rpc and auditMsg
79 auditMsg = Msg.OAM_OPERATION_STARTING;
83 private TestAbc testBaseProcessor;
84 private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class);
85 private StateHelper mockStateHelper = mock(StateHelper.class);
86 private AsyncTaskHelper mockTaskHelper = mock(AsyncTaskHelper.class);
87 private OperationHelper mockOperHelper = mock(OperationHelper.class);
89 private StartInput mockInput = mock(StartInput.class);
90 private CommonHeader mockCommonHeader = mock(CommonHeader.class);
93 public void setUp() throws Exception {
94 Mockito.doReturn(mockCommonHeader).when(mockOperHelper).getCommonHeader(mockInput);
95 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
97 testBaseProcessor = spy(
98 new TestAbc(null, mockConfigHelper, mockStateHelper, mockTaskHelper, mockOperHelper));
99 Mockito.doReturn("SOME HOST NAME").when(testBaseProcessor).getHostInfo(Mockito.anyString());
100 Whitebox.setInternalState(testBaseProcessor, "commonHeader", mockCommonHeader);
102 // to avoid operation on logger fail, mock up the logger
103 EELFLogger mockLogger = mock(EELFLogger.class);
104 Whitebox.setInternalState(testBaseProcessor, "logger", mockLogger);
108 public void testProcessRequestError() throws Exception {
109 Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
110 Mockito.doThrow(new InvalidInputException("test")).when(mockOperHelper).isInputValid(mockInput);
111 Status status = testBaseProcessor.processRequest(mockInput);
112 Assert.assertEquals("Should return reject",
113 OAMCommandStatus.INVALID_PARAMETER.getResponseCode(), status.getCode().intValue());
117 public void testProcessRequest() throws Exception {
118 Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
119 Mockito.doReturn(AppcOamStates.Starting).when(mockOperHelper).getNextState(any(), any());
120 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
121 Status status = testBaseProcessor.processRequest(mockInput);
122 Assert.assertEquals("Should return success",
123 OAMCommandStatus.ACCEPTED.getResponseCode(), status.getCode().intValue());
126 @Test(expected = InvalidInputException.class)
127 public void testPreProcessWithInvalidInput() throws Exception {
128 Mockito.doThrow(new InvalidInputException("test")).when(mockOperHelper).isInputValid(mockInput);
129 testBaseProcessor.preProcess(mockInput);
132 @Test(expected = InvalidStateException.class)
133 public void testPreProcessWithInvalidState() throws Exception {
134 Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
135 Mockito.doThrow(new InvalidStateException("test"))
136 .when(mockOperHelper).getNextState(testRpc.getAppcOperation(), currentState);
137 testBaseProcessor.preProcess(mockInput);
140 @Test(expected = APPCException.class)
141 public void testPreProcessWithAppcException() throws Exception {
142 Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
143 Mockito.doThrow(new APPCException("test"))
144 .when(mockOperHelper).getNextState(testRpc.getAppcOperation(), currentState);
145 testBaseProcessor.preProcess(mockInput);
149 public void testPreProcess() throws Exception {
150 Mockito.doReturn(currentState).when(mockStateHelper).getCurrentOamState();
151 AppcOamStates nextState = AppcOamStates.Starting;
152 Mockito.doReturn(nextState)
153 .when(mockOperHelper).getNextState(testRpc.getAppcOperation(), currentState);
154 testBaseProcessor.preProcess(mockInput);
155 Mockito.verify(mockOperHelper, times(1)).isInputValid(mockInput);
156 Mockito.verify(mockOperHelper, times(1)).getNextState(testRpc.getAppcOperation(), currentState);
157 Mockito.verify(mockStateHelper, times(1)).setState(nextState);
161 public void testScheduleAsyncTask() throws Exception {
163 testBaseProcessor.scheduleAsyncTask();
164 Assert.assertTrue(Whitebox.getInternalState(testBaseProcessor, "runnable") == null);
165 Assert.assertTrue(Whitebox.getInternalState(testBaseProcessor, "scheduledRunnable") == null);
167 BaseActionRunnable mockRunnable = mock(BaseActionRunnable.class);
168 Whitebox.setInternalState(testBaseProcessor, "runnable", mockRunnable);
169 testBaseProcessor.scheduleAsyncTask();
170 // scheduledRunnable should still be null, there's no mock done
171 // as I have trouble to make mockTaskHelper.scheduleBaseRunnable to return a proper Future
172 Assert.assertTrue(Whitebox.getInternalState(testBaseProcessor, "scheduledRunnable") == null);