Allow ingestion of edge schema at deploy time
[aai/gizmo.git] / src / test / java / org / onap / schema / RelationshipSchemaLoaderTest.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.*;
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;
28
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import static org.junit.Assert.*;
35
36 public class RelationshipSchemaLoaderTest {
37
38     @Before
39     public void init() {
40         ClassLoader classLoader = getClass().getClassLoader();
41         File dir = new File(classLoader.getResource( "model").getFile());
42         System.setProperty("CONFIG_HOME", dir.getParent());
43         RelationshipSchemaLoader.resetVersionContextMap();
44     }
45
46     @Test
47     public void loadModels() throws Exception {
48         RelationshipSchemaLoader.resetVersionContextMap();
49         RelationshipSchemaLoader.loadModels();
50         assertFalse( RelationshipSchemaLoader.getVersionContextMap().keySet().isEmpty());
51     }
52
53     @Test
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());
59     }
60
61     @Test
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"));
68     }
69
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"));
76     }
77
78
79     @Test
80     public void getSchemaForVersionFail() throws Exception {
81         RelationshipSchemaLoader.resetVersionContextMap();
82         RelationshipSchemaLoader.loadModels();
83         try {
84             RelationshipSchemaLoader.getSchemaForVersion("v1");
85         } catch (CrudException e) {
86             assertEquals(404, e.getHttpStatus().getStatusCode());
87         }
88     }
89
90     @Test
91     public void setVersionContextMap() throws Exception {
92         RelationshipSchemaLoader.resetVersionContextMap();
93         ArrayList<String> jsonString = new ArrayList<String>();
94         String rules = "{" +
95                 "\"rules\": [" +
96                 "{" +
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}\"" +
106                 "}]}";
107         String props = "{" +
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\"" +
116                 "}";
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"));
124     }
125 }