Add required properties to schema
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / EdgeRulesTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.db;
23
24
25 import com.google.common.collect.Multimap;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.edges.EdgeIngestor;
32 import org.onap.aai.edges.EdgeRule;
33 import org.onap.aai.edges.EdgeRuleQuery;
34 import org.onap.aai.edges.enums.EdgeType;
35 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
36 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
37 import org.onap.aai.setup.SchemaVersion;
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 import java.util.*;
41 import java.util.stream.Collectors;
42
43 import static junit.framework.TestCase.fail;
44 import static org.junit.Assert.assertEquals;
45
46 public class EdgeRulesTest extends AAISetup {
47
48         //set thrown.expect to whatever a specific test needs
49         //this establishes a default of expecting no exceptions to be thrown
50         @Rule
51         public ExpectedException thrown = ExpectedException.none();
52
53         @Autowired
54         private EdgeIngestor edgeIngestor;
55
56         @Test
57         public void verifyOutDirection() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
58
59             EdgeRuleQuery ruleQuery = new EdgeRuleQuery
60             .Builder("cloud-region", "flavor")
61             .edgeType(EdgeType.TREE)
62             .build();
63
64                 EdgeRule rule = edgeIngestor.getRule(ruleQuery);
65
66                 assertEquals("out direction", rule.getDirection(), Direction.IN);
67         }
68
69         @Test
70         public void verifyOutFlippedDirection() throws EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
71
72         EdgeRuleQuery ruleQuery = new EdgeRuleQuery
73             .Builder("flavor", "cloud-region")
74             .edgeType(EdgeType.TREE)
75             .build();
76
77         EdgeRule rule = edgeIngestor.getRule(ruleQuery);
78
79                 assertEquals("in direction", rule.getDirection(), Direction.OUT);
80         }
81
82         @Test
83         public void verifyMultipleGet() throws EdgeRuleNotFoundException {
84
85         EdgeRuleQuery ruleQuery = new EdgeRuleQuery
86             .Builder("model-element", "model-ver")
87             .edgeType(EdgeType.TREE)
88             .build();
89
90                 Multimap<String, EdgeRule> ruleMap = edgeIngestor.getRules(ruleQuery);
91
92         for (EdgeRule edgeRule : ruleMap.get("model|model-ver")) {
93             assertEquals("has isA rule", "org.onap.relationships.inventory.IsA",
94                 edgeRule.getLabel());
95         }
96
97         }
98
99     @Test
100     public void verifyAllRules() throws EdgeRuleNotFoundException {
101
102         // This will cause every rule in the real json files to be verified
103         // so if any required properties are missing, the verification builds
104         // will catch it and incorrect rules can't get merged in.
105         for (SchemaVersion v : schemaVersions.getVersions()) {
106             Multimap<String, EdgeRule> all = edgeIngestor.getAllRules(schemaVersions.getDefaultVersion());
107
108             //this part verifies the default properties
109             // 1) can have only at most 1 containment edge between same node type pair
110             // 2) if there is at least 1 cousin edge, there must be exactly 1 cousin edge with default=true
111             for (String key : all.keySet()) {
112
113                 Collection<EdgeRule> edgeRuleCollection = all.get(key);
114
115                 boolean foundContainment = false; //can have at most 1 containment rel btwn same pair of node types
116                 boolean foundCousin = false;
117                 boolean cousinDefault = false; //if there is a cousin edge there must be at least 1 default cousin defined
118                 Set<String> labels = new HashSet<>(); //all edges between the same pair must have different labels
119                 int cousinCount = 0;
120
121                 for(EdgeRule rule: edgeRuleCollection){
122                     EdgeRule match = rule;
123                     //check containment
124                     if (!("NONE".equals(match.getContains()))) {
125                         if (foundContainment) {
126                             fail("more than one containment edge defined for " + v.toString() + " " + key);
127                         } else {
128                             foundContainment = true;
129                         }
130                     } else { //check cousin stuff
131                         foundCousin = true;
132                         cousinCount++;
133                         if (match.isDefault()) {
134                             if (!cousinDefault) {
135                                 cousinDefault = true;
136                             } else {
137                                 fail("more than one cousin edge defined as default for " + v.toString() + " " + key);
138                             }
139                         }
140                     }
141
142                     //check labels
143                     String label = match.getLabel();
144                     if (labels.contains(label)) {
145                         fail("same label found for multiple edges for " + v.toString() + " " + key);
146                     } else {
147                         labels.add(label);
148                     }
149                 }
150                 if (foundCousin && !cousinDefault && cousinCount > 1) {
151                     fail("there is at least one cousin edge but none are designated the default for " + v.toString() + " " + key);
152                 }
153             }
154         }
155     }
156 }