From 00832f054dd0c21492af531548e321ea25cdb8b4 Mon Sep 17 00:00:00 2001 From: Shwetank Dave Date: Wed, 13 Jun 2018 08:40:26 -0400 Subject: Gizmo to load multiple Db Edge Rules files There is an Edge Rule file for each OXM file. For every OXM version that is loaded, an edge rule file should also be loaded. Issue-ID: AAI-1201 Change-Id: I728e577ea8152623d3c5e2f0e8edf677f0eb9ef4 Signed-off-by: Shwetank Dave --- .../org/onap/crud/service/CrudRestServiceTest.java | 6 +- .../java/org/onap/schema/EdgeRulesLoaderTest.java | 69 ++++++++++ .../onap/schema/RelationshipSchemaLoaderTest.java | 124 ----------------- .../org/onap/schema/RelationshipSchemaTest.java | 148 ++++++++------------- 4 files changed, 124 insertions(+), 223 deletions(-) create mode 100644 src/test/java/org/onap/schema/EdgeRulesLoaderTest.java delete mode 100644 src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java (limited to 'src/test/java') diff --git a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java index a4b4305..13cba11 100644 --- a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java +++ b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java @@ -42,8 +42,7 @@ import org.onap.crud.exception.CrudException; import org.onap.crud.service.util.TestHeaders; import org.onap.crud.service.util.TestRequest; import org.onap.crud.service.util.TestUriInfo; -import org.onap.schema.RelationshipSchemaLoader; - +import org.onap.schema.EdgeRulesLoader; public class CrudRestServiceTest { @@ -85,8 +84,7 @@ public class CrudRestServiceTest { Path parentPath = resourcePath.getParent(); System.setProperty("CONFIG_HOME", parentPath.toString()); - RelationshipSchemaLoader.resetVersionContextMap(); - + EdgeRulesLoader.resetSchemaVersionContext (); CrudGraphDataService service = new CrudGraphDataService(new TestDao()); CrudRestService restService = new CrudRestService(service, null); mockService = Mockito.spy(restService); diff --git a/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java b/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java new file mode 100644 index 0000000..073e1d3 --- /dev/null +++ b/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java @@ -0,0 +1,69 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.schema; + +import org.junit.Test; +import org.onap.crud.exception.CrudException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +public class EdgeRulesLoaderTest { + + @Test + public void loadModels() throws Exception { + EdgeRulesLoader.loadModels(); + assertTrue(EdgeRulesLoader.getSchemaForVersion ( "v11" ).isValidType ( "org.onap.relationships.inventory.groupsResourcesIn" )); + } + + @Test + public void loadModelsWithAVersion() throws Exception { + EdgeRulesLoader.resetSchemaVersionContext (); + EdgeRulesLoader.loadModels("V11"); + assertEquals(1, EdgeRulesLoader.getSchemas ().size ()); + assertEquals("v11", EdgeRulesLoader.getLatestSchemaVersion ()); + } + + @Test + public void getSchemaForVersion() throws Exception { + EdgeRulesLoader.resetSchemaVersionContext (); + EdgeRulesLoader.loadModels("v11"); + String version = EdgeRulesLoader.getLatestSchemaVersion(); + RelationshipSchema g = EdgeRulesLoader.getSchemaForVersion(version); + assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.groupsResourcesIn")); + assertNotNull(g.lookupRelation("U:V:org.onap.relationships.inventory.groupsResourcesIn")); + assertNull(g.lookupRelation("U:W:org.onap.relationships.inventory.groupsResourcesIn")); + } + + + @Test + public void getSchemaForVersionFail() throws Exception { + EdgeRulesLoader.loadModels(); + try { + EdgeRulesLoader.getSchemaForVersion("v1"); + } catch (CrudException e) { + assertEquals(404, e.getHttpStatus().getStatusCode()); + } + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java b/src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java deleted file mode 100644 index feeb1c4..0000000 --- a/src/test/java/org/onap/schema/RelationshipSchemaLoaderTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.schema; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import org.junit.Before; -import org.junit.Test; -import org.onap.crud.exception.CrudException; - -public class RelationshipSchemaLoaderTest { - - @Before - public void init() throws Exception { - Path resourcePath = Paths.get(ClassLoader.getSystemResource("model").toURI()); - Path parentPath = resourcePath.getParent(); - System.setProperty("CONFIG_HOME", parentPath.toString()); - RelationshipSchemaLoader.resetVersionContextMap(); - } - - @Test - public void loadModels() throws Exception { - RelationshipSchemaLoader.resetVersionContextMap(); - RelationshipSchemaLoader.loadModels(); - assertFalse( RelationshipSchemaLoader.getVersionContextMap().keySet().isEmpty()); - } - - @Test - public void loadModelsWithAVersion() throws Exception { - RelationshipSchemaLoader.resetVersionContextMap(); - RelationshipSchemaLoader.loadModels("v11"); - assertEquals(1, RelationshipSchemaLoader.getVersionContextMap().keySet().size()); - assertEquals("v11", RelationshipSchemaLoader.getLatestSchemaVersion()); - } - - @Test - public void getSchemaForVersion() throws Exception { - RelationshipSchemaLoader.resetVersionContextMap(); - RelationshipSchemaLoader.loadModels("v11"); - String version = RelationshipSchemaLoader.getLatestSchemaVersion(); - RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version); - assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.BelongsTo")); - } - - public void getSchemaForVersionManualFile() throws Exception { - RelationshipSchemaLoader.resetVersionContextMap(); - RelationshipSchemaLoader.loadModels("v10"); - String version = RelationshipSchemaLoader.getLatestSchemaVersion(); - RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version); - assertNotNull(g.lookupRelationType("locatedIn")); - } - - - @Test - public void getSchemaForVersionFail() throws Exception { - RelationshipSchemaLoader.resetVersionContextMap(); - RelationshipSchemaLoader.loadModels(); - try { - RelationshipSchemaLoader.getSchemaForVersion("v1"); - } catch (CrudException e) { - assertEquals(404, e.getHttpStatus().getStatusCode()); - } - } - - @Test - public void setVersionContextMap() throws Exception { - RelationshipSchemaLoader.resetVersionContextMap(); - 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")); - } -} diff --git a/src/test/java/org/onap/schema/RelationshipSchemaTest.java b/src/test/java/org/onap/schema/RelationshipSchemaTest.java index ce8559a..9467f41 100644 --- a/src/test/java/org/onap/schema/RelationshipSchemaTest.java +++ b/src/test/java/org/onap/schema/RelationshipSchemaTest.java @@ -20,128 +20,86 @@ */ package org.onap.schema; -import org.apache.commons.io.IOUtils; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import org.junit.Test; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.crud.exception.CrudException; -import com.att.aft.dme2.internal.apache.commons.lang.ArrayUtils; - -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); - assertTrue(versionContextMap.keySet().size() >= 0); + RelationshipSchema rs = loadRelations(); + assertTrue(!rs.lookupRelationType ("org.onap.some-relation" ).isEmpty ()); } @Test public void shouldContainValidTypes() throws Exception { - Map versionContextMap = new ConcurrentHashMap<>(); - loadRelations(versionContextMap); - assertTrue(versionContextMap.get("v10").isValidType("groupsResourcesIn")); - assertTrue(versionContextMap.get("v10").isValidType("uses")); - assertFalse(versionContextMap.get("v10").isValidType("notValidType")); + RelationshipSchema rs = loadRelations(); + assertTrue(rs.lookupRelationType ("org.onap.some-relation") != null); + assertTrue(rs.lookupRelationType("notValidType") == null); } @Test public void shouldLookUpByRelation() throws Exception { - Map versionContextMap = new ConcurrentHashMap<>(); - loadRelations(versionContextMap); - assertNotNull(versionContextMap.get("v10").lookupRelation("availability-zone:complex:groupsResourcesIn")); - assertTrue(versionContextMap.get("v10") - .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("prevent-delete")); + RelationshipSchema rs = loadRelations(); + assertNotNull(rs.lookupRelation("service-instance:customer:org.onap.some-relation")); } @Test public void shouldLookUpByRelationType() throws Exception { - Map versionContextMap = new ConcurrentHashMap<>(); - loadRelations(versionContextMap); - assertNotNull(versionContextMap.get("v10").lookupRelationType("groupsResourcesIn")); - assertTrue(versionContextMap.get("v10") - .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("prevent-delete")); - } - - 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())); - - // Special handling for the v12 file, as it is used for a special test - for (File f : allFiles) { - if (f.getName().equals("edge_properties_v11.json")) { - allFiles = (File[]) ArrayUtils.removeElement(allFiles, f); - } - } - - 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 { - if (fileContents.size() == 2) { - rsSchema = new RelationshipSchema(fileContents); - } - } catch (CrudException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - return rsSchema; + RelationshipSchema rs = loadRelations(); + assertNotNull(rs.lookupRelationType("org.onap.groupsResourcesIn")); + assertTrue(rs.lookupRelation("availability-zone:complex:org.onap.groupsResourcesIn").containsKey("prevent-delete")); } - 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 RelationshipSchema loadRelations() throws CrudException, EdgeRuleNotFoundException, IOException { + String defaultEdgeProps = "{" + + "\"contains-other-v\": \"java.lang.String\"," + + "\"delete-other-v\": \"java.lang.String\"," + + "\"SVC-INFRA\": \"java.lang.String\"," + + "\"prevent-delete\": \"java.lang.String\"" + + "}"; + + Map ruleOne = new HashMap<> ( ); + Map ruleTwo = new HashMap<> ( ); + + ruleOne.put("label", "org.onap.some-relation"); + ruleOne.put("direction", "OUT"); + ruleOne.put("contains-other-v", "NONE"); + ruleOne.put("delete-other-v", "NONE"); + ruleOne.put("prevent-delete", "NONE"); + ruleOne.put("from", "service-instance"); + ruleOne.put("to", "customer"); + ruleOne.put("multiplicity", "MANY2MANY"); + ruleOne.put("default", "true"); + ruleOne.put("description", ""); + + ruleTwo.put("label", "org.onap.groupsResourcesIn"); + ruleTwo.put("direction", "OUT"); + ruleTwo.put("contains-other-v", "NONE"); + ruleTwo.put("delete-other-v", "NONE"); + ruleTwo.put("prevent-delete", "NONE"); + ruleTwo.put("from", "availability-zone"); + ruleTwo.put("to", "complex"); + ruleTwo.put("multiplicity", "MANY2MANY"); + ruleTwo.put("default", "true"); + ruleTwo.put("description", ""); + + EdgeRule erOne = new EdgeRule ( ruleOne ); + EdgeRule erTwo = new EdgeRule ( ruleTwo ); + Multimap relationship = ArrayListMultimap.create(); + relationship.put ( "customer|service-instane", erOne ); + relationship.put ( "availability-zone|complex", erTwo ); + return new RelationshipSchema ( relationship, defaultEdgeProps ); - 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