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 this
6 * file except in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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
15 * SPDX-License-Identifier: Apache-2.0
16 * ============LICENSE_END=========================================================
19 package org.onap.appc.requesthandler.impl;
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.doThrow;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.times;
27 import static org.powermock.api.mockito.PowerMockito.mockStatic;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFLogger.Level;
31 import com.att.eelf.configuration.EELFManager;
33 import java.util.Arrays;
34 import java.util.Date;
35 import java.util.List;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
43 import org.onap.appc.domainmodel.lcm.CommonHeader;
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.Status;
49 import org.onap.appc.domainmodel.lcm.TransactionRecord;
50 import org.onap.appc.domainmodel.lcm.VNFOperation;
51 import org.onap.appc.exceptions.APPCException;
52 import org.onap.appc.exceptions.InvalidInputException;
53 import org.onap.appc.executor.objects.LCMCommandStatus;
54 import org.onap.appc.lockmanager.api.LockException;
55 import org.onap.appc.messageadapter.MessageAdapter;
56 import org.onap.appc.metricservice.MetricRegistry;
57 import org.onap.appc.metricservice.impl.MetricRegistryImpl;
58 import org.onap.appc.metricservice.metric.MetricType;
59 import org.onap.appc.metricservice.metric.impl.DispatchingFuntionMetricImpl;
60 import org.onap.appc.requesthandler.exceptions.RequestValidationException;
61 import org.onap.appc.requesthandler.helper.RequestValidator;
62 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
63 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
64 import org.onap.appc.transactionrecorder.TransactionRecorder;
65 import org.osgi.framework.FrameworkUtil;
66 import org.powermock.api.mockito.PowerMockito;
67 import org.powermock.core.classloader.annotations.PrepareForTest;
68 import org.powermock.modules.junit4.PowerMockRunner;
69 import org.powermock.reflect.Whitebox;
71 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({FrameworkUtil.class})
73 public class AbstractRequestHandlerImplTest implements LocalRequestHanlderTestHelper {
75 private AbstractRequestHandlerImpl requestHandler;
76 private TransactionRecorder recorder;
79 public void setUp() throws Exception {
80 setupForHandlerImplTests();
81 requestHandler = spy(new LocalRequestHandlerImpl());
82 recorder = mock(TransactionRecorder.class);
83 requestHandler.setTransactionRecorder(recorder);
84 List<RequestStatus> result = Arrays.asList(RequestStatus.ACCEPTED);
85 PowerMockito.when(recorder.getRecords(anyString(), anyString(), anyString(), anyString())).thenReturn(result);
86 final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractRequestHandlerImpl.class);
87 logger.setLevel(Level.TRACE);
88 Whitebox.setInternalState(requestHandler, "logger", logger);
92 public void testHandleRequestAbstractRequestHandler() {
93 RequestHandlerInput rhi = setupTestForHandleRequest();
94 RequestValidator rv = mock(RequestValidator.class);
95 doNothing().when(requestHandler).handleRequest(Mockito.any(RuntimeContext.class));
96 requestHandler.setRequestValidator(rv);
97 Assert.assertTrue(requestHandler.handleRequest(rhi) instanceof RequestHandlerOutput);
101 public void testHandleRequestAbstractRequestHandlerRequestValidationException() throws Exception {
102 RequestHandlerInput rhi = setupTestForHandleRequest();
103 RequestValidator rv = mock(RequestValidator.class);
104 RequestValidationException rve = new RequestValidationException("TEST");
105 rve.setTargetEntity("TEST_TARGET_ENTITY");
106 rve.setTargetService("TEST_TARGET_SERVICE");
107 rve.setLogMessage("TEST_LOG_MESSAGE");
108 rve.setLcmCommandStatus(LCMCommandStatus.SUCCESS);
110 RequestHandlerOutput output = null;
111 doThrow(rve).when(rv).validateRequest(Mockito.any(RuntimeContext.class));
112 doNothing().when(requestHandler).handleRequest(Mockito.any(RuntimeContext.class));
113 requestHandler.setRequestValidator(rv);
114 output = requestHandler.handleRequest(rhi);
115 Assert.assertEquals(LCMCommandStatus.SUCCESS.getResponseCode(),
116 output.getResponseContext().getStatus().getCode());
120 public void testHandleRequestAbstractRequestHandlerInvalidInputException() throws Exception {
121 RequestHandlerInput rhi = setupTestForHandleRequest();
122 RequestValidator rv = mock(RequestValidator.class);
123 InvalidInputException iie = new InvalidInputException("TEST");
124 RequestHandlerOutput output = null;
125 doThrow(iie).when(rv).validateRequest(Mockito.any(RuntimeContext.class));
126 doNothing().when(requestHandler).handleRequest(Mockito.any(RuntimeContext.class));
127 requestHandler.setRequestValidator(rv);
128 output = requestHandler.handleRequest(rhi);
129 Assert.assertEquals(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode(),
130 output.getResponseContext().getStatus().getCode());
134 public void testHandleRequestAbstractRequestHandlerLockException() throws Exception {
135 RequestHandlerInput rhi = setupTestNoMetric();
136 RequestValidator rv = mock(RequestValidator.class);
137 LockException le = new LockException("TEST");
138 RequestHandlerOutput output = null;
139 doThrow(le).when(rv).validateRequest(Mockito.any(RuntimeContext.class));
140 doNothing().when(requestHandler).handleRequest(Mockito.any(RuntimeContext.class));
141 requestHandler.setRequestValidator(rv);
142 output = requestHandler.handleRequest(rhi);
143 Assert.assertEquals(LCMCommandStatus.LOCKED_VNF_ID.getResponseCode(),
144 output.getResponseContext().getStatus().getCode());
148 public void testHandleRequestAbstractRequestHandlerException() throws Exception {
149 RequestHandlerInput rhi = setupTestNoMetric();
150 RequestValidator rv = mock(RequestValidator.class);
151 Exception exception = new Exception("TEST");
152 RequestHandlerOutput output = null;
153 doThrow(exception).when(rv).validateRequest(Mockito.any(RuntimeContext.class));
154 doNothing().when(requestHandler).handleRequest(Mockito.any(RuntimeContext.class));
155 requestHandler.setRequestValidator(rv);
156 output = requestHandler.handleRequest(rhi);
157 Assert.assertEquals(LCMCommandStatus.UNEXPECTED_ERROR.getResponseCode(),
158 output.getResponseContext().getStatus().getCode());
162 public void testOnRequestExecutionEnd() {
163 RuntimeContext runtimeContext = spy(new RuntimeContext());
164 TransactionRecord record = new TransactionRecord();
165 record.setRequestState(RequestStatus.ACCEPTED);
166 runtimeContext.setTransactionRecord(record);
167 MessageAdapter messageAdapter = mock(MessageAdapter.class);
168 requestHandler.setMessageAdapter(messageAdapter);
169 RequestContext rc = mock(RequestContext.class);
170 Status status = new Status();
172 ResponseContext responseContext = spy(new ResponseContext());
173 Mockito.doReturn(status).when(responseContext).getStatus();
174 Mockito.doReturn(VNFOperation.ActionStatus).when(rc).getAction();
175 Mockito.doReturn(rc).when(runtimeContext).getRequestContext();
176 Mockito.doReturn(responseContext).when(runtimeContext).getResponseContext();
177 requestHandler.onRequestExecutionEnd(runtimeContext);
178 Mockito.verify(runtimeContext, times(3)).getTransactionRecord();
182 public void testGetInprogressRequestCount() throws APPCException {
184 Mockito.doReturn(19).when(recorder).getInProgressRequestsCount();
185 i = requestHandler.getInprogressRequestCount();
186 Assert.assertEquals(19, i);
189 private RequestHandlerInput setupTestForHandleRequest() {
190 Whitebox.setInternalState(requestHandler, "isMetricEnabled", true);
191 MetricRegistry metricRegistry = spy(new MetricRegistryImpl("TEST_METRIC_REGISTRY"));
192 DispatchingFuntionMetricImpl metric =
193 spy(new DispatchingFuntionMetricImpl("DISPATCH_FUNCTION", MetricType.COUNTER, 0, 0));
194 metricRegistry.register(metric);
195 doNothing().when(metric).incrementAcceptedRequest();
196 Whitebox.setInternalState(RequestHandlerImpl.class, "metricRegistry", metricRegistry);
197 RequestHandlerInput rhi = new RequestHandlerInput();
198 RequestContext rc = new RequestContext();
199 CommonHeader ch = new CommonHeader();
200 rc.setCommonHeader(ch);
201 ch.setRequestId("TEST");
202 ch.setTimestamp(new Date(System.currentTimeMillis()));
203 VNFOperation vo = VNFOperation.findByString("ActionStatus");
205 ActionIdentifiers ai = new ActionIdentifiers();
206 rc.setActionIdentifiers(ai);
207 rhi.setRequestContext(rc);
211 private RequestHandlerInput setupTestNoMetric() {
212 RequestHandlerInput rhi = new RequestHandlerInput();
213 RequestContext rc = new RequestContext();
214 CommonHeader ch = new CommonHeader();
215 rc.setCommonHeader(ch);
216 ch.setRequestId("TEST");
217 ch.setTimestamp(new Date(System.currentTimeMillis()));
218 VNFOperation vo = VNFOperation.findByString("ActionStatus");
220 ActionIdentifiers ai = new ActionIdentifiers();
221 rc.setActionIdentifiers(ai);
222 rhi.setRequestContext(rc);