[AAI] Fix doc config files
[aai/aai-common.git] / aai-schema-ingest / src / test / java / org / onap / aai / edges / EdgeIngestorLocalTest.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.SchemaLocationsBean;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.onap.aai.testutils.TestUtilConfigTranslator;
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 = {EdgesConfiguration.class, TestUtilConfigTranslator.class})
52 @TestPropertySource(
53         properties = {
54                 "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test-local.properties"})
55
56 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
57 // @TestPropertySource(locations = "/schema-service-rest.properties" )
58 @SpringBootTest
59 public class EdgeIngestorLocalTest {
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         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("l-interface").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
186     @Test
187     public void getRuleFlippedTypesTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
188         EdgeRuleQuery q = new EdgeRuleQuery.Builder("notation", "parent").build();
189         EdgeRule result = edgeIngestor.getRule(q);
190         assertTrue("parent".equals(result.getFrom()));
191         assertTrue("notation".equals(result.getTo()));
192         assertTrue("has".equals(result.getLabel()));
193         // direction flipped to match input order per old EdgeRules.java API
194         assertTrue(Direction.IN.equals(result.getDirection()));
195         assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
196         assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
197         assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
198         assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
199         assertTrue("parent contains notation".equals(result.getDescription()));
200     }
201
202     // @Test
203     // public void getRuleWithDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
204     //
205     // EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","logical-link").version(new
206     // SchemaVersion("v11")).build();
207     // EdgeRule res = edgeIngestor.getRule(q);
208     // assertTrue(res.isDefault());
209     // assertTrue("tosca.relationships.network.LinksTo".equals(res.getLabel()));
210     // }
211     //
212     // @Test
213     // public void getRuleWithNonDefault() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
214     // EdgeRuleQuery q = new
215     // EdgeRuleQuery.Builder("l-interface","logical-link").label("org.onap.relationships.inventory.Source").version(new
216     // SchemaVersion("v11")).build();
217     // EdgeRule res = edgeIngestor.getRule(q);
218     // assertFalse(res.isDefault());
219     // assertTrue("org.onap.relationships.inventory.Source".equals(res.getLabel()));
220     // }
221
222     @Test
223     public void getRuleNoneFoundTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
224         thrown.expect(EdgeRuleNotFoundException.class);
225         thrown.expectMessage("No rule found for");
226         EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface", "nonexistent").build();
227         edgeIngestor.getRule(q);
228     }
229
230     // @Test
231     // public void getRuleTooManyPairsTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
232     // thrown.expect(AmbiguousRuleChoiceException.class);
233     // thrown.expectMessage("No way to select single rule from these pairs:");
234     // EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
235     // edgeIngestor.getRule(q);
236     // }
237
238     @Test
239     public void getRuleAmbiguousDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
240         thrown.expect(AmbiguousRuleChoiceException.class);
241         thrown.expectMessage("Multiple defaults found.");
242         EdgeRuleQuery q = new EdgeRuleQuery.Builder("seed", "plant").version(new SchemaVersion("v11")).build();
243         edgeIngestor.getRule(q);
244     }
245
246     @Test
247     public void getRuleNoDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
248         thrown.expect(AmbiguousRuleChoiceException.class);
249         thrown.expectMessage("No default found.");
250         EdgeRuleQuery q = new EdgeRuleQuery.Builder("apple", "orange").version(new SchemaVersion("v11")).build();
251         edgeIngestor.getRule(q);
252     }
253
254     // @Test
255     // public void hasRuleTest() {
256     // assertTrue(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").version(new
257     // SchemaVersion("v11")).build()));
258     // assertFalse(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
259     // }
260     //
261     // @Test
262     // public void getCousinRulesTest() {
263     // Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("dog");
264     // assertTrue(results.size() == 2);
265     // assertTrue(results.containsKey("dog|puppy"));
266     // assertTrue(results.containsKey("dog|foo"));
267     // }
268
269     @Test
270     public void getCousinRulesWithVersionTest() {
271         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v10"));
272         assertTrue(results.size() == 2);
273         assertTrue(results.containsKey("bar|foo"));
274         assertTrue(results.get("bar|foo").size() == 2);
275     }
276
277     @Test
278     public void getCousinsNoneInVersionTest() {
279         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v11"));
280         assertTrue(results.isEmpty());
281     }
282
283     // @Test
284     // public void hasCousinTest() {
285     // assertTrue(edgeIngestor.hasCousinRule("foo"));
286     // assertTrue(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v10")));
287     // assertFalse(edgeIngestor.hasCousinRule("parent"));
288     // assertFalse(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v11")));
289     // }
290
291     @Test
292     public void getChildRulesTest() {
293         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("parent");
294         assertTrue(results.size() == 6);
295         assertTrue(results.containsKey("notation|parent"));
296         assertTrue(results.containsKey("not-notation|parent"));
297         assertTrue(results.containsKey("out-out|parent"));
298         assertTrue(results.containsKey("in-in|parent"));
299         assertTrue(results.containsKey("in-out|parent"));
300         assertTrue(results.containsKey("out-in|parent"));
301     }
302
303     @Test
304     public void getChildRulesWithVersionTest() {
305         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v10"));
306         assertTrue(results.size() == 2);
307         assertTrue(results.containsKey("baz|foo"));
308         assertTrue(results.containsKey("foo|quux"));
309     }
310
311     @Test
312     public void getChildRulesNoneInVersionTest() {
313         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v11"));
314         assertTrue(results.isEmpty());
315     }
316
317     // @Test
318     // public void hasChildTest() {
319     // assertTrue(edgeIngestor.hasChildRule("foo"));
320     // assertTrue(edgeIngestor.hasChildRule("foo", new SchemaVersion("v10")));
321     // assertFalse(edgeIngestor.hasChildRule("puppy"));
322     // assertFalse(edgeIngestor.hasChildRule("foo", new SchemaVersion("v11")));
323     // }
324
325     @Test
326     public void getParentRulesTest() {
327         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("parent");
328         assertTrue(results.size() == 6);
329         assertTrue(results.containsKey("grandparent1|parent"));
330         assertTrue(results.containsKey("grandparent2|parent"));
331         assertTrue(results.containsKey("grandparent3|parent"));
332         assertTrue(results.containsKey("grandparent4|parent"));
333         assertTrue(results.containsKey("grandparent5|parent"));
334         assertTrue(results.containsKey("grandparent6|parent"));
335     }
336
337     @Test
338     public void getParentRulesWithVersionTest() {
339         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v10"));
340         assertTrue(results.size() == 1);
341         assertTrue(results.containsKey("baz|foo"));
342     }
343
344     @Test
345     public void getParentRulesNoneInVersionTest() {
346         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v11"));
347         assertTrue(results.isEmpty());
348     }
349
350     @Test
351     public void hasParentTest() {
352         assertTrue(edgeIngestor.hasParentRule("parent"));
353         assertTrue(edgeIngestor.hasParentRule("quux", new SchemaVersion("v10")));
354         assertFalse(edgeIngestor.hasParentRule("puppy"));
355         assertFalse(edgeIngestor.hasParentRule("foo", new SchemaVersion("v11")));
356     }
357
358     // @Test
359     // public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
360     // Multimap<String, EdgeRule> res = edgeIngestor.getAllCurrentRules();
361     // assertTrue(res.size() == 18);
362     // }
363
364     @Test
365     public void getAllRulesTest() throws EdgeRuleNotFoundException {
366         Multimap<String, EdgeRule> res = edgeIngestor.getAllRules(new SchemaVersion("v10"));
367         assertTrue(res.size() == 4);
368         assertTrue(res.containsKey("bar|foo"));
369         assertTrue(res.get("bar|foo").size() == 2);
370         assertTrue(res.containsKey("baz|foo"));
371         assertTrue(res.containsKey("foo|quux"));
372
373         thrown.expect(EdgeRuleNotFoundException.class);
374         thrown.expectMessage("No rules found for version v9.");
375         edgeIngestor.getAllRules(new SchemaVersion("v9"));
376     }
377 }