Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / HttpTestUtil.java
index 6a9e725..588cbc9 100644 (file)
 
 package org.onap.aai;
 
-import static org.mockito.Matchers.anyObject;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.when;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
-
-import javax.ws.rs.core.*;
-
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.javatuples.Pair;
 import org.mockito.Mockito;
 import org.onap.aai.config.SpringContextAware;
-import org.onap.aai.dbmap.DBConnectionType;
 import org.onap.aai.exceptions.AAIException;
 import org.onap.aai.introspection.Introspector;
 import org.onap.aai.introspection.Loader;
+import org.onap.aai.introspection.LoaderFactory;
 import org.onap.aai.parsers.query.QueryParser;
 import org.onap.aai.parsers.uri.URIToObject;
 import org.onap.aai.rest.db.DBRequest;
 import org.onap.aai.rest.db.HttpEntry;
+import org.onap.aai.rest.ueb.UEBNotification;
 import org.onap.aai.restcore.HttpMethod;
 import org.onap.aai.restcore.RESTAPI;
 import org.onap.aai.serialization.engines.QueryStyle;
@@ -53,15 +41,24 @@ import org.onap.aai.serialization.engines.TransactionalGraphEngine;
 import org.onap.aai.setup.SchemaVersion;
 import org.onap.aai.setup.SchemaVersions;
 
+import javax.ws.rs.core.*;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.util.*;
+
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.*;
+
 public class HttpTestUtil extends RESTAPI {
 
     protected HttpEntry traversalHttpEntry;
 
     protected HttpEntry traversalUriHttpEntry;
 
-    private static final EELFLogger logger = EELFManager.getInstance().getLogger(HttpTestUtil.class);
+    private static final Logger logger = LoggerFactory.getLogger(HttpTestUtil.class);
 
     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
+    protected static final MediaType APPLICATION_XML = MediaType.valueOf("application/xml");
 
     private static final String EMPTY = "";
     private final QueryStyle queryStyle;
@@ -74,12 +71,40 @@ public class HttpTestUtil extends RESTAPI {
 
     protected List<String> aaiRequestContextList;
     protected List<MediaType> outputMediaTypes;
+    protected LoaderFactory loaderFactory;
+    protected SchemaVersions schemaVersions;
+    protected UEBNotification notification;
+    protected int notificationDepth;
+    protected String acceptType;
 
     public HttpTestUtil(QueryStyle qs) {
+        this(qs, "application/json");
+    }
+
+    public HttpTestUtil(QueryStyle qs, String acceptType) {
         this.queryStyle = qs;
         traversalHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
         traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
+        loaderFactory = SpringContextAware.getBean(LoaderFactory.class);
+        schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
+        notification = null;
+        this.acceptType = acceptType;
+    }
 
+
+    public HttpTestUtil(QueryStyle qs, UEBNotification uebNotification, int notificationDepth) {
+        this(qs, uebNotification, notificationDepth, "application/json");
+    }
+
+    public HttpTestUtil(QueryStyle qs, UEBNotification uebNotification, int notificationDepth, String acceptType) {
+        this.queryStyle = qs;
+        this.traversalHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
+        this.traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
+        this.loaderFactory = SpringContextAware.getBean(LoaderFactory.class);
+        this.schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
+        this.notification = uebNotification;
+        this.notificationDepth = notificationDepth;
+        this.acceptType = acceptType;
     }
 
     public void init() {
@@ -93,11 +118,15 @@ public class HttpTestUtil extends RESTAPI {
         headersMultiMap.add("X-FromAppId", "JUNIT");
         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
         headersMultiMap.add("Real-Time", "true");
-        headersMultiMap.add("Accept", "application/json");
+        headersMultiMap.add("Accept", acceptType);
         headersMultiMap.add("aai-request-context", "");
 
         outputMediaTypes = new ArrayList<>();
-        outputMediaTypes.add(APPLICATION_JSON);
+        if(acceptType.equals("application/json")){
+            outputMediaTypes.add(APPLICATION_JSON);
+        } else {
+            outputMediaTypes.add(APPLICATION_XML);
+        }
 
         aaiRequestContextList = new ArrayList<>();
         aaiRequestContextList.add("");
@@ -112,9 +141,127 @@ public class HttpTestUtil extends RESTAPI {
 
         doReturn(null).when(queryParameters).remove(anyObject());
         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
+
+        try {
+            if(notification != null){
+                doNothing().when(notification).triggerEvents();
+            }
+        } catch (AAIException e) {
+            e.printStackTrace();
+        }
     }
 
     public Response doPut(String uri, String payload) throws UnsupportedEncodingException, AAIException {
+        Map<String, String> puts = new HashMap<>();
+        puts.put(uri, payload);
+        return this.doPut(puts);
+    }
+
+    public Response doPut(Map<String, String> uriPayload) throws UnsupportedEncodingException, AAIException {
+
+        this.init();
+        Response response = null;
+        boolean success = true;
+        TransactionalGraphEngine dbEngine = null;
+
+        try {
+
+            List<DBRequest> dbRequestList = new ArrayList<>();
+            for(Map.Entry<String, String> entry : uriPayload.entrySet()){
+
+                String uri = entry.getKey();
+                String payload = entry.getValue();
+                if (uri.startsWith("/aai/")) {
+                    uri = uri.substring(5);
+                }
+
+                logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
+
+                String[] arr = uri.split("/");
+
+                SchemaVersion version = null;
+
+                if (arr.length > 1) {
+                    if (arr[0].matches("^v\\d+")) {
+                        version = new SchemaVersion(arr[0]);
+                        uri = uri.replaceAll("^v\\d+", "");
+                    }
+                }
+
+                if (version == null) {
+                    version = schemaVersions.getDefaultVersion();
+                }
+                Mockito.when(uriInfo.getPath()).thenReturn(uri);
+
+                if(notification != null){
+                    traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
+                } else {
+                    traversalHttpEntry.setHttpEntryProperties(version);
+                }
+                Loader loader = traversalHttpEntry.getLoader();
+                dbEngine = traversalHttpEntry.getDbEngine();
+
+                URI uriObject = UriBuilder.fromPath(uri).build();
+                URIToObject uriToObject = new URIToObject(loader, uriObject);
+
+                String objType = uriToObject.getEntityName();
+                QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
+
+                logger.info("Unmarshalling the payload to this {}", objType);
+
+                Introspector obj;
+                HttpMethod httpMethod;
+                if (uri.contains("/relationship-list/relationship")) {
+                    obj = loader.unmarshal("relationship", payload,
+                        org.onap.aai.restcore.MediaType.getEnum("application/json"));
+                    httpMethod = HttpMethod.PUT_EDGE;
+                } else {
+                    obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
+                    httpMethod = HttpMethod.PUT;
+                    this.validateIntrospector(obj, loader, uriObject, httpMethod);
+                }
+
+                DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo,
+                    "JUNIT-TRANSACTION").rawRequestContent(payload).build();
+
+                dbRequestList.add(dbRequest);
+
+            }
+
+            Pair<Boolean, List<Pair<URI, Response>>> responsesTuple =
+                traversalHttpEntry.process(dbRequestList, "JUNIT");
+            response = responsesTuple.getValue1().get(0).getValue1();
+
+        } catch (AAIException e) {
+            response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
+            success = false;
+        } catch (Exception e) {
+            AAIException ex = new AAIException("AAI_4000", e);
+            response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
+            success = false;
+        } finally {
+            if (success) {
+                if (response != null) {
+                    if ((response.getStatus() / 100) == 2) {
+                        logger.info("Successfully completed the PUT request with status {} and committing it to DB",
+                            response.getStatus());
+                    } else {
+                        logFailure(HttpMethod.PUT, response);
+                    }
+                }
+                dbEngine.commit();
+            } else {
+                if (response != null) {
+                    logFailure(HttpMethod.PUT, response);
+                }
+                dbEngine.rollback();
+            }
+        }
+
+        return response;
+    }
+
+    public Response doPatch(String uri, String payload) throws UnsupportedEncodingException, AAIException {
 
         this.init();
         Response response = null;
@@ -133,22 +280,23 @@ public class HttpTestUtil extends RESTAPI {
 
             SchemaVersion version = null;
 
-            if (arr != null && arr.length > 1) {
+            if (arr.length > 1) {
                 if (arr[0].matches("^v\\d+")) {
                     version = new SchemaVersion(arr[0]);
                     uri = uri.replaceAll("^v\\d+", "");
                 }
             }
 
-            SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
             if (version == null) {
                 version = schemaVersions.getDefaultVersion();
             }
             Mockito.when(uriInfo.getPath()).thenReturn(uri);
 
-            DBConnectionType type = DBConnectionType.REALTIME;
-
-            traversalHttpEntry.setHttpEntryProperties(version, type);
+            if(notification != null){
+                traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
+            } else {
+                traversalHttpEntry.setHttpEntryProperties(version);
+            }
             Loader loader = traversalHttpEntry.getLoader();
             dbEngine = traversalHttpEntry.getDbEngine();
 
@@ -162,24 +310,18 @@ public class HttpTestUtil extends RESTAPI {
 
             Introspector obj;
             HttpMethod httpMethod;
-            if (uri.contains("/relationship-list/relationship")) {
-                obj = loader.unmarshal("relationship", payload,
-                        org.onap.aai.restcore.MediaType.getEnum("application/json"));
-                httpMethod = HttpMethod.PUT_EDGE;
-            } else {
-                obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
-                httpMethod = HttpMethod.PUT;
-                this.validateIntrospector(obj, loader, uriObject, httpMethod);
-            }
+            obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
+            httpMethod = HttpMethod.MERGE_PATCH;
+            this.validateIntrospector(obj, loader, uriObject, httpMethod);
 
             DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo,
-                    "JUNIT-TRANSACTION").rawRequestContent(payload).build();
+                "JUNIT-TRANSACTION").rawRequestContent(payload).build();
 
             List<DBRequest> dbRequestList = new ArrayList<>();
             dbRequestList.add(dbRequest);
 
             Pair<Boolean, List<Pair<URI, Response>>> responsesTuple =
-                    traversalHttpEntry.process(dbRequestList, "JUNIT");
+                traversalHttpEntry.process(dbRequestList, "JUNIT");
             response = responsesTuple.getValue1().get(0).getValue1();
 
         } catch (AAIException e) {
@@ -194,7 +336,7 @@ public class HttpTestUtil extends RESTAPI {
                 if (response != null) {
                     if ((response.getStatus() / 100) == 2) {
                         logger.info("Successfully completed the PUT request with status {} and committing it to DB",
-                                response.getStatus());
+                            response.getStatus());
                     } else {
                         logFailure(HttpMethod.PUT, response);
                     }
@@ -211,7 +353,11 @@ public class HttpTestUtil extends RESTAPI {
         return response;
     }
 
-    public Response doGet(String uri, String depth) {
+    public Response doGet(String uri, String depth){
+       return doGet(uri, depth, null);
+    }
+
+    public Response doGet(String uri, String depth, String format) {
 
         this.init();
         Response response = null;
@@ -230,20 +376,22 @@ public class HttpTestUtil extends RESTAPI {
 
             SchemaVersion version = null;
 
-            if (arr != null && arr.length > 1) {
+            if (arr.length > 1) {
                 if (arr[0].matches("^v\\d+")) {
                     version = new SchemaVersion(arr[0]);
                     uri = uri.replaceAll("^v\\d+", "");
                 }
             }
 
-            SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
             if (version == null) {
                 version = schemaVersions.getDefaultVersion();
             }
 
-            DBConnectionType type = DBConnectionType.REALTIME;
-            traversalHttpEntry.setHttpEntryProperties(version, type);
+            if(notification != null){
+                traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
+            } else {
+                traversalHttpEntry.setHttpEntryProperties(version);
+            }
             Loader loader = traversalHttpEntry.getLoader();
             dbEngine = traversalHttpEntry.getDbEngine();
 
@@ -253,6 +401,10 @@ public class HttpTestUtil extends RESTAPI {
                 queryParameters.add("depth", depth);
             }
 
+            if(format != null){
+                queryParameters.add("format", format);
+            }
+
             QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
 
             Mockito.when(uriInfo.getPath()).thenReturn(uri);
@@ -309,7 +461,7 @@ public class HttpTestUtil extends RESTAPI {
         return this.doGet(uri, "all");
     }
 
-    public Response doDelete(String uri, String resourceVersion) throws UnsupportedEncodingException, AAIException {
+    public Response doDelete(Map<String, Pair<String, String>> deletes){
 
         this.init();
         Response response = null;
@@ -318,77 +470,81 @@ public class HttpTestUtil extends RESTAPI {
 
         try {
 
-            uri = uri.replaceAll("/aai/", "");
-            logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
+            List<DBRequest> dbRequestList = new ArrayList<>();
+            for (Map.Entry<String, Pair<String, String>> delete : deletes.entrySet()) {
+                String uri = delete.getKey();
+                String resourceVersion = delete.getValue().getValue0();
+                String content = delete.getValue().getValue1();
+                uri = uri.replaceAll("/aai/", "");
+                logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
 
-            String[] arr = uri.split("/");
+                String[] arr = uri.split("/");
 
-            SchemaVersion version = null;
+                SchemaVersion version = null;
 
-            if (arr != null && arr.length > 1) {
-                if (arr[0].matches("^v\\d+")) {
-                    version = new SchemaVersion(arr[0]);
-                    if (!uri.contains("relationship-list/relationship")) {
+                if (arr.length > 1) {
+                    if (arr[0].matches("^v\\d+")) {
+                        version = new SchemaVersion(arr[0]);
                         uri = uri.replaceAll("^v\\d+", "");
                     }
                 }
-            }
 
-            SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
-            if (version == null) {
-                version = schemaVersions.getDefaultVersion();
-            }
+                if (version == null) {
+                    version = schemaVersions.getDefaultVersion();
+                }
 
-            Mockito.when(uriInfo.getPath()).thenReturn(uri);
-            DBConnectionType type = DBConnectionType.REALTIME;
-            traversalHttpEntry.setHttpEntryProperties(version, type);
+                Mockito.when(uriInfo.getPath()).thenReturn(uri);
+                if (notification != null) {
+                    traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
+                } else {
+                    traversalHttpEntry.setHttpEntryProperties(version);
+                }
+                Loader loader = traversalHttpEntry.getLoader();
+                dbEngine = traversalHttpEntry.getDbEngine();
 
-            traversalHttpEntry.setHttpEntryProperties(version, type);
-            Loader loader = traversalHttpEntry.getLoader();
-            dbEngine = traversalHttpEntry.getDbEngine();
+                URI uriObject = UriBuilder.fromPath(uri).build();
+                URIToObject uriToObject = new URIToObject(loader, uriObject);
 
-            URI uriObject = UriBuilder.fromPath(uri).build();
-            URIToObject uriToObject = new URIToObject(loader, uriObject);
+                String objType = uriToObject.getEntityName();
+                queryParameters.add("resource-version", resourceVersion);
+                QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
 
-            String objType = uriToObject.getEntityName();
-            queryParameters.add("resource-version", resourceVersion);
-            QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
+                logger.info("Unmarshalling the payload to this {}", objType);
 
-            logger.info("Unmarshalling the payload to this {}", objType);
+                Introspector obj;
 
-            Introspector obj;
-            HttpMethod httpMethod;
-            if (uri.contains("/relationship-list/relationship")) {
-                obj = loader.introspectorFromName("relationship");
-                httpMethod = HttpMethod.DELETE_EDGE;
-            } else {
-                obj = loader.introspectorFromName(objType);
-                httpMethod = HttpMethod.DELETE;
-            }
+                HttpMethod httpMethod;
 
-            DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo,
-                    "JUNIT-TRANSACTION").build();
+                if (uri.contains("/relationship-list/relationship")) {
+                    httpMethod = HttpMethod.DELETE_EDGE;
+                    obj = loader.unmarshal("relationship", content, org.onap.aai.restcore.MediaType.getEnum("application/json"));
+                } else {
+                    obj = loader.introspectorFromName(objType);
+                    httpMethod = HttpMethod.DELETE;
+                }
 
-            List<DBRequest> dbRequestList = new ArrayList<>();
-            dbRequestList.add(dbRequest);
+                DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION").build();
 
+
+                dbRequestList.add(dbRequest);
+            }
             Pair<Boolean, List<Pair<URI, Response>>> responsesTuple =
-                    traversalHttpEntry.process(dbRequestList, "JUNIT");
+                traversalHttpEntry.process(dbRequestList, "JUNIT");
             response = responsesTuple.getValue1().get(0).getValue1();
 
         } catch (AAIException e) {
-            response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
+            response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.DELETE, e);
             success = false;
         } catch (Exception e) {
             AAIException ex = new AAIException("AAI_4000", e);
-            response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
+            response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.DELETE, ex);
             success = false;
         } finally {
             if (success) {
                 if (response != null) {
                     if ((response.getStatus() / 100) == 2) {
                         logger.info("Successfully completed the DELETE request with status {} and committing it to DB",
-                                response.getStatus());
+                            response.getStatus());
                     } else {
                         logFailure(HttpMethod.DELETE, response);
                     }
@@ -403,6 +559,15 @@ public class HttpTestUtil extends RESTAPI {
         return response;
     }
 
+    public Response doDelete(String uri, String resourceVersion) {
+        return this.doDelete(uri, resourceVersion, null);
+    }
+    public Response doDelete(String uri, String resourceVersion, String content) {
+        Map<String, Pair<String, String>> deletes = new HashMap<>();
+        deletes.put(uri, new Pair<>(resourceVersion, content));
+        return this.doDelete(deletes);
+    }
+
     public static void logFailure(HttpMethod httpMethod, Response response) {
         logger.info("Unable to complete the {} request with status {} and rolling back", httpMethod.toString(),
                 response.getStatus());