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