Sending workflow data from UI to SO
[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
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.xebialabs.restito.semantics.Action;
38 import com.xebialabs.restito.server.StubServer;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.LinkedList;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.function.BiFunction;
46 import java.util.function.Function;
47 import javax.ws.rs.core.HttpHeaders;
48 import javax.ws.rs.core.MediaType;
49 import org.glassfish.grizzly.http.Method;
50 import org.glassfish.grizzly.http.util.HttpStatus;
51 import org.json.JSONObject;
52 import org.junit.Assert;
53 import org.onap.portalsdk.core.util.SystemProperties;
54 import org.onap.vid.changeManagement.RelatedInstanceList;
55 import org.onap.vid.changeManagement.RequestDetailsWrapper;
56 import org.onap.vid.changeManagement.WorkflowRequestDetail;
57 import org.onap.vid.mso.MsoResponseWrapper;
58 import org.onap.vid.mso.model.CloudConfiguration;
59 import org.onap.vid.mso.model.ModelInfo;
60 import org.onap.vid.mso.model.RequestInfo;
61 import org.onap.vid.mso.model.RequestParameters;
62
63 class MsoRestClientTestUtil implements AutoCloseable {
64   private final StubServer server;
65   private final String endpoint;
66   private final String responsePayload;
67   private final HttpStatus expectedStatus;
68   private final String expectedResponseStr;
69
70   MsoRestClientTestUtil(StubServer server, String endpoint, HttpStatus expectedStatus,
71       String responsePayload,
72       String expectedResponseStr) {
73     this.server = server;
74     this.endpoint = endpoint;
75     this.responsePayload = responsePayload;
76     this.expectedStatus = expectedStatus;
77     this.expectedResponseStr = expectedResponseStr;
78   }
79
80   void executePost(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func) throws IOException {
81     whenHttp(server)
82         .match(post(endpoint))
83         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
84
85     RequestDetails sampleRequestDetails =
86         new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
87
88     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
89     JSONObject actualJson = new JSONObject(response.getEntity());
90
91     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
92     Assert.assertEquals(expectedResponseStr, actualJson.toString());
93     verifyServer(server, endpoint, Method.POST);
94
95   }
96   void executePostCall(String jsonPayload, BiFunction<RequestDetailsWrapper, String, MsoResponseWrapper> func) throws IOException {
97     whenHttp(server)
98             .match(post(endpoint))
99             .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
100
101     RequestDetailsWrapper  sampleRequestDetails =
102             new ObjectMapper().readValue(jsonPayload, RequestDetailsWrapper.class);
103
104     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
105     JSONObject actualJson = new JSONObject(response.getEntity());
106
107     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
108     Assert.assertEquals(expectedResponseStr, actualJson.toString());
109     verifyServer(server, endpoint, Method.POST);
110   }
111
112   void executeDelete(String jsonPayload, BiFunction<RequestDetails, String, MsoResponseWrapper> func)
113       throws IOException {
114     whenHttp(server)
115         .match(delete(endpoint))
116         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
117
118     RequestDetails sampleRequestDetails =
119         new ObjectMapper().readValue(jsonPayload, RequestDetails.class);
120     MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint);
121
122     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
123     assertJsonEquals(expectedResponseStr, response.getEntity());
124     verifyServer(server, endpoint, Method.DELETE);
125   }
126
127   void executeGet(Function<String, MsoResponseWrapper> func) {
128     whenHttp(server)
129         .match(get(endpoint))
130         .then(status(expectedStatus), jsonContent(responsePayload), contentType(MediaType.APPLICATION_JSON));
131
132     MsoResponseWrapper response = func.apply(endpoint);
133
134     Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus());
135     assertJsonEquals(expectedResponseStr, response.getEntity());
136     verifyServer(server, endpoint, Method.GET);
137   }
138
139   static org.onap.vid.changeManagement.RequestDetails generateMockMsoRequest() {
140     org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
141     requestDetails.setVnfInstanceId("vnf-instance-id");
142     requestDetails.setVnfName("vnf-name");
143     CloudConfiguration cloudConfiguration = new CloudConfiguration();
144     cloudConfiguration.setTenantId("tenant-id");
145     cloudConfiguration.setLcpCloudRegionId("lcp-region");
146     requestDetails.setCloudConfiguration(cloudConfiguration);
147     ModelInfo modelInfo = new ModelInfo();
148     modelInfo.setModelInvariantId("model-invarient-id");
149     modelInfo.setModelCustomizationName("modelCustomizationName");
150     requestDetails.setModelInfo(modelInfo);
151     RequestInfo requestInfo = new RequestInfo();
152     requestInfo.setRequestorId("ok883e");
153     requestInfo.setSource("VID");
154     requestDetails.setRequestInfo(requestInfo);
155     RequestParameters requestParameters = new RequestParameters();
156     requestParameters.setSubscriptionServiceType("subscriber-service-type");
157     requestParameters.setAdditionalProperty("a", 1);
158     requestParameters.setAdditionalProperty("b", 2);
159     requestParameters.setAdditionalProperty("c", 3);
160     requestParameters.setAdditionalProperty("d", 4);
161     String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}";
162     requestParameters.setAdditionalProperty("payload", payload);
163
164     requestDetails.setRequestParameters(requestParameters);
165     return requestDetails;
166   }
167
168   static org.onap.vid.changeManagement.RequestDetails generateChangeManagementMockMsoRequest() {
169     List<RelatedInstanceList> relatedInstances = new LinkedList<>();
170     relatedInstances.add(new RelatedInstanceList());
171
172     org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
173
174     requestDetails.setVnfName("test-vnf-name");
175     requestDetails.setVnfInstanceId("test-vnf-instance_id");
176     requestDetails.setRelatedInstList(relatedInstances);
177
178     CloudConfiguration cloudConfiguration = new CloudConfiguration();
179     cloudConfiguration.setTenantId("tenant-id");
180     cloudConfiguration.setLcpCloudRegionId("lcp-region");
181     requestDetails.setCloudConfiguration(cloudConfiguration);
182
183     ModelInfo modelInfo = new ModelInfo();
184     modelInfo.setModelInvariantId("model-invarient-id");
185     modelInfo.setModelCustomizationName("modelCustomizationName");
186     modelInfo.setModelType("test-model-type");
187     requestDetails.setModelInfo(modelInfo);
188
189     RequestInfo requestInfo = new RequestInfo();
190     requestInfo.setRequestorId("ok883e");
191     requestInfo.setSource("VID");
192     requestDetails.setRequestInfo(requestInfo);
193
194     RequestParameters requestParameters = new RequestParameters();
195     requestParameters.setSubscriptionServiceType("subscriber-service-type");
196     requestParameters.setAdditionalProperty("a", 1);
197     requestParameters.setAdditionalProperty("b", 2);
198     requestParameters.setAdditionalProperty("c", 3);
199     requestParameters.setAdditionalProperty("d", 4);
200     String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}";
201     requestParameters.setAdditionalProperty("payload", payload);
202
203     requestDetails.setRequestParameters(requestParameters);
204     return requestDetails;
205   }
206
207   static WorkflowRequestDetail createWorkflowRequestDetail() {
208     WorkflowRequestDetail workflowRequestDetail = new WorkflowRequestDetail();
209     org.onap.vid.changeManagement.RequestParameters requestParameters = new org.onap.vid.changeManagement.RequestParameters();
210     HashMap<String,String> paramsMap = new HashMap<>();
211     paramsMap.put("testKey1","testValue1");
212     paramsMap.put("testKey2","testValue2");
213
214     List<Map<String,String>> mapArray= new ArrayList<>();
215     mapArray.add(paramsMap);
216     requestParameters.setUserParams(mapArray);
217
218     CloudConfiguration cloudConfiguration = new CloudConfiguration();
219     cloudConfiguration.setCloudOwner("testOwne");
220     cloudConfiguration.setTenantId("testId");
221     cloudConfiguration.setLcpCloudRegionId("testLcpCloudId");
222
223     workflowRequestDetail.setRequestParameters(requestParameters);
224     workflowRequestDetail.setCloudConfiguration(cloudConfiguration);
225     return workflowRequestDetail;
226   }
227
228   private void verifyServer(StubServer server, String endpoint, Method httpMethod) {
229     verifyHttp(server).once(
230         method(httpMethod),
231         uri(endpoint),
232         withHeader(HttpHeaders.AUTHORIZATION),
233         withHeader(HttpHeaders.ACCEPT),
234         withHeader(HttpHeaders.CONTENT_TYPE),
235         withHeader(MsoRestClientNew.X_FROM_APP_ID),
236         withHeader(SystemProperties.ECOMP_REQUEST_ID));
237   }
238
239   private Action jsonContent(String str) {
240     return stringContent(str);
241   }
242
243   @Override
244   public void close() {
245   }
246 }
247