2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.schema;
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.io.filefilter.DirectoryFileFilter;
24 import org.apache.commons.io.filefilter.RegexFileFilter;
25 import org.json.simple.JSONArray;
26 import org.json.simple.JSONObject;
27 import org.json.simple.parser.JSONParser;
28 import org.json.simple.parser.ParseException;
29 import org.junit.Test;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
37 import java.util.stream.Collectors;
39 import static org.junit.Assert.fail;
41 public class ValidateEdgeRulesTest {
43 private static final String DBEDGERULES_RULES = "rules";
44 private static final String DBEDGERULES_FROM = "from";
45 private static final String DBEDGERULES_TO = "to";
46 private static final String DBEDGERULES_DIRECTION = "direction";
47 private static final String DBEDGERULES_LABEL = "label";
48 private static final String DBEDGERULES_CONTAINS_OTHER_V = "contains-other-v";
49 private static final String DBEDGERULES_OUT = "OUT";
50 private static final String DBEDGERULES_IN = "IN";
53 public void testOnlyOneDefaultPerEdgeRuleBetweenTwoNodetypes() throws IOException, ParseException {
54 Path currentRelativePath = Paths.get("../aai-schema/src/main/resources/").toAbsolutePath();
55 List<File> subDirs = Arrays.asList(currentRelativePath.toFile().listFiles(File::isDirectory));
56 List<String> multipleDefaultsPerList = new ArrayList<>();
57 for (File subDir : subDirs) {
59 String edgeRule = subDir.getAbsolutePath() + "/dbedgerules";
60 File[] edgeRules = new File(edgeRule).listFiles(File::isDirectory);
61 List<String> dbEdgeRulesList = Arrays.stream(edgeRules).map(File::getAbsolutePath).collect(Collectors.toList());
64 List<File> dbEdgeRulesFileList = new ArrayList<>();
66 dbEdgeRulesList.forEach(s ->
67 FileUtils.listFiles(new File(s), new RegexFileFilter(".*\\.json"), DirectoryFileFilter.DIRECTORY)
69 .filter(file -> file.getAbsolutePath().contains("DbEdgeRules"))
70 .forEach(dbEdgeRulesFileList::add));
72 JSONParser jsonParser = new JSONParser();
74 for (File file : dbEdgeRulesFileList) {
75 FileReader reader = new FileReader(file);
76 // Map the dbEdgeRules json file into a HashMap for reference
77 Map<String, Integer> dbEdgeRules = new HashMap<>();
78 // Read JSON file. Expecting JSON file to read an object with a JSONArray names "rules"
79 JSONObject jsonObj = (JSONObject) jsonParser.parse(reader);
80 JSONArray rules = (JSONArray) jsonObj.get(DBEDGERULES_RULES);
81 for (int i = 0; i < rules.size(); i++) {
82 JSONObject rule = (JSONObject) rules.get(i);
83 String fromNode = rule.get(DBEDGERULES_FROM).toString();
84 String toNode = rule.get(DBEDGERULES_TO).toString();
85 String direction = rule.get(DBEDGERULES_DIRECTION).toString();
86 String label = rule.get(DBEDGERULES_LABEL).toString();
87 String containsOtherV = rule.get(DBEDGERULES_CONTAINS_OTHER_V).toString();
88 String isDefault = (rule.get("default") != null) ? rule.get("default").toString() : "false";
90 // special case - cvlan-tag should be replaced with cvlan-tag-entry
91 if (fromNode.equals("cvlan-tag"))
92 fromNode = "cvlan-tag-entry";
93 if (toNode.equals("cvlan-tag"))
94 toNode = "cvlan-tag-entry";
95 if (containsOtherV.equals("!${direction}")) {
96 if (direction.equals(DBEDGERULES_IN)) {
97 direction = DBEDGERULES_OUT;
98 } else if (direction.equals(DBEDGERULES_OUT)) {
99 direction = DBEDGERULES_IN;
102 dbEdgeRulesMapPut(dbEdgeRules, fromNode, toNode, direction, label, isDefault);
105 for(Map.Entry<String, Integer> entry : dbEdgeRules.entrySet()){
106 String key = entry.getKey();
107 if(entry.getValue() > 1){
108 multipleDefaultsPerList.add("\n" + file.getAbsoluteFile() + " " + key + " count: " + entry.getValue());
115 if(!multipleDefaultsPerList.isEmpty()){
116 fail(multipleDefaultsPerList.stream().collect(Collectors.joining()));
121 * Creating a hashmap to map what child nodes are associated to which parent nodes
122 * according to the dbEdgeRules json files. A HashMap was chosen for the value of the map for O(1) lookup time.
123 * @param from this variable will act as the key or value depending on the direction
124 * @param to this variable will act as the key or value depending on the direction
125 * @param direction dictates the direction of which vertex is dependent on which
126 * @return The map returned will act as a dictionary to keep track of the parent nodes. Value of the map is a hashmap to help handle collision of multiple children to one parent
128 private Map<String, Integer> dbEdgeRulesMapPut(Map<String, Integer> dbEdgeRules, String from, String to, String direction, String label, String isDefault) {
129 if (isStringEmpty(from) || isStringEmpty(to) || isStringEmpty(direction))
135 if(from.compareTo(to) > 0){
141 String edgeInfo = String.format("%s%s%s[%s]%s", from, ((direction.equals("OUT")) ? "->": "<-"), to, label, isDefault);
143 if ("false".equals(isDefault)) {
147 if(dbEdgeRules.containsKey(edgeInfo)){
148 dbEdgeRules.put(edgeInfo, dbEdgeRules.get(edgeInfo) + 1);
150 dbEdgeRules.put(edgeInfo, 1);
156 private boolean isStringEmpty(String s) {
157 return (s == null || s.isEmpty()) ? true : false;