Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / RequestExecutorTest.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.onap.appc.provider.lcm.service;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
33 import org.onap.appc.domainmodel.lcm.ActionLevel;
34 import org.onap.appc.domainmodel.lcm.CommonHeader;
35 import org.onap.appc.domainmodel.lcm.RequestContext;
36 import org.onap.appc.domainmodel.lcm.ResponseContext;
37 import org.onap.appc.domainmodel.lcm.Status;
38 import org.onap.appc.domainmodel.lcm.VNFOperation;
39 import org.onap.appc.executor.objects.LCMCommandStatus;
40 import org.onap.appc.executor.objects.Params;
41 import org.onap.appc.i18n.Msg;
42 import org.onap.appc.requesthandler.RequestHandler;
43 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
44 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
45 import org.osgi.framework.Bundle;
46 import org.osgi.framework.BundleContext;
47 import org.osgi.framework.FrameworkUtil;
48 import org.osgi.framework.InvalidSyntaxException;
49 import org.osgi.framework.ServiceReference;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
53
54 import java.util.Collection;
55 import java.util.Iterator;
56
57 import static org.mockito.Matchers.any;
58 import static org.mockito.Matchers.anyString;
59 import static org.mockito.Matchers.eq;
60 import static org.mockito.Mockito.mock;
61 import static org.mockito.Mockito.spy;
62 import static org.mockito.Mockito.times;
63 import static org.powermock.api.mockito.PowerMockito.mockStatic;
64
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({FrameworkUtil.class})
67 public class RequestExecutorTest {
68     private final VNFOperation vnfOperation = VNFOperation.ActionStatus;
69     private final ActionLevel actionLevel = ActionLevel.MGMT;
70
71     private Bundle mockBundle = mock(Bundle.class);
72     private BundleContext mockBundleContext = mock(BundleContext.class);
73
74     private RequestHandlerInput mockInput = mock(RequestHandlerInput.class);
75     private RequestContext mockRequestContext = mock(RequestContext.class);
76     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
77     private RequestHandler mockHandler = mock(RequestHandler.class);
78
79     private RequestExecutor requestExecutor;
80
81     @Before
82     public void setUp() throws Exception {
83         mockStatic(FrameworkUtil.class);
84         PowerMockito.when(FrameworkUtil.getBundle(any())).thenReturn(mockBundle);
85
86         Mockito.doReturn(mockRequestContext).when(mockInput).getRequestContext();
87         Mockito.doReturn(mockCommonHeader).when(mockRequestContext).getCommonHeader();
88
89         requestExecutor = spy(new RequestExecutor());
90     }
91
92     @Test
93     public void testExecuteRequest() throws Exception {
94         // test RequestHandler is null
95         Mockito.doReturn(null).when(mockBundle).getBundleContext();
96         Mockito.doReturn(actionLevel).when(mockRequestContext).getActionLevel();
97         Mockito.doReturn(vnfOperation).when(mockRequestContext).getAction();
98
99         Params params = new Params().addParam("errorMsg", requestExecutor.CANNOT_PROCESS);
100         LCMCommandStatus lcmCommandStatus = LCMCommandStatus.REJECTED;
101         Msg msg = Msg.REQUEST_HANDLER_UNAVAILABLE;
102
103         RequestHandlerOutput output = requestExecutor.executeRequest(mockInput);
104         ResponseContext responseContext = output.getResponseContext();
105         Status status = responseContext.getStatus();
106         Assert.assertEquals("Should have rejected status code",
107             lcmCommandStatus.getResponseCode(), status.getCode());
108         Assert.assertEquals("Should have rejected CANNOT_PROCESS status message",
109             lcmCommandStatus.getFormattedMessage(params), status.getMessage());
110         Assert.assertEquals("Should have the same commonHeader",
111             mockCommonHeader, responseContext.getCommonHeader());
112         Mockito.verify(requestExecutor, times(1)).getRequestHandler(any());
113         Mockito.verify(requestExecutor, times(1))
114             .createRequestHandlerOutput(any(), any(), any(), any());
115
116         // to get RequestHandler
117         ServiceReference<RequestHandler> mockSvcRefs = mock(ServiceReference.class);
118         Iterator mockIterator = mock(Iterator.class);
119         Mockito.doReturn(mockSvcRefs).when(mockIterator).next();
120
121         Collection<ServiceReference<RequestHandler>> mockSvcRefCollection = mock(Collection.class);
122         Mockito.doReturn(1).when(mockSvcRefCollection).size();
123         Mockito.doReturn(mockIterator).when(mockSvcRefCollection).iterator();
124
125         Mockito.doReturn(mockBundleContext).when(mockBundle).getBundleContext();
126         Mockito.doReturn(mockSvcRefCollection).when(mockBundleContext)
127             .getServiceReferences(eq(RequestHandler.class), anyString());
128
129         Mockito.doReturn(mockHandler).when(mockBundleContext).getService(mockSvcRefs);
130
131         // Skip test RequestHandler.quiesce throws exception as it does not throw exception
132
133         // test normal return
134         RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
135         Mockito.doReturn(mockOutput).when(mockHandler).handleRequest(mockInput);
136         output = requestExecutor.executeRequest(mockInput);
137         Assert.assertEquals("Should return mockOuput", mockOutput, output);
138     }
139
140     @Test
141     public void testGetRequestHandler() throws Exception {
142         // test null BundleContext
143         Mockito.doReturn(null).when(mockBundle).getBundleContext();
144         RequestHandler requestHandler = requestExecutor.getRequestHandler(actionLevel);
145         Assert.assertTrue("Should return null", requestHandler == null);
146
147         // test successful returning RequestHandler
148         ServiceReference<RequestHandler> mockSvcRefs = mock(ServiceReference.class);
149         Iterator mockIterator = mock(Iterator.class);
150         Mockito.doReturn(mockSvcRefs).when(mockIterator).next();
151
152         Collection<ServiceReference<RequestHandler>> mockSvcRefCollection = mock(Collection.class);
153         Mockito.doReturn(1).when(mockSvcRefCollection).size();
154         Mockito.doReturn(mockIterator).when(mockSvcRefCollection).iterator();
155
156         Mockito.doReturn(mockBundleContext).when(mockBundle).getBundleContext();
157         Mockito.doReturn(mockSvcRefCollection).when(mockBundleContext)
158             .getServiceReferences(eq(RequestHandler.class), anyString());
159
160         Mockito.doReturn(mockHandler).when(mockBundleContext).getService(mockSvcRefs);
161
162         requestHandler = requestExecutor.getRequestHandler(actionLevel);
163         Assert.assertEquals("Should return RequestHandler", mockHandler, requestHandler);
164     }
165
166     @Test(expected = RuntimeException.class)
167     public void testGetRequesetHandlerWithInvalidSyntaxException() throws Exception {
168         Mockito.doReturn(mockBundleContext).when(mockBundle).getBundleContext();
169         Mockito.doThrow(new InvalidSyntaxException("testing message", "testing filter"))
170             .when(mockBundleContext).getServiceReferences(eq(RequestHandler.class), anyString());
171
172         requestExecutor.getRequestHandler(actionLevel);
173     }
174
175
176     @Test(expected = RuntimeException.class)
177     public void testGetRequesetHandlerWithCannotFoundSvc() throws Exception {
178         Collection<ServiceReference<RequestHandler>> mockSvcRefCollection = mock(Collection.class);
179         Mockito.doReturn(2).when(mockSvcRefCollection).size();
180
181         Mockito.doReturn(mockBundleContext).when(mockBundle).getBundleContext();
182         Mockito.doReturn(mockSvcRefCollection).when(mockBundleContext)
183             .getServiceReferences(eq(RequestHandler.class), anyString());
184
185         requestExecutor.getRequestHandler(actionLevel);
186     }
187
188     @Test
189     public void testCreateRequestHandlerOutput() throws Exception {
190         // test exception without message
191         Exception testException = new Exception();
192         Params params = new Params().addParam("errorMsg", testException.toString());
193         LCMCommandStatus lcmCommandStatus = LCMCommandStatus.REJECTED;
194         Msg msg = Msg.REQUEST_HANDLER_UNAVAILABLE;
195
196         RequestHandlerOutput output =
197             requestExecutor.createRequestHandlerOutput(mockInput, lcmCommandStatus, msg, testException);
198         ResponseContext responseContext = output.getResponseContext();
199         Status status = responseContext.getStatus();
200         Assert.assertEquals("Should have the same status code",
201             lcmCommandStatus.getResponseCode(), status.getCode());
202         Assert.assertEquals("Should have the proper exception to String status message",
203             lcmCommandStatus.getFormattedMessage(params), status.getMessage());
204         Assert.assertEquals("Should have the same commonHeader",
205             mockCommonHeader, responseContext.getCommonHeader());
206
207         // test exception with message
208         testException = new Exception("testing exception");
209         params = new Params().addParam("errorMsg", testException.getMessage());
210         lcmCommandStatus = LCMCommandStatus.UNEXPECTED_ERROR;
211         msg = Msg.EXCEPTION_CALLING_DG;
212
213         output =
214             requestExecutor.createRequestHandlerOutput(mockInput, lcmCommandStatus, msg, testException);
215         responseContext = output.getResponseContext();
216         status = responseContext.getStatus();
217         Assert.assertEquals("Should have the same status code",
218             lcmCommandStatus.getResponseCode(), status.getCode());
219         Assert.assertEquals("Should have the proper exception to String status message",
220             lcmCommandStatus.getFormattedMessage(params), status.getMessage());
221         Assert.assertEquals("Should have the same commonHeader",
222             mockCommonHeader, responseContext.getCommonHeader());
223     }
224
225     @Test
226     public void testGetPayload() throws Exception {
227         RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
228         ResponseContext mockResponseContext = mock(ResponseContext.class);
229         // test null response context
230         Mockito.doReturn(null).when(mockOutput).getResponseContext();
231         Assert.assertTrue("Should return null with null requestContext",
232             requestExecutor.getPayload(mockOutput) == null);
233
234         // test null payload
235         Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
236         Mockito.doReturn(null).when(mockResponseContext).getPayload();
237         Assert.assertTrue("Should return null with null payload",
238             requestExecutor.getPayload(mockOutput) == null);
239
240         // test empty payload
241         Mockito.doReturn("").when(mockResponseContext).getPayload();
242         Assert.assertTrue("Should return null with empty payload",
243             requestExecutor.getPayload(mockOutput) == null);
244
245         // test proper payload
246         String testingPayload = "testing payload";
247         Mockito.doReturn(testingPayload).when(mockResponseContext).getPayload();
248         Payload payload = requestExecutor.getPayload(mockOutput);
249         Assert.assertEquals("Should return null with empty payload", testingPayload, payload.getValue());
250     }
251
252 }