Update the license for 2017-2018 license
[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-2018 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 package org.onap.aai.edges;
21
22 import static org.junit.Assert.*;
23
24 import java.util.Collection;
25
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
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.Version;
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.context.ContextConfiguration;
41 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42
43 import com.google.common.collect.Multimap;
44
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @ContextConfiguration(classes = {SchemaLocationsBean.class, TestUtilConfigTranslator.class, EdgeIngestor.class})
47 @SpringBootTest
48 public class EdgeIngestorTest {
49         @Autowired
50         EdgeIngestor ei;
51         
52         @Rule
53         public ExpectedException thrown = ExpectedException.none();
54         
55         @Test
56         public void getRulesTest1() throws EdgeRuleNotFoundException {
57                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
58                 Multimap<String, EdgeRule> results = ei.getRules(q);
59                 assertTrue(results.size() == 5);
60                 assertTrue(results.containsKey("bar|foo"));
61                 
62                 assertTrue(2 == results.get("bar|foo").size());
63                 boolean seenLabel1 = false;
64                 boolean seenLabel2 = false;
65                 for(EdgeRule r : results.get("bar|foo")) {
66                         if ("eats".equals(r.getLabel())) {
67                                 seenLabel1 = true;
68                         }
69                         if ("eatz".equals(r.getLabel())) {
70                                 seenLabel2 = true;
71                         }
72                 }
73                 assertTrue(seenLabel1 && seenLabel2);
74                 
75                 assertTrue(results.containsKey("baz|foo"));
76                 assertTrue(results.containsKey("foo|quux"));
77                 assertTrue(results.containsKey("dog|foo"));
78         }
79         
80         @Test
81         public void getRulesTest2() throws EdgeRuleNotFoundException {
82                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("puppy", "dog").build();
83                 Multimap<String, EdgeRule> results = ei.getRules(q);
84                 assertTrue(results.size() == 1);
85                 assertTrue(results.containsKey("dog|puppy"));
86                 Collection<EdgeRule> cr = results.get("dog|puppy");
87                 for (EdgeRule r : cr) {
88                         assertTrue("dog".equals(r.getFrom()));
89                         assertTrue("puppy".equals(r.getTo()));
90                         assertTrue("caresFor".equals(r.getLabel()));
91                         assertTrue(Direction.OUT.equals(r.getDirection()));
92                         assertTrue("One2Many".equalsIgnoreCase(r.getMultiplicityRule().toString()));
93                         assertTrue("NONE".equals(r.getContains()));
94                         assertTrue("OUT".equals(r.getDeleteOtherV()));
95                         assertTrue("NONE".equals(r.getPreventDelete()));
96                         assertTrue(r.isDefault());
97                 }
98         }
99         
100         @Test
101         public void getRulesTest3() throws EdgeRuleNotFoundException {
102                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface").version(Version.V11).build();
103                 Multimap<String, EdgeRule> results = ei.getRules(q);
104                 assertTrue(results.size() == 4);
105                 assertTrue(results.containsKey("lag-interface|l-interface"));
106                 assertTrue(results.containsKey("l-interface|logical-link"));
107                 assertTrue(results.get("l-interface|logical-link").size() == 3);
108         }
109         
110         @Test
111         public void getRulesNoneFound() throws EdgeRuleNotFoundException {
112                 thrown.expect(EdgeRuleNotFoundException.class);
113                 thrown.expectMessage("No rules found for");
114                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface").build();
115                 ei.getRules(q);
116         }
117         
118         @Test
119         public void getRuleSimpleTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
120                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("notation", "parent").build();
121                 EdgeRule result = ei.getRule(q);
122                 assertTrue("parent".equals(result.getFrom()));
123                 assertTrue("notation".equals(result.getTo()));
124                 assertTrue("has".equals(result.getLabel()));
125                 assertTrue(Direction.OUT.equals(result.getDirection()));
126                 assertTrue(MultiplicityRule.MANY2MANY.equals(result.getMultiplicityRule()));
127                 assertTrue(AAIDirection.OUT.toString().equals(result.getContains()));
128                 assertTrue(AAIDirection.NONE.toString().equals(result.getDeleteOtherV()));
129                 assertTrue(AAIDirection.NONE.toString().equals(result.getPreventDelete()));
130                 assertTrue("parent contains notation".equals(result.getDescription()));
131         }
132         
133         @Test
134         public void getRuleWithDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
135                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","logical-link").version(Version.V11).build(); 
136                 EdgeRule res = ei.getRule(q);
137                 assertTrue(res.isDefault());
138                 assertTrue("tosca.relationships.network.LinksTo".equals(res.getLabel()));
139         }
140         
141         @Test
142         public void getRuleWithNonDefault() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
143                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","logical-link").label("org.onap.relationships.inventory.Source").version(Version.V11).build();
144                 EdgeRule res = ei.getRule(q);
145                 assertFalse(res.isDefault());
146                 assertTrue("org.onap.relationships.inventory.Source".equals(res.getLabel()));
147         }
148         
149         @Test
150         public void getRuleNoneFoundTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
151                 thrown.expect(EdgeRuleNotFoundException.class);
152                 thrown.expectMessage("No rule found for");
153                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("l-interface","nonexistent").build();
154                 ei.getRule(q);
155         }
156         
157         @Test
158         public void getRuleTooManyPairsTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
159                 thrown.expect(AmbiguousRuleChoiceException.class);
160                 thrown.expectMessage("No way to select single rule from these pairs:");
161                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("foo").build();
162                 ei.getRule(q);
163         }
164         
165         @Test
166         public void getRuleAmbiguousDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
167                 thrown.expect(AmbiguousRuleChoiceException.class);
168                 thrown.expectMessage("Multiple defaults found.");
169                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("seed","plant").version(Version.V11).build();
170                 ei.getRule(q);
171         }
172         
173         @Test
174         public void getRuleNoDefaultTest() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
175                 thrown.expect(AmbiguousRuleChoiceException.class);
176                 thrown.expectMessage("No default found.");
177                 EdgeRuleQuery q = new EdgeRuleQuery.Builder("apple", "orange").version(Version.V11).build();
178                 ei.getRule(q);
179         }
180         
181         @Test
182         public void hasRuleTest() {
183                 assertTrue(ei.hasRule(new EdgeRuleQuery.Builder("l-interface").version(Version.V11).build()));
184                 assertFalse(ei.hasRule(new EdgeRuleQuery.Builder("l-interface").build()));
185         }
186         
187         @Test
188         public void getCousinRulesTest() {
189                 Multimap<String, EdgeRule> results = ei.getCousinRules("dog");
190                 assertTrue(results.size() == 2);
191                 assertTrue(results.containsKey("dog|puppy"));
192                 assertTrue(results.containsKey("dog|foo"));
193         }
194         
195         @Test
196         public void getCousinRulesWithVersionTest() {
197                 Multimap<String, EdgeRule> results = ei.getCousinRules("foo", Version.V10);
198                 assertTrue(results.size() == 2);
199                 assertTrue(results.containsKey("bar|foo"));
200                 assertTrue(results.get("bar|foo").size() == 2);
201         }
202         
203         @Test
204         public void getCousinsNoneInVersionTest() {
205                 Multimap<String, EdgeRule> results = ei.getCousinRules("foo", Version.V11);
206                 assertTrue(results.isEmpty());
207         }
208         
209         @Test
210         public void hasCousinTest() {
211                 assertTrue(ei.hasCousinRule("foo"));
212                 assertTrue(ei.hasCousinRule("foo", Version.V10));
213                 assertFalse(ei.hasCousinRule("parent"));
214                 assertFalse(ei.hasCousinRule("foo", Version.V11));
215         }
216
217         @Test
218         public void getChildRulesTest() {
219                 Multimap<String, EdgeRule> results = ei.getChildRules("parent");
220                 assertTrue(results.size() == 6);
221                 assertTrue(results.containsKey("notation|parent"));
222                 assertTrue(results.containsKey("not-notation|parent"));
223                 assertTrue(results.containsKey("out-out|parent"));
224                 assertTrue(results.containsKey("in-in|parent"));
225                 assertTrue(results.containsKey("in-out|parent"));
226                 assertTrue(results.containsKey("out-in|parent"));
227         }
228         
229         @Test
230         public void getChildRulesWithVersionTest() {
231                 Multimap<String, EdgeRule> results = ei.getChildRules("foo", Version.V10);
232                 assertTrue(results.size() == 2);
233                 assertTrue(results.containsKey("baz|foo"));
234                 assertTrue(results.containsKey("foo|quux"));
235         }
236         
237         @Test
238         public void getChildRulesNoneInVersionTest() {
239                 Multimap<String, EdgeRule> results = ei.getChildRules("foo", Version.V11);
240                 assertTrue(results.isEmpty());
241         }
242         
243         @Test
244         public void hasChildTest() {
245                 assertTrue(ei.hasChildRule("foo"));
246                 assertTrue(ei.hasChildRule("foo", Version.V10));
247                 assertFalse(ei.hasChildRule("puppy"));
248                 assertFalse(ei.hasChildRule("foo", Version.V11));
249         }
250         
251         @Test
252         public void getParentRulesTest() {
253                 Multimap<String, EdgeRule> results = ei.getParentRules("parent");
254                 assertTrue(results.size() == 6);
255                 assertTrue(results.containsKey("grandparent1|parent"));
256                 assertTrue(results.containsKey("grandparent2|parent"));
257                 assertTrue(results.containsKey("grandparent3|parent"));
258                 assertTrue(results.containsKey("grandparent4|parent"));
259                 assertTrue(results.containsKey("grandparent5|parent"));
260                 assertTrue(results.containsKey("grandparent6|parent"));
261         }
262         
263         @Test
264         public void getParentRulesWithVersionTest() {
265                 Multimap<String, EdgeRule> results = ei.getParentRules("baz", Version.V10);
266                 assertTrue(results.size() == 1);
267                 assertTrue(results.containsKey("baz|foo"));
268         }
269         
270         @Test
271         public void getParentRulesNoneInVersionTest() {
272                 Multimap<String, EdgeRule> results = ei.getParentRules("baz", Version.V11);
273                 assertTrue(results.isEmpty());
274         }
275         
276         @Test
277         public void hasParentTest() {
278                 assertTrue(ei.hasParentRule("parent"));
279                 assertTrue(ei.hasParentRule("quux", Version.V10));
280                 assertFalse(ei.hasParentRule("puppy"));
281                 assertFalse(ei.hasParentRule("foo", Version.V11));
282         }
283         
284         @Test
285         public void getAllCurrentRulesTest() throws EdgeRuleNotFoundException {
286                 Multimap<String, EdgeRule> res = ei.getAllCurrentRules();
287                 assertTrue(res.size() == 18);
288         }
289         
290         @Test
291         public void getAllRulesTest() throws EdgeRuleNotFoundException {
292                 Multimap<String, EdgeRule> res = ei.getAllRules(Version.V10);
293                 assertTrue(res.size() == 4);
294                 assertTrue(res.containsKey("bar|foo"));
295                 assertTrue(res.get("bar|foo").size() == 2);
296                 assertTrue(res.containsKey("baz|foo"));
297                 assertTrue(res.containsKey("foo|quux"));
298                 
299                 thrown.expect(EdgeRuleNotFoundException.class);
300                 thrown.expectMessage("No rules found for version V9.");
301                 ei.getAllRules(Version.V9);
302         }
303 }