2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.oam.util;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.MaintenanceModeInput;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StartInput;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StopInput;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.common.header.Flags;
38 import org.onap.appc.exceptions.APPCException;
39 import org.onap.appc.exceptions.InvalidInputException;
40 import org.onap.appc.exceptions.InvalidStateException;
41 import org.onap.appc.lifecyclemanager.LifecycleManager;
42 import org.onap.appc.lifecyclemanager.objects.LifecycleException;
43 import org.onap.appc.lifecyclemanager.objects.NoTransitionDefinedException;
44 import org.onap.appc.oam.AppcOam;
45 import org.onap.appc.statemachine.impl.readers.AppcOamMetaDataReader;
46 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
47 import org.osgi.framework.Bundle;
48 import org.osgi.framework.BundleContext;
49 import org.osgi.framework.FrameworkUtil;
50 import org.osgi.framework.ServiceReference;
51 import org.powermock.api.mockito.PowerMockito;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
54 import org.powermock.reflect.Whitebox;
56 import static org.mockito.Mockito.mock;
57 import static org.powermock.api.mockito.PowerMockito.mockStatic;
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest({FrameworkUtil.class})
61 public class OperationHelperTest {
62 private OperationHelper operationHelper;
63 private LifecycleManager lifecycleManager = mock(LifecycleManager.class);
64 private CommonHeader mockCommonHeader = mock(CommonHeader.class);
67 public ExpectedException expectedException = ExpectedException.none();
70 public void setUp() throws Exception {
71 operationHelper = new OperationHelper();
72 Whitebox.setInternalState(operationHelper, "lifecycleMgr", lifecycleManager);
76 public void testIsInputValidWithMissingInput() throws Exception {
77 expectedException.expect(InvalidInputException.class);
78 expectedException.expectMessage(operationHelper.MISSING_COMMON_HEADER_MESSAGE);
80 operationHelper.isInputValid(null);
81 expectedException = ExpectedException.none();
85 public void testIsInputValidWithMissingCommonHeader() throws Exception {
86 StartInput mockInput = mock(StartInput.class);
87 expectedException.expect(InvalidInputException.class);
88 expectedException.expectMessage(operationHelper.MISSING_COMMON_HEADER_MESSAGE);
90 operationHelper.isInputValid(mockInput);
91 expectedException = ExpectedException.none();
95 public void testIsInputValidWithMissingOid() throws Exception {
96 StartInput mockInput = mock(StartInput.class);
97 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
98 Mockito.doReturn(null).when(mockCommonHeader).getOriginatorId();
99 expectedException.expect(InvalidInputException.class);
100 expectedException.expectMessage(operationHelper.MISSING_FIELD_MESSAGE);
102 operationHelper.isInputValid(mockInput);
103 expectedException = ExpectedException.none();
107 public void testIsInputValidWithMissingRid() throws Exception {
108 StartInput mockInput = mock(StartInput.class);
109 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
110 Mockito.doReturn("originalId").when(mockCommonHeader).getOriginatorId();
111 Mockito.doReturn(null).when(mockCommonHeader).getRequestId();
112 expectedException.expect(InvalidInputException.class);
113 expectedException.expectMessage(operationHelper.MISSING_FIELD_MESSAGE);
115 operationHelper.isInputValid(mockInput);
116 expectedException = ExpectedException.none();
120 public void testIsInputValidWithMmodeFlags() throws Exception {
121 MaintenanceModeInput mockInput = mock(MaintenanceModeInput.class);
122 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
123 Mockito.doReturn("originalId").when(mockCommonHeader).getOriginatorId();
124 Mockito.doReturn("requestId").when(mockCommonHeader).getRequestId();
125 Mockito.doReturn(mock(Flags.class)).when(mockCommonHeader).getFlags();
126 expectedException.expect(InvalidInputException.class);
127 expectedException.expectMessage(operationHelper.NOT_SUPPORT_FLAG);
129 operationHelper.isInputValid(mockInput);
130 expectedException = ExpectedException.none();
134 public void testIsInputValidPass() throws Exception {
135 StartInput mockInput = mock(StartInput.class);
136 Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
137 Mockito.doReturn("originalId").when(mockCommonHeader).getOriginatorId();
138 Mockito.doReturn("requestId").when(mockCommonHeader).getRequestId();
141 Mockito.doReturn(mock(Flags.class)).when(mockCommonHeader).getFlags();
142 operationHelper.isInputValid(mockInput);
145 Mockito.doReturn(null).when(mockCommonHeader).getFlags();
146 operationHelper.isInputValid(mockInput);
148 // MaintenanceMode without Flags
149 MaintenanceModeInput mockInput1 = mock(MaintenanceModeInput.class);
150 Mockito.doReturn(mockCommonHeader).when(mockInput1).getCommonHeader();
151 operationHelper.isInputValid(mockInput1);
155 public void testGetCommonHeader() throws Exception {
156 CommonHeader commonHeader = mock(CommonHeader.class);
158 StartInput startInput = mock(StartInput.class);
159 Mockito.doReturn(commonHeader).when(startInput).getCommonHeader();
160 Assert.assertEquals("Should return startInput commonHeader", commonHeader,
161 operationHelper.getCommonHeader(startInput));
164 StopInput stopInput = mock(StopInput.class);
165 Mockito.doReturn(commonHeader).when(stopInput).getCommonHeader();
166 Assert.assertEquals("Should return stopInput commonHeader", commonHeader,
167 operationHelper.getCommonHeader(stopInput));
169 // for MaintenanceModeInput
170 MaintenanceModeInput mmInput = mock(MaintenanceModeInput.class);
171 Mockito.doReturn(commonHeader).when(mmInput).getCommonHeader();
172 Assert.assertEquals("Should return MaintenanceModeInput commonHeader", commonHeader,
173 operationHelper.getCommonHeader(mmInput));
176 Assert.assertTrue("should return null",
177 operationHelper.getCommonHeader(new Object()) == null);
180 @SuppressWarnings("unchecked")
182 public void testGetService() throws Exception {
183 Class<OperationHelper> operationHelperClass = OperationHelper.class;
184 String className = operationHelperClass.getName();
185 String exceptionMsg = String.format(operationHelper.NO_SERVICE_REF_FORMAT, className);
187 mockStatic(FrameworkUtil.class);
188 Bundle bundle = mock(Bundle.class);
189 PowerMockito.when(FrameworkUtil.getBundle(operationHelperClass)).thenReturn(bundle);
192 Mockito.when(bundle.getBundleContext()).thenReturn(null);
193 expectedException.expect(APPCException.class);
194 expectedException.expectMessage(exceptionMsg);
195 operationHelper.getService(operationHelperClass);
197 // No service reference
198 BundleContext bundleContext = mock(BundleContext.class);
199 Mockito.when(bundle.getBundleContext()).thenReturn(bundleContext);
200 Mockito.when(bundleContext.getServiceReference(className)).thenReturn(null);
201 expectedException.expect(APPCException.class);
202 expectedException.expectMessage(exceptionMsg);
203 operationHelper.getService(operationHelperClass);
206 ServiceReference svcRef = mock(ServiceReference.class);
207 Mockito.when(bundleContext.getServiceReference(className)).thenReturn(svcRef);
208 expectedException = ExpectedException.none();
209 Assert.assertTrue("should not be null", operationHelper.getService(operationHelperClass) != null);
213 public void testGetNextState() throws Exception {
214 AppcOamMetaDataReader.AppcOperation operation = AppcOamMetaDataReader.AppcOperation.Start;
215 AppcOamStates currentState = AppcOamStates.Stopped;
216 String exceptionMsg = String.format(AppcOam.INVALID_STATE_MESSAGE_FORMAT, operation, "APPC", currentState);
218 // got LifecycleException
219 Mockito.doThrow(LifecycleException.class).when(lifecycleManager)
220 .getNextState("APPC", operation.name(), currentState.name());
221 expectedException.expect(InvalidStateException.class);
222 expectedException.expectMessage(exceptionMsg);
223 operationHelper.getNextState(operation, currentState);
225 // got NoTransitionDefinedException
226 Mockito.doThrow(NoTransitionDefinedException.class).when(lifecycleManager)
227 .getNextState("APPC", operation.name(), currentState.name());
228 expectedException.expect(InvalidStateException.class);
229 expectedException.expectMessage(exceptionMsg);
230 operationHelper.getNextState(operation, currentState);
233 expectedException = ExpectedException.none();
234 Mockito.doReturn("starting").when(lifecycleManager)
235 .getNextState("APPC", operation.name(), currentState.name());
236 Assert.assertEquals("Should return proper Starting state", AppcOamStates.Starting,
237 operationHelper.getNextState(operation, currentState));