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