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 java.io.UnsupportedEncodingException;
46 import static org.mockito.Matchers.anyObject;
47 import static org.mockito.Mockito.doReturn;
48 import static org.mockito.Mockito.when;
50 public class HttpTestUtil extends RESTAPI {
52 private static final EELFLogger logger = EELFManager.getInstance().getLogger(HttpTestUtil.class);
54 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
56 private static final String EMPTY = "";
58 protected HttpHeaders httpHeaders;
59 protected UriInfo uriInfo;
61 protected MultivaluedMap<String, String> headersMultiMap;
62 protected MultivaluedMap<String, String> queryParameters;
64 protected List<String> aaiRequestContextList;
65 protected List<MediaType> outputMediaTypes;
67 private SchemaVersions schemaVersions;
71 schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
72 httpHeaders = Mockito.mock(HttpHeaders.class);
73 uriInfo = Mockito.mock(UriInfo.class);
75 headersMultiMap = new MultivaluedHashMap<>();
76 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
78 headersMultiMap.add("X-FromAppId", "JUNIT");
79 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
80 headersMultiMap.add("Real-Time", "true");
81 headersMultiMap.add("Accept", "application/json");
82 headersMultiMap.add("aai-request-context", "");
84 outputMediaTypes = new ArrayList<>();
85 outputMediaTypes.add(APPLICATION_JSON);
87 aaiRequestContextList = new ArrayList<>();
88 aaiRequestContextList.add("");
90 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
91 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
93 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
95 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
96 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
98 doReturn(null).when(queryParameters).remove(anyObject());
99 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
102 public Response doPut(String uri, String payload) throws UnsupportedEncodingException, AAIException {
105 Response response = null;
106 boolean success = true;
107 TransactionalGraphEngine dbEngine = null;
111 uri = uri.replaceAll("/aai/", "");
112 logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
114 String [] arr = uri.split("/");
116 SchemaVersion version = null;
118 if(arr != null && arr.length > 1){
119 if(arr[0].matches("^v\\d+")){
120 version = new SchemaVersion(arr[0]);
121 uri = uri.replaceAll("^v\\d+", "");
126 version = schemaVersions.getDefaultVersion();
128 Mockito.when(uriInfo.getPath()).thenReturn(uri);
130 DBConnectionType type = DBConnectionType.REALTIME;
131 HttpEntry resourceHttpEntry = SpringContextAware.getBean("traversalHttpEntry", HttpEntry.class);
132 resourceHttpEntry.setHttpEntryProperties(version, type);
133 // HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, QueryStyle.TRAVERSAL, type);
134 Loader loader = resourceHttpEntry.getLoader();
135 dbEngine = resourceHttpEntry.getDbEngine();
137 URI uriObject = UriBuilder.fromPath(uri).build();
138 URIToObject uriToObject = new URIToObject(loader, uriObject);
140 String objType = uriToObject.getEntityName();
141 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
144 logger.info("Unmarshalling the payload to this {}", objType);
147 HttpMethod httpMethod;
148 if(uri.contains("/relationship-list/relationship")){
149 obj = loader.unmarshal("relationship", payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
150 httpMethod = HttpMethod.PUT_EDGE;
152 obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
153 httpMethod = HttpMethod.PUT;
154 this.validateIntrospector(obj, loader, uriObject, httpMethod);
158 DBRequest dbRequest =
159 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
160 .rawRequestContent(payload).build();
162 List<DBRequest> dbRequestList = new ArrayList<>();
163 dbRequestList.add(dbRequest);
165 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = resourceHttpEntry.process(dbRequestList, "JUNIT");
166 response = responsesTuple.getValue1().get(0).getValue1();
168 } catch (AAIException e) {
169 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
171 } catch(Exception e){
172 AAIException ex = new AAIException("AAI_4000", e);
173 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
177 if(response != null){
178 if((response.getStatus() / 100) == 2){
179 logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
181 logFailure(HttpMethod.PUT, response);
186 if(response != null) {
187 logFailure(HttpMethod.PUT, response);
196 public Response doGet(String uri) throws UnsupportedEncodingException, AAIException {
199 Response response = null;
200 boolean success = true;
201 TransactionalGraphEngine dbEngine = null;
205 uri = uri.replaceAll("/aai/", "");
206 logger.info("Starting the GET request for the uri {} with depth {}", uri, "all");
208 String [] arr = uri.split("/");
210 SchemaVersion version = null;
212 if(arr != null && arr.length > 1){
213 if(arr[0].matches("^v\\d+")){
214 version = new SchemaVersion(arr[0]);
215 uri = uri.replaceAll("^v\\d+", "");
220 version = schemaVersions.getDefaultVersion();
223 DBConnectionType type = DBConnectionType.REALTIME;
225 //HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, QueryStyle.TRAVERSAL, type);
226 HttpEntry resourceHttpEntry = SpringContextAware.getBean("traversalHttpEntry", HttpEntry.class);
227 resourceHttpEntry.setHttpEntryProperties(version, type);
229 Loader loader = resourceHttpEntry.getLoader();
230 dbEngine = resourceHttpEntry.getDbEngine();
232 URI uriObject = UriBuilder.fromPath(uri).build();
233 URIToObject uriToObject = new URIToObject(loader, uriObject);
235 String objType = uriToObject.getEntityName();
236 queryParameters.add("depth", "all");
237 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
239 Mockito.when(uriInfo.getPath()).thenReturn(uri);
241 logger.info("Unmarshalling the payload to this {}", objType);
243 Introspector obj = loader.introspectorFromName(objType);
245 DBRequest dbRequest =
246 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
249 List<DBRequest> dbRequestList = new ArrayList<>();
250 dbRequestList.add(dbRequest);
252 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = resourceHttpEntry.process(dbRequestList, "JUNIT");
253 response = responsesTuple.getValue1().get(0).getValue1();
255 } catch (AAIException e) {
256 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
258 } catch(Exception e){
259 AAIException ex = new AAIException("AAI_4000", e);
260 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
264 if(response != null){
265 if((response.getStatus() / 100) == 2){
266 logger.info("Successfully completed the GET request with status {} and committing it to DB", response.getStatus());
268 logFailure(HttpMethod.GET, response);
273 logFailure(HttpMethod.GET, response);
281 public Response doDelete(String uri, String resourceVersion) throws UnsupportedEncodingException, AAIException {
284 Response response = null;
285 boolean success = true;
286 TransactionalGraphEngine dbEngine = null;
290 uri = uri.replaceAll("/aai/", "");
291 logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
293 String [] arr = uri.split("/");
295 SchemaVersion version = null;
297 if(arr != null && arr.length > 1){
298 if(arr[0].matches("^v\\d+")){
299 version = new SchemaVersion(arr[0]);
300 if(!uri.contains("relationship-list/relationship")){
301 uri = uri.replaceAll("^v\\d+", "");
307 version = schemaVersions.getDefaultVersion();
310 Mockito.when(uriInfo.getPath()).thenReturn(uri);
311 DBConnectionType type = DBConnectionType.REALTIME;
312 HttpEntry resourceHttpEntry = SpringContextAware.getBean("traversalHttpEntry", HttpEntry.class);
313 resourceHttpEntry.setHttpEntryProperties(version, type);
314 // HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, QueryStyle.TRAVERSAL, type);
315 Loader loader = resourceHttpEntry.getLoader();
316 dbEngine = resourceHttpEntry.getDbEngine();
318 URI uriObject = UriBuilder.fromPath(uri).build();
319 URIToObject uriToObject = new URIToObject(loader, uriObject);
321 String objType = uriToObject.getEntityName();
322 queryParameters.add("resource-version", resourceVersion);
323 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
325 logger.info("Unmarshalling the payload to this {}", objType);
328 HttpMethod httpMethod;
329 if(uri.contains("/relationship-list/relationship")){
330 obj = loader.introspectorFromName("relationship");
331 httpMethod = HttpMethod.DELETE_EDGE;
333 obj = loader.introspectorFromName(objType);
334 httpMethod = HttpMethod.DELETE;
337 DBRequest dbRequest =
338 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
341 List<DBRequest> dbRequestList = new ArrayList<>();
342 dbRequestList.add(dbRequest);
344 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = resourceHttpEntry.process(dbRequestList, "JUNIT");
345 response = responsesTuple.getValue1().get(0).getValue1();
347 } catch (AAIException e) {
348 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
350 } catch(Exception e){
351 AAIException ex = new AAIException("AAI_4000", e);
352 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
356 if(response != null){
357 if((response.getStatus() / 100) == 2){
358 logger.info("Successfully completed the DELETE request with status {} and committing it to DB", response.getStatus());
360 logFailure(HttpMethod.DELETE, response);
365 logFailure(HttpMethod.DELETE, response);
373 public static void logFailure(HttpMethod httpMethod, Response response){
374 logger.info("Unable to complete the {} request with status {} and rolling back", httpMethod.toString(), response.getStatus());
375 logger.info("Response body of failed request {}", response.getEntity());