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