aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/schema
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/onap/schema')
-rw-r--r--src/test/java/org/onap/schema/AaiResourceServiceTest.java197
-rw-r--r--src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java91
-rw-r--r--src/test/java/org/onap/schema/RelationshipSchemaTest.java116
3 files changed, 404 insertions, 0 deletions
diff --git a/src/test/java/org/onap/schema/AaiResourceServiceTest.java b/src/test/java/org/onap/schema/AaiResourceServiceTest.java
new file mode 100644
index 0000000..766fcff
--- /dev/null
+++ b/src/test/java/org/onap/schema/AaiResourceServiceTest.java
@@ -0,0 +1,197 @@
+package org.onap.schema;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.Map;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.aai.exceptions.AAIException;
+import org.onap.aai.serialization.db.EdgeProperty;
+import org.onap.aai.serialization.db.EdgeRule;
+import org.onap.aai.serialization.db.EdgeRules;
+import org.onap.aai.serialization.db.EdgeType;
+import org.onap.crud.exception.CrudException;
+import org.onap.crud.service.AaiResourceService;
+import org.onap.crud.service.EdgePayload;
+
+import com.google.gson.JsonElement;
+
+public class AaiResourceServiceTest {
+
+ public AaiResourceService aaiResSvc = null;
+
+
+ @Before
+ public void setup() {
+ System.setProperty("AJSC_HOME", ".");
+ System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
+
+ aaiResSvc = new AaiResourceService();
+ }
+
+
+ /**
+ * This test validates that we can apply db edge rules against an edge request
+ * payload and have the properties defined in the edge rules merged into the
+ * payload.
+ *
+ * @throws CrudException
+ * @throws AAIException
+ */
+ @Test
+ public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {
+
+ String content = "{" +
+ "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
+ "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
+ "\"properties\": {" +
+ "}" +
+ "}";
+
+ // Convert our simulated payload to an EdgePayload object.
+ EdgePayload payload = EdgePayload.fromJson(content);
+
+ // Now, apply the db edge rules against our edge payload.
+ EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);
+
+ EdgeRules rules = EdgeRules.getInstance();
+ EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
+ Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
+
+ // Validate that the properties defined in the DB edge rules show up in our
+ // final payload.
+ for(EdgeProperty key : edgeProps.keySet()) {
+ assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));
+ }
+ }
+
+
+ /**
+ * This test validates that trying to apply edge rules where there is no
+ * db edge rules entry for the supplied source and target vertex types
+ * produces an exception.
+ *
+ * @throws CrudException
+ */
+ @Test
+ public void noRuleForEdgeTest() throws CrudException {
+
+ String content = "{" +
+ "\"source\": \"services/inventory/v8/commodore-64/12345\", " +
+ "\"target\": \"services/inventory/v8/jumpman/67890\"," +
+ "\"properties\": {" +
+ "}" +
+ "}";
+
+ // Convert our simulated payload to an EdgePayload object.
+ EdgePayload payload = EdgePayload.fromJson(content);
+
+ // Now, apply the db edge rules against our edge payload.
+ try {
+ aaiResSvc.applyEdgeRulesToPayload(payload);
+
+ } catch (CrudException e) {
+
+ // We expected an exception since there is no rule for our made up vertices..
+ assertTrue(e.getMessage().contains("No edge rules for"));
+ return;
+ }
+
+ // If we're here then something unexpected happened...
+ fail();
+ }
+
+
+ /**
+ * This test validates that it is possible to merge client supplied and edge rule
+ * supplied properties into one edge property list.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void mergeEdgePropertiesTest() throws Exception {
+
+ String content = "{" +
+ "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
+ "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
+ "\"properties\": {" +
+ "\"multiplicity\": \"many\"," +
+ "\"is-parent\": true," +
+ "\"uses-resource\": \"true\"," +
+ "\"has-del-target\": \"true\"" +
+ "}" +
+ "}";
+
+ EdgePayload payload = EdgePayload.fromJson(content);
+ EdgeRules rules = EdgeRules.getInstance();
+ EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
+ Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
+
+ // Merge the client supplied properties with the properties defined in the DB edge rules.
+ JsonElement mergedProperties =
+ aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
+
+ // Now, validate that the resulting set of properties contains both the client and edge
+ // rule supplied properties.
+ String mergedPropertiesString = mergedProperties.toString();
+ assertTrue("Client supplied property 'multiplicity' is missing from merged properties set",
+ mergedPropertiesString.contains("multiplicity"));
+ assertTrue("Client supplied property 'is-parent' is missing from merged properties set",
+ mergedPropertiesString.contains("is-parent"));
+ assertTrue("Client supplied property 'uses-resource' is missing from merged properties set",
+ mergedPropertiesString.contains("uses-resource"));
+ assertTrue("Client supplied property 'has-del-target' is missing from merged properties set",
+ mergedPropertiesString.contains("has-del-target"));
+
+ for(EdgeProperty key : edgeProps.keySet()) {
+ assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set",
+ mergedPropertiesString.contains(key.toString()));
+ }
+ }
+
+ /**
+ * This test validates that if we try to merge client supplied edge properties
+ * with the properties defined in the db edge rules, and there is a conflict,
+ * then the merge will fail.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void mergeEdgePropertiesConflictTest() throws Exception {
+
+ String content = "{" +
+ "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
+ "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
+ "\"properties\": {" +
+ "\"contains-other-v\": \"OUT\"" +
+ "}" +
+ "}";
+
+ EdgePayload payload = EdgePayload.fromJson(content);
+ EdgeRules rules = EdgeRules.getInstance();
+ EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
+
+ try {
+
+ // Try to merge our client supplied properties with the properties defined
+ // in the db edge rules.
+ aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
+
+ } catch (CrudException e) {
+
+ // We should have gotten an exception because we are trying to set a parameter which is
+ // already defined in the db edge rules, so if we're here then we are good.
+ return;
+ }
+
+ // If we made it here then we were allowed to set a property that is already defined
+ // in the db edge rules, which we should not have...
+ fail();
+ }
+
+
+
+
+}
diff --git a/src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java b/src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java
new file mode 100644
index 0000000..0c62162
--- /dev/null
+++ b/src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java
@@ -0,0 +1,91 @@
+package org.onap.schema;
+
+import static org.junit.Assert.*;
+import edu.emory.mathcs.backport.java.util.Arrays;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.crud.exception.CrudException;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+public class RelationshipSchemaLoaderTest {
+
+ @Before
+ public void init() {
+ ClassLoader classLoader = getClass().getClassLoader();
+ File dir = new File(classLoader.getResource( "model").getFile());
+ System.setProperty("CONFIG_HOME", dir.getParent());
+ RelationshipSchemaLoader.resetVersionContextMap();
+ }
+
+ @Test
+ public void loadModels() throws Exception {
+ RelationshipSchemaLoader.loadModels();
+ assertFalse( RelationshipSchemaLoader.getVersionContextMap().keySet().isEmpty());
+ }
+
+ @Test
+ public void loadModelsWithAVersion() throws Exception {
+ RelationshipSchemaLoader.loadModels("v11");
+ assertEquals(1, RelationshipSchemaLoader.getVersionContextMap().keySet().size());
+ assertEquals("v11", RelationshipSchemaLoader.getLatestSchemaVersion());
+ }
+
+ @Test
+ public void getSchemaForVersion() throws Exception {
+ RelationshipSchemaLoader.loadModels("v11");
+ String version = RelationshipSchemaLoader.getLatestSchemaVersion();
+ RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version);
+ assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.BelongsTo"));
+ }
+
+ @Test
+ public void getSchemaForVersionFail() throws Exception {
+ RelationshipSchemaLoader.loadModels();
+ try {
+ RelationshipSchemaLoader.getSchemaForVersion("v1");
+ } catch (CrudException e) {
+ assertEquals(404, e.getHttpStatus().getStatusCode());
+ }
+ }
+
+ @Test
+ public void setVersionContextMap() throws Exception {
+ ArrayList<String> jsonString = new ArrayList<String>();
+ String rules = "{" +
+ "\"rules\": [" +
+ "{" +
+ "\"from\": \"availability-zone\"," +
+ "\"to\": \"complex\"," +
+ "\"label\": \"groupsResourcesIn\"," +
+ "\"direction\": \"OUT\"," +
+ "\"multiplicity\": \"Many2Many\"," +
+ "\"contains-other-v\": \"NONE\"," +
+ "\"delete-other-v\": \"NONE\"," +
+ "\"SVC-INFRA\": \"NONE\"," +
+ "\"prevent-delete\": \"!${direction}\"" +
+ "}]}";
+ String props = "{" +
+ " \"isParent\":\"java.lang.Boolean\"," +
+ " \"isParent-REV\":\"java.lang.Boolean\"," +
+ " \"usesResource\":\"java.lang.Boolean\"," +
+ " \"usesResource-REV\":\"java.lang.Boolean\"," +
+ " \"SVC-INFRA\":\"java.lang.Boolean\"," +
+ " \"SVC-INFRA-REV\":\"java.lang.Boolean\"," +
+ " \"hasDelTarget\":\"java.lang.Boolean\"," +
+ " \"hasDelTarget-REV\":\"java.lang.Boolean\"" +
+ "}";
+ jsonString.add(rules);
+ jsonString.add(props);
+ RelationshipSchema nRs = new RelationshipSchema(jsonString);
+ Map<String, RelationshipSchema> versionMap = new HashMap<>();
+ versionMap.put("v1", nRs);
+ RelationshipSchemaLoader.setVersionContextMap(versionMap);
+ assertNotNull(RelationshipSchemaLoader.getSchemaForVersion("v1").lookupRelationType("groupsResourcesIn"));
+ }
+} \ No newline at end of file
diff --git a/src/test/java/org/onap/schema/RelationshipSchemaTest.java b/src/test/java/org/onap/schema/RelationshipSchemaTest.java
new file mode 100644
index 0000000..95b3dde
--- /dev/null
+++ b/src/test/java/org/onap/schema/RelationshipSchemaTest.java
@@ -0,0 +1,116 @@
+package org.onap.schema;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.onap.crud.exception.CrudException;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.Comparator;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.*;
+
+public class RelationshipSchemaTest {
+
+ final static Pattern rulesFilePattern = Pattern.compile("DbEdgeRules(.*).json");
+ final static Pattern propsFilePattern = Pattern.compile("edge_properties_(.*).json");
+ final static Pattern versionPattern = Pattern.compile(".*(v\\d+).json");
+
+
+ @Test
+ public void shouldLoadAllTheVersionsInDirectory() throws Exception {
+ Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
+ loadRelations(versionContextMap);
+ assertTrue(versionContextMap.keySet().size() >= 0);
+ }
+
+ @Test
+ public void shouldContainValidTypes() throws Exception {
+ Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
+ loadRelations(versionContextMap);
+ assertTrue(versionContextMap.get("v11").isValidType("groupsResourcesIn"));
+ assertTrue(versionContextMap.get("v11").isValidType("uses"));
+ assertFalse(versionContextMap.get("v11").isValidType("notValidType"));
+ }
+
+ @Test
+ public void shouldLookUpByRelation() throws Exception {
+ Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
+ loadRelations(versionContextMap);
+ assertNotNull(versionContextMap.get("v11").lookupRelation("availability-zone:complex:groupsResourcesIn"));
+ assertTrue(versionContextMap.get("v11")
+ .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("usesResource"));
+ }
+
+ @Test
+ public void shouldLookUpByRelationType() throws Exception {
+ Map<String, RelationshipSchema> versionContextMap = new ConcurrentHashMap<>();
+ loadRelations(versionContextMap);
+ assertNotNull(versionContextMap.get("v11").lookupRelationType("groupsResourcesIn"));
+ assertTrue(versionContextMap.get("v11")
+ .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("usesResource"));
+ }
+
+ private void loadRelations(Map<String, RelationshipSchema> map){
+ ClassLoader classLoader = getClass().getClassLoader();
+ File dir = new File(classLoader.getResource("model").getFile());
+ File[] allFiles = dir.listFiles((d, name) ->
+ (propsFilePattern.matcher(name).matches() || rulesFilePattern.matcher(name).matches()));
+
+ Arrays.stream(allFiles).sorted(Comparator.comparing(File::getName))
+ .collect(Collectors.groupingBy(f -> myMatcher(versionPattern, f.getName())))
+ .forEach((e, f) -> map.put(e, jsonFilesLoader(f)));
+
+ }
+
+
+ private RelationshipSchema jsonFilesLoader (List<File> files) {
+ List<String> fileContents = new ArrayList<>();
+ RelationshipSchema rsSchema = null;
+ for (File f : files) {
+ fileContents.add(jsonToString(f));
+ }
+
+ try {
+ rsSchema = new RelationshipSchema(fileContents);
+ } catch (CrudException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return rsSchema;
+ }
+
+ private String jsonToString (File file) {
+ InputStream inputStream = null;
+ String content = null;
+ HashMap<String,Object> result = null;
+
+ try {
+ inputStream = new FileInputStream(file);
+ content = IOUtils.toString(inputStream, "UTF-8");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return content;
+ }
+
+ private String myMatcher (Pattern p, String s) {
+ Matcher m = p.matcher(s);
+ return m.matches() ? m.group(1) : "";
+ }
+} \ No newline at end of file