e335ad830699254ad41ae19ad0e7fd2b13f8242c
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / openecomp / appc / oam / util / OperationHelperTest.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.util;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mockito;
34 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.MaintenanceModeInput;
35 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.StartInput;
36 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.StopInput;
37 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader;
38 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.common.header.Flags;
39 import org.openecomp.appc.exceptions.APPCException;
40 import org.openecomp.appc.exceptions.InvalidInputException;
41 import org.openecomp.appc.exceptions.InvalidStateException;
42 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
43 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
44 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
45 import org.openecomp.appc.oam.AppcOam;
46 import org.openecomp.appc.statemachine.impl.readers.AppcOamMetaDataReader;
47 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
48 import org.osgi.framework.Bundle;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.FrameworkUtil;
51 import org.osgi.framework.ServiceReference;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.core.classloader.annotations.PrepareForTest;
54 import org.powermock.modules.junit4.PowerMockRunner;
55 import org.powermock.reflect.Whitebox;
56
57 import static org.mockito.Mockito.mock;
58 import static org.powermock.api.mockito.PowerMockito.mockStatic;
59
60 @RunWith(PowerMockRunner.class)
61 @PrepareForTest({FrameworkUtil.class})
62 public class OperationHelperTest {
63     private OperationHelper operationHelper;
64     private LifecycleManager lifecycleManager = mock(LifecycleManager.class);
65     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
66
67     @Rule
68     public ExpectedException expectedException = ExpectedException.none();
69
70     @Before
71     public void setUp() throws Exception {
72         operationHelper = new OperationHelper();
73         Whitebox.setInternalState(operationHelper, "lifecycleMgr", lifecycleManager);
74     }
75
76     @Test
77     public void testIsInputValidWithMissingInput() throws Exception {
78         expectedException.expect(InvalidInputException.class);
79         expectedException.expectMessage(operationHelper.MISSING_COMMON_HEADER_MESSAGE);
80
81         operationHelper.isInputValid(null);
82         expectedException = ExpectedException.none();
83     }
84
85     @Test
86     public void testIsInputValidWithMissingCommonHeader() throws Exception {
87         StartInput mockInput = mock(StartInput.class);
88         expectedException.expect(InvalidInputException.class);
89         expectedException.expectMessage(operationHelper.MISSING_COMMON_HEADER_MESSAGE);
90
91         operationHelper.isInputValid(mockInput);
92         expectedException = ExpectedException.none();
93     }
94
95     @Test
96     public void testIsInputValidWithMissingOid() throws Exception {
97         StartInput mockInput = mock(StartInput.class);
98         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
99         Mockito.doReturn(null).when(mockCommonHeader).getOriginatorId();
100         expectedException.expect(InvalidInputException.class);
101         expectedException.expectMessage(operationHelper.MISSING_FIELD_MESSAGE);
102
103         operationHelper.isInputValid(mockInput);
104         expectedException = ExpectedException.none();
105     }
106
107     @Test
108     public void testIsInputValidWithMissingRid() throws Exception {
109         StartInput mockInput = mock(StartInput.class);
110         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
111         Mockito.doReturn("originalId").when(mockCommonHeader).getOriginatorId();
112         Mockito.doReturn(null).when(mockCommonHeader).getRequestId();
113         expectedException.expect(InvalidInputException.class);
114         expectedException.expectMessage(operationHelper.MISSING_FIELD_MESSAGE);
115
116         operationHelper.isInputValid(mockInput);
117         expectedException = ExpectedException.none();
118     }
119
120     @Test
121     public void testIsInputValidWithMmodeFlags() throws Exception {
122         MaintenanceModeInput mockInput = mock(MaintenanceModeInput.class);
123         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
124         Mockito.doReturn("originalId").when(mockCommonHeader).getOriginatorId();
125         Mockito.doReturn("requestId").when(mockCommonHeader).getRequestId();
126         Mockito.doReturn(mock(Flags.class)).when(mockCommonHeader).getFlags();
127         expectedException.expect(InvalidInputException.class);
128         expectedException.expectMessage(operationHelper.NOT_SUPPORT_FLAG);
129
130         operationHelper.isInputValid(mockInput);
131         expectedException = ExpectedException.none();
132     }
133
134     @Test
135     public void testIsInputValidPass() throws Exception {
136         StartInput mockInput = mock(StartInput.class);
137         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
138         Mockito.doReturn("originalId").when(mockCommonHeader).getOriginatorId();
139         Mockito.doReturn("requestId").when(mockCommonHeader).getRequestId();
140
141         //with Flags
142         Mockito.doReturn(mock(Flags.class)).when(mockCommonHeader).getFlags();
143         operationHelper.isInputValid(mockInput);
144
145         //without Flags
146         Mockito.doReturn(null).when(mockCommonHeader).getFlags();
147         operationHelper.isInputValid(mockInput);
148
149         // MaintenanceMode without Flags
150         MaintenanceModeInput mockInput1 = mock(MaintenanceModeInput.class);
151         Mockito.doReturn(mockCommonHeader).when(mockInput1).getCommonHeader();
152         operationHelper.isInputValid(mockInput1);
153     }
154
155     @Test
156     public void testGetCommonHeader() throws Exception {
157         CommonHeader commonHeader = mock(CommonHeader.class);
158         // for StartInput
159         StartInput startInput = mock(StartInput.class);
160         Mockito.doReturn(commonHeader).when(startInput).getCommonHeader();
161         Assert.assertEquals("Should return startInput commonHeader", commonHeader,
162                 operationHelper.getCommonHeader(startInput));
163
164         // for StopInput
165         StopInput stopInput = mock(StopInput.class);
166         Mockito.doReturn(commonHeader).when(stopInput).getCommonHeader();
167         Assert.assertEquals("Should return stopInput commonHeader", commonHeader,
168                 operationHelper.getCommonHeader(stopInput));
169
170         // for MaintenanceModeInput
171         MaintenanceModeInput mmInput = mock(MaintenanceModeInput.class);
172         Mockito.doReturn(commonHeader).when(mmInput).getCommonHeader();
173         Assert.assertEquals("Should return MaintenanceModeInput commonHeader", commonHeader,
174                 operationHelper.getCommonHeader(mmInput));
175
176         // unsupported type
177         Assert.assertTrue("should return null",
178                 operationHelper.getCommonHeader(new Object()) == null);
179     }
180
181     @SuppressWarnings("unchecked")
182     @Test
183     public void testGetService() throws Exception {
184         Class<OperationHelper> operationHelperClass = OperationHelper.class;
185         String className = operationHelperClass.getName();
186         String exceptionMsg = String.format(operationHelper.NO_SERVICE_REF_FORMAT, className);
187
188         mockStatic(FrameworkUtil.class);
189         Bundle bundle = mock(Bundle.class);
190         PowerMockito.when(FrameworkUtil.getBundle(operationHelperClass)).thenReturn(bundle);
191
192         // No bundle context
193         Mockito.when(bundle.getBundleContext()).thenReturn(null);
194         expectedException.expect(APPCException.class);
195         expectedException.expectMessage(exceptionMsg);
196         operationHelper.getService(operationHelperClass);
197
198         // No service reference
199         BundleContext bundleContext = mock(BundleContext.class);
200         Mockito.when(bundle.getBundleContext()).thenReturn(bundleContext);
201         Mockito.when(bundleContext.getServiceReference(className)).thenReturn(null);
202         expectedException.expect(APPCException.class);
203         expectedException.expectMessage(exceptionMsg);
204         operationHelper.getService(operationHelperClass);
205
206         // Success path
207         ServiceReference svcRef = mock(ServiceReference.class);
208         Mockito.when(bundleContext.getServiceReference(className)).thenReturn(svcRef);
209         expectedException = ExpectedException.none();
210         Assert.assertTrue("should not be null", operationHelper.getService(operationHelperClass) != null);
211     }
212
213     @Test
214     public void testGetNextState() throws Exception {
215         AppcOamMetaDataReader.AppcOperation operation = AppcOamMetaDataReader.AppcOperation.Start;
216         AppcOamStates currentState = AppcOamStates.Stopped;
217         String exceptionMsg = String.format(AppcOam.INVALID_STATE_MESSAGE_FORMAT, operation, "APPC", currentState);
218
219         // got LifecycleException
220         Mockito.doThrow(LifecycleException.class).when(lifecycleManager)
221                 .getNextState("APPC", operation.name(), currentState.name());
222         expectedException.expect(InvalidStateException.class);
223         expectedException.expectMessage(exceptionMsg);
224         operationHelper.getNextState(operation, currentState);
225
226         // got NoTransitionDefinedException
227         Mockito.doThrow(NoTransitionDefinedException.class).when(lifecycleManager)
228                 .getNextState("APPC", operation.name(), currentState.name());
229         expectedException.expect(InvalidStateException.class);
230         expectedException.expectMessage(exceptionMsg);
231         operationHelper.getNextState(operation, currentState);
232
233         // Success path
234         expectedException = ExpectedException.none();
235         Mockito.doReturn("starting").when(lifecycleManager)
236                 .getNextState("APPC", operation.name(), currentState.name());
237         Assert.assertEquals("Should return proper Starting state", AppcOamStates.Starting,
238                 operationHelper.getNextState(operation, currentState));
239     }
240 }