Change code to use dmaap microservice
[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-2019 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.domainmodel.lcm.ActionIdentifiers;
28 import org.onap.appc.domainmodel.lcm.ActionLevel;
29 import org.onap.appc.domainmodel.lcm.CommonHeader;
30 import org.onap.appc.domainmodel.lcm.Flags;
31 import org.onap.appc.domainmodel.lcm.RequestContext;
32 import org.onap.appc.domainmodel.lcm.RequestStatus;
33 import org.onap.appc.domainmodel.lcm.ResponseContext;
34 import org.onap.appc.domainmodel.lcm.RuntimeContext;
35 import org.onap.appc.domainmodel.lcm.VNFOperation;
36 import org.onap.appc.lockmanager.api.LockManager;
37 import org.onap.appc.requesthandler.RequestHandler;
38 import org.onap.appc.transactionrecorder.TransactionRecorder;
39 import org.osgi.framework.Bundle;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.osgi.framework.ServiceReference;
43 import org.powermock.api.mockito.PowerMockito;
44 import static org.mockito.Matchers.any;
45 import static org.mockito.Matchers.anyCollectionOf;
46 import static org.mockito.Matchers.anyString;
47 import static org.mockito.Mockito.mock;
48 import static org.powermock.api.mockito.PowerMockito.mockStatic;
49 import java.util.Arrays;
50 import java.util.Date;
51 import java.util.List;
52
53 /**
54  * Creates RequestContextInput for ActionStatus JUnits
55  */
56 public interface LocalRequestHanlderTestHelper {
57     default RuntimeContext createRequestHandlerRuntimeContext(String vnfId, String payload) {
58         RuntimeContext context = new RuntimeContext();
59
60         RequestContext requestContext = createRequestContext(VNFOperation.ActionStatus, "requestId1",
61             vnfId, ActionLevel.MGMT, payload);
62         context.setRequestContext(requestContext);
63
64         ResponseContext resContext = new ResponseContext();
65         resContext.setCommonHeader(context.getRequestContext().getCommonHeader());
66         context.setResponseContext(resContext);
67
68         return context;
69     }
70
71     default RequestContext createRequestContext(VNFOperation operation, String requestId, String vnfId,
72                                                 ActionLevel level, String payload) {
73         RequestContext reqContext = new RequestContext();
74         reqContext.setCommonHeader(getCommonHeader(requestId, "2.0.0", "originatorId"));
75         reqContext.setActionLevel(level);
76         reqContext.setAction(operation);
77         reqContext.setPayload(payload);
78         reqContext.setActionIdentifiers(getActionIdentifiers(vnfId, null, null));
79
80         return reqContext;
81     }
82
83     default ActionIdentifiers getActionIdentifiers(String vnfId, String vnfcId, String vserverId) {
84         ActionIdentifiers builder = new ActionIdentifiers();
85         builder.setVnfId(vnfId);
86         builder.setVnfcName(vnfcId);
87         builder.setvServerId(vserverId);
88         return builder;
89     }
90
91     default CommonHeader getCommonHeader(String requestId, String apiVer, String originatorId) {
92         CommonHeader builder = new CommonHeader();
93         builder.setRequestId(requestId);
94         builder.setApiVer(apiVer);
95         builder.setOriginatorId(originatorId);
96         builder.setTimestamp(new Date(System.currentTimeMillis()));
97         return builder;
98     }
99
100     default CommonHeader getCommonHeader(String requestId, Date date) {
101         CommonHeader builder = new CommonHeader();
102         builder.setRequestId(requestId);
103         builder.setTimestamp(date);
104         Flags flags = new Flags();
105         flags.setTtl(0);
106         builder.setFlags(flags);
107         return builder;
108     }
109
110     default void setupForHandlerImplTests() {
111         mockStatic(FrameworkUtil.class);
112         Bundle myBundle = mock(Bundle.class);
113         PowerMockito.when(FrameworkUtil.getBundle(any())).thenReturn(myBundle);
114
115         BundleContext myBundleContext = mock(BundleContext.class);
116         Mockito.when(myBundle.getBundleContext()).thenReturn(myBundleContext);
117     }
118 }