2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.db;
25 import com.google.common.collect.Multimap;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.edges.EdgeIngestor;
32 import org.onap.aai.edges.EdgeRule;
33 import org.onap.aai.edges.EdgeRuleQuery;
34 import org.onap.aai.edges.enums.EdgeType;
35 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
36 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
37 import org.onap.aai.setup.SchemaVersion;
38 import org.springframework.beans.factory.annotation.Autowired;
41 import java.util.stream.Collectors;
43 import static junit.framework.TestCase.fail;
44 import static org.junit.Assert.assertEquals;
46 public class EdgeRulesTest extends AAISetup {
48 //set thrown.expect to whatever a specific test needs
49 //this establishes a default of expecting no exceptions to be thrown
51 public ExpectedException thrown = ExpectedException.none();
54 private EdgeIngestor edgeIngestor;
57 public void verifyOutDirection() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
59 EdgeRuleQuery ruleQuery = new EdgeRuleQuery
60 .Builder("cloud-region", "flavor")
61 .edgeType(EdgeType.TREE)
64 EdgeRule rule = edgeIngestor.getRule(ruleQuery);
66 assertEquals("out direction", rule.getDirection(), Direction.IN);
70 public void verifyOutFlippedDirection() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
72 EdgeRuleQuery ruleQuery = new EdgeRuleQuery
73 .Builder("flavor", "cloud-region")
74 .edgeType(EdgeType.TREE)
77 EdgeRule rule = edgeIngestor.getRule(ruleQuery);
79 assertEquals("in direction", rule.getDirection(), Direction.OUT);
83 public void verifyMultipleGet() throws EdgeRuleNotFoundException {
85 EdgeRuleQuery ruleQuery = new EdgeRuleQuery
86 .Builder("model-element", "model-ver")
87 .edgeType(EdgeType.TREE)
90 Multimap<String, EdgeRule> ruleMap = edgeIngestor.getRules(ruleQuery);
92 for (EdgeRule edgeRule : ruleMap.get("model|model-ver")) {
93 assertEquals("has isA rule", "org.onap.relationships.inventory.IsA",
100 public void verifyAllRules() throws EdgeRuleNotFoundException {
102 // This will cause every rule in the real json files to be verified
103 // so if any required properties are missing, the verification builds
104 // will catch it and incorrect rules can't get merged in.
105 for (SchemaVersion v : schemaVersions.getVersions()) {
106 Multimap<String, EdgeRule> all = edgeIngestor.getAllRules(schemaVersions.getDefaultVersion());
108 //this part verifies the default properties
109 // 1) can have only at most 1 containment edge between same node type pair
110 // 2) if there is at least 1 cousin edge, there must be exactly 1 cousin edge with default=true
111 for (String key : all.keySet()) {
113 Collection<EdgeRule> edgeRuleCollection = all.get(key);
115 boolean foundContainment = false; //can have at most 1 containment rel btwn same pair of node types
116 boolean foundCousin = false;
117 boolean cousinDefault = false; //if there is a cousin edge there must be at least 1 default cousin defined
118 Set<String> labels = new HashSet<>(); //all edges between the same pair must have different labels
121 for(EdgeRule rule: edgeRuleCollection){
122 EdgeRule match = rule;
124 if (!("NONE".equals(match.getContains()))) {
125 if (foundContainment) {
126 fail("more than one containment edge defined for " + v.toString() + " " + key);
128 foundContainment = true;
130 } else { //check cousin stuff
133 if (match.isDefault()) {
134 if (!cousinDefault) {
135 cousinDefault = true;
137 fail("more than one cousin edge defined as default for " + v.toString() + " " + key);
143 String label = match.getLabel();
144 if (labels.contains(label)) {
145 fail("same label found for multiple edges for " + v.toString() + " " + key);
150 if (foundCousin && !cousinDefault && cousinCount > 1) {
151 fail("there is at least one cousin edge but none are designated the default for " + v.toString() + " " + key);