2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-18 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.onap.aai.edges;
23 import static org.junit.Assert.*;
25 import com.google.common.collect.Multimap;
27 import java.util.Collection;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.onap.aai.config.EdgesConfiguration;
35 import org.onap.aai.edges.enums.AAIDirection;
36 import org.onap.aai.edges.enums.MultiplicityRule;
37 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
38 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
39 import org.onap.aai.restclient.MockProvider;
40 import org.onap.aai.setup.SchemaVersion;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.annotation.DirtiesContext;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(classes = {MockProvider.class, EdgesConfiguration.class})
51 properties = {"schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test.properties",
52 "spring.main.allow-bean-definition-overriding=true"})
53 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
55 public class EdgeIngestorTest {
57 EdgeIngestor edgeIngestor;
60 public ExpectedException thrown = ExpectedException.none();
63 public void getRulesTest1() throws EdgeRuleNotFoundException {
64 EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
65 Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
66 System.out.println(results.size());
67 for (String key : results.keySet()) {
68 System.out.println(key);
70 assertTrue(results.size() == 5);
71 assertTrue(results.containsKey("bar|foo"));
73 assertTrue(2 == results.get("bar|foo").size());
74 boolean seenLabel1 = false;
75 boolean seenLabel2 = false;
76 for (EdgeRule r : results.get("bar|foo")) {
77 if ("eats".equals(r.getLabel())) {
80 if ("eatz".equals(r.getLabel())) {
84 assertTrue(seenLabel1 && seenLabel2);
86 assertTrue(results.containsKey("baz|foo"));
87 assertTrue(results.containsKey("foo|quux"));
88 assertTrue(results.containsKey("dog|foo"));
92 public void getRulesTest2() throws EdgeRuleNotFoundException {
93 EdgeRuleQuery q = new EdgeRuleQuery.Builder("dog", "puppy").build();
94 Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
95 assertTrue(results.size() == 1);
96 assertTrue(results.containsKey("dog|puppy"));
97 Collection<EdgeRule> cr = results.get("dog|puppy");
98 for (EdgeRule r : cr) {
99 assertTrue("dog".equals(r.getFrom()));
100 assertTrue("puppy".equals(r.getTo()));
101 assertTrue("caresFor".equals(r.getLabel()));
102 assertTrue(Direction.OUT.equals(r.getDirection()));
103 assertTrue("One2Many".equalsIgnoreCase(r.getMultiplicityRule().toString()));
104 assertTrue("NONE".equals(r.getContains()));
105 assertTrue("OUT".equals(r.getDeleteOtherV()));
106 assertTrue("NONE".equals(r.getPreventDelete()));
107 assertTrue(r.isDefault());
112 public void getRulesFlippedTypesTest() throws EdgeRuleNotFoundException {
114 new EdgeRuleQuery.Builder("l-interface", "logical-link").version(new SchemaVersion("v11")).build();
115 Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
116 assertTrue(results.size() == 3);
117 for (EdgeRule r : results.get("l-interface|logical-link")) {
118 if ("org.onap.relationships.inventory.Source".equals(r.getLabel())
119 || "org.onap.relationships.inventory.Destination".equals(r.getLabel())) {
120 // these are defined with from=logical-link, to=l-interface, so they must be flipped
121 assertTrue(Direction.IN.equals(r.getDirection()));
122 } else if ("tosca.relationships.network.LinksTo".equals(r.getLabel())) {
123 // this is defined with from=l-interface, to=logical-link, so it shouldn't be flipped
124 assertTrue(Direction.OUT.equals(r.getDirection()));
126 fail("how did you get here");
132 public void fromToSameFlipTests() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
133 // getRules, setting from and to
134 EdgeRuleQuery q = new EdgeRuleQuery.Builder("bloop", "bloop").version(new SchemaVersion("v11")).build();
135 Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
136 assertTrue(results.size() == 1);
137 for (EdgeRule r : results.get("bloop|bloop")) {
138 assertTrue(Direction.IN.equals(r.getDirection()));
141 // getRule, setting just from
142 EdgeRuleQuery q2 = new EdgeRuleQuery.Builder("bloop").version(new SchemaVersion("v11")).build();
143 assertTrue(Direction.IN.equals(edgeIngestor.getRule(q2).getDirection()));
146 Multimap<String, EdgeRule> child = edgeIngestor.getChildRules("bloop", new SchemaVersion("v11"));
147 assertTrue(child.size() == 1);
148 for (EdgeRule r : child.get("bloop|bloop")) {
149 assertTrue(Direction.IN.equals(r.getDirection()));
154 public void getRulesTest3() throws EdgeRuleNotFoundException {
155 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v11")).build();
156 Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
157 assertTrue(results.size() == 4);
158 assertTrue(results.containsKey("lag-interface|l-interface"));
159 assertTrue(results.containsKey("l-interface|logical-link"));
160 assertTrue(results.get("l-interface|logical-link").size() == 3);
164 public void getRulesNoneFound() throws EdgeRuleNotFoundException {
165 thrown.expect(EdgeRuleNotFoundException.class);
166 thrown.expectMessage("No rules found for");
167 EdgeRuleQuery q = new EdgeRuleQuery.Builder("bogus-value").build();
168 edgeIngestor.getRules(q);
172 public void getRuleSimpleTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
173 EdgeRuleQuery q = new EdgeRuleQuery.Builder("parent", "notation").build();
174 EdgeRule result = edgeIngestor.getRule(q);
175 assertTrue("parent".equals(result.getFrom()));
176 assertTrue("notation".equals(result.getTo()));
177 assertTrue("has".equals(result.getLabel()));
178 assertTrue(Direction.OUT.equals(result.getDirection()));
179 assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
180 assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
181 assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
182 assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
183 assertTrue("parent contains notation".equals(result.getDescription()));
186 // public void getRuleSimpleTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
187 // EdgeRuleQuery q = new EdgeRuleQuery.Builder("parent", "notation").build();
188 // Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
189 // assertTrue(results.size() == 1);
190 // // EdgeRule result = edgeIngestor.getRule(q);
191 // for (EdgeRule result : results.get("parent|notation")) {
192 // assertTrue("parent".equals(result.getFrom()));
193 // assertTrue("notation".equals(result.getTo()));
194 // assertTrue("has".equals(result.getLabel()));
195 // assertTrue(Direction.OUT.equals(result.getDirection()));
196 // assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
197 // assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
198 // assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
199 // assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
200 // assertTrue("parent contains notation".equals(result.getDescription()));
205 public void getRuleFlippedTypesTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
206 EdgeRuleQuery q = new EdgeRuleQuery.Builder("notation", "parent").build();
207 EdgeRule result = edgeIngestor.getRule(q);
208 assertTrue("parent".equals(result.getFrom()));
209 assertTrue("notation".equals(result.getTo()));
210 assertTrue("has".equals(result.getLabel()));
211 // direction flipped to match input order per old EdgeRules.java API
212 assertTrue(Direction.IN.equals(result.getDirection()));
213 assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
214 assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
215 assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
216 assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
217 assertTrue("parent contains notation".equals(result.getDescription()));
221 public void getRuleWithDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
224 new EdgeRuleQuery.Builder("l-interface", "logical-link").version(new SchemaVersion("v11")).build();
225 EdgeRule res = edgeIngestor.getRule(q);
226 assertTrue(res.isDefault());
227 assertTrue("tosca.relationships.network.LinksTo".equals(res.getLabel()));
231 public void getRuleWithNonDefault() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
232 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface", "logical-link")
233 .label("org.onap.relationships.inventory.Source").version(new SchemaVersion("v11")).build();
234 EdgeRule res = edgeIngestor.getRule(q);
235 assertFalse(res.isDefault());
236 assertTrue("org.onap.relationships.inventory.Source".equals(res.getLabel()));
240 public void getRuleNoneFoundTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
241 thrown.expect(EdgeRuleNotFoundException.class);
242 thrown.expectMessage("No rule found for");
243 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface", "nonexistent").build();
244 edgeIngestor.getRule(q);
248 public void getRuleTooManyPairsTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
249 thrown.expect(AmbiguousRuleChoiceException.class);
250 thrown.expectMessage("No way to select single rule from these pairs:");
251 EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
252 edgeIngestor.getRule(q);
256 public void getRuleAmbiguousDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
257 thrown.expect(AmbiguousRuleChoiceException.class);
258 thrown.expectMessage("Multiple defaults found.");
259 EdgeRuleQuery q = new EdgeRuleQuery.Builder("seed", "plant").version(new SchemaVersion("v11")).build();
260 edgeIngestor.getRule(q);
264 public void getRuleNoDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
265 thrown.expect(AmbiguousRuleChoiceException.class);
266 thrown.expectMessage("No default found.");
267 EdgeRuleQuery q = new EdgeRuleQuery.Builder("apple", "orange").version(new SchemaVersion("v11")).build();
268 edgeIngestor.getRule(q);
272 public void hasRuleTest() {
273 assertTrue(edgeIngestor
274 .hasRule(new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v11")).build()));
275 assertFalse(edgeIngestor
276 .hasRule(new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v10")).build()));
277 assertTrue(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
278 // assertFalse(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
282 public void getCousinRulesTest() {
283 Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("dog");
284 assertTrue(results.size() == 2);
285 assertTrue(results.containsKey("dog|puppy"));
286 assertTrue(results.containsKey("dog|foo"));
290 public void getCousinRulesWithVersionTest() {
291 Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v10"));
292 assertTrue(results.size() == 2);
293 assertTrue(results.containsKey("bar|foo"));
294 assertTrue(results.get("bar|foo").size() == 2);
298 public void getCousinsNoneInVersionTest() {
299 Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v11"));
300 assertTrue(results.isEmpty());
304 public void hasCousinTest() {
305 assertTrue(edgeIngestor.hasCousinRule("foo"));
306 assertTrue(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v10")));
307 assertFalse(edgeIngestor.hasCousinRule("parent"));
308 assertFalse(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v11")));
312 public void getChildRulesTest() {
313 Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("parent");
314 assertTrue(results.size() == 6);
315 assertTrue(results.containsKey("notation|parent"));
316 assertTrue(results.containsKey("not-notation|parent"));
317 assertTrue(results.containsKey("out-out|parent"));
318 assertTrue(results.containsKey("in-in|parent"));
319 assertTrue(results.containsKey("in-out|parent"));
320 assertTrue(results.containsKey("out-in|parent"));
324 public void getChildRulesWithVersionTest() {
325 Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v10"));
326 assertTrue(results.size() == 2);
327 assertTrue(results.containsKey("baz|foo"));
328 assertTrue(results.containsKey("foo|quux"));
332 public void getChildRulesNoneInVersionTest() {
333 Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v11"));
334 assertTrue(results.isEmpty());
338 public void hasChildTest() {
339 assertTrue(edgeIngestor.hasChildRule("foo"));
340 assertTrue(edgeIngestor.hasChildRule("foo", new SchemaVersion("v10")));
341 assertFalse(edgeIngestor.hasChildRule("puppy"));
342 assertFalse(edgeIngestor.hasChildRule("foo", new SchemaVersion("v11")));
346 public void getParentRulesTest() {
347 Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("parent");
348 assertTrue(results.size() == 6);
349 assertTrue(results.containsKey("grandparent1|parent"));
350 assertTrue(results.containsKey("grandparent2|parent"));
351 assertTrue(results.containsKey("grandparent3|parent"));
352 assertTrue(results.containsKey("grandparent4|parent"));
353 assertTrue(results.containsKey("grandparent5|parent"));
354 assertTrue(results.containsKey("grandparent6|parent"));
358 public void getParentRulesWithVersionTest() {
359 Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v10"));
360 assertTrue(results.size() == 1);
361 assertTrue(results.containsKey("baz|foo"));
365 public void getParentRulesNoneInVersionTest() {
366 Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v11"));
367 assertTrue(results.isEmpty());
371 public void hasParentTest() {
372 assertTrue(edgeIngestor.hasParentRule("parent"));
373 assertTrue(edgeIngestor.hasParentRule("quux", new SchemaVersion("v10")));
374 assertFalse(edgeIngestor.hasParentRule("puppy"));
375 assertFalse(edgeIngestor.hasParentRule("foo", new SchemaVersion("v11")));
379 public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
380 Multimap<String, EdgeRule> res = edgeIngestor.getAllCurrentRules();
381 assertTrue(res.size() == 24);
385 public void getAllRulesTest() throws EdgeRuleNotFoundException {
386 Multimap<String, EdgeRule> res = edgeIngestor.getAllRules(new SchemaVersion("v10"));
387 assertTrue(res.size() == 4);
388 assertTrue(res.containsKey("bar|foo"));
389 assertTrue(res.get("bar|foo").size() == 2);
390 assertTrue(res.containsKey("baz|foo"));
391 assertTrue(res.containsKey("foo|quux"));
393 thrown.expect(EdgeRuleNotFoundException.class);
394 thrown.expectMessage("No rules found for version v9.");
395 edgeIngestor.getAllRules(new SchemaVersion("v9"));