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.dbmap.DBConnectionType;
27 import org.onap.aai.exceptions.AAIException;
28 import org.onap.aai.introspection.Introspector;
29 import org.onap.aai.introspection.Loader;
30 import org.onap.aai.introspection.ModelType;
31 import org.onap.aai.introspection.Version;
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;
41 import javax.ws.rs.core.*;
42 import java.io.UnsupportedEncodingException;
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.UUID;
48 import static org.mockito.Matchers.anyObject;
49 import static org.mockito.Mockito.doReturn;
50 import static org.mockito.Mockito.when;
52 public class HttpTestUtil extends RESTAPI {
54 private static final EELFLogger logger = EELFManager.getInstance().getLogger(HttpTestUtil.class);
56 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
58 private static final String EMPTY = "";
59 private final QueryStyle queryStyle;
61 protected HttpHeaders httpHeaders;
62 protected UriInfo uriInfo;
64 protected MultivaluedMap<String, String> headersMultiMap;
65 protected MultivaluedMap<String, String> queryParameters;
67 protected List<String> aaiRequestContextList;
68 protected List<MediaType> outputMediaTypes;
70 public HttpTestUtil(QueryStyle qs) {
76 httpHeaders = Mockito.mock(HttpHeaders.class);
77 uriInfo = Mockito.mock(UriInfo.class);
79 headersMultiMap = new MultivaluedHashMap<>();
80 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
82 headersMultiMap.add("X-FromAppId", "JUNIT");
83 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
84 headersMultiMap.add("Real-Time", "true");
85 headersMultiMap.add("Accept", "application/json");
86 headersMultiMap.add("aai-request-context", "");
88 outputMediaTypes = new ArrayList<>();
89 outputMediaTypes.add(APPLICATION_JSON);
91 aaiRequestContextList = new ArrayList<>();
92 aaiRequestContextList.add("");
94 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
95 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
97 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
99 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
100 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
102 doReturn(null).when(queryParameters).remove(anyObject());
103 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
106 public Response doPut(String uri, String payload) throws UnsupportedEncodingException, AAIException {
109 Response response = null;
110 boolean success = true;
111 TransactionalGraphEngine dbEngine = null;
115 uri = uri.replaceAll("/aai/", "");
116 logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
118 String [] arr = uri.split("/");
120 Version version = null;
122 if(arr != null && arr.length > 1){
123 if(arr[0].matches("^v\\d+")){
124 version = Version.getVersion(arr[0]);
125 uri = uri.replaceAll("^v\\d+", "");
130 version = Version.getLatest();
132 Mockito.when(uriInfo.getPath()).thenReturn(uri);
134 DBConnectionType type = DBConnectionType.REALTIME;
135 HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, queryStyle, type);
136 Loader loader = httpEntry.getLoader();
137 dbEngine = httpEntry.getDbEngine();
139 URI uriObject = UriBuilder.fromPath(uri).build();
140 URIToObject uriToObject = new URIToObject(loader, uriObject);
142 String objType = uriToObject.getEntityName();
143 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
146 logger.info("Unmarshalling the payload to this {}", objType);
149 HttpMethod httpMethod;
150 if(uri.contains("/relationship-list/relationship")){
151 obj = loader.unmarshal("relationship", payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
152 httpMethod = HttpMethod.PUT_EDGE;
154 obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
155 httpMethod = HttpMethod.PUT;
156 this.validateIntrospector(obj, loader, uriObject, httpMethod);
160 DBRequest dbRequest =
161 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
162 .rawRequestContent(payload).build();
164 List<DBRequest> dbRequestList = new ArrayList<>();
165 dbRequestList.add(dbRequest);
167 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = httpEntry.process(dbRequestList, "JUNIT");
168 response = responsesTuple.getValue1().get(0).getValue1();
170 } catch (AAIException e) {
171 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
173 } catch(Exception e){
174 AAIException ex = new AAIException("AAI_4000", e);
175 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
179 if(response != null){
180 if((response.getStatus() / 100) == 2){
181 logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
183 logFailure(HttpMethod.PUT, response);
188 if(response != null) {
189 logFailure(HttpMethod.PUT, response);
198 public Response doGet(String uri) throws UnsupportedEncodingException, AAIException {
201 Response response = null;
202 boolean success = true;
203 TransactionalGraphEngine dbEngine = null;
207 uri = uri.replaceAll("/aai/", "");
208 logger.info("Starting the GET request for the uri {} with depth {}", uri, "all");
210 String [] arr = uri.split("/");
212 Version version = null;
214 if(arr != null && arr.length > 1){
215 if(arr[0].matches("^v\\d+")){
216 version = Version.getVersion(arr[0]);
217 uri = uri.replaceAll("^v\\d+", "");
222 version = Version.getLatest();
225 DBConnectionType type = DBConnectionType.REALTIME;
226 HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, queryStyle, type);
227 Loader loader = httpEntry.getLoader();
228 dbEngine = httpEntry.getDbEngine();
230 URI uriObject = UriBuilder.fromPath(uri).build();
231 URIToObject uriToObject = new URIToObject(loader, uriObject);
233 String objType = uriToObject.getEntityName();
234 queryParameters.add("depth", "all");
235 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
237 Mockito.when(uriInfo.getPath()).thenReturn(uri);
239 logger.info("Unmarshalling the payload to this {}", objType);
241 Introspector obj = loader.introspectorFromName(objType);
243 DBRequest dbRequest =
244 new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
247 List<DBRequest> dbRequestList = new ArrayList<>();
248 dbRequestList.add(dbRequest);
250 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = httpEntry.process(dbRequestList, "JUNIT");
251 response = responsesTuple.getValue1().get(0).getValue1();
253 } catch (AAIException e) {
254 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
256 } catch(Exception e){
257 AAIException ex = new AAIException("AAI_4000", e);
258 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
262 if(response != null){
263 if((response.getStatus() / 100) == 2){
264 logger.info("Successfully completed the GET request with status {} and committing it to DB", response.getStatus());
266 logFailure(HttpMethod.GET, response);
271 logFailure(HttpMethod.GET, response);
279 public Response doDelete(String uri, String resourceVersion) throws UnsupportedEncodingException, AAIException {
282 Response response = null;
283 boolean success = true;
284 TransactionalGraphEngine dbEngine = null;
288 uri = uri.replaceAll("/aai/", "");
289 logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
291 String [] arr = uri.split("/");
293 Version version = null;
295 if(arr != null && arr.length > 1){
296 if(arr[0].matches("^v\\d+")){
297 version = Version.getVersion(arr[0]);
298 if(!uri.contains("relationship-list/relationship")){
299 uri = uri.replaceAll("^v\\d+", "");
305 version = Version.getLatest();
308 Mockito.when(uriInfo.getPath()).thenReturn(uri);
309 DBConnectionType type = DBConnectionType.REALTIME;
310 HttpEntry httpEntry = new HttpEntry(version, ModelType.MOXY, queryStyle, type);
311 Loader loader = httpEntry.getLoader();
312 dbEngine = httpEntry.getDbEngine();
314 URI uriObject = UriBuilder.fromPath(uri).build();
315 URIToObject uriToObject = new URIToObject(loader, uriObject);
317 String objType = uriToObject.getEntityName();
318 queryParameters.add("resource-version", resourceVersion);
319 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
321 logger.info("Unmarshalling the payload to this {}", objType);
324 HttpMethod httpMethod;
325 if(uri.contains("/relationship-list/relationship")){
326 obj = loader.introspectorFromName("relationship");
327 httpMethod = HttpMethod.DELETE_EDGE;
329 obj = loader.introspectorFromName(objType);
330 httpMethod = HttpMethod.DELETE;
333 DBRequest dbRequest =
334 new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION")
337 List<DBRequest> dbRequestList = new ArrayList<>();
338 dbRequestList.add(dbRequest);
340 Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = httpEntry.process(dbRequestList, "JUNIT");
341 response = responsesTuple.getValue1().get(0).getValue1();
343 } catch (AAIException e) {
344 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
346 } catch(Exception e){
347 AAIException ex = new AAIException("AAI_4000", e);
348 response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
352 if(response != null){
353 if((response.getStatus() / 100) == 2){
354 logger.info("Successfully completed the DELETE request with status {} and committing it to DB", response.getStatus());
356 logFailure(HttpMethod.DELETE, response);
361 logFailure(HttpMethod.DELETE, response);
369 public static void logFailure(HttpMethod httpMethod, Response response){
370 logger.info("Unable to complete the {} request with status {} and rolling back", httpMethod.toString(), response.getStatus());
371 logger.info("Response body of failed request {}", response.getEntity());