2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.javatuples.Pair;
25 import org.mockito.Mockito;
26 import org.onap.aai.config.SpringContextAware;
27 import org.onap.aai.dbmap.DBConnectionType;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.introspection.Introspector;
30 import org.onap.aai.introspection.Loader;
31 import org.onap.aai.setup.SchemaVersion;
32 import org.onap.aai.parsers.query.QueryParser;
33 import org.onap.aai.parsers.uri.URIToObject;
34 import org.onap.aai.rest.db.DBRequest;
35 import org.onap.aai.rest.db.HttpEntry;
36 import org.onap.aai.restcore.HttpMethod;
37 import org.onap.aai.restcore.RESTAPI;
38 import org.onap.aai.serialization.engines.QueryStyle;
39 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
40 import org.onap.aai.setup.SchemaVersions;
42 import javax.ws.rs.core.*;
43 import java.io.UnsupportedEncodingException;
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.UUID;
49 import static org.mockito.Matchers.anyObject;
50 import static org.mockito.Mockito.doReturn;
51 import static org.mockito.Mockito.when;
53 public class HttpTestUtil extends RESTAPI {
56 protected HttpEntry traversalHttpEntry;
58 protected HttpEntry traversalUriHttpEntry;
60 private static final EELFLogger logger = EELFManager.getInstance().getLogger(HttpTestUtil.class);
62 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
64 private static final String EMPTY = "";
65 private final QueryStyle queryStyle;
67 protected HttpHeaders httpHeaders;
68 protected UriInfo uriInfo;
70 protected MultivaluedMap<String, String> headersMultiMap;
71 protected MultivaluedMap<String, String> queryParameters;
73 protected List<String> aaiRequestContextList;
74 protected List<MediaType> outputMediaTypes;
76 public HttpTestUtil(QueryStyle qs) {
78 traversalHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
79 traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
85 httpHeaders = Mockito.mock(HttpHeaders.class);
86 uriInfo = Mockito.mock(UriInfo.class);
88 headersMultiMap = new MultivaluedHashMap<>();
89 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
91 headersMultiMap.add("X-FromAppId", "JUNIT");
92 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
93 headersMultiMap.add("Real-Time", "true");
94 headersMultiMap.add("Accept", "application/json");
95 headersMultiMap.add("aai-request-context", "");
97 outputMediaTypes = new ArrayList<>();
98 outputMediaTypes.add(APPLICATION_JSON);
100 aaiRequestContextList = new ArrayList<>();
101 aaiRequestContextList.add("");
103 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
104 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
106 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
108 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
109 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
111 doReturn(null).when(queryParameters).remove(anyObject());
112 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
115 public Response doPut(String uri, String payload) throws UnsupportedEncodingException, AAIException {
118 Response response = null;
119 boolean success = true;
120 TransactionalGraphEngine dbEngine = null;
124 if(uri.startsWith("/aai/")){
125 uri = uri.substring(5);
128 logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
130 String [] arr = uri.split("/");
132 SchemaVersion version = null;
134 if(arr != null && arr.length > 1){
135 if(arr[0].matches("^v\\d+")){
136 version = new SchemaVersion(arr[0]);
137 uri = uri.replaceAll("^v\\d+", "");
141 SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
143 version = schemaVersions.getDefaultVersion();
145 Mockito.when(uriInfo.getPath()).thenReturn(uri);
147 DBConnectionType type = DBConnectionType.REALTIME;
149 traversalHttpEntry.setHttpEntryProperties(version, type);
150 Loader loader = traversalHttpEntry.getLoader();
151 dbEngine = traversalHttpEntry.getDbEngine();
153 URI uriObject = UriBuilder.fromPath(uri).build();
154 URIToObject uriToObject = new URIToObject(loader, uriObject);
156 String objType = uriToObject.getEntityName();
157 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
160 logger.info("Unmarshalling the payload to this {}", objType);
163 HttpMethod httpMethod;
164 if(uri.contains("/relationship-list/relationship")){
165 obj = loader.unmarshal("relationship", payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
166 httpMethod = HttpMethod.PUT_EDGE;
168 obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
169 httpMethod = HttpMethod.PUT;
170 this.validateIntrospector(obj, loader, uriObject, httpMethod);
174 DBRequest dbRequest =
175 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
176 .rawRequestContent(payload).build();
178 List<DBRequest> dbRequestList = new ArrayList<>();
179 dbRequestList.add(dbRequest);
181 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
182 response = responsesTuple.getValue1().get(0).getValue1();
184 } catch (AAIException e) {
185 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
187 } catch(Exception e){
188 AAIException ex = new AAIException("AAI_4000", e);
189 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
193 if(response != null){
194 if((response.getStatus() / 100) == 2){
195 logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
197 logFailure(HttpMethod.PUT, response);
202 if(response != null) {
203 logFailure(HttpMethod.PUT, response);
212 public Response doGet(String uri, String depth){
215 Response response = null;
216 boolean success = true;
217 TransactionalGraphEngine dbEngine = null;
221 if(uri.startsWith("/aai/")){
222 uri = uri.substring(5);
225 logger.info("Starting the GET request for the uri {} with depth {}", uri, depth);
227 String [] arr = uri.split("/");
229 SchemaVersion version = null;
231 if(arr != null && arr.length > 1){
232 if(arr[0].matches("^v\\d+")){
233 version = new SchemaVersion(arr[0]);
234 uri = uri.replaceAll("^v\\d+", "");
238 SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
240 version = schemaVersions.getDefaultVersion();
243 DBConnectionType type = DBConnectionType.REALTIME;
244 traversalHttpEntry.setHttpEntryProperties(version, type);
245 Loader loader = traversalHttpEntry.getLoader();
246 dbEngine = traversalHttpEntry.getDbEngine();
248 URI uriObject = UriBuilder.fromPath(uri).build();
251 queryParameters.add("depth", depth);
254 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
256 Mockito.when(uriInfo.getPath()).thenReturn(uri);
258 URIToObject uriToObject = new URIToObject(loader, uriObject);
260 if (!uriQuery.getContainerType().equals("")) {
261 objType = uriQuery.getContainerType();
263 objType = uriQuery.getResultType();
265 logger.info("Unmarshalling the payload to this {}", objType);
267 Introspector obj = loader.introspectorFromName(objType);
269 DBRequest dbRequest =
270 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
273 List<DBRequest> dbRequestList = new ArrayList<>();
274 dbRequestList.add(dbRequest);
276 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
277 response = responsesTuple.getValue1().get(0).getValue1();
279 } catch (AAIException e) {
280 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
282 } catch(Exception e){
283 AAIException ex = new AAIException("AAI_4000", e);
284 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
288 if(response != null){
289 if((response.getStatus() / 100) == 2){
290 logger.info("Successfully completed the GET request with status {} and committing it to DB", response.getStatus());
292 logFailure(HttpMethod.GET, response);
297 logFailure(HttpMethod.GET, response);
305 public Response doGet(String uri) throws UnsupportedEncodingException, AAIException {
306 return this.doGet(uri, "all");
309 public Response doDelete(String uri, String resourceVersion) throws UnsupportedEncodingException, AAIException {
312 Response response = null;
313 boolean success = true;
314 TransactionalGraphEngine dbEngine = null;
318 uri = uri.replaceAll("/aai/", "");
319 logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
321 String [] arr = uri.split("/");
323 SchemaVersion version = null;
325 if(arr != null && arr.length > 1){
326 if(arr[0].matches("^v\\d+")){
327 version = new SchemaVersion(arr[0]);
328 if(!uri.contains("relationship-list/relationship")){
329 uri = uri.replaceAll("^v\\d+", "");
334 SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
336 version = schemaVersions.getDefaultVersion();
339 Mockito.when(uriInfo.getPath()).thenReturn(uri);
340 DBConnectionType type = DBConnectionType.REALTIME;
341 traversalHttpEntry.setHttpEntryProperties(version, type);
343 traversalHttpEntry.setHttpEntryProperties(version, type);
344 Loader loader = traversalHttpEntry.getLoader();
345 dbEngine = traversalHttpEntry.getDbEngine();
347 URI uriObject = UriBuilder.fromPath(uri).build();
348 URIToObject uriToObject = new URIToObject(loader, uriObject);
350 String objType = uriToObject.getEntityName();
351 queryParameters.add("resource-version", resourceVersion);
352 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
354 logger.info("Unmarshalling the payload to this {}", objType);
357 HttpMethod httpMethod;
358 if(uri.contains("/relationship-list/relationship")){
359 obj = loader.introspectorFromName("relationship");
360 httpMethod = HttpMethod.DELETE_EDGE;
362 obj = loader.introspectorFromName(objType);
363 httpMethod = HttpMethod.DELETE;
366 DBRequest dbRequest =
367 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
370 List<DBRequest> dbRequestList = new ArrayList<>();
371 dbRequestList.add(dbRequest);
373 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
374 response = responsesTuple.getValue1().get(0).getValue1();
376 } catch (AAIException e) {
377 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
379 } catch(Exception e){
380 AAIException ex = new AAIException("AAI_4000", e);
381 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
385 if(response != null){
386 if((response.getStatus() / 100) == 2){
387 logger.info("Successfully completed the DELETE request with status {} and committing it to DB", response.getStatus());
389 logFailure(HttpMethod.DELETE, response);
394 logFailure(HttpMethod.DELETE, response);
402 public static void logFailure(HttpMethod httpMethod, Response response){
403 logger.info("Unable to complete the {} request with status {} and rolling back", httpMethod.toString(), response.getStatus());
404 logger.info("Response body of failed request {}", response.getEntity());