2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.serialization.db;
24 import org.apache.tinkerpop.gremlin.structure.Direction;
25 import org.junit.Test;
26 import org.openecomp.aai.AAISetup;
27 import static org.junit.Assert.assertEquals;
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
33 import org.apache.tinkerpop.gremlin.structure.Graph;
34 import org.apache.tinkerpop.gremlin.structure.T;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
37 import org.junit.Rule;
38 import org.junit.rules.ExpectedException;
40 import org.openecomp.aai.exceptions.AAIException;
41 import org.openecomp.aai.introspection.Version;
42 import org.openecomp.aai.serialization.db.exceptions.EdgeMultiplicityException;
43 import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
45 import com.google.common.collect.Multimap;
47 public class EdgeRulesTest extends AAISetup {
49 //set thrown.expect to whatever a specific test needs
50 //this establishes a default of expecting no exceptions to be thrown
52 public ExpectedException thrown = ExpectedException.none();
55 public void verifyOutDirection() throws AAIException, NoEdgeRuleFoundException {
56 EdgeRules rules = EdgeRules.getInstance();
57 EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, "cloud-region", "flavor");
59 assertEquals("out direction", rule.getDirection(), Direction.OUT);
63 public void verifyOutFlippedDirection() throws AAIException, NoEdgeRuleFoundException {
64 EdgeRules rules = EdgeRules.getInstance();
65 EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, "flavor", "cloud-region");
67 assertEquals("in direction", rule.getDirection(), Direction.IN);
71 public void verifyInDirection() throws AAIException, NoEdgeRuleFoundException {
72 EdgeRules rules = EdgeRules.getInstance();
73 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "model-ver", "model-element");
75 assertEquals("in direction", rule.getDirection(), Direction.IN);
79 public void verifyInFlippedDirection() throws AAIException, NoEdgeRuleFoundException {
80 EdgeRules rules = EdgeRules.getInstance();
81 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "model-element", "model-ver");
83 assertEquals("out direction", rule.getDirection(), Direction.OUT);
86 public void verifyMultipleGet() throws AAIException {
87 EdgeRules rules = EdgeRules.getInstance();
88 Map<String, EdgeRule> ruleMap = rules.getEdgeRules("model-element", "model-ver");
89 assertEquals("has isA rule", "isA", ruleMap.get("isA").getLabel());
90 assertEquals("has startsWith rule", "startsWith", ruleMap.get("startsWith").getLabel());
94 public void verifyMultipleGetSingleRule() throws AAIException {
95 EdgeRules rules = EdgeRules.getInstance();
96 Map<String, EdgeRule> ruleMap = rules.getEdgeRules("availability-zone", "complex");
97 assertEquals("has groupsResourcesIn rule", "groupsResourcesIn", ruleMap.get("groupsResourcesIn").getLabel());
101 public void verifyOldEdgeRule() throws AAIException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
102 assertEquals(true, EdgeRules.getInstance().hasEdgeRule("model-element", "model-ver"));
103 assertEquals(true, EdgeRules.getInstance(Version.v8).hasEdgeRule("pserver", "complex"));
104 assertEquals(false, EdgeRules.getInstance(Version.v8).hasEdgeRule("model-element", "model-ver"));
108 public void hasEdgeRuleVertexTest() {
109 Graph graph = TinkerGraph.open();
110 Vertex v1 = graph.addVertex("aai-node-type", "cloud-region");
111 Vertex v2 = graph.addVertex("aai-node-type", "tenant");
112 assertEquals(true, EdgeRules.getInstance().hasEdgeRule(v1, v2));
116 public void getEdgeRuleByTypeAndVertices() throws AAIException {
117 Graph graph = TinkerGraph.open();
118 Vertex v1 = graph.addVertex("aai-node-type", "cloud-region");
119 Vertex v2 = graph.addVertex("aai-node-type", "tenant");
120 EdgeRules rules = EdgeRules.getInstance();
121 EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, v1, v2);
122 assertEquals(true, "OUT".equalsIgnoreCase(rule.getContains()));
123 assertEquals(true, "NONE".equalsIgnoreCase(rule.getDeleteOtherV()));
124 assertEquals(true, MultiplicityRule.ONE2MANY.equals(rule.getMultiplicityRule()));
125 assertEquals(true, "IN".equalsIgnoreCase(rule.getServiceInfrastructure()));
126 assertEquals(true, "OUT".equalsIgnoreCase(rule.getPreventDelete()));
130 public void addTreeEdgeTest() throws AAIException {
131 Graph graph = TinkerGraph.open();
132 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "cloud-region");
133 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "tenant");
134 EdgeRules rules = EdgeRules.getInstance();
135 GraphTraversalSource g = graph.traversal();
136 rules.addTreeEdge(g, v1, v2);
137 assertEquals(true, g.V(v1).out("has").has("aai-node-type", "tenant").hasNext());
139 Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "cloud-region");
140 assertEquals(null, rules.addTreeEdgeIfPossible(g, v3, v2));
144 public void addCousinEdgeTest() throws AAIException {
145 Graph graph = TinkerGraph.open();
146 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "flavor");
147 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "vserver");
148 EdgeRules rules = EdgeRules.getInstance(Version.getLatest());
149 GraphTraversalSource g = graph.traversal();
150 rules.addEdge(g, v1, v2);
151 assertEquals(true, g.V(v2).out("hasFlavor").has("aai-node-type", "flavor").hasNext());
153 Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "flavor");
154 assertEquals(null, rules.addEdgeIfPossible(g, v3, v2));
158 public void multiplicityViolationTest() throws AAIException {
159 thrown.expect(EdgeMultiplicityException.class);
160 thrown.expectMessage("multiplicity rule violated: only one edge can exist with label: uses between vf-module and volume-group");
162 Graph graph = TinkerGraph.open();
163 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "vf-module");
164 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "volume-group");
165 EdgeRules rules = EdgeRules.getInstance(Version.getLatest());
166 GraphTraversalSource g = graph.traversal();
168 rules.addEdge(g, v2, v1);
169 Vertex v3 = graph.addVertex(T.id, "3", "aai-node-type", "vf-module");
170 rules.addEdge(g, v2, v3);
174 public void getChildrenTest() {
175 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
176 Set<EdgeRule> children = rules.getChildren("foo");
177 assertEquals(2, children.size());
178 boolean sawBazRule = false;
179 boolean sawQuuxRule = false;
180 for (EdgeRule r : children) {
181 if ("isVeryHappyAbout".equals(r.getLabel())) {
183 } else if ("dancesWith".equals(r.getLabel())) {
187 assertEquals(true, sawBazRule && sawQuuxRule);
191 public void getAllRulesTest() {
192 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
193 Multimap<String, EdgeRule> allRules = rules.getAllRules();
194 assertEquals(3, allRules.size());
195 assertEquals(true, allRules.containsKey("foo|bar"));
196 assertEquals(true, allRules.containsKey("foo|bar"));
197 assertEquals(true, allRules.containsKey("quux|foo"));
201 public void getAllRulesMissingPropertyTest() {
202 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
204 thrown.expect(RuntimeException.class);
205 thrown.expectMessage("org.openecomp.aai.exceptions.AAIException: Rule between foo and bar is missing property delete-other-v.");
210 public void getChildrenMissingPropertyTest() {
211 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
213 thrown.expect(RuntimeException.class);
214 thrown.expectMessage("org.openecomp.aai.exceptions.AAIException: Rule between quux and foo is missing property SVC-INFRA.");
215 rules.getChildren("foo");
219 public void getEdgeRuleMissingPropertyTest() throws AAIException {
220 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
222 thrown.expect(RuntimeException.class);
223 thrown.expectMessage("org.openecomp.aai.exceptions.AAIException: Rule between quux and foo is missing property SVC-INFRA.");
224 rules.getEdgeRules("foo", "quux");
228 public void verifyAllRules() {
229 // This will cause every rule in the real json files to be verified
230 // so if any required properties are missing, the verification builds
231 // will catch it and incorrect rules can't get merged in.
232 for (Version v : Version.values()) {
233 EdgeRules rules = EdgeRules.getInstance(v);