123737f4f6b899091d3b856a83a4fb2422002942
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / MsoRestClientTestUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.mso.rest;
22
23 import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
24 import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
25 import static com.xebialabs.restito.semantics.Action.contentType;
26 import static com.xebialabs.restito.semantics.Action.status;
27 import static com.xebialabs.restito.semantics.Action.stringContent;
28 import static com.xebialabs.restito.semantics.Condition.delete;
29 import static com.xebialabs.restito.semantics.Condition.get;
30 import static com.xebialabs.restito.semantics.Condition.method;
31 import static com.xebialabs.restito.semantics.Condition.post;
32 import static com.xebialabs.restito.semantics.Condition.uri;
33 import static com.xebialabs.restito.semantics.Condition.withHeader;
34 import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals;
35 import static org.onap.vid.logging.Headers.PARTNER_NAME;
36 import static org.onap.vid.utils.Logging.ONAP_REQUEST_ID_HEADER_KEY;
37
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.xebialabs.restito.semantics.Action;
40 import com.xebialabs.restito.server.StubServer;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.LinkedList;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.function.BiFunction;
48 import java.util.function.Function;
49 import javax.ws.rs.core.HttpHeaders;
50 import javax.ws.rs.core.MediaType;
51 import org.glassfish.grizzly.http.Method;
52 import org.glassfish.grizzly.http.util.HttpStatus;
53 import org.json.JSONObject;
54 import org.junit.Assert;
55 import org.onap.portalsdk.core.util.SystemProperties;
56 import org.onap.vid.changeManagement.RelatedInstanceList;
57 import org.onap.vid.changeManagement.RequestDetailsWrapper;
58 import org.onap.vid.changeManagement.WorkflowRequestDetail;
59 import org.onap.vid.mso.MsoResponseWrapper;
60 import org.onap.vid.mso.model.CloudConfiguration;
61 import org.onap.vid.mso.model.ModelInfo;
62 import org.onap.vid.mso.model.RequestInfo;
63 import org.onap.vid.mso.model.RequestParameters;
64
65 class MsoRestClientTestUtil implements AutoCloseable {
66   private final StubServer server;
67   private final String endpoint;
68   private final String responsePayload;
69   private final HttpStatus expectedStatus;
70   private final String expectedResponseStr;
71
72   MsoRestClientTestUtil(StubServer server, String endpoint, HttpStatus expectedStatus,
73       String responsePayload,
74       String expectedResponseStr) {
75     this.server = server;
76     this.endpoint = endpoint;
77     this.responsePayload = responsePayload;
78     this.expectedStatus = expectedStatus;
79     this.expectedResponseStr = expectedResponseStr;
80   }
81
82   void executePost(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func) throws IOException {
83     whenHttp(server)
84         .match(post(endpoint))
85         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
86
87     RequestDetails sampleRequestDetails =
88         new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
89
90     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
91     JSONObject actualJson = new JSONObject(response.getEntity());
92
93     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
94     Assert.assertEquals(expectedResponseStr, actualJson.toString());
95     verifyServer(server, endpoint, Method.POST);
96
97   }
98   void executePostCall(String jsonPayload, BiFunction<RequestDetailsWrapper, String, MsoResponseWrapper> func) throws IOException {
99     whenHttp(server)
100             .match(post(endpoint))
101             .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
102
103     RequestDetailsWrapper  sampleRequestDetails =
104             new ObjectMapper().readValue(jsonPayload, RequestDetailsWrapper.class);
105
106     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
107     JSONObject actualJson = new JSONObject(response.getEntity());
108
109     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
110     Assert.assertEquals(expectedResponseStr, actualJson.toString());
111     verifyServer(server, endpoint, Method.POST);
112   }
113
114   void executeDelete(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func)
115       throws IOException {
116     whenHttp(server)
117         .match(delete(endpoint))
118         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
119
120     RequestDetails sampleRequestDetails =
121         new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
122     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
123
124     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
125     assertJsonEquals(expectedResponseStr, response.getEntity());
126     verifyServer(server, endpoint, Method.DELETE);
127   }
128
129   void executeGet(Function<String, MsoResponseWrapper> func) {
130     whenHttp(server)
131         .match(get(endpoint))
132         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
133
134     MsoResponseWrapper response = func.apply(endpoint);
135
136     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
137     assertJsonEquals(expectedResponseStr, response.getEntity());
138     verifyServer(server, endpoint, Method.GET);
139   }
140
141   static org.onap.vid.changeManagement.RequestDetails generateMockMsoRequest() {
142     org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
143     requestDetails.setVnfInstanceId("vnf-instance-id");
144     requestDetails.setVnfName("vnf-name");
145     CloudConfiguration cloudConfiguration = new CloudConfiguration();
146     cloudConfiguration.setTenantId("tenant-id");
147     cloudConfiguration.setLcpCloudRegionId("lcp-region");
148     requestDetails.setCloudConfiguration(cloudConfiguration);
149     ModelInfo modelInfo = new ModelInfo();
150     modelInfo.setModelInvariantId("model-invarient-id");
151     modelInfo.setModelCustomizationName("modelCustomizationName");
152     requestDetails.setModelInfo(modelInfo);
153     RequestInfo requestInfo = new RequestInfo();
154     requestInfo.setRequestorId("ok883e");
155     requestInfo.setSource("VID");
156     requestDetails.setRequestInfo(requestInfo);
157     RequestParameters requestParameters = new RequestParameters();
158     requestParameters.setSubscriptionServiceType("subscriber-service-type");
159     requestParameters.setAdditionalProperty("a", 1);
160     requestParameters.setAdditionalProperty("b", 2);
161     requestParameters.setAdditionalProperty("c", 3);
162     requestParameters.setAdditionalProperty("d", 4);
163     String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}";
164     requestParameters.setAdditionalProperty("payload", payload);
165
166     requestDetails.setRequestParameters(requestParameters);
167     return requestDetails;
168   }
169
170   static org.onap.vid.changeManagement.RequestDetails generateChangeManagementMockMsoRequest() {
171     List<RelatedInstanceList> relatedInstances = new LinkedList<>();
172     relatedInstances.add(new RelatedInstanceList());
173
174     org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
175
176     requestDetails.setVnfName("test-vnf-name");
177     requestDetails.setVnfInstanceId("test-vnf-instance_id");
178     requestDetails.setRelatedInstList(relatedInstances);
179
180     CloudConfiguration cloudConfiguration = new CloudConfiguration();
181     cloudConfiguration.setTenantId("tenant-id");
182     cloudConfiguration.setLcpCloudRegionId("lcp-region");
183     requestDetails.setCloudConfiguration(cloudConfiguration);
184
185     ModelInfo modelInfo = new ModelInfo();
186     modelInfo.setModelInvariantId("model-invarient-id");
187     modelInfo.setModelCustomizationName("modelCustomizationName");
188     modelInfo.setModelType("test-model-type");
189     requestDetails.setModelInfo(modelInfo);
190
191     RequestInfo requestInfo = new RequestInfo();
192     requestInfo.setRequestorId("ok883e");
193     requestInfo.setSource("VID");
194     requestDetails.setRequestInfo(requestInfo);
195
196     RequestParameters requestParameters = new RequestParameters();
197     requestParameters.setSubscriptionServiceType("subscriber-service-type");
198     requestParameters.setAdditionalProperty("a", 1);
199     requestParameters.setAdditionalProperty("b", 2);
200     requestParameters.setAdditionalProperty("c", 3);
201     requestParameters.setAdditionalProperty("d", 4);
202     String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}";
203     requestParameters.setAdditionalProperty("payload", payload);
204
205     requestDetails.setRequestParameters(requestParameters);
206     return requestDetails;
207   }
208
209   static WorkflowRequestDetail createWorkflowRequestDetail() {
210     WorkflowRequestDetail workflowRequestDetail = new WorkflowRequestDetail();
211     org.onap.vid.changeManagement.RequestParameters requestParameters = new org.onap.vid.changeManagement.RequestParameters();
212     HashMap<String,String> paramsMap = new HashMap<>();
213     paramsMap.put("testKey1","testValue1");
214     paramsMap.put("testKey2","testValue2");
215
216     List<Map<String,String>> mapArray= new ArrayList<>();
217     mapArray.add(paramsMap);
218     requestParameters.setUserParams(mapArray);
219
220     CloudConfiguration cloudConfiguration = new CloudConfiguration();
221     cloudConfiguration.setCloudOwner("testOwne");
222     cloudConfiguration.setTenantId("testId");
223     cloudConfiguration.setLcpCloudRegionId("testLcpCloudId");
224
225     workflowRequestDetail.setRequestParameters(requestParameters);
226     workflowRequestDetail.setCloudConfiguration(cloudConfiguration);
227     return workflowRequestDetail;
228   }
229
230   private void verifyServer(StubServer server, String endpoint, Method httpMethod) {
231     verifyHttp(server).once(
232         method(httpMethod),
233         uri(endpoint),
234         withHeader(HttpHeaders.AUTHORIZATION),
235         withHeader(HttpHeaders.ACCEPT),
236         withHeader(HttpHeaders.CONTENT_TYPE),
237         withHeader(MsoRestClientNew.X_FROM_APP_ID),
238         withHeader(PARTNER_NAME.getHeaderName(), "VID.VID"),
239         withHeader(SystemProperties.ECOMP_REQUEST_ID),
240         withHeader(ONAP_REQUEST_ID_HEADER_KEY)
241     );
242   }
243
244   private Action jsonContent(String str) {
245     return stringContent(str);
246   }
247
248   @Override
249   public void close() {
250   }
251 }
252