Remove Multiplicity feature
[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.service.EdgePayload;
33 import org.onap.crud.util.CrudServiceUtil;
34 import org.junit.runner.RunWith;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.crud.OXMModelLoaderSetup;
37
38 @RunWith(MockitoJUnitRunner.Silent.class)
39 public class EdgeRulesLoaderTest extends OXMModelLoaderSetup{
40
41     @Test
42     public void loadModels() throws Exception {
43         EdgeRulesLoader.loadModels();
44         assertTrue(EdgeRulesLoader.getSchemaForVersion ( "v11" ).isValidType ( "org.onap.relationships.inventory.groupsResourcesIn" ));
45     }
46
47     @Test
48     public void loadModelsWithAVersion() throws Exception {
49         EdgeRulesLoader.resetSchemaVersionContext ();
50         EdgeRulesLoader.loadModels("V11");
51         assertEquals(1, EdgeRulesLoader.getSchemas ().size ());
52         assertEquals("v11", EdgeRulesLoader.getLatestSchemaVersion ());
53     }
54
55     @Test
56     public void getSchemaForVersion() throws Exception {
57         EdgeRulesLoader.resetSchemaVersionContext ();
58         EdgeRulesLoader.loadModels("v11");
59         String version = EdgeRulesLoader.getLatestSchemaVersion();
60         RelationshipSchema g = EdgeRulesLoader.getSchemaForVersion(version);
61         assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.groupsResourcesIn"));
62         assertNotNull(g.lookupRelation("U:V:org.onap.relationships.inventory.groupsResourcesIn"));
63         assertNull(g.lookupRelation("U:W:org.onap.relationships.inventory.groupsResourcesIn"));
64     }
65
66     @Test
67     public void getRelationshipTypeForNodePair() throws Exception {
68         EdgeRulesLoader.resetSchemaVersionContext();
69         EdgeRulesLoader.loadModels("v11");
70         RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion("v11");
71         
72         EdgePayload payload1 = new EdgePayload();
73         payload1.setSource("services/inventory/v11/availability-zone/xxx");
74         payload1.setTarget("services/inventory/v11/cloud-region/xxx");
75
76         EdgePayload payload2 = new EdgePayload();
77         payload2.setSource("services/inventory/v11/image/xxx");
78         payload2.setTarget("services/inventory/v11/pserver/xxx");
79         
80         EdgePayload payload3 = new EdgePayload();
81         payload3.setSource("services/inventory/v11/allotted-resource/xxx");
82         payload3.setTarget("services/inventory/v11/instance-group/xxx");
83         
84         // Get edge types for node pair with a single possible edge between them
85         Set<String> typeList = schema.getValidRelationTypes("availability-zone", "cloud-region");
86         assertEquals(1, typeList.size());
87         assertTrue(typeList.contains("org.onap.relationships.inventory.BelongsTo"));
88         assertEquals(CrudServiceUtil.determineEdgeType(payload1, "v11"), "org.onap.relationships.inventory.BelongsTo");
89         
90         // Get edge types for node pair with no possible edge between them
91         typeList = schema.getValidRelationTypes("image", "pserver");
92         assertEquals(0, typeList.size());
93         typeList = schema.getValidRelationTypes("cloud-region", "availability-zone");
94         assertEquals(0, typeList.size());
95         
96         try {
97           // Should throw an exception here
98           CrudServiceUtil.determineEdgeType(payload2, "v11");
99           assertTrue(false);
100         }
101         catch (CrudException ex) {
102           System.out.println(ex.getMessage());
103         }
104         
105         typeList = schema.getValidRelationTypes("allotted-resource", "instance-group");
106         assertEquals(2, typeList.size());
107         assertTrue(typeList.contains("org.onap.relationships.inventory.TestEdge"));
108         assertTrue(typeList.contains("org.onap.relationships.inventory.MemberOf"));
109         
110         for (String type : typeList) {
111           System.out.println(type);
112         }
113         
114         try {
115           // Should throw an exception here
116           CrudServiceUtil.determineEdgeType(payload3, "v11");
117           assertTrue(false);
118         }
119         catch (CrudException ex) {
120           System.out.println(ex.getMessage());
121         }
122     }
123
124     @Test
125     public void getSchemaForVersionFail() throws Exception {
126         EdgeRulesLoader.loadModels();
127         try {
128             EdgeRulesLoader.getSchemaForVersion("v1");
129         } catch (CrudException e) {
130             assertEquals(404, e.getHttpStatus().getStatusCode());
131         }
132     }
133 }