Update license date and text
[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.loadModels();
49         assertFalse( RelationshipSchemaLoader.getVersionContextMap().keySet().isEmpty());
50     }
51
52     @Test
53     public void loadModelsWithAVersion() throws Exception {
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.loadModels("v11");
62         String version = RelationshipSchemaLoader.getLatestSchemaVersion();
63         RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version);
64         assertNotNull(g.lookupRelationType("org.onap.relationships.inventory.BelongsTo"));
65     }
66
67     @Test
68     public void getSchemaForVersionFail() throws Exception {
69         RelationshipSchemaLoader.loadModels();
70         try {
71             RelationshipSchemaLoader.getSchemaForVersion("v1");
72         } catch (CrudException e) {
73             assertEquals(404, e.getHttpStatus().getStatusCode());
74         }
75     }
76
77     @Test
78     public void setVersionContextMap() throws Exception {
79         ArrayList<String> jsonString = new ArrayList<String>();
80         String rules = "{" +
81                 "\"rules\": [" +
82                 "{" +
83                 "\"from\": \"availability-zone\"," +
84                 "\"to\": \"complex\"," +
85                 "\"label\": \"groupsResourcesIn\"," +
86                 "\"direction\": \"OUT\"," +
87                 "\"multiplicity\": \"Many2Many\"," +
88                 "\"contains-other-v\": \"NONE\"," +
89                 "\"delete-other-v\": \"NONE\"," +
90                 "\"SVC-INFRA\": \"NONE\"," +
91                 "\"prevent-delete\": \"!${direction}\"" +
92                 "}]}";
93         String props = "{" +
94                 "  \"isParent\":\"java.lang.Boolean\"," +
95                 "  \"isParent-REV\":\"java.lang.Boolean\"," +
96                 "  \"usesResource\":\"java.lang.Boolean\"," +
97                 "  \"usesResource-REV\":\"java.lang.Boolean\"," +
98                 "  \"SVC-INFRA\":\"java.lang.Boolean\"," +
99                 "  \"SVC-INFRA-REV\":\"java.lang.Boolean\"," +
100                 "  \"hasDelTarget\":\"java.lang.Boolean\"," +
101                 "  \"hasDelTarget-REV\":\"java.lang.Boolean\"" +
102                 "}";
103         jsonString.add(rules);
104         jsonString.add(props);
105         RelationshipSchema nRs = new RelationshipSchema(jsonString);
106         Map<String, RelationshipSchema> versionMap = new HashMap<>();
107         versionMap.put("v1", nRs);
108         RelationshipSchemaLoader.setVersionContextMap(versionMap);
109         assertNotNull(RelationshipSchemaLoader.getSchemaForVersion("v1").lookupRelationType("groupsResourcesIn"));
110     }
111 }