Merge "[AAI] Fix doc config files"
[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.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;
47
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(classes = {MockProvider.class, EdgesConfiguration.class})
50 @TestPropertySource(
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)
54 @SpringBootTest
55 public class EdgeIngestorTest {
56     @Autowired
57     EdgeIngestor edgeIngestor;
58
59     @Rule
60     public ExpectedException thrown = ExpectedException.none();
61
62     @Test
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);
69         }
70         assertTrue(results.size() == 5);
71         assertTrue(results.containsKey("bar|foo"));
72
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())) {
78                 seenLabel1 = true;
79             }
80             if ("eatz".equals(r.getLabel())) {
81                 seenLabel2 = true;
82             }
83         }
84         assertTrue(seenLabel1 && seenLabel2);
85
86         assertTrue(results.containsKey("baz|foo"));
87         assertTrue(results.containsKey("foo|quux"));
88         assertTrue(results.containsKey("dog|foo"));
89     }
90
91     @Test
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());
108         }
109     }
110
111     @Test
112     public void getRulesFlippedTypesTest() throws EdgeRuleNotFoundException {
113         EdgeRuleQuery q =
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()));
125             } else {
126                 fail("how did you get here");
127             }
128         }
129     }
130
131     @Test
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()));
139         }
140
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()));
144
145         // getChildRules
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()));
150         }
151     }
152
153     @Test
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);
161     }
162
163     @Test
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);
169     }
170
171     @Test
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()));
184     }
185     // @Test
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()));
201     // }
202     // }
203
204     @Test
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()));
218     }
219
220     @Test
221     public void getRuleWithDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
222
223         EdgeRuleQuery q =
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()));
228     }
229
230     @Test
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()));
237     }
238
239     @Test
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);
245     }
246
247     @Test
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);
253     }
254
255     @Test
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);
261     }
262
263     @Test
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);
269     }
270
271     @Test
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()));
279     }
280
281     @Test
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"));
287     }
288
289     @Test
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);
295     }
296
297     @Test
298     public void getCousinsNoneInVersionTest() {
299         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v11"));
300         assertTrue(results.isEmpty());
301     }
302
303     @Test
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")));
309     }
310
311     @Test
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"));
321     }
322
323     @Test
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"));
329     }
330
331     @Test
332     public void getChildRulesNoneInVersionTest() {
333         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v11"));
334         assertTrue(results.isEmpty());
335     }
336
337     @Test
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")));
343     }
344
345     @Test
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"));
355     }
356
357     @Test
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"));
362     }
363
364     @Test
365     public void getParentRulesNoneInVersionTest() {
366         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v11"));
367         assertTrue(results.isEmpty());
368     }
369
370     @Test
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")));
376     }
377
378     @Test
379     public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
380         Multimap<String, EdgeRule> res = edgeIngestor.getAllCurrentRules();
381         assertTrue(res.size() == 24);
382     }
383
384     @Test
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"));
392
393         thrown.expect(EdgeRuleNotFoundException.class);
394         thrown.expectMessage("No rules found for version v9.");
395         edgeIngestor.getAllRules(new SchemaVersion("v9"));
396     }
397 }