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