Process multi-OXM files
[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.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.crud.exception.CrudException;
34
35 public class RelationshipSchemaLoaderTest {
36
37     @Before
38     public void init() throws Exception {
39         Path resourcePath = Paths.get(ClassLoader.getSystemResource("model").toURI());
40         Path parentPath = resourcePath.getParent();
41         System.setProperty("CONFIG_HOME", parentPath.toString());
42         RelationshipSchemaLoader.resetVersionContextMap();
43     }
44
45     @Test
46     public void loadModels() throws Exception {
47         RelationshipSchemaLoader.resetVersionContextMap();
48         RelationshipSchemaLoader.loadModels();
49         assertFalse( RelationshipSchemaLoader.getVersionContextMap().keySet().isEmpty());
50     }
51
52     @Test
53     public void loadModelsWithAVersion() throws Exception {
54         RelationshipSchemaLoader.resetVersionContextMap();
55         RelationshipSchemaLoader.loadModels("v11");
56         assertEquals(1, RelationshipSchemaLoader.getVersionContextMap().keySet().size());
57         assertEquals("v11",  RelationshipSchemaLoader.getLatestSchemaVersion());
58     }
59
60     @Test
61     public void getSchemaForVersion() throws Exception {
62         RelationshipSchemaLoader.resetVersionContextMap();
63         RelationshipSchemaLoader.loadModels("v11");
64         String version = RelationshipSchemaLoader.getLatestSchemaVersion();
65         RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version);
66         assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.BelongsTo"));
67     }
68
69     public void getSchemaForVersionManualFile() throws Exception {
70       RelationshipSchemaLoader.resetVersionContextMap();
71       RelationshipSchemaLoader.loadModels("v10");
72       String version = RelationshipSchemaLoader.getLatestSchemaVersion();
73       RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version);
74       assertNotNull(g.lookupRelationType("locatedIn"));
75     }
76
77
78     @Test
79     public void getSchemaForVersionFail() throws Exception {
80         RelationshipSchemaLoader.resetVersionContextMap();
81         RelationshipSchemaLoader.loadModels();
82         try {
83             RelationshipSchemaLoader.getSchemaForVersion("v1");
84         } catch (CrudException e) {
85             assertEquals(404, e.getHttpStatus().getStatusCode());
86         }
87     }
88
89     @Test
90     public void setVersionContextMap() throws Exception {
91         RelationshipSchemaLoader.resetVersionContextMap();
92         ArrayList<String> jsonString = new ArrayList<String>();
93         String rules = "{" +
94                 "\"rules\": [" +
95                 "{" +
96                 "\"from\": \"availability-zone\"," +
97                 "\"to\": \"complex\"," +
98                 "\"label\": \"groupsResourcesIn\"," +
99                 "\"direction\": \"OUT\"," +
100                 "\"multiplicity\": \"Many2Many\"," +
101                 "\"contains-other-v\": \"NONE\"," +
102                 "\"delete-other-v\": \"NONE\"," +
103                 "\"SVC-INFRA\": \"NONE\"," +
104                 "\"prevent-delete\": \"!${direction}\"" +
105                 "}]}";
106         String props = "{" +
107                 "  \"isParent\":\"java.lang.Boolean\"," +
108                 "  \"isParent-REV\":\"java.lang.Boolean\"," +
109                 "  \"usesResource\":\"java.lang.Boolean\"," +
110                 "  \"usesResource-REV\":\"java.lang.Boolean\"," +
111                 "  \"SVC-INFRA\":\"java.lang.Boolean\"," +
112                 "  \"SVC-INFRA-REV\":\"java.lang.Boolean\"," +
113                 "  \"hasDelTarget\":\"java.lang.Boolean\"," +
114                 "  \"hasDelTarget-REV\":\"java.lang.Boolean\"" +
115                 "}";
116         jsonString.add(rules);
117         jsonString.add(props);
118         RelationshipSchema nRs = new RelationshipSchema(jsonString);
119         Map<String, RelationshipSchema> versionMap = new HashMap<>();
120         versionMap.put("v1", nRs);
121         RelationshipSchemaLoader.setVersionContextMap(versionMap);
122         assertNotNull(RelationshipSchemaLoader.getSchemaForVersion("v1").lookupRelationType("groupsResourcesIn"));
123     }
124 }