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