Using DbEdgeRules.json files from aai-core jar.
[aai/gizmo.git] / src / test / java / org / openecomp / schema / RelationshipSchemaLoaderTest.java
1 package org.openecomp.schema;
2
3 import static org.junit.Assert.*;
4 import edu.emory.mathcs.backport.java.util.Arrays;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.openecomp.crud.exception.CrudException;
8
9 import java.io.File;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import static org.junit.Assert.*;
15
16 public class RelationshipSchemaLoaderTest {
17
18     @Before
19     public void init() {
20         ClassLoader classLoader = getClass().getClassLoader();
21         File dir = new File(classLoader.getResource( "model").getFile());
22         System.setProperty("CONFIG_HOME", dir.getParent());
23         RelationshipSchemaLoader.resetVersionContextMap();
24     }
25
26     @Test
27     public void loadModels() throws Exception {
28         RelationshipSchemaLoader.loadModels();
29         assertFalse( RelationshipSchemaLoader.getVersionContextMap().keySet().isEmpty());
30     }
31
32     @Test
33     public void loadModelsWithAVersion() throws Exception {
34         RelationshipSchemaLoader.loadModels("v11");
35         assertEquals(1, RelationshipSchemaLoader.getVersionContextMap().keySet().size());
36         assertEquals("v11",  RelationshipSchemaLoader.getLatestSchemaVersion());
37     }
38
39     @Test
40     public void getSchemaForVersion() throws Exception {
41         RelationshipSchemaLoader.loadModels("v11");
42         String version = RelationshipSchemaLoader.getLatestSchemaVersion();
43         RelationshipSchema g = RelationshipSchemaLoader.getSchemaForVersion(version);
44         assertNotNull(g.lookupRelationType("has"));
45     }
46
47     @Test
48     public void getSchemaForVersionFail() throws Exception {
49         RelationshipSchemaLoader.loadModels();
50         try {
51             RelationshipSchemaLoader.getSchemaForVersion("v1");
52         } catch (CrudException e) {
53             assertEquals(404, e.getHttpStatus().getStatusCode());
54         }
55     }
56
57     @Test
58     public void setVersionContextMap() throws Exception {
59         ArrayList<String> jsonString = new ArrayList<String>();
60         String rules = "{" +
61                 "\"rules\": [" +
62                 "{" +
63                 "\"from\": \"availability-zone\"," +
64                 "\"to\": \"complex\"," +
65                 "\"label\": \"groupsResourcesIn\"," +
66                 "\"direction\": \"OUT\"," +
67                 "\"multiplicity\": \"Many2Many\"," +
68                 "\"contains-other-v\": \"NONE\"," +
69                 "\"delete-other-v\": \"NONE\"," +
70                 "\"SVC-INFRA\": \"NONE\"," +
71                 "\"prevent-delete\": \"!${direction}\"" +
72                 "}]}";
73         String props = "{" +
74                 "  \"isParent\":\"java.lang.Boolean\"," +
75                 "  \"isParent-REV\":\"java.lang.Boolean\"," +
76                 "  \"usesResource\":\"java.lang.Boolean\"," +
77                 "  \"usesResource-REV\":\"java.lang.Boolean\"," +
78                 "  \"SVC-INFRA\":\"java.lang.Boolean\"," +
79                 "  \"SVC-INFRA-REV\":\"java.lang.Boolean\"," +
80                 "  \"hasDelTarget\":\"java.lang.Boolean\"," +
81                 "  \"hasDelTarget-REV\":\"java.lang.Boolean\"" +
82                 "}";
83         jsonString.add(rules);
84         jsonString.add(props);
85         RelationshipSchema nRs = new RelationshipSchema(jsonString);
86         Map<String, RelationshipSchema> versionMap = new HashMap<>();
87         versionMap.put("v1", nRs);
88         RelationshipSchemaLoader.setVersionContextMap(versionMap);
89         assertNotNull(RelationshipSchemaLoader.getSchemaForVersion("v1").lookupRelationType("groupsResourcesIn"));
90     }
91 }