Add junit tests
[aai/gizmo.git] / src / test / java / org / onap / crud / service / AaiResourceServiceTest.java
diff --git a/src/test/java/org/onap/crud/service/AaiResourceServiceTest.java b/src/test/java/org/onap/crud/service/AaiResourceServiceTest.java
new file mode 100644 (file)
index 0000000..5eb8a68
--- /dev/null
@@ -0,0 +1,216 @@
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.aai\r
+ * ================================================================================\r
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright © 2017-2018 Amdocs\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *       http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+package org.onap.crud.service;\r
+\r
+import static org.junit.Assert.assertTrue;\r
+import static org.junit.Assert.fail;\r
+\r
+import java.util.Map;\r
+\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.onap.aai.exceptions.AAIException;\r
+import org.onap.aai.serialization.db.EdgeProperty;\r
+import org.onap.aai.serialization.db.EdgeRule;\r
+import org.onap.aai.serialization.db.EdgeRules;\r
+import org.onap.aai.serialization.db.EdgeType;\r
+import org.onap.crud.exception.CrudException;\r
+import org.onap.crud.service.EdgePayload;\r
+\r
+import com.google.gson.JsonElement;\r
+\r
+public class AaiResourceServiceTest {\r
+\r
+  public AaiResourceService aaiResSvc = null;\r
+  \r
+  \r
+  @Before\r
+  public void setup() {\r
+    System.setProperty("AJSC_HOME", ".");\r
+    System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");\r
+    \r
+    aaiResSvc = new AaiResourceService();\r
+  }\r
+  \r
+  \r
+  /**\r
+   * This test validates that we can apply db edge rules against an edge request\r
+   * payload and have the properties defined in the edge rules merged into the\r
+   * payload.\r
+   * \r
+   * @throws CrudException\r
+   * @throws AAIException\r
+   */\r
+  @Test\r
+  public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {\r
+    \r
+    String content = "{" +\r
+        "\"source\": \"services/inventory/v8/l-interface/369553424\", " +\r
+        "\"target\": \"services/inventory/v8/logical-link/573444128\"," +\r
+        "\"properties\": {" +\r
+        "}" +\r
+     "}";\r
+    \r
+    // Convert our simulated payload to an EdgePayload object.\r
+    EdgePayload payload = EdgePayload.fromJson(content);\r
+    \r
+    // Now, apply the db edge rules against our edge payload.\r
+    EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);\r
+    \r
+    EdgeRules rules = EdgeRules.getInstance();\r
+    EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");\r
+    Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();\r
+    \r
+    // Validate that the properties defined in the DB edge rules show up in our\r
+    // final payload.\r
+    for(EdgeProperty key : edgeProps.keySet()) {\r
+      assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));\r
+    }\r
+  }\r
+  \r
+  \r
+  /**\r
+   * This test validates that trying to apply edge rules where there is no\r
+   * db edge rules entry for the supplied source and target vertex types\r
+   * produces an exception.\r
+   * \r
+   * @throws CrudException\r
+   */\r
+  @Test\r
+  public void noRuleForEdgeTest() throws CrudException {\r
+        \r
+    String content = "{" +\r
+        "\"source\": \"services/inventory/v8/commodore-64/12345\", " +\r
+        "\"target\": \"services/inventory/v8/jumpman/67890\"," +\r
+        "\"properties\": {" +\r
+        "}" +\r
+     "}";\r
+    \r
+    // Convert our simulated payload to an EdgePayload object.\r
+    EdgePayload payload = EdgePayload.fromJson(content);\r
+    \r
+    // Now, apply the db edge rules against our edge payload.\r
+    try {\r
+      aaiResSvc.applyEdgeRulesToPayload(payload);\r
+      \r
+    } catch (CrudException e) {\r
+      \r
+      // We expected an exception since there is no rule for our made up vertices..\r
+      assertTrue(e.getMessage().contains("No edge rules for"));\r
+      return;\r
+    }\r
+    \r
+    // If we're here then something unexpected happened...\r
+    fail();\r
+  }\r
+  \r
+  \r
+  /**\r
+   * This test validates that it is possible to merge client supplied and edge rule\r
+   * supplied properties into one edge property list.\r
+   * \r
+   * @throws Exception\r
+   */\r
+  @Test\r
+  public void mergeEdgePropertiesTest() throws Exception {\r
+        \r
+    String content = "{" +\r
+        "\"source\": \"services/inventory/v8/l-interface/369553424\", " +\r
+        "\"target\": \"services/inventory/v8/logical-link/573444128\"," +\r
+        "\"properties\": {" +\r
+          "\"multiplicity\": \"many\"," +\r
+          "\"is-parent\": true," +\r
+          "\"uses-resource\": \"true\"," +\r
+          "\"has-del-target\": \"true\"" +\r
+        "}" +\r
+     "}";\r
+    \r
+    EdgePayload payload = EdgePayload.fromJson(content);\r
+    EdgeRules rules = EdgeRules.getInstance();\r
+    EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");\r
+    Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();\r
+\r
+    // Merge the client supplied properties with the properties defined in the DB edge rules.\r
+    JsonElement mergedProperties = \r
+        aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());\r
+    \r
+    // Now, validate that the resulting set of properties contains both the client and edge\r
+    // rule supplied properties.\r
+    String mergedPropertiesString = mergedProperties.toString();\r
+    assertTrue("Client supplied property 'multiplicity' is missing from merged properties set",\r
+               mergedPropertiesString.contains("multiplicity"));\r
+    assertTrue("Client supplied property 'is-parent' is missing from merged properties set",\r
+               mergedPropertiesString.contains("is-parent"));\r
+    assertTrue("Client supplied property 'uses-resource' is missing from merged properties set",\r
+               mergedPropertiesString.contains("uses-resource"));\r
+    assertTrue("Client supplied property 'has-del-target' is missing from merged properties set",\r
+               mergedPropertiesString.contains("has-del-target"));\r
+    \r
+    for(EdgeProperty key : edgeProps.keySet()) {\r
+      assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set",\r
+                 mergedPropertiesString.contains(key.toString()));\r
+    }\r
+  }\r
+  \r
+  /**\r
+   * This test validates that if we try to merge client supplied edge properties\r
+   * with the properties defined in the db edge rules, and there is a conflict,\r
+   * then the merge will fail.\r
+   * \r
+   * @throws Exception\r
+   */\r
+  @Test\r
+  public void mergeEdgePropertiesConflictTest() throws Exception {\r
+        \r
+    String content = "{" +\r
+        "\"source\": \"services/inventory/v8/l-interface/369553424\", " +\r
+        "\"target\": \"services/inventory/v8/logical-link/573444128\"," +\r
+        "\"properties\": {" +\r
+          "\"contains-other-v\": \"OUT\"" +\r
+        "}" +\r
+     "}";\r
+    \r
+    EdgePayload payload = EdgePayload.fromJson(content);\r
+    EdgeRules rules = EdgeRules.getInstance();\r
+    EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");\r
+\r
+    try {\r
+      \r
+      // Try to merge our client supplied properties with the properties defined\r
+      // in the db edge rules.\r
+      aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());\r
+    \r
+    } catch (CrudException e) {\r
+      \r
+      // We should have gotten an exception because we are trying to set a parameter which is\r
+      // already defined in the db edge rules, so if we're here then we are good.\r
+      return;\r
+    }\r
+\r
+    // If we made it here then we were allowed to set a property that is already defined\r
+    // in the db edge rules, which we should not have...\r
+    fail();\r
+  }\r
+  \r
+\r
+\r
+\r
+}\r