f163c6eab91b95eed3ee63f89d9867e14399526e
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / test / java / org / onap / appc / requesthandler / impl / RequestHandlerImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
6  * this file except in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.appc.requesthandler.impl;
20
21 import static org.mockito.Mockito.anyString;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFLogger.Level;
30 import com.att.eelf.configuration.EELFManager;
31 import com.att.eelf.i18n.EELFResourceManager;
32
33 import java.util.Arrays;
34 import java.util.Date;
35 import java.util.List;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
42 import org.onap.appc.domainmodel.lcm.CommonHeader;
43 import org.onap.appc.domainmodel.lcm.Flags;
44 import org.onap.appc.domainmodel.lcm.RequestContext;
45 import org.onap.appc.domainmodel.lcm.RequestStatus;
46 import org.onap.appc.domainmodel.lcm.ResponseContext;
47 import org.onap.appc.domainmodel.lcm.RuntimeContext;
48 import org.onap.appc.domainmodel.lcm.VNFContext;
49 import org.onap.appc.domainmodel.lcm.VNFOperation;
50 import org.onap.appc.exceptions.APPCException;
51 import org.onap.appc.executor.CommandExecutor;
52 import org.onap.appc.executor.objects.CommandExecutorInput;
53 import org.onap.appc.executor.objects.LCMCommandStatus;
54 import org.onap.appc.executor.objects.Params;
55 import org.onap.appc.i18n.Msg;
56 import org.onap.appc.lockmanager.api.LockException;
57 import org.onap.appc.lockmanager.api.LockManager;
58 import org.onap.appc.logging.LoggingConstants;
59 import org.onap.appc.metricservice.MetricRegistry;
60 import org.onap.appc.metricservice.impl.MetricRegistryImpl;
61 import org.onap.appc.metricservice.metric.MetricType;
62 import org.onap.appc.metricservice.metric.impl.DispatchingFuntionMetricImpl;
63 import org.onap.appc.transactionrecorder.TransactionRecorder;
64 import org.osgi.framework.FrameworkUtil;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.core.classloader.annotations.PrepareForTest;
67 import org.powermock.modules.junit4.PowerMockRunner;
68 import org.powermock.reflect.Whitebox;
69
70 @RunWith(PowerMockRunner.class)
71 @PrepareForTest({ FrameworkUtil.class })
72 public class RequestHandlerImplTest implements LocalRequestHanlderTestHelper {
73
74     private RequestHandlerImpl requestHandler;
75     private TransactionRecorder recorder;
76     private LockManager lockManager;
77
78     @Before
79     public void setUp() throws Exception {
80         setupForHandlerImplTests();
81         requestHandler = PowerMockito.spy(new RequestHandlerImpl());
82         recorder = mock(TransactionRecorder.class);
83         requestHandler.setTransactionRecorder(recorder);
84         lockManager = mock(LockManager.class);
85         requestHandler.setLockManager(lockManager);
86         List<RequestStatus> result = Arrays.asList(RequestStatus.ACCEPTED);
87         PowerMockito.when(recorder.getRecords(anyString(), anyString(), anyString(), anyString())).thenReturn(result);
88         final EELFLogger logger = EELFManager.getInstance().getLogger(RequestHandlerImpl.class);
89         logger.setLevel(Level.TRACE);
90         Whitebox.setInternalState(requestHandler, "logger", logger);
91     }
92
93     @Test
94     public void testHandleRequestRuntimeContext() {
95         Whitebox.setInternalState(requestHandler, "isMetricEnabled", true);
96         MetricRegistry metricRegistry = spy(new MetricRegistryImpl("TEST_METRIC_REGISTRY"));
97         DispatchingFuntionMetricImpl metric = spy(
98                 new DispatchingFuntionMetricImpl("DISPATCH_FUNCTION", MetricType.COUNTER, 0, 0));
99         metricRegistry.register(metric);
100         doNothing().when(metric).incrementAcceptedRequest();
101         Whitebox.setInternalState(RequestHandlerImpl.class, "metricRegistry", metricRegistry);
102         RuntimeContext runtimeContext = spy(new RuntimeContext());
103         RequestContext requestContext = setupRequestContext();
104         runtimeContext.setRequestContext(requestContext);
105         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis()));
106         requestContext.setCommonHeader(ch);
107         CommandExecutor commandExecutor = mock(CommandExecutor.class);
108         requestHandler.setCommandExecutor(commandExecutor);
109         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
110                 Mockito.any(LCMCommandStatus.class), Mockito.any());
111         requestHandler.handleRequest(runtimeContext);
112         Mockito.verify(requestHandler).fillStatus(runtimeContext, LCMCommandStatus.ACCEPTED, null);
113     }
114
115     @Test
116     public void testHandleRequestRuntimeContextNegativeTTL() {
117         RuntimeContext runtimeContext = spy(new RuntimeContext());
118         RequestContext requestContext = setupRequestContext();
119         runtimeContext.setRequestContext(requestContext);
120         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis() - 100000));
121         requestContext.setCommonHeader(ch);
122         CommandExecutor commandExecutor = mock(CommandExecutor.class);
123         requestHandler.setCommandExecutor(commandExecutor);
124         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
125                 Mockito.any(LCMCommandStatus.class), Mockito.any());
126         doNothing().when(requestHandler).storeErrorMessageToLog(Mockito.any(RuntimeContext.class), Mockito.anyString(),
127                 Mockito.anyString(), Mockito.anyString());
128         requestHandler.handleRequest(runtimeContext);
129         Mockito.verify(requestHandler).fillStatus(runtimeContext, LCMCommandStatus.EXPIRED_REQUEST, null);
130
131     }
132
133     @Test
134     public void testHandleRequestRuntimeContextAPPCException() throws APPCException {
135         RuntimeContext runtimeContext = spy(new RuntimeContext());
136         RequestContext requestContext = setupRequestContext();
137         runtimeContext.setRequestContext(requestContext);
138         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis()));
139         requestContext.setCommonHeader(ch);
140         CommandExecutor commandExecutor = mock(CommandExecutor.class);
141         requestHandler.setCommandExecutor(commandExecutor);
142         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
143                 Mockito.any(LCMCommandStatus.class), Mockito.any());
144         doThrow(new APPCException("TEST_APPC_EXCEPTION")).when(commandExecutor)
145         .executeCommand(Mockito.any(CommandExecutorInput.class));
146         doNothing().when(requestHandler).storeErrorMessageToLog(Mockito.any(RuntimeContext.class), Mockito.anyString(),
147                 Mockito.anyString(), Mockito.anyString());
148         requestHandler.handleRequest(runtimeContext);
149         Mockito.verify(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
150                 Mockito.any(LCMCommandStatus.class), Mockito.any(Params.class));
151     }
152
153     @Test
154     public void testHandleRequestRuntimeContextCaseLock() throws LockException {
155         RuntimeContext runtimeContext = spy(new RuntimeContext());
156         RequestContext requestContext = new RequestContext();
157         requestContext.setAction(VNFOperation.Lock);
158         runtimeContext.setRequestContext(requestContext);
159         VNFContext vnfContext = new VNFContext();
160         vnfContext.setId("TEST_VNF_CONTEXT_ID");
161         runtimeContext.setVnfContext(vnfContext);
162         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis()));
163         requestContext.setCommonHeader(ch);
164         CommandExecutor commandExecutor = mock(CommandExecutor.class);
165         requestHandler.setCommandExecutor(commandExecutor);
166         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
167                 Mockito.any(LCMCommandStatus.class), Mockito.any());
168         doReturn(true).when(lockManager).acquireLock(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong());
169         requestHandler.handleRequest(runtimeContext);
170         Mockito.verify(requestHandler).fillStatus(runtimeContext, LCMCommandStatus.SUCCESS, null);
171     }
172
173     @Test
174     public void testHandleRequestRuntimeContextLockError() throws LockException {
175         RuntimeContext runtimeContext = spy(new RuntimeContext());
176         RequestContext requestContext = new RequestContext();
177         requestContext.setAction(VNFOperation.Lock);
178         runtimeContext.setRequestContext(requestContext);
179         VNFContext vnfContext = new VNFContext();
180         vnfContext.setId("TEST_VNF_CONTEXT_ID");
181         runtimeContext.setVnfContext(vnfContext);
182         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis()));
183         requestContext.setCommonHeader(ch);
184         CommandExecutor commandExecutor = mock(CommandExecutor.class);
185         requestHandler.setCommandExecutor(commandExecutor);
186         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
187                 Mockito.any(LCMCommandStatus.class), Mockito.any());
188         doThrow(new LockException("TEST_LOCK_EXCEPTION")).when(lockManager).acquireLock(Mockito.anyString(),
189                 Mockito.anyString(), Mockito.anyLong());
190         doNothing().when(requestHandler).storeErrorMessageToLog(Mockito.any(RuntimeContext.class), Mockito.anyString(),
191                 Mockito.anyString(), Mockito.anyString());
192         requestHandler.handleRequest(runtimeContext);
193         Mockito.verify(requestHandler).storeErrorMessageToLog(runtimeContext, LoggingConstants.TargetNames.APPC,
194                 LoggingConstants.TargetNames.LOCK_MANAGER,
195                 EELFResourceManager.format(Msg.VF_SERVER_BUSY, "TEST_VNF_CONTEXT_ID"));
196     }
197
198     @Test
199     public void testHandleRequestRuntimeContextCaseUnlock() throws LockException {
200         RuntimeContext runtimeContext = spy(new RuntimeContext());
201         RequestContext requestContext = new RequestContext();
202         requestContext.setAction(VNFOperation.Unlock);
203         runtimeContext.setRequestContext(requestContext);
204         VNFContext vnfContext = new VNFContext();
205         vnfContext.setId("TEST_VNF_CONTEXT_ID");
206         runtimeContext.setVnfContext(vnfContext);
207         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis()));
208         requestContext.setCommonHeader(ch);
209         CommandExecutor commandExecutor = mock(CommandExecutor.class);
210         requestHandler.setCommandExecutor(commandExecutor);
211         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
212                 Mockito.any(LCMCommandStatus.class), Mockito.any());
213         doReturn(true).when(lockManager).acquireLock(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong());
214         requestHandler.handleRequest(runtimeContext);
215         Mockito.verify(requestHandler).fillStatus(runtimeContext, LCMCommandStatus.SUCCESS, null);
216     }
217
218     @Test
219     public void testHandleRequestRuntimeContextUnlockError() throws LockException {
220         RuntimeContext runtimeContext = spy(new RuntimeContext());
221         RequestContext requestContext = new RequestContext();
222         requestContext.setAction(VNFOperation.Unlock);
223         runtimeContext.setRequestContext(requestContext);
224         VNFContext vnfContext = new VNFContext();
225         vnfContext.setId("TEST_VNF_CONTEXT_ID");
226         runtimeContext.setVnfContext(vnfContext);
227         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis()));
228         requestContext.setCommonHeader(ch);
229         CommandExecutor commandExecutor = mock(CommandExecutor.class);
230         requestHandler.setCommandExecutor(commandExecutor);
231         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
232                 Mockito.any(LCMCommandStatus.class), Mockito.any());
233         doThrow(new LockException("TEST_LOCK_EXCEPTION")).when(lockManager).releaseLock(Mockito.anyString(),
234                 Mockito.anyString());
235         doNothing().when(requestHandler).storeErrorMessageToLog(Mockito.any(RuntimeContext.class), Mockito.anyString(),
236                 Mockito.anyString(), Mockito.anyString());
237         requestHandler.handleRequest(runtimeContext);
238         Mockito.verify(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
239                 Mockito.any(LCMCommandStatus.class), Mockito.any(Params.class));
240     }
241
242     @Test
243     public void testHandleRequestRuntimeContextCaseCheckLock() {
244         RuntimeContext runtimeContext = spy(new RuntimeContext());
245         RequestContext requestContext = new RequestContext();
246         ResponseContext responseContext = spy(new ResponseContext());
247         runtimeContext.setResponseContext(responseContext);
248         requestContext.setAction(VNFOperation.CheckLock);
249         runtimeContext.setRequestContext(requestContext);
250         VNFContext vnfContext = new VNFContext();
251         vnfContext.setId("TEST_VNF_CONTEXT_ID");
252         runtimeContext.setVnfContext(vnfContext);
253         CommonHeader ch = getCommonHeader("TEST", new Date(System.currentTimeMillis()));
254         requestContext.setCommonHeader(ch);
255         CommandExecutor commandExecutor = mock(CommandExecutor.class);
256         requestHandler.setCommandExecutor(commandExecutor);
257         doNothing().when(requestHandler).fillStatus(Mockito.any(RuntimeContext.class),
258                 Mockito.any(LCMCommandStatus.class), Mockito.any());
259         doReturn(true).when(lockManager).isLocked("TEST_VNF_CONTEXT_ID");
260         requestHandler.handleRequest(runtimeContext);
261         Mockito.verify(responseContext).addKeyValueToAdditionalContext("locked", String.valueOf(true).toUpperCase());
262     }
263
264     private RequestContext setupRequestContext() {
265         RequestContext requestContext = new RequestContext();
266         requestContext.setAction(VNFOperation.ActionStatus);
267         ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
268         actionIdentifiers.setVnfId("TEST_VNF_ID");
269         requestContext.setActionIdentifiers(actionIdentifiers);
270         return requestContext;
271     }
272 }