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.*;
24 import edu.emory.mathcs.backport.java.util.Arrays;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onap.crud.exception.CrudException;
30 import java.util.ArrayList;
31 import java.util.HashMap;
34 import static org.junit.Assert.*;
36 public class RelationshipSchemaLoaderTest {
40 ClassLoader classLoader = getClass().getClassLoader();
41 File dir = new File(classLoader.getResource( "model").getFile());
42 System.setProperty("CONFIG_HOME", dir.getParent());
43 RelationshipSchemaLoader.resetVersionContextMap();
47 public void loadModels() throws Exception {
48 RelationshipSchemaLoader.resetVersionContextMap();
49 RelationshipSchemaLoader.loadModels();
50 assertFalse( RelationshipSchemaLoader.getVersionContextMap().keySet().isEmpty());
54 public void loadModelsWithAVersion() throws Exception {
55 RelationshipSchemaLoader.resetVersionContextMap();
56 RelationshipSchemaLoader.loadModels("v11");
57 assertEquals(1, RelationshipSchemaLoader.getVersionContextMap().keySet().size());
58 assertEquals("v11", RelationshipSchemaLoader.getLatestSchemaVersion());
62 public void getSchemaForVersion() throws Exception {
63 RelationshipSchemaLoader.resetVersionContextMap();
64 RelationshipSchemaLoader.loadModels("v11");
65 String version = RelationshipSchemaLoader.getLatestSchemaVersion();
66 RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version);
67 assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.BelongsTo"));
70 public void getSchemaForVersionManualFile() throws Exception {
71 RelationshipSchemaLoader.resetVersionContextMap();
72 RelationshipSchemaLoader.loadModels("v10");
73 String version = RelationshipSchemaLoader.getLatestSchemaVersion();
74 RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version);
75 assertNotNull(g.lookupRelationType("locatedIn"));
80 public void getSchemaForVersionFail() throws Exception {
81 RelationshipSchemaLoader.resetVersionContextMap();
82 RelationshipSchemaLoader.loadModels();
84 RelationshipSchemaLoader.getSchemaForVersion("v1");
85 } catch (CrudException e) {
86 assertEquals(404, e.getHttpStatus().getStatusCode());
91 public void setVersionContextMap() throws Exception {
92 RelationshipSchemaLoader.resetVersionContextMap();
93 ArrayList<String> jsonString = new ArrayList<String>();
97 "\"from\": \"availability-zone\"," +
98 "\"to\": \"complex\"," +
99 "\"label\": \"groupsResourcesIn\"," +
100 "\"direction\": \"OUT\"," +
101 "\"multiplicity\": \"Many2Many\"," +
102 "\"contains-other-v\": \"NONE\"," +
103 "\"delete-other-v\": \"NONE\"," +
104 "\"SVC-INFRA\": \"NONE\"," +
105 "\"prevent-delete\": \"!${direction}\"" +
108 " \"isParent\":\"java.lang.Boolean\"," +
109 " \"isParent-REV\":\"java.lang.Boolean\"," +
110 " \"usesResource\":\"java.lang.Boolean\"," +
111 " \"usesResource-REV\":\"java.lang.Boolean\"," +
112 " \"SVC-INFRA\":\"java.lang.Boolean\"," +
113 " \"SVC-INFRA-REV\":\"java.lang.Boolean\"," +
114 " \"hasDelTarget\":\"java.lang.Boolean\"," +
115 " \"hasDelTarget-REV\":\"java.lang.Boolean\"" +
117 jsonString.add(rules);
118 jsonString.add(props);
119 RelationshipSchema nRs = new RelationshipSchema(jsonString);
120 Map<String, RelationshipSchema> versionMap = new HashMap<>();
121 versionMap.put("v1", nRs);
122 RelationshipSchemaLoader.setVersionContextMap(versionMap);
123 assertNotNull(RelationshipSchemaLoader.getSchemaForVersion("v1").lookupRelationType("groupsResourcesIn"));