Update aai-schema-ingest dependency in the schema-service
[aai/schema-service.git] / aai-schema-gen / src / test / java / org / onap / aai / schemagen / genxsd / EdgeDescriptionTest.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
21 package org.onap.aai.schemagen.genxsd;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.common.collect.Multimap;
28
29 import java.io.BufferedWriter;
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.SortedSet;
36 import java.util.TreeSet;
37
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.onap.aai.edges.EdgeIngestor;
44 import org.onap.aai.edges.EdgeRule;
45 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
46 import org.onap.aai.schemagen.testutils.TestUtilConfigTranslatorforEdges;
47 import org.onap.aai.setup.ConfigTranslator;
48 import org.onap.aai.setup.SchemaConfigVersions;
49 import org.onap.aai.setup.SchemaLocationsBean;
50 import org.onap.aai.setup.SchemaVersion;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.test.context.ContextConfiguration;
53 import org.springframework.test.context.TestPropertySource;
54 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
55
56 @RunWith(SpringJUnit4ClassRunner.class)
57 @ContextConfiguration(
58     classes = {SchemaConfigVersions.class, SchemaLocationsBean.class,
59         TestUtilConfigTranslatorforEdges.class, EdgeIngestor.class})
60 @TestPropertySource(
61     properties = {
62         "schemaIngestPropLoc = src/test/resources/schemaIngest/schemaIngestTest.properties"})
63
64 public class EdgeDescriptionTest {
65     private static final String EDGEFILENAME =
66         "src/test/resources/dbedgerules/EdgeDescriptionRules_test.json";
67     @Autowired
68     ConfigTranslator ct;
69     @Autowired
70     EdgeIngestor edgeIngestor;
71     String nodeName = "availability-zone";
72     String toNode = "complex";
73     SchemaVersion v10 = new SchemaVersion("v10");
74
75     @BeforeClass
76     public static void setUpBeforeClass() throws Exception {
77         String json = "{" + "  \"rules\": [" + "    {" + "      \"from\": \"availability-zone\","
78             + "      \"to\": \"complex\","
79             + "      \"label\": \"org.onap.relationships.inventory.LocatedIn\","
80             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2ONE\","
81             + "      \"contains-other-v\": \"NONE\"," + "      \"delete-other-v\": \"NONE\","
82             + "      \"SVC-INFRA\": \"NONE\"," + "      \"prevent-delete\": \"!${direction}\","
83             + "      \"default\": \"true\"," + "      \"description\":\"this description\""
84             + "    }," + "    {" + "      \"from\": \"availability-zone\","
85             + "      \"to\": \"service-capability\","
86             + "      \"label\": \"org.onap.relationships.inventory.AppliesTo\","
87             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2MANY\","
88             + "      \"contains-other-v\": \"NONE\"," + "      \"delete-other-v\": \"NONE\","
89             + "      \"SVC-INFRA\": \"NONE\"," + "      \"prevent-delete\": \"!${direction}\","
90             + "      \"default\": \"true\"," + "      \"description\":\"\"" + "    }," + "    {"
91             + "      \"from\": \"availability-zone\"," + "      \"to\": \"cloud-region\","
92             + "      \"label\": \"org.onap.relationships.inventory.BelongsTo\","
93             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2ONE\","
94             + "      \"contains-other-v\": \"!${direction}\","
95             + "      \"delete-other-v\": \"!${direction}\"," + "      \"SVC-INFRA\": \"NONE\","
96             + "      \"prevent-delete\": \"NONE\"," + "      \"default\": \"true\","
97             + "      \"description\":\"\"" + "    }," + "    {" + "      \"from\": \"ctag-pool\","
98             + "      \"to\": \"availability-zone\","
99             + "      \"label\": \"org.onap.relationships.inventory.AppliesTo\","
100             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2MANY\","
101             + "      \"contains-other-v\": \"${direction}\","
102             + "      \"delete-other-v\": \"NONE\"," + "      \"SVC-INFRA\": \"NONE\","
103             + "      \"prevent-delete\": \"!${direction}\"," + "      \"default\": \"true\","
104             + "      \"description\":\"\"" + "    }," + "    {" + "      \"from\": \"dvs-switch\","
105             + "      \"to\": \"availability-zone\","
106             + "      \"label\": \"org.onap.relationships.inventory.AppliesTo\","
107             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2MANY\","
108             + "      \"contains-other-v\": \"NONE\"," + "      \"delete-other-v\": \"NONE\","
109             + "      \"SVC-INFRA\": \"NONE\"," + "      \"prevent-delete\": \"!${direction}\","
110             + "      \"default\": \"true\"," + "      \"description\":\"\"" + "    }," + "    {"
111             + "      \"from\": \"generic-vnf\"," + "      \"to\": \"availability-zone\","
112             + "      \"label\": \"org.onap.relationships.inventory.Uses\","
113             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2MANY\","
114             + "      \"contains-other-v\": \"NONE\"," + "      \"delete-other-v\": \"NONE\","
115             + "      \"SVC-INFRA\": \"${direction}\","
116             + "      \"prevent-delete\": \"!${direction}\"," + "      \"default\": \"true\","
117             + "      \"description\":\"\"" + "    }," + "    {" + "      \"from\": \"pserver\","
118             + "      \"to\": \"availability-zone\","
119             + "      \"label\": \"org.onap.relationships.inventory.MemberOf\","
120             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2ONE\","
121             + "      \"contains-other-v\": \"NONE\"," + "      \"delete-other-v\": \"NONE\","
122             + "      \"SVC-INFRA\": \"${direction}\","
123             + "      \"prevent-delete\": \"!${direction}\"," + "      \"default\": \"true\","
124             + "      \"description\":\"\"" + "    }," + "    {" + "      \"from\": \"vce\","
125             + "      \"to\": \"availability-zone\","
126             + "      \"label\": \"org.onap.relationships.inventory.Uses\","
127             + "      \"direction\": \"OUT\"," + "      \"multiplicity\": \"MANY2MANY\","
128             + "      \"contains-other-v\": \"NONE\"," + "      \"delete-other-v\": \"NONE\","
129             + "      \"SVC-INFRA\": \"NONE\"," + "      \"prevent-delete\": \"!${direction}\","
130             + "      \"default\": \"true\"," + "      \"description\":\"\"" + "    }," + "  ]}";
131         BufferedWriter bw = new BufferedWriter(new FileWriter(EDGEFILENAME));
132         bw.write(json);
133         bw.close();
134     }
135
136     @AfterClass
137     public static void tearDownAfterClass() throws Exception {
138         File edges = new File(EDGEFILENAME);
139         edges.delete();
140     }
141
142     @Before
143     public void setUp() throws Exception {
144
145     }
146
147     @Test
148     public void test() {
149
150         Map<SchemaVersion, List<String>> edges = ct.getEdgeFiles();
151         assertTrue(edges.containsKey(v10));
152         assertTrue(1 == edges.get(v10).size());
153         assertTrue("src/test/resources/dbedgerules/DbEdgeBusinessRules_test.json"
154             .equals(edges.get(v10).get(0)));
155     }
156
157     @Test
158     public void testGetDeleteOtherV() throws EdgeRuleNotFoundException {
159         String target = "availability-zone" + "|" + toNode + "-" + "NONE";
160         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
161         SortedSet<String> ss = new TreeSet<String>(results.keySet());
162         for (String key : ss) {
163             results.get(key).stream()
164                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
165                     EdgeDescription ed = new EdgeDescription(i);
166                     assertThat(ed.getRuleKey() + "-" + ed.getDeleteOtherV(), is(target));
167                 });
168         }
169     }
170
171     @Test
172     public void testGetPreventDelete() throws EdgeRuleNotFoundException {
173         String target = "availability-zone" + "|" + toNode + "-" + "IN";
174         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
175         SortedSet<String> ss = new TreeSet<String>(results.keySet());
176         for (String key : ss) {
177             results.get(key).stream()
178                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
179                     EdgeDescription ed = new EdgeDescription(i);
180                     assertThat(ed.getRuleKey() + "-" + ed.getPreventDelete(), is(target));
181                 });
182         }
183     }
184
185     @Test
186     public void testGetAlsoDeleteFootnote() throws EdgeRuleNotFoundException {
187         // String toNode="cloud-region";
188         // String target = "availability-zone"+"|"+toNode+"-"+"(4)";
189         List<String> notedTypes = Arrays.asList("cloud-region", "ctag-pool");
190         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
191         SortedSet<String> ss = new TreeSet<String>(results.keySet());
192         for (String key : ss) {
193             results.get(key).stream()
194                 .filter((i) -> (i.getTo().equals("availability-zone") && (!i.isPrivateEdge())))
195                 .forEach((i) -> {
196                     EdgeDescription ed = new EdgeDescription(i);
197                     String target =
198                         ed.getRuleKey() + "-" + (notedTypes.contains(ed.getTo()) ? "(4)" : "");
199                     assertThat(ed.getRuleKey() + "-" + ed.getAlsoDeleteFootnote(ed.getFrom()),
200                         is(target));
201                 });
202         }
203         /*
204          * for (EdgeDescription ed : edges) {
205          * String modResult = ed.getRuleKey() + "-" + ed.getAlsoDeleteFootnote(ed.getFrom());
206          * assertThat(modResult, is(target));
207          * ed.setDeleteOtherV("IN");
208          * target = "availability-zone"+"|"+"complex"+"-"+"(4)";
209          * modResult = ed.getRuleKey() + "-" + ed.getAlsoDeleteFootnote(ed.getFrom());
210          * assertThat(modResult, is(target));
211          * target = "availability-zone"+"|"+"complex"+"-"+"(1)";
212          * modResult = ed.getRuleKey() + "-" + ed.getAlsoDeleteFootnote(ed.getTo());
213          * assertThat(modResult, is(target));
214          * ed.setDeleteOtherV("OUT");
215          * target = "availability-zone"+"|"+"complex"+"-"+"(2)";
216          * modResult = ed.getRuleKey() + "-" + ed.getAlsoDeleteFootnote(ed.getFrom());
217          * assertThat(modResult, is(target));
218          * target = "availability-zone"+"|"+"complex"+"-"+"(3)";
219          * modResult = ed.getRuleKey() + "-" + ed.getAlsoDeleteFootnote(ed.getTo());
220          * assertThat(modResult, is(target));
221          * }
222          */
223     }
224
225     @Test
226     public void testGetTo() throws EdgeRuleNotFoundException {
227         String target = "availability-zone" + "|" + toNode + "-" + toNode;
228         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
229         SortedSet<String> ss = new TreeSet<String>(results.keySet());
230         for (String key : ss) {
231             results.get(key).stream()
232                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
233                     EdgeDescription ed = new EdgeDescription(i);
234                     assertThat(ed.getRuleKey() + "-" + ed.getTo(), is(target));
235                 });
236         }
237     }
238
239     @Test
240     public void testGetFrom() throws EdgeRuleNotFoundException {
241         String target = "availability-zone" + "|" + toNode + "-" + "availability-zone";
242         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
243         SortedSet<String> ss = new TreeSet<String>(results.keySet());
244         for (String key : ss) {
245             results.get(key).stream()
246                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
247                     EdgeDescription ed = new EdgeDescription(i);
248                     assertThat(ed.getRuleKey() + "-" + ed.getFrom(), is(target));
249                 });
250         }
251     }
252
253     @Test
254     public void testGetRuleKey() throws EdgeRuleNotFoundException {
255         String target = "availability-zone" + "|" + toNode;
256         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
257         SortedSet<String> ss = new TreeSet<String>(results.keySet());
258         for (String key : ss) {
259             results.get(key).stream()
260                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
261                     EdgeDescription ed = new EdgeDescription(i);
262                     assertThat(ed.getRuleKey(), is(target));
263                 });
264         }
265     }
266
267     @Test
268     public void testGetMultiplicity() throws EdgeRuleNotFoundException {
269         String target = "availability-zone" + "|" + toNode + "-" + "MANY2ONE";
270         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
271         SortedSet<String> ss = new TreeSet<String>(results.keySet());
272         for (String key : ss) {
273             results.get(key).stream()
274                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
275                     EdgeDescription ed = new EdgeDescription(i);
276                     assertThat(ed.getRuleKey() + "-" + ed.getMultiplicity(), is(target));
277                 });
278         }
279     }
280
281     @Test
282     public void testGetDirection() throws EdgeRuleNotFoundException {
283         String target = "availability-zone" + "|" + toNode + "-" + "OUT";
284         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
285         SortedSet<String> ss = new TreeSet<String>(results.keySet());
286         for (String key : ss) {
287             results.get(key).stream()
288                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
289                     EdgeDescription ed = new EdgeDescription(i);
290                     assertThat(ed.getRuleKey() + "-" + ed.getDirection(), is(target));
291                 });
292         }
293     }
294
295     @Test
296     public void testGetDescription() throws EdgeRuleNotFoundException {
297         String target = "availability-zone" + "|" + toNode + "-" + "this description";
298         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
299         SortedSet<String> ss = new TreeSet<String>(results.keySet());
300         for (String key : ss) {
301             results.get(key).stream()
302                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
303                     EdgeDescription ed = new EdgeDescription(i);
304                     assertThat(ed.getRuleKey() + "-" + ed.getDescription(), is(target));
305                 });
306         }
307     }
308
309     @Test
310     public void testGetRelationshipDescription() throws EdgeRuleNotFoundException {
311         String target = "availability-zone" + "|" + toNode + "-" + "this description";
312         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
313         SortedSet<String> ss = new TreeSet<String>(results.keySet());
314         for (String key : ss) {
315             results.get(key).stream()
316                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
317                     EdgeDescription ed = new EdgeDescription(i);
318                     assertThat(ed.getRuleKey() + "-" + ed.getDescription(), is(target));
319                 });
320         }
321     }
322
323     @Test
324     public void testGetType() throws EdgeRuleNotFoundException {
325         String toNode = "cloud-region";
326         String target = "availability-zone" + "|" + toNode + "-" + "PARENT";
327         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
328         SortedSet<String> ss = new TreeSet<String>(results.keySet());
329         for (String key : ss) {
330             results.get(key).stream()
331                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
332                     EdgeDescription ed = new EdgeDescription(i);
333                     assertThat(ed.getRuleKey() + "-" + ed.getType(), is(target));
334                 });
335         }
336     }
337
338     @Test
339     public void testGetLabel() throws EdgeRuleNotFoundException {
340         String target =
341             "availability-zone" + "|" + toNode + "-" + "org.onap.relationships.inventory.LocatedIn";
342         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
343         SortedSet<String> ss = new TreeSet<String>(results.keySet());
344         for (String key : ss) {
345             results.get(key).stream()
346                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
347                     EdgeDescription ed = new EdgeDescription(i);
348                     assertThat(ed.getRuleKey() + "-" + ed.getLabel(), is(target));
349                 });
350         }
351     }
352
353     @Test
354     public void testGetShortLabel() throws EdgeRuleNotFoundException {
355         String target = "availability-zone" + "|" + toNode + "-" + "LocatedIn";
356         Multimap<String, EdgeRule> results = edgeIngestor.getAllRules(v10);
357         SortedSet<String> ss = new TreeSet<String>(results.keySet());
358         for (String key : ss) {
359             results.get(key).stream()
360                 .filter((i) -> (i.getTo().equals(toNode) && (!i.isPrivateEdge()))).forEach((i) -> {
361                     EdgeDescription ed = new EdgeDescription(i);
362                     assertThat(ed.getRuleKey() + "-" + ed.getShortLabel(), is(target));
363                 });
364         }
365     }
366 }