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