2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.schema;
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;
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;
35 public class EdgeRulesLoaderTest {
38 public void loadModels() throws Exception {
39 EdgeRulesLoader.loadModels();
40 assertTrue(EdgeRulesLoader.getSchemaForVersion ( "v11" ).isValidType ( "org.onap.relationships.inventory.groupsResourcesIn" ));
44 public void loadModelsWithAVersion() throws Exception {
45 EdgeRulesLoader.resetSchemaVersionContext ();
46 EdgeRulesLoader.loadModels("V11");
47 assertEquals(1, EdgeRulesLoader.getSchemas ().size ());
48 assertEquals("v11", EdgeRulesLoader.getLatestSchemaVersion ());
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"));
63 public void getRelationshipTypeForNodePair() throws Exception {
64 EdgeRulesLoader.resetSchemaVersionContext();
65 EdgeRulesLoader.loadModels("v11");
66 RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion("v11");
68 EdgePayload payload1 = new EdgePayload();
69 payload1.setSource("services/inventory/v11/availability-zone/xxx");
70 payload1.setTarget("services/inventory/v11/cloud-region/xxx");
72 EdgePayload payload2 = new EdgePayload();
73 payload2.setSource("services/inventory/v11/image/xxx");
74 payload2.setTarget("services/inventory/v11/pserver/xxx");
76 EdgePayload payload3 = new EdgePayload();
77 payload3.setSource("services/inventory/v11/allotted-resource/xxx");
78 payload3.setTarget("services/inventory/v11/instance-group/xxx");
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");
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());
93 // Should throw an exception here
94 CrudServiceUtil.determineEdgeType(payload2, "v11");
97 catch (CrudException ex) {
98 System.out.println(ex.getMessage());
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"));
106 for (String type : typeList) {
107 System.out.println(type);
111 // Should throw an exception here
112 CrudServiceUtil.determineEdgeType(payload3, "v11");
115 catch (CrudException ex) {
116 System.out.println(ex.getMessage());
121 public void getSchemaForVersionFail() throws Exception {
122 EdgeRulesLoader.loadModels();
124 EdgeRulesLoader.getSchemaForVersion("v1");
125 } catch (CrudException e) {
126 assertEquals(404, e.getHttpStatus().getStatusCode());