--- /dev/null
+package org.onap.schema;\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.AaiResourceService;\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