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