Refactor HttpEntry class - Part 1 87/138087/2
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Tue, 28 May 2024 09:11:09 +0000 (11:11 +0200)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Tue, 28 May 2024 09:55:34 +0000 (11:55 +0200)
- simplify setHttpEntryProperties (later on this will likely be done in the constructor)
- move some logic of UEBNotification into separate methods
- resolve jsonassert dependency conflict

Issue-ID: AAI-3863
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Change-Id: Ic84b43156890352509dc9c547478abdf53e96d6a

aai-core/pom.xml
aai-core/src/main/java/org/onap/aai/rest/db/HttpEntry.java
aai-core/src/main/java/org/onap/aai/rest/ueb/UEBNotification.java
aai-core/src/test/java/org/onap/aai/rest/db/HttpEntryTest.java
aai-els-onap-logging/pom.xml
aai-rest/pom.xml

index b9814e8..955f62b 100644 (file)
@@ -365,6 +365,12 @@ limitations under the License.
                        <groupId>org.skyscreamer</groupId>
                        <artifactId>jsonassert</artifactId>
                        <scope>test</scope>
+                       <exclusions>
+                               <exclusion>
+                                       <groupId>com.vaadin.external.google</groupId>
+                                       <artifactId>android-json</artifactId>
+                               </exclusion>
+                       </exclusions>
                </dependency>
                <dependency>
                        <groupId>org.apache.httpcomponents</groupId>
index 28d66dd..7ecdd6d 100644 (file)
@@ -155,49 +155,22 @@ public class HttpEntry {
     }
 
     public HttpEntry setHttpEntryProperties(SchemaVersion version, String serverBase) {
-        this.version = version;
-        this.loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
-        this.dbEngine = new JanusGraphDBEngine(queryStyle, loader);
-
-        getDbEngine().startTransaction();
-        this.notification = new UEBNotification(loader, loaderFactory, schemaVersions);
-        if ("true".equals(AAIConfig.get("aai.notification.depth.all.enabled", "true"))) {
-            this.notificationDepth = AAIProperties.MAXIMUM_DEPTH;
-        } else {
-            this.notificationDepth = AAIProperties.MINIMUM_DEPTH;
-        }
-
+        setHttpEntryProperties(version);
         this.serverBase = serverBase;
         return this;
     }
 
     public HttpEntry setHttpEntryProperties(SchemaVersion version, UEBNotification notification) {
-        this.version = version;
-        this.loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
-        this.dbEngine = new JanusGraphDBEngine(queryStyle, loader);
-
+        setHttpEntryProperties(version);
         this.notification = notification;
-
-        if ("true".equals(AAIConfig.get("aai.notification.depth.all.enabled", "true"))) {
-            this.notificationDepth = AAIProperties.MAXIMUM_DEPTH;
-        } else {
-            this.notificationDepth = AAIProperties.MINIMUM_DEPTH;
-        }
-        // start transaction on creation
-        getDbEngine().startTransaction();
         return this;
     }
 
     public HttpEntry setHttpEntryProperties(SchemaVersion version, UEBNotification notification,
             int notificationDepth) {
-        this.version = version;
-        this.loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
-        this.dbEngine = new JanusGraphDBEngine(queryStyle, loader);
-
+        setHttpEntryProperties(version);
         this.notification = notification;
         this.notificationDepth = notificationDepth;
-        // start transaction on creation
-        getDbEngine().startTransaction();
         return this;
     }
 
index be30c46..0c8fde6 100644 (file)
@@ -98,42 +98,15 @@ public class UEBNotification {
             Introspector obj, HashMap<String, Introspector> relatedObjects, String basePath)
             throws AAIException, UnsupportedEncodingException {
 
-        String action = "UPDATE";
-
-        if (status.equals(Status.CREATED)) {
-            action = "CREATE";
-        } else if (status.equals(Status.OK)) {
-            action = "UPDATE";
-        } else if (status.equals(Status.NO_CONTENT)) {
-            action = "DELETE";
-        }
+        String action = getAction(status);
 
         try {
             Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header");
             URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects);
 
-            if ((basePath != null) && (!basePath.isEmpty())) {
-                if (!(basePath.startsWith("/"))) {
-                    basePath = "/" + basePath;
-                }
-                if (!(basePath.endsWith("/"))) {
-                    basePath = basePath + "/";
-                }
-            } else {
-                // default
-                basePath = "/aai/";
-                if (LOGGER.isDebugEnabled()) {
-                    LOGGER.debug("Please check the schema.uri.base.path as it didn't seem to be set");
-                }
-            }
+            basePath = formatBasePath(basePath);
 
-            String uriStr = getUri(uri.toString(), basePath);
-            String entityLink;
-            if (uriStr.startsWith("/")) {
-                entityLink = basePath + notificationVersion + uriStr;
-            } else {
-                entityLink = basePath + notificationVersion + "/" + uriStr;
-            }
+            String entityLink = formatEntityLink(uri, basePath);
 
             eventHeader.setValue("entity-link", entityLink);
             eventHeader.setValue("action", action);
@@ -191,6 +164,48 @@ public class UEBNotification {
         }
     }
 
+    private String formatEntityLink(URI uri, String basePath) {
+        String uriStr = getUri(uri.toString(), basePath);
+        String entityLink;
+        if (uriStr.startsWith("/")) {
+            entityLink = basePath + notificationVersion + uriStr;
+        } else {
+            entityLink = basePath + notificationVersion + "/" + uriStr;
+        }
+        return entityLink;
+    }
+
+    private String formatBasePath(String basePath) {
+        if ((basePath != null) && (!basePath.isEmpty())) {
+            if (!(basePath.startsWith("/"))) {
+                basePath = "/" + basePath;
+            }
+            if (!(basePath.endsWith("/"))) {
+                basePath = basePath + "/";
+            }
+        } else {
+            // default
+            basePath = "/aai/";
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("Please check the schema.uri.base.path as it didn't seem to be set");
+            }
+        }
+        return basePath;
+    }
+
+    private String getAction(Status status) {
+        String action = "UPDATE";
+
+        if (status.equals(Status.CREATED)) {
+            action = "CREATE";
+        } else if (status.equals(Status.OK)) {
+            action = "UPDATE";
+        } else if (status.equals(Status.NO_CONTENT)) {
+            action = "DELETE";
+        }
+        return action;
+    }
+
     /**
      * Trigger events.
      *
index 371c07a..8d703d3 100644 (file)
@@ -196,7 +196,13 @@ public class HttpEntryTest extends AAISetup {
                 .put("equip-type", "theEquipType")
                 .toString();
 
+        JSONObject expectedResponseBody = new JSONObject()
+                .put("hostname", "theHostname")
+                .put("equip-type", "theEquipType");
         Response response = doRequest(traversalHttpEntry, loader, dbEngine, HttpMethod.GET, uri, requestBody);
+        JSONObject actualResponseBody = new JSONObject(response.getEntity().toString());
+
+        JSONAssert.assertEquals(expectedResponseBody, actualResponseBody, JSONCompareMode.NON_EXTENSIBLE);
         assertEquals("Expected the pserver to be returned", 200, response.getStatus());
     }
 
index aa0782b..2180f6e 100644 (file)
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
+            <exclusions>
+                               <exclusion>
+                                       <groupId>com.vaadin.external.google</groupId>
+                                       <artifactId>android-json</artifactId>
+                               </exclusion>
+                       </exclusions>
         </dependency>
         <dependency>
             <groupId>junit</groupId>
index 9e8697e..0ff4dad 100644 (file)
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
+            <exclusions>
+                               <exclusion>
+                                       <groupId>com.vaadin.external.google</groupId>
+                                       <artifactId>android-json</artifactId>
+                               </exclusion>
+                       </exclusions>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>