From 7e69be504213aa92893fd3354a767128b3a583f1 Mon Sep 17 00:00:00 2001 From: Shwetank Dave Date: Tue, 22 Aug 2017 10:36:30 -0400 Subject: Using DbEdgeRules.json files from aai-core jar. Before we were aai_relationship_vxx.json files. Now using the edge rules directly from aai-core jar. Issue-ID: AAI-21 Change-Id: Id62494caabc75bc29e4f3558268ec78897946937 Signed-off-by: Shwetank Dave --- .../schema/RelationshipSchemaLoaderTest.java | 91 ++++++++++++++++ .../openecomp/schema/RelationshipSchemaTest.java | 116 +++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 src/test/java/org/openecomp/schema/RelationshipSchemaLoaderTest.java create mode 100644 src/test/java/org/openecomp/schema/RelationshipSchemaTest.java (limited to 'src/test/java/org/openecomp/schema') diff --git a/src/test/java/org/openecomp/schema/RelationshipSchemaLoaderTest.java b/src/test/java/org/openecomp/schema/RelationshipSchemaLoaderTest.java new file mode 100644 index 0000000..85ef2fe --- /dev/null +++ b/src/test/java/org/openecomp/schema/RelationshipSchemaLoaderTest.java @@ -0,0 +1,91 @@ +package org.openecomp.schema; + +import static org.junit.Assert.*; +import edu.emory.mathcs.backport.java.util.Arrays; +import org.junit.Before; +import org.junit.Test; +import org.openecomp.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("has")); + } + + @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 jsonString = new ArrayList(); + 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 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/openecomp/schema/RelationshipSchemaTest.java b/src/test/java/org/openecomp/schema/RelationshipSchemaTest.java new file mode 100644 index 0000000..f575105 --- /dev/null +++ b/src/test/java/org/openecomp/schema/RelationshipSchemaTest.java @@ -0,0 +1,116 @@ +package org.openecomp.schema; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.openecomp.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 versionContextMap = new ConcurrentHashMap<>(); + loadRelations(versionContextMap); + assertEquals(6, versionContextMap.keySet().size()); + } + + @Test + public void shouldContainValidTypes() throws Exception { + Map versionContextMap = new ConcurrentHashMap<>(); + loadRelations(versionContextMap); + assertTrue(versionContextMap.get("v11").isValidType("groupsResourcesIn")); + assertTrue(versionContextMap.get("v11").isValidType("uses")); + assertFalse(versionContextMap.get("v11").isValidType("has")); + } + + @Test + public void shouldLookUpByRelation() throws Exception { + Map 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 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 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 files) { + List 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 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 -- cgit 1.2.3-korg