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