aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/test/java/org/openecomp/aai/serialization/db/EdgeRulesTest.java
blob: 87bb6ca781f1320e0e34dc914442ab6bd828024d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 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=========================================================
 */

package org.openecomp.aai.serialization.db;


import org.apache.tinkerpop.gremlin.structure.Direction;
import org.junit.Test;
import org.openecomp.aai.AAISetup;
import static org.junit.Assert.assertEquals;

import java.util.Map;
import java.util.Set;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.introspection.Version;
import org.openecomp.aai.serialization.db.exceptions.EdgeMultiplicityException;
import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;

import com.google.common.collect.Multimap;

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();

	@Test
	public void verifyOutDirection() throws AAIException, NoEdgeRuleFoundException {
		EdgeRules rules = EdgeRules.getInstance();
		EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, "cloud-region", "flavor");
		
		assertEquals("out direction", rule.getDirection(), Direction.OUT);
	}
	
	@Test
	public void verifyOutFlippedDirection() throws AAIException, NoEdgeRuleFoundException {
		EdgeRules rules = EdgeRules.getInstance();
		EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, "flavor", "cloud-region");
		
		assertEquals("in direction", rule.getDirection(), Direction.IN);
	}
	
	@Test
	public void verifyInDirection() throws AAIException, NoEdgeRuleFoundException {
		EdgeRules rules = EdgeRules.getInstance();
		EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "model-ver", "model-element");
		
		assertEquals("in direction", rule.getDirection(), Direction.IN);
	}
	
	@Test
	public void verifyInFlippedDirection() throws AAIException, NoEdgeRuleFoundException {
		EdgeRules rules = EdgeRules.getInstance();
		EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "model-element", "model-ver");
		
		assertEquals("out direction", rule.getDirection(), Direction.OUT);
	}
	@Test
	public void verifyMultipleGet() throws AAIException {
		EdgeRules rules = EdgeRules.getInstance();
		Map<String, EdgeRule> ruleMap = rules.getEdgeRules("model-element", "model-ver");
		assertEquals("has isA rule", "isA", ruleMap.get("isA").getLabel());
		assertEquals("has startsWith rule", "startsWith", ruleMap.get("startsWith").getLabel());
	}
	
	@Test
	public void verifyMultipleGetSingleRule() throws AAIException {
		EdgeRules rules = EdgeRules.getInstance();
		Map<String, EdgeRule> ruleMap = rules.getEdgeRules("availability-zone", "complex");
		assertEquals("has groupsResourcesIn rule", "groupsResourcesIn", ruleMap.get("groupsResourcesIn").getLabel());
	}
	
	@Test
	public void verifyOldEdgeRule() throws AAIException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
		assertEquals(true, EdgeRules.getInstance().hasEdgeRule("model-element", "model-ver"));
		assertEquals(true, EdgeRules.getInstance(Version.v8).hasEdgeRule("pserver", "complex"));
		assertEquals(false, EdgeRules.getInstance(Version.v8).hasEdgeRule("model-element", "model-ver"));
	}

	@Test
	public void hasEdgeRuleVertexTest() {
		Graph graph = TinkerGraph.open();
		Vertex v1 = graph.addVertex("aai-node-type", "cloud-region");
		Vertex v2 = graph.addVertex("aai-node-type", "tenant");
		assertEquals(true, EdgeRules.getInstance().hasEdgeRule(v1, v2));
	}
	
	@Test
	public void getEdgeRuleByTypeAndVertices() throws AAIException {
		Graph graph = TinkerGraph.open();
		Vertex v1 = graph.addVertex("aai-node-type", "cloud-region");
		Vertex v2 = graph.addVertex("aai-node-type", "tenant");
		EdgeRules rules = EdgeRules.getInstance();
		EdgeRule rule = rules.getEdgeRule(EdgeType.TREE, v1, v2);
		assertEquals(true, "OUT".equalsIgnoreCase(rule.getContains()));
		assertEquals(true, "NONE".equalsIgnoreCase(rule.getDeleteOtherV()));
		assertEquals(true, MultiplicityRule.ONE2MANY.equals(rule.getMultiplicityRule()));
		assertEquals(true,  "IN".equalsIgnoreCase(rule.getServiceInfrastructure()));
		assertEquals(true, "OUT".equalsIgnoreCase(rule.getPreventDelete()));
	}
	
	@Test
	public void addTreeEdgeTest() throws AAIException {
		Graph graph = TinkerGraph.open();
		Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "cloud-region");
		Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "tenant");
		EdgeRules rules = EdgeRules.getInstance();
		GraphTraversalSource g = graph.traversal();
		rules.addTreeEdge(g, v1, v2);
		assertEquals(true, g.V(v1).out("has").has("aai-node-type", "tenant").hasNext());
		
		Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "cloud-region");
		assertEquals(null, rules.addTreeEdgeIfPossible(g, v3, v2));
	}
	
	@Test
	public void addCousinEdgeTest() throws AAIException {
		Graph graph = TinkerGraph.open();
		Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "flavor");
		Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "vserver");
		EdgeRules rules = EdgeRules.getInstance(Version.getLatest());
		GraphTraversalSource g = graph.traversal();
		rules.addEdge(g, v1, v2);
		assertEquals(true, g.V(v2).out("hasFlavor").has("aai-node-type", "flavor").hasNext());
		
		Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "flavor");
		assertEquals(null, rules.addEdgeIfPossible(g, v3, v2));
	}
	
	@Test
	public void multiplicityViolationTest() throws AAIException {
		thrown.expect(EdgeMultiplicityException.class);
		thrown.expectMessage("multiplicity rule violated: only one edge can exist with label: uses between vf-module and volume-group");
		
		Graph graph = TinkerGraph.open();
		Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "vf-module");
		Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "volume-group");
		EdgeRules rules = EdgeRules.getInstance(Version.getLatest());
		GraphTraversalSource g = graph.traversal();
		
		rules.addEdge(g, v2, v1);
		Vertex v3 = graph.addVertex(T.id, "3", "aai-node-type", "vf-module");
		rules.addEdge(g, v2, v3);
	}
	
	@Test
	public void getChildrenTest() {
		EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
		Set<EdgeRule> children = rules.getChildren("foo");
		assertEquals(2, children.size());
		boolean sawBazRule = false;
		boolean sawQuuxRule = false;
		for (EdgeRule r : children) {
			if ("isVeryHappyAbout".equals(r.getLabel())) { 
				sawBazRule = true; 
			} else if ("dancesWith".equals(r.getLabel())) {
				sawQuuxRule = true;
			}
		}
		assertEquals(true, sawBazRule && sawQuuxRule);
	}
	
	@Test
	public void getAllRulesTest() {
		EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
		Multimap<String, EdgeRule> allRules = rules.getAllRules();
		assertEquals(3, allRules.size());
		assertEquals(true, allRules.containsKey("foo|bar"));
		assertEquals(true, allRules.containsKey("foo|bar"));
		assertEquals(true, allRules.containsKey("quux|foo"));
	}
	
	@Test
	public void getAllRulesMissingPropertyTest() {
		EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
		
		thrown.expect(RuntimeException.class);
		thrown.expectMessage("org.openecomp.aai.exceptions.AAIException: Rule between foo and bar is missing property delete-other-v.");
		rules.getAllRules();
	}
	
	@Test
	public void getChildrenMissingPropertyTest() {
		EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
		
		thrown.expect(RuntimeException.class);
		thrown.expectMessage("org.openecomp.aai.exceptions.AAIException: Rule between quux and foo is missing property SVC-INFRA.");
		rules.getChildren("foo");
	}
	
	@Test
	public void getEdgeRuleMissingPropertyTest() throws AAIException {
		EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test_broken.json");
		
		thrown.expect(RuntimeException.class);
		thrown.expectMessage("org.openecomp.aai.exceptions.AAIException: Rule between quux and foo is missing property SVC-INFRA.");
		rules.getEdgeRules("foo", "quux");
	}
	
	@Test
	public void verifyAllRules() {
		// 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 (Version v : Version.values()) {
			EdgeRules rules = EdgeRules.getInstance(v);
			rules.getAllRules();
		}
	}
}