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