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 uri = uri.replaceAll("/aai/", "");
125 logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
127 String [] arr = uri.split("/");
129 SchemaVersion version = null;
131 if(arr != null && arr.length > 1){
132 if(arr[0].matches("^v\\d+")){
133 version = new SchemaVersion(arr[0]);
134 uri = uri.replaceAll("^v\\d+", "");
138 SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
140 version = schemaVersions.getDefaultVersion();
142 Mockito.when(uriInfo.getPath()).thenReturn(uri);
144 DBConnectionType type = DBConnectionType.REALTIME;
146 traversalHttpEntry.setHttpEntryProperties(version, type);
147 Loader loader = traversalHttpEntry.getLoader();
148 dbEngine = traversalHttpEntry.getDbEngine();
150 URI uriObject = UriBuilder.fromPath(uri).build();
151 URIToObject uriToObject = new URIToObject(loader, uriObject);
153 String objType = uriToObject.getEntityName();
154 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
157 logger.info("Unmarshalling the payload to this {}", objType);
160 HttpMethod httpMethod;
161 if(uri.contains("/relationship-list/relationship")){
162 obj = loader.unmarshal("relationship", payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
163 httpMethod = HttpMethod.PUT_EDGE;
165 obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
166 httpMethod = HttpMethod.PUT;
167 this.validateIntrospector(obj, loader, uriObject, httpMethod);
171 DBRequest dbRequest =
172 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
173 .rawRequestContent(payload).build();
175 List<DBRequest> dbRequestList = new ArrayList<>();
176 dbRequestList.add(dbRequest);
178 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
179 response = responsesTuple.getValue1().get(0).getValue1();
181 } catch (AAIException e) {
182 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
184 } catch(Exception e){
185 AAIException ex = new AAIException("AAI_4000", e);
186 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
190 if(response != null){
191 if((response.getStatus() / 100) == 2){
192 logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
194 logFailure(HttpMethod.PUT, response);
199 if(response != null) {
200 logFailure(HttpMethod.PUT, response);
209 public Response doGet(String uri) throws UnsupportedEncodingException, AAIException {
212 Response response = null;
213 boolean success = true;
214 TransactionalGraphEngine dbEngine = null;
218 uri = uri.replaceAll("/aai/", "");
219 logger.info("Starting the GET request for the uri {} with depth {}", uri, "all");
221 String [] arr = uri.split("/");
223 SchemaVersion version = null;
225 if(arr != null && arr.length > 1){
226 if(arr[0].matches("^v\\d+")){
227 version = new SchemaVersion(arr[0]);
228 uri = uri.replaceAll("^v\\d+", "");
232 SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
234 version = schemaVersions.getDefaultVersion();
237 DBConnectionType type = DBConnectionType.REALTIME;
238 traversalHttpEntry.setHttpEntryProperties(version, type);
239 Loader loader = traversalHttpEntry.getLoader();
240 dbEngine = traversalHttpEntry.getDbEngine();
242 URI uriObject = UriBuilder.fromPath(uri).build();
243 URIToObject uriToObject = new URIToObject(loader, uriObject);
245 String objType = uriToObject.getEntityName();
246 queryParameters.add("depth", "all");
247 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
249 Mockito.when(uriInfo.getPath()).thenReturn(uri);
251 logger.info("Unmarshalling the payload to this {}", objType);
253 Introspector obj = loader.introspectorFromName(objType);
255 DBRequest dbRequest =
256 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
259 List<DBRequest> dbRequestList = new ArrayList<>();
260 dbRequestList.add(dbRequest);
262 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
263 response = responsesTuple.getValue1().get(0).getValue1();
265 } catch (AAIException e) {
266 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
268 } catch(Exception e){
269 AAIException ex = new AAIException("AAI_4000", e);
270 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
274 if(response != null){
275 if((response.getStatus() / 100) == 2){
276 logger.info("Successfully completed the GET request with status {} and committing it to DB", response.getStatus());
278 logFailure(HttpMethod.GET, response);
283 logFailure(HttpMethod.GET, response);
291 public Response doDelete(String uri, String resourceVersion) throws UnsupportedEncodingException, AAIException {
294 Response response = null;
295 boolean success = true;
296 TransactionalGraphEngine dbEngine = null;
300 uri = uri.replaceAll("/aai/", "");
301 logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
303 String [] arr = uri.split("/");
305 SchemaVersion version = null;
307 if(arr != null && arr.length > 1){
308 if(arr[0].matches("^v\\d+")){
309 version = new SchemaVersion(arr[0]);
310 if(!uri.contains("relationship-list/relationship")){
311 uri = uri.replaceAll("^v\\d+", "");
316 SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
318 version = schemaVersions.getDefaultVersion();
321 Mockito.when(uriInfo.getPath()).thenReturn(uri);
322 DBConnectionType type = DBConnectionType.REALTIME;
323 traversalHttpEntry.setHttpEntryProperties(version, type);
325 traversalHttpEntry.setHttpEntryProperties(version, type);
326 Loader loader = traversalHttpEntry.getLoader();
327 dbEngine = traversalHttpEntry.getDbEngine();
329 URI uriObject = UriBuilder.fromPath(uri).build();
330 URIToObject uriToObject = new URIToObject(loader, uriObject);
332 String objType = uriToObject.getEntityName();
333 queryParameters.add("resource-version", resourceVersion);
334 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
336 logger.info("Unmarshalling the payload to this {}", objType);
339 HttpMethod httpMethod;
340 if(uri.contains("/relationship-list/relationship")){
341 obj = loader.introspectorFromName("relationship");
342 httpMethod = HttpMethod.DELETE_EDGE;
344 obj = loader.introspectorFromName(objType);
345 httpMethod = HttpMethod.DELETE;
348 DBRequest dbRequest =
349 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
352 List<DBRequest> dbRequestList = new ArrayList<>();
353 dbRequestList.add(dbRequest);
355 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
356 response = responsesTuple.getValue1().get(0).getValue1();
358 } catch (AAIException e) {
359 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
361 } catch(Exception e){
362 AAIException ex = new AAIException("AAI_4000", e);
363 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
367 if(response != null){
368 if((response.getStatus() / 100) == 2){
369 logger.info("Successfully completed the DELETE request with status {} and committing it to DB", response.getStatus());
371 logFailure(HttpMethod.DELETE, response);
376 logFailure(HttpMethod.DELETE, response);
384 public static void logFailure(HttpMethod httpMethod, Response response){
385 logger.info("Unable to complete the {} request with status {} and rolling back", httpMethod.toString(), response.getStatus());
386 logger.info("Response body of failed request {}", response.getEntity());