d8280344dc1e58ae94e52b4bef687bd7a2ca5fd0
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / onap / appc / oam / util / OperationHelperTest.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.util;
25
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;
55
56 import static org.mockito.Mockito.mock;
57 import static org.powermock.api.mockito.PowerMockito.mockStatic;
58
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);
65
66     @Rule
67     public ExpectedException expectedException = ExpectedException.none();
68
69     @Before
70     public void setUp() throws Exception {
71         operationHelper = new OperationHelper();
72         Whitebox.setInternalState(operationHelper, "lifecycleMgr", lifecycleManager);
73     }
74
75     @Test
76     public void testIsInputValidWithMissingInput() throws Exception {
77         expectedException.expect(InvalidInputException.class);
78         expectedException.expectMessage(operationHelper.MISSING_COMMON_HEADER_MESSAGE);
79
80         operationHelper.isInputValid(null);
81         expectedException = ExpectedException.none();
82     }
83
84     @Test
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);
89
90         operationHelper.isInputValid(mockInput);
91         expectedException = ExpectedException.none();
92     }
93
94     @Test
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);
101
102         operationHelper.isInputValid(mockInput);
103         expectedException = ExpectedException.none();
104     }
105
106     @Test
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);
114
115         operationHelper.isInputValid(mockInput);
116         expectedException = ExpectedException.none();
117     }
118
119     @Test
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);
128
129         operationHelper.isInputValid(mockInput);
130         expectedException = ExpectedException.none();
131     }
132
133     @Test
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();
139
140         //with Flags
141         Mockito.doReturn(mock(Flags.class)).when(mockCommonHeader).getFlags();
142         operationHelper.isInputValid(mockInput);
143
144         //without Flags
145         Mockito.doReturn(null).when(mockCommonHeader).getFlags();
146         operationHelper.isInputValid(mockInput);
147
148         // MaintenanceMode without Flags
149         MaintenanceModeInput mockInput1 = mock(MaintenanceModeInput.class);
150         Mockito.doReturn(mockCommonHeader).when(mockInput1).getCommonHeader();
151         operationHelper.isInputValid(mockInput1);
152     }
153
154     @Test
155     public void testGetCommonHeader() throws Exception {
156         CommonHeader commonHeader = mock(CommonHeader.class);
157         // for StartInput
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));
162
163         // for StopInput
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));
168
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));
174
175         // unsupported type
176         Assert.assertTrue("should return null",
177                 operationHelper.getCommonHeader(new Object()) == null);
178     }
179
180     @SuppressWarnings("unchecked")
181     @Test
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);
186
187         mockStatic(FrameworkUtil.class);
188         Bundle bundle = mock(Bundle.class);
189         PowerMockito.when(FrameworkUtil.getBundle(operationHelperClass)).thenReturn(bundle);
190
191         // No bundle context
192         Mockito.when(bundle.getBundleContext()).thenReturn(null);
193         expectedException.expect(APPCException.class);
194         expectedException.expectMessage(exceptionMsg);
195         operationHelper.getService(operationHelperClass);
196
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);
204
205         // Success path
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);
210     }
211
212     @Test
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);
217
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);
224
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);
231
232         // Success path
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));
238     }
239 }