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.parsers.query.QueryParser;
32 import org.onap.aai.parsers.uri.URIToObject;
33 import org.onap.aai.rest.db.DBRequest;
34 import org.onap.aai.rest.db.HttpEntry;
35 import org.onap.aai.restcore.HttpMethod;
36 import org.onap.aai.restcore.RESTAPI;
37 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
38 import org.onap.aai.setup.SchemaVersion;
39 import org.onap.aai.setup.SchemaVersions;
41 import javax.ws.rs.core.*;
42 import javax.xml.validation.Schema;
43 import java.io.UnsupportedEncodingException;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.doReturn;
49 import static org.mockito.Mockito.when;
51 public class HttpTestUtil extends RESTAPI {
53 private static final EELFLogger logger = EELFManager.getInstance().getLogger(HttpTestUtil.class);
55 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
57 private static final String EMPTY = "";
59 protected HttpHeaders httpHeaders;
60 protected UriInfo uriInfo;
62 protected MultivaluedMap<String, String> headersMultiMap;
63 protected MultivaluedMap<String, String> queryParameters;
65 protected List<String> aaiRequestContextList;
66 protected List<MediaType> outputMediaTypes;
68 private SchemaVersions schemaVersions;
72 schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
73 httpHeaders = Mockito.mock(HttpHeaders.class);
74 uriInfo = Mockito.mock(UriInfo.class);
76 headersMultiMap = new MultivaluedHashMap<>();
77 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
79 headersMultiMap.add("X-FromAppId", "JUNIT");
80 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
81 headersMultiMap.add("Real-Time", "true");
82 headersMultiMap.add("Accept", "application/json");
83 headersMultiMap.add("aai-request-context", "");
85 outputMediaTypes = new ArrayList<>();
86 outputMediaTypes.add(APPLICATION_JSON);
88 aaiRequestContextList = new ArrayList<>();
89 aaiRequestContextList.add("");
91 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
92 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
94 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
96 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
97 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
99 doReturn(null).when(queryParameters).remove(anyObject());
100 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
103 public Response doPut(String uri, String payload) throws UnsupportedEncodingException, AAIException {
106 Response response = null;
107 boolean success = true;
108 TransactionalGraphEngine dbEngine = null;
112 uri = uri.replaceAll("/aai/", "");
113 logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
115 String [] arr = uri.split("/");
117 SchemaVersion version = null;
119 if(arr != null && arr.length > 1){
120 if(arr[0].matches("^v\\d+")){
121 version = new SchemaVersion(arr[0]);
122 uri = uri.replaceAll("^v\\d+", "");
127 version = schemaVersions.getDefaultVersion();
129 Mockito.when(uriInfo.getPath()).thenReturn(uri);
131 DBConnectionType type = DBConnectionType.REALTIME;
132 HttpEntry resourceHttpEntry = SpringContextAware.getBean("traversalHttpEntry", HttpEntry.class);
133 resourceHttpEntry.setHttpEntryProperties(version, type);
134 // HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, QueryStyle.TRAVERSAL, type);
135 Loader loader = resourceHttpEntry.getLoader();
136 dbEngine = resourceHttpEntry.getDbEngine();
138 URI uriObject = UriBuilder.fromPath(uri).build();
139 URIToObject uriToObject = new URIToObject(loader, uriObject);
141 String objType = uriToObject.getEntityName();
142 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
145 logger.info("Unmarshalling the payload to this {}", objType);
148 HttpMethod httpMethod;
149 if(uri.contains("/relationship-list/relationship")){
150 obj = loader.unmarshal("relationship", payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
151 httpMethod = HttpMethod.PUT_EDGE;
153 obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
154 httpMethod = HttpMethod.PUT;
155 this.validateIntrospector(obj, loader, uriObject, httpMethod);
159 DBRequest dbRequest =
160 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
161 .rawRequestContent(payload).build();
163 List<DBRequest> dbRequestList = new ArrayList<>();
164 dbRequestList.add(dbRequest);
166 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = resourceHttpEntry.process(dbRequestList, "JUNIT");
167 response = responsesTuple.getValue1().get(0).getValue1();
169 } catch (AAIException e) {
170 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
172 } catch(Exception e){
173 AAIException ex = new AAIException("AAI_4000", e);
174 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
178 if(response != null){
179 if((response.getStatus() / 100) == 2){
180 logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
182 logFailure(HttpMethod.PUT, response);
187 if(response != null) {
188 logFailure(HttpMethod.PUT, response);
197 public Response doGet(String uri) throws UnsupportedEncodingException, AAIException {
200 Response response = null;
201 boolean success = true;
202 TransactionalGraphEngine dbEngine = null;
206 uri = uri.replaceAll("/aai/", "");
207 logger.info("Starting the GET request for the uri {} with depth {}", uri, "all");
209 String [] arr = uri.split("/");
211 SchemaVersion version = null;
213 if(arr != null && arr.length > 1){
214 if(arr[0].matches("^v\\d+")){
215 version = new SchemaVersion(arr[0]);
216 uri = uri.replaceAll("^v\\d+", "");
221 version = schemaVersions.getDefaultVersion();
224 DBConnectionType type = DBConnectionType.REALTIME;
226 //HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, QueryStyle.TRAVERSAL, type);
227 HttpEntry resourceHttpEntry = SpringContextAware.getBean("traversalHttpEntry", HttpEntry.class);
228 resourceHttpEntry.setHttpEntryProperties(version, type);
230 Loader loader = resourceHttpEntry.getLoader();
231 dbEngine = resourceHttpEntry.getDbEngine();
233 URI uriObject = UriBuilder.fromPath(uri).build();
234 URIToObject uriToObject = new URIToObject(loader, uriObject);
236 String objType = uriToObject.getEntityName();
237 queryParameters.add("depth", "all");
238 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
240 Mockito.when(uriInfo.getPath()).thenReturn(uri);
242 logger.info("Unmarshalling the payload to this {}", objType);
244 Introspector obj = loader.introspectorFromName(objType);
246 DBRequest dbRequest =
247 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
250 List<DBRequest> dbRequestList = new ArrayList<>();
251 dbRequestList.add(dbRequest);
253 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = resourceHttpEntry.process(dbRequestList, "JUNIT");
254 response = responsesTuple.getValue1().get(0).getValue1();
256 } catch (AAIException e) {
257 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
259 } catch(Exception e){
260 AAIException ex = new AAIException("AAI_4000", e);
261 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
265 if(response != null){
266 if((response.getStatus() / 100) == 2){
267 logger.info("Successfully completed the GET request with status {} and committing it to DB", response.getStatus());
269 logFailure(HttpMethod.GET, response);
274 logFailure(HttpMethod.GET, response);
282 public Response doDelete(String uri, String resourceVersion) throws UnsupportedEncodingException, AAIException {
285 Response response = null;
286 boolean success = true;
287 TransactionalGraphEngine dbEngine = null;
291 uri = uri.replaceAll("/aai/", "");
292 logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
294 String [] arr = uri.split("/");
296 SchemaVersion version = null;
298 if(arr != null && arr.length > 1){
299 if(arr[0].matches("^v\\d+")){
300 version = new SchemaVersion(arr[0]);
301 if(!uri.contains("relationship-list/relationship")){
302 uri = uri.replaceAll("^v\\d+", "");
308 version = schemaVersions.getDefaultVersion();
311 Mockito.when(uriInfo.getPath()).thenReturn(uri);
312 DBConnectionType type = DBConnectionType.REALTIME;
313 HttpEntry resourceHttpEntry = SpringContextAware.getBean("traversalHttpEntry", HttpEntry.class);
314 resourceHttpEntry.setHttpEntryProperties(version, type);
315 // HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, QueryStyle.TRAVERSAL, type);
316 Loader loader = resourceHttpEntry.getLoader();
317 dbEngine = resourceHttpEntry.getDbEngine();
319 URI uriObject = UriBuilder.fromPath(uri).build();
320 URIToObject uriToObject = new URIToObject(loader, uriObject);
322 String objType = uriToObject.getEntityName();
323 queryParameters.add("resource-version", resourceVersion);
324 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
326 logger.info("Unmarshalling the payload to this {}", objType);
329 HttpMethod httpMethod;
330 if(uri.contains("/relationship-list/relationship")){
331 obj = loader.introspectorFromName("relationship");
332 httpMethod = HttpMethod.DELETE_EDGE;
334 obj = loader.introspectorFromName(objType);
335 httpMethod = HttpMethod.DELETE;
338 DBRequest dbRequest =
339 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
342 List<DBRequest> dbRequestList = new ArrayList<>();
343 dbRequestList.add(dbRequest);
345 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = resourceHttpEntry.process(dbRequestList, "JUNIT");
346 response = responsesTuple.getValue1().get(0).getValue1();
348 } catch (AAIException e) {
349 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
351 } catch(Exception e){
352 AAIException ex = new AAIException("AAI_4000", e);
353 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
357 if(response != null){
358 if((response.getStatus() / 100) == 2){
359 logger.info("Successfully completed the DELETE request with status {} and committing it to DB", response.getStatus());
361 logFailure(HttpMethod.DELETE, response);
366 logFailure(HttpMethod.DELETE, response);
374 public static void logFailure(HttpMethod httpMethod, Response response){
375 logger.info("Unable to complete the {} request with status {} and rolling back", httpMethod.toString(), response.getStatus());
376 logger.info("Response body of failed request {}", response.getEntity());