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;
34 import org.junit.runner.RunWith;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.crud.OXMModelLoaderSetup;
38 @RunWith(MockitoJUnitRunner.Silent.class)
39 public class EdgeRulesLoaderTest extends OXMModelLoaderSetup{
42 public void loadModels() throws Exception {
43 EdgeRulesLoader.loadModels();
44 assertTrue(EdgeRulesLoader.getSchemaForVersion ( "v11" ).isValidType ( "org.onap.relationships.inventory.groupsResourcesIn" ));
48 public void loadModelsWithAVersion() throws Exception {
49 EdgeRulesLoader.resetSchemaVersionContext ();
50 EdgeRulesLoader.loadModels("V11");
51 assertEquals(1, EdgeRulesLoader.getSchemas ().size ());
52 assertEquals("v11", EdgeRulesLoader.getLatestSchemaVersion ());
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"));
67 public void getRelationshipTypeForNodePair() throws Exception {
68 EdgeRulesLoader.resetSchemaVersionContext();
69 EdgeRulesLoader.loadModels("v11");
70 RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion("v11");
72 EdgePayload payload1 = new EdgePayload();
73 payload1.setSource("services/inventory/v11/availability-zone/xxx");
74 payload1.setTarget("services/inventory/v11/cloud-region/xxx");
76 EdgePayload payload2 = new EdgePayload();
77 payload2.setSource("services/inventory/v11/image/xxx");
78 payload2.setTarget("services/inventory/v11/pserver/xxx");
80 EdgePayload payload3 = new EdgePayload();
81 payload3.setSource("services/inventory/v11/allotted-resource/xxx");
82 payload3.setTarget("services/inventory/v11/instance-group/xxx");
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");
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());
97 // Should throw an exception here
98 CrudServiceUtil.determineEdgeType(payload2, "v11");
101 catch (CrudException ex) {
102 System.out.println(ex.getMessage());
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"));
110 for (String type : typeList) {
111 System.out.println(type);
115 // Should throw an exception here
116 CrudServiceUtil.determineEdgeType(payload3, "v11");
119 catch (CrudException ex) {
120 System.out.println(ex.getMessage());
125 public void getSchemaForVersionFail() throws Exception {
126 EdgeRulesLoader.loadModels();
128 EdgeRulesLoader.getSchemaForVersion("v1");
129 } catch (CrudException e) {
130 assertEquals(404, e.getHttpStatus().getStatusCode());