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 org.apache.tinkerpop.gremlin.structure.Direction;
26 import org.junit.Test;
27 import org.onap.aai.AAISetup;
28 import static org.junit.Assert.assertEquals;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
34 import org.apache.tinkerpop.gremlin.structure.Graph;
35 import org.apache.tinkerpop.gremlin.structure.T;
36 import org.apache.tinkerpop.gremlin.structure.Vertex;
37 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
38 import org.junit.Rule;
39 import org.junit.rules.ExpectedException;
41 import org.onap.aai.exceptions.AAIException;
42 import org.onap.aai.introspection.Version;
43 import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException;
44 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
46 import com.google.common.collect.Multimap;
48 public class EdgeRulesTest extends AAISetup {
50 //set thrown.expect to whatever a specific test needs
51 //this establishes a default of expecting no exceptions to be thrown
53 public ExpectedException thrown = ExpectedException.none();
56 public void verifyOutDirection() throws AAIException, NoEdgeRuleFoundException {
57 EdgeRules rules = EdgeRules.getInstance();
58 EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, "cloud-region", "flavor");
60 assertEquals("out direction", rule.getDirection(), Direction.OUT);
64 public void verifyOutFlippedDirection() throws AAIException, NoEdgeRuleFoundException {
65 EdgeRules rules = EdgeRules.getInstance();
66 EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, "flavor", "cloud-region");
68 assertEquals("in direction", rule.getDirection(), Direction.IN);
72 public void verifyInDirection() throws AAIException, NoEdgeRuleFoundException {
73 EdgeRules rules = EdgeRules.getInstance();
74 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "model-ver", "model-element");
76 assertEquals("in direction", rule.getDirection(), Direction.IN);
80 public void verifyInFlippedDirection() throws AAIException, NoEdgeRuleFoundException {
81 EdgeRules rules = EdgeRules.getInstance();
82 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "model-element", "model-ver");
84 assertEquals("out direction", rule.getDirection(), Direction.OUT);
87 public void verifyMultipleGet() throws AAIException {
88 EdgeRules rules = EdgeRules.getInstance();
89 Map<String, EdgeRule> ruleMap = rules.getEdgeRules("model-element", "model-ver");
90 assertEquals("has isA rule", "isA", ruleMap.get("isA").getLabel());
91 assertEquals("has startsWith rule", "startsWith", ruleMap.get("startsWith").getLabel());
95 public void verifyMultipleGetSingleRule() throws AAIException {
96 EdgeRules rules = EdgeRules.getInstance();
97 Map<String, EdgeRule> ruleMap = rules.getEdgeRules("availability-zone", "complex");
98 assertEquals("has groupsResourcesIn rule", "groupsResourcesIn", ruleMap.get("groupsResourcesIn").getLabel());
102 public void verifyOldEdgeRule() throws AAIException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
103 assertEquals(true, EdgeRules.getInstance().hasEdgeRule("model-element", "model-ver"));
104 assertEquals(true, EdgeRules.getInstance(Version.v8).hasEdgeRule("pserver", "complex"));
105 assertEquals(false, EdgeRules.getInstance(Version.v8).hasEdgeRule("model-element", "model-ver"));
109 public void hasEdgeRuleVertexTest() {
110 Graph graph = TinkerGraph.open();
111 Vertex v1 = graph.addVertex("aai-node-type", "cloud-region");
112 Vertex v2 = graph.addVertex("aai-node-type", "tenant");
113 assertEquals(true, EdgeRules.getInstance().hasEdgeRule(v1, v2));
117 public void getEdgeRuleByTypeAndVertices() throws AAIException {
118 Graph graph = TinkerGraph.open();
119 Vertex v1 = graph.addVertex("aai-node-type", "cloud-region");
120 Vertex v2 = graph.addVertex("aai-node-type", "tenant");
121 EdgeRules rules = EdgeRules.getInstance();
122 EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, v1, v2);
123 assertEquals(true, "OUT".equalsIgnoreCase(rule.getContains()));
124 assertEquals(true, "OUT".equalsIgnoreCase(rule.getDeleteOtherV()));
125 assertEquals(true, MultiplicityRule.ONE2MANY.equals(rule.getMultiplicityRule()));
126 assertEquals(true, "IN".equalsIgnoreCase(rule.getServiceInfrastructure()));
127 assertEquals(true, "OUT".equalsIgnoreCase(rule.getPreventDelete()));
131 public void addTreeEdgeTest() throws AAIException {
132 Graph graph = TinkerGraph.open();
133 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "cloud-region");
134 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "tenant");
135 EdgeRules rules = EdgeRules.getInstance();
136 GraphTraversalSource g = graph.traversal();
137 rules.addTreeEdge(g, v1, v2);
138 assertEquals(true, g.V(v1).out("has").has("aai-node-type", "tenant").hasNext());
140 Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "cloud-region");
141 assertEquals(null, rules.addTreeEdgeIfPossible(g, v3, v2));
145 public void addCousinEdgeTest() throws AAIException {
146 Graph graph = TinkerGraph.open();
147 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "flavor");
148 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "vserver");
149 EdgeRules rules = EdgeRules.getInstance(Version.getLatest());
150 GraphTraversalSource g = graph.traversal();
151 rules.addEdge(g, v1, v2);
152 assertEquals(true, g.V(v2).out("hasFlavor").has("aai-node-type", "flavor").hasNext());
154 Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "flavor");
155 assertEquals(null, rules.addEdgeIfPossible(g, v3, v2));
159 public void multiplicityViolationTest() throws AAIException {
160 thrown.expect(EdgeMultiplicityException.class);
161 thrown.expectMessage("multiplicity rule violated: only one edge can exist with label: uses between vf-module and volume-group");
163 Graph graph = TinkerGraph.open();
164 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "vf-module");
165 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "volume-group");
166 EdgeRules rules = EdgeRules.getInstance(Version.getLatest());
167 GraphTraversalSource g = graph.traversal();
169 rules.addEdge(g, v2, v1);
170 Vertex v3 = graph.addVertex(T.id, "3", "aai-node-type", "vf-module");
171 rules.addEdge(g, v2, v3);
175 public void getChildrenTest() {
176 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
177 Set<EdgeRule> children = rules.getChildren("foo");
178 assertEquals(2, children.size());
179 boolean sawBazRule = false;
180 boolean sawQuuxRule = false;
181 for (EdgeRule r : children) {
182 if ("isVeryHappyAbout".equals(r.getLabel())) {
184 } else if ("dancesWith".equals(r.getLabel())) {
188 assertEquals(true, sawBazRule && sawQuuxRule);
192 public void getAllRulesTest() {
193 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
194 Multimap<String, EdgeRule> allRules = rules.getAllRules();
195 assertEquals(6, allRules.size());
196 assertEquals(true, allRules.containsKey("foo|bar"));
197 assertEquals(true, allRules.containsKey("foo|bar"));
198 assertEquals(true, allRules.containsKey("quux|foo"));
202 public void getAllRulesMissingPropertyTest() {
203 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
205 thrown.expect(RuntimeException.class);
206 thrown.expectMessage("org.onap.aai.exceptions.AAIException: Rule between foo and bar is missing property delete-other-v.");
211 public void getChildrenMissingPropertyTest() {
212 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
214 thrown.expect(RuntimeException.class);
215 thrown.expectMessage("org.onap.aai.exceptions.AAIException: Rule between quux and foo is missing property SVC-INFRA.");
216 rules.getChildren("foo");
220 public void getEdgeRuleMissingPropertyTest() throws AAIException {
221 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
223 thrown.expect(RuntimeException.class);
224 thrown.expectMessage("org.onap.aai.exceptions.AAIException: Rule between quux and foo is missing property SVC-INFRA.");
225 rules.getEdgeRules("foo", "quux");
229 public void verifyAllRules() {
230 // This will cause every rule in the real json files to be verified
231 // so if any required properties are missing, the verification builds
232 // will catch it and incorrect rules can't get merged in.
233 for (Version v : Version.values()) {
234 EdgeRules rules = EdgeRules.getInstance(v);