Auto-resolve edge type
[aai/gizmo.git] / src / test / java / org / onap / schema / EdgeRulesLoaderTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.schema;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.Set;
29
30 import org.junit.Test;
31 import org.onap.crud.exception.CrudException;
32 import org.onap.crud.parser.EdgePayload;
33 import org.onap.crud.util.CrudServiceUtil;
34
35 public class EdgeRulesLoaderTest {
36
37     @Test
38     public void loadModels() throws Exception {
39         EdgeRulesLoader.loadModels();
40         assertTrue(EdgeRulesLoader.getSchemaForVersion ( "v11" ).isValidType ( "org.onap.relationships.inventory.groupsResourcesIn" ));
41     }
42
43     @Test
44     public void loadModelsWithAVersion() throws Exception {
45         EdgeRulesLoader.resetSchemaVersionContext ();
46         EdgeRulesLoader.loadModels("V11");
47         assertEquals(1, EdgeRulesLoader.getSchemas ().size ());
48         assertEquals("v11", EdgeRulesLoader.getLatestSchemaVersion ());
49     }
50
51     @Test
52     public void getSchemaForVersion() throws Exception {
53         EdgeRulesLoader.resetSchemaVersionContext ();
54         EdgeRulesLoader.loadModels("v11");
55         String version = EdgeRulesLoader.getLatestSchemaVersion();
56         RelationshipSchema g = EdgeRulesLoader.getSchemaForVersion(version);
57         assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.groupsResourcesIn"));
58         assertNotNull(g.lookupRelation("U:V:org.onap.relationships.inventory.groupsResourcesIn"));
59         assertNull(g.lookupRelation("U:W:org.onap.relationships.inventory.groupsResourcesIn"));
60     }
61
62     @Test
63     public void getRelationshipTypeForNodePair() throws Exception {
64         EdgeRulesLoader.resetSchemaVersionContext();
65         EdgeRulesLoader.loadModels("v11");
66         RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion("v11");
67         
68         EdgePayload payload1 = new EdgePayload();
69         payload1.setSource("services/inventory/v11/availability-zone/xxx");
70         payload1.setTarget("services/inventory/v11/cloud-region/xxx");
71
72         EdgePayload payload2 = new EdgePayload();
73         payload2.setSource("services/inventory/v11/image/xxx");
74         payload2.setTarget("services/inventory/v11/pserver/xxx");
75         
76         EdgePayload payload3 = new EdgePayload();
77         payload3.setSource("services/inventory/v11/allotted-resource/xxx");
78         payload3.setTarget("services/inventory/v11/instance-group/xxx");
79         
80         // Get edge types for node pair with a single possible edge between them
81         Set<String> typeList = schema.getValidRelationTypes("availability-zone", "cloud-region");
82         assertEquals(1, typeList.size());
83         assertTrue(typeList.contains("org.onap.relationships.inventory.BelongsTo"));
84         assertEquals(CrudServiceUtil.determineEdgeType(payload1, "v11"), "org.onap.relationships.inventory.BelongsTo");
85         
86         // Get edge types for node pair with no possible edge between them
87         typeList = schema.getValidRelationTypes("image", "pserver");
88         assertEquals(0, typeList.size());
89         typeList = schema.getValidRelationTypes("cloud-region", "availability-zone");
90         assertEquals(0, typeList.size());
91         
92         try {
93           // Should throw an exception here
94           CrudServiceUtil.determineEdgeType(payload2, "v11");
95           assertTrue(false);
96         }
97         catch (CrudException ex) {
98           System.out.println(ex.getMessage());
99         }
100         
101         typeList = schema.getValidRelationTypes("allotted-resource", "instance-group");
102         assertEquals(2, typeList.size());
103         assertTrue(typeList.contains("org.onap.relationships.inventory.TestEdge"));
104         assertTrue(typeList.contains("org.onap.relationships.inventory.MemberOf"));
105         
106         for (String type : typeList) {
107           System.out.println(type);
108         }
109         
110         try {
111           // Should throw an exception here
112           CrudServiceUtil.determineEdgeType(payload3, "v11");
113           assertTrue(false);
114         }
115         catch (CrudException ex) {
116           System.out.println(ex.getMessage());
117         }
118     }
119
120     @Test
121     public void getSchemaForVersionFail() throws Exception {
122         EdgeRulesLoader.loadModels();
123         try {
124             EdgeRulesLoader.getSchemaForVersion("v1");
125         } catch (CrudException e) {
126             assertEquals(404, e.getHttpStatus().getStatusCode());
127         }
128     }
129 }