ab83e1969626a547482db999b30dead4ead02c24
[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.Ignore;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.onap.aai.restclient.MockProvider;
34 import org.onap.aai.restclient.MockRestClient;
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.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 import com.google.common.collect.Multimap;
49
50 @RunWith(SpringJUnit4ClassRunner.class)
51 @ContextConfiguration(classes = {MockProvider.class, EdgesConfiguration.class})
52 @TestPropertySource(properties = { "schema.ingest.file = src/test/resources/forWiringTests/schema-ingest-wiring-test.properties" })
53
54 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
55 @SpringBootTest
56 public class EdgeIngestorTest {
57     @Autowired
58     EdgeIngestor edgeIngestor;
59
60     @Rule
61     public ExpectedException thrown = ExpectedException.none();
62
63     @Test
64     public void getRulesTest1() throws EdgeRuleNotFoundException {
65         EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
66         Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
67         System.out.println(results.size());
68         for (String key : results.keySet()) {
69             System.out.println(key);
70         }
71         assertTrue(results.size() == 5);
72         assertTrue(results.containsKey("bar|foo"));
73
74         assertTrue(2 == results.get("bar|foo").size());
75         boolean seenLabel1 = false;
76         boolean seenLabel2 = false;
77         for(EdgeRule r : results.get("bar|foo")) {
78             if ("eats".equals(r.getLabel())) {
79                 seenLabel1 = true;
80             }
81             if ("eatz".equals(r.getLabel())) {
82                 seenLabel2 = true;
83             }
84         }
85         assertTrue(seenLabel1 && seenLabel2);
86
87         assertTrue(results.containsKey("baz|foo"));
88         assertTrue(results.containsKey("foo|quux"));
89         assertTrue(results.containsKey("dog|foo"));
90     }
91
92     @Test
93     public void getRulesTest2() throws EdgeRuleNotFoundException {
94         EdgeRuleQuery q = new EdgeRuleQuery.Builder("dog", "puppy").build();
95         Multimap<String, EdgeRule> results = edgeIngestor.getRules(q);
96         assertTrue(results.size() == 1);
97         assertTrue(results.containsKey("dog|puppy"));
98         Collection<EdgeRule> cr = results.get("dog|puppy");
99         for (EdgeRule r : cr) {
100             assertTrue("dog".equals(r.getFrom()));
101             assertTrue("puppy".equals(r.getTo()));
102             assertTrue("caresFor".equals(r.getLabel()));
103             assertTrue(Direction.OUT.equals(r.getDirection()));
104             assertTrue("One2Many".equalsIgnoreCase(r.getMultiplicityRule().toString()));
105             assertTrue("NONE".equals(r.getContains()));
106             assertTrue("OUT".equals(r.getDeleteOtherV()));
107             assertTrue("NONE".equals(r.getPreventDelete()));
108             assertTrue(r.isDefault());
109         }
110     }
111
112     @Test
113     public void getRulesFlippedTypesTest() throws EdgeRuleNotFoundException {
114         EdgeRuleQuery q = 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
205     @Test
206     public void getRuleFlippedTypesTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
207         EdgeRuleQuery q = new EdgeRuleQuery.Builder("notation", "parent").build();
208         EdgeRule result = edgeIngestor.getRule(q);
209         assertTrue("parent".equals(result.getFrom()));
210         assertTrue("notation".equals(result.getTo()));
211         assertTrue("has".equals(result.getLabel()));
212         //direction flipped to match input order per old EdgeRules.java API
213         assertTrue(Direction.IN.equals(result.getDirection()));
214         assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
215         assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
216         assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
217         assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
218         assertTrue("parent contains notation".equals(result.getDescription()));
219     }
220
221     @Test
222     public void getRuleWithDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
223
224         EdgeRuleQuery q = 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").label("org.onap.relationships.inventory.Source").version(new SchemaVersion("v11")).build();
233         EdgeRule res = edgeIngestor.getRule(q);
234         assertFalse(res.isDefault());
235         assertTrue("org.onap.relationships.inventory.Source".equals(res.getLabel()));
236     }
237
238     @Test
239     public void getRuleNoneFoundTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
240         thrown.expect(EdgeRuleNotFoundException.class);
241         thrown.expectMessage("No rule found for");
242         EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","nonexistent").build();
243         edgeIngestor.getRule(q);
244     }
245
246     @Test
247     public void getRuleTooManyPairsTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
248         thrown.expect(AmbiguousRuleChoiceException.class);
249         thrown.expectMessage("No way to select single rule from these pairs:");
250         EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
251         edgeIngestor.getRule(q);
252     }
253
254     @Test
255     public void getRuleAmbiguousDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
256         thrown.expect(AmbiguousRuleChoiceException.class);
257         thrown.expectMessage("Multiple defaults found.");
258         EdgeRuleQuery q = new EdgeRuleQuery.Builder("seed","plant").version(new SchemaVersion("v11")).build();
259         edgeIngestor.getRule(q);
260     }
261
262     @Test
263     public void getRuleNoDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
264         thrown.expect(AmbiguousRuleChoiceException.class);
265         thrown.expectMessage("No default found.");
266         EdgeRuleQuery q = new EdgeRuleQuery.Builder("apple", "orange").version(new SchemaVersion("v11")).build();
267         edgeIngestor.getRule(q);
268     }
269
270     @Test
271     public void hasRuleTest() {
272         assertTrue(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v11")).build()));
273         assertFalse(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").version(new SchemaVersion("v10")).build()));
274         assertTrue(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
275 //        assertFalse(edgeIngestor.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
276     }
277
278     @Test
279     public void getCousinRulesTest() {
280         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("dog");
281         assertTrue(results.size() == 2);
282         assertTrue(results.containsKey("dog|puppy"));
283         assertTrue(results.containsKey("dog|foo"));
284     }
285
286     @Test
287     public void getCousinRulesWithVersionTest() {
288         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v10"));
289         assertTrue(results.size() == 2);
290         assertTrue(results.containsKey("bar|foo"));
291         assertTrue(results.get("bar|foo").size() == 2);
292     }
293
294     @Test
295     public void getCousinsNoneInVersionTest() {
296         Multimap<String, EdgeRule> results = edgeIngestor.getCousinRules("foo", new SchemaVersion("v11"));
297         assertTrue(results.isEmpty());
298     }
299
300     @Test
301     public void hasCousinTest() {
302         assertTrue(edgeIngestor.hasCousinRule("foo"));
303         assertTrue(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v10")));
304         assertFalse(edgeIngestor.hasCousinRule("parent"));
305         assertFalse(edgeIngestor.hasCousinRule("foo", new SchemaVersion("v11")));
306     }
307
308     @Test
309     public void getChildRulesTest() {
310         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("parent");
311         assertTrue(results.size() == 6);
312         assertTrue(results.containsKey("notation|parent"));
313         assertTrue(results.containsKey("not-notation|parent"));
314         assertTrue(results.containsKey("out-out|parent"));
315         assertTrue(results.containsKey("in-in|parent"));
316         assertTrue(results.containsKey("in-out|parent"));
317         assertTrue(results.containsKey("out-in|parent"));
318     }
319
320     @Test
321     public void getChildRulesWithVersionTest() {
322         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v10"));
323         assertTrue(results.size() == 2);
324         assertTrue(results.containsKey("baz|foo"));
325         assertTrue(results.containsKey("foo|quux"));
326     }
327
328     @Test
329     public void getChildRulesNoneInVersionTest() {
330         Multimap<String, EdgeRule> results = edgeIngestor.getChildRules("foo", new SchemaVersion("v11"));
331         assertTrue(results.isEmpty());
332     }
333
334     @Test
335     public void hasChildTest() {
336         assertTrue(edgeIngestor.hasChildRule("foo"));
337         assertTrue(edgeIngestor.hasChildRule("foo", new SchemaVersion("v10")));
338         assertFalse(edgeIngestor.hasChildRule("puppy"));
339         assertFalse(edgeIngestor.hasChildRule("foo", new SchemaVersion("v11")));
340     }
341
342     @Test
343     public void getParentRulesTest() {
344         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("parent");
345         assertTrue(results.size() == 6);
346         assertTrue(results.containsKey("grandparent1|parent"));
347         assertTrue(results.containsKey("grandparent2|parent"));
348         assertTrue(results.containsKey("grandparent3|parent"));
349         assertTrue(results.containsKey("grandparent4|parent"));
350         assertTrue(results.containsKey("grandparent5|parent"));
351         assertTrue(results.containsKey("grandparent6|parent"));
352     }
353
354     @Test
355     public void getParentRulesWithVersionTest() {
356         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v10"));
357         assertTrue(results.size() == 1);
358         assertTrue(results.containsKey("baz|foo"));
359     }
360
361     @Test
362     public void getParentRulesNoneInVersionTest() {
363         Multimap<String, EdgeRule> results = edgeIngestor.getParentRules("baz", new SchemaVersion("v11"));
364         assertTrue(results.isEmpty());
365     }
366
367     @Test
368     public void hasParentTest() {
369         assertTrue(edgeIngestor.hasParentRule("parent"));
370         assertTrue(edgeIngestor.hasParentRule("quux", new SchemaVersion("v10")));
371         assertFalse(edgeIngestor.hasParentRule("puppy"));
372         assertFalse(edgeIngestor.hasParentRule("foo", new SchemaVersion("v11")));
373     }
374
375     @Test
376     public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
377         Multimap<String, EdgeRule> res = edgeIngestor.getAllCurrentRules();
378         assertTrue(res.size() == 24);
379     }
380
381     @Test
382     public void getAllRulesTest() throws EdgeRuleNotFoundException {
383         Multimap<String, EdgeRule> res = edgeIngestor.getAllRules(new SchemaVersion("v10"));
384         assertTrue(res.size() == 4);
385         assertTrue(res.containsKey("bar|foo"));
386         assertTrue(res.get("bar|foo").size() == 2);
387         assertTrue(res.containsKey("baz|foo"));
388         assertTrue(res.containsKey("foo|quux"));
389
390         thrown.expect(EdgeRuleNotFoundException.class);
391         thrown.expectMessage("No rules found for version v9.");
392         edgeIngestor.getAllRules(new SchemaVersion("v9"));
393     }
394 }