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