[AAI] Release docker artifact 1.12.4
[aai/schema-service.git] / aai-schema / src / test / java / org / onap / aai / schema / ValidateEdgeRulesTest.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.schema;
22
23 import static org.junit.Assert.fail;
24
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.*;
31 import java.util.stream.Collectors;
32
33 import org.apache.commons.io.FileUtils;
34 import org.apache.commons.io.filefilter.DirectoryFileFilter;
35 import org.apache.commons.io.filefilter.RegexFileFilter;
36 import org.json.simple.JSONArray;
37 import org.json.simple.JSONObject;
38 import org.json.simple.parser.JSONParser;
39 import org.json.simple.parser.ParseException;
40 import org.junit.Test;
41
42 public class ValidateEdgeRulesTest {
43
44     private static final String DBEDGERULES_RULES = "rules";
45     private static final String DBEDGERULES_FROM = "from";
46     private static final String DBEDGERULES_TO = "to";
47     private static final String DBEDGERULES_DIRECTION = "direction";
48     private static final String DBEDGERULES_LABEL = "label";
49     private static final String DBEDGERULES_CONTAINS_OTHER_V = "contains-other-v";
50     private static final String DBEDGERULES_OUT = "OUT";
51     private static final String DBEDGERULES_IN = "IN";
52
53     @Test
54     public void testOnlyOneDefaultPerEdgeRuleBetweenTwoNodetypes()
55         throws IOException, ParseException {
56         Path currentRelativePath = Paths.get("../aai-schema/src/main/resources/").toAbsolutePath();
57         List<File> subDirs =
58             Arrays.asList(currentRelativePath.toFile().listFiles(File::isDirectory));
59         List<String> multipleDefaultsPerList = new ArrayList<>();
60         for (File subDir : subDirs) {
61
62             String edgeRule = subDir.getAbsolutePath() + "/dbedgerules";
63             File[] edgeRules = new File(edgeRule).listFiles(File::isDirectory);
64             List<String> dbEdgeRulesList =
65                 Arrays.stream(edgeRules).map(File::getAbsolutePath).collect(Collectors.toList());
66
67             List<File> dbEdgeRulesFileList = new ArrayList<>();
68
69             dbEdgeRulesList.forEach(s -> FileUtils
70                 .listFiles(new File(s), new RegexFileFilter(".*\\.json"),
71                     DirectoryFileFilter.DIRECTORY)
72                 .stream().filter(file -> file.getAbsolutePath().contains("DbEdgeRules"))
73                 .forEach(dbEdgeRulesFileList::add));
74
75             JSONParser jsonParser = new JSONParser();
76
77             for (File file : dbEdgeRulesFileList) {
78                 FileReader reader = new FileReader(file);
79                 // Map the dbEdgeRules json file into a HashMap for reference
80                 Map<String, Integer> dbEdgeRules = new HashMap<>();
81                 // Read JSON file. Expecting JSON file to read an object with a JSONArray names
82                 // "rules"
83                 JSONObject jsonObj = (JSONObject) jsonParser.parse(reader);
84                 JSONArray rules = (JSONArray) jsonObj.get(DBEDGERULES_RULES);
85                 for (int i = 0; i < rules.size(); i++) {
86                     JSONObject rule = (JSONObject) rules.get(i);
87                     String fromNode = rule.get(DBEDGERULES_FROM).toString();
88                     String toNode = rule.get(DBEDGERULES_TO).toString();
89                     String direction = rule.get(DBEDGERULES_DIRECTION).toString();
90                     String label = rule.get(DBEDGERULES_LABEL).toString();
91                     String containsOtherV = rule.get(DBEDGERULES_CONTAINS_OTHER_V).toString();
92                     String isDefault =
93                         (rule.get("default") != null) ? rule.get("default").toString() : "false";
94
95                     // special case - cvlan-tag should be replaced with cvlan-tag-entry
96                     if (fromNode.equals("cvlan-tag"))
97                         fromNode = "cvlan-tag-entry";
98                     if (toNode.equals("cvlan-tag"))
99                         toNode = "cvlan-tag-entry";
100                     if (containsOtherV.equals("!${direction}")) {
101                         if (direction.equals(DBEDGERULES_IN)) {
102                             direction = DBEDGERULES_OUT;
103                         } else if (direction.equals(DBEDGERULES_OUT)) {
104                             direction = DBEDGERULES_IN;
105                         }
106                     }
107                     dbEdgeRulesMapPut(dbEdgeRules, fromNode, toNode, direction, label, isDefault);
108                 }
109
110                 for (Map.Entry<String, Integer> entry : dbEdgeRules.entrySet()) {
111                     String key = entry.getKey();
112                     if (entry.getValue() > 1) {
113                         multipleDefaultsPerList.add("\n" + file.getAbsoluteFile() + " " + key
114                             + " count: " + entry.getValue());
115                     }
116                 }
117             }
118
119         }
120
121         if (!multipleDefaultsPerList.isEmpty()) {
122             fail(multipleDefaultsPerList.stream().collect(Collectors.joining()));
123         }
124     }
125
126     /**
127      * Creating a hashmap to map what child nodes are associated to which parent nodes
128      * according to the dbEdgeRules json files. A HashMap was chosen for the value of the map for
129      * O(1) lookup time.
130      * 
131      * @param from this variable will act as the key or value depending on the direction
132      * @param to this variable will act as the key or value depending on the direction
133      * @param direction dictates the direction of which vertex is dependent on which
134      * @return The map returned will act as a dictionary to keep track of the parent nodes. Value of
135      *         the map is a hashmap to help handle collision of multiple children to one parent
136      */
137     private Map<String, Integer> dbEdgeRulesMapPut(Map<String, Integer> dbEdgeRules, String from,
138         String to, String direction, String label, String isDefault) {
139         if (isStringEmpty(from) || isStringEmpty(to) || isStringEmpty(direction))
140             return dbEdgeRules;
141
142         String temp;
143
144         if (from.compareTo(to) > 0) {
145             temp = from;
146             from = to;
147             to = temp;
148         }
149
150         String edgeInfo = String.format("%s%s%s[%s]%s", from,
151             ((direction.equals("OUT")) ? "->" : "<-"), to, label, isDefault);
152
153         if ("false".equals(isDefault)) {
154             return dbEdgeRules;
155         }
156
157         if (dbEdgeRules.containsKey(edgeInfo)) {
158             dbEdgeRules.put(edgeInfo, dbEdgeRules.get(edgeInfo) + 1);
159         } else {
160             dbEdgeRules.put(edgeInfo, 1);
161         }
162
163         return dbEdgeRules;
164     }
165
166     private boolean isStringEmpty(String s) {
167         return (s == null || s.isEmpty()) ? true : false;
168     }
169 }