Reenable spring-boot2 in aai-common
[aai/aai-common.git] / aai-schema-ingest / src / test / java / org / onap / aai / edges / EdgeIngestorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.edges;
22
23 import static org.junit.Assert.*;
24
25 import com.google.common.collect.Multimap;
26
27 import java.util.Collection;
28
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.junit.Ignore;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.onap.aai.config.EdgesConfiguration;
36 import org.onap.aai.edges.enums.AAIDirection;
37 import org.onap.aai.edges.enums.MultiplicityRule;
38 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
39 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
40 import org.onap.aai.restclient.MockProvider;
41 import org.onap.aai.restclient.MockRestClient;
42 import org.onap.aai.setup.SchemaVersion;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.test.annotation.DirtiesContext;
46 import org.springframework.test.context.ContextConfiguration;
47 import org.springframework.test.context.TestPropertySource;
48 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @ContextConfiguration(classes = {MockProvider.class, EdgesConfiguration.class})
52 @TestPropertySource(
53         properties = {
54             "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test.properties",
55             "spring.main.allow-bean-definition-overriding=true"
56         })
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 @SpringBootTest
59 public class EdgeIngestorTest {
60     @Autowired
61     EdgeIngestor edgeIngestor;
62
63     @Rule
64     public ExpectedException thrown = ExpectedException.none();
65
66     @Test
67     public void getRulesTest1() throws EdgeRuleNotFoundException {
68         EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
69         Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
70         System.out.println(results.size());
71         for (String key : results.keySet()) {
72             System.out.println(key);
73         }
74         assertTrue(results.size() == 5);
75         assertTrue(results.containsKey("bar|foo"));
76
77         assertTrue(2 == results.get("bar|foo").size());
78         boolean seenLabel1 = false;
79         boolean seenLabel2 = false;
80         for (EdgeRule r : results.get("bar|foo")) {
81             if ("eats".equals(r.getLabel())) {
82                 seenLabel1 = true;
83             }
84             if ("eatz".equals(r.getLabel())) {
85                 seenLabel2 = true;
86             }
87         }
88         assertTrue(seenLabel1 && seenLabel2);
89
90         assertTrue(results.containsKey("baz|foo"));
91         assertTrue(results.containsKey("foo|quux"));
92         assertTrue(results.containsKey("dog|foo"));
93     }
94
95     @Test
96     public void getRulesTest2() throws EdgeRuleNotFoundException {
97         EdgeRuleQuery q = new EdgeRuleQuery.Builder("dog", "puppy").build();
98         Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
99         assertTrue(results.size() == 1);
100         assertTrue(results.containsKey("dog|puppy"));
101         Collection<EdgeRule> cr = results.get("dog|puppy");
102         for (EdgeRule r : cr) {
103             assertTrue("dog".equals(r.getFrom()));
104             assertTrue("puppy".equals(r.getTo()));
105             assertTrue("caresFor".equals(r.getLabel()));
106             assertTrue(Direction.OUT.equals(r.getDirection()));
107             assertTrue("One2Many".equalsIgnoreCase(r.getMultiplicityRule().toString()));
108             assertTrue("NONE".equals(r.getContains()));
109             assertTrue("OUT".equals(r.getDeleteOtherV()));
110             assertTrue("NONE".equals(r.getPreventDelete()));
111             assertTrue(r.isDefault());
112         }
113     }
114
115     @Test
116     public void getRulesFlippedTypesTest() throws EdgeRuleNotFoundException {
117         EdgeRuleQuery q =
118                 new EdgeRuleQuery.Builder("l-interface", "logical-link").version(new SchemaVersion("v11")).build();
119         Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
120         assertTrue(results.size() == 3);
121         for (EdgeRule r : results.get("l-interface|logical-link")) {
122             if ("org.onap.relationships.inventory.Source".equals(r.getLabel())
123                     || "org.onap.relationships.inventory.Destination".equals(r.getLabel())) {
124                 // these are defined with from=logical-link, to=l-interface, so they must be flipped
125                 assertTrue(Direction.IN.equals(r.getDirection()));
126             } else if ("tosca.relationships.network.LinksTo".equals(r.getLabel())) {
127                 // this is defined with from=l-interface, to=logical-link, so it shouldn't be flipped
128                 assertTrue(Direction.OUT.equals(r.getDirection()));
129             } else {
130                 fail("how did you get here");
131             }
132         }
133     }
134
135     @Test
136     public void fromToSameFlipTests() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
137         // getRules, setting from and to
138         EdgeRuleQuery q = new EdgeRuleQuery.Builder("bloop", "bloop").version(new SchemaVersion("v11")).build();
139         Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
140         assertTrue(results.size() == 1);
141         for (EdgeRule r : results.get("bloop|bloop")) {
142             assertTrue(Direction.IN.equals(r.getDirection()));
143         }
144
145         // getRule, setting just from
146         EdgeRuleQuery q2 = new EdgeRuleQuery.Builder("bloop").version(new SchemaVersion("v11")).build();
147         assertTrue(Direction.IN.equals(edgeIngestor.getRule(q2).getDirection()));
148
149         // getChildRules
150         Multimap<String, EdgeRule> child = edgeIngestor.getChildRules("bloop", new SchemaVersion("v11"));
151         assertTrue(child.size() == 1);
152         for (EdgeRule r : child.get("bloop|bloop")) {
153             assertTrue(Direction.IN.equals(r.getDirection()));
154         }
155     }
156
157     @Test
158     public void getRulesTest3() throws EdgeRuleNotFoundException {
159         EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v11")).build();
160         Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
161         assertTrue(results.size() == 4);
162         assertTrue(results.containsKey("lag-interface|l-interface"));
163         assertTrue(results.containsKey("l-interface|logical-link"));
164         assertTrue(results.get("l-interface|logical-link").size() == 3);
165     }
166
167     @Test
168     public void getRulesNoneFound() throws EdgeRuleNotFoundException {
169         thrown.expect(EdgeRuleNotFoundException.class);
170         thrown.expectMessage("No rules found for");
171         EdgeRuleQuery q = new EdgeRuleQuery.Builder("bogus-value").build();
172         edgeIngestor.getRules(q);
173     }
174
175     @Test
176     public void getRuleSimpleTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
177         EdgeRuleQuery q = new EdgeRuleQuery.Builder("parent", "notation").build();
178         EdgeRule result = edgeIngestor.getRule(q);
179         assertTrue("parent".equals(result.getFrom()));
180         assertTrue("notation".equals(result.getTo()));
181         assertTrue("has".equals(result.getLabel()));
182         assertTrue(Direction.OUT.equals(result.getDirection()));
183         assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
184         assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
185         assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
186         assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
187         assertTrue("parent contains notation".equals(result.getDescription()));
188     }
189     // @Test
190     // public void getRuleSimpleTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
191     // EdgeRuleQuery q = new EdgeRuleQuery.Builder("parent", "notation").build();
192     // Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
193     // assertTrue(results.size() == 1);
194     // // EdgeRule result = edgeIngestor.getRule(q);
195     // for (EdgeRule result : results.get("parent|notation")) {
196     // assertTrue("parent".equals(result.getFrom()));
197     // assertTrue("notation".equals(result.getTo()));
198     // assertTrue("has".equals(result.getLabel()));
199     // assertTrue(Direction.OUT.equals(result.getDirection()));
200     // assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
201     // assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
202     // assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
203     // assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
204     // assertTrue("parent contains notation".equals(result.getDescription()));
205     // }
206     // }
207
208     @Test
209     public void getRuleFlippedTypesTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
210         EdgeRuleQuery q = new EdgeRuleQuery.Builder("notation", "parent").build();
211         EdgeRule result = edgeIngestor.getRule(q);
212         assertTrue("parent".equals(result.getFrom()));
213         assertTrue("notation".equals(result.getTo()));
214         assertTrue("has".equals(result.getLabel()));
215         // direction flipped to match input order per old EdgeRules.java API
216         assertTrue(Direction.IN.equals(result.getDirection()));
217         assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
218         assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
219         assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
220         assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
221         assertTrue("parent contains notation".equals(result.getDescription()));
222     }
223
224     @Test
225     public void getRuleWithDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
226
227         EdgeRuleQuery q =
228                 new EdgeRuleQuery.Builder("l-interface", "logical-link").version(new SchemaVersion("v11")).build();
229         EdgeRule res = edgeIngestor.getRule(q);
230         assertTrue(res.isDefault());
231         assertTrue("tosca.relationships.network.LinksTo".equals(res.getLabel()));
232     }
233
234     @Test
235     public void getRuleWithNonDefault() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
236         EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface", "logical-link")
237                 .label("org.onap.relationships.inventory.Source").version(new SchemaVersion("v11")).build();
238         EdgeRule res = edgeIngestor.getRule(q);
239         assertFalse(res.isDefault());
240         assertTrue("org.onap.relationships.inventory.Source".equals(res.getLabel()));
241     }
242
243     @Test
244     public void getRuleNoneFoundTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
245         thrown.expect(EdgeRuleNotFoundException.class);
246         thrown.expectMessage("No rule found for");
247         EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface", "nonexistent").build();
248         edgeIngestor.getRule(q);
249     }
250
251     @Test
252     public void getRuleTooManyPairsTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
253         thrown.expect(AmbiguousRuleChoiceException.class);
254         thrown.expectMessage("No way to select single rule from these pairs:");
255         EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
256         edgeIngestor.getRule(q);
257     }
258
259     @Test
260     public void getRuleAmbiguousDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
261         thrown.expect(AmbiguousRuleChoiceException.class);
262         thrown.expectMessage("Multiple defaults found.");
263         EdgeRuleQuery q = new EdgeRuleQuery.Builder("seed", "plant").version(new SchemaVersion("v11")).build();
264         edgeIngestor.getRule(q);
265     }
266
267     @Test
268     public void getRuleNoDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
269         thrown.expect(AmbiguousRuleChoiceException.class);
270         thrown.expectMessage("No default found.");
271         EdgeRuleQuery q = new EdgeRuleQuery.Builder("apple", "orange").version(new SchemaVersion("v11")).build();
272         edgeIngestor.getRule(q);
273     }
274
275     @Test
276     public void hasRuleTest() {
277         assertTrue(edgeIngestor
278                 .hasRule(new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v11")).build()));
279         assertFalse(edgeIngestor
280                 .hasRule(new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v10")).build()));
281         assertTrue(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
282         // assertFalse(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
283     }
284
285     @Test
286     public void getCousinRulesTest() {
287         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("dog");
288         assertTrue(results.size() == 2);
289         assertTrue(results.containsKey("dog|puppy"));
290         assertTrue(results.containsKey("dog|foo"));
291     }
292
293     @Test
294     public void getCousinRulesWithVersionTest() {
295         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v10"));
296         assertTrue(results.size() == 2);
297         assertTrue(results.containsKey("bar|foo"));
298         assertTrue(results.get("bar|foo").size() == 2);
299     }
300
301     @Test
302     public void getCousinsNoneInVersionTest() {
303         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v11"));
304         assertTrue(results.isEmpty());
305     }
306
307     @Test
308     public void hasCousinTest() {
309         assertTrue(edgeIngestor.hasCousinRule("foo"));
310         assertTrue(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v10")));
311         assertFalse(edgeIngestor.hasCousinRule("parent"));
312         assertFalse(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v11")));
313     }
314
315     @Test
316     public void getChildRulesTest() {
317         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("parent");
318         assertTrue(results.size() == 6);
319         assertTrue(results.containsKey("notation|parent"));
320         assertTrue(results.containsKey("not-notation|parent"));
321         assertTrue(results.containsKey("out-out|parent"));
322         assertTrue(results.containsKey("in-in|parent"));
323         assertTrue(results.containsKey("in-out|parent"));
324         assertTrue(results.containsKey("out-in|parent"));
325     }
326
327     @Test
328     public void getChildRulesWithVersionTest() {
329         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v10"));
330         assertTrue(results.size() == 2);
331         assertTrue(results.containsKey("baz|foo"));
332         assertTrue(results.containsKey("foo|quux"));
333     }
334
335     @Test
336     public void getChildRulesNoneInVersionTest() {
337         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v11"));
338         assertTrue(results.isEmpty());
339     }
340
341     @Test
342     public void hasChildTest() {
343         assertTrue(edgeIngestor.hasChildRule("foo"));
344         assertTrue(edgeIngestor.hasChildRule("foo", new SchemaVersion("v10")));
345         assertFalse(edgeIngestor.hasChildRule("puppy"));
346         assertFalse(edgeIngestor.hasChildRule("foo", new SchemaVersion("v11")));
347     }
348
349     @Test
350     public void getParentRulesTest() {
351         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("parent");
352         assertTrue(results.size() == 6);
353         assertTrue(results.containsKey("grandparent1|parent"));
354         assertTrue(results.containsKey("grandparent2|parent"));
355         assertTrue(results.containsKey("grandparent3|parent"));
356         assertTrue(results.containsKey("grandparent4|parent"));
357         assertTrue(results.containsKey("grandparent5|parent"));
358         assertTrue(results.containsKey("grandparent6|parent"));
359     }
360
361     @Test
362     public void getParentRulesWithVersionTest() {
363         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v10"));
364         assertTrue(results.size() == 1);
365         assertTrue(results.containsKey("baz|foo"));
366     }
367
368     @Test
369     public void getParentRulesNoneInVersionTest() {
370         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v11"));
371         assertTrue(results.isEmpty());
372     }
373
374     @Test
375     public void hasParentTest() {
376         assertTrue(edgeIngestor.hasParentRule("parent"));
377         assertTrue(edgeIngestor.hasParentRule("quux", new SchemaVersion("v10")));
378         assertFalse(edgeIngestor.hasParentRule("puppy"));
379         assertFalse(edgeIngestor.hasParentRule("foo", new SchemaVersion("v11")));
380     }
381
382     @Test
383     public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
384         Multimap<String, EdgeRule> res = edgeIngestor.getAllCurrentRules();
385         assertTrue(res.size() == 24);
386     }
387
388     @Test
389     public void getAllRulesTest() throws EdgeRuleNotFoundException {
390         Multimap<String, EdgeRule> res = edgeIngestor.getAllRules(new SchemaVersion("v10"));
391         assertTrue(res.size() == 4);
392         assertTrue(res.containsKey("bar|foo"));
393         assertTrue(res.get("bar|foo").size() == 2);
394         assertTrue(res.containsKey("baz|foo"));
395         assertTrue(res.containsKey("foo|quux"));
396
397         thrown.expect(EdgeRuleNotFoundException.class);
398         thrown.expectMessage("No rules found for version v9.");
399         edgeIngestor.getAllRules(new SchemaVersion("v9"));
400     }
401 }