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=========================================================
21 package org.onap.aai.schema;
23 import static org.junit.Assert.fail;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
31 import java.util.stream.Collectors;
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;
42 public class ValidateEdgeRulesTest {
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";
54 public void testOnlyOneDefaultPerEdgeRuleBetweenTwoNodetypes()
55 throws IOException, ParseException {
56 Path currentRelativePath = Paths.get("../aai-schema/src/main/resources/").toAbsolutePath();
58 Arrays.asList(currentRelativePath.toFile().listFiles(File::isDirectory));
59 List<String> multipleDefaultsPerList = new ArrayList<>();
60 for (File subDir : subDirs) {
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());
67 List<File> dbEdgeRulesFileList = new ArrayList<>();
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));
75 JSONParser jsonParser = new JSONParser();
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
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();
93 (rule.get("default") != null) ? rule.get("default").toString() : "false";
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;
107 dbEdgeRulesMapPut(dbEdgeRules, fromNode, toNode, direction, label, isDefault);
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());
121 if (!multipleDefaultsPerList.isEmpty()) {
122 fail(multipleDefaultsPerList.stream().collect(Collectors.joining()));
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
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
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))
144 if (from.compareTo(to) > 0) {
150 String edgeInfo = String.format("%s%s%s[%s]%s", from,
151 ((direction.equals("OUT")) ? "->" : "<-"), to, label, isDefault);
153 if ("false".equals(isDefault)) {
157 if (dbEdgeRules.containsKey(edgeInfo)) {
158 dbEdgeRules.put(edgeInfo, dbEdgeRules.get(edgeInfo) + 1);
160 dbEdgeRules.put(edgeInfo, 1);
166 private boolean isStringEmpty(String s) {
167 return (s == null || s.isEmpty()) ? true : false;