From 9d661ab0572c486ad0534fd21d502de0782bd4ea Mon Sep 17 00:00:00 2001 From: "Kajur, Harish (vk250x)" Date: Thu, 1 Nov 2018 15:56:56 -0400 Subject: Add required properties to schema Issue-ID: AAI-1826 Change-Id: Ifc7122880e2c98888aa6b478cba3f70fb4c62e52 Signed-off-by: Kajur, Harish (vk250x) --- .../onap/aai/serialization/db/EdgeRulesTest.java | 156 +++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 aai-core/src/test/java/org/onap/aai/serialization/db/EdgeRulesTest.java (limited to 'aai-core/src/test') diff --git a/aai-core/src/test/java/org/onap/aai/serialization/db/EdgeRulesTest.java b/aai-core/src/test/java/org/onap/aai/serialization/db/EdgeRulesTest.java new file mode 100644 index 00000000..0bb07895 --- /dev/null +++ b/aai-core/src/test/java/org/onap/aai/serialization/db/EdgeRulesTest.java @@ -0,0 +1,156 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.serialization.db; + + +import com.google.common.collect.Multimap; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.aai.AAISetup; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.setup.SchemaVersion; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.*; +import java.util.stream.Collectors; + +import static junit.framework.TestCase.fail; +import static org.junit.Assert.assertEquals; + +public class EdgeRulesTest extends AAISetup { + + //set thrown.expect to whatever a specific test needs + //this establishes a default of expecting no exceptions to be thrown + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Autowired + private EdgeIngestor edgeIngestor; + + @Test + public void verifyOutDirection() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException { + + EdgeRuleQuery ruleQuery = new EdgeRuleQuery + .Builder("cloud-region", "flavor") + .edgeType(EdgeType.TREE) + .build(); + + EdgeRule rule = edgeIngestor.getRule(ruleQuery); + + assertEquals("out direction", rule.getDirection(), Direction.IN); + } + + @Test + public void verifyOutFlippedDirection() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException { + + EdgeRuleQuery ruleQuery = new EdgeRuleQuery + .Builder("flavor", "cloud-region") + .edgeType(EdgeType.TREE) + .build(); + + EdgeRule rule = edgeIngestor.getRule(ruleQuery); + + assertEquals("in direction", rule.getDirection(), Direction.OUT); + } + + @Test + public void verifyMultipleGet() throws EdgeRuleNotFoundException { + + EdgeRuleQuery ruleQuery = new EdgeRuleQuery + .Builder("model-element", "model-ver") + .edgeType(EdgeType.TREE) + .build(); + + Multimap ruleMap = edgeIngestor.getRules(ruleQuery); + + for (EdgeRule edgeRule : ruleMap.get("model|model-ver")) { + assertEquals("has isA rule", "org.onap.relationships.inventory.IsA", + edgeRule.getLabel()); + } + + } + + @Test + public void verifyAllRules() throws EdgeRuleNotFoundException { + + // This will cause every rule in the real json files to be verified + // so if any required properties are missing, the verification builds + // will catch it and incorrect rules can't get merged in. + for (SchemaVersion v : schemaVersions.getVersions()) { + Multimap all = edgeIngestor.getAllRules(schemaVersions.getDefaultVersion()); + + //this part verifies the default properties + // 1) can have only at most 1 containment edge between same node type pair + // 2) if there is at least 1 cousin edge, there must be exactly 1 cousin edge with default=true + for (String key : all.keySet()) { + + Collection edgeRuleCollection = all.get(key); + + boolean foundContainment = false; //can have at most 1 containment rel btwn same pair of node types + boolean foundCousin = false; + boolean cousinDefault = false; //if there is a cousin edge there must be at least 1 default cousin defined + Set labels = new HashSet<>(); //all edges between the same pair must have different labels + int cousinCount = 0; + + for(EdgeRule rule: edgeRuleCollection){ + EdgeRule match = rule; + //check containment + if (!("NONE".equals(match.getContains()))) { + if (foundContainment) { + fail("more than one containment edge defined for " + v.toString() + " " + key); + } else { + foundContainment = true; + } + } else { //check cousin stuff + foundCousin = true; + cousinCount++; + if (match.isDefault()) { + if (!cousinDefault) { + cousinDefault = true; + } else { + fail("more than one cousin edge defined as default for " + v.toString() + " " + key); + } + } + } + + //check labels + String label = match.getLabel(); + if (labels.contains(label)) { + fail("same label found for multiple edges for " + v.toString() + " " + key); + } else { + labels.add(label); + } + } + if (foundCousin && !cousinDefault && cousinCount > 1) { + fail("there is at least one cousin edge but none are designated the default for " + v.toString() + " " + key); + } + } + } + } +} -- cgit 1.2.3-korg