a6bbe571a3ce80cc5e292d267a2527805231de02
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / test / java / org / onap / appc / requesthandler / impl / LocalRequestHanlderTestHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.requesthandler.impl;
25
26 import org.mockito.Mockito;
27 import org.onap.appc.adapter.message.MessageAdapterFactory;
28 import org.onap.appc.adapter.message.Producer;
29 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
30 import org.onap.appc.domainmodel.lcm.ActionLevel;
31 import org.onap.appc.domainmodel.lcm.CommonHeader;
32 import org.onap.appc.domainmodel.lcm.Flags;
33 import org.onap.appc.domainmodel.lcm.RequestContext;
34 import org.onap.appc.domainmodel.lcm.RequestStatus;
35 import org.onap.appc.domainmodel.lcm.ResponseContext;
36 import org.onap.appc.domainmodel.lcm.RuntimeContext;
37 import org.onap.appc.domainmodel.lcm.VNFOperation;
38 import org.onap.appc.lockmanager.api.LockManager;
39 import org.onap.appc.requesthandler.RequestHandler;
40 import org.onap.appc.transactionrecorder.TransactionRecorder;
41 import org.osgi.framework.Bundle;
42 import org.osgi.framework.BundleContext;
43 import org.osgi.framework.FrameworkUtil;
44 import org.osgi.framework.ServiceReference;
45 import org.powermock.api.mockito.PowerMockito;
46 import static org.mockito.Matchers.any;
47 import static org.mockito.Matchers.anyCollectionOf;
48 import static org.mockito.Matchers.anyString;
49 import static org.mockito.Mockito.mock;
50 import static org.powermock.api.mockito.PowerMockito.mockStatic;
51 import java.util.Arrays;
52 import java.util.Date;
53 import java.util.List;
54
55 /**
56  * Creates RequestContextInput for ActionStatus JUnits
57  */
58 public interface LocalRequestHanlderTestHelper {
59     default RuntimeContext createRequestHandlerRuntimeContext(String vnfId, String payload) {
60         RuntimeContext context = new RuntimeContext();
61
62         RequestContext requestContext = createRequestContext(VNFOperation.ActionStatus, "requestId1",
63             vnfId, ActionLevel.MGMT, payload);
64         context.setRequestContext(requestContext);
65
66         ResponseContext resContext = new ResponseContext();
67         resContext.setCommonHeader(context.getRequestContext().getCommonHeader());
68         context.setResponseContext(resContext);
69
70         return context;
71     }
72
73     default RequestContext createRequestContext(VNFOperation operation, String requestId, String vnfId,
74                                                 ActionLevel level, String payload) {
75         RequestContext reqContext = new RequestContext();
76         reqContext.setCommonHeader(getCommonHeader(requestId, "2.0.0", "originatorId"));
77         reqContext.setActionLevel(level);
78         reqContext.setAction(operation);
79         reqContext.setPayload(payload);
80         reqContext.setActionIdentifiers(getActionIdentifiers(vnfId, null, null));
81
82         return reqContext;
83     }
84
85     default ActionIdentifiers getActionIdentifiers(String vnfId, String vnfcId, String vserverId) {
86         ActionIdentifiers builder = new ActionIdentifiers();
87         builder.setVnfId(vnfId);
88         builder.setVnfcName(vnfcId);
89         builder.setvServerId(vserverId);
90         return builder;
91     }
92
93     default CommonHeader getCommonHeader(String requestId, String apiVer, String originatorId) {
94         CommonHeader builder = new CommonHeader();
95         builder.setRequestId(requestId);
96         builder.setApiVer(apiVer);
97         builder.setOriginatorId(originatorId);
98         builder.setTimestamp(new Date(System.currentTimeMillis()));
99         return builder;
100     }
101
102     default CommonHeader getCommonHeader(String requestId, Date date) {
103         CommonHeader builder = new CommonHeader();
104         builder.setRequestId(requestId);
105         builder.setTimestamp(date);
106         Flags flags = new Flags();
107         flags.setTtl(0);
108         builder.setFlags(flags);
109         return builder;
110     }
111
112     default void setupForHandlerImplTests() {
113         mockStatic(FrameworkUtil.class);
114         Bundle myBundle = mock(Bundle.class);
115         PowerMockito.when(FrameworkUtil.getBundle(any())).thenReturn(myBundle);
116
117         BundleContext myBundleContext = mock(BundleContext.class);
118         Mockito.when(myBundle.getBundleContext()).thenReturn(myBundleContext);
119
120         ServiceReference svcRef = mock(ServiceReference.class);
121         Mockito.when(myBundleContext.getServiceReference(MessageAdapterFactory.class.getName())).thenReturn(svcRef);
122
123         Producer producer = mock(Producer.class);
124         MessageAdapterFactory factory = mock(MessageAdapterFactory.class);
125         Mockito.when(myBundleContext.getService(svcRef)).thenReturn(factory);
126         Mockito.when(factory.createProducer(anyCollectionOf(String.class), anyString(), anyString(), anyString()))
127         .thenReturn(producer);
128     }
129 }