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